annotate tests/test_mesh.nim @ 729:7b72da29ad8d

update shader to work correctly
author Sam <sam@basx.dev>
date Sun, 28 May 2023 18:11:17 +0700
parents 5f7ec8d1bd33
children b5fb27b0f7a4
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
683
2ca938595aea add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff changeset
1 import semicongine
2ca938595aea add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff changeset
2
2ca938595aea add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff changeset
3 proc main() =
708
3bb199dd45ba fix: gltf loading
Sam <sam@basx.dev>
parents: 707
diff changeset
4 var ent1 = newEntity("hoho", rect())
3bb199dd45ba fix: gltf loading
Sam <sam@basx.dev>
parents: 707
diff changeset
5 var ent2 = newEntity("hehe", ent1)
3bb199dd45ba fix: gltf loading
Sam <sam@basx.dev>
parents: 707
diff changeset
6 var myScene = newScene("hi", ent2)
3bb199dd45ba fix: gltf loading
Sam <sam@basx.dev>
parents: 707
diff changeset
7 myScene.root.transform = translate3d(0.2'f32, 0'f32, 0'f32)
3bb199dd45ba fix: gltf loading
Sam <sam@basx.dev>
parents: 707
diff changeset
8 myScene.root.children[0].transform = translate3d(0'f32, 0.2'f32, 0'f32)
700
abc0d97392cb did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 691
diff changeset
9 var scenes = [
729
7b72da29ad8d update shader to work correctly
Sam <sam@basx.dev>
parents: 714
diff changeset
10 # loadScene("default_cube.glb", "1"),
708
3bb199dd45ba fix: gltf loading
Sam <sam@basx.dev>
parents: 707
diff changeset
11 # loadScene("default_cube1.glb", "3"),
3bb199dd45ba fix: gltf loading
Sam <sam@basx.dev>
parents: 707
diff changeset
12 # loadScene("default_cube2.glb", "4"),
3bb199dd45ba fix: gltf loading
Sam <sam@basx.dev>
parents: 707
diff changeset
13 # loadScene("flat.glb", "5"),
729
7b72da29ad8d update shader to work correctly
Sam <sam@basx.dev>
parents: 714
diff changeset
14 loadScene("tutorialk-donat.glb", "6"),
708
3bb199dd45ba fix: gltf loading
Sam <sam@basx.dev>
parents: 707
diff changeset
15 # myScene,
3bb199dd45ba fix: gltf loading
Sam <sam@basx.dev>
parents: 707
diff changeset
16 # loadScene("personv3.glb", "2"),
700
abc0d97392cb did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 691
diff changeset
17 ]
683
2ca938595aea add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff changeset
18
2ca938595aea add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff changeset
19 var engine = initEngine("Test meshes")
2ca938595aea add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff changeset
20 const
700
abc0d97392cb did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 691
diff changeset
21 vertexInput = @[
abc0d97392cb did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 691
diff changeset
22 attr[Vec3f]("position", memoryPerformanceHint=PreferFastRead),
abc0d97392cb did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 691
diff changeset
23 attr[uint8]("material", memoryPerformanceHint=PreferFastRead),
708
3bb199dd45ba fix: gltf loading
Sam <sam@basx.dev>
parents: 707
diff changeset
24 attr[Vec2f]("texcoord_0", memoryPerformanceHint=PreferFastRead),
3bb199dd45ba fix: gltf loading
Sam <sam@basx.dev>
parents: 707
diff changeset
25 attr[Mat4]("transform", memoryPerformanceHint=PreferFastWrite, perInstance=true),
700
abc0d97392cb did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 691
diff changeset
26 ]
729
7b72da29ad8d update shader to work correctly
Sam <sam@basx.dev>
parents: 714
diff changeset
27 vertexOutput = @[
7b72da29ad8d update shader to work correctly
Sam <sam@basx.dev>
parents: 714
diff changeset
28 attr[Vec4f]("vertexColor"),
7b72da29ad8d update shader to work correctly
Sam <sam@basx.dev>
parents: 714
diff changeset
29 attr[Vec2f]("colorTexCoord"),
7b72da29ad8d update shader to work correctly
Sam <sam@basx.dev>
parents: 714
diff changeset
30 attr[uint8]("materialId", noInterpolation=true)
7b72da29ad8d update shader to work correctly
Sam <sam@basx.dev>
parents: 714
diff changeset
31 ]
683
2ca938595aea add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff changeset
32 fragOutput = @[attr[Vec4f]("color")]
706
0936ae49f0c4 add: correct camera calculations
Sam <sam@basx.dev>
parents: 705
diff changeset
33 uniforms = @[
0936ae49f0c4 add: correct camera calculations
Sam <sam@basx.dev>
parents: 705
diff changeset
34 attr[Mat4]("projection"),
0936ae49f0c4 add: correct camera calculations
Sam <sam@basx.dev>
parents: 705
diff changeset
35 attr[Mat4]("view"),
729
7b72da29ad8d update shader to work correctly
Sam <sam@basx.dev>
parents: 714
diff changeset
36 attr[Vec4f]("baseColorFactor", arrayCount=4),
706
0936ae49f0c4 add: correct camera calculations
Sam <sam@basx.dev>
parents: 705
diff changeset
37 ]
708
3bb199dd45ba fix: gltf loading
Sam <sam@basx.dev>
parents: 707
diff changeset
38 samplers = @[
729
7b72da29ad8d update shader to work correctly
Sam <sam@basx.dev>
parents: 714
diff changeset
39 attr[Sampler2DType]("baseColorTexture", arrayCount=4),
708
3bb199dd45ba fix: gltf loading
Sam <sam@basx.dev>
parents: 707
diff changeset
40 ]
683
2ca938595aea add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff changeset
41 vertexCode = compileGlslShader(
2ca938595aea add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff changeset
42 stage=VK_SHADER_STAGE_VERTEX_BIT,
2ca938595aea add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff changeset
43 inputs=vertexInput,
700
abc0d97392cb did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 691
diff changeset
44 outputs=vertexOutput,
683
2ca938595aea add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff changeset
45 uniforms=uniforms,
708
3bb199dd45ba fix: gltf loading
Sam <sam@basx.dev>
parents: 707
diff changeset
46 samplers=samplers,
3bb199dd45ba fix: gltf loading
Sam <sam@basx.dev>
parents: 707
diff changeset
47 main="""
3bb199dd45ba fix: gltf loading
Sam <sam@basx.dev>
parents: 707
diff changeset
48 gl_Position = vec4(position, 1.0) * (transform * Uniforms.view * Uniforms.projection);
729
7b72da29ad8d update shader to work correctly
Sam <sam@basx.dev>
parents: 714
diff changeset
49 vertexColor = Uniforms.baseColorFactor[material];
7b72da29ad8d update shader to work correctly
Sam <sam@basx.dev>
parents: 714
diff changeset
50 colorTexCoord = texcoord_0;
7b72da29ad8d update shader to work correctly
Sam <sam@basx.dev>
parents: 714
diff changeset
51 materialId = material;
708
3bb199dd45ba fix: gltf loading
Sam <sam@basx.dev>
parents: 707
diff changeset
52 """
683
2ca938595aea add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff changeset
53 )
2ca938595aea add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff changeset
54 fragmentCode = compileGlslShader(
2ca938595aea add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff changeset
55 stage=VK_SHADER_STAGE_FRAGMENT_BIT,
700
abc0d97392cb did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 691
diff changeset
56 inputs=vertexOutput,
683
2ca938595aea add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff changeset
57 outputs=fragOutput,
2ca938595aea add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff changeset
58 uniforms=uniforms,
708
3bb199dd45ba fix: gltf loading
Sam <sam@basx.dev>
parents: 707
diff changeset
59 samplers=samplers,
729
7b72da29ad8d update shader to work correctly
Sam <sam@basx.dev>
parents: 714
diff changeset
60 main="""
7b72da29ad8d update shader to work correctly
Sam <sam@basx.dev>
parents: 714
diff changeset
61 vec4 col[4] = vec4[4](vec4(1, 0, 0, 1), vec4(0, 1, 0, 1), vec4(0, 0, 1, 1), vec4(1, 1, 1, 1));
7b72da29ad8d update shader to work correctly
Sam <sam@basx.dev>
parents: 714
diff changeset
62 color = texture(baseColorTexture[materialId], colorTexCoord) * vertexColor;
7b72da29ad8d update shader to work correctly
Sam <sam@basx.dev>
parents: 714
diff changeset
63 """
683
2ca938595aea add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff changeset
64 )
700
abc0d97392cb did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 691
diff changeset
65 engine.setRenderer(engine.gpuDevice.simpleForwardRenderPass(vertexCode, fragmentCode, clearColor=newVec4f(0, 0, 0, 1)))
abc0d97392cb did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 691
diff changeset
66 for scene in scenes.mitems:
714
5f7ec8d1bd33 fix: API changes
sam <sam@basx.dev>
parents: 708
diff changeset
67 engine.addScene(scene, vertexInput, samplers, transformAttribute="transform")
706
0936ae49f0c4 add: correct camera calculations
Sam <sam@basx.dev>
parents: 705
diff changeset
68 scene.addShaderGlobal("projection", Unit4)
0936ae49f0c4 add: correct camera calculations
Sam <sam@basx.dev>
parents: 705
diff changeset
69 scene.addShaderGlobal("view", Unit4)
700
abc0d97392cb did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 691
diff changeset
70 var
708
3bb199dd45ba fix: gltf loading
Sam <sam@basx.dev>
parents: 707
diff changeset
71 size = 1'f32
707
3742bba2293f fix: better defaults
Sam <sam@basx.dev>
parents: 706
diff changeset
72 elevation = 0'f32
700
abc0d97392cb did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 691
diff changeset
73 azimut = 0'f32
abc0d97392cb did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 691
diff changeset
74 currentScene = 0
abc0d97392cb did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 691
diff changeset
75
683
2ca938595aea add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff changeset
76 while engine.updateInputs() == Running and not engine.keyIsDown(Escape):
700
abc0d97392cb did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 691
diff changeset
77 if engine.keyWasPressed(`1`):
abc0d97392cb did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 691
diff changeset
78 currentScene = 0
abc0d97392cb did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 691
diff changeset
79 elif engine.keyWasPressed(`2`):
abc0d97392cb did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 691
diff changeset
80 currentScene = 1
abc0d97392cb did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 691
diff changeset
81 elif engine.keyWasPressed(`3`):
abc0d97392cb did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 691
diff changeset
82 currentScene = 2
abc0d97392cb did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 691
diff changeset
83 elif engine.keyWasPressed(`4`):
abc0d97392cb did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 691
diff changeset
84 currentScene = 3
abc0d97392cb did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 691
diff changeset
85 elif engine.keyWasPressed(`5`):
abc0d97392cb did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 691
diff changeset
86 currentScene = 4
abc0d97392cb did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 691
diff changeset
87 elif engine.keyWasPressed(`6`):
abc0d97392cb did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 691
diff changeset
88 currentScene = 5
abc0d97392cb did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 691
diff changeset
89
abc0d97392cb did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 691
diff changeset
90 if engine.keyWasPressed(NumberRowExtra3):
707
3742bba2293f fix: better defaults
Sam <sam@basx.dev>
parents: 706
diff changeset
91 size = 0.3'f32
3742bba2293f fix: better defaults
Sam <sam@basx.dev>
parents: 706
diff changeset
92 elevation = 0'f32
700
abc0d97392cb did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 691
diff changeset
93 azimut = 0'f32
abc0d97392cb did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 691
diff changeset
94
707
3742bba2293f fix: better defaults
Sam <sam@basx.dev>
parents: 706
diff changeset
95 let ratio = engine.getWindow().size[0] / engine.getWindow().size[1]
700
abc0d97392cb did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 691
diff changeset
96 size *= 1'f32 + engine.mouseWheel() * 0.05
abc0d97392cb did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 691
diff changeset
97 azimut += engine.mouseMove().x / 180'f32
abc0d97392cb did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 691
diff changeset
98 elevation -= engine.mouseMove().y / 180'f32
706
0936ae49f0c4 add: correct camera calculations
Sam <sam@basx.dev>
parents: 705
diff changeset
99 scenes[currentScene].setShaderGlobal("projection", ortho(-ratio, ratio, -1, 1, -1, 1))
700
abc0d97392cb did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 691
diff changeset
100 scenes[currentScene].setShaderGlobal(
706
0936ae49f0c4 add: correct camera calculations
Sam <sam@basx.dev>
parents: 705
diff changeset
101 "view",
0936ae49f0c4 add: correct camera calculations
Sam <sam@basx.dev>
parents: 705
diff changeset
102 scale3d(size, size, size) * rotate3d(elevation, newVec3f(1, 0, 0)) * rotate3d(azimut, Yf32)
691
9182a5d2ea3a add: test mesh
Sam <sam@basx.dev>
parents: 683
diff changeset
103 )
700
abc0d97392cb did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 691
diff changeset
104 engine.renderScene(scenes[currentScene])
683
2ca938595aea add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff changeset
105 engine.destroy()
2ca938595aea add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff changeset
106
2ca938595aea add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff changeset
107 when isMainModule:
2ca938595aea add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff changeset
108 main()