annotate tests/test_mesh.nim @ 1052:b8d20f75ecef

add: some sync (not finished), simplify renderpass af
author sam <sam@basx.dev>
date Sat, 30 Mar 2024 20:29:33 +0700
parents 91e018270832
children 6406766a222d
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 = [
373
f4f1474dc70a fix: make tests working again
Sam <sam@basx.dev>
parents: 371
diff changeset
5 Scene(name: "Donut", meshes: loadMeshes("donut.glb", COLORED_SINGLE_TEXTURE_MATERIAL)[0].toSeq),
239
f05497ef8fd2 did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 230
diff changeset
6 ]
222
ddfc54036e00 add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff changeset
7
ddfc54036e00 add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff changeset
8 var engine = initEngine("Test meshes")
ddfc54036e00 add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff changeset
9 const
333
27aaf43e18b4 fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents: 318
diff changeset
10 shaderConfiguration = createShaderConfiguration(
420
91e018270832 fix: all tests (once more)
Sam <sam@basx.dev>
parents: 373
diff changeset
11 inputs = [
91e018270832 fix: all tests (once more)
Sam <sam@basx.dev>
parents: 373
diff changeset
12 attr[Vec3f]("position", memoryPerformanceHint = PreferFastRead),
91e018270832 fix: all tests (once more)
Sam <sam@basx.dev>
parents: 373
diff changeset
13 attr[uint16](MATERIALINDEX_ATTRIBUTE, memoryPerformanceHint = PreferFastRead, perInstance = true),
91e018270832 fix: all tests (once more)
Sam <sam@basx.dev>
parents: 373
diff changeset
14 attr[Vec2f]("texcoord_0", memoryPerformanceHint = PreferFastRead),
91e018270832 fix: all tests (once more)
Sam <sam@basx.dev>
parents: 373
diff changeset
15 attr[Mat4]("transform", memoryPerformanceHint = PreferFastWrite, perInstance = true),
333
27aaf43e18b4 fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents: 318
diff changeset
16 ],
420
91e018270832 fix: all tests (once more)
Sam <sam@basx.dev>
parents: 373
diff changeset
17 intermediates = [
333
27aaf43e18b4 fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents: 318
diff changeset
18 attr[Vec4f]("vertexColor"),
27aaf43e18b4 fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents: 318
diff changeset
19 attr[Vec2f]("colorTexCoord"),
420
91e018270832 fix: all tests (once more)
Sam <sam@basx.dev>
parents: 373
diff changeset
20 attr[uint16]("materialIndexOut", noInterpolation = true)
333
27aaf43e18b4 fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents: 318
diff changeset
21 ],
420
91e018270832 fix: all tests (once more)
Sam <sam@basx.dev>
parents: 373
diff changeset
22 outputs = [attr[Vec4f]("color")],
91e018270832 fix: all tests (once more)
Sam <sam@basx.dev>
parents: 373
diff changeset
23 uniforms = [
333
27aaf43e18b4 fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents: 318
diff changeset
24 attr[Mat4]("projection"),
27aaf43e18b4 fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents: 318
diff changeset
25 attr[Mat4]("view"),
420
91e018270832 fix: all tests (once more)
Sam <sam@basx.dev>
parents: 373
diff changeset
26 attr[Vec4f]("color", arrayCount = 4),
333
27aaf43e18b4 fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents: 318
diff changeset
27 ],
420
91e018270832 fix: all tests (once more)
Sam <sam@basx.dev>
parents: 373
diff changeset
28 samplers = [attr[Texture]("baseTexture", arrayCount = 4)],
91e018270832 fix: all tests (once more)
Sam <sam@basx.dev>
parents: 373
diff changeset
29 vertexCode = &"""
333
27aaf43e18b4 fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents: 318
diff changeset
30 gl_Position = vec4(position, 1.0) * (transform * Uniforms.view * Uniforms.projection);
420
91e018270832 fix: all tests (once more)
Sam <sam@basx.dev>
parents: 373
diff changeset
31 vertexColor = Uniforms.color[{MATERIALINDEX_ATTRIBUTE}];
333
27aaf43e18b4 fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents: 318
diff changeset
32 colorTexCoord = texcoord_0;
420
91e018270832 fix: all tests (once more)
Sam <sam@basx.dev>
parents: 373
diff changeset
33 materialIndexOut = {MATERIALINDEX_ATTRIBUTE};
333
27aaf43e18b4 fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents: 318
diff changeset
34 """,
420
91e018270832 fix: all tests (once more)
Sam <sam@basx.dev>
parents: 373
diff changeset
35 fragmentCode = "color = texture(baseTexture[materialIndexOut], colorTexCoord) * vertexColor;"
222
ddfc54036e00 add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff changeset
36 )
371
f054b8bacab8 fix: tests, test_materials and test_mesh still needs to be done
Sam <sam@basx.dev>
parents: 353
diff changeset
37 engine.initRenderer({COLORED_SINGLE_TEXTURE_MATERIAL: shaderConfiguration})
333
27aaf43e18b4 fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents: 318
diff changeset
38
239
f05497ef8fd2 did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 230
diff changeset
39 for scene in scenes.mitems:
333
27aaf43e18b4 fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents: 318
diff changeset
40 scene.addShaderGlobal("projection", Unit4F32)
27aaf43e18b4 fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents: 318
diff changeset
41 scene.addShaderGlobal("view", Unit4F32)
371
f054b8bacab8 fix: tests, test_materials and test_mesh still needs to be done
Sam <sam@basx.dev>
parents: 353
diff changeset
42 engine.loadScene(scene)
333
27aaf43e18b4 fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents: 318
diff changeset
43
239
f05497ef8fd2 did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 230
diff changeset
44 var
247
beb41c93aa3f fix: gltf loading
Sam <sam@basx.dev>
parents: 246
diff changeset
45 size = 1'f32
246
dcbd9f256f6a fix: better defaults
Sam <sam@basx.dev>
parents: 245
diff changeset
46 elevation = 0'f32
239
f05497ef8fd2 did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 230
diff changeset
47 azimut = 0'f32
f05497ef8fd2 did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 230
diff changeset
48 currentScene = 0
f05497ef8fd2 did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 230
diff changeset
49
222
ddfc54036e00 add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff changeset
50 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
51 if engine.keyWasPressed(`1`):
f05497ef8fd2 did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 230
diff changeset
52 currentScene = 0
f05497ef8fd2 did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 230
diff changeset
53 elif engine.keyWasPressed(`2`):
f05497ef8fd2 did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 230
diff changeset
54 currentScene = 1
f05497ef8fd2 did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 230
diff changeset
55 elif engine.keyWasPressed(`3`):
f05497ef8fd2 did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 230
diff changeset
56 currentScene = 2
f05497ef8fd2 did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 230
diff changeset
57 elif engine.keyWasPressed(`4`):
f05497ef8fd2 did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 230
diff changeset
58 currentScene = 3
f05497ef8fd2 did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 230
diff changeset
59 elif engine.keyWasPressed(`5`):
f05497ef8fd2 did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 230
diff changeset
60 currentScene = 4
f05497ef8fd2 did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 230
diff changeset
61 elif engine.keyWasPressed(`6`):
f05497ef8fd2 did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 230
diff changeset
62 currentScene = 5
f05497ef8fd2 did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 230
diff changeset
63
f05497ef8fd2 did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 230
diff changeset
64 if engine.keyWasPressed(NumberRowExtra3):
246
dcbd9f256f6a fix: better defaults
Sam <sam@basx.dev>
parents: 245
diff changeset
65 size = 0.3'f32
dcbd9f256f6a fix: better defaults
Sam <sam@basx.dev>
parents: 245
diff changeset
66 elevation = 0'f32
239
f05497ef8fd2 did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 230
diff changeset
67 azimut = 0'f32
f05497ef8fd2 did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 230
diff changeset
68
246
dcbd9f256f6a fix: better defaults
Sam <sam@basx.dev>
parents: 245
diff changeset
69 let 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
70 size *= 1'f32 + engine.mouseWheel() * 0.05
f05497ef8fd2 did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 230
diff changeset
71 azimut += engine.mouseMove().x / 180'f32
f05497ef8fd2 did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 230
diff changeset
72 elevation -= engine.mouseMove().y / 180'f32
245
a19c9d089f25 add: correct camera calculations
Sam <sam@basx.dev>
parents: 244
diff changeset
73 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
74 scenes[currentScene].setShaderGlobal(
245
a19c9d089f25 add: correct camera calculations
Sam <sam@basx.dev>
parents: 244
diff changeset
75 "view",
333
27aaf43e18b4 fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents: 318
diff changeset
76 scale(size, size, size) * rotate(elevation, newVec3f(1, 0, 0)) * rotate(azimut, Yf32)
230
027f6ff06585 add: test mesh
Sam <sam@basx.dev>
parents: 222
diff changeset
77 )
239
f05497ef8fd2 did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 230
diff changeset
78 engine.renderScene(scenes[currentScene])
222
ddfc54036e00 add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff changeset
79 engine.destroy()
ddfc54036e00 add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff changeset
80
ddfc54036e00 add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff changeset
81 when isMainModule:
ddfc54036e00 add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff changeset
82 main()