Mercurial > games > semicongine
annotate tests/test_mesh.nim @ 252:f31d848ab551
did: update configs
author | sam <sam@basx.dev> |
---|---|
date | Wed, 24 May 2023 01:31:02 +0700 |
parents | beb41c93aa3f |
children | ad078e26a1c7 |
rev | line source |
---|---|
222
ddfc54036e00
add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff
changeset
|
1 import semicongine |
ddfc54036e00
add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff
changeset
|
2 |
ddfc54036e00
add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff
changeset
|
3 proc main() = |
247 | 4 var ent1 = newEntity("hoho", rect()) |
5 var ent2 = newEntity("hehe", ent1) | |
6 var myScene = newScene("hi", ent2) | |
7 myScene.root.transform = translate3d(0.2'f32, 0'f32, 0'f32) | |
8 myScene.root.children[0].transform = translate3d(0'f32, 0.2'f32, 0'f32) | |
239
f05497ef8fd2
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
230
diff
changeset
|
9 var scenes = [ |
247 | 10 # loadScene("default_cube.glb", "1"), |
11 # loadScene("default_cube1.glb", "3"), | |
12 # loadScene("default_cube2.glb", "4"), | |
13 # loadScene("flat.glb", "5"), | |
239
f05497ef8fd2
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
230
diff
changeset
|
14 loadScene("tutorialk-donat.glb", "6"), |
247 | 15 # myScene, |
16 # loadScene("personv3.glb", "2"), | |
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 |
239
f05497ef8fd2
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
230
diff
changeset
|
21 vertexInput = @[ |
f05497ef8fd2
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
230
diff
changeset
|
22 attr[Vec3f]("position", memoryPerformanceHint=PreferFastRead), |
f05497ef8fd2
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
230
diff
changeset
|
23 attr[uint8]("material", memoryPerformanceHint=PreferFastRead), |
247 | 24 attr[Vec2f]("texcoord_0", memoryPerformanceHint=PreferFastRead), |
25 attr[Mat4]("transform", memoryPerformanceHint=PreferFastWrite, perInstance=true), | |
239
f05497ef8fd2
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
230
diff
changeset
|
26 ] |
247 | 27 vertexOutput = @[attr[Vec4f]("vertexColor"), attr[Vec2f]("colorTexCoord")] |
222
ddfc54036e00
add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff
changeset
|
28 fragOutput = @[attr[Vec4f]("color")] |
245 | 29 uniforms = @[ |
30 attr[Mat4]("projection"), | |
31 attr[Mat4]("view"), | |
32 attr[Vec4f]("material_color", arrayCount=16), | |
33 ] | |
247 | 34 samplers = @[ |
35 attr[Sampler2DType]("material_color_texture", arrayCount=1), | |
36 ] | |
222
ddfc54036e00
add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff
changeset
|
37 vertexCode = compileGlslShader( |
ddfc54036e00
add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff
changeset
|
38 stage=VK_SHADER_STAGE_VERTEX_BIT, |
ddfc54036e00
add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff
changeset
|
39 inputs=vertexInput, |
239
f05497ef8fd2
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
230
diff
changeset
|
40 outputs=vertexOutput, |
222
ddfc54036e00
add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff
changeset
|
41 uniforms=uniforms, |
247 | 42 samplers=samplers, |
43 main=""" | |
44 gl_Position = vec4(position, 1.0) * (transform * Uniforms.view * Uniforms.projection); | |
45 vertexColor = Uniforms.material_color[material]; | |
46 colorTexCoord = texcoord_0; | |
47 """ | |
222
ddfc54036e00
add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff
changeset
|
48 ) |
ddfc54036e00
add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff
changeset
|
49 fragmentCode = compileGlslShader( |
ddfc54036e00
add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff
changeset
|
50 stage=VK_SHADER_STAGE_FRAGMENT_BIT, |
239
f05497ef8fd2
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
230
diff
changeset
|
51 inputs=vertexOutput, |
222
ddfc54036e00
add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff
changeset
|
52 outputs=fragOutput, |
ddfc54036e00
add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff
changeset
|
53 uniforms=uniforms, |
247 | 54 samplers=samplers, |
55 main="""color = texture(material_color_texture[0], colorTexCoord) * vertexColor;""" | |
222
ddfc54036e00
add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff
changeset
|
56 ) |
239
f05497ef8fd2
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
230
diff
changeset
|
57 engine.setRenderer(engine.gpuDevice.simpleForwardRenderPass(vertexCode, fragmentCode, clearColor=newVec4f(0, 0, 0, 1))) |
f05497ef8fd2
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
230
diff
changeset
|
58 for scene in scenes.mitems: |
247 | 59 engine.addScene(scene, vertexInput, transformAttribute="transform") |
245 | 60 scene.addShaderGlobal("projection", Unit4) |
61 scene.addShaderGlobal("view", Unit4) | |
239
f05497ef8fd2
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
230
diff
changeset
|
62 var |
247 | 63 size = 1'f32 |
246 | 64 elevation = 0'f32 |
239
f05497ef8fd2
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
230
diff
changeset
|
65 azimut = 0'f32 |
f05497ef8fd2
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
230
diff
changeset
|
66 currentScene = 0 |
f05497ef8fd2
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
230
diff
changeset
|
67 |
222
ddfc54036e00
add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff
changeset
|
68 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
|
69 if engine.keyWasPressed(`1`): |
f05497ef8fd2
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
230
diff
changeset
|
70 currentScene = 0 |
f05497ef8fd2
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
230
diff
changeset
|
71 elif engine.keyWasPressed(`2`): |
f05497ef8fd2
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
230
diff
changeset
|
72 currentScene = 1 |
f05497ef8fd2
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
230
diff
changeset
|
73 elif engine.keyWasPressed(`3`): |
f05497ef8fd2
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
230
diff
changeset
|
74 currentScene = 2 |
f05497ef8fd2
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
230
diff
changeset
|
75 elif engine.keyWasPressed(`4`): |
f05497ef8fd2
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
230
diff
changeset
|
76 currentScene = 3 |
f05497ef8fd2
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
230
diff
changeset
|
77 elif engine.keyWasPressed(`5`): |
f05497ef8fd2
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
230
diff
changeset
|
78 currentScene = 4 |
f05497ef8fd2
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
230
diff
changeset
|
79 elif engine.keyWasPressed(`6`): |
f05497ef8fd2
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
230
diff
changeset
|
80 currentScene = 5 |
f05497ef8fd2
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
230
diff
changeset
|
81 |
f05497ef8fd2
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
230
diff
changeset
|
82 if engine.keyWasPressed(NumberRowExtra3): |
246 | 83 size = 0.3'f32 |
84 elevation = 0'f32 | |
239
f05497ef8fd2
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
230
diff
changeset
|
85 azimut = 0'f32 |
f05497ef8fd2
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
230
diff
changeset
|
86 |
246 | 87 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
|
88 size *= 1'f32 + engine.mouseWheel() * 0.05 |
f05497ef8fd2
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
230
diff
changeset
|
89 azimut += engine.mouseMove().x / 180'f32 |
f05497ef8fd2
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
230
diff
changeset
|
90 elevation -= engine.mouseMove().y / 180'f32 |
245 | 91 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
|
92 scenes[currentScene].setShaderGlobal( |
245 | 93 "view", |
94 scale3d(size, size, size) * rotate3d(elevation, newVec3f(1, 0, 0)) * rotate3d(azimut, Yf32) | |
230 | 95 ) |
239
f05497ef8fd2
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
230
diff
changeset
|
96 engine.renderScene(scenes[currentScene]) |
222
ddfc54036e00
add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff
changeset
|
97 engine.destroy() |
ddfc54036e00
add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff
changeset
|
98 |
ddfc54036e00
add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff
changeset
|
99 when isMainModule: |
ddfc54036e00
add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff
changeset
|
100 main() |