Hello,
I’ve been playing with shaders using OpenSceneGraph and as an experiment, I thought I’d try implementing vertex normals using a geometry shader.
What’s odd is that I can get the shader to output only two of the three normals at a time and I have no idea why. If I comment out any one of the three normals, the other two are rendered fine. If, however, I try to render all three, the shader fails.
Unfortunately, I’m having a difficult time extracting the log in OSG, so I don’t have compile error information (working on that, though).
That said, I have no idea why the code doesn’t work for all three normals, nor am I sure that the problem is in the shader code (it could be something with OSG, I suppose).
Anyway, the shader code follows. Any thoughts would be appreciated.
vertex shader:
#version 150
uniform mat4 osg_ModelViewMatrix; //from application program.
uniform mat4 osg_ModelViewProjectionMatrix;
in vec4 osg_Vertex; //from vertex buffer from app pgm.
void main (void) {
gl_Position = osg_ModelViewMatrix * osg_Vertex;
}
geometry shader:
#version 150
layout (triangles) in;
layout (line_strip, max_vertices = 6) out;
uniform mat4 osg_ProjectionMatrix; //from application program.
out vec3 normal;
void main (void) {
vec3 vector1;
vec3 vector2;
vector1 = gl_in[1].gl_Position.xyz - gl_in[0].gl_Position.xyz;
vector2 = gl_in[2].gl_Position.xyz - gl_in[0].gl_Position.xyz;
normal = normalize (cross (vector1, vector2));
gl_Position = osg_ProjectionMatrix * gl_in[0].gl_Position;
EmitVertex ();
gl_Position = osg_ProjectionMatrix * (gl_in[0].gl_Position + vec4(normal, 0.0));
EmitVertex ();
EndPrimitive();
gl_Position = osg_ProjectionMatrix * gl_in[1].gl_Position;
EmitVertex ();
gl_Position = osg_ProjectionMatrix * (gl_in[1].gl_Position + vec4(normal, 0.0));
EmitVertex ();
EndPrimitve ();
gl_Position = osg_ProjectionMatrix * gl_in[2].gl_Position;
EmitVertex ();
gl_Position = osg_ProjectionMatrix * (gl_in[2].gl_Position + vec4(normal, 0.0));
EmitVertex ();
}
frag shader:
#version 150
in vec3 normal; //from geometry shader.
out vec4 color; //to framebuffer.
void main (void) {
gl_FragColor = vec4 (normal, 1.0);
}