1 | /*************************************************************************** |
---|
2 | |
---|
3 | This source file is part of OGREBULLET |
---|
4 | (Object-oriented Graphics Rendering Engine Bullet Wrapper) |
---|
5 | For the latest info, see http://www.ogre3d.org/phpBB2addons/viewforum.php?f=10 |
---|
6 | |
---|
7 | Copyright (c) 2007 tuan.kuranes@gmail.com (Use it Freely, even Statically, but have to contribute any changes) |
---|
8 | |
---|
9 | |
---|
10 | |
---|
11 | This program is free software; you can redistribute it and/or modify it under |
---|
12 | the terms of the GPL General Public License with runtime exception as published by the Free Software |
---|
13 | Foundation; either version 2 of the License, or (at your option) any later |
---|
14 | version. |
---|
15 | |
---|
16 | This program is distributed in the hope that it will be useful, but WITHOUT |
---|
17 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
---|
18 | FOR A PARTICULAR PURPOSE. See the GPL General Public License with runtime exception for more details. |
---|
19 | |
---|
20 | You should have received a copy of the GPL General Public License with runtime exception along with |
---|
21 | this program; if not, write to the Free Software Foundation, Inc., 59 Temple |
---|
22 | Place - Suite 330, Boston, MA 02111-1307, USA, or go to |
---|
23 | http://www.gnu.org/licenses/old-licenses/gpl-2.0.html |
---|
24 | ----------------------------------------------------------------------------- |
---|
25 | */ |
---|
26 | |
---|
27 | #include "OgreBulletCollisions.h" |
---|
28 | |
---|
29 | #include "Shapes/OgreBulletCollisionsTrimeshShape.h" |
---|
30 | #include "Debug/OgreBulletCollisionsDebugLines.h" |
---|
31 | #include "Utils/OgreBulletConverter.h" |
---|
32 | |
---|
33 | using namespace Ogre; |
---|
34 | using namespace OgreBulletCollisions; |
---|
35 | |
---|
36 | namespace OgreBulletCollisions |
---|
37 | { |
---|
38 | // ------------------------------------------------------------------------- |
---|
39 | TriangleMeshCollisionShape::TriangleMeshCollisionShape( |
---|
40 | Vector3 *vertices, |
---|
41 | unsigned int vertexCount, |
---|
42 | unsigned int *indices, |
---|
43 | unsigned int indexCount, |
---|
44 | bool use32bitsIndices) : |
---|
45 | CollisionShape(), |
---|
46 | mTriMesh(0) |
---|
47 | { |
---|
48 | unsigned int numFaces = indexCount / 3; |
---|
49 | |
---|
50 | mTriMesh = new btTriangleMesh(use32bitsIndices); |
---|
51 | |
---|
52 | btVector3 vertexPos[3]; |
---|
53 | for (unsigned int n = 0; n < numFaces; ++n) |
---|
54 | { |
---|
55 | { |
---|
56 | const Vector3 &vec = vertices[*indices]; |
---|
57 | vertexPos[0][0] = vec.x; |
---|
58 | vertexPos[0][1] = vec.y; |
---|
59 | vertexPos[0][2] = vec.z; |
---|
60 | } |
---|
61 | { |
---|
62 | const Vector3 &vec = vertices[*(indices + 1)]; |
---|
63 | vertexPos[1][0] = vec.x; |
---|
64 | vertexPos[1][1] = vec.y; |
---|
65 | vertexPos[1][2] = vec.z; |
---|
66 | } |
---|
67 | { |
---|
68 | const Vector3 &vec = vertices[*(indices + 2)]; |
---|
69 | vertexPos[2][0] = vec.x; |
---|
70 | vertexPos[2][1] = vec.y; |
---|
71 | vertexPos[2][2] = vec.z; |
---|
72 | } |
---|
73 | |
---|
74 | indices += 3; |
---|
75 | |
---|
76 | mTriMesh->addTriangle(vertexPos[0], vertexPos[1], vertexPos[2]); |
---|
77 | } |
---|
78 | |
---|
79 | const bool useQuantizedAABB = true; |
---|
80 | mShape = new btBvhTriangleMeshShape(mTriMesh, useQuantizedAABB); |
---|
81 | |
---|
82 | } |
---|
83 | // ------------------------------------------------------------------------- |
---|
84 | TriangleMeshCollisionShape::~TriangleMeshCollisionShape() |
---|
85 | { |
---|
86 | } |
---|
87 | // ------------------------------------------------------------------------- |
---|
88 | bool TriangleMeshCollisionShape::drawWireFrame(DebugLines *wire, |
---|
89 | const Ogre::Vector3 &pos, |
---|
90 | const Ogre::Quaternion &quat) const |
---|
91 | { |
---|
92 | const int numTris = mTriMesh->getNumTriangles (); |
---|
93 | if (numTris > 0) |
---|
94 | { |
---|
95 | |
---|
96 | const int numSubParts = mTriMesh->getNumSubParts (); |
---|
97 | for (int currSubPart = 0; currSubPart < numSubParts; currSubPart++) |
---|
98 | { |
---|
99 | const unsigned char* vertexBase = NULL; |
---|
100 | int numVerts; |
---|
101 | PHY_ScalarType vertexType; |
---|
102 | int vertexStride; |
---|
103 | const unsigned char* indexBase = NULL; |
---|
104 | int indexStride; |
---|
105 | int numFaces; |
---|
106 | PHY_ScalarType indexType; |
---|
107 | |
---|
108 | mTriMesh->getLockedReadOnlyVertexIndexBase (&vertexBase, numVerts, |
---|
109 | vertexType, vertexStride, |
---|
110 | &indexBase, indexStride, numFaces, indexType, currSubPart); |
---|
111 | |
---|
112 | float* p; |
---|
113 | btVector3 vert0; |
---|
114 | btVector3 vert1; |
---|
115 | btVector3 vert2; |
---|
116 | for (int t = 0; t < numFaces; t++) |
---|
117 | { |
---|
118 | #define setVector(A, B) {A.setX(B[0]);A.setY(B[1]);A.setZ(B[2]);}; |
---|
119 | |
---|
120 | if (indexType == PHY_SHORT) |
---|
121 | { |
---|
122 | short int* index = (short int*)(indexBase + t*indexStride); |
---|
123 | |
---|
124 | p = (float*)(vertexBase + index[0]*vertexStride); |
---|
125 | setVector(vert0, p); |
---|
126 | p = (float*)(vertexBase + index[1]*vertexStride); |
---|
127 | setVector(vert1, p); |
---|
128 | p = (float*)(vertexBase + index[2]*vertexStride); |
---|
129 | setVector(vert2, p); |
---|
130 | } |
---|
131 | else |
---|
132 | { |
---|
133 | int* index = (int*)(indexBase + t*indexStride); |
---|
134 | |
---|
135 | p = (float*)(vertexBase + index[0]*vertexStride); |
---|
136 | setVector(vert0, p); |
---|
137 | p = (float*)(vertexBase + index[1]*vertexStride); |
---|
138 | setVector(vert1, p); |
---|
139 | p = (float*)(vertexBase + index[2]*vertexStride); |
---|
140 | setVector(vert2, p); |
---|
141 | } |
---|
142 | #undef setVector |
---|
143 | |
---|
144 | wire->addLine (BtOgreConverter::to(vert0), BtOgreConverter::to(vert1)); |
---|
145 | wire->addLine (BtOgreConverter::to(vert1), BtOgreConverter::to(vert2)); |
---|
146 | wire->addLine (BtOgreConverter::to(vert2), BtOgreConverter::to(vert0)); |
---|
147 | } |
---|
148 | } |
---|
149 | return true; |
---|
150 | } |
---|
151 | return false; |
---|
152 | } |
---|
153 | } |
---|
154 | |
---|