-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathPointCloud.cpp
102 lines (82 loc) · 2.71 KB
/
PointCloud.cpp
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
#include "PointCloud.h"
PointCloud::PointCloud(std::string objFilename, GLfloat pointSize)
: pointSize(pointSize)
{
/*
* TODO: Section 2: Currently, all the points are hard coded below.
* Modify this to read points from an obj file.
*/
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)
};
/*
* TODO: Section 4, you will need to normalize the object to fit in the
* screen.
*/
// Set the model matrix to an identity matrix.
model = glm::mat4(1);
// Set the color.
color = glm::vec3(1, 0, 0);
// Generate a Vertex Array (VAO) and Vertex Buffer Object (VBO)
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
// Bind VAO
glBindVertexArray(VAO);
// Bind VBO to the bound VAO, and store the point data
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(glm::vec3) * points.size(), points.data(), GL_STATIC_DRAW);
// Enable Vertex Attribute 0 to pass point data through to the shader
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), 0);
// Unbind the VBO/VAO
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
}
PointCloud::~PointCloud()
{
// Delete the VBO and the VAO.
glDeleteBuffers(1, &VBO);
glDeleteVertexArrays(1, &VAO);
}
void PointCloud::draw(const glm::mat4& view, const glm::mat4& projection, GLuint shader)
{
// Actiavte the shader program
glUseProgram(shader);
// Get the shader variable locations and send the uniform data to the shader
glUniformMatrix4fv(glGetUniformLocation(shader, "view"), 1, false, glm::value_ptr(view));
glUniformMatrix4fv(glGetUniformLocation(shader, "projection"), 1, false, glm::value_ptr(projection));
glUniformMatrix4fv(glGetUniformLocation(shader, "model"), 1, GL_FALSE, glm::value_ptr(model));
glUniform3fv(glGetUniformLocation(shader, "color"), 1, glm::value_ptr(color));
// Bind the VAO
glBindVertexArray(VAO);
// Set point size
glPointSize(pointSize);
// Draw the points
glDrawArrays(GL_POINTS, 0, points.size());
// Unbind the VAO and shader program
glBindVertexArray(0);
glUseProgram(0);
}
void PointCloud::update()
{
// Spin the cube by 1 degree
spin(0.1f);
}
void PointCloud::updatePointSize(GLfloat size)
{
/*
* TODO: Section 3: Implement this function to adjust the point size.
*/
}
void PointCloud::spin(float deg)
{
// Update the model matrix by multiplying a rotation matrix
model = model * glm::rotate(glm::radians(deg), glm::vec3(0.0f, 1.0f, 0.0f));
}