1
votes

Im currently writing my own GLSL shaders, and wanted to have smooth shading. The shading worked when i calculated the normals bevore sending them to a VBO, but the problem here is when I implement animations with bone matricies, the normals are not corect.

I am using a geometry shader to calculate the normals, but i cant find out how to smooth them.

Here is my geometry shader:

#version 150

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

in vec2 texCoord0[3];
in vec3 worldPos0[3];

out vec2 texCoord1;
out vec3 normal1;
out vec3 worldPos1;

 void main()
 {

        vec3 n = cross(worldPos0[1].xyz-worldPos0[0].xyz, worldPos0[2].xyz-worldPos0[0].xyz);
        for(int i = 0; i < gl_in.length(); i++)
        {
             gl_Position = gl_in[i].gl_Position;

             texCoord1 = texCoord0;
             normal1 = n;
             worldPos1 = worldPos0;

             EmitVertex();
        }
}

I need the faces next to the face that I calculate the normals for, but i dont know how to get them.

1
do you need to recalculate normals each frame?fen
Do the normals need to be unit length? i.e., normalized ?Brett Hale
Why are you performing skeletal posing in the geometry shader? Posing is normally done in the vertex shader.datenwolf
@BrettHale I normalize them in the fragment shader.CoderCloud
@datenwolf I have my bone matrices in my fragment shader.CoderCloud

1 Answers

2
votes

The geometry shader in OpenGL only has access to single triangles and not the whole mesh, so the normal must be calculated from a single triangle.

The usual solution to this problem is to calculate the normals once for each vertex and store them in vertex arrays for easy access. This turns out to be faster and simpler, as you don't need to recalculate anything in shaders.