zoukankan      html  css  js  c++  java
  • osg::NodeVisitor中计算一个节点对应的世界变换矩阵、法向量、顶点坐标

    class MyNodeVisitor:public osg::NodeVisitor

    {

    pulic:

      MyNodeVisitor():osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN)

      {}

      void apply(osg::Geode& geode)

      {

        //计算当前geode节点对应的世界变换矩阵,用来计算geode中顶点对应的世界坐标

        osg::Matrix geodeMatrix=osg::computeLocalToWorld(getNodePath());

        

        unsigned int count=geode.getNumDrawables();

        for(unsigned int geomIdx=0; geomIdx<count; geomIdx++)

        {

          osg::Geometery *geometry = geode.getDrawable(geomIdx)->asGeometry();

          if(!geometry) continue;

          //顶点数据

          osg::Vec3Array* vertices=dynamic_cast<osg::Vec3Array*>(geometry->getVertexArray());

          //法向量

          osg::Vec3Array* normals=dynamic_cast<osg::Vec3Array*>(geometry->getNormalArray());

          //索引数组

          for(unsigned int primitiveIdx=0; primitiveIdx<geometry->getNumPrimitiveSets(); ++primitiveIdx)

          {

            osg::PrimitiveSet* ps=geometry->getPrimitiveSet(primitiveIdx);

            if(!ps) continue;

            switch(ps->getType())

            {

              case osg::PrimitiveSet::DrawElementsUShortPrimitiveType:

              {

                osg::DrawElementsUShort* deus=dynamic_cast<osg::DrawElementsUShort*>(ps);

                const unsigned int indexNum=deus->getNumIndices();

                switch(deus->getMode)

                {

                case osg::PrimitiveSet::TRIANGLES:

                //假设geometry->getNormalBinding()==osg::Geometry::BIND_PER_VERTEX)

                //即每一个顶点对应一个法向量

                for(unsigned int i=0; i<indexNum; i++)

                {

                  //顶点索引

                  unsigned int idx=deus->at(i);

                  //法向量。需要转换成世界坐标,需要乘以geodeMatrix的逆矩阵的转置,具体原因可以参考计算法向量那篇随笔

                  Vec3 normalWorld=normals->at(idx)*(geodeMatrix的逆矩阵的转置);

                  //顶点坐标

                  Vec3 vertexWorld=vertices->at(idx)*geodeMatrix;

                }

                break;

                }

              }

            }

          }

        }

      }

    }

  • 相关阅读:
    Sublime Text 2 实用快捷键[Mac OS X]
    SublimeText2 快捷键一览表
    sublime text 3 快捷键大全以及配置编译环境
    解决Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in
    PHP中include和require的区别详解
    Call to undefined function imagettftext()解决方法
    mysql单引号和双引号的用法
    php.ini 配置文件的深入解析
    《JavaScript设计模式与开发实践》—— 代理模式
    《JavaScript高级程序设计》—— DOM
  • 原文地址:https://www.cnblogs.com/coolbear/p/4056844.html
Copyright © 2011-2022 走看看