-
Notifications
You must be signed in to change notification settings - Fork 3
/
Cube.cpp
83 lines (72 loc) · 4.12 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
#include "Cube.h"
#include "Window.h"
Cube::Cube()
{
toWorld = glm::mat4(1.0f);
// Create array object and buffers. Remember to delete your buffers when the object is destroyed!
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glGenBuffers(1, &EBO);
// Bind the Vertex Array Object (VAO) first, then bind the associated buffers to it.
// Consider the VAO as a container for all your buffers.
glBindVertexArray(VAO);
// Now bind a VBO to it as a GL_ARRAY_BUFFER. The GL_ARRAY_BUFFER is an array containing relevant data to what
// you want to draw, such as vertices, normals, colors, etc.
glBindBuffer(GL_ARRAY_BUFFER, VBO);
// glBufferData populates the most recently bound buffer with data starting at the 3rd argument and ending after
// the 2nd argument number of indices. How does OpenGL know how long an index spans? Go to glVertexAttribPointer.
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
// Enable the usage of layout location 0 (check the vertex shader to see what this is)
glEnableVertexAttribArray(0);
glVertexAttribPointer(0,// This first parameter x should be the same as the number passed into the line "layout (location = x)" in the vertex shader. In this case, it's 0. Valid values are 0 to GL_MAX_UNIFORM_LOCATIONS.
3, // This second line tells us how any components there are per vertex. In this case, it's 3 (we have an x, y, and z component)
GL_FLOAT, // What type these components are
GL_FALSE, // GL_TRUE means the values should be normalized. GL_FALSE means they shouldn't
3 * sizeof(GLfloat), // Offset between consecutive indices. Since each of our vertices have 3 floats, they should have the size of 3 floats in between
(GLvoid*)0); // Offset of the first vertex's component. In our case it's 0 since we don't pad the vertices array with anything.
// We've sent the vertex data over to OpenGL, but there's still something missing.
// In what order should it draw those vertices? That's why we'll need a GL_ELEMENT_ARRAY_BUFFER for this.
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
// Unbind the currently bound buffer so that we don't accidentally make unwanted changes to it.
glBindBuffer(GL_ARRAY_BUFFER, 0);
// Unbind the VAO now so we don't accidentally tamper with it.
// NOTE: You must NEVER unbind the element array buffer associated with a VAO!
glBindVertexArray(0);
}
Cube::~Cube()
{
// Delete previously generated buffers. Note that forgetting to do this can waste GPU memory in a
// large project! This could crash the graphics driver due to memory leaks, or slow down application performance!
glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1, &VBO);
glDeleteBuffers(1, &EBO);
}
void Cube::draw(GLuint shaderProgram)
{
// Calculate the combination of the model and view (camera inverse) matrices
glm::mat4 modelview = Window::V * toWorld;
// We need to calcullate this because modern OpenGL does not keep track of any matrix other than the viewport (D)
// Consequently, we need to forward the projection, view, and model matrices to the shader programs
// Get the location of the uniform variables "projection" and "modelview"
uProjection = glGetUniformLocation(shaderProgram, "projection");
uModelview = glGetUniformLocation(shaderProgram, "modelview");
// Now send these values to the shader program
glUniformMatrix4fv(uProjection, 1, GL_FALSE, &Window::P[0][0]);
glUniformMatrix4fv(uModelview, 1, GL_FALSE, &modelview[0][0]);
// Now draw the cube. We simply need to bind the VAO associated with it.
glBindVertexArray(VAO);
// Tell OpenGL to draw with triangles, using 36 indices, the type of the indices, and the offset to start from
glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, 0);
// Unbind the VAO when we're done so we don't accidentally draw extra stuff or tamper with its bound buffers
glBindVertexArray(0);
}
void Cube::update()
{
spin(1.0f);
}
void Cube::spin(float deg)
{
// If you haven't figured it out from the last project, this is how you fix spin's behavior
toWorld = toWorld * glm::rotate(glm::mat4(1.0f), 1.0f / 180.0f * glm::pi<float>(), glm::vec3(0.0f, 1.0f, 0.0f));
}