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();
}
#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();
}
相关文章推荐
- Opengl_es模型矩阵位置:glFrustumx与glTranslatef参数的相互影响--立方体旋转特效
- asp.net 获取指定表中指定位置的行对象,将行数组转换为表结构
- Camshift原理 camshift利用目标的颜色直方图模型将图像转换为颜色概率分布图,初始化一个搜索窗的大小和位置,并根据上一帧得到的结果自适应调整搜索窗口的位置和大小,从而定位出当前图像中目标的
- osg求交,模型矩阵
- [D3D9] 世界坐标转换矩阵的获取:旋转矩阵、平移矩阵、缩放矩阵
- OSG获取模型XYZ范围并生成最小包围盒
- winform中坐标系转换的问题,获取某点在屏幕中的绝对位置等
- sas转java(1)截取子矩阵、unique、获取矩阵的行列数、创建特殊矩阵、求矩阵中的最大最小值、横纵合并矩阵、将矩阵转换为0,1形式、转置求逆
- 小白学opengl之 获取模型视图矩阵和投影矩阵
- 5*5矩阵的检索和位置转换
- java:图像(BufferedImage)色彩空间转换(灰度)暨获取图像矩阵数据byte[](sRGB/gray)
- Opengl_es模型矩阵位置:glFrustumx与glTranslatef参数的相互影响--立方体旋转特效
- RevitAPI: 如何获取RevitLinkInstance链接模型的位置?
- OSG获取鼠标点选事件的位置
- Cesium 获取鼠标当前位置的模型高度,地形高度,OSGB高度,及其经纬度。
- 设置节点属性;setInterval和setTimeout;转换为字符串;获取字符位置方法;检测字符串长度
- iOS开发 获取控件在屏幕上的位置(坐标系转换)
- OSG中获取任一时刻相机的位置
- opencv获取矩阵某位置元素值的几种简便方法
- glsl着色语言和osg的交互及模型矩阵的传递