annotate tests/test_mesh.nim @ 706:0936ae49f0c4

add: correct camera calculations
author Sam <sam@basx.dev>
date Mon, 22 May 2023 19:21:05 +0700
parents 37ef17c96bdb
children dcbd9f256f6a
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() =
700
abc0d97392cb did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 691
diff changeset
4 var scenes = [
abc0d97392cb did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 691
diff changeset
5 loadScene("default_cube.glb", "1"),
abc0d97392cb did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 691
diff changeset
6 loadScene("default_cube1.glb", "3"),
abc0d97392cb did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 691
diff changeset
7 loadScene("default_cube2.glb", "4"),
abc0d97392cb did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 691
diff changeset
8 loadScene("flat.glb", "5"),
abc0d97392cb did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 691
diff changeset
9 loadScene("tutorialk-donat.glb", "6"),
abc0d97392cb did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 691
diff changeset
10 loadScene("personv3.glb", "2"),
abc0d97392cb did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 691
diff changeset
11 ]
683
2ca938595aea add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff changeset
12
2ca938595aea add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff changeset
13 var engine = initEngine("Test meshes")
2ca938595aea add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff changeset
14 const
700
abc0d97392cb did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 691
diff changeset
15 vertexInput = @[
abc0d97392cb did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 691
diff changeset
16 attr[Vec3f]("position", memoryPerformanceHint=PreferFastRead),
abc0d97392cb did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 691
diff changeset
17 attr[uint8]("material", memoryPerformanceHint=PreferFastRead),
abc0d97392cb did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 691
diff changeset
18 ]
abc0d97392cb did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 691
diff changeset
19 vertexOutput = @[attr[Vec4f]("vertexColor")]
683
2ca938595aea add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff changeset
20 fragOutput = @[attr[Vec4f]("color")]
706
0936ae49f0c4 add: correct camera calculations
Sam <sam@basx.dev>
parents: 705
diff changeset
21 uniforms = @[
0936ae49f0c4 add: correct camera calculations
Sam <sam@basx.dev>
parents: 705
diff changeset
22 attr[Mat4]("projection"),
0936ae49f0c4 add: correct camera calculations
Sam <sam@basx.dev>
parents: 705
diff changeset
23 attr[Mat4]("view"),
0936ae49f0c4 add: correct camera calculations
Sam <sam@basx.dev>
parents: 705
diff changeset
24 attr[Mat4]("model"),
0936ae49f0c4 add: correct camera calculations
Sam <sam@basx.dev>
parents: 705
diff changeset
25 attr[Vec4f]("material_color", arrayCount=16),
0936ae49f0c4 add: correct camera calculations
Sam <sam@basx.dev>
parents: 705
diff changeset
26 ]
683
2ca938595aea add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff changeset
27 vertexCode = compileGlslShader(
2ca938595aea add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff changeset
28 stage=VK_SHADER_STAGE_VERTEX_BIT,
2ca938595aea add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff changeset
29 inputs=vertexInput,
700
abc0d97392cb did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 691
diff changeset
30 outputs=vertexOutput,
683
2ca938595aea add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff changeset
31 uniforms=uniforms,
706
0936ae49f0c4 add: correct camera calculations
Sam <sam@basx.dev>
parents: 705
diff changeset
32 main="""gl_Position = Uniforms.projection * Uniforms.view * Uniforms.model * vec4(position, 1.0); vertexColor = Uniforms.material_color[material];"""
683
2ca938595aea add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff changeset
33 )
2ca938595aea add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff changeset
34 fragmentCode = compileGlslShader(
2ca938595aea add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff changeset
35 stage=VK_SHADER_STAGE_FRAGMENT_BIT,
700
abc0d97392cb did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 691
diff changeset
36 inputs=vertexOutput,
683
2ca938595aea add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff changeset
37 outputs=fragOutput,
2ca938595aea add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff changeset
38 uniforms=uniforms,
700
abc0d97392cb did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 691
diff changeset
39 main="""color = vertexColor;"""
683
2ca938595aea add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff changeset
40 )
700
abc0d97392cb did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 691
diff changeset
41 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
42 for scene in scenes.mitems:
abc0d97392cb did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 691
diff changeset
43 engine.addScene(scene, vertexInput)
706
0936ae49f0c4 add: correct camera calculations
Sam <sam@basx.dev>
parents: 705
diff changeset
44 scene.addShaderGlobal("projection", Unit4)
0936ae49f0c4 add: correct camera calculations
Sam <sam@basx.dev>
parents: 705
diff changeset
45 scene.addShaderGlobal("view", Unit4)
0936ae49f0c4 add: correct camera calculations
Sam <sam@basx.dev>
parents: 705
diff changeset
46 scene.addShaderGlobal("model", Unit4)
700
abc0d97392cb did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 691
diff changeset
47 var
abc0d97392cb did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 691
diff changeset
48 size = 1'f32
abc0d97392cb did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 691
diff changeset
49 elevation = -float32(PI) / 3'f32
abc0d97392cb did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 691
diff changeset
50 azimut = 0'f32
abc0d97392cb did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 691
diff changeset
51 currentScene = 0
abc0d97392cb did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 691
diff changeset
52
683
2ca938595aea add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff changeset
53 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
54 if engine.keyWasPressed(`1`):
abc0d97392cb did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 691
diff changeset
55 currentScene = 0
abc0d97392cb did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 691
diff changeset
56 elif engine.keyWasPressed(`2`):
abc0d97392cb did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 691
diff changeset
57 currentScene = 1
abc0d97392cb did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 691
diff changeset
58 elif engine.keyWasPressed(`3`):
abc0d97392cb did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 691
diff changeset
59 currentScene = 2
abc0d97392cb did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 691
diff changeset
60 elif engine.keyWasPressed(`4`):
abc0d97392cb did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 691
diff changeset
61 currentScene = 3
abc0d97392cb did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 691
diff changeset
62 elif engine.keyWasPressed(`5`):
abc0d97392cb did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 691
diff changeset
63 currentScene = 4
abc0d97392cb did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 691
diff changeset
64 elif engine.keyWasPressed(`6`):
abc0d97392cb did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 691
diff changeset
65 currentScene = 5
abc0d97392cb did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 691
diff changeset
66
abc0d97392cb did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 691
diff changeset
67 if engine.keyWasPressed(NumberRowExtra3):
abc0d97392cb did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 691
diff changeset
68 size = 1'f32
abc0d97392cb did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 691
diff changeset
69 elevation = -float32(PI) / 3'f32
abc0d97392cb did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 691
diff changeset
70 azimut = 0'f32
706
0936ae49f0c4 add: correct camera calculations
Sam <sam@basx.dev>
parents: 705
diff changeset
71 var 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
72
abc0d97392cb did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 691
diff changeset
73 size *= 1'f32 + engine.mouseWheel() * 0.05
abc0d97392cb did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 691
diff changeset
74 azimut += engine.mouseMove().x / 180'f32
abc0d97392cb did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 691
diff changeset
75 elevation -= engine.mouseMove().y / 180'f32
706
0936ae49f0c4 add: correct camera calculations
Sam <sam@basx.dev>
parents: 705
diff changeset
76 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
77 scenes[currentScene].setShaderGlobal(
706
0936ae49f0c4 add: correct camera calculations
Sam <sam@basx.dev>
parents: 705
diff changeset
78 "view",
0936ae49f0c4 add: correct camera calculations
Sam <sam@basx.dev>
parents: 705
diff changeset
79 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
80 )
706
0936ae49f0c4 add: correct camera calculations
Sam <sam@basx.dev>
parents: 705
diff changeset
81 scenes[currentScene].setShaderGlobal("model", Unit4f32)
700
abc0d97392cb did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 691
diff changeset
82 engine.renderScene(scenes[currentScene])
683
2ca938595aea add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff changeset
83 engine.destroy()
2ca938595aea add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff changeset
84
2ca938595aea add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff changeset
85
2ca938595aea add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff changeset
86 when isMainModule:
2ca938595aea add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff changeset
87 main()