osg 矩阵相关

发布于:2024-10-09 ⋅ 阅读:(175) ⋅ 点赞:(0)

下面结果是一样的 

    osg::Matrix mtrixx;
    mtrixx.makeRotate(90 / 180.f * osg::PI, osg::Vec3(1, 0, 0));

    osg::Matrix mtrixx12 = osg::Matrix::rotate(90 / 180.f * osg::PI, 1, 0, 0);

备注: rotate或makerotate第一个参数是弧度,可以用  弧度值=osg::inDegrees(角度值)  得到弧度值,也可以用公式 : 角度值 / 180.f * osg::PI  来计算弧度值


下面结果是一样

    osg::Matrix m1;
    m1.makeTranslate(osg::Vec3(18, 12,3));

    osg::Matrix m2= osg::Matrix::translate(osg::Vec3(18, 12, 3));


下面结果是一样

    osg::Matrix mtrixxttt;
    mtrixxttt.makeScale(osg::Vec3(2, 3,3));

    osg::Matrix mtrixxppp = osg::Matrix::scale(osg::Vec3(2, 3, 3));

之所以一样是因为下面,




 下面两段代码是一样的,旋转90移动15,然后再转90然后再移动15

    osg::Matrix mtrixx;
    mtrixx.makeRotate(90 / 180.f * osg::PI, osg::Vec3(0, 1, 0));
    tr->setMatrix(mtrixx * tr->getMatrix());
    mtrixx.makeTranslate(osg::Vec3(15, 0, 0));
    tr->setMatrix(mtrixx * tr->getMatrix());
    mtrixx.makeRotate(90 / 180.f * osg::PI, osg::Vec3(0, 1, 0));
    tr->setMatrix(mtrixx * tr->getMatrix());
    mtrixx.makeTranslate(osg::Vec3(15, 0, 0));
    tr->setMatrix(mtrixx * tr->getMatrix());
    osg::Matrix mtrixx;
    tr->setMatrix(osg::Matrix::rotate(90 / 180.f * osg::PI, 0, 1, 0) * tr->getMatrix());
    tr->setMatrix(osg::Matrix::translate(osg::Vec3(15, 0, 0)) * tr->getMatrix());
    tr->setMatrix(osg::Matrix::rotate(90 / 180.f * osg::PI, 0, 1, 0) * tr->getMatrix());
    tr->setMatrix(osg::Matrix::translate(osg::Vec3(15, 0, 0)) * tr->getMatrix());




牛的局部坐标是这样的 



下面做一个实验 ,先构建这个关系,然后只比较位置

然后在通过,下面值比较一下两者的区别

m_rpMtPosition->setMatrix(osg::Matrix::translate( osg::Vec3d(xx,xx,xx)));

m_rpPATposition->setPosition( osg::Vec3d(xx,xx,xx));

#include<osgViewer/Viewer>
#include<osgDB/ReadFile>
#include <osg/MatrixTransform>
#include <osg/PositionAttitudeTransform>
int main(int argc, char** argv)
{

    osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer;
    osg::ref_ptr<osg::Group> root = new osg::Group;

    osg::ref_ptr<osg::Node> cow = osgDB::readNodeFile("cow.osg");
    osg::ref_ptr<osg::MatrixTransform> mt = new osg::MatrixTransform;
    osg::ref_ptr<osg::PositionAttitudeTransform> pt = new osg::PositionAttitudeTransform;
    mt->setMatrix(osg::Matrix::translate(15, 0, 0)); //先移动15
    mt->setMatrix(osg::Matrix::translate(15, 0, 0)*mt->getMatrix()); //在现有基础上再移动15
    mt->setMatrix(osg::Matrix::rotate(osg::PI/2,0, 1, 0) * mt->getMatrix()); //在现有基础上再移动15
    mt->setMatrix(osg::Matrix::translate(15, 0, 0) * mt->getMatrix()); //在现有基础上往x轴再移动15
    pt->setPosition(osg::Vec3(15,0,0));

    mt->addChild(cow);
    pt->addChild(cow);

    root->addChild(mt);
    root->addChild(pt);
    root->addChild(cow);
    viewer->setSceneData(root.get());
    viewer->realize();
    viewer->run();

    return 0;
}






  下面说明  setMatrix  相对于父节点移动

#include<osgViewer/Viewer>
#include<osgDB/ReadFile>
#include <osg/MatrixTransform>
#include <osg/PositionAttitudeTransform>
int main(int argc, char** argv)
{

    osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer;
    osg::ref_ptr<osg::Group> root = new osg::Group;

    osg::ref_ptr<osg::Node> cow = osgDB::readNodeFile("cow.osg");
    osg::ref_ptr<osg::MatrixTransform> mt1 = new osg::MatrixTransform;
    osg::ref_ptr<osg::MatrixTransform> mt2 = new osg::MatrixTransform;
    mt1->setMatrix(osg::Matrix::translate(-30, 0, 0)); //mt1先左移30
    mt2->setMatrix(osg::Matrix::translate(30, 0, 0)); //mt2再往右移动30

    mt2->addChild(cow);
    mt1->addChild(mt2);

    root->addChild(mt1);
    root->addChild(cow);
    viewer->setSceneData(root.get());
    viewer->realize();
    viewer->run();

    return 0;
}


网站公告

今日签到

点亮在社区的每一天
去签到