Hi Dusten -- The short answer (probably) is that you should look at
osg::TriangleFunctor.

The longer answer:

I'm currently developing an open source library containing utilities for
integrating OSG with Bullet. Here is the code I use to create triangle
meshes. The code for ComputeTriMeshVisitor is attached.

 
btTriangleMeshShape* osgBullet::btTriMeshCollisionShapeFromOSG( osg::Node*
node )
{
  ComputeTriMeshVisitor visitor;
  node->accept( visitor );
  osg::Vec3Array* vertices = visitor.getTriMesh();
  if( vertices->size() < 3 )
  {
    osg::notify( osg::WARN ) << "osgBullet::btTriMeshCollisionShapeFromOSG,
no triangles found" << std::endl;
    return( NULL );
  }

  btTriangleMesh* mesh = new btTriangleMesh;
  for( size_t i = 0; i + 3 < vertices->size(); i += 3 )
  {
    osg::Vec3& p1 = ( *vertices )[ i ];
    osg::Vec3& p2 = ( *vertices )[ i + 1 ];
    osg::Vec3& p3 = ( *vertices )[ i + 2 ];
    mesh->addTriangle( btVector3( p1.x(), p1.y(), p1.z() ),
        btVector3( p2.x(), p2.y(), p2.z() ),
        btVector3( p3.x(), p3.y(), p3.z() ) );
  }

  btBvhTriangleMeshShape* meshShape = new btBvhTriangleMeshShape( mesh, true
);
  return( meshShape );
}


I hope this is helpful. I'll post more info here when I make this code
publically available.

Paul Martz
Skew Matrix Software LLC
http://www.skew-matrix.com
+1 303 859 9466
/*
 *
 * Copyright (c) 2008 Blue Newt Software LLC and Skew Matrix Software LLC.
 * All rights reserved.
 *
 */

#ifndef OSGBULLET_COMPUTETRIMESHVISITOR
#define OSGBULLET_COMPUTETRIMESHVISITOR    1

#include <osg/Array>
#include <osg/NodeVisitor>

namespace osgBullet {

/* TBD Consider using OSG localtoworld method instead of keeping a matrix 
stack. */
class ComputeTriMeshVisitor
    : public osg::NodeVisitor
{
public:

    ComputeTriMeshVisitor( osg::NodeVisitor::TraversalMode traversalMode = 
TRAVERSE_ALL_CHILDREN );

    virtual void reset();


    osg::Vec3Array * getTriMesh()
    {
        return( mesh.get() );
    }

    void apply( osg::Node & node );


    void apply( osg::Transform & transform );


    void apply( osg::Geode & geode );


    inline void pushMatrix( osg::Matrix & matrix )
    {
        stack.push_back( matrix );
    }

    inline void popMatrix()
    {
        stack.pop_back();
    }

    void applyDrawable( osg::Drawable * drawable );

protected:
    typedef std::vector< osg::Matrix >   MatrixStack;

    MatrixStack stack;
    osg::ref_ptr< osg::Vec3Array > mesh;
};

}

#endif
/*
 *
 * Copyright (c) 2008 Blue Newt Software LLC and Skew Matrix Software LLC.
 * All rights reserved.
 *
 */

#include <osg/Transform>
#include <osg/Drawable>
#include <osg/Geode>
#include <osg/PrimitiveSet>
#include <osg/TriangleFunctor>

#include <osgBullet/ComputeTriMeshVisitor.h>

#include <iostream>

using namespace osgBullet;
using namespace osg;

struct ComputeTriMeshFunc
{
    ComputeTriMeshFunc()
    {
        vertices = new osg::Vec3Array;

        vertices->clear();
    }

    void inline operator()( const osg::Vec3 v1, const osg::Vec3 v2, const 
osg::Vec3 v3, bool _temp )
    {
        vertices->push_back( v1 );
        vertices->push_back( v2 );
        vertices->push_back( v3 );
    }

    osg::ref_ptr< osg::Vec3Array > vertices;
};

ComputeTriMeshVisitor::ComputeTriMeshVisitor( osg::NodeVisitor::TraversalMode 
traversalMode )
    : osg::NodeVisitor( traversalMode )
{
    stack.push_back( osg::Matrix::identity() );
    mesh = new osg::Vec3Array;
}

void ComputeTriMeshVisitor::reset()
{
    stack.clear();
    stack.push_back( osg::Matrix::identity() );
    mesh->clear();
}

void ComputeTriMeshVisitor::apply( osg::Node & node )
{
    traverse( node );
}

void ComputeTriMeshVisitor::apply( osg::Transform & transform )
{
    osg::Matrix matrix;

    matrix = stack.back();

    transform.computeLocalToWorldMatrix( matrix, this );

    pushMatrix( matrix );

    traverse( transform );

    popMatrix();
}

void ComputeTriMeshVisitor::apply( osg::Geode & geode )
{
    for( unsigned int i = 0; i < geode.getNumDrawables(); ++i )
    {
        applyDrawable( geode.getDrawable( i ) );
    }
}

void ComputeTriMeshVisitor::applyDrawable( osg::Drawable * drawable )
{
    osg::TriangleFunctor< ComputeTriMeshFunc > functor;
    drawable->accept( functor );

    for( osg::Vec3Array::iterator iter = functor.vertices->begin();
         iter != functor.vertices->end(); ++iter )
    {
        osg::Matrix & matrix = stack.back();
        mesh->push_back( *iter * matrix );
    }
}

_______________________________________________
osg-users mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Reply via email to