Back to index

supertuxkart  0.5+dfsg1
btTriangleMesh.cpp
Go to the documentation of this file.
00001 /*
00002 Bullet Continuous Collision Detection and Physics Library
00003 Copyright (c) 2003-2006 Erwin Coumans  http://continuousphysics.com/Bullet/
00004 
00005 This software is provided 'as-is', without any express or implied warranty.
00006 In no event will the authors be held liable for any damages arising from the use of this software.
00007 Permission is granted to anyone to use this software for any purpose, 
00008 including commercial applications, and to alter it and redistribute it freely, 
00009 subject to the following restrictions:
00010 
00011 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
00012 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
00013 3. This notice may not be removed or altered from any source distribution.
00014 */
00015 
00016 #include "btTriangleMesh.h"
00017 #include <assert.h>
00018 
00019 
00020 btTriangleMesh::btTriangleMesh (bool use32bitIndices,bool use4componentVertices)
00021 :m_use32bitIndices(use32bitIndices),
00022 m_use4componentVertices(use4componentVertices)
00023 {
00024        btIndexedMesh meshIndex;
00025        meshIndex.m_numTriangles = 0;
00026        meshIndex.m_numVertices = 0;
00027        meshIndex.m_indexType = PHY_INTEGER;
00028        meshIndex.m_triangleIndexBase = 0;
00029        meshIndex.m_triangleIndexStride = 3*sizeof(int);
00030        meshIndex.m_vertexBase = 0;
00031        meshIndex.m_vertexStride = sizeof(btVector3);
00032        m_indexedMeshes.push_back(meshIndex);
00033 
00034        if (m_use32bitIndices)
00035        {
00036               m_indexedMeshes[0].m_numTriangles = m_32bitIndices.size()/3;
00037               m_indexedMeshes[0].m_triangleIndexBase = (unsigned char*) &m_32bitIndices[0];
00038               m_indexedMeshes[0].m_indexType = PHY_INTEGER;
00039               m_indexedMeshes[0].m_triangleIndexStride = 3*sizeof(int);
00040        } else
00041        {
00042               m_indexedMeshes[0].m_numTriangles = m_16bitIndices.size()/3;
00043               m_indexedMeshes[0].m_triangleIndexBase = (unsigned char*) &m_16bitIndices[0];
00044               m_indexedMeshes[0].m_indexType = PHY_SHORT;
00045               m_indexedMeshes[0].m_triangleIndexStride = 3*sizeof(short int);
00046        }
00047 
00048        if (m_use4componentVertices)
00049        {
00050               m_indexedMeshes[0].m_numVertices = m_4componentVertices.size();
00051               m_indexedMeshes[0].m_vertexBase = (unsigned char*)&m_4componentVertices[0];
00052               m_indexedMeshes[0].m_vertexStride = sizeof(btVector3);
00053        } else
00054        {
00055               m_indexedMeshes[0].m_numVertices = m_3componentVertices.size()/3;
00056               m_indexedMeshes[0].m_vertexBase = (unsigned char*)&m_3componentVertices[0];
00057               m_indexedMeshes[0].m_vertexStride = 3*sizeof(btScalar);
00058        }
00059 
00060 
00061 }
00062 
00063               
00064 void   btTriangleMesh::addTriangle(const btVector3& vertex0,const btVector3& vertex1,const btVector3& vertex2)
00065 {
00066        m_indexedMeshes[0].m_numTriangles++;
00067        m_indexedMeshes[0].m_numVertices+=3;
00068 
00069        if (m_use4componentVertices)
00070        {
00071               m_4componentVertices.push_back(vertex0);
00072               m_4componentVertices.push_back(vertex1);
00073               m_4componentVertices.push_back(vertex2);
00074               m_indexedMeshes[0].m_vertexBase = (unsigned char*)&m_4componentVertices[0];
00075        } else
00076        {
00077               m_3componentVertices.push_back(vertex0.getX());
00078               m_3componentVertices.push_back(vertex0.getY());
00079               m_3componentVertices.push_back(vertex0.getZ());
00080 
00081               m_3componentVertices.push_back(vertex1.getX());
00082               m_3componentVertices.push_back(vertex1.getY());
00083               m_3componentVertices.push_back(vertex1.getZ());
00084 
00085               m_3componentVertices.push_back(vertex2.getX());
00086               m_3componentVertices.push_back(vertex2.getY());
00087               m_3componentVertices.push_back(vertex2.getZ());
00088               m_indexedMeshes[0].m_vertexBase = (unsigned char*)&m_3componentVertices[0];
00089        }
00090 
00091        if (m_use32bitIndices)
00092        {
00093               int curIndex = m_32bitIndices.size();
00094               m_32bitIndices.push_back(curIndex++);
00095               m_32bitIndices.push_back(curIndex++);
00096               m_32bitIndices.push_back(curIndex++);
00097               m_indexedMeshes[0].m_triangleIndexBase = (unsigned char*) &m_32bitIndices[0];
00098        } else
00099        {
00100               int curIndex = m_16bitIndices.size();
00101               m_16bitIndices.push_back(curIndex++);
00102               m_16bitIndices.push_back(curIndex++);
00103               m_16bitIndices.push_back(curIndex++);
00104               m_indexedMeshes[0].m_triangleIndexBase = (unsigned char*) &m_16bitIndices[0];
00105        }
00106 }
00107 
00108 int btTriangleMesh::getNumTriangles() const
00109 {
00110        if (m_use32bitIndices)
00111        {
00112               return m_32bitIndices.size() / 3;
00113        }
00114        return m_16bitIndices.size() / 3;
00115 }