struct Subset
{
std::vector<float> vertexs;//位置
std::vector<float> normals;//法向
std::vector<float> texCoords;//纹理
std::vector<unsigned int> indices;//索引下标
std::vector<unsigned int> faceMtrls;//面材质索引
}; class GetSimplifySTLDataVisitor : public osg::NodeVisitor
{
public:
GetSimplifySTLDataVisitor(Subset &s):dstSubset(s),
osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN)
{
} virtual void apply(osg::Geode& geode)
{
unsigned int count = geode.getNumDrawables();
for ( unsigned int i = ; i < count; i++ )
{
osg::Geometry *geometry = geode.getDrawable( i )->asGeometry();
if ( !geometry )
continue;
// 顶点数据
osg::Vec3Array* vertices = dynamic_cast<osg::Vec3Array*>(geometry->getVertexArray());
int vertexlNum = vertices->size();
for ( int i=; i<vertexlNum; i++) {
dstSubset.vertexs.push_back( vertices->at(i).x() );
dstSubset.vertexs.push_back( vertices->at(i).y() );
dstSubset.vertexs.push_back( vertices->at(i).z() );
}
// 法向量
osg::Vec3Array* normals = dynamic_cast<osg::Vec3Array*>(geometry->getNormalArray());
int normalNum = normals->size();
for (int i=; i<normalNum; i++) {
dstSubset.normals.push_back( normals->at(i).x() );
dstSubset.normals.push_back( normals->at(i).y() );
dstSubset.normals.push_back( normals->at(i).z() );
}
normalBindKinds.insert(geometry->getNormalBinding());
if(osg::Geometry::BIND_PER_VERTEX != geometry->getNormalBinding() )
{
std::string msg="未处理的法向量绑定方式";
MessageBox(, LPCTSTR(msg.c_str()), "警告", MB_OK);
}
// 索引数组
for ( unsigned int i = ; i < geometry->getNumPrimitiveSets(); ++i )
{
osg::PrimitiveSet* ps = geometry->getPrimitiveSet( i );
if ( !ps ) continue;
pts.insert(ps->getType());
modes.insert(ps->getMode());
switch( ps->getType() )
{
case osg::PrimitiveSet::DrawElementsUIntPrimitiveType :
{
osg::DrawElementsUInt* deui = dynamic_cast<osg::DrawElementsUInt*>(ps);
const unsigned indexNum = deui->getNumIndices();
switch( deui->getMode() )
{
case osg::PrimitiveSet::TRIANGLES :
{
for (unsigned int i=; i<indexNum; i++)
{
dstSubset.indices.push_back( deui->at(i) );
}
break;
}
default:
{
std::string msg="未处理的绘制模式";
MessageBox(, LPCTSTR(msg.c_str()), "警告", MB_OK);
}
}
break;
}
case osg::PrimitiveSet::DrawElementsUShortPrimitiveType :
{
osg::DrawElementsUShort* de = dynamic_cast<osg::DrawElementsUShort*>(ps);
const unsigned indexNum = de->getNumIndices();
switch( de->getMode() )
{
case osg::PrimitiveSet::TRIANGLES :
{
for (unsigned int i=; i<indexNum; i++)
{
dstSubset.indices.push_back( de->at(i) );
}
break;
}
case osg::PrimitiveSet::TRIANGLE_STRIP :
{
for (unsigned int i=; i<indexNum-; i++)
{
//此处索引为何与基数偶数有关,可百度GL_TRIANGLE_STRIP
if (i%==)
{
dstSubset.indices.push_back( de->at(i) );
dstSubset.indices.push_back( de->at(i+) );
dstSubset.indices.push_back( de->at(i+) );
}
else
{
dstSubset.indices.push_back( de->at(i) );
dstSubset.indices.push_back( de->at(i+) );
dstSubset.indices.push_back( de->at(i+) );
}
}
break;
}
default:
{
std::string msg="未处理的绘制模式";
MessageBox(, LPCTSTR(msg.c_str()), "警告", MB_OK);
}
}
break;
}
case osg::PrimitiveSet::DrawArraysPrimitiveType :
{
osg::DrawArrays* da = dynamic_cast<osg::DrawArrays*>(ps);
int first=da->getFirst();
int count=da->getCount();
switch( da->getMode() )
{
case osg::PrimitiveSet::TRIANGLES :
{
for ( int i=first; i<first+count; i++) {
dstSubset.indices.push_back( i );
}
break;
}
case osg::PrimitiveSet::TRIANGLE_STRIP :
{
for ( int i=first; i<first+count-; i++) {
if(i%==)
{
dstSubset.indices.push_back( i );
dstSubset.indices.push_back( i+ );
dstSubset.indices.push_back( i+ );
}
else
{
dstSubset.indices.push_back( i );
dstSubset.indices.push_back( i+ );
dstSubset.indices.push_back( i+ );
}
}
break;
}
default:
{
std::string msg="未处理的绘制模式";
MessageBox(, LPCTSTR(msg.c_str()), "警告", MB_OK);
}
}
break;
}
default:
{
std::string msg="未处理的图元类型";
MessageBox(, LPCTSTR(msg.c_str()), "警告", MB_OK);
}
}
}
}
}
private:
Subset &dstSubset;
//以下三个数组用来测试
std::set<osg::Geometry::AttributeBinding> normalBindKinds;
std::set<osg::PrimitiveSet::Type> pts;
std::set<GLenum> modes;
};

NodeVisitor的使用-遍历Geode节点下的Geometry并获取顶点、法向量等数据的更多相关文章

  1. NodeVisitor的使用-遍历Geode节点并在它与父节点之间添加一个LOD节点

    #include <osg\NodeVisitor>#include <osg\MatrixTransform>#include <osg\PagedLOD>#in ...

  2. PHP遍历文件夹下的文件和获取到input name的值

    <?php$dir = dirname(__FILE__); //要遍历的目录名字 ->当前文件所在的文件夹//$dir='D:\PHP\wamp\www\admin\hosts\admi ...

  3. Extjs4.x Ext.tree.Panel 遍历当前节点下的所有子节点

    Ext.define('WMS.controller.Org', { extend: 'Ext.app.Controller', stores: ['OrgUser', 'OrgTree'], mod ...

  4. PAT树_层序遍历叶节点、中序建树后序输出、AVL树的根、二叉树路径存在性判定、奇妙的完全二叉搜索树、最小堆路径、文件路由

    03-树1. List Leaves (25) Given a tree, you are supposed to list all the leaves in the order of top do ...

  5. VBS获取Ini配置文件一个节点下的所有字段的值

    ''* 功能:使用VBS读取ini文件中指定节点下的所有值'* 输入参数:inipath :ini文件的地址'*           initypes :ini文件中包含在"["和 ...

  6. 遍历树节点(多层)的方法(java)

    前序遍历,后序遍历,广度遍历,深度遍历,遍历一级节点.以及按钮如何响应点击事件. import java.awt.*; import java.awt.event.*; import java.uti ...

  7. asp.net 遍历文件夹下全部子文件夹并绑定到gridview上

    遍历文件夹下所有子文件夹,并且遍历配置文件某一节点中所有key,value并且绑定到GridView上 Helper app_Helper = new Helper(); DataSet ds = n ...

  8. 记录JS如何使用广度遍历找到节点的所有父节点

    我们在实际的工作业务场景中经常遇到这样的场景,求取树数据中某个节点的父亲节点以及所有的父亲节点,这样的场景下不建议使用深度遍历,使用广度遍历可以更快找到. 1.案例解说 比如树的长相是这样的: 树的数 ...

  9. C#遍历文件夹下所有文件

    FolderForm.cs的代码如下: using System; using System.Collections.Generic; using System.Diagnostics; using ...

随机推荐

  1. Javascript高级程序设计——在HTML中使用Javascript

    <script>元素 向HTML页面中插入Javascript的主要方法,就是使用<script>元素,<script>元素有六个属性: async:可选.表示应该 ...

  2. 阻止点击<a>标签链接跳转

      我们常用的在a标签中有点击事件(<a href="地址">链接</a>),其中“href”参数只要不为空,点击该链接时,页面会自动跳转:如果指定的“hr ...

  3. ducument.ready不生效的问题 ruby on rails

    rails web app页面之间的跳转的时候使用ducument.ready只有在再次加载的时候才生效, 因为rails用了turbolinks, https://github.com/turbol ...

  4. leetcode 86. Partition List

    Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...

  5. jQuery中的checkbox问题

    一开始的代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...

  6. Python验证Url地址的正则表达式

    如下是django中做url验证的正则表达式: regex = re.compile( r'^(?:http|ftp)s?://' # http:// or https:// r'(?:(?:[A-Z ...

  7. [POJ1328]Radar Installation

    [POJ1328]Radar Installation 试题描述 Assume the coasting is an infinite straight line. Land is in one si ...

  8. (转)listview中常见难题总结

    原文地址:http://blog.csdn.net/cherry609195946/article/details/8844224 1. PopWindow中listview的item获取不到点击事件 ...

  9. RouterOS首次打开网页强制跳转

    网上极少有关于RouterOS的第一次打开网页强制跳转主页的方法,大多数都方法是将浏览某个域名的IP地址跳转到自己的主页,这种方法有时会失效.还有一种方法就是当用户用80端口连接时,抓取源地址到地址列 ...

  10. tcp 三次握手