1 /********************************************************************************
2 * ReactPhysics3D physics library, http://www.reactphysics3d.com                 *
3 * Copyright (c) 2010-2016 Daniel Chappuis                                       *
4 *********************************************************************************
5 *                                                                               *
6 * This software is provided 'as-is', without any express or implied warranty.   *
7 * In no event will the authors be held liable for any damages arising from the  *
8 * use of this software.                                                         *
9 *                                                                               *
10 * Permission is granted to anyone to use this software for any purpose,         *
11 * including commercial applications, and to alter it and redistribute it        *
12 * freely, subject to the following restrictions:                                *
13 *                                                                               *
14 * 1. The origin of this software must not be misrepresented; you must not claim *
15 *    that you wrote the original software. If you use this software in a        *
16 *    product, an acknowledgment in the product documentation would be           *
17 *    appreciated but is not required.                                           *
18 *                                                                               *
19 * 2. Altered source versions must be plainly marked as such, and must not be    *
20 *    misrepresented as being the original software.                             *
21 *                                                                               *
22 * 3. This notice may not be removed or altered from any source distribution.    *
23 *                                                                               *
24 ********************************************************************************/
25 
26 #ifndef HEIGHT_FIELD_H
27 #define HEIGHT_FIELD_H
28 
29 // Libraries
30 #include "openglframework.h"
31 #include <reactphysics3d/reactphysics3d.h>
32 #include "PhysicsObject.h"
33 
34 
35 // Class HeightField
36 class HeightField : public PhysicsObject {
37 
38     private :
39 
40         static const int NB_POINTS_WIDTH = 100;
41         static const int NB_POINTS_LENGTH = 100;
42 
43         // -------------------- Attributes -------------------- //
44 
45         /// Height field data
46         float mHeightData[NB_POINTS_WIDTH * NB_POINTS_LENGTH];
47 
48         /// Collision shape
49         rp3d::HeightFieldShape* mHeightFieldShape;
50         rp3d::Collider* mCollider;
51 
52         /// Scaling matrix
53         openglframework::Matrix4 mScalingMatrix;
54 
55         /// Vertex Buffer Object for the vertices data
56         openglframework::VertexBufferObject mVBOVertices;
57 
58         /// Vertex Buffer Object for the normals data
59         openglframework::VertexBufferObject mVBONormals;
60 
61         /// Vertex Buffer Object for the texture coords
62         openglframework::VertexBufferObject mVBOTextureCoords;
63 
64         /// Vertex Buffer Object for the indices
65         openglframework::VertexBufferObject mVBOIndices;
66 
67         /// Vertex Array Object for the vertex data
68         openglframework::VertexArrayObject mVAO;
69 
70         /// Min/Max height of the height field values
71         float mMinHeight, mMaxHeight;
72 
73         // -------------------- Methods -------------------- //
74 
75         /// Create the Vertex Buffer Objects used to render with OpenGL.
76         void createVBOAndVAO();
77 
78         /// Compute the heights of the height field
79         void generateHeightField();
80 
81         /// Generate the graphics mesh to render the height field
82         void generateGraphicsMesh();
83 
84     public :
85 
86         // -------------------- Methods -------------------- //
87 
88         /// Constructor
89         HeightField(bool createRigidBody, rp3d::PhysicsCommon& physicsCommon, rp3d::PhysicsWorld* physicsWorld);
90 
91         /// Destructor
92         virtual ~HeightField() override;
93 
94         /// Render the mesh at the correct position and with the correct orientation
95         void render(openglframework::Shader& shader,
96                     const openglframework::Matrix4& worldToCameraMatrix) override;
97 
98         /// Update the transform matrix of the object
99         virtual void updateTransform(float interpolationFactor) override;
100 
101         /// Return the collider
102         rp3d::Collider* getCollider();
103 };
104 
105 // Update the transform matrix of the object
updateTransform(float interpolationFactor)106 inline void HeightField::updateTransform(float interpolationFactor) {
107     mTransformMatrix = computeTransform(interpolationFactor, mScalingMatrix);
108 }
109 
110 // Return the collider
getCollider()111 inline rp3d::Collider* HeightField::getCollider() {
112     return mCollider;
113 }
114 
115 #endif
116