0
votes

I'm trying to calculate per-face normals in geometry shader, using the following pipeline

//VERTEX_SHADER
#version 330
layout(location = 0) in vec4 vertex;
out vec3 vert;
uniform mat4 projMatrix;
uniform mat4 mvMatrix;

void main()
{
  vert = vertex.xyz;
  gl_Position = projMatrix * mvMatrix * vertex;
}

//GEOMETRY_SHADER
#version 330

layout ( triangles ) in;
layout ( triangle_strip, max_vertices = 3 ) out;

out vec3 normal_out;
uniform mat3 normalMatrix;

void main()
{
    vec3 A = gl_in[2].gl_Position.xyz - gl_in[0].gl_Position.xyz;
    vec3 B = gl_in[1].gl_Position.xyz - gl_in[0].gl_Position.xyz;
    normal_out = normalMatrix * normalize(cross(A,B));
    gl_Position = gl_in[0].gl_Position;
    EmitVertex();
    gl_Position = gl_in[1].gl_Position;
    EmitVertex();
    gl_Position = gl_in[2].gl_Position;
    EmitVertex();
    EndPrimitive();
}

//FRAG_SHADER
#version 330
in vec3 normal_out;
in vec3 vert;
out vec4 fColor;
uniform vec3 lightPos;

void main()
{
    highp vec3 L = normalize(lightPos - vert);
    highp float NL = max(dot(normal_out, L), 0.0);
    highp vec3 color = vec3(1, 1, 0.0);
    fColor = vec4(color*NL, 1.0);
}

However, I end up with very weird looking faces that keeps flickering(I included a snapshot below). It occurred to me that it might be because I'm using 8 vertices to represent 1 cell(cube) instead of 24 vertices, But I'm not quite sure if that is what is causing the problem.

Left: Using Light Weighting 'NL', Right:Without Left: Using Light Weighting 'NL', Right:Without

1

1 Answers

2
votes

After every call to EmitVertex, the contents of all output variables are made undefined. Therefore, if you want to output the same value to multiple vertices, you must copy it to the output every time.

Also, note that each shader stage's outputs provide inputs only to the next stage. So if you have a GS, and you want to pass a value from the VS to the FS, you must have the GS explicitly pass that value through.