annotate tests/test_mesh.nim @ 329:69e18f69713b

add: scene/shader compatability check, fix collision code to work with new APIs
author Sam <sam@basx.dev>
date Tue, 29 Aug 2023 00:01:13 +0700
parents e656c0aad093
children 27aaf43e18b4
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
318
e656c0aad093 fix: test not running with temporary new material system
Sam <sam@basx.dev>
parents: 316
diff changeset
1 import std/sequtils
e656c0aad093 fix: test not running with temporary new material system
Sam <sam@basx.dev>
parents: 316
diff changeset
2 import std/tables
222
ddfc54036e00 add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff changeset
3 import semicongine
ddfc54036e00 add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff changeset
4
ddfc54036e00 add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff changeset
5 proc main() =
302
da0bd61abe91 did: change API for ECS
Sam <sam@basx.dev>
parents: 269
diff changeset
6 var ent1 = newEntity("hoho", {"mesh": Component(rect())})
da0bd61abe91 did: change API for ECS
Sam <sam@basx.dev>
parents: 269
diff changeset
7 var ent2 = newEntity("hehe", [], ent1)
247
beb41c93aa3f fix: gltf loading
Sam <sam@basx.dev>
parents: 246
diff changeset
8 var myScene = newScene("hi", ent2)
beb41c93aa3f fix: gltf loading
Sam <sam@basx.dev>
parents: 246
diff changeset
9 myScene.root.transform = translate3d(0.2'f32, 0'f32, 0'f32)
316
b145a05c2459 add: changing rendering system, not finished yet, also upgrading to Nim 2
Sam <sam@basx.dev>
parents: 302
diff changeset
10 myScene.root[0].transform = translate3d(0'f32, 0.2'f32, 0'f32)
239
f05497ef8fd2 did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 230
diff changeset
11 var scenes = [
268
d16fd73959c1 update shader to work correctly
Sam <sam@basx.dev>
parents: 253
diff changeset
12 # loadScene("default_cube.glb", "1"),
247
beb41c93aa3f fix: gltf loading
Sam <sam@basx.dev>
parents: 246
diff changeset
13 # loadScene("default_cube1.glb", "3"),
beb41c93aa3f fix: gltf loading
Sam <sam@basx.dev>
parents: 246
diff changeset
14 # loadScene("default_cube2.glb", "4"),
beb41c93aa3f fix: gltf loading
Sam <sam@basx.dev>
parents: 246
diff changeset
15 # loadScene("flat.glb", "5"),
268
d16fd73959c1 update shader to work correctly
Sam <sam@basx.dev>
parents: 253
diff changeset
16 loadScene("tutorialk-donat.glb", "6"),
247
beb41c93aa3f fix: gltf loading
Sam <sam@basx.dev>
parents: 246
diff changeset
17 # myScene,
beb41c93aa3f fix: gltf loading
Sam <sam@basx.dev>
parents: 246
diff changeset
18 # loadScene("personv3.glb", "2"),
239
f05497ef8fd2 did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 230
diff changeset
19 ]
222
ddfc54036e00 add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff changeset
20
ddfc54036e00 add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff changeset
21 var engine = initEngine("Test meshes")
ddfc54036e00 add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff changeset
22 const
239
f05497ef8fd2 did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 230
diff changeset
23 vertexInput = @[
f05497ef8fd2 did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 230
diff changeset
24 attr[Vec3f]("position", memoryPerformanceHint=PreferFastRead),
318
e656c0aad093 fix: test not running with temporary new material system
Sam <sam@basx.dev>
parents: 316
diff changeset
25 attr[uint16]("materialIndex", memoryPerformanceHint=PreferFastRead),
247
beb41c93aa3f fix: gltf loading
Sam <sam@basx.dev>
parents: 246
diff changeset
26 attr[Vec2f]("texcoord_0", memoryPerformanceHint=PreferFastRead),
beb41c93aa3f fix: gltf loading
Sam <sam@basx.dev>
parents: 246
diff changeset
27 attr[Mat4]("transform", memoryPerformanceHint=PreferFastWrite, perInstance=true),
239
f05497ef8fd2 did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 230
diff changeset
28 ]
269
b5fb27b0f7a4 add: simpler shader construction
Sam <sam@basx.dev>
parents: 268
diff changeset
29 intermediate = @[
268
d16fd73959c1 update shader to work correctly
Sam <sam@basx.dev>
parents: 253
diff changeset
30 attr[Vec4f]("vertexColor"),
d16fd73959c1 update shader to work correctly
Sam <sam@basx.dev>
parents: 253
diff changeset
31 attr[Vec2f]("colorTexCoord"),
318
e656c0aad093 fix: test not running with temporary new material system
Sam <sam@basx.dev>
parents: 316
diff changeset
32 attr[uint16]("materialIndexOut", noInterpolation=true)
268
d16fd73959c1 update shader to work correctly
Sam <sam@basx.dev>
parents: 253
diff changeset
33 ]
222
ddfc54036e00 add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff changeset
34 fragOutput = @[attr[Vec4f]("color")]
245
a19c9d089f25 add: correct camera calculations
Sam <sam@basx.dev>
parents: 244
diff changeset
35 uniforms = @[
a19c9d089f25 add: correct camera calculations
Sam <sam@basx.dev>
parents: 244
diff changeset
36 attr[Mat4]("projection"),
a19c9d089f25 add: correct camera calculations
Sam <sam@basx.dev>
parents: 244
diff changeset
37 attr[Mat4]("view"),
268
d16fd73959c1 update shader to work correctly
Sam <sam@basx.dev>
parents: 253
diff changeset
38 attr[Vec4f]("baseColorFactor", arrayCount=4),
245
a19c9d089f25 add: correct camera calculations
Sam <sam@basx.dev>
parents: 244
diff changeset
39 ]
269
b5fb27b0f7a4 add: simpler shader construction
Sam <sam@basx.dev>
parents: 268
diff changeset
40 samplers = @[attr[Sampler2DType]("baseColorTexture", arrayCount=4)]
b5fb27b0f7a4 add: simpler shader construction
Sam <sam@basx.dev>
parents: 268
diff changeset
41 (vertexCode, fragmentCode) = compileVertexFragmentShaderSet(
222
ddfc54036e00 add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff changeset
42 inputs=vertexInput,
269
b5fb27b0f7a4 add: simpler shader construction
Sam <sam@basx.dev>
parents: 268
diff changeset
43 intermediate=intermediate,
b5fb27b0f7a4 add: simpler shader construction
Sam <sam@basx.dev>
parents: 268
diff changeset
44 outputs=fragOutput,
222
ddfc54036e00 add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff changeset
45 uniforms=uniforms,
247
beb41c93aa3f fix: gltf loading
Sam <sam@basx.dev>
parents: 246
diff changeset
46 samplers=samplers,
269
b5fb27b0f7a4 add: simpler shader construction
Sam <sam@basx.dev>
parents: 268
diff changeset
47 vertexCode="""
247
beb41c93aa3f fix: gltf loading
Sam <sam@basx.dev>
parents: 246
diff changeset
48 gl_Position = vec4(position, 1.0) * (transform * Uniforms.view * Uniforms.projection);
318
e656c0aad093 fix: test not running with temporary new material system
Sam <sam@basx.dev>
parents: 316
diff changeset
49 vertexColor = Uniforms.baseColorFactor[materialIndex];
268
d16fd73959c1 update shader to work correctly
Sam <sam@basx.dev>
parents: 253
diff changeset
50 colorTexCoord = texcoord_0;
318
e656c0aad093 fix: test not running with temporary new material system
Sam <sam@basx.dev>
parents: 316
diff changeset
51 materialIndexOut = materialIndex;
269
b5fb27b0f7a4 add: simpler shader construction
Sam <sam@basx.dev>
parents: 268
diff changeset
52 """,
b5fb27b0f7a4 add: simpler shader construction
Sam <sam@basx.dev>
parents: 268
diff changeset
53 fragmentCode="""
318
e656c0aad093 fix: test not running with temporary new material system
Sam <sam@basx.dev>
parents: 316
diff changeset
54 // 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));
e656c0aad093 fix: test not running with temporary new material system
Sam <sam@basx.dev>
parents: 316
diff changeset
55 color = texture(baseColorTexture[materialIndexOut], colorTexCoord) * vertexColor;
268
d16fd73959c1 update shader to work correctly
Sam <sam@basx.dev>
parents: 253
diff changeset
56 """
222
ddfc54036e00 add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff changeset
57 )
239
f05497ef8fd2 did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 230
diff changeset
58 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
59 for scene in scenes.mitems:
318
e656c0aad093 fix: test not running with temporary new material system
Sam <sam@basx.dev>
parents: 316
diff changeset
60 engine.addScene(scene, vertexInput, samplers)
245
a19c9d089f25 add: correct camera calculations
Sam <sam@basx.dev>
parents: 244
diff changeset
61 scene.addShaderGlobal("projection", Unit4)
a19c9d089f25 add: correct camera calculations
Sam <sam@basx.dev>
parents: 244
diff changeset
62 scene.addShaderGlobal("view", Unit4)
318
e656c0aad093 fix: test not running with temporary new material system
Sam <sam@basx.dev>
parents: 316
diff changeset
63 let baseColors = scene.materials.map(proc(i: Material): Vec4f = getValue[Vec4f](i[].constants["baseColorFactor"]))
e656c0aad093 fix: test not running with temporary new material system
Sam <sam@basx.dev>
parents: 316
diff changeset
64 scene.addShaderGlobalArray("baseColorFactor", baseColors)
239
f05497ef8fd2 did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 230
diff changeset
65 var
247
beb41c93aa3f fix: gltf loading
Sam <sam@basx.dev>
parents: 246
diff changeset
66 size = 1'f32
246
dcbd9f256f6a fix: better defaults
Sam <sam@basx.dev>
parents: 245
diff changeset
67 elevation = 0'f32
239
f05497ef8fd2 did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 230
diff changeset
68 azimut = 0'f32
f05497ef8fd2 did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 230
diff changeset
69 currentScene = 0
f05497ef8fd2 did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 230
diff changeset
70
222
ddfc54036e00 add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff changeset
71 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
72 if engine.keyWasPressed(`1`):
f05497ef8fd2 did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 230
diff changeset
73 currentScene = 0
f05497ef8fd2 did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 230
diff changeset
74 elif engine.keyWasPressed(`2`):
f05497ef8fd2 did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 230
diff changeset
75 currentScene = 1
f05497ef8fd2 did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 230
diff changeset
76 elif engine.keyWasPressed(`3`):
f05497ef8fd2 did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 230
diff changeset
77 currentScene = 2
f05497ef8fd2 did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 230
diff changeset
78 elif engine.keyWasPressed(`4`):
f05497ef8fd2 did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 230
diff changeset
79 currentScene = 3
f05497ef8fd2 did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 230
diff changeset
80 elif engine.keyWasPressed(`5`):
f05497ef8fd2 did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 230
diff changeset
81 currentScene = 4
f05497ef8fd2 did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 230
diff changeset
82 elif engine.keyWasPressed(`6`):
f05497ef8fd2 did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 230
diff changeset
83 currentScene = 5
f05497ef8fd2 did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 230
diff changeset
84
f05497ef8fd2 did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 230
diff changeset
85 if engine.keyWasPressed(NumberRowExtra3):
246
dcbd9f256f6a fix: better defaults
Sam <sam@basx.dev>
parents: 245
diff changeset
86 size = 0.3'f32
dcbd9f256f6a fix: better defaults
Sam <sam@basx.dev>
parents: 245
diff changeset
87 elevation = 0'f32
239
f05497ef8fd2 did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 230
diff changeset
88 azimut = 0'f32
f05497ef8fd2 did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 230
diff changeset
89
246
dcbd9f256f6a fix: better defaults
Sam <sam@basx.dev>
parents: 245
diff changeset
90 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
91 size *= 1'f32 + engine.mouseWheel() * 0.05
f05497ef8fd2 did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 230
diff changeset
92 azimut += engine.mouseMove().x / 180'f32
f05497ef8fd2 did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 230
diff changeset
93 elevation -= engine.mouseMove().y / 180'f32
245
a19c9d089f25 add: correct camera calculations
Sam <sam@basx.dev>
parents: 244
diff changeset
94 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
95 scenes[currentScene].setShaderGlobal(
245
a19c9d089f25 add: correct camera calculations
Sam <sam@basx.dev>
parents: 244
diff changeset
96 "view",
a19c9d089f25 add: correct camera calculations
Sam <sam@basx.dev>
parents: 244
diff changeset
97 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
98 )
239
f05497ef8fd2 did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 230
diff changeset
99 engine.renderScene(scenes[currentScene])
222
ddfc54036e00 add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff changeset
100 engine.destroy()
ddfc54036e00 add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff changeset
101
ddfc54036e00 add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff changeset
102 when isMainModule:
ddfc54036e00 add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff changeset
103 main()