Skip to content

Commit 4496821

Browse files
committed
Actualización a las clases Model y Camera.
1 parent 69512cc commit 4496821

File tree

2 files changed

+80
-74
lines changed

2 files changed

+80
-74
lines changed

Common/OpenGLCamera.hpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,11 @@ class OpenGLCamera {
7777
int width, height;
7878
glfwGetWindowSize(window, &width, &height);
7979

80-
return glm::perspective(45.0f, static_cast<float>(width) / static_cast<float>(height), 0.1f, 100.0f);
80+
return glm::perspective(45.0f, static_cast<float>(width) / static_cast<float>(height), 0.1f, 1000.0f);
81+
}
82+
83+
glm::vec3 getPosition() {
84+
return cameraPos;
8185
}
8286
};
8387

Common/OpenGLModel.hpp

Lines changed: 75 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
#include <GL\GL.h>
55
#include <GL\glew.h>
66

7-
#include <assimp\Importer.hpp> // C++ importer interface
8-
#include <assimp\scene.h> // Output data structure
9-
#include <assimp\postprocess.h> // Post processing flags
7+
#include <assimp\Importer.hpp>
8+
#include <assimp\scene.h>
9+
#include <assimp\postprocess.h>
1010

1111
#include <glm\glm.hpp>
1212
#include <glm\gtc\type_ptr.hpp>
@@ -31,7 +31,10 @@ class Model {
3131

3232
public:
3333
Mesh(const aiMesh* mesh, Model* model) :
34-
buffer{ 0 }, vao{ 0 } {
34+
buffer{ 0 }, vao{ 0 },
35+
texture_diffuse{ 0 },
36+
texture_specular{ 0 },
37+
texture_ambient{ 0 } {
3538
this->model = model;
3639
load(mesh);
3740
create();
@@ -50,41 +53,36 @@ class Model {
5053
};
5154

5255
void draw(GLuint program) {
53-
54-
//glActiveTexture(GL_TEXTURE0);
55-
//glBindTexture(GL_TEXTURE_2D, textures[aiTextureType_AMBIENT]);
56-
//glUniform1i(glGetUniformLocation(program, "texture_ambient"), 0);
57-
58-
//glActiveTexture(GL_TEXTURE1);
59-
//glBindTexture(GL_TEXTURE_2D, textures[aiTextureType_DIFFUSE]);
60-
//glUniform1i(glGetUniformLocation(program, "texture_diffuse"), 1);
61-
62-
//glActiveTexture(GL_TEXTURE2);
63-
//glBindTexture(GL_TEXTURE_2D, textures[aiTextureType_SPECULAR]);
64-
//glUniform1i(glGetUniformLocation(program, "texture_specular"), 2);
65-
66-
glBindVertexArray(vao);
67-
68-
glUniform3fv(glGetUniformLocation(program, "material.ambient"), 1, color_ambient);
69-
glUniform3fv(glGetUniformLocation(program, "material.diffuse"), 1, color_diffuse);
70-
glUniform3fv(glGetUniformLocation(program, "material.specular"), 1, color_specular);
71-
glUniform3fv(glGetUniformLocation(program, "material.emissive"), 1, color_emissive);
56+
activeTextureNum(0, texture_diffuse, program, "material.diffuse");
57+
activeTextureNum(1, texture_specular, program, "material.specular");
58+
activeTextureNum(2, texture_ambient, program, "material.ambient");
59+
60+
//glUniform3fv(glGetUniformLocation(program, "material.ambient"), 1, color_ambient);
61+
//glUniform3fv(glGetUniformLocation(program, "material.diffuse"), 1, color_diffuse);
62+
//glUniform3fv(glGetUniformLocation(program, "material.specular"), 1, color_specular);
63+
//glUniform3fv(glGetUniformLocation(program, "material.emissive"), 1, color_emissive);
7264
glUniform1f(glGetUniformLocation(program, "material.shininess"), shininess);
7365
glUniform1f(glGetUniformLocation(program, "material.shininess_strength"), shininess_strength);
7466

67+
glBindVertexArray(vao);
7568
glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, NULL);
76-
7769
glBindVertexArray(0);
7870

79-
//glActiveTexture(GL_TEXTURE0);
80-
//glBindTexture(GL_TEXTURE_2D, 0);
71+
disableAllTexture();
72+
};
8173

82-
//glActiveTexture(GL_TEXTURE1);
83-
//glBindTexture(GL_TEXTURE_2D, 1);
74+
void activeTextureNum(int num, GLuint id, GLuint program, const string& name) {
75+
glActiveTexture(GL_TEXTURE0 + num);
76+
glBindTexture(GL_TEXTURE_2D, id);
77+
glUniform1i(glGetUniformLocation(program, name.c_str()), num);
78+
}
8479

85-
//glActiveTexture(GL_TEXTURE2);
86-
//glBindTexture(GL_TEXTURE_2D, 2);
87-
};
80+
void disableAllTexture() {
81+
for (size_t i = 0; i < 8; i++) {
82+
glActiveTexture(GL_TEXTURE0 + i);
83+
glBindTexture(GL_TEXTURE_2D, 0);
84+
}
85+
}
8886

8987
// inicializar el mesh
9088
void init(const aiMesh* mesh) {
@@ -100,6 +98,7 @@ class Model {
10098

10199
GLuint buffer[4];
102100
GLuint vao;
101+
GLuint texture_ambient, texture_diffuse, texture_specular;
103102

104103
float shininess, shininess_strength;
105104
float color_ambient[4] = { 1, 1, 1, 1 };
@@ -142,9 +141,9 @@ class Model {
142141
indices.push_back(mesh->mFaces[i].mIndices[2]);
143142
}
144143

145-
//load_material(mesh, aiTextureType_AMBIENT);
146-
//load_material(mesh, aiTextureType_DIFFUSE);
147-
//load_material(mesh, aiTextureType_SPECULAR);
144+
load_material(mesh, aiTextureType_AMBIENT, texture_ambient);
145+
load_material(mesh, aiTextureType_DIFFUSE, texture_diffuse);
146+
load_material(mesh, aiTextureType_SPECULAR, texture_specular);
148147

149148
if (mesh->mMaterialIndex >= 0) {
150149
// obtener el material correspondiente a este Mesh
@@ -180,52 +179,54 @@ class Model {
180179
dst[3] = src.a;
181180
}
182181

183-
//void load_material(const aiMesh* mesh, aiTextureType ttype) {
184-
// if (mesh->mMaterialIndex >= 0) {
185-
// const aiMaterial* material = model->scene->mMaterials[mesh->mMaterialIndex];
186-
187-
// for (unsigned int i = 0; i < material->GetTextureCount(ttype); i++) {
188-
// aiString path;
189-
// material->GetTexture(ttype, i, &path);
190-
191-
// GLuint tex_2d = TextureFromFile(texture_path(path.C_Str()));
192-
// textures.insert({ ttype, tex_2d });
193-
// }
194-
// }
195-
//}
196-
197-
//GLint TextureFromFile(string filename)
198-
//{
199-
// GLuint textureID = 0;
200-
201-
// glGenTextures(1, &textureID);
182+
void load_material(const aiMesh* mesh, aiTextureType ttype, GLuint& texture) {
183+
if (mesh->mMaterialIndex >= 0) {
184+
const aiMaterial* material = model->scene->mMaterials[mesh->mMaterialIndex];
202185

203-
// int width, height, n;
204-
// unsigned char *image = stbi_load(filename.c_str(), &width, &height, &n, 0);
186+
for (unsigned int i = 0; i < material->GetTextureCount(ttype); i++) {
187+
aiString path;
188+
if (AI_SUCCESS == material->GetTexture(ttype, i, &path)) {
189+
const string tex_path = path.C_Str();
190+
191+
if (model->textures.count(tex_path) == 0) {
192+
texture = TextureFromFile(texture_path(path.C_Str()));
193+
model->textures.insert({ tex_path, texture });
194+
}
195+
else texture = model->textures[tex_path];
196+
}
197+
}
198+
}
199+
}
205200

206-
// GLint mode = n == 4 ? GL_RGBA : GL_RGB;
201+
GLint TextureFromFile(const std::string& filename)
202+
{
203+
GLuint textureID = 0;
204+
glGenTextures(1, &textureID);
207205

208-
// glBindTexture(GL_TEXTURE_2D, textureID);
209-
// glTexImage2D(GL_TEXTURE_2D, 0, mode, width, height, 0, mode, GL_UNSIGNED_BYTE, image);
210-
// glGenerateMipmap(GL_TEXTURE_2D);
206+
int width, height, comp;
207+
unsigned char *image = stbi_load(filename.c_str(), &width, &height, &comp, 3);
211208

212-
// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
213-
// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
214-
// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
215-
// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
216-
// glBindTexture(GL_TEXTURE_2D, 0);
209+
glBindTexture(GL_TEXTURE_2D, textureID);
210+
211+
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
212+
213+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
214+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
215+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
216+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
217217

218-
// stbi_image_free(image);
218+
glBindTexture(GL_TEXTURE_2D, 0);
219219

220-
// return textureID;
221-
//}
220+
stbi_image_free(image);
222221

222+
return textureID;
223+
}
223224

224-
//string texture_path(const string& path) {
225-
// size_t start = path.find_last_of("/");
226-
// string tex_path = start == string::npos ? path : path.substr(start + 1);
227-
// return model->directory.empty() ? tex_path : model->directory + "/" + tex_path;
228-
//}
225+
std::string texture_path(const std::string& path) {
226+
size_t start = path.find_last_of("\\/");
227+
string tex_path = start == string::npos ? path : path.substr(start + 1);
228+
return model->directory.empty() ? tex_path : model->directory + "/" + tex_path;
229+
}
229230

230231
void create() {
231232
// generar y activar el VAO
@@ -269,6 +270,7 @@ class Model {
269270
public:
270271
string directory;
271272
const aiScene* scene;
273+
map<string, GLuint> textures;
272274

273275
private:
274276
vector<shared_ptr<Mesh>> meshes;
@@ -291,7 +293,7 @@ class Model {
291293
// cargar el archivo deseado
292294
void init(const std::string& file_name) {
293295

294-
size_t index = file_name.find_last_of("/");
296+
size_t index = file_name.find_last_of("\\/");
295297
directory = index == string::npos ? "" : file_name.substr(0, index);
296298

297299
Assimp::Importer importer;

0 commit comments

Comments
 (0)