diff --git a/src/DotRecast.Core/RcMatrix4x4f.cs b/src/DotRecast.Core/RcMatrix4x4f.cs index 2cfd032..65f90f0 100644 --- a/src/DotRecast.Core/RcMatrix4x4f.cs +++ b/src/DotRecast.Core/RcMatrix4x4f.cs @@ -107,7 +107,7 @@ namespace DotRecast.Core M31 == 0f && M32 == 0f && M34 == 0f && M41 == 0f && M42 == 0f && M43 == 0f; - public static RcMatrix4x4f Mul(RcMatrix4x4f left, RcMatrix4x4f right) + public static RcMatrix4x4f Mul(ref RcMatrix4x4f left, ref RcMatrix4x4f right) { float m11 = left.M11 * right.M11 + left.M21 * right.M12 + left.M31 * right.M13 + left.M41 * right.M14; float m12 = left.M12 * right.M11 + left.M22 * right.M12 + left.M32 * right.M13 + left.M42 * right.M14; diff --git a/src/DotRecast.Recast.Demo/Draw/DebugDraw.cs b/src/DotRecast.Recast.Demo/Draw/DebugDraw.cs index 34f525c..443ca3d 100644 --- a/src/DotRecast.Recast.Demo/Draw/DebugDraw.cs +++ b/src/DotRecast.Recast.Demo/Draw/DebugDraw.cs @@ -303,8 +303,8 @@ public class DebugDraw private bool circleInit = false; private const int CIRCLE_NUM_SEG = 40; private readonly float[] circeDir = new float[CIRCLE_NUM_SEG * 2]; - private float[] _viewMatrix = new float[16]; - private readonly float[] _projectionMatrix = new float[16]; + private RcMatrix4x4f _viewMatrix = new(); + private RcMatrix4x4f _projectionMatrix = new(); public void AppendCircle(float x, float y, float z, float r, int col) { @@ -628,10 +628,10 @@ public class DebugDraw } - public float[] ProjectionMatrix(float fovy, float aspect, float near, float far) + public RcMatrix4x4f ProjectionMatrix(float fovy, float aspect, float near, float far) { - GLU.GlhPerspectivef2(_projectionMatrix, fovy, aspect, near, far); - GetOpenGlDraw().ProjectionMatrix(_projectionMatrix); + GLU.GlhPerspectivef2(ref _projectionMatrix, fovy, aspect, near, far); + GetOpenGlDraw().ProjectionMatrix(ref _projectionMatrix); UpdateFrustum(); return _projectionMatrix; } @@ -640,18 +640,17 @@ public class DebugDraw { var rx = RcMatrix4x4f.CreateFromRotate(cameraEulers[0], 1, 0, 0); var ry = RcMatrix4x4f.CreateFromRotate(cameraEulers[1], 0, 1, 0); - var r = RcMatrix4x4f.Mul(rx, ry); + var r = RcMatrix4x4f.Mul(ref rx, ref ry); var t = new RcMatrix4x4f(); t.M11 = t.M22 = t.M33 = t.M44 = 1; t.M41 = -cameraPos.x; t.M42 = -cameraPos.y; t.M43 = -cameraPos.z; - var mul = RcMatrix4x4f.Mul(r, t); - mul.CopyTo(_viewMatrix); - GetOpenGlDraw().ViewMatrix(ref mul); + _viewMatrix = RcMatrix4x4f.Mul(ref r, ref t); + GetOpenGlDraw().ViewMatrix(ref _viewMatrix); UpdateFrustum(); - return mul; + return _viewMatrix; } @@ -667,7 +666,7 @@ public class DebugDraw private void UpdateFrustum() { - var vpm = RcMatrix4x4f.Mul(_projectionMatrix, _viewMatrix); + var vpm = RcMatrix4x4f.Mul(ref _projectionMatrix, ref _viewMatrix); NormalizePlane(vpm.M14 + vpm.M11, vpm.M24 + vpm.M21, vpm.M34 + vpm.M31, vpm.M44 + vpm.M41, ref frustumPlanes[0]); // left NormalizePlane(vpm.M14 - vpm.M11, vpm.M24 - vpm.M21, vpm.M34 - vpm.M31, vpm.M44 - vpm.M41, ref frustumPlanes[1]); // right NormalizePlane(vpm.M14 - vpm.M12, vpm.M24 - vpm.M22, vpm.M34 - vpm.M32, vpm.M44 - vpm.M42, ref frustumPlanes[2]); // top diff --git a/src/DotRecast.Recast.Demo/Draw/GLU.cs b/src/DotRecast.Recast.Demo/Draw/GLU.cs index 2c228e8..efc7e37 100644 --- a/src/DotRecast.Recast.Demo/Draw/GLU.cs +++ b/src/DotRecast.Recast.Demo/Draw/GLU.cs @@ -23,45 +23,45 @@ namespace DotRecast.Recast.Demo.Draw; public static class GLU { - public static float[] GluPerspective(float fovy, float aspect, float near, float far) + public static RcMatrix4x4f GluPerspective(float fovy, float aspect, float near, float far) { - float[] projectionMatrix = new float[16]; - GlhPerspectivef2(projectionMatrix, fovy, aspect, near, far); + var projectionMatrix = new RcMatrix4x4f(); + GlhPerspectivef2(ref projectionMatrix, fovy, aspect, near, far); //GlLoadMatrixf(projectionMatrix); return projectionMatrix; } - public static void GlhPerspectivef2(float[] matrix, float fovyInDegrees, float aspectRatio, float znear, float zfar) + public static void GlhPerspectivef2(ref RcMatrix4x4f matrix, float fovyInDegrees, float aspectRatio, float znear, float zfar) { float ymax, xmax; ymax = (float)(znear * Math.Tan(fovyInDegrees * Math.PI / 360.0)); xmax = ymax * aspectRatio; - GlhFrustumf2(matrix, -xmax, xmax, -ymax, ymax, znear, zfar); + GlhFrustumf2(ref matrix, -xmax, xmax, -ymax, ymax, znear, zfar); } - private static void GlhFrustumf2(float[] matrix, float left, float right, float bottom, float top, float znear, float zfar) + private static void GlhFrustumf2(ref RcMatrix4x4f matrix, float left, float right, float bottom, float top, float znear, float zfar) { float temp, temp2, temp3, temp4; temp = 2.0f * znear; temp2 = right - left; temp3 = top - bottom; temp4 = zfar - znear; - matrix[0] = temp / temp2; - matrix[1] = 0.0f; - matrix[2] = 0.0f; - matrix[3] = 0.0f; - matrix[4] = 0.0f; - matrix[5] = temp / temp3; - matrix[6] = 0.0f; - matrix[7] = 0.0f; - matrix[8] = (right + left) / temp2; - matrix[9] = (top + bottom) / temp3; - matrix[10] = (-zfar - znear) / temp4; - matrix[11] = -1.0f; - matrix[12] = 0.0f; - matrix[13] = 0.0f; - matrix[14] = (-temp * zfar) / temp4; - matrix[15] = 0.0f; + matrix.M11 = temp / temp2; + matrix.M12 = 0.0f; + matrix.M13 = 0.0f; + matrix.M14 = 0.0f; + matrix.M21 = 0.0f; + matrix.M22 = temp / temp3; + matrix.M23 = 0.0f; + matrix.M24 = 0.0f; + matrix.M31 = (right + left) / temp2; + matrix.M32 = (top + bottom) / temp3; + matrix.M33 = (-zfar - znear) / temp4; + matrix.M34 = -1.0f; + matrix.M41 = 0.0f; + matrix.M42 = 0.0f; + matrix.M43 = (-temp * zfar) / temp4; + matrix.M44 = 0.0f; } public static int GlhUnProjectf(float winx, float winy, float winz, float[] modelview, float[] projection, int[] viewport, ref RcVec3f objectCoordinate) diff --git a/src/DotRecast.Recast.Demo/Draw/IOpenGLDraw.cs b/src/DotRecast.Recast.Demo/Draw/IOpenGLDraw.cs index 60836f3..4b6e098 100644 --- a/src/DotRecast.Recast.Demo/Draw/IOpenGLDraw.cs +++ b/src/DotRecast.Recast.Demo/Draw/IOpenGLDraw.cs @@ -27,7 +27,7 @@ public interface IOpenGLDraw void Texture(GLCheckerTexture g_tex, bool state); - void ProjectionMatrix(float[] projectionMatrix); + void ProjectionMatrix(ref RcMatrix4x4f projectionMatrix); void ViewMatrix(ref RcMatrix4x4f viewMatrix); diff --git a/src/DotRecast.Recast.Demo/Draw/ModernOpenGLDraw.cs b/src/DotRecast.Recast.Demo/Draw/ModernOpenGLDraw.cs index da7b1ff..f942240 100644 --- a/src/DotRecast.Recast.Demo/Draw/ModernOpenGLDraw.cs +++ b/src/DotRecast.Recast.Demo/Draw/ModernOpenGLDraw.cs @@ -22,8 +22,8 @@ public class ModernOpenGLDraw : IOpenGLDraw private readonly ArrayBuffer vertices = new(); private readonly ArrayBuffer elements = new(); private GLCheckerTexture _texture; - private float[] _viewMatrix = new float[16]; - private float[] _projectionMatrix; + private readonly float[] _viewMatrix = new float[16]; + private readonly float[] _projectionMatrix = new float[16]; private int uniformUseTexture; private int uniformFog; private int uniformFogStart; @@ -315,9 +315,9 @@ public class ModernOpenGLDraw : IOpenGLDraw } } - public void ProjectionMatrix(float[] projectionMatrix) + public void ProjectionMatrix(ref RcMatrix4x4f projectionMatrix) { - _projectionMatrix = projectionMatrix; + projectionMatrix.CopyTo(_projectionMatrix); } public void ViewMatrix(ref RcMatrix4x4f viewMatrix) diff --git a/src/DotRecast.Recast.Demo/RecastDemo.cs b/src/DotRecast.Recast.Demo/RecastDemo.cs index 8f359db..9565190 100644 --- a/src/DotRecast.Recast.Demo/RecastDemo.cs +++ b/src/DotRecast.Recast.Demo/RecastDemo.cs @@ -607,7 +607,7 @@ public class RecastDemo : IRecastDemoChannel { // Clear the screen dd.Clear(); - projectionMatrix = dd.ProjectionMatrix(50f, (float)width / (float)height, 1.0f, camr); + dd.ProjectionMatrix(50f, (float)width / (float)height, 1.0f, camr).CopyTo(projectionMatrix); dd.ViewMatrix(cameraPos, cameraEulers).CopyTo(modelviewMatrix); dd.Fog(camr * 0.1f, camr * 1.25f); diff --git a/src/DotRecast.Recast.Toolset/Tools/RcDynamicUpdateTool.cs b/src/DotRecast.Recast.Toolset/Tools/RcDynamicUpdateTool.cs index ff4efd5..d62b7a5 100644 --- a/src/DotRecast.Recast.Toolset/Tools/RcDynamicUpdateTool.cs +++ b/src/DotRecast.Recast.Toolset/Tools/RcDynamicUpdateTool.cs @@ -299,7 +299,7 @@ namespace DotRecast.Recast.Toolset.Tools { var rx = RcMatrix4x4f.CreateFromRotate((float)random.NextDouble() * ax, 1, 0, 0); var ry = RcMatrix4x4f.CreateFromRotate((float)random.NextDouble() * 360, 0, 1, 0); - var m = RcMatrix4x4f.Mul(rx, ry); + var m = RcMatrix4x4f.Mul(ref rx, ref ry); float[] verts = new float[geom.vertices.Length]; RcVec3f v = new RcVec3f(); RcVec3f vr = new RcVec3f();