OpenSceneGraph 笔记--如何导出三角形数据

转载:http://blog.csdn.net/pizi0475/article/details/5384389

在OpenSceneGraph开发中,为了方便会经常使用到一些不是三角形片的数据,比如四边形等数据。例如画一个管子用四边形带比用三角形片好计算得多。比如现在我们要画一个由两个平面组成的面,我可以这样做:

    osg::Geode* geode=new osg::Geode;
    osg::Geometry* polyGeom = new osg::Geometry;
    osg::Vec3 myCoords[]=
    {
        osg::Vec3(0,1,0),
        osg::Vec3(0,0,0),
        osg::Vec3(1,1,0),
        osg::Vec3(1,0,0),
        osg::Vec3(2,1,0),
        osg::Vec3(2,0,0)
    };

int numCoords = sizeof(myCoords)/sizeof(osg::Vec3);
    osg::Vec3Array* vertices = new osg::Vec3Array(numCoords,myCoords);
    polyGeom->setVertexArray(vertices);
    polyGeom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUAD_STRIP,0,numCoords));
    geode->addDrawable(polyGeom);

这样就用6个点,用OpenGL提供的QUAD_STRIP方式画出了两个平面。
但是如果要把这个平面用于碰撞检测等技术,那么就需要把这六个点所表示的四边形带转换成三角形片才行。这些三角形定点如下:
0 1 0
0 0 0
1 1 0

0 0 0
1 0 0
1 1 0

1 1 0
1 0 0
2 1 0

1 0 0
2 0 0
2 1 0
可以看出两个平面由4个三角形组成,而且都是逆时针排列(朝向一致)。
以前我自己做过转换,但是感觉很麻烦。OpenSceneGraph的Example osggeometry中提供了一个printTriangles函数,它可以打印出一个drawable所有的三角形片,不管最初的数据结构如何:

struct NormalPrint
{
    void operator() (const osg::Vec3& v1,const osg::Vec3& v2,const osg::Vec3& v3, bool) const 
    {
        osg::Vec3 normal = (v2-v1)^(v3-v2);
        normal.normalize();
        std::cout << "/t("<<v1<<") ("<<v2<<") ("<<v3<<") "<<") normal ("<<normal<<")"<<std::endl;
    }
};

// decompose Drawable primtives into triangles, print out these triangles and computed normals.
void printTriangles(const std::string& name, osg::Drawable& drawable)
{
    std::cout<<name<<std::endl;
    
    osg::TriangleFunctor<NormalPrint> tf;
    drawable.accept(tf);
 
    std::cout<<std::endl;
}

核心的思想就是利用osg::TriangleFunctor这个模版。这个模版会让你重载()运算符,然后让Drawable去visit它。在这个过程中,所有原始的数据(不管是三角形片的,还是四边形的)都转换成了三角形片数据。
那么如何把三角形数据导出哪?只需要修改一下借助这个思路,将NormalPrint修改成我们需要的就对了。

struct GetVertex
{
    void operator() (const osg::Vec3& v1,const osg::Vec3& v2,const osg::Vec3& v3, bool) const 
    {
        vertexList->push_back(v1);
        vertexList->push_back(v2);
        vertexList->push_back(v3);
    }

osg::Vec3Array* vertexList;
    
};

void getTriangles(osg::Drawable& drawable)
{
    osg::TriangleFunctor<GetVertex> tf;
    tf.vertexList=new osg::Vec3Array;

drawable.accept(tf);

for(osg::Vec3Array::iterator itr=tf.vertexList->begin();
        itr!=tf.vertexList->end();
        itr++)
    {
        osg::Vec3 vertex=*itr;
        std::cout<<vertex<<std::endl;
    }

std::cout<<std::endl;
}

以下是完整的示例文件:

// PrimitiveSet.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>

struct GetVertex
{
    void operator() (const osg::Vec3& v1,const osg::Vec3& v2,const osg::Vec3& v3, bool) const 
    {
        vertexList->push_back(v1);
        vertexList->push_back(v2);
        vertexList->push_back(v3);
    }

osg::Vec3Array* vertexList;
    
};

void getTriangles(osg::Drawable& drawable)
{
    osg::TriangleFunctor<GetVertex> tf;
    tf.vertexList=new osg::Vec3Array;

drawable.accept(tf);

for(osg::Vec3Array::iterator itr=tf.vertexList->begin();
        itr!=tf.vertexList->end();
        itr++)
    {
        osg::Vec3 vertex=*itr;
        std::cout<<vertex<<std::endl;
    }

std::cout<<std::endl;
}

osg::Node* createGeode()
{
    osg::Geode* geode=new osg::Geode;
    osg::Geometry* polyGeom = new osg::Geometry;
    osg::Vec3 myCoords[]=
    {
        osg::Vec3(0,1,0),
        osg::Vec3(0,0,0),
        osg::Vec3(1,1,0),
        osg::Vec3(1,0,0),
        osg::Vec3(2,1,0),
        osg::Vec3(2,0,0)
    };

int numCoords = sizeof(myCoords)/sizeof(osg::Vec3);
    osg::Vec3Array* vertices = new osg::Vec3Array(numCoords,myCoords);
    polyGeom->setVertexArray(vertices);
    polyGeom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUAD_STRIP,0,numCoords));
    geode->addDrawable(polyGeom);
    getTriangles(*polyGeom);
    return geode;
}

int _tmain(int argc, _TCHAR* argv[])
{
    //Set up viewer
    osgViewer::Viewer viewer;
    osg::ref_ptr<osg::GraphicsContext::Traits> traits=new osg::GraphicsContext::Traits;
    traits->x=200;
    traits->y=200;
    traits->width=800;
    traits->height=600;
    traits->windowDecoration=true;
    traits->doubleBuffer=true;
    traits->sharedContext=0;
    
    osg::ref_ptr<osg::GraphicsContext> gc=osg::GraphicsContext::createGraphicsContext(traits.get());
    osg::ref_ptr<osg::Camera> camera=new osg::Camera;
    //osg::Camera camera=new osg::Camera;
    camera->setGraphicsContext(gc.get());
    camera->setViewport(new osg::Viewport(0,0,traits->width,traits->height));
    camera->setDrawBuffer(GL_BACK);
    camera->setReadBuffer(GL_BACK);
    osgGA::TrackballManipulator* tm=new osgGA::TrackballManipulator;
    
    viewer.setCameraManipulator(tm);
    
    viewer.addSlave(camera.get());

//Set up root node
    osg::ref_ptr<osg::Group> root=new osg::Group;

root->addChild(createGeode());

//Start show!
    viewer.setSceneData(root.get());
    viewer.realize();

while(!viewer.done())
    {
        viewer.frame();
    }
}

0
0

OpenSceneGraph 笔记--如何导出三角形数据的更多相关文章

  1. matlab学习笔记4--导入和导出Internet数据

    一起来学matlab-matlab学习笔记4 数据导入和导出_4 导入和导出Internet数据 觉得有用的话,欢迎一起讨论相互学习~Follow Me 参考书籍 <matlab 程序设计与综合 ...

  2. matlab学习笔记4--导入和导出电子数据表

    一起来学matlab-matlab学习笔记4 数据导入和导出_3 导入和导出电子数据表 觉得有用的话,欢迎一起讨论相互学习~Follow Me 参考书籍 <matlab 程序设计与综合应用> ...

  3. R学习笔记(4): 使用外部数据

    来源于:R学习笔记(4): 使用外部数据 博客:心内求法 鉴于内存的非持久性和容量限制,一个有效的数据处理工具必须能够使用外部数据:能够从外部获取大量的数据,也能够将处理结果保存.R中提供了一系列的函 ...

  4. SpringBoot图文教程10—模板导出|百万数据Excel导出|图片导出「easypoi」

    有天上飞的概念,就要有落地的实现 概念十遍不如代码一遍,朋友,希望你把文中所有的代码案例都敲一遍 先赞后看,养成习惯 SpringBoot 图文教程系列文章目录 SpringBoot图文教程1「概念+ ...

  5. 【Telerik】<telerik:RadComboBox>导出列表数据

    近来在做项目,做到导出功能.使用<telerik:RadComboBox>的下拉框来实现导出部分或导出所有数据的功能.

  6. 使用BCP导出导入数据

    bcp 实用工具可以在 Microsoft SQL Server 实例和用户指定格式的数据文件间大容量复制数据. 使用 bcp 实用工具可以将大量新行导入 SQL Server 表,或将表数据导出到数 ...

  7. mysqldump导出部分数据的方法: 加入--where参数

    mysqldump导出部分数据的方法: 加入--where参数 mysqldump -u用户名 -p密码 数据库名 表名 --where="筛选条件" > 导出文件路径 my ...

  8. linux下导入、导出mysql数据库命令 下载文件到本地

    一.下载到本地 yum install lrzsz sz filename  下载 rz filename  上传   linux下导入.导出mysql数据库命令 一.导出数据库用mysqldump命 ...

  9. phpmyadmin导入导出大数据文件的办法

    在phpmyadmin的使用中,经常需要进行导入导出数据库的操作. 但是在导入导出大型数据库文件的时候经常会只是部分导出或者部分导入. 或者是导入导出不成功. 原因就是服务器和php.mysql限制了 ...

随机推荐

  1. 【Network】OVS、VXLAN/GRE、OVN等 实现 Docker/Kubernetes 网络的多租户隔离

    多租户隔离 DragonFlow与OVN | SDNLAB | 专注网络创新技术 Neutron社区每周记(6.25~7.6)| OVS将加入Linux基金会?OVN或抛弃ovsdb? | Unite ...

  2. 解除sql server数据库占用

    use master go ) set @dbName='dbname' --数据库名 ) declare cur_lock cursor for SELECT DISTINCT request_se ...

  3. [转]加速Android Studio/Gradle构建

    加速Android Studio/Gradle构建 android android studio gradle   已经使用Android Studio进行开发超过一年,随着项目的增大,依赖库的增多, ...

  4. 【转】Caffe初试(九)solver及其设置

    solver算是caffe的核心的核心,它协调着整个模型的运作.caffe程序运行必带的一个参数就是solver配置文件.运行代码一般为 #caffe train --solver=*_solver. ...

  5. 51node1264(判断线段相交)

    题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1264 题意:中文题诶- 思路:对于直线a1a2, b1b2, ...

  6. 【转】Redis入门

    Redis是一个开源,先进的key-value存储,并用于构建高性能,可扩展的Web应用程序的完美解决方案. Redis从它的许多竞争继承来的三个主要特点: Redis数据库完全在内存中,使用磁盘仅用 ...

  7. design包 TabLayout使用

    类似"网易新闻"UI设计就很好,顶部是导航,下面是各个页面.如图 这种效果使用design包中的TabLayout可以轻松的实现.   一.分析TabLayout 常见 UI 上图 ...

  8. Unity Cookie

    1   在Unity里面,选择脚本单击左键打开 Sync Mono Development  这样就可以打开整个工程的脚本文件 进而才能在脚本中继续进行切换      Mesh    MeshFilt ...

  9. [Spring] Spring配置文件中特殊字符的规定

    今天查找一个错误,发现在xml里面不能包含特殊字符:&,特来总结一下: XML中共有5个特殊的字符,分别是:&<>“’.如果配置文件中的注入值包括这些特殊字符,就需要进行特 ...

  10. (转) Wp7 list 中列表项多样化的解决方案-Custom DataTemplateSelector

    本文转自: http://www.cnblogs.com/sonyye/archive/2012/03/03/2378825.html 在这篇文章中,我将解释如何在Windows Phone 7中创建 ...