Mercurial > games > semicongine
annotate tests/test_mesh.nim @ 1024:0edb6e38dcd5
did: refactor renderer a bit
author | sam <sam@basx.dev> |
---|---|
date | Mon, 20 May 2024 17:34:04 +0700 |
parents | 73b572f82a1f |
children | 74957cbf589b |
rev | line source |
---|---|
979 | 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 | 4 const |
5 MeshMaterial* = MaterialType( | |
6 name: "colored single texture material", | |
7 vertexAttributes: { | |
8 "position": Vec3F32, | |
9 "texcoord_0": Vec2F32, | |
10 }.toTable, | |
11 attributes: {"baseTexture": TextureType, "color": Vec4F32}.toTable | |
12 ) | |
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 = [ |
1021 | 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 |
ddfc54036e00
add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff
changeset
|
19 var engine = initEngine("Test meshes") |
ddfc54036e00
add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff
changeset
|
20 const |
333
27aaf43e18b4
fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents:
318
diff
changeset
|
21 shaderConfiguration = createShaderConfiguration( |
1021 | 22 name = "default shader", |
420 | 23 inputs = [ |
24 attr[Vec3f]("position", memoryPerformanceHint = PreferFastRead), | |
25 attr[uint16](MATERIALINDEX_ATTRIBUTE, memoryPerformanceHint = PreferFastRead, perInstance = true), | |
26 attr[Vec2f]("texcoord_0", memoryPerformanceHint = PreferFastRead), | |
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 | 29 intermediates = [ |
333
27aaf43e18b4
fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents:
318
diff
changeset
|
30 attr[Vec4f]("vertexColor"), |
27aaf43e18b4
fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents:
318
diff
changeset
|
31 attr[Vec2f]("colorTexCoord"), |
420 | 32 attr[uint16]("materialIndexOut", noInterpolation = true) |
333
27aaf43e18b4
fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents:
318
diff
changeset
|
33 ], |
420 | 34 outputs = [attr[Vec4f]("color")], |
35 uniforms = [ | |
333
27aaf43e18b4
fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents:
318
diff
changeset
|
36 attr[Mat4]("projection"), |
27aaf43e18b4
fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents:
318
diff
changeset
|
37 attr[Mat4]("view"), |
420 | 38 attr[Vec4f]("color", arrayCount = 4), |
333
27aaf43e18b4
fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents:
318
diff
changeset
|
39 ], |
420 | 40 samplers = [attr[Texture]("baseTexture", arrayCount = 4)], |
41 vertexCode = &""" | |
1021 | 42 gl_Position = vec4(position, 1.0) * (transform * (Uniforms.view * Uniforms.projection)); |
420 | 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 | 45 materialIndexOut = {MATERIALINDEX_ATTRIBUTE}; |
333
27aaf43e18b4
fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents:
318
diff
changeset
|
46 """, |
420 | 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 ) |
1021 | 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: |
333
27aaf43e18b4
fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents:
318
diff
changeset
|
52 scene.addShaderGlobal("projection", Unit4F32) |
27aaf43e18b4
fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents:
318
diff
changeset
|
53 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
|
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 | 57 size = 1'f32 |
246 | 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 |
1021 | 62 while engine.updateInputs() and not keyIsDown(Escape): |
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 |
1021 | 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 |
1021 | 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 |
1021 | 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 |
1021 | 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 |
1021 | 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 |
1021 | 76 if keyWasPressed(NumberRowExtra3): |
246 | 77 size = 0.3'f32 |
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 |
246 | 81 let ratio = engine.getWindow().size[0] / engine.getWindow().size[1] |
1021 | 82 size *= 1'f32 + mouseWheel() * 0.05 |
83 azimut += mouseMove().x / 180'f32 | |
84 elevation -= mouseMove().y / 180'f32 | |
85 scenes[currentScene].setShaderGlobal("projection", perspective(PI / 2, ratio, -0.5, 1)) | |
239
f05497ef8fd2
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
230
diff
changeset
|
86 scenes[currentScene].setShaderGlobal( |
245 | 87 "view", |
333
27aaf43e18b4
fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents:
318
diff
changeset
|
88 scale(size, size, size) * rotate(elevation, newVec3f(1, 0, 0)) * rotate(azimut, Yf32) |
230 | 89 ) |
239
f05497ef8fd2
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
230
diff
changeset
|
90 engine.renderScene(scenes[currentScene]) |
222
ddfc54036e00
add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff
changeset
|
91 engine.destroy() |
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() |