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

在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
(请您对文章做出评价)
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
编译运行 OpenSceneGraph-OpenSceneGraph-3.6.4 需要按照以下步骤进行操作: 1. 首先,确保你的计算机上已经安装了 CMake,C++ 编译器和 OpenGL 兼容的图形驱动程序。这些是编译和运行 OpenSceneGraph 所必需的工具和库。 2. 下载 OpenSceneGraph-OpenSceneGraph-3.6.4 的源代码,可以在官方网站或开源项目托管网站上找到。确保下载的版本正确,避免出错。 3. 解压源代码文件并进入解压后的目录。 4. 创建一个用于构建的构建目录,并进入该目录。例如:mkdir build && cd build。 5. 运行 CMake 命令来生成构建系统所需的文件和配置。命令可能类似于:cmake path/to/OpenSceneGraph-OpenSceneGraph-3.6.4。你可以使用其他参数和选项来自定义构建过程。 6. 确保 CMake 执行成功并生成了构建系统所需的文件。 7. 使用你的 C++ 编译器来构建 OpenSceneGraph。可以使用 make 命令,或者其他编译工具,根据你的操作系统和编译环境来选择。例如:make。 8. 等待编译完成,这可能需要一段时间,具体取决于你的计算机性能。 9. 构建完成后,检查是否有错误或警告信息。如果有,需要解决它们,并重新运行编译步骤。 10. 运行编译好的 OpenSceneGraph 可执行文件,这将启动 OpenSceneGraph 程序并运行示例或其他自定义的应用程序。 总之,编译和运行 OpenSceneGraph-OpenSceneGraph-3.6.4 需要先安装必需的工具和库,然后使用 CMake 生成构建系统所需的文件,再使用 C++ 编译器进行编译,最后运行生成的可执行文件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值