-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcube.cpp
More file actions
143 lines (88 loc) · 2.87 KB
/
cube.cpp
File metadata and controls
143 lines (88 loc) · 2.87 KB
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
#include "cube.h"
Cube::Cube()
{
createVertices();
}
Cube::~Cube()
{
cube_vertices.clear();
cube_indices.clear();
}
glm::mat4 Cube::GetModel()
{
return cube_model;
}
void Cube::createVertices() {
cube_vertices = {
{{-1.0f, 1.0f, -1.0f}, {1.0f, 1.0f, 1.0f}},
{{1.0f, 1.0f, -1.0f}, {1.0f, 1.0f, 0.0f}},
{{-1.0f, -1.0f, -1.0f}, {1.0f, 0.0f, 1.0f}},
{{1.0f, -1.0f, -1.0f}, {1.0f, 0.0f, 0.0f}},
{{-1.0f, 1.0f, 1.0f}, {0.0f, 1.0f, 1.0f}},
{{1.0f, 1.0f, 1.0f}, {0.0f, 1.0f, 0.0f}},
{{-1.0f, -1.0f, 1.0f}, {0.0f, 0.0f, 1.0f}},
{{1.0f, -1.0f, 1.0f}, {0.0f, 0.0f, 0.0f}},
};
cube_indices = {
0, 1, 2,
2,3,1,
1,5,7,
7,3,1,
5,4,6,
6,7,5,
4,0,2,
2,6,4,
5,4,0,
0,1,5,
2,6,7,
2,7,3,
};
}
void Cube::Initialize(GLint posAttribLoc, GLint colAttribLoc) {
// Set up your VOA
glGenVertexArrays(1, &cube_vao);
glBindVertexArray(cube_vao);
// setting the Vertex VBO
glGenBuffers(1, &cube_VB);
glBindBuffer(GL_ARRAY_BUFFER, cube_VB);
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex) * cube_vertices.size(), &cube_vertices[0], GL_STATIC_DRAW);
glVertexAttribPointer(posAttribLoc, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), 0);
glVertexAttribPointer(colAttribLoc, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, color));
// Setting the Index VBO
glGenBuffers(1, &cube_IB);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, cube_IB);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned int) * cube_indices.size(), &cube_indices[0], GL_STATIC_DRAW);
// Computing the model matrix
// Model orientation
angle = 1 * 3.1415f;
float tvec1 = glm::linearRand(-5.f, 5.f);
float tvec2 = glm::linearRand(-5.f, 5.f);
float tvec3 = glm::linearRand(-2.f, 2.f);
cube_model = glm::translate(glm::mat4(1.0f), glm::vec3(tvec1, tvec2, tvec3));
cube_model *= glm::rotate(glm::mat4(1.0f), angle, glm::vec3(0, 1.0f, .0f));
}
void Cube::Update(unsigned int dt)
{
cube_model = glm::translate(cube_model, cube_m_speed);
}
void Cube::Render(GLint posAttribLoc, GLint colAttribLoc)
{
// Bind VAO
glBindVertexArray(cube_vao);
// Bind VBO(s)
glBindBuffer(GL_ARRAY_BUFFER, cube_VB);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, cube_IB);
// enable the vertex attribute arrays
// this is the poistion attrib in the vertex shader
glEnableVertexAttribArray(posAttribLoc);
// this is the color attribe in the vertex shader
glEnableVertexAttribArray(colAttribLoc);
// Draw call to OpenGL
glDrawElements(GL_TRIANGLES, cube_indices.size(), GL_UNSIGNED_INT, 0);
// disable the vertex attributes
glDisableVertexAttribArray(posAttribLoc);
glDisableVertexAttribArray(colAttribLoc);
// unbind VBO(s) and ElementBuffer(s)
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}