annotate tests/test_mesh.nim @ 832:388c4b35a6e3

fix: tests, test_materials and test_mesh still needs to be done
author Sam <sam@basx.dev>
date Wed, 22 Nov 2023 23:24:47 +0700
parents 6a09fe5dc99b
children f4f1474dc70a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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() =
700
abc0d97392cb did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 691
diff changeset
7 var scenes = [
832
388c4b35a6e3 fix: tests, test_materials and test_mesh still needs to be done
Sam <sam@basx.dev>
parents: 814
diff changeset
8 Scene(name: "Donut", meshes: loadMeshes("tutorialk-donat.glb", COLORED_SINGLE_TEXTURE_MATERIAL)[0].toSeq),
700
abc0d97392cb did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 691
diff changeset
9 ]
683
2ca938595aea add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff changeset
10
2ca938595aea add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff changeset
11 var engine = initEngine("Test meshes")
2ca938595aea add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff changeset
12 const
794
59c54c4486c4 fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents: 779
diff changeset
13 shaderConfiguration = createShaderConfiguration(
59c54c4486c4 fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents: 779
diff changeset
14 inputs=[
59c54c4486c4 fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents: 779
diff changeset
15 attr[Vec3f]("position", memoryPerformanceHint=PreferFastRead),
832
388c4b35a6e3 fix: tests, test_materials and test_mesh still needs to be done
Sam <sam@basx.dev>
parents: 814
diff changeset
16 attr[uint16]("materialIndex", memoryPerformanceHint=PreferFastRead, perInstance=true),
794
59c54c4486c4 fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents: 779
diff changeset
17 attr[Vec2f]("texcoord_0", memoryPerformanceHint=PreferFastRead),
59c54c4486c4 fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents: 779
diff changeset
18 attr[Mat4]("transform", memoryPerformanceHint=PreferFastWrite, perInstance=true),
59c54c4486c4 fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents: 779
diff changeset
19 ],
59c54c4486c4 fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents: 779
diff changeset
20 intermediates=[
59c54c4486c4 fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents: 779
diff changeset
21 attr[Vec4f]("vertexColor"),
59c54c4486c4 fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents: 779
diff changeset
22 attr[Vec2f]("colorTexCoord"),
59c54c4486c4 fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents: 779
diff changeset
23 attr[uint16]("materialIndexOut", noInterpolation=true)
59c54c4486c4 fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents: 779
diff changeset
24 ],
59c54c4486c4 fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents: 779
diff changeset
25 outputs=[attr[Vec4f]("color")],
59c54c4486c4 fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents: 779
diff changeset
26 uniforms=[
59c54c4486c4 fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents: 779
diff changeset
27 attr[Mat4]("projection"),
59c54c4486c4 fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents: 779
diff changeset
28 attr[Mat4]("view"),
832
388c4b35a6e3 fix: tests, test_materials and test_mesh still needs to be done
Sam <sam@basx.dev>
parents: 814
diff changeset
29 attr[Vec4f]("color", arrayCount=4),
794
59c54c4486c4 fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents: 779
diff changeset
30 ],
832
388c4b35a6e3 fix: tests, test_materials and test_mesh still needs to be done
Sam <sam@basx.dev>
parents: 814
diff changeset
31 samplers=[attr[Texture]("baseTexture", arrayCount=4)],
730
88ab3e6dcaa0 add: simpler shader construction
Sam <sam@basx.dev>
parents: 729
diff changeset
32 vertexCode="""
794
59c54c4486c4 fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents: 779
diff changeset
33 gl_Position = vec4(position, 1.0) * (transform * Uniforms.view * Uniforms.projection);
832
388c4b35a6e3 fix: tests, test_materials and test_mesh still needs to be done
Sam <sam@basx.dev>
parents: 814
diff changeset
34 vertexColor = Uniforms.color[materialIndex];
794
59c54c4486c4 fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents: 779
diff changeset
35 colorTexCoord = texcoord_0;
59c54c4486c4 fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents: 779
diff changeset
36 materialIndexOut = materialIndex;
59c54c4486c4 fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents: 779
diff changeset
37 """,
832
388c4b35a6e3 fix: tests, test_materials and test_mesh still needs to be done
Sam <sam@basx.dev>
parents: 814
diff changeset
38 fragmentCode="color = texture(baseTexture[materialIndexOut], colorTexCoord) * vertexColor;"
683
2ca938595aea add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff changeset
39 )
832
388c4b35a6e3 fix: tests, test_materials and test_mesh still needs to be done
Sam <sam@basx.dev>
parents: 814
diff changeset
40 engine.initRenderer({COLORED_SINGLE_TEXTURE_MATERIAL: shaderConfiguration})
794
59c54c4486c4 fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents: 779
diff changeset
41
700
abc0d97392cb did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 691
diff changeset
42 for scene in scenes.mitems:
794
59c54c4486c4 fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents: 779
diff changeset
43 scene.addShaderGlobal("projection", Unit4F32)
59c54c4486c4 fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents: 779
diff changeset
44 scene.addShaderGlobal("view", Unit4F32)
832
388c4b35a6e3 fix: tests, test_materials and test_mesh still needs to be done
Sam <sam@basx.dev>
parents: 814
diff changeset
45 engine.loadScene(scene)
794
59c54c4486c4 fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents: 779
diff changeset
46
700
abc0d97392cb did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 691
diff changeset
47 var
708
3bb199dd45ba fix: gltf loading
Sam <sam@basx.dev>
parents: 707
diff changeset
48 size = 1'f32
707
3742bba2293f fix: better defaults
Sam <sam@basx.dev>
parents: 706
diff changeset
49 elevation = 0'f32
700
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):
707
3742bba2293f fix: better defaults
Sam <sam@basx.dev>
parents: 706
diff changeset
68 size = 0.3'f32
3742bba2293f fix: better defaults
Sam <sam@basx.dev>
parents: 706
diff changeset
69 elevation = 0'f32
700
abc0d97392cb did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 691
diff changeset
70 azimut = 0'f32
abc0d97392cb did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 691
diff changeset
71
707
3742bba2293f fix: better defaults
Sam <sam@basx.dev>
parents: 706
diff changeset
72 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
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",
794
59c54c4486c4 fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents: 779
diff changeset
79 scale(size, size, size) * rotate(elevation, newVec3f(1, 0, 0)) * rotate(azimut, Yf32)
691
9182a5d2ea3a add: test mesh
Sam <sam@basx.dev>
parents: 683
diff changeset
80 )
700
abc0d97392cb did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 691
diff changeset
81 engine.renderScene(scenes[currentScene])
683
2ca938595aea add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff changeset
82 engine.destroy()
2ca938595aea add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff changeset
83
2ca938595aea add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff changeset
84 when isMainModule:
2ca938595aea add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff changeset
85 main()