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