Mercurial > games > semicongine
comparison tests/test_mesh.nim @ 239:f05497ef8fd2
did: nicer test example, scene switching still problematic
author | Sam <sam@basx.dev> |
---|---|
date | Sat, 20 May 2023 23:21:03 +0700 |
parents | 027f6ff06585 |
children | f52fccedf5ab |
comparison
equal
deleted
inserted
replaced
238:ef8cea2545f8 | 239:f05497ef8fd2 |
---|---|
1 import std/times | |
2 | |
3 import semicongine | 1 import semicongine |
4 | 2 |
5 proc main() = | 3 proc main() = |
6 var scene = loadScene("default_cube.glb") | 4 var scenes = [ |
5 loadScene("default_cube.glb", "1"), | |
6 loadScene("default_cube1.glb", "3"), | |
7 loadScene("default_cube2.glb", "4"), | |
8 loadScene("flat.glb", "5"), | |
9 loadScene("tutorialk-donat.glb", "6"), | |
10 loadScene("personv3.glb", "2"), | |
11 ] | |
7 | 12 |
8 var engine = initEngine("Test meshes") | 13 var engine = initEngine("Test meshes") |
9 const | 14 const |
10 vertexInput = @[attr[Vec3f]("POSITION", memoryPerformanceHint=PreferFastRead)] | 15 vertexInput = @[ |
16 attr[Vec3f]("position", memoryPerformanceHint=PreferFastRead), | |
17 attr[uint8]("material", memoryPerformanceHint=PreferFastRead), | |
18 ] | |
19 vertexOutput = @[attr[Vec4f]("vertexColor")] | |
11 fragOutput = @[attr[Vec4f]("color")] | 20 fragOutput = @[attr[Vec4f]("color")] |
12 uniforms = @[attr[Mat4]("transform")] | 21 uniforms = @[attr[Mat4]("transform"), attr[Vec4f]("material_colors", arrayCount=16), ] |
13 vertexCode = compileGlslShader( | 22 vertexCode = compileGlslShader( |
14 stage=VK_SHADER_STAGE_VERTEX_BIT, | 23 stage=VK_SHADER_STAGE_VERTEX_BIT, |
15 inputs=vertexInput, | 24 inputs=vertexInput, |
25 outputs=vertexOutput, | |
16 uniforms=uniforms, | 26 uniforms=uniforms, |
17 main="""gl_Position = vec4(POSITION, 1.0) * Uniforms.transform;""" | 27 main="""gl_Position = vec4(position, 1.0) * Uniforms.transform; vertexColor = Uniforms.material_colors[material];""" |
18 ) | 28 ) |
19 fragmentCode = compileGlslShader( | 29 fragmentCode = compileGlslShader( |
20 stage=VK_SHADER_STAGE_FRAGMENT_BIT, | 30 stage=VK_SHADER_STAGE_FRAGMENT_BIT, |
31 inputs=vertexOutput, | |
21 outputs=fragOutput, | 32 outputs=fragOutput, |
22 uniforms=uniforms, | 33 uniforms=uniforms, |
23 main=""" | 34 main="""color = vertexColor;""" |
24 color = vec4(61/255, 43/255, 31/255, 1); | |
25 """ | |
26 ) | 35 ) |
27 engine.setRenderer(engine.gpuDevice.simpleForwardRenderPass(vertexCode, fragmentCode)) | 36 engine.setRenderer(engine.gpuDevice.simpleForwardRenderPass(vertexCode, fragmentCode, clearColor=newVec4f(0, 0, 0, 1))) |
28 engine.addScene(scene, vertexInput) | 37 for scene in scenes.mitems: |
29 let rotateAxis = newVec3f(0, 1, 0) | 38 engine.addScene(scene, vertexInput) |
30 scene.addShaderGlobal("transform", Unit4) | 39 scene.addShaderGlobal("transform", Unit4) |
31 var t = cpuTime() | 40 var |
41 size = 1'f32 | |
42 elevation = -float32(PI) / 3'f32 | |
43 azimut = 0'f32 | |
44 currentScene = 0 | |
45 | |
32 while engine.updateInputs() == Running and not engine.keyIsDown(Escape): | 46 while engine.updateInputs() == Running and not engine.keyIsDown(Escape): |
33 scene.setShaderGlobal( | 47 if engine.keyWasPressed(`1`): |
48 currentScene = 0 | |
49 elif engine.keyWasPressed(`2`): | |
50 currentScene = 1 | |
51 elif engine.keyWasPressed(`3`): | |
52 currentScene = 2 | |
53 elif engine.keyWasPressed(`4`): | |
54 currentScene = 3 | |
55 elif engine.keyWasPressed(`5`): | |
56 currentScene = 4 | |
57 elif engine.keyWasPressed(`6`): | |
58 currentScene = 5 | |
59 | |
60 if engine.keyWasPressed(NumberRowExtra3): | |
61 size = 1'f32 | |
62 elevation = -float32(PI) / 3'f32 | |
63 azimut = 0'f32 | |
64 | |
65 size *= 1'f32 + engine.mouseWheel() * 0.05 | |
66 azimut += engine.mouseMove().x / 180'f32 | |
67 elevation -= engine.mouseMove().y / 180'f32 | |
68 scenes[currentScene].setShaderGlobal( | |
34 "transform", | 69 "transform", |
35 scale3d(0.2'f32, 0.2'f32, 0.2'f32) * translate3d(0'f32, 0'f32, 1.8'f32) * rotate3d(float32(PI) / 4'f32, newVec3f(1, 0, 0)) * rotate3d(-float32(cpuTime() - t), rotateAxis) | 70 scale3d(size, size, size) * rotate3d(elevation, newVec3f(1, 0, 0)) * rotate3d(azimut, Yf32) |
36 ) | 71 ) |
37 engine.renderScene(scene) | 72 engine.renderScene(scenes[currentScene]) |
38 engine.destroy() | 73 engine.destroy() |
39 | 74 |
40 | 75 |
41 when isMainModule: | 76 when isMainModule: |
42 main() | 77 main() |