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:
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!