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