-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObject.cpp
More file actions
253 lines (207 loc) · 9.21 KB
/
Object.cpp
File metadata and controls
253 lines (207 loc) · 9.21 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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#include "Object.h"
#include <fstream>
#include <sstream>
#include <iostream>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
Object::Object(GLuint shaderProgram, const char* objPath,
const std::vector<const char*>& texturePaths,
const std::vector<MaterialProperties>& matProperties,
float _xPos, float _yPos, float _zPos, float _scale, float _angle, int axis)
: shaderProgram(shaderProgram), xPos(_xPos), yPos(_yPos), zPos(_zPos),
scale(_scale), angle(_angle), axis(axis), name(objPath), materials(matProperties) {
if (!LoadOBJ(objPath)) {
throw std::runtime_error("Failed to load OBJ file!");
}
model = glm::mat4(1.0f);
model = glm::translate(model, glm::vec3(xPos, yPos, zPos));
glm::vec3 rotation_axis(0.0f, 1.0f, 0.0f);
if (axis == 0) rotation_axis = glm::vec3(1.0f, 0.0f, 0.0f);
if (axis == 2) rotation_axis = glm::vec3(0.0f, 0.0f, 1.0f);
model = glm::rotate(model, angle, rotation_axis);
model = glm::scale(model, glm::vec3(scale));
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(Vertex), vertices.data(), GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, position));
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, texture_coord));
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, normal));
textures.resize(texturePaths.size());
for (size_t i = 0; i < texturePaths.size(); i++) {
if (!LoadTexture(texturePaths[i], textures[i])) {
throw std::runtime_error("Failed to load texture: " + std::string(texturePaths[i]));
}
}
}
// Parser de .obj, se há faces que utilizam textura precisa de um "usemtl" antes da definição delas
bool Object::LoadOBJ(const char* path) {
std::vector<glm::vec3> temp_vertices;
std::vector<glm::vec2> temp_texcoords;
std::vector<glm::vec3> temp_normals;
std::string current_material;
size_t vertex_count = 0;
std::ifstream file(path);
if (!file) {
std::cerr << "Cannot open file: " << path << std::endl;
return false;
}
std::string line;
while (std::getline(file, line)) {
std::istringstream iss(line);
std::string type;
iss >> type;
if (type == "v") {
glm::vec3 vertex;
iss >> vertex.x >> vertex.y >> vertex.z;
temp_vertices.push_back(vertex);
}
else if (type == "vt") {
glm::vec2 tex;
iss >> tex.x >> tex.y;
temp_texcoords.push_back(tex);
}
else if (type == "vn") {
glm::vec3 normal;
iss >> normal.x >> normal.y >> normal.z;
temp_normals.push_back(normal);
}
else if (type == "usemtl") {
if (!current_material.empty()) {
materialGroups.back().second.second = vertex_count - materialGroups.back().second.first;
}
iss >> current_material;
std::cout << current_material << " vertice inicial = " << vertex_count << std::endl;
materialGroups.push_back({current_material, {vertex_count, 0}});
}
else if (type == "f") {
std::string v1, v2, v3;
iss >> v1 >> v2 >> v3;
auto process_vertex = [&](const std::string& v) {
std::stringstream ss(v);
std::string index_str;
std::vector<int> indices;
while (std::getline(ss, index_str, '/')) {
indices.push_back(!index_str.empty() ? std::stoi(index_str) : 0);
}
Vertex vertex;
vertex.position = temp_vertices[indices[0] - 1];
vertex.texture_coord = indices[1] > 0 ? temp_texcoords[indices[1] - 1] : glm::vec2(0.0f);
vertex.normal = indices[2] > 0 ? temp_normals[indices[2] - 1] : glm::vec3(0.0f, 1.0f, 0.0f);
vertices.push_back(vertex);
vertex_count++;
};
process_vertex(v1);
process_vertex(v2);
process_vertex(v3);
}
}
if (!current_material.empty()) {
materialGroups.back().second.second = vertex_count - materialGroups.back().second.first;
}
return true;
}
bool Object::LoadTexture(const char* path, GLuint& texture) {
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
int width, height, channels;
stbi_set_flip_vertically_on_load(true);
unsigned char* data = stbi_load(path, &width, &height, &channels, 0);
if (data) {
std::cout << path << " ";
if (channels == 4) {
std::cout << "RGBA" << std::endl;
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
} else {
std::cout << "RGB" << std::endl;
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
}
stbi_image_free(data);
return true;
}
std::cerr << "Failed to load texture: " << path << std::endl;
return false;
}
void Object::Draw(bool mesh_active) {
glUseProgram(shaderProgram);
model = glm::mat4(1.0f);
model = glm::translate(model, glm::vec3(xPos, yPos, zPos));
glm::vec3 rotation_axis(0.0f, 1.0f, 0.0f);
if (axis == 0) rotation_axis = glm::vec3(1.0f, 0.0f, 0.0f);
if (axis == 2) rotation_axis = glm::vec3(0.0f, 0.0f, 1.0f);
model = glm::rotate(model, angle, rotation_axis);
model = glm::scale(model, glm::vec3(scale));
GLint loc_model = glGetUniformLocation(shaderProgram, "model");
glUniformMatrix4fv(loc_model, 1, GL_FALSE, glm::value_ptr(model));
glBindVertexArray(vao);
for (size_t i = 0; i < materialGroups.size(); i++) {
GLint emissionLoc = glGetUniformLocation(shaderProgram, "material.emission");
GLint shininessLoc = glGetUniformLocation(shaderProgram, "material.shininess");
GLint isLightSourceLoc = glGetUniformLocation(shaderProgram, "material.isLightSource");
GLint constantLoc = glGetUniformLocation(shaderProgram, "material.constant");
GLint linearLoc = glGetUniformLocation(shaderProgram, "material.linear");
GLint quadraticLoc = glGetUniformLocation(shaderProgram, "material.quadratic");
GLint cutOffLoc = glGetUniformLocation(shaderProgram, "material.cutOff");
GLint outerCutOffLoc = glGetUniformLocation(shaderProgram, "material.outerCutOff");
GLint directionLoc = glGetUniformLocation(shaderProgram, "material.direction");
GLint isActiveLoc = glGetUniformLocation(shaderProgram, "material.isActive");
GLint diffuseReflectionLoc = glGetUniformLocation(shaderProgram, "material.diffuseReflection");
GLint specularReflectionLoc = glGetUniformLocation(shaderProgram, "material.specularReflection");
// esses são ignorados no shader caso não seja fonte de luz
const auto& mat = materials[i];
glUniform3fv(emissionLoc, 1, glm::value_ptr(mat.emission));
glUniform1f(shininessLoc, mat.shininess);
glUniform1i(isLightSourceLoc, mat.isLightSource);
glUniform1f(constantLoc, mat.constant);
glUniform1f(linearLoc, mat.linear);
glUniform1f(quadraticLoc, mat.quadratic);
glUniform1f(cutOffLoc, mat.cutOff);
glUniform1f(outerCutOffLoc, mat.outerCutOff);
glUniform3fv(directionLoc, 1, glm::value_ptr(mat.direction));
glUniform1i(isActiveLoc, mat.isActive);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, textures[i]);
GLint loc_texture = glGetUniformLocation(shaderProgram, "material.diffuse");
glUniform1i(loc_texture, 0);
glUniform3fv(diffuseReflectionLoc, 1, glm::value_ptr(mat.diffuseReflection));
glUniform3fv(specularReflectionLoc, 1, glm::value_ptr(mat.specularReflection));
const auto& group = materialGroups[i];
glDrawArrays(GL_TRIANGLES, group.second.first, group.second.second);
}
}
void Object::Move(float dx, float dy, float dz) {
xPos += dx;
yPos += dy;
zPos += dz;
}
void Object::Scale(float factor) {
scale -= factor;
}
void Object::Rotate(float angle_delta) {
angle += angle_delta;
}
void Object::ToggleLights() {
for (auto& mat : materials) {
if (mat.isLightSource) {
mat.isActive = !mat.isActive;
}
}
}
glm::mat4 Object::GetModelMatrix() {
return model;
}
Object::~Object() {
if (vao) glDeleteVertexArrays(1, &vao);
if (vbo) glDeleteBuffers(1, &vbo);
if (!textures.empty()) glDeleteTextures(textures.size(), textures.data());
}