annotate old_tests/test_mesh.nim @ 1346:e6fdd7327847

add: option to bind descriptor without pipeline, but only pipeline layout
author sam <sam@basx.dev>
date Tue, 27 Aug 2024 19:53:24 +0700
parents 6360c8d17ce0
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
979
6406766a222d fix: tests
sam <sam@basx.dev>
parents: 420
diff changeset
1 import std/strformat
222
ddfc54036e00 add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff changeset
2 import semicongine
ddfc54036e00 add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff changeset
3
1021
73b572f82a1f add: bases for a better input-system
sam <sam@basx.dev>
parents: 979
diff changeset
4 const
73b572f82a1f add: bases for a better input-system
sam <sam@basx.dev>
parents: 979
diff changeset
5 MeshMaterial* = MaterialType(
73b572f82a1f add: bases for a better input-system
sam <sam@basx.dev>
parents: 979
diff changeset
6 name: "colored single texture material",
73b572f82a1f add: bases for a better input-system
sam <sam@basx.dev>
parents: 979
diff changeset
7 vertexAttributes: {
73b572f82a1f add: bases for a better input-system
sam <sam@basx.dev>
parents: 979
diff changeset
8 "position": Vec3F32,
73b572f82a1f add: bases for a better input-system
sam <sam@basx.dev>
parents: 979
diff changeset
9 "texcoord_0": Vec2F32,
73b572f82a1f add: bases for a better input-system
sam <sam@basx.dev>
parents: 979
diff changeset
10 }.toTable,
73b572f82a1f add: bases for a better input-system
sam <sam@basx.dev>
parents: 979
diff changeset
11 attributes: {"baseTexture": TextureType, "color": Vec4F32}.toTable
73b572f82a1f add: bases for a better input-system
sam <sam@basx.dev>
parents: 979
diff changeset
12 )
73b572f82a1f add: bases for a better input-system
sam <sam@basx.dev>
parents: 979
diff changeset
13
222
ddfc54036e00 add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff changeset
14 proc main() =
239
f05497ef8fd2 did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 230
diff changeset
15 var scenes = [
1139
114f395b9144 did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents: 1138
diff changeset
16 Scene(name: "Donut", meshes: LoadMeshes("donut.glb", MeshMaterial)[0].toSeq),
239
f05497ef8fd2 did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 230
diff changeset
17 ]
222
ddfc54036e00 add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff changeset
18
1138
02e1d2658ff5 did: more renaming
sam <sam@basx.dev>
parents: 1137
diff changeset
19 var engine = InitEngine("Test meshes")
222
ddfc54036e00 add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff changeset
20 const
1139
114f395b9144 did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents: 1138
diff changeset
21 shaderConfiguration = CreateShaderConfiguration(
1021
73b572f82a1f add: bases for a better input-system
sam <sam@basx.dev>
parents: 979
diff changeset
22 name = "default shader",
420
91e018270832 fix: all tests (once more)
Sam <sam@basx.dev>
parents: 373
diff changeset
23 inputs = [
1137
a4aa9f374d44 did: more renaming
sam <sam@basx.dev>
parents: 1136
diff changeset
24 Attr[Vec3f]("position", memoryPerformanceHint = PreferFastRead),
a4aa9f374d44 did: more renaming
sam <sam@basx.dev>
parents: 1136
diff changeset
25 Attr[uint16](MATERIALINDEX_ATTRIBUTE, memoryPerformanceHint = PreferFastRead, perInstance = true),
a4aa9f374d44 did: more renaming
sam <sam@basx.dev>
parents: 1136
diff changeset
26 Attr[Vec2f]("texcoord_0", memoryPerformanceHint = PreferFastRead),
a4aa9f374d44 did: more renaming
sam <sam@basx.dev>
parents: 1136
diff changeset
27 Attr[Mat4]("transform", memoryPerformanceHint = PreferFastWrite, perInstance = true),
333
27aaf43e18b4 fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents: 318
diff changeset
28 ],
420
91e018270832 fix: all tests (once more)
Sam <sam@basx.dev>
parents: 373
diff changeset
29 intermediates = [
1137
a4aa9f374d44 did: more renaming
sam <sam@basx.dev>
parents: 1136
diff changeset
30 Attr[Vec4f]("vertexColor"),
a4aa9f374d44 did: more renaming
sam <sam@basx.dev>
parents: 1136
diff changeset
31 Attr[Vec2f]("colorTexCoord"),
a4aa9f374d44 did: more renaming
sam <sam@basx.dev>
parents: 1136
diff changeset
32 Attr[uint16]("materialIndexOut", noInterpolation = true)
333
27aaf43e18b4 fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents: 318
diff changeset
33 ],
1137
a4aa9f374d44 did: more renaming
sam <sam@basx.dev>
parents: 1136
diff changeset
34 outputs = [Attr[Vec4f]("color")],
420
91e018270832 fix: all tests (once more)
Sam <sam@basx.dev>
parents: 373
diff changeset
35 uniforms = [
1137
a4aa9f374d44 did: more renaming
sam <sam@basx.dev>
parents: 1136
diff changeset
36 Attr[Mat4]("projection"),
a4aa9f374d44 did: more renaming
sam <sam@basx.dev>
parents: 1136
diff changeset
37 Attr[Mat4]("view"),
a4aa9f374d44 did: more renaming
sam <sam@basx.dev>
parents: 1136
diff changeset
38 Attr[Vec4f]("color", arrayCount = 4),
333
27aaf43e18b4 fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents: 318
diff changeset
39 ],
1137
a4aa9f374d44 did: more renaming
sam <sam@basx.dev>
parents: 1136
diff changeset
40 samplers = [Attr[Texture]("baseTexture", arrayCount = 4)],
420
91e018270832 fix: all tests (once more)
Sam <sam@basx.dev>
parents: 373
diff changeset
41 vertexCode = &"""
1021
73b572f82a1f add: bases for a better input-system
sam <sam@basx.dev>
parents: 979
diff changeset
42 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
43 vertexColor = Uniforms.color[{MATERIALINDEX_ATTRIBUTE}];
333
27aaf43e18b4 fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents: 318
diff changeset
44 colorTexCoord = texcoord_0;
420
91e018270832 fix: all tests (once more)
Sam <sam@basx.dev>
parents: 373
diff changeset
45 materialIndexOut = {MATERIALINDEX_ATTRIBUTE};
333
27aaf43e18b4 fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents: 318
diff changeset
46 """,
420
91e018270832 fix: all tests (once more)
Sam <sam@basx.dev>
parents: 373
diff changeset
47 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
48 )
1138
02e1d2658ff5 did: more renaming
sam <sam@basx.dev>
parents: 1137
diff changeset
49 engine.InitRenderer({MeshMaterial: shaderConfiguration})
333
27aaf43e18b4 fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents: 318
diff changeset
50
239
f05497ef8fd2 did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 230
diff changeset
51 for scene in scenes.mitems:
1139
114f395b9144 did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents: 1138
diff changeset
52 scene.AddShaderGlobal("projection", Unit4F32)
114f395b9144 did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents: 1138
diff changeset
53 scene.AddShaderGlobal("view", Unit4F32)
1138
02e1d2658ff5 did: more renaming
sam <sam@basx.dev>
parents: 1137
diff changeset
54 engine.LoadScene(scene)
333
27aaf43e18b4 fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents: 318
diff changeset
55
239
f05497ef8fd2 did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 230
diff changeset
56 var
247
beb41c93aa3f fix: gltf loading
Sam <sam@basx.dev>
parents: 246
diff changeset
57 size = 1'f32
246
dcbd9f256f6a fix: better defaults
Sam <sam@basx.dev>
parents: 245
diff changeset
58 elevation = 0'f32
239
f05497ef8fd2 did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 230
diff changeset
59 azimut = 0'f32
f05497ef8fd2 did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 230
diff changeset
60 currentScene = 0
f05497ef8fd2 did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 230
diff changeset
61
1135
74957cbf589b fix: update tests according to some API renaming
sam <sam@basx.dev>
parents: 1021
diff changeset
62 while engine.UpdateInputs() and not KeyIsDown(Escape):
74957cbf589b fix: update tests according to some API renaming
sam <sam@basx.dev>
parents: 1021
diff changeset
63 if KeyWasPressed(`1`):
239
f05497ef8fd2 did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 230
diff changeset
64 currentScene = 0
1135
74957cbf589b fix: update tests according to some API renaming
sam <sam@basx.dev>
parents: 1021
diff changeset
65 elif KeyWasPressed(`2`):
239
f05497ef8fd2 did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 230
diff changeset
66 currentScene = 1
1135
74957cbf589b fix: update tests according to some API renaming
sam <sam@basx.dev>
parents: 1021
diff changeset
67 elif KeyWasPressed(`3`):
239
f05497ef8fd2 did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 230
diff changeset
68 currentScene = 2
1135
74957cbf589b fix: update tests according to some API renaming
sam <sam@basx.dev>
parents: 1021
diff changeset
69 elif KeyWasPressed(`4`):
239
f05497ef8fd2 did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 230
diff changeset
70 currentScene = 3
1135
74957cbf589b fix: update tests according to some API renaming
sam <sam@basx.dev>
parents: 1021
diff changeset
71 elif KeyWasPressed(`5`):
239
f05497ef8fd2 did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 230
diff changeset
72 currentScene = 4
1135
74957cbf589b fix: update tests according to some API renaming
sam <sam@basx.dev>
parents: 1021
diff changeset
73 elif KeyWasPressed(`6`):
239
f05497ef8fd2 did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 230
diff changeset
74 currentScene = 5
f05497ef8fd2 did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 230
diff changeset
75
1135
74957cbf589b fix: update tests according to some API renaming
sam <sam@basx.dev>
parents: 1021
diff changeset
76 if KeyWasPressed(NumberRowExtra3):
246
dcbd9f256f6a fix: better defaults
Sam <sam@basx.dev>
parents: 245
diff changeset
77 size = 0.3'f32
dcbd9f256f6a fix: better defaults
Sam <sam@basx.dev>
parents: 245
diff changeset
78 elevation = 0'f32
239
f05497ef8fd2 did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 230
diff changeset
79 azimut = 0'f32
f05497ef8fd2 did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents: 230
diff changeset
80
1139
114f395b9144 did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents: 1138
diff changeset
81 let ratio = engine.GetWindow().Size[0] / engine.GetWindow().Size[1]
1135
74957cbf589b fix: update tests according to some API renaming
sam <sam@basx.dev>
parents: 1021
diff changeset
82 size *= 1'f32 + MouseWheel() * 0.05
74957cbf589b fix: update tests according to some API renaming
sam <sam@basx.dev>
parents: 1021
diff changeset
83 azimut += MouseMove().x / 180'f32
74957cbf589b fix: update tests according to some API renaming
sam <sam@basx.dev>
parents: 1021
diff changeset
84 elevation -= MouseMove().y / 180'f32
1139
114f395b9144 did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents: 1138
diff changeset
85 scenes[currentScene].SetShaderGlobal("projection", Perspective(PI / 2, ratio, -0.5, 1))
114f395b9144 did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents: 1138
diff changeset
86 scenes[currentScene].SetShaderGlobal(
245
a19c9d089f25 add: correct camera calculations
Sam <sam@basx.dev>
parents: 244
diff changeset
87 "view",
1136
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 1135
diff changeset
88 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
89 )
1138
02e1d2658ff5 did: more renaming
sam <sam@basx.dev>
parents: 1137
diff changeset
90 engine.RenderScene(scenes[currentScene])
02e1d2658ff5 did: more renaming
sam <sam@basx.dev>
parents: 1137
diff changeset
91 engine.Destroy()
222
ddfc54036e00 add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff changeset
92
ddfc54036e00 add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff changeset
93 when isMainModule:
ddfc54036e00 add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff changeset
94 main()