# HG changeset patch # User Sam # Date 1695990607 -25200 # Node ID ad009d1c747f78678f0ff484d847af42e98e4236 # Parent 84c56d1aef392091c95f7c538189e7c81e543f39 add: support inverse matrix and normalized mouse position diff -r 84c56d1aef39 -r ad009d1c747f src/semicongine/core/matrix.nim --- 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 diff -r 84c56d1aef39 -r ad009d1c747f src/semicongine/engine.nim --- 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