Mercurial > games > semicongine
annotate tests/test_mesh.nim @ 1002:892aa207a5b0
undid: some ugly formatting
author | sam <sam@basx.dev> |
---|---|
date | Sat, 13 Apr 2024 21:31:40 +0700 |
parents | 6406766a222d |
children | 73b572f82a1f |
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 |
ddfc54036e00
add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff
changeset
|
4 proc main() = |
239
f05497ef8fd2
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
230
diff
changeset
|
5 var scenes = [ |
373 | 6 Scene(name: "Donut", meshes: loadMeshes("donut.glb", COLORED_SINGLE_TEXTURE_MATERIAL)[0].toSeq), |
239
f05497ef8fd2
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
230
diff
changeset
|
7 ] |
222
ddfc54036e00
add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff
changeset
|
8 |
ddfc54036e00
add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff
changeset
|
9 var engine = initEngine("Test meshes") |
ddfc54036e00
add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff
changeset
|
10 const |
333
27aaf43e18b4
fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents:
318
diff
changeset
|
11 shaderConfiguration = createShaderConfiguration( |
420 | 12 inputs = [ |
13 attr[Vec3f]("position", memoryPerformanceHint = PreferFastRead), | |
14 attr[uint16](MATERIALINDEX_ATTRIBUTE, memoryPerformanceHint = PreferFastRead, perInstance = true), | |
15 attr[Vec2f]("texcoord_0", memoryPerformanceHint = PreferFastRead), | |
16 attr[Mat4]("transform", memoryPerformanceHint = PreferFastWrite, perInstance = true), | |
333
27aaf43e18b4
fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents:
318
diff
changeset
|
17 ], |
420 | 18 intermediates = [ |
333
27aaf43e18b4
fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents:
318
diff
changeset
|
19 attr[Vec4f]("vertexColor"), |
27aaf43e18b4
fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents:
318
diff
changeset
|
20 attr[Vec2f]("colorTexCoord"), |
420 | 21 attr[uint16]("materialIndexOut", noInterpolation = true) |
333
27aaf43e18b4
fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents:
318
diff
changeset
|
22 ], |
420 | 23 outputs = [attr[Vec4f]("color")], |
24 uniforms = [ | |
333
27aaf43e18b4
fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents:
318
diff
changeset
|
25 attr[Mat4]("projection"), |
27aaf43e18b4
fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents:
318
diff
changeset
|
26 attr[Mat4]("view"), |
420 | 27 attr[Vec4f]("color", arrayCount = 4), |
333
27aaf43e18b4
fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents:
318
diff
changeset
|
28 ], |
420 | 29 samplers = [attr[Texture]("baseTexture", arrayCount = 4)], |
30 vertexCode = &""" | |
333
27aaf43e18b4
fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents:
318
diff
changeset
|
31 gl_Position = vec4(position, 1.0) * (transform * Uniforms.view * Uniforms.projection); |
420 | 32 vertexColor = Uniforms.color[{MATERIALINDEX_ATTRIBUTE}]; |
333
27aaf43e18b4
fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents:
318
diff
changeset
|
33 colorTexCoord = texcoord_0; |
420 | 34 materialIndexOut = {MATERIALINDEX_ATTRIBUTE}; |
333
27aaf43e18b4
fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents:
318
diff
changeset
|
35 """, |
420 | 36 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
|
37 ) |
371
f054b8bacab8
fix: tests, test_materials and test_mesh still needs to be done
Sam <sam@basx.dev>
parents:
353
diff
changeset
|
38 engine.initRenderer({COLORED_SINGLE_TEXTURE_MATERIAL: shaderConfiguration}) |
333
27aaf43e18b4
fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents:
318
diff
changeset
|
39 |
239
f05497ef8fd2
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
230
diff
changeset
|
40 for scene in scenes.mitems: |
333
27aaf43e18b4
fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents:
318
diff
changeset
|
41 scene.addShaderGlobal("projection", Unit4F32) |
27aaf43e18b4
fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents:
318
diff
changeset
|
42 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
|
43 engine.loadScene(scene) |
333
27aaf43e18b4
fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents:
318
diff
changeset
|
44 |
239
f05497ef8fd2
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
230
diff
changeset
|
45 var |
247 | 46 size = 1'f32 |
246 | 47 elevation = 0'f32 |
239
f05497ef8fd2
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
230
diff
changeset
|
48 azimut = 0'f32 |
f05497ef8fd2
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
230
diff
changeset
|
49 currentScene = 0 |
f05497ef8fd2
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
230
diff
changeset
|
50 |
222
ddfc54036e00
add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff
changeset
|
51 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
|
52 if engine.keyWasPressed(`1`): |
f05497ef8fd2
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
230
diff
changeset
|
53 currentScene = 0 |
f05497ef8fd2
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
230
diff
changeset
|
54 elif engine.keyWasPressed(`2`): |
f05497ef8fd2
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
230
diff
changeset
|
55 currentScene = 1 |
f05497ef8fd2
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
230
diff
changeset
|
56 elif engine.keyWasPressed(`3`): |
f05497ef8fd2
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
230
diff
changeset
|
57 currentScene = 2 |
f05497ef8fd2
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
230
diff
changeset
|
58 elif engine.keyWasPressed(`4`): |
f05497ef8fd2
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
230
diff
changeset
|
59 currentScene = 3 |
f05497ef8fd2
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
230
diff
changeset
|
60 elif engine.keyWasPressed(`5`): |
f05497ef8fd2
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
230
diff
changeset
|
61 currentScene = 4 |
f05497ef8fd2
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
230
diff
changeset
|
62 elif engine.keyWasPressed(`6`): |
f05497ef8fd2
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
230
diff
changeset
|
63 currentScene = 5 |
f05497ef8fd2
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
230
diff
changeset
|
64 |
f05497ef8fd2
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
230
diff
changeset
|
65 if engine.keyWasPressed(NumberRowExtra3): |
246 | 66 size = 0.3'f32 |
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 |
246 | 70 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
|
71 size *= 1'f32 + engine.mouseWheel() * 0.05 |
f05497ef8fd2
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
230
diff
changeset
|
72 azimut += engine.mouseMove().x / 180'f32 |
f05497ef8fd2
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
230
diff
changeset
|
73 elevation -= engine.mouseMove().y / 180'f32 |
245 | 74 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
|
75 scenes[currentScene].setShaderGlobal( |
245 | 76 "view", |
333
27aaf43e18b4
fix: material handling, gltf loading, loader example
Sam <sam@basx.dev>
parents:
318
diff
changeset
|
77 scale(size, size, size) * rotate(elevation, newVec3f(1, 0, 0)) * rotate(azimut, Yf32) |
230 | 78 ) |
239
f05497ef8fd2
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
230
diff
changeset
|
79 engine.renderScene(scenes[currentScene]) |
222
ddfc54036e00
add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff
changeset
|
80 engine.destroy() |
ddfc54036e00
add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff
changeset
|
81 |
ddfc54036e00
add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff
changeset
|
82 when isMainModule: |
ddfc54036e00
add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff
changeset
|
83 main() |