osg 坐标系以及坐标变换

        刚刚接触osg,写的有什么问题欢迎批评指正~

        关于osg坐标系的以及坐标变换csdn有很多说明,我这里要说的是如何理解,坐标变化和坐标系的关系。其实很简单坐标变换无非平移和旋转两个步骤,然后把变化和坐标系对应起来就行了,也就是说弄清在什么坐标系下做的变换。

       举个例子:想要改变物体在世界坐标系的位置,那么直接使用osg提供的MatrixTransform类绑定对应的节点就行了。

        那么就会出现以下两个途径,如果想要改变物体和相机的相对位置,既可以在世界坐标系下改变物体位置,也可以在相机坐标系下对物体进行平移或者旋转。二者本质上是一样的,只不过在平移矩阵和旋转矩阵参数上有差别。

        下面是一段我初学的代码

         

[code]

#include "stdafx.h"


#include <Windows.h>


#include <osgViewer/Viewer>
#include <osgDB/ReadFile>
#include <osg/Node>
#include <osg/Geode>
#include <osg/Geometry>
#include <osg/ShapeDrawable>
#include <osg/MatrixTransform>




int main(int argc,char** argv)
{
osgViewer::Viewer view;
osg::Group* root = new osg::Group();


// this part of code show you how to using class Geometry to plot the object
// the Geometry is one of method belong to the class of Drawable ,
//      and the other is ShapeDrawable,which dispaly in the next part 
osg::ref_ptr<osg::Geometry> geom = new osg::Geometry();
//定义顶点
osg::ref_ptr<osg::Vec3Array> v = new osg::Vec3Array();
geom->setVertexArray(v);
v->push_back(osg::Vec3(-1.f,0.f,1.f));
v->push_back(osg::Vec3(1.f,0.f,-1.f));
v->push_back(osg::Vec3(1.f,0.f,1.f));
//定义颜色数组
osg::ref_ptr<osg::Vec4Array> c =new osg::Vec4Array();
geom->setColorArray(c);
geom->setColorBinding(osg::Geometry::BIND_PER_VERTEX);
c->push_back(osg::Vec4(1.f,0.f,0.f,1.f));
c->push_back(osg::Vec4(0.f,1.f,0.f,1.f));
c->push_back(osg::Vec4(0.f,0.f,1.f,1.f));
//定义法线
osg::ref_ptr<osg::Vec3Array> n = new osg::Vec3Array();
geom->setNormalArray(n);
geom->setNormalBinding(osg::Geometry::BIND_OVERALL);
n->push_back(osg::Vec3(0.f,-1.f,0.f));
//设置顶点关联方式
//PrimitiveSet类,这个类松散地封装了OpenGL的绘图基元,
//包括点(POINTS),线(LINES),多段线(LINE_STRIP),封闭线(LINE_LOOP),四边形(QUADS),多边形(POLYGON)等。
geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::POLYGON,0,3));
osg::ref_ptr<osg::Geode> geo = new osg::Geode();//几何组节点
geo->addDrawable(geom);
root->addChild(geo);




// the part of code help you understand how to using the ShapeDrawable class to play the object
osg::ref_ptr<osg::Geode> geo2 = new osg::Geode();
osg::ref_ptr<osg::ShapeDrawable> shape1 = new osg::ShapeDrawable(new osg::Box(root->getBound().center(),0.5,0.6,0.2));
shape1->setColor(osg::Vec4(0.1,0.3,0.7,0.1));
geo->addDrawable(shape1);



// this part of code may help you understand how to move the object
//
osg::ref_ptr<osg::Geode> node = new osg::Geode();
osg::ref_ptr<osg::ShapeDrawable> shape2 = new osg::ShapeDrawable(new osg::Sphere(root->getBound().center(),1.0));
shape1->setColor(osg::Vec4(0.8,0.5,0.2,1.0));
osg::ref_ptr<osg::MatrixTransform> tranf =new osg::MatrixTransform();
tranf->setMatrix(osg::Matrix::translate(-2,0,0));
node->addDrawable(shape2);
tranf->addChild(node);
root->addChild(tranf);


// this part of code show the object rotate in the World coordinate system
// the box size is same to the shape1
osg::ref_ptr<osg::Geode> node1 = new osg::Geode();
osg::ref_ptr<osg::ShapeDrawable> shape3 = new osg::ShapeDrawable(new osg::Box(root->getBound().center(),0.5,0.6,0.2));
shape1->setColor(osg::Vec4(0.5,0.1,0.8,1.0));
osg::ref_ptr<osg::MatrixTransform> tranf1 =new osg::MatrixTransform();
tranf1->setMatrix(osg::Matrix::rotate(1.0,osg::Vec3(1.0,0,0)));
node1->addDrawable(shape3);
tranf1->addChild(node1);
root->addChild(tranf1);


// this part of code help to realize the osg coordinate system 
// there many source in csdn you can read it 
// there are one important thing you need to be careful:whatever the change you did to the coor
//        you need to know the correspondence coordinate  
/*view.getCamera()->setProjectionMatrixAsPerspective(40.,1.,1.,100.);
osg::Matrix trans;
trans.makeTranslate(0.,0.,-12.);
double angle(0.0);
while (!view.done())
{
osg::Matrix rot;
rot.makeRotate(angle,osg::Vec3(1.0,0.,0.));
angle+=0.01;
view.getCamera()->setViewMatrix(rot*trans);
view.frame();
}*/


view.setSceneData(root);
view.realize();
view.run();
return 0;
}

/[code]


  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值