Mercurial > games > semicongine
annotate tests/test_mesh.nim @ 316:b145a05c2459
add: changing rendering system, not finished yet, also upgrading to Nim 2
author | Sam <sam@basx.dev> |
---|---|
date | Mon, 07 Aug 2023 00:23:00 +0700 |
parents | da0bd61abe91 |
children | e656c0aad093 |
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() = |
302 | 4 var ent1 = newEntity("hoho", {"mesh": Component(rect())}) |
5 var ent2 = newEntity("hehe", [], ent1) | |
247 | 6 var myScene = newScene("hi", ent2) |
7 myScene.root.transform = translate3d(0.2'f32, 0'f32, 0'f32) | |
316
b145a05c2459
add: changing rendering system, not finished yet, also upgrading to Nim 2
Sam <sam@basx.dev>
parents:
302
diff
changeset
|
8 myScene.root[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 = [ |
268 | 10 # loadScene("default_cube.glb", "1"), |
247 | 11 # loadScene("default_cube1.glb", "3"), |
12 # loadScene("default_cube2.glb", "4"), | |
13 # loadScene("flat.glb", "5"), | |
268 | 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 ] |
269 | 27 intermediate = @[ |
268 | 28 attr[Vec4f]("vertexColor"), |
29 attr[Vec2f]("colorTexCoord"), | |
30 attr[uint8]("materialId", noInterpolation=true) | |
31 ] | |
222
ddfc54036e00
add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff
changeset
|
32 fragOutput = @[attr[Vec4f]("color")] |
245 | 33 uniforms = @[ |
34 attr[Mat4]("projection"), | |
35 attr[Mat4]("view"), | |
268 | 36 attr[Vec4f]("baseColorFactor", arrayCount=4), |
245 | 37 ] |
269 | 38 samplers = @[attr[Sampler2DType]("baseColorTexture", arrayCount=4)] |
39 (vertexCode, fragmentCode) = compileVertexFragmentShaderSet( | |
222
ddfc54036e00
add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff
changeset
|
40 inputs=vertexInput, |
269 | 41 intermediate=intermediate, |
42 outputs=fragOutput, | |
222
ddfc54036e00
add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff
changeset
|
43 uniforms=uniforms, |
247 | 44 samplers=samplers, |
269 | 45 vertexCode=""" |
247 | 46 gl_Position = vec4(position, 1.0) * (transform * Uniforms.view * Uniforms.projection); |
268 | 47 vertexColor = Uniforms.baseColorFactor[material]; |
48 colorTexCoord = texcoord_0; | |
49 materialId = material; | |
269 | 50 """, |
51 fragmentCode=""" | |
268 | 52 vec4 col[4] = vec4[4](vec4(1, 0, 0, 1), vec4(0, 1, 0, 1), vec4(0, 0, 1, 1), vec4(1, 1, 1, 1)); |
53 color = texture(baseColorTexture[materialId], colorTexCoord) * vertexColor; | |
54 """ | |
222
ddfc54036e00
add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff
changeset
|
55 ) |
239
f05497ef8fd2
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
230
diff
changeset
|
56 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
|
57 for scene in scenes.mitems: |
316
b145a05c2459
add: changing rendering system, not finished yet, also upgrading to Nim 2
Sam <sam@basx.dev>
parents:
302
diff
changeset
|
58 engine.addScene(scene, vertexInput, samplers, transformAttribute="transform", materialIndexAttribute="") |
245 | 59 scene.addShaderGlobal("projection", Unit4) |
60 scene.addShaderGlobal("view", Unit4) | |
239
f05497ef8fd2
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
230
diff
changeset
|
61 var |
247 | 62 size = 1'f32 |
246 | 63 elevation = 0'f32 |
239
f05497ef8fd2
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
230
diff
changeset
|
64 azimut = 0'f32 |
f05497ef8fd2
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
230
diff
changeset
|
65 currentScene = 0 |
f05497ef8fd2
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
230
diff
changeset
|
66 |
222
ddfc54036e00
add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff
changeset
|
67 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
|
68 if engine.keyWasPressed(`1`): |
f05497ef8fd2
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
230
diff
changeset
|
69 currentScene = 0 |
f05497ef8fd2
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
230
diff
changeset
|
70 elif engine.keyWasPressed(`2`): |
f05497ef8fd2
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
230
diff
changeset
|
71 currentScene = 1 |
f05497ef8fd2
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
230
diff
changeset
|
72 elif engine.keyWasPressed(`3`): |
f05497ef8fd2
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
230
diff
changeset
|
73 currentScene = 2 |
f05497ef8fd2
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
230
diff
changeset
|
74 elif engine.keyWasPressed(`4`): |
f05497ef8fd2
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
230
diff
changeset
|
75 currentScene = 3 |
f05497ef8fd2
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
230
diff
changeset
|
76 elif engine.keyWasPressed(`5`): |
f05497ef8fd2
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
230
diff
changeset
|
77 currentScene = 4 |
f05497ef8fd2
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
230
diff
changeset
|
78 elif engine.keyWasPressed(`6`): |
f05497ef8fd2
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
230
diff
changeset
|
79 currentScene = 5 |
f05497ef8fd2
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
230
diff
changeset
|
80 |
f05497ef8fd2
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
230
diff
changeset
|
81 if engine.keyWasPressed(NumberRowExtra3): |
246 | 82 size = 0.3'f32 |
83 elevation = 0'f32 | |
239
f05497ef8fd2
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
230
diff
changeset
|
84 azimut = 0'f32 |
f05497ef8fd2
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
230
diff
changeset
|
85 |
246 | 86 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
|
87 size *= 1'f32 + engine.mouseWheel() * 0.05 |
f05497ef8fd2
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
230
diff
changeset
|
88 azimut += engine.mouseMove().x / 180'f32 |
f05497ef8fd2
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
230
diff
changeset
|
89 elevation -= engine.mouseMove().y / 180'f32 |
245 | 90 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
|
91 scenes[currentScene].setShaderGlobal( |
245 | 92 "view", |
93 scale3d(size, size, size) * rotate3d(elevation, newVec3f(1, 0, 0)) * rotate3d(azimut, Yf32) | |
230 | 94 ) |
239
f05497ef8fd2
did: nicer test example, scene switching still problematic
Sam <sam@basx.dev>
parents:
230
diff
changeset
|
95 engine.renderScene(scenes[currentScene]) |
222
ddfc54036e00
add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff
changeset
|
96 engine.destroy() |
ddfc54036e00
add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff
changeset
|
97 |
ddfc54036e00
add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff
changeset
|
98 when isMainModule: |
ddfc54036e00
add: basic loading of glTF files (*.glb), no materials yet
Sam <sam@basx.dev>
parents:
diff
changeset
|
99 main() |