Repository navigation
Expand file tree
/
Copy pathPointCloud.cpp
More file actions
328 lines (281 loc) · 8.31 KB
/
Copy pathPointCloud.cpp
File metadata and controls
328 lines (281 loc) · 8.31 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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#include "PointCloud.h"
#include "Window.h"
#include <math.h>
const float SCALE_FACTOR = 9.0f;
const float ANIMATION_LENGTH = 100.0f;
PointCloud::PointCloud(std::string objFilename, GLfloat pointSize, Material *m)
: pointSize(pointSize)
{
randomAnimPlaying = false;
animationStartTime = 0;
localSpinCounter = 0.0f;
material = m;
//set default behaviour to load points of a cube
if (objFilename == "foo") {
points =
{
glm::vec3(-2.5, 2.5, 2.5),
glm::vec3(-2.5, -2.5, 2.5),
glm::vec3(2.5, -2.5, 2.5),
glm::vec3(2.5, 2.5, 2.5),
glm::vec3(-2.5, 2.5, -2.5),
glm::vec3(-2.5, -2.5, -2.5),
glm::vec3(2.5, -2.5, -2.5),
glm::vec3(2.5, 2.5, -2.5)
};
}
//else load points from the specified obj
else {
std::ifstream objStream(objFilename);
std::string line;
if (objStream.is_open()) {
while (getline(objStream, line)) {
std::stringstream ss;
ss << line;
//check for vertex label
std::string label;
ss >> label;
if (label == "v") {
//construct a point from the line
glm::vec3 point;
ss >> point.x;
ss >> point.y;
ss >> point.z;
//push the point to the point list
points.push_back(point);
}
if (label == "vn") {
glm::vec3 normal;
ss >> normal.x;
ss >> normal.y;
ss >> normal.z;
//push the normal to the normals list
normals.push_back(normal);
}
if (label == "f") {
//read the vertices for the triangles
std::string s;
std::stringstream vx;
std::stringstream vy;
std::stringstream vz;
std::string v;
glm::uvec3 triangle;
ss >> s;
vx << s;
std::getline(vx, v, '/');
//std::cout << v << " ";
triangle.x = std::stoi(v) - 1;
ss >> s;
vy << s;
std::getline(vy, v, '/');
//std::cout << v << " ";
triangle.y = std::stoi(v) - 1;
ss >> s;
vz << s;
std::getline(vz, v, '/');
//std::cout << v << "\n";
triangle.z = std::stoi(v) - 1;
triangles.push_back(triangle);
//std::cout << triangle.x << " " << triangle.y << " " << triangle.z << "\n";
}
}
objStream.close();
}
else {
std::cout << "File cannot be read or does not exist: " << objFilename;
}
}
//traverse points and calculate the middle between them
float lowestX = points[0].x;
float highestX = points[0].x;
float lowestY = points[0].y;
float highestY = points[0].y;
float lowestZ = points[0].z;
float highestZ = points[0].z;
for (int i = 0; i < points.size(); i++) {
//X points
if (points[i].x < lowestX) {
lowestX = points[i].x;
}
else if (points[i].x > highestX) {
highestX = points[i].x;
}
//Y points
if (points[i].y < lowestY) {
lowestY = points[i].y;
}
else if (points[i].y > highestY) {
highestY = points[i].y;
}
//Z points
if (points[i].z < lowestZ) {
lowestZ = points[i].z;
}
else if (points[i].z > highestZ) {
highestZ = points[i].z;
}
//calculate max distance
}
//calculate mid points
float midX = (lowestX + highestX) / 2.0f;
float midY = (lowestY + highestY) / 2.0f;
float midZ = (lowestZ + highestZ) / 2.0f;
//translate all the points and also calculate max distance
float maxDist = 0;
for (int i = 0; i < points.size(); i++) {
points[i].x -= midX;
points[i].y -= midY;
points[i].z -= midZ;
float dist = sqrt(pow(points[i].x,2) + pow(points[i].y, 2) + pow(points[i].z, 2));
if (dist > maxDist) {
maxDist = dist;
}
}
//scale all points to the max distance and scale factor accordingly
//also push these final processed points into the original points vector
for (int i = 0; i < points.size(); i++) {
points[i].x = points[i].x / maxDist * SCALE_FACTOR;
points[i].y = points[i].y / maxDist * SCALE_FACTOR;
points[i].z = points[i].z / maxDist * SCALE_FACTOR;
glm::vec3 pushPoint;
pushPoint.x = points[i].x;
pushPoint.y = points[i].y;
pushPoint.z = points[i].z;
originalPoints.push_back(pushPoint);
}
// Set the model and rotation matrix to an identity matrix.
model = glm::mat4(1);
rotation = glm::mat4(1);
// Set the color.
color = glm::vec3(1, 1, 1);
// Generate a vertex array (VAO) and a vertex buffer objects (VBO) + EBO.
glGenVertexArrays(1, &vao);
glGenBuffers(1, &vbo);
glGenBuffers(1, &vbo_n);
glGenBuffers(1, &ebo);
// Bind to the VAO.
glBindVertexArray(vao);
// Bind to the first VBO. We will use it to store the points.
glBindBuffer(GL_ARRAY_BUFFER, vbo);
// Pass in the data.
glBufferData(GL_ARRAY_BUFFER, sizeof(glm::vec3) * points.size(),
points.data(), GL_STATIC_DRAW);
// Enable vertex attribute 0.
// We will be able to access points through it.
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), 0);
//Send normal data to gpu
glBindBuffer(GL_ARRAY_BUFFER, vbo_n);
glBufferData(GL_ARRAY_BUFFER, sizeof(glm::vec3) * normals.size(), normals.data(), GL_STATIC_DRAW);
//Bind to the EBO
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
//Send the information to the gpu
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(glm::uvec3) * triangles.size(), triangles.data(), GL_STATIC_DRAW);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), 0);
// Unbind from the VBO.
glBindBuffer(GL_ARRAY_BUFFER, 0);
// Unbind from the VAO.
glBindVertexArray(0);
}
PointCloud::~PointCloud()
{
// Delete the VBO and the VAO.
glDeleteBuffers(1, &vbo);
glDeleteVertexArrays(1, &vao);
}
void PointCloud::draw()
{
// Bind to the VAO.
glBindVertexArray(vao);
// Draw the model
glDrawElements(GL_TRIANGLES, triangles.size()*3, GL_UNSIGNED_INT, 0);
// Unbind from the VAO.
glBindVertexArray(0);
}
void PointCloud::update()
{
}
float PointCloud::getLocalSpinCounter() {
return localSpinCounter;
}
void PointCloud::setLocalSpinCounter(float s) {
localSpinCounter = s;
}
std::vector<glm::vec3>* PointCloud::getPoints() {
return &originalPoints;
}
void PointCloud::updatePointSize(GLfloat size)
{
pointSize = size;
}
void PointCloud::spin(float deg)
{
// Update the model matrix by multiplying a rotation matrix
model = glm::rotate(model, glm::radians(deg), glm::vec3(0.0f, 1.0f, 0.0f));
}
void PointCloud::rotateAboutAxis(float deg, glm::vec3 axis) {
model = Window::rotateAboutAxis(deg, axis, model);
}
void PointCloud::randomizePoints() {
randomAnimPlaying = true;
animationStartTime = glfwGetTime();
//randomize points depending on the scale factor
for (int i = 0; i < points.size(); i++) {
float r = SCALE_FACTOR * 2 * rand() / (float)RAND_MAX - SCALE_FACTOR;
points[i].x = r;
r = SCALE_FACTOR * 2 * rand() / (float)RAND_MAX - SCALE_FACTOR;
points[i].y = r;
r = SCALE_FACTOR * 2 * rand() / (float)RAND_MAX - SCALE_FACTOR;
points[i].z = r;
}
}
void PointCloud::setPoints(std::vector<glm::vec3>* p) {
randomAnimPlaying = true;
animationStartTime = glfwGetTime();
for (int i = 0; i < points.size(); i++) {
points[i] = p->at(i);
}
}
void PointCloud::returnPointsToOriginal() {
for (int i = 0; i < points.size(); i++) {
points[i] = originalPoints[i];
}
}
//Interpolates the points based on a smooth cosine function
void PointCloud::interpolatePoints(float t) {
for (int i = 0; i < points.size(); i++) {
//another method is to use a cosine smooth interpolation function
//COSINE SMOOTH
/*
points[i].x = lerp(points[i].x, originalPoints[i].x, -(cos(glm::pi<float>() * t)/2) + 0.5f);
points[i].y = lerp(points[i].y, originalPoints[i].y, -(cos(glm::pi<float>() * t) / 2) + 0.5f);
points[i].z = lerp(points[i].z, originalPoints[i].z, -(cos(glm::pi<float>() * t) / 2) + 0.5f);
*/
//DECELERATION SMOOTH
points[i].x = lerp(points[i].x, originalPoints[i].x, 1 - ((1 - t) * (1 - t)));
points[i].y = lerp(points[i].y, originalPoints[i].y, 1 - ((1 - t) * (1 - t)));
points[i].z = lerp(points[i].z, originalPoints[i].z, 1 - ((1 - t) * (1 - t)));
}
}
float PointCloud::lerp(float p1, float p2, float t) {
return p1 + t * (p2 - p1);
}
void PointCloud::updateBuffer() {
glBindBuffer(GL_ARRAY_BUFFER, vbo);
float* loc = (float*)glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY);
if (loc) {
//edit the points here
for (int i = 0; i < points.size() * 3; i+=3) {
loc[i] = points[i/3].x;
loc[i+1] = points[i/3].y;
loc[i+2] = points[i/3].z;
}
glUnmapBuffer(GL_ARRAY_BUFFER);
}
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
void PointCloud::changeScale(double delta) {
scale = 1 - delta;
model = glm::scale(model, glm::vec3(scale, scale, scale));
}