Re: [osg-users] problem in camera rotation
Hi, yeah. I found my geometry center as ( 0.04,0.075,0.0). So first I do the rotation of 90 degree about z-axis and then translating my geometry to 0 by subtracting the aboive center values. Again I m getting my geometry is translated and half of the geometry is off the view. Have a look at my code, I dont understand where I am making mistake. void applyTexture(osg::Drawable* drawable, std::string imgName) { static std::string filePath = getenv(OSGHOME); osg::ref_ptrosg::Image image = osgDB::readImageFile(filePath + imgName); osg::ref_ptrosg::Texture2D tex2D = new osg::Texture2D; tex2D-setImage(image.get()); tex2D-setResizeNonPowerOfTwoHint(false); drawable-getOrCreateStateSet()-setTextureAttributeAndModes (0, tex2D.get()); drawable-getOrCreateStateSet()-setMode(GL_LIGHTING, osg::StateAttribute::OVERRIDE | osg::StateAttribute::OFF); osg::ref_ptrosg::TexEnv texEnv = new osg::TexEnv; texEnv-setMode(osg::TexEnv::REPLACE); drawable-getOrCreateStateSet()-setTextureAttributeAndModes (0, texEnv.get()); } osg::Geode* createGeometry() { float x = 0.0, y = 0.0; float width=0.08 , height=0.15 ; std::string imgName=\\images\\ring_active.png; osg::Geometry* boxGeom = new osg::Geometry; boxGeom-setName(myGeometry); osg::Vec3Array* vertices = new osg::Vec3Array; int _z=0.0; vertices-push_back(osg::Vec3(x, y, _z)); vertices-push_back(osg::Vec3(x + width, y, _z)); vertices-push_back(osg::Vec3(x + width, y + height, _z)); vertices-push_back(osg::Vec3(x, y + height , _z)); boxGeom-setVertexArray(vertices); osg::Vec3Array* normals = new osg::Vec3Array; normals-push_back(osg::Vec3(0.0f,50.0f,1.0f)); boxGeom-setNormalArray(normals); boxGeom-setNormalBinding(osg::Geometry::BIND_OVERALL); osg::Vec4Array* colors = new osg::Vec4Array; colors-push_back(osg::Vec4(0., 0., 0., 0.9)); boxGeom-setColorArray(colors); boxGeom-setColorBinding(osg::Geometry::BIND_OVERALL); //applying texture osg::ref_ptrosg::Vec2Array texCoordArray = new osg::Vec2Array; texCoordArray-push_back(osg::Vec2(0.f, 0.f)); texCoordArray-push_back(osg::Vec2(1.0, 0.f)); texCoordArray-push_back(osg::Vec2(1.0, 1.0)); texCoordArray-push_back(osg::Vec2(0.0, 1.0)); boxGeom-setTexCoordArray(0, texCoordArray.get()); applyTexture(boxGeom, imgName); boxGeom-addPrimitiveSet(new osg::DrawArrays(GL_QUADS,0,4)); osg::StateSet* stateset = boxGeom-getOrCreateStateSet(); stateset-setMode(GL_BLEND,osg::StateAttribute::ON); stateset-setRenderingHint(osg::StateSet::TRANSPARENT_BIN); osg::Geode* _geode=new osg::Geode; _geode-addDrawable(boxGeom); _geode-setName(myGeode); geometry_center=boxGeom-getBound().center(); return _geode; } int main( int argc, char **argv ) { osg::Camera* g_camera=new osg::Camera; g_camera-setProjectionMatrixAsOrtho2D(0,1,0,1.25); g_camera-setReferenceFrame(osg::Transform::ABSOLUTE_RF); g_camera-setClearMask(GL_DEPTH_BUFFER_BIT); g_camera-setRenderOrder(osg::Camera::POST_RENDER); //rotate osg::ref_ptrosg::MatrixTransform rotMT=new osg::MatrixTransform;osg::Matrix rmatrix; rmatrix.makeRotate(osg::DegreesToRadians(45.),osg::Vec3f(0.0,0.0,1.0)); rotMT-setMatrix(rmatrix); //translate osg::ref_ptrosg::MatrixTransform transMT=new osg::MatrixTransform;osg::Matrix tmatrix; tmatrix.makeTranslate(osg::Vec3f(-0.04,-0.075,0.0)); transMT-setMatrix(tmatrix); rotMT-addChild(transMT.get()); transMT-addChild(createGeometry()); g_camera-addChild(rotMT.get()); osgViewer::Viewer viewer; viewer.setThreadingModel(osgViewer::Viewer::SingleThreaded); viewer.setCameraManipulator( new osgGA::TrackballManipulator() ); viewer.addEventHandler(new Rot); viewer.setSceneData(g_camera); osg::Vec3d eye(osg::Vec3f(0.0,0.0,0.0)); osg::Vec3d center(osg::Vec3f(0.0,0.0,0.0)); osg::Vec3d up(osg::Vec3f(0.0,0.0,0.0)); viewer.getCameraManipulator()-setHomePosition(eye,center,up); viewer.home(); viewer.realize(); while(!viewer.done()) viewer.frame(); return 0; } Cheers, Akilan -- Read this topic online here: http://forum.openscenegraph.org/viewtopic.php?p=17210#17210 ___ osg-users mailing list osg-users@lists.openscenegraph.org
Re: [osg-users] problem in camera rotation
Hi Akilan; Matrix multiplaction is not commutative. If you change the order of matrix as a result the accumulated matrix will behave different as you anticipated. So order of matrix multiplexing is Scale, Rotate and lastly Translate. So in multiplexting operation order of them as follows; AccumulatedMatrix = TranslateMatrix * RotateMatrix * ScaleMatrix; So I suggest you to use one MatrixTransform because you can do all operation in one MatrixTransform too. And try rotMT-setMatrix(tmatrix * rmatrix); and delete transMT. Use single MatrixTransform. HTH. Regards. Ümit Uzun 2009/9/10 Akilan Thangamani akilan.thangam...@gmail.com Hi, yeah. I found my geometry center as ( 0.04,0.075,0.0). So first I do the rotation of 90 degree about z-axis and then translating my geometry to 0 by subtracting the aboive center values. Again I m getting my geometry is translated and half of the geometry is off the view. Have a look at my code, I dont understand where I am making mistake. void applyTexture(osg::Drawable* drawable, std::string imgName) { static std::string filePath = getenv(OSGHOME); osg::ref_ptrosg::Image image = osgDB::readImageFile(filePath + imgName); osg::ref_ptrosg::Texture2D tex2D = new osg::Texture2D; tex2D-setImage(image.get()); tex2D-setResizeNonPowerOfTwoHint(false); drawable-getOrCreateStateSet()-setTextureAttributeAndModes (0, tex2D.get()); drawable-getOrCreateStateSet()-setMode(GL_LIGHTING, osg::StateAttribute::OVERRIDE | osg::StateAttribute::OFF); osg::ref_ptrosg::TexEnv texEnv = new osg::TexEnv; texEnv-setMode(osg::TexEnv::REPLACE); drawable-getOrCreateStateSet()-setTextureAttributeAndModes (0, texEnv.get()); } osg::Geode* createGeometry() { float x = 0.0, y = 0.0; float width=0.08 , height=0.15 ; std::string imgName=\\images\\ring_active.png; osg::Geometry* boxGeom = new osg::Geometry; boxGeom-setName(myGeometry); osg::Vec3Array* vertices = new osg::Vec3Array; int _z=0.0; vertices-push_back(osg::Vec3(x, y, _z)); vertices-push_back(osg::Vec3(x + width, y, _z)); vertices-push_back(osg::Vec3(x + width, y + height, _z)); vertices-push_back(osg::Vec3(x, y + height , _z)); boxGeom-setVertexArray(vertices); osg::Vec3Array* normals = new osg::Vec3Array; normals-push_back(osg::Vec3(0.0f,50.0f,1.0f)); boxGeom-setNormalArray(normals); boxGeom-setNormalBinding(osg::Geometry::BIND_OVERALL); osg::Vec4Array* colors = new osg::Vec4Array; colors-push_back(osg::Vec4(0., 0., 0., 0.9)); boxGeom-setColorArray(colors); boxGeom-setColorBinding(osg::Geometry::BIND_OVERALL); //applying texture osg::ref_ptrosg::Vec2Array texCoordArray = new osg::Vec2Array; texCoordArray-push_back(osg::Vec2(0.f, 0.f)); texCoordArray-push_back(osg::Vec2(1.0, 0.f)); texCoordArray-push_back(osg::Vec2(1.0, 1.0)); texCoordArray-push_back(osg::Vec2(0.0, 1.0)); boxGeom-setTexCoordArray(0, texCoordArray.get()); applyTexture(boxGeom, imgName); boxGeom-addPrimitiveSet(new osg::DrawArrays(GL_QUADS,0,4)); osg::StateSet* stateset = boxGeom-getOrCreateStateSet(); stateset-setMode(GL_BLEND,osg::StateAttribute::ON); stateset-setRenderingHint(osg::StateSet::TRANSPARENT_BIN); osg::Geode* _geode=new osg::Geode; _geode-addDrawable(boxGeom); _geode-setName(myGeode); geometry_center=boxGeom-getBound().center(); return _geode; } int main( int argc, char **argv ) { osg::Camera* g_camera=new osg::Camera; g_camera-setProjectionMatrixAsOrtho2D(0,1,0,1.25); g_camera-setReferenceFrame(osg::Transform::ABSOLUTE_RF); g_camera-setClearMask(GL_DEPTH_BUFFER_BIT); g_camera-setRenderOrder(osg::Camera::POST_RENDER); //rotate osg::ref_ptrosg::MatrixTransform rotMT=new osg::MatrixTransform;osg::Matrix rmatrix; rmatrix.makeRotate(osg::DegreesToRadians(45.),osg::Vec3f(0.0,0.0,1.0)); rotMT-setMatrix(rmatrix); //translate osg::ref_ptrosg::MatrixTransform transMT=new osg::MatrixTransform;osg::Matrix tmatrix; tmatrix.makeTranslate(osg::Vec3f(-0.04,-0.075,0.0)); transMT-setMatrix(tmatrix); rotMT-addChild(transMT.get()); transMT-addChild(createGeometry()); g_camera-addChild(rotMT.get()); osgViewer::Viewer viewer; viewer.setThreadingModel(osgViewer::Viewer::SingleThreaded); viewer.setCameraManipulator( new osgGA::TrackballManipulator() ); viewer.addEventHandler(new Rot); viewer.setSceneData(g_camera); osg::Vec3d eye(osg::Vec3f(0.0,0.0,0.0)); osg::Vec3d center(osg::Vec3f(0.0,0.0,0.0)); osg::Vec3d
Re: [osg-users] problem in camera rotation
Hi, I changed the order of matrix manipulations and put everything in a single transformation only. But the result I m getting is same. The geometry is going out of view. I dont understand the problem. Pls anyone correct me. Thank you! Cheers, Akilan -- Read this topic online here: http://forum.openscenegraph.org/viewtopic.php?p=17217#17217 ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
Re: [osg-users] problem in camera rotation
Hi, Akilan Thangamani wrote: Hi, Yes. I changed the way I was doing. I set geometry under a matrix transform node and trying to rotate the geometry 90 degree over Z-axis. It s getting translated too. I want to know how much unit I have to translate it back and If I want to rotate on other than Z-axis, what initial matrix I have to set to MT? osg::Camera* g_camera=new osg::Camera; g_camera-setProjectionMatrixAsOrtho2D(0,1,0,1); g_camera-setReferenceFrame(osg::Transform::ABSOLUTE_RF); g_camera-setClearMask(GL_DEPTH_BUFFER_BIT); g_camera-setRenderOrder(osg::Camera::POST_RENDER); osg::ref_ptrosg::MatrixTransform MT=new osg::MatrixTransform; osg::Matrix mat(osg::Matrix::identity()); mat.makeRotate(osg::DegreesToRadians(90.),osg::Vec3f(0.0,0.0,1.0)); MT-setMatrix(mat); MT-addChild(create_geom()); g_camera-addChild(MT.get()); osgViewer::Viewer viewer; viewer.setThreadingModel(osgViewer::Viewer::SingleThreaded); viewer.setCameraManipulator( new osgGA::TrackballManipulator() ); viewer.setSceneData(g_camera); viewer.realize(); while(!viewer.done()) { viewer.frame(); } Look @ the file attached. In that, figure (1) is what my actual geometry and figure (2) is what I wanted to result. The geometry would rotate about its own 0 point, so if your 0 point is not in the center, it would appear that the geometry is translating and rotating. So, what you want to do is move your geometry first so that the point you want to rotate around is where you want it (I don't know the exact numbers, you will have to look in your geometry) and then do the rotation. You can link matrixtransforms under each other, e.g. say your geometry center is at (1,1,0) you can do root-Rotate(90 about Z)-translate(-1,-1,0)-geom Have a look at the Red Book here: http://www.glprogramming.com/red/chapter03.html especially the section: Viewing and Modeling Transformations jp Thank you! Cheers, Akilan -- Read this topic online here: http://forum.openscenegraph.org/viewtopic.php?p=17167#17167 ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org -- This message is subject to the CSIR's copyright terms and conditions, e-mail legal notice, and implemented Open Document Format (ODF) standard. The full disclaimer details can be found at http://www.csir.co.za/disclaimer.html. This message has been scanned for viruses and dangerous content by MailScanner, and is believed to be clean. MailScanner thanks Transtec Computers for their support. ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
Re: [osg-users] problem in camera rotation
Hi, Akilan Thangamani wrote: Hi, I understand that this problem has already been discussed . But as I was not able to understand clearly I post my simple camera rotation problem., I written a code osg::refosg::Camera g_camera=new osg:::camera; g_camera-setProjectionMatrix(osg::Matrix::ortho2D(0,1,0,1)); g_camera-setReferenceFrame(osg::Transform::ABSOLUTE_RF); g_camera-setViewMatrix(osg::Matrix::Identity()); g_camera-setClearMask(GL_DEPTH_BUFFER_BIT); g_camera-setRenderOrder(osg::camera::POST_RENDER); g_camera-setAllowEventFocus(true); g_camera-addChild(create_geometry()); //create_geometry() fn will return a geode with //a drawable added .. .. ... osgViewer::Viewer viewer; viewer.setSceneData(g_camera.get()); . while(!viewer.done()) { if(isKey_R_pressed){ .. osg::Matrixd vmat=g_camera-getViewMatrix(); //here i get the camera thru traversal vmat.makeRotate(osg::DegreesToRadians(45.),osg::osg::Vec3f(1.,0.,0.)); g_camera-setViewMatrix(vmat); .} viewer.frame(); } } I dont understand how my geometry is not getting rotated? Do you want to rotate the camera or the geometry you are looking at? The camera with identity view matrix looks along -z axis, you are rotating about x. What is the problem in this? I don't know what you want to do, so I cannot say. Try to explain what you expect. What are you currently getting? jp } } Thank you! Cheers, Akilan -- Read this topic online here: http://forum.openscenegraph.org/viewtopic.php?p=17131#17131 ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org -- This message is subject to the CSIR's copyright terms and conditions, e-mail legal notice, and implemented Open Document Format (ODF) standard. The full disclaimer details can be found at http://www.csir.co.za/disclaimer.html. This message has been scanned for viruses and dangerous content by MailScanner, and is believed to be clean. MailScanner thanks Transtec Computers for their support. ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
Re: [osg-users] problem in camera rotation
Hi, Actually, I would like to simulate a navigation control for north orientation as available in google earth. I wanted to rotate the control along the X-axis(+/-) that will get reflected on geocentric virtual earth. I set the camera to the geometry(navigation control). What I thought was that rotating the camera would give the feel of geometry rotation. Is it the correct way to do? Thank you! Cheers, Akilan -- Read this topic online here: http://forum.openscenegraph.org/viewtopic.php?p=17141#17141 ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
Re: [osg-users] problem in camera rotation
Hi, Akilan Thangamani wrote: Hi, Actually, I would like to simulate a navigation control for north orientation as available in google earth. I wanted to rotate the control along the X-axis(+/-) that will get reflected on geocentric virtual earth. I set the camera to the geometry(navigation control). What I thought was that rotating the camera would give the feel of geometry rotation. Is it the correct way to do? It's all relative, so you can make a camera rotation plus translation look like a geometry rotation, but I would just rotate the geometry. Put your geometry under a matrixtransform node. jp Thank you! Cheers, Akilan -- Read this topic online here: http://forum.openscenegraph.org/viewtopic.php?p=17141#17141 ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org -- This message is subject to the CSIR's copyright terms and conditions, e-mail legal notice, and implemented Open Document Format (ODF) standard. The full disclaimer details can be found at http://www.csir.co.za/disclaimer.html. This message has been scanned for viruses and dangerous content by MailScanner, and is believed to be clean. MailScanner thanks Transtec Computers for their support. ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org