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;

                }

              }

            }

          }

        }

      }

    }

  • 相关阅读:
    C#函数式编程
    三种观察者模式的C#实现
    使用C#设计Fluent Interface
    02.Python网络爬虫第二弹《http和https协议》
    03.Python网络爬虫第一弹《Python网络爬虫相关基础概念》
    第七章:Python基础のXML操作和面向对象(一)
    第五章:Python基础の生成器、迭代器、序列化和虚拟环境的应用
    第六章:Python基础の反射与常用模块解密
    第四章:Python基础の快速认识內置函数和操作实战
    第三章:Python基础の函数和文件操作实战
  • 原文地址:https://www.cnblogs.com/coolbear/p/4056844.html
Copyright © 2011-2022 走看看