This commit is contained in:
Abdi Ibrahim 2025-01-15 20:57:29 -03:00 committed by GitHub
commit f2ac36c430
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -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);
@ -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++)
{
@ -230,4 +236,4 @@ void Clay_Raylib_Render(Clay_RenderCommandArray renderCommands)
}
}
}
}
}