Skip to content

Commit fbe830f

Browse files
committed
Avoiding division by zero in vertex shader
1 parent 427788a commit fbe830f

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

rviz_rendering/ogre_media/materials/glsl120/depth_fade_point.vert

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,23 @@ float hash21(vec3 p)
3030

3131
void main()
3232
{
33+
gl_Position = worldviewproj_matrix * gl_Vertex;
34+
3335
vec4 pos_rel_view = worldview_matrix * gl_Vertex;
3436
float depth = -pos_rel_view.z;
3537

36-
gl_Position = worldviewproj_matrix * gl_Vertex;
38+
if (depth < 1e-6)
39+
{
40+
// point is behind the camera, remove it
41+
gl_Position.w = -1.0; // outside frustum
42+
return;
43+
}
3744

3845
float focal = projection_matrix[1][1];
3946
// Compute pixel size from world-size (size.x), focal, depth and viewport height
4047
// pixel_size = world_size * focal / depth * (viewport_height / 2)
4148
float pixelSize = size.x * focal / depth * (viewport_height * 0.5);
42-
gl_PointSize = min(max(pixelSize, 1.0), MAX_PIXEL_SIZE);
49+
gl_PointSize = clamp(pixelSize, 1.0, MAX_PIXEL_SIZE);
4350

4451
// For very small points, reduce alpha to create a fading effect
4552
// alpha scales with pixel size squared (area)
@@ -53,6 +60,7 @@ void main()
5360
{
5461
// remove point
5562
gl_Position.w = -1.0; // outside frustum
63+
return;
5664
}
5765
finalAlpha = MIN_ALPHA_THRESHOLD;
5866
}

0 commit comments

Comments
 (0)