TDME2 1.9.121
TerrainMesh.cpp
Go to the documentation of this file.
2
3#include <vector>
4
5#include <ext/reactphysics3d/src/collision/shapes/ConcaveMeshShape.h>
6#include <ext/reactphysics3d/src/collision/TriangleMesh.h>
7#include <ext/reactphysics3d/src/collision/TriangleVertexArray.h>
8
9#include <tdme/tdme.h>
14
15using std::to_string;
16using std::vector;
17
23
24TerrainMesh::TerrainMesh()
25{
26}
27
29{
30 // fetch vertices and indices
31 vector<Triangle> triangles;
32 vector<Vector3> indexedVertices;
33 model->getTriangles(triangles);
34 Vector3 vertexTransformed;
35 for (auto& triangle: triangles) {
36 for (const auto& vertex: triangle.getVertices()) {
37 vertexTransformed = transformations.getTransformationsMatrix().multiply(vertex);
38 auto i = 0;
39 for (; i < indexedVertices.size(); i++) {
40 if (indexedVertices[i].equals(vertexTransformed) == true) break;
41 }
42 if (i == indexedVertices.size()) {
43 indexedVertices.push_back(vertexTransformed);
44 vertices.push_back(vertexTransformed[0]);
45 vertices.push_back(vertexTransformed[1]);
46 vertices.push_back(vertexTransformed[2]);
47 }
48 indices.push_back(i);
49 }
50 }
51 vertices.shrink_to_fit();
52 indices.shrink_to_fit();
53 setScale(Vector3(1.0f, 1.0f, 1.0f));
54}
55
57 if (triangleMesh != nullptr) delete triangleMesh;
58 if (triangleVertexArray != nullptr) delete triangleVertexArray;
59}
60
61void TerrainMesh::setScale(const Vector3& scale) {
62 if (scale.equals(Vector3(1.0f, 1.0f, 1.0f)) == false) {
63 Console::println("TerrainMesh::setScale(): != 1.0f: Not supported!");
64 }
65 // store new scale
66 this->scale.set(scale);
67
68 // delete old collision shape
69 if (collisionShape != nullptr) delete collisionShape;
70 if (triangleMesh != nullptr) delete triangleMesh;
71 if (triangleVertexArray != nullptr) delete triangleVertexArray;
72
73 // RP3D triangle vertex array
74 triangleVertexArray = new reactphysics3d::TriangleVertexArray(
75 vertices.size() / 3,
76 vertices.data(),
77 3 * sizeof(float),
78 indices.size() / 3,
79 indices.data(),
80 3 * sizeof(int),
81 reactphysics3d::TriangleVertexArray::VertexDataType::VERTEX_FLOAT_TYPE,
82 reactphysics3d::TriangleVertexArray::IndexDataType::INDEX_INTEGER_TYPE
83 );
84
85 // add the triangle vertex array to the triangle mesh
86 triangleMesh = new reactphysics3d::TriangleMesh();
88
89 // create the concave mesh shape
90 collisionShape = new reactphysics3d::ConcaveMeshShape(triangleMesh);
91
92 // compute bounding box
94
95 //
96 vertices.clear();
97 indices.clear();;
98}
99
101 Console::println("TerrainMesh::fromTransformations(): Not supported!");
102}
103
104TerrainMesh::BoundingVolume* TerrainMesh::clone() const {
105 Console::println("TerrainMesh::clone(): Not supported!");
106 return nullptr;
107}
Transformations which contain scale, rotations and translation.
const Matrix4x4 & getTransformationsMatrix() const
reactphysics3d::CollisionShape * collisionShape
void computeBoundingBox()
Compute bounding box.
Terrain mesh physics primitive.
Definition: TerrainMesh.h:28
reactphysics3d::TriangleVertexArray * triangleVertexArray
Definition: TerrainMesh.h:32
reactphysics3d::TriangleMesh * triangleMesh
Definition: TerrainMesh.h:33
BoundingVolume * clone() const override
Clones this bounding volume.
void fromTransformations(const Transformations &transformations) override
Transform bounding volume from given transformations.
void setScale(const Vector3 &scale) override
Set local scale.
Definition: TerrainMesh.cpp:61
Triangle entity, this is not directly connectable with physics engine.
Definition: Triangle.h:19
void getTriangles(vector< Triangle > &triangles, int nodeIdx=-1)
Retrieves list of triangles of all or given nodes.
Vector3 multiply(const Vector3 &v) const
Multiplies a vector3 with this matrix into destination vector.
Definition: Matrix4x4.h:351
3D vector 3 class
Definition: Vector3.h:22
bool equals(const Vector3 &v) const
Compares this vector with given vector.
Definition: Vector3.h:381
Vector3 & set(float x, float y, float z)
Set up vector.
Definition: Vector3.h:73
Console class.
Definition: Console.h:26