Mercurial > games > semicongine
annotate tests/test_mesh.nim @ 795:4a15d94d9418
fix: remaining tests and an issue with updating uniforms
author | Sam <sam@basx.dev> |
---|---|
date | Mon, 04 Sep 2023 00:48:00 +0700 |
parents | 59c54c4486c4 |
children | b83b3a1ccb05 |
rev | line source |
---|---|
794
59c54c4486c4
fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents:
779
diff
changeset
|
1 import std/algorithm |
779
a2c14402acd2
fix: test not running with temporary new material system
Sam <sam@basx.dev>
parents:
777
diff
changeset
|
2 import std/sequtils |
a2c14402acd2
fix: test not running with temporary new material system
Sam <sam@basx.dev>
parents:
777
diff
changeset
|
3 import std/tables |
683
2ca938595aea
add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff
changeset
|
4 import semicongine |
2ca938595aea
add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff
changeset
|
5 |
2ca938595aea
add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff
changeset
|
6 proc main() = |
794
59c54c4486c4
fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents:
779
diff
changeset
|
7 # var myScene = Scene(name: "hi", meshes: @[rect()]) |
59c54c4486c4
fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents:
779
diff
changeset
|
8 # myScene.meshes[0].transform = translate3d(0.2'f32, 0'f32, 0'f32) |
59c54c4486c4
fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents:
779
diff
changeset
|
9 # myScene.root[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
|
10 var scenes = [ |
729 | 11 # loadScene("default_cube.glb", "1"), |
708 | 12 # loadScene("default_cube1.glb", "3"), |
13 # loadScene("default_cube2.glb", "4"), | |
14 # loadScene("flat.glb", "5"), | |
794
59c54c4486c4
fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents:
779
diff
changeset
|
15 Scene(name: "Donut", meshes: loadMeshes("tutorialk-donat.glb")[0].toSeq), |
708 | 16 # myScene, |
17 # loadScene("personv3.glb", "2"), | |
700
abc0d97392cb
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
691
diff
changeset
|
18 ] |
683
2ca938595aea
add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff
changeset
|
19 |
2ca938595aea
add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff
changeset
|
20 var engine = initEngine("Test meshes") |
2ca938595aea
add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff
changeset
|
21 const |
794
59c54c4486c4
fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents:
779
diff
changeset
|
22 shaderConfiguration = createShaderConfiguration( |
59c54c4486c4
fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents:
779
diff
changeset
|
23 inputs=[ |
59c54c4486c4
fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents:
779
diff
changeset
|
24 attr[Vec3f]("position", memoryPerformanceHint=PreferFastRead), |
59c54c4486c4
fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents:
779
diff
changeset
|
25 attr[uint16]("materialIndex", memoryPerformanceHint=PreferFastRead), |
59c54c4486c4
fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents:
779
diff
changeset
|
26 attr[Vec2f]("texcoord_0", memoryPerformanceHint=PreferFastRead), |
59c54c4486c4
fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents:
779
diff
changeset
|
27 attr[Mat4]("transform", memoryPerformanceHint=PreferFastWrite, perInstance=true), |
59c54c4486c4
fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents:
779
diff
changeset
|
28 ], |
59c54c4486c4
fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents:
779
diff
changeset
|
29 intermediates=[ |
59c54c4486c4
fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents:
779
diff
changeset
|
30 attr[Vec4f]("vertexColor"), |
59c54c4486c4
fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents:
779
diff
changeset
|
31 attr[Vec2f]("colorTexCoord"), |
59c54c4486c4
fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents:
779
diff
changeset
|
32 attr[uint16]("materialIndexOut", noInterpolation=true) |
59c54c4486c4
fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents:
779
diff
changeset
|
33 ], |
59c54c4486c4
fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents:
779
diff
changeset
|
34 outputs=[attr[Vec4f]("color")], |
59c54c4486c4
fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents:
779
diff
changeset
|
35 uniforms=[ |
59c54c4486c4
fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents:
779
diff
changeset
|
36 attr[Mat4]("projection"), |
59c54c4486c4
fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents:
779
diff
changeset
|
37 attr[Mat4]("view"), |
59c54c4486c4
fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents:
779
diff
changeset
|
38 attr[Vec4f]("baseColorFactor", arrayCount=4), |
59c54c4486c4
fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents:
779
diff
changeset
|
39 ], |
59c54c4486c4
fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents:
779
diff
changeset
|
40 samplers=[attr[Texture]("baseColorTexture", arrayCount=4)], |
730 | 41 vertexCode=""" |
794
59c54c4486c4
fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents:
779
diff
changeset
|
42 gl_Position = vec4(position, 1.0) * (transform * Uniforms.view * Uniforms.projection); |
59c54c4486c4
fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents:
779
diff
changeset
|
43 vertexColor = Uniforms.baseColorFactor[materialIndex]; |
59c54c4486c4
fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents:
779
diff
changeset
|
44 colorTexCoord = texcoord_0; |
59c54c4486c4
fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents:
779
diff
changeset
|
45 materialIndexOut = materialIndex; |
59c54c4486c4
fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents:
779
diff
changeset
|
46 """, |
59c54c4486c4
fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents:
779
diff
changeset
|
47 fragmentCode="color = texture(baseColorTexture[materialIndexOut], colorTexCoord) * vertexColor;" |
683
2ca938595aea
add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff
changeset
|
48 ) |
794
59c54c4486c4
fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents:
779
diff
changeset
|
49 engine.initRenderer({ |
59c54c4486c4
fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents:
779
diff
changeset
|
50 "Material": shaderConfiguration, |
59c54c4486c4
fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents:
779
diff
changeset
|
51 "Material.001": shaderConfiguration, |
59c54c4486c4
fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents:
779
diff
changeset
|
52 "Material.002": shaderConfiguration, |
59c54c4486c4
fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents:
779
diff
changeset
|
53 "Material.004": shaderConfiguration, |
59c54c4486c4
fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents:
779
diff
changeset
|
54 }.toTable) |
59c54c4486c4
fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents:
779
diff
changeset
|
55 |
700
abc0d97392cb
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
691
diff
changeset
|
56 for scene in scenes.mitems: |
794
59c54c4486c4
fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents:
779
diff
changeset
|
57 scene.addShaderGlobal("projection", Unit4F32) |
59c54c4486c4
fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents:
779
diff
changeset
|
58 scene.addShaderGlobal("view", Unit4F32) |
59c54c4486c4
fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents:
779
diff
changeset
|
59 var materials: Table[uint16, Material] |
59c54c4486c4
fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents:
779
diff
changeset
|
60 for mesh in scene.meshes: |
59c54c4486c4
fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents:
779
diff
changeset
|
61 if not materials.contains(mesh.material.index): |
59c54c4486c4
fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents:
779
diff
changeset
|
62 materials[mesh.material.index] = mesh.material |
59c54c4486c4
fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents:
779
diff
changeset
|
63 let baseColors = sortedByIt(values(materials).toSeq, it.index).mapIt(getValue[Vec4f](it.constants["baseColorFactor"], 0)) |
59c54c4486c4
fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents:
779
diff
changeset
|
64 let baseTextures = sortedByIt(values(materials).toSeq, it.index).mapIt(it.textures["baseColorTexture"]) |
59c54c4486c4
fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents:
779
diff
changeset
|
65 for t in baseTextures: |
59c54c4486c4
fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents:
779
diff
changeset
|
66 echo "- ", t |
779
a2c14402acd2
fix: test not running with temporary new material system
Sam <sam@basx.dev>
parents:
777
diff
changeset
|
67 scene.addShaderGlobalArray("baseColorFactor", baseColors) |
794
59c54c4486c4
fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents:
779
diff
changeset
|
68 scene.addShaderGlobalArray("baseColorTexture", baseTextures) |
59c54c4486c4
fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents:
779
diff
changeset
|
69 engine.addScene(scene) |
59c54c4486c4
fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents:
779
diff
changeset
|
70 |
700
abc0d97392cb
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
691
diff
changeset
|
71 var |
708 | 72 size = 1'f32 |
707 | 73 elevation = 0'f32 |
700
abc0d97392cb
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
691
diff
changeset
|
74 azimut = 0'f32 |
abc0d97392cb
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
691
diff
changeset
|
75 currentScene = 0 |
abc0d97392cb
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
691
diff
changeset
|
76 |
683
2ca938595aea
add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff
changeset
|
77 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
|
78 if engine.keyWasPressed(`1`): |
abc0d97392cb
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
691
diff
changeset
|
79 currentScene = 0 |
abc0d97392cb
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
691
diff
changeset
|
80 elif engine.keyWasPressed(`2`): |
abc0d97392cb
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
691
diff
changeset
|
81 currentScene = 1 |
abc0d97392cb
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
691
diff
changeset
|
82 elif engine.keyWasPressed(`3`): |
abc0d97392cb
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
691
diff
changeset
|
83 currentScene = 2 |
abc0d97392cb
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
691
diff
changeset
|
84 elif engine.keyWasPressed(`4`): |
abc0d97392cb
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
691
diff
changeset
|
85 currentScene = 3 |
abc0d97392cb
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
691
diff
changeset
|
86 elif engine.keyWasPressed(`5`): |
abc0d97392cb
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
691
diff
changeset
|
87 currentScene = 4 |
abc0d97392cb
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
691
diff
changeset
|
88 elif engine.keyWasPressed(`6`): |
abc0d97392cb
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
691
diff
changeset
|
89 currentScene = 5 |
abc0d97392cb
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
691
diff
changeset
|
90 |
abc0d97392cb
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
691
diff
changeset
|
91 if engine.keyWasPressed(NumberRowExtra3): |
707 | 92 size = 0.3'f32 |
93 elevation = 0'f32 | |
700
abc0d97392cb
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
691
diff
changeset
|
94 azimut = 0'f32 |
abc0d97392cb
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
691
diff
changeset
|
95 |
707 | 96 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
|
97 size *= 1'f32 + engine.mouseWheel() * 0.05 |
abc0d97392cb
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
691
diff
changeset
|
98 azimut += engine.mouseMove().x / 180'f32 |
abc0d97392cb
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
691
diff
changeset
|
99 elevation -= engine.mouseMove().y / 180'f32 |
706 | 100 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
|
101 scenes[currentScene].setShaderGlobal( |
706 | 102 "view", |
794
59c54c4486c4
fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents:
779
diff
changeset
|
103 scale(size, size, size) * rotate(elevation, newVec3f(1, 0, 0)) * rotate(azimut, Yf32) |
691 | 104 ) |
700
abc0d97392cb
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
691
diff
changeset
|
105 engine.renderScene(scenes[currentScene]) |
683
2ca938595aea
add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff
changeset
|
106 engine.destroy() |
2ca938595aea
add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff
changeset
|
107 |
2ca938595aea
add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff
changeset
|
108 when isMainModule: |
2ca938595aea
add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff
changeset
|
109 main() |