osg 矩阵转换后,获取矩阵中模型的位置



#include "stdafx.h"
#include <osg/ComputeBoundsVisitor>
#include <osg/ShapeDrawable>
#include <osg/AnimationPath>
#include <osg/MatrixTransform>
#include <osg/PolygonMode>
#include <osgDB/ReadFile>
#include <osgViewer/Viewer>
#include <osg/LineWidth>


osg::Node* createAxis(double length)//建立坐标系
{
osg::Geode* geode = new osg::Geode();


osg::Geometry* linesGeom = new osg::Geometry();
osg::Geometry* linesGeom1 = new osg::Geometry();
osg::Geometry* linesGeom2 = new osg::Geometry();
osg::Vec3dArray* vertices = new osg::Vec3dArray;
linesGeom->setVertexArray(vertices);
linesGeom1->setVertexArray(vertices);
linesGeom2->setVertexArray(vertices);
vertices->push_back(osg::Vec3d(0,0,0));
vertices->push_back(osg::Vec3d(length,0,0));
vertices->push_back(osg::Vec3d(0,0,0));
vertices->push_back(osg::Vec3d(0,length,0));
vertices->push_back(osg::Vec3d(0,0,0));
vertices->push_back(osg::Vec3d(0,0,length));
osg::Vec4Array* colors = new osg::Vec4Array;
colors->push_back(osg::Vec4(1.0,0.0,0.0,1));




osg::Vec4Array* colors1 = new osg::Vec4Array;
colors1->push_back(osg::Vec4(0.0,1.0,0.0,1));
osg::Vec4Array* colors2 = new osg::Vec4Array;
colors2->push_back(osg::Vec4(0.0,0.0,1.0,1));
linesGeom->setColorArray(colors,osg::Array::BIND_OVERALL);
linesGeom1->setColorArray(colors1,osg::Array::BIND_OVERALL);
linesGeom2->setColorArray(colors2,osg::Array::BIND_OVERALL);
//linesGeom->setColorBinding(osg::Geometry::BIND_OVERALL);


// set the normal in the same way color.
osg::Vec3Array* normals = new osg::Vec3Array;
normals->push_back(osg::Vec3(0.0f,-1.0f,0.0f));
normals->push_back(osg::Vec3(0.0f,1.0f,0.0f));
linesGeom->setNormalArray(normals,osg::Array::BIND_OVERALL);
linesGeom1->setNormalArray(normals,osg::Array::BIND_OVERALL);
linesGeom2->setNormalArray(normals,osg::Array::BIND_OVERALL);
//linesGeom->setNormalBinding(osg::Geometry::BIND_OVERALL);




// This time we simply use primitive, and hardwire the number of coords to use 
// since we know up front,
linesGeom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINES,0,2));
linesGeom1->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINES,2,2));
linesGeom2->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINES,4,2));
//linesGeom->setUpdateCallback(new MyTrailCallback())
geode->getOrCreateStateSet()->setMode(GL_LIGHTING,osg::StateAttribute::OFF);
geode->getOrCreateStateSet()->setMode(GL_COLOR_MATERIAL,osg::StateAttribute::OFF);
geode->getOrCreateStateSet()->setAttribute( new osg::LineWidth(5.0f) );
geode->addDrawable(linesGeom);
geode->addDrawable(linesGeom1);
geode->addDrawable(linesGeom2);
return geode;
}


int _tmain(int argc, _TCHAR* argv[])
{
osg::ref_ptr<osg::MatrixTransform> cessna =
new osg::MatrixTransform;

cessna->setMatrix( osg::Matrix::translate(50.0f, 50.0f, 50.0f) );
//cessna->addChild(createAxis(100));


osg::ref_ptr<osg::MatrixTransform> cessnaRotate =
new osg::MatrixTransform;


cessnaRotate->addChild(osgDB::readNodeFile("cessna.osgt.0,0,90.rot"));
cessnaRotate->addChild(createAxis(100));
cessnaRotate->setMatrix( osg::Matrix::rotate(osg::DegreesToRadians(30.f),osg::Vec3(1,0,0)) );
cessna->addChild(cessnaRotate);
//cessna->setUpdateCallback(new BoundingBoxCallback);
osg::ref_ptr<osg::MatrixTransform> dumptruck =
new osg::MatrixTransform;
dumptruck->addChild( osgDB::readNodeFile("dumptruck.osgt") );
dumptruck->setMatrix( osg::Matrix::translate(100.0f, 100.0f, 100.0f) );


osg::ref_ptr<osg::Group> root = new osg::Group;


osg::ref_ptr<osg::MatrixTransform> models =
new osg::MatrixTransform;
models->addChild( cessna.get() );
models->addChild( dumptruck.get() );
models->addChild( createAxis(200) );


root->addChild( models.get() );




osg::Geometry* linesGeom = new osg::Geometry();
osg::Vec3dArray* vertices = new osg::Vec3dArray;
linesGeom->setVertexArray(vertices);
linesGeom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::TRIANGLE_STRIP,0,3));
vertices->push_back(osg::Vec3d(0,0,0));

//geode中创建一条直线,连接dumptruck中的模型和cessnaRotate中的模型
osg::Geode* geode = new osg::Geode();
geode->getOrCreateStateSet()->setMode(GL_LIGHTING,osg::StateAttribute::OFF);
geode->getOrCreateStateSet()->setMode(GL_COLOR_MATERIAL,osg::StateAttribute::OFF);
geode->getOrCreateStateSet()->setAttribute( new osg::LineWidth(5.0f) );
geode->addDrawable(linesGeom);
cessnaRotate->addChild(geode);

//先把dumptruck矩阵转换到cessna的矩阵下。dumptruck的矩阵乘以cessna的逆矩阵得到dumptruck在cessna中的局部矩阵pos1

osg::Matrix pos1 = dumptruck->getMatrix()*cessna->getInverseMatrix();

//再把dumptruck矩阵转换到cessnaRotate的矩阵下。pos1再乘以 cessnaRotate的逆矩阵得到dumptruck在cessnaRotate中的局部矩阵pos2

osg::Matrix pos2 = pos1 * cessnaRotate->getInverseMatrix();
//vertices->push_back(pos1.getTrans());
vertices->push_back(pos2.getTrans());

//通过下面的方式也可以获取dumptruck在cessnaRotate中的局部矩阵

osg::Matrix mat = dumptruck->getMatrix()*osg::computeWorldToLocal(cessna->getParentalNodePaths()[0] );


osgViewer::Viewer viewer;
viewer.setSceneData( root.get() );


return viewer.run();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值