Skip to content

Commit

Permalink
#219: Port the vertex clipping code to DR.
Browse files Browse the repository at this point in the history
  • Loading branch information
codereader committed Mar 25, 2022
1 parent c00fd79 commit 4dcebf6
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 11 deletions.
22 changes: 22 additions & 0 deletions install/gl/shadowmap_vp.glsl
Expand Up @@ -12,6 +12,7 @@ uniform vec4 u_DiffuseTextureMatrix[2];
// The final diffuse texture coordinate at this vertex
varying vec2 var_TexDiffuse;

// The modelview matrices for each of the 6 cube map faces
const mat3 cubicTransformations[6] = mat3[6]
(
mat3(0, 0, -1,
Expand Down Expand Up @@ -39,17 +40,38 @@ const mat3 cubicTransformations[6] = mat3[6]
0, 0, 1)
);

// These 4 clip planes are 45 degrees planes passing the origin (opening towards negative z)
// bounding the view frustum of a point light at 0,0,0
const float clipEps = 0e-2;
const vec4 ClipPlanes[4] = vec4[4]
(
vec4(1, 0, -1, clipEps),
vec4(-1, 0, -1, clipEps),
vec4(0, 1, -1, clipEps),
vec4(0, -1, -1, clipEps)
);

void main()
{
// Transform the model vertex to world space, then subtract the light origin
// to move the vertex into light space (with the light residing at 0,0,0)
vec4 lightSpacePos = u_ObjectTransform * attr_Position;
lightSpacePos.xyz -= u_LightOrigin;

// Render the vertex 6 times, once for each cubemap face (gl_InstanceID = [0..5])
// This is just a rotation, no scaling or projection involved
vec4 fragPos = vec4(cubicTransformations[gl_InstanceID] * lightSpacePos.xyz, 1);

gl_Position.x = fragPos.x / 6 + fragPos.z * 5/6 - fragPos.z / 3 * gl_InstanceID;
gl_Position.y = fragPos.y;
gl_Position.z = -fragPos.z - 2;
gl_Position.w = -fragPos.z;

// To clip the vertices outside the view frustum, calculate its clip distance
// every fragment with a distance < 0 will be discarded
// This relies on the GL_CLIP_DISTANCE0-3 flags activated in the renderer code
gl_ClipDistance[0] = dot(fragPos, ClipPlanes[0]);
gl_ClipDistance[1] = dot(fragPos, ClipPlanes[1]);
gl_ClipDistance[2] = dot(fragPos, ClipPlanes[2]);
gl_ClipDistance[3] = dot(fragPos, ClipPlanes[3]);
}
25 changes: 14 additions & 11 deletions radiantcore/rendersystem/backend/LightingModeRenderer.cpp
Expand Up @@ -99,16 +99,18 @@ IRenderResult::Ptr LightingModeRenderer::render(RenderStateFlags globalFlagsMask
glDepthFunc(GL_LEQUAL);
current.setDepthFunc(GL_LEQUAL);

#if 1
glEnable(GL_DEPTH_TEST);
current.setRenderFlag(RENDER_DEPTHTEST);

glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
current.setRenderFlag(RENDER_FILL);
#endif
#if 0
glPolygonOffset(0, 0);
glEnable(GL_POLYGON_OFFSET_FILL);
#endif

// Enable the 4 clip planes, they are used in the vertex shader
glEnable(GL_CLIP_DISTANCE0);
glEnable(GL_CLIP_DISTANCE1);
glEnable(GL_CLIP_DISTANCE2);
glEnable(GL_CLIP_DISTANCE3);

// Render a single light to the shadow map buffer
for (auto& interactionList : interactionLists)
{
Expand All @@ -122,16 +124,17 @@ IRenderResult::Ptr LightingModeRenderer::render(RenderStateFlags globalFlagsMask
_shadowMapFbo->unbind();
_shadowMapProgram->disable();

glDisable(GL_CLIP_DISTANCE3);
glDisable(GL_CLIP_DISTANCE2);
glDisable(GL_CLIP_DISTANCE1);
glDisable(GL_CLIP_DISTANCE0);

// Restore view port
glViewport(previousViewport[0], previousViewport[1], previousViewport[2], previousViewport[3]);

#if 0
glDisable(GL_POLYGON_OFFSET_FILL);
#endif
#if 1
glDisable(GL_DEPTH_TEST);
current.clearRenderFlag(RENDER_DEPTHTEST);
#endif

// Load the model view & projection matrix for the main scene
setupViewMatrices(view);

Expand Down

0 comments on commit 4dcebf6

Please sign in to comment.