-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCube.cpp
117 lines (103 loc) · 2.77 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
#include "Cube.h"
Cube::Cube(float size)
{
// Model matrix. Since the original size of the cube is 2, in order to
// have a cube of some size, we need to scale the cube by size / 2.
model = glm::scale(glm::vec3(size / 2.f));
// The color of the cube. Try setting it to something else!
color = glm::vec3(1.0f, 0.95f, 0.1f);
/*
* Cube indices used below.
* 4----7
* /| /|
* 0-+--3 |
* | 5--+-6
* |/ |/
* 1----2
*
*/
// The 8 vertices of a cube.
std::vector<glm::vec3> vertices
{
glm::vec3(-1, 1, 1),
glm::vec3(-1, -1, 1),
glm::vec3(1, -1, 1),
glm::vec3(1, 1, 1),
glm::vec3(-1, 1, -1),
glm::vec3(-1, -1, -1),
glm::vec3(1, -1, -1),
glm::vec3(1, 1, -1)
};
// Each ivec3(v1, v2, v3) define a triangle consists of vertices v1, v2
// and v3 in counter-clockwise order.
std::vector<glm::ivec3> indices
{
// Front face.
glm::ivec3(0, 1, 2),
glm::ivec3(2, 3, 0),
// Back face.
glm::ivec3(7, 6, 5),
glm::ivec3(5, 4, 7),
// Right face.
glm::ivec3(3, 2, 6),
glm::ivec3(6, 7, 3),
// Left face.
glm::ivec3(4, 5, 1),
glm::ivec3(1, 0, 4),
// Top face.
glm::ivec3(4, 0, 3),
glm::ivec3(3, 7, 4),
// Bottom face.
glm::ivec3(1, 5, 6),
glm::ivec3(6, 2, 1),
};
// Generate a vertex array (VAO) and two vertex buffer objects (VBO).
glGenVertexArrays(1, &vao);
glGenBuffers(2, vbos);
// Bind to the VAO.
glBindVertexArray(vao);
// Bind to the first VBO. We will use it to store the vertices.
glBindBuffer(GL_ARRAY_BUFFER, vbos[0]);
// Pass in the data.
glBufferData(GL_ARRAY_BUFFER, sizeof(glm::vec3) * vertices.size(),
vertices.data(), GL_STATIC_DRAW);
// Enable vertex attribute 0.
// We will be able to access vertices through it.
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 indices.
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbos[1]);
// Pass in the data.
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(glm::ivec3) * indices.size(),
indices.data(), GL_STATIC_DRAW);
// Unbind from the VBOs.
glBindBuffer(GL_ARRAY_BUFFER, 0);
// Unbind from the VAO.
glBindVertexArray(0);
}
Cube::~Cube()
{
// Delete the VBOs and the VAO.
glDeleteBuffers(2, vbos);
glDeleteVertexArrays(1, &vao);
}
void Cube::draw()
{
// Bind to the VAO.
glBindVertexArray(vao);
// Draw triangles using the indices in the second VBO, which is an
// elemnt array buffer.
glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, 0);
// Unbind from the VAO.
glBindVertexArray(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 = glm::rotate(model, glm::radians(deg), glm::vec3(0.0f, 1.0f, 0.0f));
}