-
Notifications
You must be signed in to change notification settings - Fork 2
/
Cube.cpp
184 lines (146 loc) · 4.88 KB
/
Cube.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
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
#include "Cube.h"
////////////////////////////////////////////////////////////////////////////////
Cube::Cube(glm::vec3 cubeMin, glm::vec3 cubeMax)
{
// Model matrix.
model = glm::mat4(1.0f);
// The color of the cube. Try setting it to something else!
color = glm::vec3(1.0f, 0.95f, 0.1f);
// Specify vertex positions
positions = {
// Front
glm::vec3(cubeMin.x,cubeMin.y,cubeMax.z),
glm::vec3(cubeMax.x,cubeMin.y,cubeMax.z),
glm::vec3(cubeMax.x,cubeMax.y,cubeMax.z),
glm::vec3(cubeMin.x,cubeMax.y,cubeMax.z),
// Back
glm::vec3(cubeMax.x,cubeMin.y,cubeMin.z),
glm::vec3(cubeMin.x,cubeMin.y,cubeMin.z),
glm::vec3(cubeMin.x,cubeMax.y,cubeMin.z),
glm::vec3(cubeMax.x,cubeMax.y,cubeMin.z),
// Top
glm::vec3(cubeMin.x,cubeMax.y,cubeMax.z),
glm::vec3(cubeMax.x,cubeMax.y,cubeMax.z),
glm::vec3(cubeMax.x,cubeMax.y,cubeMin.z),
glm::vec3(cubeMin.x,cubeMax.y,cubeMin.z),
// Bottom
glm::vec3(cubeMin.x,cubeMin.y,cubeMin.z),
glm::vec3(cubeMax.x,cubeMin.y,cubeMin.z),
glm::vec3(cubeMax.x,cubeMin.y,cubeMax.z),
glm::vec3(cubeMin.x,cubeMin.y,cubeMax.z),
// Left
glm::vec3(cubeMin.x,cubeMin.y,cubeMin.z),
glm::vec3(cubeMin.x,cubeMin.y,cubeMax.z),
glm::vec3(cubeMin.x,cubeMax.y,cubeMax.z),
glm::vec3(cubeMin.x,cubeMax.y,cubeMin.z),
// Right
glm::vec3(cubeMax.x,cubeMin.y,cubeMax.z),
glm::vec3(cubeMax.x,cubeMin.y,cubeMin.z),
glm::vec3(cubeMax.x,cubeMax.y,cubeMin.z),
glm::vec3(cubeMax.x,cubeMax.y,cubeMax.z)
};
// Specify normals
normals = {
// Front
glm::vec3(0,0,1),
glm::vec3(0,0,1),
glm::vec3(0,0,1),
glm::vec3(0,0,1),
// Back
glm::vec3(0,0,-1),
glm::vec3(0,0,-1),
glm::vec3(0,0,-1),
glm::vec3(0,0,-1),
// Top
glm::vec3(0,1,0),
glm::vec3(0,1,0),
glm::vec3(0,1,0),
glm::vec3(0,1,0),
// Bottom
glm::vec3(0,-1,0),
glm::vec3(0,-1,0),
glm::vec3(0,-1,0),
glm::vec3(0,-1,0),
// Left
glm::vec3(-1,0,0),
glm::vec3(-1,0,0),
glm::vec3(-1,0,0),
glm::vec3(-1,0,0),
// Right
glm::vec3(1,0,0),
glm::vec3(1,0,0),
glm::vec3(1,0,0),
glm::vec3(1,0,0)
};
// Specify indices
indices = {
0,1,2, 0,2,3, // Front
4,5,6, 4,6,7, // Back
8,9,10, 8,10,11, // Top
12,13,14, 12,14,15, // Bottom
16,17,18, 16,18,19, // Left
20,21,22, 20,22,23, // Right
};
// Generate a vertex array (VAO) and two vertex buffer objects (VBO).
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO_positions);
glGenBuffers(1, &VBO_normals);
// Bind to the VAO.
glBindVertexArray(VAO);
// Bind to the first VBO - We will use it to store the vertices
glBindBuffer(GL_ARRAY_BUFFER, VBO_positions);
glBufferData(GL_ARRAY_BUFFER, sizeof(glm::vec3) * positions.size(), positions.data(), GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), 0);
// Bind to the second VBO - We will use it to store the normals
glBindBuffer(GL_ARRAY_BUFFER, VBO_normals);
glBufferData(GL_ARRAY_BUFFER, sizeof(glm::vec3)* normals.size(), normals.data(), GL_STATIC_DRAW);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), 0);
// Generate EBO, bind the EBO to the bound VAO and send the data
glGenBuffers(1, &EBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned int) * indices.size(), indices.data(), GL_STATIC_DRAW);
// Unbind the VBOs.
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
}
////////////////////////////////////////////////////////////////////////////////
Cube::~Cube()
{
// Delete the VBOs and the VAO.
glDeleteBuffers(1, &VBO_positions);
glDeleteBuffers(1, &VBO_normals);
glDeleteBuffers(1, &EBO);
glDeleteVertexArrays(1, &VAO);
}
////////////////////////////////////////////////////////////////////////////////
void Cube::draw(const glm::mat4& viewProjMtx, GLuint shader)
{
// actiavte the shader program
glUseProgram(shader);
// get the locations and send the uniforms to the shader
glUniformMatrix4fv(glGetUniformLocation(shader, "viewProj"), 1, false, (float*)&viewProjMtx);
glUniformMatrix4fv(glGetUniformLocation(shader, "model"), 1, GL_FALSE, (float*)&model);
glUniform3fv(glGetUniformLocation(shader, "DiffuseColor"), 1, &color[0]);
// Bind the VAO
glBindVertexArray(VAO);
// draw the points using triangles, indexed with the EBO
glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, 0);
// Unbind the VAO and shader program
glBindVertexArray(0);
glUseProgram(0);
}
////////////////////////////////////////////////////////////////////////////////
void Cube::update()
{
// Spin the cube by 1 degree.
spin(0.1f);
}
////////////////////////////////////////////////////////////////////////////////
void Cube::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));
}
////////////////////////////////////////////////////////////////////////////////