annotate tests/test_mesh.nim @ 245:a19c9d089f25

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