Mercurial > games > semicongine
comparison tests/test_mesh.nim @ 706:0936ae49f0c4
add: correct camera calculations
author | Sam <sam@basx.dev> |
---|---|
date | Mon, 22 May 2023 19:21:05 +0700 |
parents | 37ef17c96bdb |
children | dcbd9f256f6a |
comparison
equal
deleted
inserted
replaced
705:37ef17c96bdb | 706:0936ae49f0c4 |
---|---|
16 attr[Vec3f]("position", memoryPerformanceHint=PreferFastRead), | 16 attr[Vec3f]("position", memoryPerformanceHint=PreferFastRead), |
17 attr[uint8]("material", memoryPerformanceHint=PreferFastRead), | 17 attr[uint8]("material", memoryPerformanceHint=PreferFastRead), |
18 ] | 18 ] |
19 vertexOutput = @[attr[Vec4f]("vertexColor")] | 19 vertexOutput = @[attr[Vec4f]("vertexColor")] |
20 fragOutput = @[attr[Vec4f]("color")] | 20 fragOutput = @[attr[Vec4f]("color")] |
21 uniforms = @[attr[Mat4]("transform"), attr[Vec4f]("material_color", arrayCount=16), ] | 21 uniforms = @[ |
22 attr[Mat4]("projection"), | |
23 attr[Mat4]("view"), | |
24 attr[Mat4]("model"), | |
25 attr[Vec4f]("material_color", arrayCount=16), | |
26 ] | |
22 vertexCode = compileGlslShader( | 27 vertexCode = compileGlslShader( |
23 stage=VK_SHADER_STAGE_VERTEX_BIT, | 28 stage=VK_SHADER_STAGE_VERTEX_BIT, |
24 inputs=vertexInput, | 29 inputs=vertexInput, |
25 outputs=vertexOutput, | 30 outputs=vertexOutput, |
26 uniforms=uniforms, | 31 uniforms=uniforms, |
27 main="""gl_Position = vec4(position, 1.0) * Uniforms.transform; vertexColor = Uniforms.material_color[material];""" | 32 main="""gl_Position = Uniforms.projection * Uniforms.view * Uniforms.model * vec4(position, 1.0); vertexColor = Uniforms.material_color[material];""" |
28 ) | 33 ) |
29 fragmentCode = compileGlslShader( | 34 fragmentCode = compileGlslShader( |
30 stage=VK_SHADER_STAGE_FRAGMENT_BIT, | 35 stage=VK_SHADER_STAGE_FRAGMENT_BIT, |
31 inputs=vertexOutput, | 36 inputs=vertexOutput, |
32 outputs=fragOutput, | 37 outputs=fragOutput, |
34 main="""color = vertexColor;""" | 39 main="""color = vertexColor;""" |
35 ) | 40 ) |
36 engine.setRenderer(engine.gpuDevice.simpleForwardRenderPass(vertexCode, fragmentCode, clearColor=newVec4f(0, 0, 0, 1))) | 41 engine.setRenderer(engine.gpuDevice.simpleForwardRenderPass(vertexCode, fragmentCode, clearColor=newVec4f(0, 0, 0, 1))) |
37 for scene in scenes.mitems: | 42 for scene in scenes.mitems: |
38 engine.addScene(scene, vertexInput) | 43 engine.addScene(scene, vertexInput) |
39 scene.addShaderGlobal("transform", Unit4) | 44 scene.addShaderGlobal("projection", Unit4) |
45 scene.addShaderGlobal("view", Unit4) | |
46 scene.addShaderGlobal("model", Unit4) | |
40 var | 47 var |
41 size = 1'f32 | 48 size = 1'f32 |
42 elevation = -float32(PI) / 3'f32 | 49 elevation = -float32(PI) / 3'f32 |
43 azimut = 0'f32 | 50 azimut = 0'f32 |
44 currentScene = 0 | 51 currentScene = 0 |
59 | 66 |
60 if engine.keyWasPressed(NumberRowExtra3): | 67 if engine.keyWasPressed(NumberRowExtra3): |
61 size = 1'f32 | 68 size = 1'f32 |
62 elevation = -float32(PI) / 3'f32 | 69 elevation = -float32(PI) / 3'f32 |
63 azimut = 0'f32 | 70 azimut = 0'f32 |
71 var ratio = engine.getWindow().size[0] / engine.getWindow().size[1] | |
64 | 72 |
65 size *= 1'f32 + engine.mouseWheel() * 0.05 | 73 size *= 1'f32 + engine.mouseWheel() * 0.05 |
66 azimut += engine.mouseMove().x / 180'f32 | 74 azimut += engine.mouseMove().x / 180'f32 |
67 elevation -= engine.mouseMove().y / 180'f32 | 75 elevation -= engine.mouseMove().y / 180'f32 |
76 scenes[currentScene].setShaderGlobal("projection", ortho(-ratio, ratio, -1, 1, -1, 1)) | |
68 scenes[currentScene].setShaderGlobal( | 77 scenes[currentScene].setShaderGlobal( |
69 "transform", | 78 "view", |
70 scale3d(size, size, size) * rotate3d(elevation, newVec3f(1, 0, 0)) * rotate3d(azimut, Yf32) | 79 scale3d(size, size, size) * rotate3d(elevation, newVec3f(1, 0, 0)) * rotate3d(azimut, Yf32) |
71 ) | 80 ) |
81 scenes[currentScene].setShaderGlobal("model", Unit4f32) | |
72 engine.renderScene(scenes[currentScene]) | 82 engine.renderScene(scenes[currentScene]) |
73 engine.destroy() | 83 engine.destroy() |
74 | 84 |
75 | 85 |
76 when isMainModule: | 86 when isMainModule: |