Mercurial > games > semicongine
annotate tests/test_mesh.nim @ 707:3742bba2293f
fix: better defaults
author | Sam <sam@basx.dev> |
---|---|
date | Mon, 22 May 2023 19:27:17 +0700 |
parents | 0936ae49f0c4 |
children | beb41c93aa3f |
rev | line source |
---|---|
683
2ca938595aea
add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff
changeset
|
1 import semicongine |
2ca938595aea
add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff
changeset
|
2 |
2ca938595aea
add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff
changeset
|
3 proc main() = |
700
abc0d97392cb
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
691
diff
changeset
|
4 var scenes = [ |
abc0d97392cb
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
691
diff
changeset
|
5 loadScene("default_cube.glb", "1"), |
abc0d97392cb
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
691
diff
changeset
|
6 loadScene("default_cube1.glb", "3"), |
abc0d97392cb
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
691
diff
changeset
|
7 loadScene("default_cube2.glb", "4"), |
abc0d97392cb
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
691
diff
changeset
|
8 loadScene("flat.glb", "5"), |
abc0d97392cb
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
691
diff
changeset
|
9 loadScene("tutorialk-donat.glb", "6"), |
abc0d97392cb
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
691
diff
changeset
|
10 loadScene("personv3.glb", "2"), |
abc0d97392cb
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
691
diff
changeset
|
11 ] |
683
2ca938595aea
add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff
changeset
|
12 |
2ca938595aea
add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff
changeset
|
13 var engine = initEngine("Test meshes") |
2ca938595aea
add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff
changeset
|
14 const |
700
abc0d97392cb
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
691
diff
changeset
|
15 vertexInput = @[ |
abc0d97392cb
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
691
diff
changeset
|
16 attr[Vec3f]("position", memoryPerformanceHint=PreferFastRead), |
abc0d97392cb
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
691
diff
changeset
|
17 attr[uint8]("material", memoryPerformanceHint=PreferFastRead), |
abc0d97392cb
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
691
diff
changeset
|
18 ] |
abc0d97392cb
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
691
diff
changeset
|
19 vertexOutput = @[attr[Vec4f]("vertexColor")] |
683
2ca938595aea
add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff
changeset
|
20 fragOutput = @[attr[Vec4f]("color")] |
706 | 21 uniforms = @[ |
22 attr[Mat4]("projection"), | |
23 attr[Mat4]("view"), | |
24 attr[Mat4]("model"), | |
25 attr[Vec4f]("material_color", arrayCount=16), | |
26 ] | |
683
2ca938595aea
add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff
changeset
|
27 vertexCode = compileGlslShader( |
2ca938595aea
add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff
changeset
|
28 stage=VK_SHADER_STAGE_VERTEX_BIT, |
2ca938595aea
add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff
changeset
|
29 inputs=vertexInput, |
700
abc0d97392cb
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
691
diff
changeset
|
30 outputs=vertexOutput, |
683
2ca938595aea
add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff
changeset
|
31 uniforms=uniforms, |
706 | 32 main="""gl_Position = Uniforms.projection * Uniforms.view * Uniforms.model * vec4(position, 1.0); vertexColor = Uniforms.material_color[material];""" |
683
2ca938595aea
add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff
changeset
|
33 ) |
2ca938595aea
add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff
changeset
|
34 fragmentCode = compileGlslShader( |
2ca938595aea
add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff
changeset
|
35 stage=VK_SHADER_STAGE_FRAGMENT_BIT, |
700
abc0d97392cb
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
691
diff
changeset
|
36 inputs=vertexOutput, |
683
2ca938595aea
add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff
changeset
|
37 outputs=fragOutput, |
2ca938595aea
add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff
changeset
|
38 uniforms=uniforms, |
700
abc0d97392cb
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
691
diff
changeset
|
39 main="""color = vertexColor;""" |
683
2ca938595aea
add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff
changeset
|
40 ) |
700
abc0d97392cb
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
691
diff
changeset
|
41 engine.setRenderer(engine.gpuDevice.simpleForwardRenderPass(vertexCode, fragmentCode, clearColor=newVec4f(0, 0, 0, 1))) |
abc0d97392cb
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
691
diff
changeset
|
42 for scene in scenes.mitems: |
abc0d97392cb
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
691
diff
changeset
|
43 engine.addScene(scene, vertexInput) |
706 | 44 scene.addShaderGlobal("projection", Unit4) |
45 scene.addShaderGlobal("view", Unit4) | |
46 scene.addShaderGlobal("model", Unit4) | |
700
abc0d97392cb
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
691
diff
changeset
|
47 var |
707 | 48 size = 0.3'f32 |
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 | 68 size = 0.3'f32 |
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 | 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 | 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 | 78 "view", |
79 scale3d(size, size, size) * rotate3d(elevation, newVec3f(1, 0, 0)) * rotate3d(azimut, Yf32) | |
691 | 80 ) |
706 | 81 scenes[currentScene].setShaderGlobal("model", Unit4f32) |
700
abc0d97392cb
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
691
diff
changeset
|
82 engine.renderScene(scenes[currentScene]) |
683
2ca938595aea
add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff
changeset
|
83 engine.destroy() |
2ca938595aea
add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff
changeset
|
84 |
2ca938595aea
add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff
changeset
|
85 |
2ca938595aea
add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff
changeset
|
86 when isMainModule: |
2ca938595aea
add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff
changeset
|
87 main() |