TDME2  1.9.200
TerrainMesh.cpp
Go to the documentation of this file.
2 
3 #include <memory>
4 #include <vector>
5 
6 #include <reactphysics3d/collision/shapes/ConcaveMeshShape.h>
7 #include <reactphysics3d/collision/TriangleMesh.h>
8 #include <reactphysics3d/collision/TriangleVertexArray.h>
9 
10 #include <tdme/tdme.h>
15 #include <tdme/utilities/Console.h>
16 
17 using std::make_unique;
18 using std::to_string;
19 using std::unique_ptr;
20 using std::vector;
21 
28 
29 TerrainMesh::TerrainMesh()
30 {
31 }
32 
34 {
35  // fetch vertices and indices
36  vector<Triangle> triangles;
37  vector<Vector3> indexedVertices;
38  model->getTriangles(triangles);
39  Vector3 vertexTransformed;
40  for (const auto& triangle: triangles) {
41  for (const auto& vertex: triangle.getVertices()) {
42  vertexTransformed = transform.getTransformMatrix().multiply(vertex);
43  auto i = 0;
44  for (; i < indexedVertices.size(); i++) {
45  if (indexedVertices[i].equals(vertexTransformed) == true) break;
46  }
47  if (i == indexedVertices.size()) {
48  indexedVertices.push_back(vertexTransformed);
49  vertices.push_back(vertexTransformed[0]);
50  vertices.push_back(vertexTransformed[1]);
51  vertices.push_back(vertexTransformed[2]);
52  }
53  indices.push_back(i);
54  }
55  }
56  vertices.shrink_to_fit();
57  indices.shrink_to_fit();
58  setScale(Vector3(1.0f, 1.0f, 1.0f));
59 }
60 
63 }
64 
65 void TerrainMesh::setScale(const Vector3& scale) {
66  //
67  if (scale.equals(Vector3(1.0f, 1.0f, 1.0f)) == false) {
68  Console::println("TerrainMesh::setScale(): != 1.0f: Not supported!");
69  }
70  // store new scale
71  this->scale.set(scale);
72 }
73 
74 void TerrainMesh::setTransform(const Transform& transform) {
75  Console::println("TerrainMesh::setTransform(): Not supported!");
76 }
77 
79  if (collisionShape == nullptr) return;
80  this->world->physicsCommon.destroyConcaveMeshShape(static_cast<reactphysics3d::ConcaveMeshShape*>(collisionShape));
81  this->world->physicsCommon.destroyTriangleMesh(triangleMesh);
82  triangleVertexArray = nullptr;
83  collisionShape = nullptr;
84  triangleMesh = nullptr;
85  triangleVertexArray = nullptr;
86 }
87 
89  if (this->world != nullptr && this->world != world) {
90  Console::println("TerrainMesh::createCollisionShape(): already attached to a world.");
91  }
92  this->world = world;
93 
94  // RP3D triangle vertex array
95  triangleVertexArray = make_unique<reactphysics3d::TriangleVertexArray>(
96  static_cast<uint32_t>(vertices.size() / 3),
97  vertices.data(),
98  3 * sizeof(float),
99  static_cast<uint32_t>(indices.size() / 3),
100  indices.data(),
101  3 * sizeof(int),
102  reactphysics3d::TriangleVertexArray::VertexDataType::VERTEX_FLOAT_TYPE,
103  reactphysics3d::TriangleVertexArray::IndexDataType::INDEX_INTEGER_TYPE
104  );
105 
106  // add the triangle vertex array to the triangle mesh
107  triangleMesh = world->physicsCommon.createTriangleMesh();
108  triangleMesh->addSubpart(triangleVertexArray.get());
109 
110  // create the concave mesh shape
111  collisionShape = world->physicsCommon.createConcaveMeshShape(triangleMesh);
112 }
113 
114 TerrainMesh::BoundingVolume* TerrainMesh::clone() const {
115  auto terrainMesh = new TerrainMesh();
116  terrainMesh->vertices = vertices;
117  terrainMesh->indices = indices;
118  return terrainMesh;
119 }
Transform which contain scale, rotations and translation.
Definition: Transform.h:29
const Matrix4x4 & getTransformMatrix() const
Definition: Transform.h:169
Dynamic physics world class.
Definition: World.h:38
reactphysics3d::PhysicsCommon physicsCommon
Definition: World.h:53
reactphysics3d::CollisionShape * collisionShape
Terrain mesh physics primitive.
Definition: TerrainMesh.h:31
reactphysics3d::TriangleMesh * triangleMesh
Definition: TerrainMesh.h:36
BoundingVolume * clone() const override
Clones this bounding volume.
unique_ptr< reactphysics3d::TriangleVertexArray > triangleVertexArray
Definition: TerrainMesh.h:35
void setTransform(const Transform &transform) override
Transform bounding volume from given transform.
Definition: TerrainMesh.cpp:74
void destroyCollisionShape() override
Destroy collision shape.
Definition: TerrainMesh.cpp:78
void setScale(const Vector3 &scale) override
Set local scale.
Definition: TerrainMesh.cpp:65
void createCollisionShape(World *world) override
Create collision shap.
Definition: TerrainMesh.cpp:88
Triangle entity, this is not directly connectable with physics engine.
Definition: Triangle.h:18
void getTriangles(vector< Triangle > &triangles, int nodeIdx=-1)
Retrieves list of triangles of all or given nodes.
Definition: ObjectBase.cpp:99
Vector3 multiply(const Vector3 &vector3) const
Multiplies this matrix with vector3.
Definition: Matrix4x4.h:225
Vector3 class representing vector3 mathematical structure and operations with x, y,...
Definition: Vector3.h:20
Vector3 & set(float x, float y, float z)
Sets this vector3 by its components.
Definition: Vector3.h:70
bool equals(const Vector3 &vector3, float tolerance=Math::EPSILON) const
Compares this vector3 with given vector3.
Definition: Vector3.h:226
Console class.
Definition: Console.h:29