Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/physics/src/bullet/BulletCollision/CollisionShapes/btTriangleMesh.cpp @ 1963

Last change on this file since 1963 was 1963, checked in by rgrieder, 16 years ago

Added Bullet physics engine.

  • Property svn:eol-style set to native
File size: 4.2 KB
Line 
1/*
2Bullet Continuous Collision Detection and Physics Library
3Copyright (c) 2003-2006 Erwin Coumans  http://continuousphysics.com/Bullet/
4
5This software is provided 'as-is', without any express or implied warranty.
6In no event will the authors be held liable for any damages arising from the use of this software.
7Permission is granted to anyone to use this software for any purpose,
8including commercial applications, and to alter it and redistribute it freely,
9subject to the following restrictions:
10
111. 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.
122. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
133. This notice may not be removed or altered from any source distribution.
14*/
15
16#include "btTriangleMesh.h"
17
18
19
20btTriangleMesh::btTriangleMesh (bool use32bitIndices,bool use4componentVertices)
21:m_use32bitIndices(use32bitIndices),
22m_use4componentVertices(use4componentVertices),
23m_weldingThreshold(0.0)
24{
25        btIndexedMesh meshIndex;
26        meshIndex.m_numTriangles = 0;
27        meshIndex.m_numVertices = 0;
28        meshIndex.m_indexType = PHY_INTEGER;
29        meshIndex.m_triangleIndexBase = 0;
30        meshIndex.m_triangleIndexStride = 3*sizeof(int);
31        meshIndex.m_vertexBase = 0;
32        meshIndex.m_vertexStride = sizeof(btVector3);
33        m_indexedMeshes.push_back(meshIndex);
34
35        if (m_use32bitIndices)
36        {
37                m_indexedMeshes[0].m_numTriangles = m_32bitIndices.size()/3;
38                m_indexedMeshes[0].m_triangleIndexBase = (unsigned char*) &m_32bitIndices[0];
39                m_indexedMeshes[0].m_indexType = PHY_INTEGER;
40                m_indexedMeshes[0].m_triangleIndexStride = 3*sizeof(int);
41        } else
42        {
43                m_indexedMeshes[0].m_numTriangles = m_16bitIndices.size()/3;
44                m_indexedMeshes[0].m_triangleIndexBase = (unsigned char*) &m_16bitIndices[0];
45                m_indexedMeshes[0].m_indexType = PHY_SHORT;
46                m_indexedMeshes[0].m_triangleIndexStride = 3*sizeof(short int);
47        }
48
49        if (m_use4componentVertices)
50        {
51                m_indexedMeshes[0].m_numVertices = m_4componentVertices.size();
52                m_indexedMeshes[0].m_vertexBase = (unsigned char*)&m_4componentVertices[0];
53                m_indexedMeshes[0].m_vertexStride = sizeof(btVector3);
54        } else
55        {
56                m_indexedMeshes[0].m_numVertices = m_3componentVertices.size()/3;
57                m_indexedMeshes[0].m_vertexBase = (unsigned char*)&m_3componentVertices[0];
58                m_indexedMeshes[0].m_vertexStride = 3*sizeof(btScalar);
59        }
60
61
62}
63
64void    btTriangleMesh::addIndex(int index)
65{
66        if (m_use32bitIndices)
67        {
68                m_32bitIndices.push_back(index);
69                m_indexedMeshes[0].m_triangleIndexBase = (unsigned char*) &m_32bitIndices[0];
70        } else
71        {
72                m_16bitIndices.push_back(index);
73                m_indexedMeshes[0].m_triangleIndexBase = (unsigned char*) &m_16bitIndices[0];
74        }
75}
76
77int     btTriangleMesh::findOrAddVertex(const btVector3& vertex)
78{
79        //return index of new/existing vertex
80        //todo: could use acceleration structure for this
81        if (m_use4componentVertices)
82        {
83                for (int i=0;i< m_4componentVertices.size();i++)
84                {
85                        if ((m_4componentVertices[i]-vertex).length2() <= m_weldingThreshold)
86                        {
87                                return i;
88                        }
89                }
90                m_indexedMeshes[0].m_numVertices++;
91                m_4componentVertices.push_back(vertex);
92                m_indexedMeshes[0].m_vertexBase = (unsigned char*)&m_4componentVertices[0];
93
94                return m_4componentVertices.size()-1;
95               
96        } else
97        {
98               
99                for (int i=0;i< m_3componentVertices.size();i+=3)
100                {
101                        btVector3 vtx(m_3componentVertices[i],m_3componentVertices[i+1],m_3componentVertices[i+2]);
102                        if ((vtx-vertex).length2() <= m_weldingThreshold)
103                        {
104                                return i/3;
105                        }
106                }
107                m_3componentVertices.push_back(vertex.getX());
108                m_3componentVertices.push_back(vertex.getY());
109                m_3componentVertices.push_back(vertex.getZ());
110                m_indexedMeshes[0].m_numVertices++;
111                m_indexedMeshes[0].m_vertexBase = (unsigned char*)&m_3componentVertices[0];
112                return (m_3componentVertices.size()/3)-1;
113        }
114
115}
116               
117void    btTriangleMesh::addTriangle(const btVector3& vertex0,const btVector3& vertex1,const btVector3& vertex2)
118{
119        m_indexedMeshes[0].m_numTriangles++;
120               
121        addIndex(findOrAddVertex(vertex0));
122        addIndex(findOrAddVertex(vertex1));
123        addIndex(findOrAddVertex(vertex2));
124}
125
126int btTriangleMesh::getNumTriangles() const
127{
128        if (m_use32bitIndices)
129        {
130                return m_32bitIndices.size() / 3;
131        }
132        return m_16bitIndices.size() / 3;
133}
Note: See TracBrowser for help on using the repository browser.