SuperTuxKart
triangle_mesh.hpp
1 //
2 // SuperTuxKart - a fun racing game with go-kart
3 // Copyright (C) 2006-2015 Joerg Henrichs
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 3
8 // of the License, or (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 
19 #ifndef HEADER_TRIANGLE_MESH_HPP
20 #define HEADER_TRIANGLE_MESH_HPP
21 
22 #include <vector>
23 #include "btBulletDynamicsCommon.h"
24 
25 #include "physics/user_pointer.hpp"
26 #include "utils/aligned_array.hpp"
27 
28 class Material;
29 
35 {
36 private:
37  UserPointer m_user_pointer;
38  std::vector<const Material*> m_triangleIndex2Material;
39  btRigidBody *m_body;
42 
43  btCollisionObject *m_collision_object;
44  btTriangleMesh m_mesh;
45  btVector3 dummy1, dummy2;
46  btDefaultMotionState *m_motion_state;
47  btCollisionShape *m_collision_shape;
48 
50  AlignedArray<btVector3> m_normals;
51 
53  AlignedArray<float> m_p1p2p3;
54 
59 
60 public:
61  class RigidBodyTriangleMesh : public btRigidBody
62  {
63  public:
64  TriangleMesh *m_triangle_mesh;
66  const btRigidBody::btRigidBodyConstructionInfo &ci)
67  : btRigidBody(ci),
68  m_triangle_mesh(tm)
69  {
70  } // RigidBodyTriangleMesh
71  };
72 
73  TriangleMesh(bool can_be_transformed);
74  ~TriangleMesh();
75  void addTriangle(const btVector3 &t1, const btVector3 &t2,
76  const btVector3 &t3, const btVector3 &n1,
77  const btVector3 &n2, const btVector3 &n3,
78  const Material* m);
79  void createCollisionShape(bool create_collision_object=true, const char* serialized_bhv=NULL);
80  void createPhysicalBody(float friction,
81  btCollisionObject::CollisionFlags flags=
82  (btCollisionObject::CollisionFlags)0,
83  const char* serializedBhv = NULL);
84  void removeAll();
85  void removeCollisionObject();
86  btVector3 getInterpolatedNormal(unsigned int index,
87  const btVector3 &position) const;
88  // ------------------------------------------------------------------------
94  void setBody(btRigidBody *body)
95  {
96  assert(!m_body);
97  // Mark that the body should not be deleted when this object is
98  // deleted, since the body is managed elsewhere.
99  m_free_body = false;
100  m_body = body;
101  }
102  const btRigidBody *getBody() const { return m_body; }
103  // ------------------------------------------------------------------------
104  const Material* getMaterial(int n) const
105  {return m_triangleIndex2Material[n];}
106  // ------------------------------------------------------------------------
107  const btCollisionShape &getCollisionShape() const
108  { return *m_collision_shape; }
109  // ------------------------------------------------------------------------
110  btCollisionShape &getCollisionShape() { return *m_collision_shape; }
111  // ------------------------------------------------------------------------
112  bool castRay(const btVector3 &from, const btVector3 &to,
113  btVector3 *xyz, const Material **material,
114  btVector3 *normal=NULL, bool interpolate_normal=false) const;
115  // ------------------------------------------------------------------------
119  void getTriangle(unsigned int indx, btVector3 *p1, btVector3 *p2,
120  btVector3 *p3) const
121  {
122  const IndexedMeshArray &m = m_mesh.getIndexedMeshArray();
123  btVector3 *p = &(((btVector3*)(m[0].m_vertexBase))[3*indx]);
124  *p1 = p[0];
125  *p2 = p[1];
126  *p3 = p[2];
127  } // getTriangle
128  // ------------------------------------------------------------------------
132  void getNormals(unsigned int indx, btVector3 *n1,
133  btVector3 *n2, btVector3 *n3) const
134  {
135  assert(indx < m_triangleIndex2Material.size());
136  unsigned int n = indx*3;
137  *n1 = m_normals[n ];
138  *n2 = m_normals[n+1];
139  *n3 = m_normals[n+2];
140  } // getNormals
141  // ------------------------------------------------------------------------
144  float getP1P2P3(unsigned int indx) const
145  {
146  assert(indx < m_p1p2p3.size());
147  return m_p1p2p3[indx];
148  }
149  // ------------------------------------------------------------------------
150  void copyFrom(const TriangleMesh& tm)
151  {
152  for (int i = 0; i < tm.m_mesh.getNumTriangles(); i++)
153  {
154  btVector3 v[6];
155  tm.getTriangle(i, v, v + 1, v + 2);
156  tm.getNormals(i, v + 3, v + 4, v + 5);
157  const Material* m = tm.getMaterial(i);
158  addTriangle(v[0], v[1], v[2], v[3], v[4], v[5], m);
159  }
160  }
161 };
162 #endif
163 /* EOF */
164 
Definition: material.hpp:48
Definition: triangle_mesh.hpp:62
A special class to store a triangle mesh with a separate material per triangle.
Definition: triangle_mesh.hpp:35
void getNormals(unsigned int indx, btVector3 *n1, btVector3 *n2, btVector3 *n3) const
Returns the normals of the triangle with the given index.
Definition: triangle_mesh.hpp:132
bool castRay(const btVector3 &from, const btVector3 &to, btVector3 *xyz, const Material **material, btVector3 *normal=NULL, bool interpolate_normal=false) const
Casts a ray from 'from' to 'to'.
Definition: triangle_mesh.cpp:321
TriangleMesh(bool can_be_transformed)
Constructor: Initialises all data structures with zero.
Definition: triangle_mesh.cpp:35
bool m_can_be_transformed
If the rigid body can be transformed (which means that normalising the normals need to update the ver...
Definition: triangle_mesh.hpp:58
bool m_free_body
Keep track if the physical body was created here or not.
Definition: triangle_mesh.hpp:41
AlignedArray< float > m_p1p2p3
Pre-compute value used in smoothing.
Definition: triangle_mesh.hpp:53
void createCollisionShape(bool create_collision_object=true, const char *serialized_bhv=NULL)
Creates a collision body only, which can be used for raycasting, but has no physical properties.
Definition: triangle_mesh.cpp:96
void removeAll()
Removes the created body and/or collision object from the physics world.
Definition: triangle_mesh.cpp:218
void addTriangle(const btVector3 &t1, const btVector3 &t2, const btVector3 &t3, const btVector3 &n1, const btVector3 &n2, const btVector3 &n3, const Material *m)
Adds a triangle to the bullet mesh.
Definition: triangle_mesh.cpp:66
AlignedArray< btVector3 > m_normals
The three normals for each triangle.
Definition: triangle_mesh.hpp:50
void createPhysicalBody(float friction, btCollisionObject::CollisionFlags flags=(btCollisionObject::CollisionFlags) 0, const char *serializedBhv=NULL)
Creates the physics body for this triangle mesh.
Definition: triangle_mesh.cpp:185
~TriangleMesh()
Destructor: delete all allocated data structures.
Definition: triangle_mesh.cpp:54
btVector3 getInterpolatedNormal(unsigned int index, const btVector3 &position) const
Interpolates the normal at the given position for the triangle with a given index.
Definition: triangle_mesh.cpp:244
void setBody(btRigidBody *body)
In case of physical objects of shape 'exact', the physical body is created outside of the mesh.
Definition: triangle_mesh.hpp:94
void getTriangle(unsigned int indx, btVector3 *p1, btVector3 *p2, btVector3 *p3) const
Returns the points of the 'indx' triangle.
Definition: triangle_mesh.hpp:119
float getP1P2P3(unsigned int indx) const
Returns basically the area of the triangle, which is needed when smoothing the normals.
Definition: triangle_mesh.hpp:144
A UserPointer is stored as a user pointer in all bullet bodies.
Definition: user_pointer.hpp:36