-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cube.cpp
118 lines (97 loc) · 3.98 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
#include "Cube.h"
#include "Window.h"
GLfloat vertices[] = {
// Front vertices
-0.5, -0.5, 0.5,
0.5, -0.5, 0.5,
0.5, 0.5, 0.5,
-0.5, 0.5, 0.5,
// Back vertices
-0.5, -0.5, -0.5,
0.5, -0.5, -0.5,
0.5, 0.5, -0.5,
-0.5, 0.5, -0.5
};
GLuint indices[] = { // Note that we start from 0!
// Front face
0, 1, 2,
2, 3, 0,
// Top face
1, 5, 6,
6, 2, 1,
// Back face
7, 6, 5,
5, 4, 7,
// Bottom face
4, 0, 3,
3, 7, 4,
// Left face
4, 5, 1,
1, 0, 4,
// Right face
3, 2, 6,
6, 7, 3
};
Cube::Cube()
{
this->toWorld = glm::mat4(1.0f);
this->angle = 0.0f;
// Create buffers/arrays
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glGenBuffers(1, &EBO);
// Bind the Vertex Array Object first, then bind and set vertex buffer(s) and attribute pointer(s).
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
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 vertex attributes. 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.
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0); // Note that this is allowed, the call to glVertexAttribPointer registered VBO as the currently bound vertex buffer object so afterwards we can safely unbind
glBindVertexArray(0); // Unbind VAO (it's always a good thing to unbind any buffer/array to prevent strange bugs), remember: do NOT unbind the EBO, keep it bound to this VAO
}
Cube::~Cube()
{
// Properly de-allocate all resources once they've outlived their purpose
glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1, &VBO);
glDeleteBuffers(1, &EBO);
}
void Cube::draw(GLuint shaderProgram)
{
// Calculate combination of the model (toWorld), view (camera inverse), and perspective matrices
//glm::mat4 MVP = Window::P * Window::V * toWorld;
// We need to calculate this because as of GLSL version 1.40 (OpenGL 3.1, released March 2009), gl_ModelViewProjectionMatrix has been
// removed from the language. The user is expected to supply this matrix to the shader when using modern OpenGL.
//GLuint MatrixID = glGetUniformLocation(shaderProgram, "MVP");
//glUniformMatrix4fv(MatrixID, 1, GL_FALSE, &MVP[0][0]);
glUseProgram(shaderProgram);
GLuint MatrixID = glGetUniformLocation(shaderProgram, "toWorld");
glUniformMatrix4fv(MatrixID, 1, GL_FALSE, &toWorld[0][0]);
GLuint MatrixID1 = glGetUniformLocation(shaderProgram, "view");
glUniformMatrix4fv(MatrixID1, 1, GL_FALSE, &Window::V[0][0]);
GLuint MatrixID2 = glGetUniformLocation(shaderProgram, "projection");
glUniformMatrix4fv(MatrixID2, 1, GL_FALSE, &Window::P[0][0]);
glBindVertexArray(VAO);
glBindTexture(GL_TEXTURE_CUBE_MAP, Window::cubemapTexture_window);
glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
}
void Cube::update()
{
spin(1.0f);
}
void Cube::spin(float deg)
{
this->angle += deg;
if (this->angle > 360.0f || this->angle < -360.0f) this->angle = 0.0f;
// This creates the matrix to rotate the cube. You'd probably want to change this with the more favorable way of performing a spin
// which you did in the previous project.
this->toWorld = glm::rotate(glm::mat4(1.0f), this->angle / 180.0f * glm::pi<float>(), glm::vec3(0.0f, 1.0f, 0.0f));
}