[osg-users] Trouble with a simple shader program.

Robert Osfield robert.osfield at gmail.com
Sun Apr 12 04:48:51 PDT 2015


Hi Alexander,

If you want to use the osg_ModelViewMatrix then you need to enable it via
the osg::State object for the graphics context.  See the osgsimplegl3
example, it has:

    // for non GL3/GL4 and non GLES2 platforms we need enable the osg_
uniforms that the shaders will use,
    // you don't need thse two lines on GL3/GL4 and GLES2 specific builds
as these will be enable by default.
    gc->getState()->setUseModelViewAndProjectionUniforms(true);
    gc->getState()->setUseVertexAttributeAliasing(true);

If you don't need to use GL3 or GL4 core profile, then you can just use GL2
profile which is the default for the OSG, here you can happily use
gl_ModelViewMatrix etc.  Most of the other OSG shader examples use this
approach.  Just do a search for osg::Shader in the OSG code base and you'll
find plenty of the examples to learn from.

Robert.



On 11 April 2015 at 18:21, Alexander Wieser <alexander.wieser at crystalbyte.de
> wrote:

> Hi,
>
> I am currently working on my bachelor thesis using which involves the
> OpenSceneGraph. One task is to apply a distortion Shader to a rendered
> texture.
>
> Unfortunately I am failing in getting any Shader to work at all.
> After spending the last couple of days researching and reading books, I
> managed to get several shaders running in other libraries, be it online
> using WebGL or directly using OpenGL. I simply can't get it running using
> OSG.
>
> The code initializing the program is this.
>
>
> Code:
>
> // A simple 4 vertex rectangular geometry drawable.
> Plane* plane = new Plane(40);
> this->addDrawable(plane);
>
> osg::Program* program = new osg::Program();
> program->setName("Debug Shader");
>
> // This refers to a Geode.
> osg::StateSet& state = *this->getOrCreateStateSet();
> state.setAttributeAndModes(program, osg::StateAttribute::ON);
>
> boost::filesystem::path currentPath(boost::filesystem::current_path());
>
> // Init vertex shader.
> RTT::log(RTT::Debug) << "Loading vertex shader."<< RTT::endlog();
> osg::Shader* vertexShader = new osg::Shader(osg::Shader::VERTEX);
> vertexShader->loadShaderSourceFromFile(currentPath.string() +
> "/../resources/shaders/debug.vert");
> program->addShader(vertexShader);
>
> // Init fragment shader.
> RTT::log(RTT::Debug) << "Loading fragment shader."<< RTT::endlog();
> osg::Shader* fragmentShader = new osg::Shader(osg::Shader::FRAGMENT);
> fragmentShader->loadShaderSourceFromFile(currentPath.string() +
> "/../resources/shaders/debug.frag");
> program->addShader(fragmentShader);
>
>
>
>
>
> Vertex Shader
>
> Code:
>
> #version 430
>
> uniform mat4 osg_ModelViewProjectionMatrix;
> in vec4 osg_Vertex;
>
> void main() {
> gl_Position = osg_ModelViewProjectionMatrix * osg_Vertex;
> }
>
>
>
>
>
> Fragment Shader
>
> Code:
>
> #version 430
>
> layout(location = 0) out vec4 FragColor;
>
> void main() {
> FragColor = vec4(1.0, 0.0, 0.0, 1.0);
> }
>
>
>
>
> As you can see, the program is not that complicated and does compile fine.
> Unfortunately I don't see anything on the screen. There is no model.
> If I remove the program and simply use the built in lighting, coloring and
> texturing support it renders fine.
>
> Any help is greatly appreciated.
>
>
> Thank you!
>
> Cheers,
> Alexander
>
> ------------------
> Read this topic online here:
> http://forum.openscenegraph.org/viewtopic.php?p=63328#63328
>
>
>
>
>
> _______________________________________________
> osg-users mailing list
> osg-users at lists.openscenegraph.org
> http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.openscenegraph.org/pipermail/osg-users-openscenegraph.org/attachments/20150412/20604c80/attachment-0003.htm>


More information about the osg-users mailing list