Add rlCullDistance variables/getters and rlSetClipPlanes function (#3912)

The `RL_CULL_DISTANCE_` definition remains as the initial value
of the variables.

Basic usage can be:
```c
#include <raylib.h>
#include <rlgl.h>

rlSetClipPlanes(RL_CULL_DISTANCE_NEAR, MY_CULL_DISTANCE_FAR);

if (must_reset_clip_planes)
    rlSetClipPlanes(RL_CULL_DISTANCE_NEAR, RL_CULL_DISTANCE_FAR);
```
This commit is contained in:
KotzaBoss 2024-04-23 14:24:37 +02:00 committed by GitHub
parent d80febde7d
commit 4b0e25d3af
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 32 additions and 8 deletions

View File

@ -208,7 +208,7 @@ static TextureCubemap GenTextureCubemap(Shader shader, Texture2D panorama, int s
rlEnableShader(shader.id); rlEnableShader(shader.id);
// Define projection matrix and send it to shader // Define projection matrix and send it to shader
Matrix matFboProjection = MatrixPerspective(90.0*DEG2RAD, 1.0, RL_CULL_DISTANCE_NEAR, RL_CULL_DISTANCE_FAR); Matrix matFboProjection = MatrixPerspective(90.0*DEG2RAD, 1.0, rlGetCullDistanceNear(), rlGetCullDistanceFar());
rlSetUniformMatrix(shader.locs[SHADER_LOC_MATRIX_PROJECTION], matFboProjection); rlSetUniformMatrix(shader.locs[SHADER_LOC_MATRIX_PROJECTION], matFboProjection);
// Define view matrix for every side of the cubemap // Define view matrix for every side of the cubemap

View File

@ -992,10 +992,10 @@ void BeginMode3D(Camera camera)
if (camera.projection == CAMERA_PERSPECTIVE) if (camera.projection == CAMERA_PERSPECTIVE)
{ {
// Setup perspective projection // Setup perspective projection
double top = RL_CULL_DISTANCE_NEAR*tan(camera.fovy*0.5*DEG2RAD); double top = rlGetCullDistanceNear()*tan(camera.fovy*0.5*DEG2RAD);
double right = top*aspect; double right = top*aspect;
rlFrustum(-right, right, -top, top, RL_CULL_DISTANCE_NEAR, RL_CULL_DISTANCE_FAR); rlFrustum(-right, right, -top, top, rlGetCullDistanceNear(), rlGetCullDistanceFar());
} }
else if (camera.projection == CAMERA_ORTHOGRAPHIC) else if (camera.projection == CAMERA_ORTHOGRAPHIC)
{ {
@ -1003,7 +1003,7 @@ void BeginMode3D(Camera camera)
double top = camera.fovy/2.0; double top = camera.fovy/2.0;
double right = top*aspect; double right = top*aspect;
rlOrtho(-right, right, -top,top, RL_CULL_DISTANCE_NEAR, RL_CULL_DISTANCE_FAR); rlOrtho(-right, right, -top,top, rlGetCullDistanceNear(), rlGetCullDistanceFar());
} }
rlMatrixMode(RL_MODELVIEW); // Switch back to modelview matrix rlMatrixMode(RL_MODELVIEW); // Switch back to modelview matrix
@ -1207,7 +1207,7 @@ VrStereoConfig LoadVrStereoConfig(VrDeviceInfo device)
// Compute camera projection matrices // Compute camera projection matrices
float projOffset = 4.0f*lensShift; // Scaled to projection space coordinates [-1..1] float projOffset = 4.0f*lensShift; // Scaled to projection space coordinates [-1..1]
Matrix proj = MatrixPerspective(fovy, aspect, RL_CULL_DISTANCE_NEAR, RL_CULL_DISTANCE_FAR); Matrix proj = MatrixPerspective(fovy, aspect, rlGetCullDistanceNear(), rlGetCullDistanceFar());
config.projection[0] = MatrixMultiply(proj, MatrixTranslate(projOffset, 0.0f, 0.0f)); config.projection[0] = MatrixMultiply(proj, MatrixTranslate(projOffset, 0.0f, 0.0f));
config.projection[1] = MatrixMultiply(proj, MatrixTranslate(-projOffset, 0.0f, 0.0f)); config.projection[1] = MatrixMultiply(proj, MatrixTranslate(-projOffset, 0.0f, 0.0f));
@ -1446,7 +1446,7 @@ Ray GetScreenToWorldRayEx(Vector2 position, Camera camera, int width, int height
if (camera.projection == CAMERA_PERSPECTIVE) if (camera.projection == CAMERA_PERSPECTIVE)
{ {
// Calculate projection matrix from perspective // Calculate projection matrix from perspective
matProj = MatrixPerspective(camera.fovy*DEG2RAD, ((double)width/(double)height), RL_CULL_DISTANCE_NEAR, RL_CULL_DISTANCE_FAR); matProj = MatrixPerspective(camera.fovy*DEG2RAD, ((double)width/(double)height), rlGetCullDistanceNear(), rlGetCullDistanceFar());
} }
else if (camera.projection == CAMERA_ORTHOGRAPHIC) else if (camera.projection == CAMERA_ORTHOGRAPHIC)
{ {
@ -1533,7 +1533,7 @@ Vector2 GetWorldToScreenEx(Vector3 position, Camera camera, int width, int heigh
if (camera.projection == CAMERA_PERSPECTIVE) if (camera.projection == CAMERA_PERSPECTIVE)
{ {
// Calculate projection matrix from perspective // Calculate projection matrix from perspective
matProj = MatrixPerspective(camera.fovy*DEG2RAD, ((double)width/(double)height), RL_CULL_DISTANCE_NEAR, RL_CULL_DISTANCE_FAR); matProj = MatrixPerspective(camera.fovy*DEG2RAD, ((double)width/(double)height), rlGetCullDistanceNear(), rlGetCullDistanceFar());
} }
else if (camera.projection == CAMERA_ORTHOGRAPHIC) else if (camera.projection == CAMERA_ORTHOGRAPHIC)
{ {
@ -1542,7 +1542,7 @@ Vector2 GetWorldToScreenEx(Vector3 position, Camera camera, int width, int heigh
double right = top*aspect; double right = top*aspect;
// Calculate projection matrix from orthographic // Calculate projection matrix from orthographic
matProj = MatrixOrtho(-right, right, -top, top, RL_CULL_DISTANCE_NEAR, RL_CULL_DISTANCE_FAR); matProj = MatrixOrtho(-right, right, -top, top, rlGetCullDistanceNear(), rlGetCullDistanceFar());
} }
// Calculate view matrix from camera look at (and transpose it) // Calculate view matrix from camera look at (and transpose it)

View File

@ -559,6 +559,10 @@ typedef enum {
extern "C" { // Prevents name mangling of functions extern "C" { // Prevents name mangling of functions
#endif #endif
RLAPI void rlSetClipPlanes(double near, double far);
RLAPI double rlGetCullDistanceNear();
RLAPI double rlGetCullDistanceFar();
RLAPI void rlMatrixMode(int mode); // Choose the current matrix to be transformed RLAPI void rlMatrixMode(int mode); // Choose the current matrix to be transformed
RLAPI void rlPushMatrix(void); // Push the current matrix to stack RLAPI void rlPushMatrix(void); // Push the current matrix to stack
RLAPI void rlPopMatrix(void); // Pop latest inserted matrix from stack RLAPI void rlPopMatrix(void); // Pop latest inserted matrix from stack
@ -1083,6 +1087,10 @@ typedef void *(*rlglLoadProc)(const char *name); // OpenGL extension functions
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
// Global Variables Definition // Global Variables Definition
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
static double rlCullDistanceNear = RL_CULL_DISTANCE_NEAR;
static double rlCullDistanceFar = RL_CULL_DISTANCE_FAR;
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
static rlglData RLGL = { 0 }; static rlglData RLGL = { 0 };
#endif // GRAPHICS_API_OPENGL_33 || GRAPHICS_API_OPENGL_ES2 #endif // GRAPHICS_API_OPENGL_33 || GRAPHICS_API_OPENGL_ES2
@ -1127,6 +1135,22 @@ static Matrix rlMatrixInvert(Matrix mat); // Invert provided m
// Module Functions Definition - Matrix operations // Module Functions Definition - Matrix operations
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
void rlSetClipPlanes(double near, double far)
{
rlCullDistanceNear = near;
rlCullDistanceFar = far;
}
double rlGetCullDistanceFar()
{
return rlCullDistanceFar;
}
double rlGetCullDistanceNear()
{
return rlCullDistanceNear;
}
#if defined(GRAPHICS_API_OPENGL_11) #if defined(GRAPHICS_API_OPENGL_11)
// Fallback to OpenGL 1.1 function calls // Fallback to OpenGL 1.1 function calls
//--------------------------------------- //---------------------------------------