BulletMesh.cpp

Go to the documentation of this file.
00001 
00005 /* Copyright © 2009, 2010 James Legg.
00006     This program is free software: you can redistribute it and/or modify
00007     it under the terms of the GNU General Public License as published by
00008     the Free Software Foundation, either version 3 of the License, or
00009     (at your option) any later version.
00010 */
00011 #include "BulletMesh.h"
00012 
00013 #include <fstream>
00014 
00015 #include <GL/gl.h>
00016 #include <btBulletDynamicsCommon.h>
00017 #include <btBulletCollisionCommon.h>
00018 #include <BulletCollision/CollisionShapes/btShapeHull.h>
00019 
00020 #include <Debug.h>
00021 
00022 namespace Track
00023 {
00024 
00025 BulletMesh::BulletMesh(std::string filename, unsigned int generators, DrawableMesh::RenderMode mode)
00026     :   DrawableMesh(filename, mode)
00027     ,   simplified_convex_shape(0)
00028     ,   triangle_mesh_shape(0)
00029     ,   triangle_mesh(0)
00030 {
00031     create(generators);
00032 }
00033 
00034 BulletMesh::BulletMesh(std::istream & source, unsigned int generators, DrawableMesh::RenderMode mode)
00035     :   DrawableMesh(source, mode)
00036     ,   simplified_convex_shape(0)
00037     ,   triangle_mesh_shape(0)
00038     ,   triangle_mesh(0)
00039 {
00040     create(generators);
00041 }
00042 
00043 BulletMesh::BulletMesh(ConstructionInformation construction_information)
00044     :   DrawableMesh(construction_information.first.c_str())
00045     ,   simplified_convex_shape(0)
00046     ,   triangle_mesh_shape(0)
00047     ,   triangle_mesh(0)
00048 {
00049     create(construction_information.second);
00050 }
00051 
00052 void BulletMesh::create(unsigned int generators)
00053 { 
00054     bool make_convex = generators & genererator_convex_hull_bit;
00055     bool make_triangle = generators & genererator_triangle_mesh_bit;
00056     
00057     btConvexHullShape base_shape;
00058     if (make_triangle)
00059     {
00060         triangle_mesh = new btTriangleMesh();
00061     }
00062     {
00063         if (make_convex)
00064         {
00065             for (std::vector<btVector3>::const_iterator it = vertices_position.begin();
00066                  it != vertices_position.end();
00067                  it++)
00068             {
00069                 base_shape.addPoint(*it);
00070             }
00071         }
00072         if (make_triangle)
00073         {
00074             for (std::vector<MeshFaces::Face>::const_iterator it = faces.begin();
00075                  it != faces.end();
00076                  it++)
00077             {
00078                 triangle_mesh->addTriangle(vertices_position[it->fv1.vertex_index],
00079                                            vertices_position[it->fv2.vertex_index],
00080                                            vertices_position[it->fv3.vertex_index]);
00081             }
00082         }
00083     }
00084 
00085     // simplify the shape with a hull approximation
00086     if (make_convex)
00087     {
00088         btShapeHull hull(&base_shape);
00089         btScalar margin = base_shape.getMargin();
00090         hull.buildHull(margin);
00091         simplified_convex_shape = new btConvexHullShape(&(hull.getVertexPointer()->m_floats[0]),
00092                                                         hull.numVertices());
00093     }
00094     
00095     // make the triangle mesh shape
00096     if (make_triangle)
00097     {
00098         triangle_mesh_shape = new btBvhTriangleMeshShape(triangle_mesh, false);
00099     }
00100 }
00101 
00102 BulletMesh::~BulletMesh()
00103 {
00104     DEBUG_MESSAGE("Deleting mesh");
00105     delete simplified_convex_shape; 
00106     delete triangle_mesh_shape;
00107     delete triangle_mesh;
00108 }
00109 
00110 btConvexHullShape * BulletMesh::get_convex_hull_shape()
00111 {
00112     return simplified_convex_shape;
00113 }
00114 
00115 btBvhTriangleMeshShape * BulletMesh::get_triangle_shape()
00116 {
00117     return triangle_mesh_shape;
00118 }
00119 
00120 } // namespace Track

Get Racer at SourceForge.net. Fast, secure and Free Open Source software downloads

Generated at Mon Sep 6 00:41:11 2010 by Doxygen version 1.4.7 for Racer version svn335.