-
Notifications
You must be signed in to change notification settings - Fork 1
/
object.cpp
176 lines (141 loc) · 4.83 KB
/
object.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include "object.h"
#include "graphics.h"
Object::Object()
{
// Vertex Set Up
setupVerticies();
// Buffer Set Up
if (!InitBuffers()) {
printf("Some buffers not initialized.\n");
}
}
Object::Object(const std::string& fname, const std::string& nMapname)
{
// Vertex Set Up
setupVerticies();
// Buffer Set Up
if (!InitBuffers()) {
printf("Some buffers not initialized.\n");
}
m_texture = nullptr;
m_normalMap = nullptr;
if (fname.length() > 0) {
m_texture = new Texture(fname.c_str());
}
if (nMapname.length() > 0) {
m_normalMap = new Texture(nMapname.c_str());
}
}
Object::~Object()
{
Vertices.clear();
Indices.clear();
}
void Object::Render(GLint posAttribLoc, GLint colAttribLoc)
{
glBindVertexArray(vao);
// Enable vertex attibute arrays for each vertex attrib
glEnableVertexAttribArray(posAttribLoc);
glEnableVertexAttribArray(colAttribLoc);
// Bind your VBO
glBindBuffer(GL_ARRAY_BUFFER, VB);
// Set vertex attribute pointers to the load correct data
glVertexAttribPointer(posAttribLoc, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), 0);
glVertexAttribPointer(colAttribLoc, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, normal));
glVertexAttribPointer(colAttribLoc, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, texcoord));
glVertexAttribPointer(colAttribLoc, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, tangent));
glVertexAttribPointer(colAttribLoc, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, bitangent));
// Bind your Element Array
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IB);
// Render
glDrawElements(GL_TRIANGLES, Indices.size(), GL_UNSIGNED_INT, 0);
// Disable vertex arrays
glDisableVertexAttribArray(posAttribLoc);
glDisableVertexAttribArray(colAttribLoc);
}
void Object::Render() {
glBindVertexArray(vao);
Graphics* graphics = Graphics::getInstance();
// Enable vertex attibute arrays for each vertex attrib
glEnableVertexAttribArray(graphics->getPositionAttribute());
glEnableVertexAttribArray(graphics->getNormalAttribute());
glEnableVertexAttribArray(graphics->getTextCoordAttribute());
// Bind your VBO
glBindBuffer(GL_ARRAY_BUFFER, VB);
// Set vertex attribute pointers to the load correct data. Update here to load the correct attributes.
glVertexAttribPointer(graphics->getPositionAttribute(), 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), 0);
glVertexAttribPointer(graphics->getNormalAttribute(), 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, normal));
glVertexAttribPointer(graphics->getTextCoordAttribute(), 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, texcoord));
// If has texture, set up texture unit(s): update here for texture rendering
glUniform1i(graphics->getHasTextureAttribute(), hasTex());
glUniform1i(graphics->getIsEmissiveAttribute(), false);
// Bind your Element Array
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IB);
// Render
glDrawElements(GL_TRIANGLES, Indices.size(), GL_UNSIGNED_INT, 0);
// Disable vertex arrays
glDisableVertexAttribArray(graphics->getPositionAttribute());
glDisableVertexAttribArray(graphics->getNormalAttribute());
glDisableVertexAttribArray(graphics->getTextCoordAttribute());
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
bool Object::InitBuffers() {
// For OpenGL 3
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
glGenBuffers(1, &VB);
glBindBuffer(GL_ARRAY_BUFFER, VB);
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex) * Vertices.size(), &Vertices[0], GL_STATIC_DRAW);
glGenBuffers(1, &IB);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IB);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned int) * Indices.size(), &Indices[0], GL_STATIC_DRAW);
return true;
}
void Object::setupVerticies() {
Vertices = {
{{1.0f, -1.0f, -1.0f}, {0.0f, 0.0f, 0.0f}, {0,0.}, {1.,1.,1.}, {1.,1.,1.}},
{{1.0f, -1.0f, 1.0f}, {1.0f, 0.0f, 0.0f}, {1.,0.}, {1.,1.,1.}, {1.,1.,1.}},
{{-1.0f, -1.0f, 1.0f}, {0.0f, 1.0f, 0.0f}, {1.,1.}, {1.,1.,1.}, {1.,1.,1.}},
{{-1.0f, -1.0f, -1.0f}, {0.0f, 0.0f, 1.0f}, {0.,1.}, {1.,1.,1.}, {1.,1.,1.}},
{{1.0f, 1.0f, -1.0f}, {1.0f, 1.0f, 0.0f}, {0.,0.}, {1.,1.,1.}, {1.,1.,1.}},
{{1.0f, 1.0f, 1.0f}, {1.0f, 0.0f, 1.0f}, {1.,0.}, {1.,1.,1.}, {1.,1.,1.}},
{{-1.0f, 1.0f, 1.0f}, {0.0f, 1.0f, 1.0f}, {1.,1.}, {1.,1.,1.}, {1.,1.,1.}},
{{-1.0f, 1.0f, -1.0f}, {1.0f, 1.0f, 1.0f}, {0.,0.}, {1.,1.,1.}, {1.,1.,1.}}
};
Indices = {
1, 2, 3,
7, 6, 5,
0, 4, 5,
1, 5, 6,
6, 7, 3,
0, 3, 7,
0, 1, 3,
4, 7, 5,
1, 0, 5,
2, 1, 6,
2, 6, 3,
4, 0, 7
};
}
bool Object::hasTex() {
return m_texture != nullptr;
}
bool Object::hasNormalMap()
{
return m_normalMap != nullptr;
}
GLuint Object::getTextureID() {
return m_texture->getTextureID();
}
GLuint Object::getNormalMapID()
{
return m_normalMap->getTextureID();
}
bool Object::getIsEmissive() const
{
return isEmissive;
}
void Object::setIsEmissive(bool newValue)
{
isEmissive = newValue;
}