Re: [osg-users] Problem passing matrices as uniform
Hi I think its wrong matrix multiplication order: mvmatrix = _viewercam-getProjectionMatrix()*vmatrix*wmatrix; should be mvmatrix = wmatrix*vmatrix*_viewercam-getProjectionMatrix(); in osg to transform vertex you use v = v * m, in shaders it's opposite Cheers, Sergey. 12.08.2012, 12:59, Felix Meißgeier felixmeissge...@web.de: Hey, I can't find a solution. Maybe it helps if I post some code I extracted from my project. It demonstrates my camera- and shader-setup. In the case I only pass prjection-matrix to vertex-shader and replace testmatrix*gl_Vertex with gl_ProjectionMatrix*testmatrix*gl_Vertex everything works fine. But when I try to pass the whole modelviewprojection matrix it failed and nothing of the scene is rendered. Thanks for help! osg-code: Code: struct TestCallback : public osg::Uniform::Callback{ TestCallback(osg::Camera* viewercam, osg::Camera* othercam) :_viewercam(viewercam),_othercam(othercam){ } virtual void operator () (osg::Uniform* uniform, osg::NodeVisitor* nv){ osg::Matrixd vmatrix=_viewercam-getViewMatrix(); //vmatrix=vmatrix.rotate(- M_PI / 2.0, 1, 0, 0); osg::Matrixd wmatrix=osg::computeLocalToWorld(nv-getNodePath()); osg::Matrixd mvmatrix = _viewercam-getProjectionMatrix()*vmatrix*wmatrix; uniform-set(mvmatrix); } osg::Camera* _viewercam; osg::Camera* _othercam; }; class MovementPostDrawCallback : public osg::Camera::DrawCallback{ public: MovementPostDrawCallback(osg::PositionAttitudeTransform* patnode, double velocity) :_patnode(patnode),_velocity(velocity){ _startpos=_patnode-getPosition(); } virtual void operator()(osg::RenderInfo renderinfo) const{ osg::Vec3 crtpos=_patnode-getPosition(); double crttimestp=renderinfo.getState()-getFrameStamp()-getSimulationTime(); double bankangle=0.7; crtpos[0]=_startpos[0]+sin(crttimestp)*100.0*_velocity; bankangle=-sin(crttimestp)/2; _patnode-setPosition(crtpos); _patnode-setAttitude(osg::Quat(0.0, osg::X_AXIS, bankangle, osg::Y_AXIS, 0.0, osg::Z_AXIS)); } osg::PositionAttitudeTransform* _patnode; osg::Vec3 _startpos; double _velocity; }; bool loadShaderSrcFiles(std::string vssrcfile, osg::Shader* vs, std::string fssrcfile, osg::Shader* fs){ bool returnval=true; if(!vs-loadShaderSourceFromFile(vssrcfile)){ osg::notify(osg::WARN)Error while loading vertexshader-sourcefile (vssrcfile)...; returnval=false; } if(!fs-loadShaderSourceFromFile(fssrcfile)) osg::notify(osg::WARN)Error while loading fragmentshader-sourcefile (fssrcfile)...; return returnval; } int _tmain(int argc, _TCHAR* argv[]) { osg::setNotifyLevel( osg::WARN ); int width=800; int height=600; osg::Vec2 displayres(width,height); std::coutsetup scene...\n; //setup scene SceneBuilder sbuilder; osg::Node* root = sbuilder.buildSubgraph(); std::coutscene built...\n; //setup texture osg::ref_ptrosg::Texture2D texture = new osg::Texture2D(); texture-setTextureSize(width,height); texture-setInternalFormat(GL_RGBA); texture-setFilter(osg::Texture2D::MIN_FILTER, osg::Texture2D::LINEAR); texture-setFilter(osg::Texture2D::MAG_FILTER, osg::Texture2D::LINEAR); //rtt cam osg::ref_ptrosg::Camera rttcam = new osg::Camera(); rttcam-setViewport(0,0,width, height); rttcam-setClearMask(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); rttcam-setClearColor(osg::Vec4(105/255.0,125/255.0,202/255.0,1.0)); rttcam-setRenderOrder(osg::Camera::PRE_RENDER,1); rttcam-setRenderTargetImplementation(osg::Camera::FRAME_BUFFER_OBJECT); rttcam-setReferenceFrame(osg::Transform::RELATIVE_RF); rttcam-setProjectionMatrix(osg::Matrixd::identity()); rttcam-attach(osg::Camera::COLOR_BUFFER, texture.get()); rttcam-addChild(root); rttcam-setPostDrawCallback(new MovementPostDrawCallback(sbuilder.getCessnaPAT(),0.8)); rttcam-setName(RTTCam); std::coutrttcam created...\n; //setup postrender-geode osg::ref_ptrosg::Geometry prquad = osg::createTexturedQuadGeometry(osg::Vec3(),osg::Vec3(width,0.0,0.0),osg::Vec3(0.0,height,0.0)); osg::ref_ptrosg::Geode prgeode = new osg::Geode(); prgeode-addDrawable(prquad.get()); prgeode-setName(PostRenderDisplayGeode); prquad-getOrCreateStateSet()-setTextureAttributeAndModes(0,texture.get(),osg::StateAttribute::ON); prquad-setName(PostRenderDisplayQuad); //visualisation osg::ref_ptrosg::Camera scenecam = new osg::Camera(); scenecam-setViewport(0,0,width, height); scenecam-setClearMask(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT); scenecam-setClearColor(osg::Vec4(1.0,1.0,1.0,1.0)); scenecam-setRenderOrder(osg::Camera::NESTED_RENDER); scenecam-setRenderTargetImplementation(osg::Camera::FRAME_BUFFER_OBJECT); scenecam-setReferenceFrame(osg::Camera::ABSOLUTE_RF); scenecam-setProjectionMatrix(osg::Matrix::ortho2D(0,width,0,height)); scenecam-addChild(prgeode.get());
Re: [osg-users] Problem passing matrices as uniform
Hi, yes I also recognized this as the main-mistake this morning... anyway thanks for help ! regards, Felix -- Read this topic online here: http://forum.openscenegraph.org/viewtopic.php?p=49244#49244 ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
Re: [osg-users] Problem passing matrices as uniform
Hey, I can't find a solution. Maybe it helps if I post some code I extracted from my project. It demonstrates my camera- and shader-setup. In the case I only pass prjection-matrix to vertex-shader and replace testmatrix*gl_Vertex with gl_ProjectionMatrix*testmatrix*gl_Vertex everything works fine. But when I try to pass the whole modelviewprojection matrix it failed and nothing of the scene is rendered. Thanks for help! osg-code: Code: struct TestCallback : public osg::Uniform::Callback{ TestCallback(osg::Camera* viewercam, osg::Camera* othercam) :_viewercam(viewercam),_othercam(othercam){ } virtual void operator () (osg::Uniform* uniform, osg::NodeVisitor* nv){ osg::Matrixd vmatrix=_viewercam-getViewMatrix(); //vmatrix=vmatrix.rotate(- M_PI / 2.0, 1, 0, 0); osg::Matrixd wmatrix=osg::computeLocalToWorld(nv-getNodePath()); osg::Matrixd mvmatrix = _viewercam-getProjectionMatrix()*vmatrix*wmatrix; uniform-set(mvmatrix); } osg::Camera* _viewercam; osg::Camera* _othercam; }; class MovementPostDrawCallback : public osg::Camera::DrawCallback{ public: MovementPostDrawCallback(osg::PositionAttitudeTransform* patnode, double velocity) :_patnode(patnode),_velocity(velocity){ _startpos=_patnode-getPosition(); } virtual void operator()(osg::RenderInfo renderinfo) const{ osg::Vec3 crtpos=_patnode-getPosition(); double crttimestp=renderinfo.getState()-getFrameStamp()-getSimulationTime(); double bankangle=0.7; crtpos[0]=_startpos[0]+sin(crttimestp)*100.0*_velocity; bankangle=-sin(crttimestp)/2; _patnode-setPosition(crtpos); _patnode-setAttitude(osg::Quat(0.0, osg::X_AXIS, bankangle, osg::Y_AXIS, 0.0, osg::Z_AXIS)); } osg::PositionAttitudeTransform* _patnode; osg::Vec3 _startpos; double _velocity; }; bool loadShaderSrcFiles(std::string vssrcfile, osg::Shader* vs, std::string fssrcfile, osg::Shader* fs){ bool returnval=true; if(!vs-loadShaderSourceFromFile(vssrcfile)){ osg::notify(osg::WARN)Error while loading vertexshader-sourcefile (vssrcfile)...; returnval=false; } if(!fs-loadShaderSourceFromFile(fssrcfile)) osg::notify(osg::WARN)Error while loading fragmentshader-sourcefile (fssrcfile)...; return returnval; } int _tmain(int argc, _TCHAR* argv[]) { osg::setNotifyLevel( osg::WARN ); int width=800; int height=600; osg::Vec2 displayres(width,height); std::coutsetup scene...\n; //setup scene SceneBuilder sbuilder; osg::Node* root = sbuilder.buildSubgraph(); std::coutscene built...\n; //setup texture osg::ref_ptrosg::Texture2D texture = new osg::Texture2D(); texture-setTextureSize(width,height); texture-setInternalFormat(GL_RGBA); texture-setFilter(osg::Texture2D::MIN_FILTER, osg::Texture2D::LINEAR); texture-setFilter(osg::Texture2D::MAG_FILTER, osg::Texture2D::LINEAR); //rtt cam osg::ref_ptrosg::Camera rttcam = new osg::Camera(); rttcam-setViewport(0,0,width, height); rttcam-setClearMask(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); rttcam-setClearColor(osg::Vec4(105/255.0,125/255.0,202/255.0,1.0)); rttcam-setRenderOrder(osg::Camera::PRE_RENDER,1); rttcam-setRenderTargetImplementation(osg::Camera::FRAME_BUFFER_OBJECT); rttcam-setReferenceFrame(osg::Transform::RELATIVE_RF); rttcam-setProjectionMatrix(osg::Matrixd::identity()); rttcam-attach(osg::Camera::COLOR_BUFFER, texture.get()); rttcam-addChild(root); rttcam-setPostDrawCallback(new MovementPostDrawCallback(sbuilder.getCessnaPAT(),0.8)); rttcam-setName(RTTCam); std::coutrttcam created...\n; //setup postrender-geode osg::ref_ptrosg::Geometry prquad = osg::createTexturedQuadGeometry(osg::Vec3(),osg::Vec3(width,0.0,0.0),osg::Vec3(0.0,height,0.0)); osg::ref_ptrosg::Geode prgeode = new osg::Geode(); prgeode-addDrawable(prquad.get()); prgeode-setName(PostRenderDisplayGeode); prquad-getOrCreateStateSet()-setTextureAttributeAndModes(0,texture.get(),osg::StateAttribute::ON); prquad-setName(PostRenderDisplayQuad); //visualisation osg::ref_ptrosg::Camera scenecam = new osg::Camera(); scenecam-setViewport(0,0,width, height); scenecam-setClearMask(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT); scenecam-setClearColor(osg::Vec4(1.0,1.0,1.0,1.0)); scenecam-setRenderOrder(osg::Camera::NESTED_RENDER); scenecam-setRenderTargetImplementation(osg::Camera::FRAME_BUFFER_OBJECT); scenecam-setReferenceFrame(osg::Camera::ABSOLUTE_RF); scenecam-setProjectionMatrix(osg::Matrix::ortho2D(0,width,0,height)); scenecam-addChild(prgeode.get()); scenecam-setName(SceneCam); std::coutscenecam created...\n; osg::ref_ptrosg::Group rootgroup = new osg::Group(); rootgroup-setName(RootGroup); rootgroup-addChild(rttcam.get()); rootgroup-addChild(scenecam.get()); osgViewer::Viewer viewer; viewer.setUpViewInWindow(250,50,width,height); viewer.setSceneData(rootgroup.get()); osg::ref_ptrosg::Shader testverts = new osg::Shader(osg::Shader::VERTEX);
Re: [osg-users] Problem passing matrices as uniform
Hi, Felix I dont know for sure what matrix get returned from state, but if you get view\projection matrix from camera (or , for projection matrix - from osg::Projection) - it works perfectly fine, at least for me. Cheers, Sergey. 11.08.2012, 02:52, Felix Meißgeier felixmeissge...@web.de: Hey, i've got a problem passing modelview-matrix as uniform to a vertexshader. I want to realize a object-motion-blur-effect and so I need the previous-mv-matrix. More precisely I guess (not to say I'm sure) that my passed matrix doesn't work. I read about some difference between the orientations of the opengl- and osg-coordinate system. Could this be a reason, why the fixed-builtin-uniforms gl_ProjectionMatrix, gl_ModelViewMatrix,... work perfectly and my matrix doesn't?? For testing purpose I created a simple shader which bypasses the clipcoords of a vertex to fragmentshader (gl_Position) and sets gl_FragColor to white (using my own projection-matrix). The computation gl_Position = gl_ProjectionMatrix * gl_ModelViewMatrix * gl_Vertex worked fine, but gl_Position = myownprojmat * gl_ModelViewMatrix * gl_Vertex failed. I tried different ways to generate the proj-matrix. I got it from my (pre-render-) camera which is the parent of my scene-root and I tried Code: osgViewer::Viewer::Contexts contexts; _viewer-getContexts(contexts); pmatrix=contexts[0]-getState()-getProjectionMatrix(); During my tests passing the modelview-matrix I used the view-matrix of the parental-camera and the computelocaltoworld(osg::Nodepath)-function... Maybe anybody has an idea where I've got the mistake or has a tip how to implement this. Thank you! Cheers, Felix -- Read this topic online here: http://forum.openscenegraph.org/viewtopic.php?p=49235#49235 ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
Re: [osg-users] Problem passing matrices as uniform
Thanks for help! But the problem already exists. Actually the main-problem seems to be, that either the view-matrix or the world-(model-)matrix is not passed to the shader correctly. I have a scenegraph consisting of two rendertotexture-prerender-cams (one to compute a velocity map and one to actually render the scene to a texture) and of one nested-cam (scenecam, which adds the prerendered texture to a geometry and displays it). All cams are added to a group-node and latter is used as scenedata of a osgViewer::Viewer. Because of the fact, that I think, that something is wrong with the viewmatrix I want to make sure, that it's right to use the viewercam (viewer.getCamera()) or could there be a problem with the nested-cam??? thanks, regards Felix -- Read this topic online here: http://forum.openscenegraph.org/viewtopic.php?p=49237#49237 ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
[osg-users] Problem passing matrices as uniform
Hey, i've got a problem passing modelview-matrix as uniform to a vertexshader. I want to realize a object-motion-blur-effect and so I need the previous-mv-matrix. More precisely I guess (not to say I'm sure) that my passed matrix doesn't work. I read about some difference between the orientations of the opengl- and osg-coordinate system. Could this be a reason, why the fixed-builtin-uniforms gl_ProjectionMatrix, gl_ModelViewMatrix,... work perfectly and my matrix doesn't?? For testing purpose I created a simple shader which bypasses the clipcoords of a vertex to fragmentshader (gl_Position) and sets gl_FragColor to white (using my own projection-matrix). The computation gl_Position = gl_ProjectionMatrix * gl_ModelViewMatrix * gl_Vertex worked fine, but gl_Position = myownprojmat * gl_ModelViewMatrix * gl_Vertex failed. I tried different ways to generate the proj-matrix. I got it from my (pre-render-) camera which is the parent of my scene-root and I tried Code: osgViewer::Viewer::Contexts contexts; _viewer-getContexts(contexts); pmatrix=contexts[0]-getState()-getProjectionMatrix(); During my tests passing the modelview-matrix I used the view-matrix of the parental-camera and the computelocaltoworld(osg::Nodepath)-function... Maybe anybody has an idea where I've got the mistake or has a tip how to implement this. Thank you! Cheers, Felix -- Read this topic online here: http://forum.openscenegraph.org/viewtopic.php?p=49235#49235 ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org