-
Notifications
You must be signed in to change notification settings - Fork 3
/
Cube.cpp
124 lines (106 loc) · 3.28 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
#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 vertex buffer object (VBO).
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
// Bind to the VAO.
glBindVertexArray(VAO);
// Bind VBO to the bound VAO, and store the vertex data
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(glm::vec3) * vertices.size(), vertices.data(), GL_STATIC_DRAW);
// Enable Vertex Attribute 0 to pass the vertex data through to the shader
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), 0);
// Generate EBO, bind the EBO to the bound VAO, and send the index data
glGenBuffers(1, &EBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(glm::ivec3) * indices.size(), indices.data(), GL_STATIC_DRAW);
// Unbind the VBO/VAO
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
}
Cube::~Cube()
{
// Delete the VBO/EBO and the VAO.
glDeleteBuffers(1, &VBO);
glDeleteBuffers(1, &EBO);
glDeleteVertexArrays(1, &VAO);
}
void Cube::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);
// Draw the points using triangles, indexed with the EBO
glDrawElements(GL_TRIANGLES, 36, 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));
}