# HG changeset patch # User Sam # Date 1684758065 -25200 # Node ID 0936ae49f0c4cd1eaf6be21f97b16da071ff503a # Parent 37ef17c96bdb362db5d8c6df920ba70f1f37ac8b add: correct camera calculations diff -r 37ef17c96bdb -r 0936ae49f0c4 src/semicongine/core/matrix.nim --- a/src/semicongine/core/matrix.nim Mon May 22 00:51:41 2023 +0700 +++ b/src/semicongine/core/matrix.nim Mon May 22 19:21:05 2023 +0700 @@ -378,19 +378,19 @@ makeRandomInit(TMat43) makeRandomInit(TMat4) -func perspective*[T: SomeFloat](fovy, aspect, zNear, zFar: T): TMat4[T] = - let tanHalfFovy = tan(fovy / T(2)) - return TMat4[T](data:[ - T(1) / (aspect * tanHalfFovy), T(0), T(0), T(0), - T(0), T(1) / tanHalfFovy, T(0), T(0), - T(0), T(0), T(zFar / (zFar - zNear)), T(-(zFar * zNear) / (zFar - zNear)), - T(0), T(0), T(1), T(1), +func perspective*(fovy, aspect, zNear, zFar: float32): Mat4 = + let tanHalfFovy = tan(fovy / 2) + return Mat4(data:[ + 1 / (aspect * tanHalfFovy), 0, 0, 0, + 0, 1 / tanHalfFovy, 0, 0, + 0, 0, zFar / (zFar - zNear), -(zFar * zNear) / (zFar - zNear), + 0, 0, 1, 1, ]) -func ortho*[T: SomeFloat](left, right, top, bottom, zNear, zFar: T): TMat4[T] = - TMat4[T](data:[ - T(2) / (right - left), T(0), T(0), -(right + left) / (right - left), - T(0), T(2) / (bottom - top), T(0), -(bottom + top) / (bottom - top), - T(0), T(0), T(1) / (zFar - zNear), -zNear / (zFar - zNear), - T(0), T(0), T(1), T(1), +func ortho*(left, right, top, bottom, zNear, zFar: float32): Mat4 = + Mat4(data:[ + 2 / (right - left), 0, 0, -(right + left) / (right - left), + 0, 2 / (bottom - top), 0, -(bottom + top) / (bottom - top), + 0, 0, 1 / (zFar - zNear), -zNear / (zFar - zNear), + 0, 0, 1, 1, ]) diff -r 37ef17c96bdb -r 0936ae49f0c4 tests/test_mesh.nim --- a/tests/test_mesh.nim Mon May 22 00:51:41 2023 +0700 +++ b/tests/test_mesh.nim Mon May 22 19:21:05 2023 +0700 @@ -18,13 +18,18 @@ ] vertexOutput = @[attr[Vec4f]("vertexColor")] fragOutput = @[attr[Vec4f]("color")] - uniforms = @[attr[Mat4]("transform"), attr[Vec4f]("material_color", arrayCount=16), ] + uniforms = @[ + attr[Mat4]("projection"), + attr[Mat4]("view"), + attr[Mat4]("model"), + attr[Vec4f]("material_color", arrayCount=16), + ] vertexCode = compileGlslShader( stage=VK_SHADER_STAGE_VERTEX_BIT, inputs=vertexInput, outputs=vertexOutput, uniforms=uniforms, - main="""gl_Position = vec4(position, 1.0) * Uniforms.transform; vertexColor = Uniforms.material_color[material];""" + main="""gl_Position = Uniforms.projection * Uniforms.view * Uniforms.model * vec4(position, 1.0); vertexColor = Uniforms.material_color[material];""" ) fragmentCode = compileGlslShader( stage=VK_SHADER_STAGE_FRAGMENT_BIT, @@ -36,7 +41,9 @@ engine.setRenderer(engine.gpuDevice.simpleForwardRenderPass(vertexCode, fragmentCode, clearColor=newVec4f(0, 0, 0, 1))) for scene in scenes.mitems: engine.addScene(scene, vertexInput) - scene.addShaderGlobal("transform", Unit4) + scene.addShaderGlobal("projection", Unit4) + scene.addShaderGlobal("view", Unit4) + scene.addShaderGlobal("model", Unit4) var size = 1'f32 elevation = -float32(PI) / 3'f32 @@ -61,14 +68,17 @@ size = 1'f32 elevation = -float32(PI) / 3'f32 azimut = 0'f32 + var ratio = engine.getWindow().size[0] / engine.getWindow().size[1] size *= 1'f32 + engine.mouseWheel() * 0.05 azimut += engine.mouseMove().x / 180'f32 elevation -= engine.mouseMove().y / 180'f32 + scenes[currentScene].setShaderGlobal("projection", ortho(-ratio, ratio, -1, 1, -1, 1)) scenes[currentScene].setShaderGlobal( - "transform", - scale3d(size, size, size) * rotate3d(elevation, newVec3f(1, 0, 0)) * rotate3d(azimut, Yf32) + "view", + scale3d(size, size, size) * rotate3d(elevation, newVec3f(1, 0, 0)) * rotate3d(azimut, Yf32) ) + scenes[currentScene].setShaderGlobal("model", Unit4f32) engine.renderScene(scenes[currentScene]) engine.destroy()