The uniform is missing a type. Try:
primitiveState->getOrCreateUniform("RotationMatrix",
osg::Uniform::FLOAT_MAT4)->set(rotationMatrix);
Glenn Waldron / osgEarth / @glennwaldron
On Sun, Oct 6, 2013 at 6:59 AM, Sajjadul Islam <[email protected]>wrote:
> Hi forum,
>
> I am sending a osg::Matrix as uniform value to the shader and i am getting
> a warning and a blank screen:
>
>
> Code:
>
> Warning: detected OpenGL error 'invalid operation' at After
> Renderer::compile
>
>
>
>
> I am sending the uniform as follows:
>
>
> Code:
>
> #include <osg/ref_ptr>
> #include <osgDB/Registry>
> #include <osgDB/WriteFile>
> #include <osg/Notify>
> #include <osg/Geode>
> #include <osg/GraphicsContext>
> #include <osg/Geometry>
> #include <osg/Shader>
> #include <osg/Program>
> #include <osg/Camera>
> #include <osgViewer/Viewer>
> #include <osg/Uniform>
> #include <osg/MatrixTransform>
>
> #include <iostream>
>
> using std::endl;
>
> osg::ref_ptr<osg::Node> createSceneGraph();
>
> int main()
> {
> osg::ref_ptr<osg::Node> root = createSceneGraph();
>
> const std::string version("4.2");
> osg::ref_ptr<osg::GraphicsContext::Traits> traits = new
> osg::GraphicsContext::Traits();
> traits->x = 20;
> traits->y = 30;
> traits->width = 800;
> traits->height = 600;
> traits->windowDecoration = true;
> traits->doubleBuffer = true;
> traits->glContextVersion = version;
>
>
> osg::ref_ptr< osg::GraphicsContext > gc =
> osg::GraphicsContext::createGraphicsContext( traits.get() );
>
>
>
> if( !gc.valid() )
> {
> osg::notify( osg::FATAL ) << "Unable to create OpenGL v" << version
> << " context." << std::endl;
> return( 1 );
> }
>
> osg::ref_ptr<osg::Camera> camera = new osg::Camera;
> camera->setGraphicsContext(gc.get());
> camera->setViewport(new osg::Viewport(0,0,traits->width,traits->height));
>
> osgViewer::Viewer viewer;
> viewer.setCamera(camera.get());
> viewer.setSceneData(root.get());
>
> return viewer.run() ;
> }
>
> osg::ref_ptr<osg::Node> createSceneGraph()
> {
> //create an object to store the geometry in
> osg::ref_ptr<osg::Geometry> geom = new osg::Geometry;
>
> //create an array of four vertices
> osg::ref_ptr<osg::Vec3Array> v = new osg::Vec3Array;
>
> v->push_back(osg::Vec3(-0.8f, -0.8f, 0.0f));
> v->push_back(osg::Vec3(0.8f, -0.8f, 0.0f));
> v->push_back(osg::Vec3(0.0f, 0.8f, 0.0f));
>
> // geom->setVertexArray(v.get());
>
> //make sure that vertex array is used
> geom->setUseDisplayList(false);
> geom->setVertexAttribArray(0,v.get());
> geom->setVertexAttribBinding(0,osg::Geometry::BIND_PER_VERTEX);
>
> //create an array of three colors
> osg::ref_ptr<osg::Vec3Array> c = new osg::Vec3Array;
> c->push_back(osg::Vec3(1.0f, 0.0f, 0.0f));
> c->push_back(osg::Vec3(0.0f, 1.0f, 0.0f));
> c->push_back(osg::Vec3(0.0f, 0.0f, 1.0f));
>
> // geom->setColorArray(c.get());
> geom->setColorBinding(osg::Geometry::BIND_PER_VERTEX);
> geom->setVertexAttribArray(1,c.get());
> geom->setVertexAttribBinding(1,geom->getColorBinding());
>
>
> //draw a three-vertex tringle from the stored data
> geom->addPrimitiveSet(new
> osg::DrawArrays(osg::PrimitiveSet::TRIANGLES,0,3));
>
> //add the geometry to the geode and return the geode
> osg::ref_ptr<osg::Geode> geode = new osg::Geode;
> geode->addDrawable(geom.get());
>
>
> osg::ref_ptr<osg::StateSet> primitiveState =
> geode->getOrCreateStateSet();
>
> //declare a rotation matrix
> osg::Matrix rotationMatrix;
> rotationMatrix.rotate(50,osg::Vec3(0.0f,0.0f,1.0f));
>
> //add the rotation matrix as uniform to primitives state
> primitiveState->addUniform(new osg::Uniform("RotationMatrix",
> rotationMatrix));
>
> osg::ref_ptr<osg::Program> basicPrimitiveShaderProg = new osg::Program;
>
> osg::ref_ptr<osg::Shader>
> primitiveVertexShader(osg::Shader::readShaderFile(osg::Shader::VERTEX,"shader/basicUniform.vert"));
> osg::ref_ptr<osg::Shader>
> elephantFragmentShader(osg::Shader::readShaderFile(osg::Shader::FRAGMENT,"shader/basicUniform.frag"));
>
> basicPrimitiveShaderProg->addShader(primitiveVertexShader.get());
> basicPrimitiveShaderProg->addShader(elephantFragmentShader.get());
>
> basicPrimitiveShaderProg->addBindAttribLocation("VertexPosition",0);
> basicPrimitiveShaderProg->addBindAttribLocation("VertexColor",1);
>
>
> primitiveState->setAttributeAndModes(basicPrimitiveShaderProg.get(),osg::StateAttribute::ON);
>
>
> return geode.get();
> }
>
>
>
>
>
> Any idea ?
>
> Regards,
> Sajjadul
>
> ------------------
> Read this topic online here:
> http://forum.openscenegraph.org/viewtopic.php?p=56634#56634
>
>
>
>
>
> _______________________________________________
> osg-users mailing list
> [email protected]
> http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
>
_______________________________________________
osg-users mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org