Hi,
I've been developing an app in OpenSceneGraph to help me learn shaders. One
component of that is to be able to visualize normals (surface, lighting,
camera, etc.). I am attempting to implement geometry shaders to do this in
GLSL 1.5 and OSG 2.9.9. At present, I've had success loading vertex and
fragment shaders, but the geometry shader implementation eludes me.
>From what little I understand of geometry shaders and GLSL, I don't see
>anything wrong with my shader, but since I can't use RenderMonkey to test for
>compile errors, I could be wrong.
That said, I've attempted to implement the shader (vertex, geometry and
fragment), and I don't see any compile errors on the console when I run the
program in debug mode (using VS2010) and break execution just before I run the
viewer. I have set OSG_NOTIFY_LEVEL to DEBUG, so I expect that any compile
problems with the shaders would be output to the console window. If not, and I
have to retrieve the info log myself, I'd appreciate some info on how to do
that, as attempting to do so generates an unhandled exception for me.
Relevant code:
Vertex Shader
Code:
#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
Code:
#version 150
layout (triangles) in;
layout (traingle_strip, max_vertices = 3) out;
uniform mat4 osg_ProjectionMatrix; //from application program.
out vec3 normal; //to fragment shader.
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[1].gl_Position;
EmitVertex ();
gl_Position = osg_ProjectionMatrix * gl_in[2].gl_Position;
EmitVertex ();
EndPrimitve ();
}
Fragment Shader
Code:
#version 150
in vec3 normal; //from geometry shader.
out vec4 color; //to framebuffer.
void main (void) {
gl_FragColor = vec4 (normal, 1.0);
}
OSG Code
Code:
void loadShaders (osg::ref_ptr<osg::Group> rootGroup)
{
osg::ref_ptr<osg::Geode> rootGeode (dynamic_cast<osg::Geode *>
(FindNode(rootGroup, "geode_root")->getFirst()));
osg::ref_ptr<osg::Geometry> SAQgeometry (FindGeometry(rootGeode,
"geometry_ScreenAlignedQuad"));
osg::ref_ptr<osg::Shader> vertexShader(
osg::Shader::readShaderFile (osg::Shader::VERTEX,
"C:/OpenSceneGraph/OpenSceneGraph-Data-2.8.0/myShaders/test.vert"));
osg::ref_ptr<osg::Shader> geometryShader(
osg::Shader::readShaderFile (osg::Shader::GEOMETRY,
"C:/OpenSceneGraph/OpenSceneGraph-Data-2.8.0/myShaders/test.geom"));
osg::ref_ptr<osg::Shader> fragShader(
osg::Shader::readShaderFile (osg::Shader::FRAGMENT,
"C:/OpenSceneGraph/OpenSceneGraph-Data-2.8.0/myShaders/test.frag"));
osg::ref_ptr<osg::Program> program (new osg::Program());
program->addShader(vertexShader); //new osg::Shader(osg::Shader::VERTEX,
vertexShaderSource));
program->addShader(geometryShader);
program->addShader(fragShader); //new osg::Shader(osg::Shader::FRAGMENT,
fragmentShaderSource));
program->setParameter( GL_GEOMETRY_VERTICES_OUT_EXT, 3 );
program->setParameter( GL_GEOMETRY_INPUT_TYPE_EXT, GL_TRIANGLE_STRIP );
program->setParameter( GL_GEOMETRY_OUTPUT_TYPE_EXT, GL_TRIANGLE_STRIP );
program->addBindAttribLocation("osg_Vertex", 0);
program->addBindAttribLocation("osg_Normal", 2);
SAQgeometry->getOrCreateStateSet()->setAttribute(program);
////////////////////////////////////////
osg::ref_ptr<osg::StateSet> ss = SAQgeometry->getStateSet();
...
}
Just to ensure there's nothing wrong with my OSG code, I commented out the
geometry shader-related code and had the frag shader color the primitive yellow
and it worked perfectly.
Anyway, I've looked for answers and I'm finding geometry shader information,
esp. as it relates to OSG is (not surprisingly) somewhat sparse.
I appreciate any thoughts / comments / tips that anyone may have.
Thanks.
Joel
------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=34101#34101
_______________________________________________
osg-users mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org