From 275c9d98cd4c6260bd65074330802b82a2d38221 Mon Sep 17 00:00:00 2001 From: Abdi Ibrahim Date: Sun, 29 Dec 2024 09:21:40 -0500 Subject: [PATCH 1/2] precompute once per frame --- renderers/raylib/clay_renderer_raylib.c | 44 ++++++++++++------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/renderers/raylib/clay_renderer_raylib.c b/renderers/raylib/clay_renderer_raylib.c index 0fe545a6..e66c71af 100644 --- a/renderers/raylib/clay_renderer_raylib.c +++ b/renderers/raylib/clay_renderer_raylib.c @@ -38,8 +38,28 @@ typedef struct }; } CustomLayoutElement; +// Precompute the matrices at start of the frame +Matrix PrecomputeViewMatrix(const Camera camera){ + return MatrixLookAt(camera.position, camera.target, camera.up); +} + +Matrix PrecomputeProjectionMatrix(const Camera camera, int screenWidth, int screenHeight, float zDistance) { + Matrix matProj = MatrixIdentity(); + if (camera.projection == CAMERA_PERSPECTIVE) { + // Calculate projection matrix from perspective + matProj = MatrixPerspective(camera.fovy * DEG2RAD, ((double)screenWidth / (double)screenHeight), 0.01f, zDistance); + } else if (camera.projection == CAMERA_ORTHOGRAPHIC) { + double aspect = (double)screenWidth / (double)screenHeight; + double top = camera.fovy / 2.0; + double right = top * aspect; + // Calculate projection matrix from orthographic + matProj = MatrixOrtho(-right, right, -top, top, 0.01, 1000.0); + } + return matProj; +} + // Get a ray trace from the screen position (i.e mouse) within a specific section of the screen -Ray GetScreenToWorldPointWithZDistance(Vector2 position, Camera camera, int screenWidth, int screenHeight, float zDistance) +Ray GetScreenToWorldPointWithZDistance(Vector2 position, const Matrix matView, const Matrix matProj, int screenWidth, int screenHeight) { { Ray ray = { 0 }; @@ -52,26 +72,6 @@ Ray GetScreenToWorldPointWithZDistance(Vector2 position, Camera camera, int scre // Store values in a vector Vector3 deviceCoords = { x, y, z }; - // Calculate view matrix from camera look at - Matrix matView = MatrixLookAt(camera.position, camera.target, camera.up); - - Matrix matProj = MatrixIdentity(); - - if (camera.projection == CAMERA_PERSPECTIVE) - { - // Calculate projection matrix from perspective - matProj = MatrixPerspective(camera.fovy*DEG2RAD, ((double)screenWidth/(double)screenHeight), 0.01f, zDistance); - } - else if (camera.projection == CAMERA_ORTHOGRAPHIC) - { - double aspect = (double)screenWidth/(double)screenHeight; - double top = camera.fovy/2.0; - double right = top*aspect; - - // Calculate projection matrix from orthographic - matProj = MatrixOrtho(-right, right, -top, top, 0.01, 1000.0); - } - // Unproject far/near points Vector3 nearPoint = Vector3Unproject((Vector3){ deviceCoords.x, deviceCoords.y, 0.0f }, matProj, matView); Vector3 farPoint = Vector3Unproject((Vector3){ deviceCoords.x, deviceCoords.y, 1.0f }, matProj, matView); @@ -230,4 +230,4 @@ void Clay_Raylib_Render(Clay_RenderCommandArray renderCommands) } } } -} +} \ No newline at end of file From 4f50603b8f77b950beb29f16b15a95a057cd96a8 Mon Sep 17 00:00:00 2001 From: Abdi Ibrahim Date: Sun, 29 Dec 2024 09:39:59 -0500 Subject: [PATCH 2/2] updated render --- renderers/raylib/clay_renderer_raylib.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/renderers/raylib/clay_renderer_raylib.c b/renderers/raylib/clay_renderer_raylib.c index e66c71af..bad41374 100644 --- a/renderers/raylib/clay_renderer_raylib.c +++ b/renderers/raylib/clay_renderer_raylib.c @@ -129,6 +129,12 @@ void Clay_Raylib_Initialize(int width, int height, const char *title, unsigned i void Clay_Raylib_Render(Clay_RenderCommandArray renderCommands) { + + int screenWidth = GetScreenWidth(); + int screenHeight = GetScreenHeight(); + Matrix matView = PrecomputeViewMatrix(Raylib_camera); + Matrix matProj = PrecomputeProjectionMatrix(Raylib_camera, screenWidth, screenHeight, 140.0f); + measureCalls = 0; for (int j = 0; j < renderCommands.length; j++) {