2

I'm writing a solar system simulation and some objects are either too far or too small to be rendered even to a single pixel. This behavior is expected but it's creating some artifacts. For small camera movements, the object alternates between being rendered to a single pixel and not being rendered at all, creating a "flickering" artifact.

Is it possible to enforce that an object is always rendered to at least 1 pixel? Or maybe any other technique to avoid this problem.

The issue persists when calling

glDepthFunc(GL_ALWAYS)

and the fartherst object is 4412 units away from the camera while the far plane is 10000 units away.

Objects rendered are spheres (planets) using the code from this answer with 36 sectors and 36 rings, the draw call for the spheres is

glDrawElements(GL_QUADS, m_Indices.size(), GL_UNSIGNED_SHORT, (void *)0)

and the shaders used are listed below:

Vertex Shader

#version 330 core

layout(location = 0) in vec3 in_Position;
layout(location = 1) in vec3 in_Normal;
layout(location = 2) in vec2 in_TexCoord;

uniform mat4 un_mvpMatrix;

out vec2 TexCoord;

void main() {
    gl_Position = un_mvpMatrix * vec4(in_Position, 1.0);

    TexCoord = in_TexCoord;
}

Fragment Shader

#version 330 core

in vec2 TexCoord;

uniform sampler2D in_Texture;

out vec4 FragColor;

void main() {
    FragColor = texture(in_Texture, TexCoord);
}
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
Jean Catanho
  • 326
  • 7
  • 18
  • @Rabbid76 I've edited the question with the relevant information for your comment. – Jean Catanho Aug 14 '17 at 06:48
  • @Rabbid76 Since I'm dealing with a solar system simulation, the objects are spheres. The question once again is updated with relevant information. I'm just doing positioning and texture mapping in the shaders. – Jean Catanho Aug 14 '17 at 08:34
  • 1
    This is a consequence of OpenGL's rasterization rules. You could look at fading the object, which makes sense for a diffuse reflector, or you *may* have something like the `GL_NV_conservative_raster` extension. – Brett Hale Aug 14 '17 at 08:41
  • Another solution would be to use multisampling/supersampling. – derhass Aug 14 '17 at 13:42

1 Answers1

7

Draw GL_POINTS for each object at their center in one pass and then the 3D geometry on top in another. That way you'll always have the point even if the 3D geometry goes away.

genpfault
  • 51,148
  • 11
  • 85
  • 139