Skip to content

Commit 4b0e25d

Browse files
authored
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); ```
1 parent d80febd commit 4b0e25d

File tree

3 files changed

+32
-8
lines changed

3 files changed

+32
-8
lines changed

examples/models/models_skybox.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ static TextureCubemap GenTextureCubemap(Shader shader, Texture2D panorama, int s
208208
rlEnableShader(shader.id);
209209

210210
// Define projection matrix and send it to shader
211-
Matrix matFboProjection = MatrixPerspective(90.0*DEG2RAD, 1.0, RL_CULL_DISTANCE_NEAR, RL_CULL_DISTANCE_FAR);
211+
Matrix matFboProjection = MatrixPerspective(90.0*DEG2RAD, 1.0, rlGetCullDistanceNear(), rlGetCullDistanceFar());
212212
rlSetUniformMatrix(shader.locs[SHADER_LOC_MATRIX_PROJECTION], matFboProjection);
213213

214214
// Define view matrix for every side of the cubemap

src/rcore.c

+7-7
Original file line numberDiff line numberDiff line change
@@ -992,18 +992,18 @@ void BeginMode3D(Camera camera)
992992
if (camera.projection == CAMERA_PERSPECTIVE)
993993
{
994994
// Setup perspective projection
995-
double top = RL_CULL_DISTANCE_NEAR*tan(camera.fovy*0.5*DEG2RAD);
995+
double top = rlGetCullDistanceNear()*tan(camera.fovy*0.5*DEG2RAD);
996996
double right = top*aspect;
997997

998-
rlFrustum(-right, right, -top, top, RL_CULL_DISTANCE_NEAR, RL_CULL_DISTANCE_FAR);
998+
rlFrustum(-right, right, -top, top, rlGetCullDistanceNear(), rlGetCullDistanceFar());
999999
}
10001000
else if (camera.projection == CAMERA_ORTHOGRAPHIC)
10011001
{
10021002
// Setup orthographic projection
10031003
double top = camera.fovy/2.0;
10041004
double right = top*aspect;
10051005

1006-
rlOrtho(-right, right, -top,top, RL_CULL_DISTANCE_NEAR, RL_CULL_DISTANCE_FAR);
1006+
rlOrtho(-right, right, -top,top, rlGetCullDistanceNear(), rlGetCullDistanceFar());
10071007
}
10081008

10091009
rlMatrixMode(RL_MODELVIEW); // Switch back to modelview matrix
@@ -1207,7 +1207,7 @@ VrStereoConfig LoadVrStereoConfig(VrDeviceInfo device)
12071207

12081208
// Compute camera projection matrices
12091209
float projOffset = 4.0f*lensShift; // Scaled to projection space coordinates [-1..1]
1210-
Matrix proj = MatrixPerspective(fovy, aspect, RL_CULL_DISTANCE_NEAR, RL_CULL_DISTANCE_FAR);
1210+
Matrix proj = MatrixPerspective(fovy, aspect, rlGetCullDistanceNear(), rlGetCullDistanceFar());
12111211

12121212
config.projection[0] = MatrixMultiply(proj, MatrixTranslate(projOffset, 0.0f, 0.0f));
12131213
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
14461446
if (camera.projection == CAMERA_PERSPECTIVE)
14471447
{
14481448
// Calculate projection matrix from perspective
1449-
matProj = MatrixPerspective(camera.fovy*DEG2RAD, ((double)width/(double)height), RL_CULL_DISTANCE_NEAR, RL_CULL_DISTANCE_FAR);
1449+
matProj = MatrixPerspective(camera.fovy*DEG2RAD, ((double)width/(double)height), rlGetCullDistanceNear(), rlGetCullDistanceFar());
14501450
}
14511451
else if (camera.projection == CAMERA_ORTHOGRAPHIC)
14521452
{
@@ -1533,7 +1533,7 @@ Vector2 GetWorldToScreenEx(Vector3 position, Camera camera, int width, int heigh
15331533
if (camera.projection == CAMERA_PERSPECTIVE)
15341534
{
15351535
// Calculate projection matrix from perspective
1536-
matProj = MatrixPerspective(camera.fovy*DEG2RAD, ((double)width/(double)height), RL_CULL_DISTANCE_NEAR, RL_CULL_DISTANCE_FAR);
1536+
matProj = MatrixPerspective(camera.fovy*DEG2RAD, ((double)width/(double)height), rlGetCullDistanceNear(), rlGetCullDistanceFar());
15371537
}
15381538
else if (camera.projection == CAMERA_ORTHOGRAPHIC)
15391539
{
@@ -1542,7 +1542,7 @@ Vector2 GetWorldToScreenEx(Vector3 position, Camera camera, int width, int heigh
15421542
double right = top*aspect;
15431543

15441544
// Calculate projection matrix from orthographic
1545-
matProj = MatrixOrtho(-right, right, -top, top, RL_CULL_DISTANCE_NEAR, RL_CULL_DISTANCE_FAR);
1545+
matProj = MatrixOrtho(-right, right, -top, top, rlGetCullDistanceNear(), rlGetCullDistanceFar());
15461546
}
15471547

15481548
// Calculate view matrix from camera look at (and transpose it)

src/rlgl.h

+24
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,10 @@ typedef enum {
559559
extern "C" { // Prevents name mangling of functions
560560
#endif
561561

562+
RLAPI void rlSetClipPlanes(double near, double far);
563+
RLAPI double rlGetCullDistanceNear();
564+
RLAPI double rlGetCullDistanceFar();
565+
562566
RLAPI void rlMatrixMode(int mode); // Choose the current matrix to be transformed
563567
RLAPI void rlPushMatrix(void); // Push the current matrix to stack
564568
RLAPI void rlPopMatrix(void); // Pop latest inserted matrix from stack
@@ -1083,6 +1087,10 @@ typedef void *(*rlglLoadProc)(const char *name); // OpenGL extension functions
10831087
//----------------------------------------------------------------------------------
10841088
// Global Variables Definition
10851089
//----------------------------------------------------------------------------------
1090+
1091+
static double rlCullDistanceNear = RL_CULL_DISTANCE_NEAR;
1092+
static double rlCullDistanceFar = RL_CULL_DISTANCE_FAR;
1093+
10861094
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
10871095
static rlglData RLGL = { 0 };
10881096
#endif // GRAPHICS_API_OPENGL_33 || GRAPHICS_API_OPENGL_ES2
@@ -1127,6 +1135,22 @@ static Matrix rlMatrixInvert(Matrix mat); // Invert provided m
11271135
// Module Functions Definition - Matrix operations
11281136
//----------------------------------------------------------------------------------
11291137

1138+
void rlSetClipPlanes(double near, double far)
1139+
{
1140+
rlCullDistanceNear = near;
1141+
rlCullDistanceFar = far;
1142+
}
1143+
1144+
double rlGetCullDistanceFar()
1145+
{
1146+
return rlCullDistanceFar;
1147+
}
1148+
1149+
double rlGetCullDistanceNear()
1150+
{
1151+
return rlCullDistanceNear;
1152+
}
1153+
11301154
#if defined(GRAPHICS_API_OPENGL_11)
11311155
// Fallback to OpenGL 1.1 function calls
11321156
//---------------------------------------

0 commit comments

Comments
 (0)