-
Notifications
You must be signed in to change notification settings - Fork 0
/
G_Cuboid.cpp
91 lines (69 loc) · 2.58 KB
/
G_Cuboid.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
#include <GL/glew.h>
#include <string>
#include "Util.hpp"
#include "G_Cuboid.hpp"
#include "Shader.hpp"
/* Graphical data of cuboid
*/
G_Cuboid::G_Cuboid(
const fv* vertexData, std::string vertPath, std::string fragPath) :
vertex_data(vertexData), shader(vertPath.c_str(),fragPath.c_str()) {
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
}
// for now since I don't care about colour I bind the
// positional data as the colour data
void G_Cuboid::bindBuffers() const {
const fv& vertices = *vertex_data;
// Bind the Vertex Array Object first, then bind and set vertex bufer(s) and attribute pointer(s).
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * vertices.size(), vertices.data(), GL_STATIC_DRAW);
// Position attribute
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid*)0);
glEnableVertexAttribArray(0);
// Colour attribute
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid*)0);
glEnableVertexAttribArray(1);
glBindVertexArray(0); // Unbind VAO
}
GLint G_Cuboid::shaderProgram() const {
return shader.Program;
}
void G_Cuboid::useShader() const {
shader.use();
}
int G_Cuboid::drawSize() const {
return vertex_data->size();
}
G_Cuboid::~G_Cuboid() {
glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1, &VBO);
}
G_Cuboid_Color::G_Cuboid_Color(const fv* vertexData,
std::string vertPath, std::string fragPath,
const fv* color_data) :
G_Cuboid(vertexData, vertPath, fragPath),
color_data(color_data) {
glGenBuffers(1, &VBO_color);
}
void G_Cuboid_Color::bindBuffers() const {
const fv& vertices = *vertex_data;
const fv& colors = *color_data;
// Bind the Vertex Array Object first, then bind and set vertex bufer(s) and attribute pointer(s).
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * vertices.size(), vertices.data(), GL_STATIC_DRAW);
// Position attribute
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid*)0);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, VBO_color);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * colors.size(), colors.data(), GL_STATIC_DRAW);
// Colour attribute
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid*)0);
glEnableVertexAttribArray(1);
glBindVertexArray(0); // Unbind VAO
}
G_Cuboid_Color::~G_Cuboid_Color() {
glDeleteBuffers(1, &VBO_color);
}