0

I draw vertex normals using geometry shader. Everything shows up as expected except that when I move the camera some lines partially disappear. First, I thought this was due to the frustrum size but I have other objects in the scene bigger than this one drawn just fine.

Before movement

After movement

If anyone could give me any pointer how to get rid of this effect of line disappearing, I would really appreciate it.

Below is the code of my geometry shader

#version 330 core


layout (triangles) in;
layout (line_strip, max_vertices = 6) out;

in Data{
    vec4 position;
    vec4 t_position;
    vec4 normal;
    vec2 texCoord;
    vec4 color;
    mat4 mvp;
    mat4 view;
    mat4 mv;
} received[];


out Data{
   vec4 color;
   vec2 uv;
   vec4 normal;
   vec2 texCoord;
   mat4 view;
} gdata;


const float MAGNITUDE = 1.5f;

void GenerateLine(int index) {
    const vec4 green = vec4(0.0f, 1.0f, 0.0f, 1.0f);
    const vec4 blue = vec4(0.0f, 0.0f, 1.0f, 1.0f);

    gl_Position = received[index].t_position;
    //gdata.color = received[index].color;
    gdata.color = green;
    EmitVertex();

    gl_Position = received[index].t_position + received[index].normal * MAGNITUDE;
    //gdata.color = received[index].color;
    gdata.color = blue;
    EmitVertex();

    EndPrimitive();
}

void main() {
    GenerateLine(0); // First vertex normal
    GenerateLine(1); // Second vertex normal
    GenerateLine(2); // Third vertex normal
}

Vertex Shader

#version 330


layout(location = 0) in vec3 Position;
layout(location = 1) in vec2 TexCoord;
layout(location = 2) in vec3 Normal;

out Data{
    vec4 position;
    vec4 t_position;
    vec4 normal;
    vec2 texCoord;
    vec4 color;
    mat4 mvp;
    mat4 view;
    mat4 mv;
} vdata;

//MVP
uniform mat4 model;
uniform mat4 projection;
uniform mat4 view;

void main() {
    vdata.position = vec4(Position, 1.0f);
    vdata.normal = view * model * vec4(Normal, 0.0);
    vdata.texCoord = TexCoord;

    vdata.view = view;

    vec4 modelColor = vec4(0.8f, 0.8f, 0.8f, 1.0f);
    vdata.color = modelColor;

    vdata.mvp = projection * view * model;
    vdata.mv = view * model;
    vdata.t_position = vdata.mvp * vdata.position;

    gl_Position = vdata.t_position;
};
Illia
  • 301
  • 1
  • 4
  • 16
  • Definitely looks like depth buffer clipping. – Quentin May 18 '16 at 21:26
  • I am new to the OpenGL. How would you suggest to fix it? – Illia May 18 '16 at 21:28
  • @Illia You probably need to add a Projection Matrix in the Vertex Shader, as it looks like you're lacking one right now. – Xirema May 18 '16 at 21:34
  • @Xirema Thank you for you comment but I do have proj matrix in my Vertex shader. I just added the code my vertex shader. Or do you mean something different? – Illia May 18 '16 at 21:40
  • @Illia: Why are you outputting *matrices* from your vertex shader? Why not just make them *uniforms* that the GS can access? – Nicol Bolas May 18 '16 at 22:09

3 Answers3

1

If anyone else runs into this issue the solution is the following:

glm::perspective(45.0f, m_aspectRatio, m_zNear, m_zFar);

I originally had my m_zNear equal to 0.1 but when switched it to 1.0, the lines stopped disappearing. I am not completely sure why is that. If any one knows please share it.

Illia
  • 301
  • 1
  • 4
  • 16
1

Have you try to google zFar zNear? Here you go.

Or at least trying to google that miraculous helping glm::perspective(...) function?

Nox
  • 932
  • 1
  • 9
  • 27
1

Referring to the answer by Illia May 18 '16 at 22:53

I originally had my m_zNear equal to 0.1 but when switched it to 1.0, the lines stopped disappearing. I am not completely sure why is that. If any one knows please share it

The disappearing line is about depth buffer clipping. The vertex is projected (multiplied with MVP-matrix) and then the vertex position is changed AFTER the projection in geometry shader (GS). These changes in GS causes the z-value to fall outside normalized device coordinates (NDC) in perspective division. With a larger zNear value the projected z-value is smaller so it does not fall outside NDC. Though if the value of MAGNITUDE in GS was large enough the lines would be clipped anyway even with a larger zNear. One option to fix this is to do projection in GS.

Community
  • 1
  • 1
jparimaa
  • 1,964
  • 15
  • 19