# Problem with vertex normals! >_<

Hello, I’m writing here because I can’t understand where the problem/s of my shader/s is/are…

For debugging I set up two programs, one that renders the mesh and other program to show the vertex normal vectors using a geometry shader. So i render two times.

The first one (showing the vertex normals):

VS:

``````
#version 330

layout(std140) uniform MatriusGlobals
{
mat4 ProjectionMatrix;
mat4 LookAtMatrix; //MODELVIEW MATRIX
};

in vec3 in_Position;
in vec3 in_Normals;

out VertexData
{
vec3 normal;

}vertex;

void main(void)
{

mat3 NormalMatrix = transpose(mat3(inverse(LookAtMatrix)));
mat4 MVP = ProjectionMatrix * LookAtMatrix;

vertex.normal=normalize(NormalMatrix * in_Normals);

gl_Position = MVP * vec4(in_Position, 1.0);
}

``````

GS:

``````
#version 330
precision highp float;

layout (triangles) in;
layout (line_strip) out;
layout (max_vertices = 8) out;

in VertexData
{
vec3 normal;

}vertex[];

void main()
{
for(int i = 0; i< gl_in.length(); i++)
{
gl_Position = gl_in[i].gl_Position;
EmitVertex();

gl_Position = vec4 (gl_in[i].gl_Position.xyz + (vertex[i].normal.xyz *25.0),1.0);
EmitVertex();
EndPrimitive();

}
}

``````

FP:

``````#version 330

out vec4 out_Color;
void main(void)
{
out_Color = vec4(0.0,0.4,0.6,1.0);
}

``````

Now to render the mesh i’m using this program:
VS

``````
#version 330

layout(std140) uniform MatriusGlobals
{
mat4 ProjectionMatrix;
mat4 LookAtMatrix;
};

in vec3 in_Position;
in vec3 in_Normals;

out vec4 pass_Color;

uniform vec3 posicio_llum;
uniform vec3 diffuseColor;

void main(void)
{
//TRANSFORMO EL VECTOR DE LA NORMAL
mat3 NormalMatrix = transpose(mat3(inverse(LookAtMatrix)));
vec3 vNormal=normalize(NormalMatrix * in_Normals);

//TRANSFORMO LA POSICIO DEL VERTEX
mat4 MVP = ProjectionMatrix * LookAtMatrix;
vec4 posicio_buffer = MVP * vec4(in_Position, 1.0);
vec3 posicio_vertex = posicio_buffer.xyz / posicio_buffer.w;

//CALCULO EL VECTOR QUE FORMA LA LLUM AMB EL VERTEX
vec3 vector_director_llum = normalize(posicio_llum - posicio_vertex);

//FAIG EL PRODUCTE ESCALAR ENTRE EL VECTOR DE LA LLUM I EL VECTOR DE LA NORMAL
float intensitat = clamp(dot(vNormal,vector_director_llum),0.0,1.0);

//CALCULO EL COLOR AMB LA INTENSITAT
pass_Color.xyz = intensitat * diffuseColor;
pass_Color.a= 1.0;

//PASSO LA TRANSFORMACIO DEL VERTEX
gl_Position = posicio_buffer;
}
``````

FS:

``````#version 330

out vec4 out_Color;

in vec4 pass_Color;

void main(void)
{
out_Color = pass_Color;

}
``````

What i get is that:

IMAGE
The problem is if I move the camera (a spherical one) there are certain positions that mess all. Here you can see what happens:

IMAGE

What’s happening? :S In other tests the geometry is ok with the camera, but in that test the normals are “unhappy” in certain positions, why?

Thank you, and sorry for my English!

The probable reason for this is that you are using the clip space positions in the normal drawing geometry shader. To circumvent this dont multiply the vertex positions with the MVP matrix in the normal’s vertex shader let the positions pass through as it is to the geometry shader i.e. you vertex shader is simply passing the object space normals and positions

``````
void main(void)
{
vertex.normal=in_Normals;
gl_Position = vec4(in_Position, 1.0);
}

``````

Now in the geometry shader, do the MVP matrix multiplication

``````
for(int i = 0; i< gl_in.length(); i++)
{
gl_Position = MVP* gl_in[i].gl_Position;
EmitVertex();

gl_Position = MVP* vec4 (gl_in[i].gl_Position.xyz + (vertex[i].normal.xyz *25.0),1.0);
EmitVertex();
EndPrimitive();

}

``````

Hello Mobeen thank you!

Now I can see the vertex normals very well. But this doesn’t solve the problem with them.

FULL IMAGE
This Image shows what i’m getting, from left to right, the first one is ok but when I move the camera you can see how the vertex normals are wrong in the others to the point that they are like inverted… it’s problem of my normal matrix? Or what’s causing this? >_<