您的位置:首页 > 其它

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

2014-11-26 16:41 1036 查看
#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();

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐