-
-
Notifications
You must be signed in to change notification settings - Fork 259
Expand file tree
/
Copy pathVisualContactPoint.cpp
More file actions
294 lines (228 loc) · 11.6 KB
/
VisualContactPoint.cpp
File metadata and controls
294 lines (228 loc) · 11.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/********************************************************************************
* ReactPhysics3D physics library, http://www.reactphysics3d.com *
* Copyright (c) 2010-2016 Daniel Chappuis *
*********************************************************************************
* *
* This software is provided 'as-is', without any express or implied warranty. *
* In no event will the authors be held liable for any damages arising from the *
* use of this software. *
* *
* Permission is granted to anyone to use this software for any purpose, *
* including commercial applications, and to alter it and redistribute it *
* freely, subject to the following restrictions: *
* *
* 1. The origin of this software must not be misrepresented; you must not claim *
* that you wrote the original software. If you use this software in a *
* product, an acknowledgment in the product documentation would be *
* appreciated but is not required. *
* *
* 2. Altered source versions must be plainly marked as such, and must not be *
* misrepresented as being the original software. *
* *
* 3. This notice may not be removed or altered from any source distribution. *
* *
********************************************************************************/
// Libraries
#include "VisualContactPoint.h"
#include "ResourceManager.h"
// Initialization of static variables
openglframework::VertexBufferObject VisualContactPoint::mVBOVertices(GL_ARRAY_BUFFER);
openglframework::VertexBufferObject VisualContactPoint::mVBOVerticesNormalLine(GL_ARRAY_BUFFER);
openglframework::VertexBufferObject VisualContactPoint::mVBONormals(GL_ARRAY_BUFFER);
openglframework::VertexBufferObject VisualContactPoint::mVBOIndices(GL_ELEMENT_ARRAY_BUFFER);
openglframework::VertexArrayObject VisualContactPoint::mVAO;
openglframework::VertexArrayObject VisualContactPoint::mVAONormalLine;
openglframework::Vector3 VisualContactPoint::mContactNormalLineStaticPoints[2] = {openglframework::Vector3(0, 0, 0), openglframework::Vector3(0, 1, 0)};
int VisualContactPoint::mNbTotalPoints = 0;
openglframework::Mesh VisualContactPoint::mMesh;
bool VisualContactPoint::mStaticDataCreated = false;
// Constructor
VisualContactPoint::VisualContactPoint(const openglframework::Vector3& position,
const openglframework::Vector3& normalLineEndPointLocal, const openglframework::Color& color)
: mColor(color) {
mContactNormalLinePoints[0] = openglframework::Vector3(0, 0, 0);
mContactNormalLinePoints[1] = (normalLineEndPointLocal - position) * 0.5f;
// Initialize the position where the mesh will be rendered
translateWorld(position);
// Compute the rotation quaternion from unit (0, 1, 0) to the actual normal vector in local-space
openglframework::Vector3 normalLineEndPointLocalVec(normalLineEndPointLocal.x, normalLineEndPointLocal.y, normalLineEndPointLocal.z);
openglframework::Vector3 positionVec(position.x, position.y, position.z);
openglframework::Vector3 v1(0, 1, 0);
openglframework::Vector3 v2 = (normalLineEndPointLocalVec - positionVec);
v2.normalize();
const float dot = v1.dot(v2);
openglframework::Vector3 xUnitVec(1, 0, 0);
openglframework::Vector3 yUnitVec(0, 1, 0);
openglframework::Vector3 rotVector;
// Handle parallel vectors
if (dot < -0.99999f) {
rotVector = xUnitVec.cross(v1);
if (rotVector.lengthSquared() < 0.000001f) {
rotVector = yUnitVec.cross(v1);
}
rotVector.normalize();
mNormalRotation.setAllValues(rotVector.x, rotVector.y, rotVector.z, openglframework::PI);
}
else if (dot > 0.99999f) {
mNormalRotation.setAllValues(0, 0, 0, 1);
}
else { // Handlee other vectors
rotVector = v1.cross(v2);
const float w = sqrt(v1.lengthSquared() * v2.lengthSquared());
mNormalRotation.setAllValues(rotVector.x, rotVector.y, rotVector.z, w + dot);
mNormalRotation.normalize();
}
}
// Load and initialize the mesh for all the contact points
void VisualContactPoint::createStaticData(const std::string& meshFolderPath) {
if (mStaticDataCreated) return;
// Load the mesh from a file
openglframework::MeshReaderWriter::loadMeshFromFile(ResourceManager::getMeshPath("sphere.obj"), mMesh);
// Calculate the normals of the mesh
mMesh.calculateNormals();
mMesh.scaleVertices(VISUAL_CONTACT_POINT_RADIUS);
createSphereVBOAndVAO();
createContactNormalLineVBOAndVAO();
mStaticDataCreated = true;
}
// Destroy the mesh for the contact points
void VisualContactPoint::destroyStaticData() {
if (!mStaticDataCreated) return;
// Destroy the VBOs and VAO
mVBOIndices.destroy();
mVBOVertices.destroy();
mVBONormals.destroy();
mVAO.destroy();
mVAONormalLine.destroy();
mVBOVerticesNormalLine.destroy();
mMesh.destroy();
mStaticDataCreated = false;
}
// Render the sphere at the correct position and with the correct orientation
void VisualContactPoint::render(openglframework::Shader& shader, const openglframework::Matrix4& worldToCameraMatrix) {
// Bind the VAO
mVAO.bind();
// Bind the shader
shader.bind();
// Set the vertex color
openglframework::Vector4 color(mColor.r, mColor.g, mColor.b, mColor.a);
shader.setIntUniform("isGlobalVertexColorEnabled", 1, false);
shader.setVector4Uniform("globalVertexColor", color, false);
mVBOVertices.bind();
// Set the model to camera matrix
shader.setMatrix4x4Uniform("localToWorldMatrix", mTransformMatrix);
shader.setMatrix4x4Uniform("worldToCameraMatrix", worldToCameraMatrix);
// Set the normal matrix (inverse transpose of the 3x3 upper-left sub matrix of the
// model-view matrix)
const openglframework::Matrix4 localToCameraMatrix = worldToCameraMatrix * mTransformMatrix;
const openglframework::Matrix3 normalMatrix =
localToCameraMatrix.getUpperLeft3x3Matrix().getInverse().getTranspose();
shader.setMatrix3x3Uniform("normalMatrix", normalMatrix, false);
// Get the location of shader attribute variables
GLint vertexPositionLoc = shader.getAttribLocation("vertexPosition");
GLint vertexNormalLoc = shader.getAttribLocation("vertexNormal", false);
glEnableVertexAttribArray(vertexPositionLoc);
glVertexAttribPointer(vertexPositionLoc, 3, GL_FLOAT, GL_FALSE, 0, (char*)NULL);
mVBONormals.bind();
if (vertexNormalLoc != -1) glVertexAttribPointer(vertexNormalLoc, 3, GL_FLOAT, GL_FALSE, 0, (char*)NULL);
if (vertexNormalLoc != -1) glEnableVertexAttribArray(vertexNormalLoc);
// For each part of the mesh
for (unsigned int i=0; i<mMesh.getNbParts(); i++) {
glDrawElements(GL_TRIANGLES, mMesh.getNbFaces(i) * 3, GL_UNSIGNED_INT, (char*)NULL);
}
glDisableVertexAttribArray(vertexPositionLoc);
if (vertexNormalLoc != -1) glDisableVertexAttribArray(vertexNormalLoc);
mVBONormals.unbind();
mVBOVertices.unbind();
// Unbind the VAO
mVAO.unbind();
// Unbind the shader
shader.unbind();
// Render the contact normal line
renderContactNormalLine(shader, worldToCameraMatrix);
}
void VisualContactPoint::renderContactNormalLine(openglframework::Shader& shader, const openglframework::Matrix4& worldToCameraMatrix) {
// Bind the VAO
mVAONormalLine.bind();
// Bind the shader
shader.bind();
mVBOVerticesNormalLine.bind();
// Set the model to camera matrix
const rp3d::Matrix3x3 m = mNormalRotation.getMatrix();
const openglframework::Matrix4 contactNormalRotation(m[0][0], m[0][1], m[0][2], 0,
m[1][0], m[1][1], m[1][2], 0,
m[2][0], m[2][1], m[2][2], 0,
0, 0, 0, 1);
const openglframework::Matrix4 localToCameraMatrix = worldToCameraMatrix * mTransformMatrix * contactNormalRotation;
shader.setMatrix4x4Uniform("localToWorldMatrix", mTransformMatrix* contactNormalRotation);
shader.setMatrix4x4Uniform("worldToCameraMatrix", worldToCameraMatrix);
// Set the normal matrix (inverse transpose of the 3x3 upper-left sub matrix of the
// model-view matrix)
const openglframework::Matrix3 normalMatrix =
localToCameraMatrix.getUpperLeft3x3Matrix().getInverse().getTranspose();
shader.setMatrix3x3Uniform("normalMatrix", normalMatrix, false);
// Set the vertex color
openglframework::Vector4 color(0, 1, 0, 1);
shader.setIntUniform("isGlobalVertexColorEnabled", 1, false);
shader.setVector4Uniform("globalVertexColor", color, false);
// Get the location of shader attribute variables
GLint vertexPositionLoc = shader.getAttribLocation("vertexPosition");
glEnableVertexAttribArray(vertexPositionLoc);
glVertexAttribPointer(vertexPositionLoc, 3, GL_FLOAT, GL_FALSE, 0, (char*)NULL);
// Draw the lines
glDrawArrays(GL_LINES, 0, 2);
glDisableVertexAttribArray(vertexPositionLoc);
mVBOVerticesNormalLine.unbind();
// Unbind the VAO
mVAONormalLine.unbind();
shader.unbind();
}
// Create the Vertex Buffer Objects used to render the contact point sphere with OpenGL.
/// We create two VBOs (one for vertices and one for indices)
void VisualContactPoint::createSphereVBOAndVAO() {
// Create the VBO for the vertices data
mVBOVertices.create();
mVBOVertices.bind();
size_t sizeVertices = mMesh.getVertices().size() * sizeof(openglframework::Vector3);
mVBOVertices.copyDataIntoVBO(sizeVertices, mMesh.getVerticesPointer(), GL_STATIC_DRAW);
mVBOVertices.unbind();
// Create the VBO for the normals data
mVBONormals.create();
mVBONormals.bind();
size_t sizeNormals = mMesh.getNormals().size() * sizeof(openglframework::Vector3);
mVBONormals.copyDataIntoVBO(sizeNormals, mMesh.getNormalsPointer(), GL_STATIC_DRAW);
mVBONormals.unbind();
// Create th VBO for the indices data
mVBOIndices.create();
mVBOIndices.bind();
size_t sizeIndices = mMesh.getIndices(0).size() * sizeof(unsigned int);
mVBOIndices.copyDataIntoVBO(sizeIndices, mMesh.getIndicesPointer(), GL_STATIC_DRAW);
mVBOIndices.unbind();
// Create the VAO for both VBOs
mVAO.create();
mVAO.bind();
// Bind the VBO of vertices
mVBOVertices.bind();
// Bind the VBO of normals
mVBONormals.bind();
// Bind the VBO of indices
mVBOIndices.bind();
// Unbind the VAO
mVAO.unbind();
}
// Create the Vertex Buffer Objects used to render the contact normal line
void VisualContactPoint::createContactNormalLineVBOAndVAO() {
// Create the VBO for the vertices data
mVBOVerticesNormalLine.create();
mVBOVerticesNormalLine.bind();
size_t sizeNormalLineVertices = 2 * sizeof(openglframework::Vector3);
mVBOVerticesNormalLine.copyDataIntoVBO(sizeNormalLineVertices, &mContactNormalLineStaticPoints[0], GL_STATIC_DRAW);
mVBOVerticesNormalLine.unbind();
// Create the VAO for both VBOs
mVAONormalLine.create();
mVAONormalLine.bind();
// Bind the VBO of vertices
mVBOVerticesNormalLine.bind();
// Unbind the VAO
mVAONormalLine.unbind();
}