Mercurial > games > semicongine
changeset 362:3eccdbf32c53
add: support inverse matrix and normalized mouse position
author | Sam <sam@basx.dev> |
---|---|
date | Fri, 29 Sep 2023 19:30:07 +0700 |
parents | 97e913c0e7fe |
children | 451b7ccfe722 |
files | src/semicongine/core/matrix.nim src/semicongine/engine.nim |
diffstat | 2 files changed, 26 insertions(+), 2 deletions(-) [+] |
line wrap: on
line diff
--- a/src/semicongine/core/matrix.nim Sun Sep 24 23:15:08 2023 +0700 +++ b/src/semicongine/core/matrix.nim Fri Sep 29 19:30:07 2023 +0700 @@ -404,6 +404,27 @@ 0'f32, 0'f32, 0'f32, 1'f32, ]) +func asMat3(m: Mat4): auto = + Mat3(data: [ + m[0, 0], m[0, 1], m[0, 2], + m[1, 0], m[1, 1], m[1, 2], + m[2, 0], m[2, 1], m[2, 2], + ]) + + +func inversed*(m: Mat4): Mat4 = + var m3 = m.asMat3.transposed + m3[0, 0] = 1'f32 / m3[0, 0] + m3[1, 1] = 1'f32 / m3[1, 1] + m3[2, 2] = 1'f32 / m3[2, 2] + let col3 = -(m3 * m.col(3).xyz) + return Mat4(data: [ + m3[0, 0], m3[0, 1], m3[0, 2], col3.x, + m3[1, 0], m3[1, 1], m3[1, 2], col3.y, + m3[2, 0], m3[2, 1], m3[2, 2], col3.z, + 0, 0, 0, 1, + ]) + # call e.g. TMat32[int]().randomized() to get a random matrix template makeRandomInit(mattype: typedesc) = @@ -444,10 +465,10 @@ func orthoWindowAspect*(windowAspect: float32): Mat4 = if windowAspect > 1: let space = 2 * (windowAspect - 1) / 2 - ortho(-1, 1, -1 - space, 1 + space, -1, 1) + ortho(-1, 1, -1 - space, 1 + space, 0, 1) else: let space = 2 * (1 / windowAspect - 1) / 2 - ortho(-1 - space, 1 + space, -1, 1, -1, 1) + ortho(-1 - space, 1 + space, -1, 1, 0, 1) func position*(mat: Mat4): Vec3f = mat.col(3).toVec3
--- a/src/semicongine/engine.nim Sun Sep 24 23:15:08 2023 +0700 +++ b/src/semicongine/engine.nim Fri Sep 29 19:30:07 2023 +0700 @@ -194,6 +194,9 @@ func mouseWasPressed*(engine: Engine, button: MouseButton): auto = button in engine.input.mouseWasPressed func mouseWasReleased*(engine: Engine, button: MouseButton): auto = button in engine.input.mouseWasReleased func mousePosition*(engine: Engine): auto = engine.input.mousePosition +func mousePositionNormalized*(engine: Engine): Vec2f = + result.x = (engine.input.mousePosition.x / float32(engine.window.size[0])) * 2.0 - 1.0 + result.y = (engine.input.mousePosition.y / float32(engine.window.size[1])) * 2.0 - 1.0 func mouseMove*(engine: Engine): auto = engine.input.mouseMove func mouseWheel*(engine: Engine): auto = engine.input.mouseWheel func eventsProcessed*(engine: Engine): auto = engine.input.eventsProcessed