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;

                }

              }

            }

          }

        }

      }

    }

  • 相关阅读:
    4.23计算机网络
    CF436F Banners
    CF1467C Three Bags
    LG P3247 [HNOI2016]最小公倍数
    LG P5473 [NOI2019] I 君的探险
    LG P3261 [JLOI2015]城池攻占
    LG P4149 [IOI2011]Race
    LG P3181 [HAOI2016]找相同字符
    SP7258 SUBLEX
    SP1811 LCS
  • 原文地址:https://www.cnblogs.com/coolbear/p/4056844.html
Copyright © 2011-2022 走看看