4
4
#include < GL\GL.h>
5
5
#include < GL\glew.h>
6
6
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>
10
10
11
11
#include < glm\glm.hpp>
12
12
#include < glm\gtc\type_ptr.hpp>
@@ -31,7 +31,10 @@ class Model {
31
31
32
32
public:
33
33
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 } {
35
38
this ->model = model;
36
39
load (mesh);
37
40
create ();
@@ -50,41 +53,36 @@ class Model {
50
53
};
51
54
52
55
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);
72
64
glUniform1f (glGetUniformLocation (program, " material.shininess" ), shininess);
73
65
glUniform1f (glGetUniformLocation (program, " material.shininess_strength" ), shininess_strength);
74
66
67
+ glBindVertexArray (vao);
75
68
glDrawElements (GL_TRIANGLES, indices.size (), GL_UNSIGNED_INT, NULL );
76
-
77
69
glBindVertexArray (0 );
78
70
79
- // glActiveTexture(GL_TEXTURE0 );
80
- // glBindTexture(GL_TEXTURE_2D, 0) ;
71
+ disableAllTexture ( );
72
+ } ;
81
73
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
+ }
84
79
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
+ }
88
86
89
87
// inicializar el mesh
90
88
void init (const aiMesh* mesh) {
@@ -100,6 +98,7 @@ class Model {
100
98
101
99
GLuint buffer[4 ];
102
100
GLuint vao;
101
+ GLuint texture_ambient, texture_diffuse, texture_specular;
103
102
104
103
float shininess, shininess_strength;
105
104
float color_ambient[4 ] = { 1 , 1 , 1 , 1 };
@@ -142,9 +141,9 @@ class Model {
142
141
indices.push_back (mesh->mFaces [i].mIndices [2 ]);
143
142
}
144
143
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 );
148
147
149
148
if (mesh->mMaterialIndex >= 0 ) {
150
149
// obtener el material correspondiente a este Mesh
@@ -180,52 +179,54 @@ class Model {
180
179
dst[3 ] = src.a ;
181
180
}
182
181
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 ];
202
185
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
+ }
205
200
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);
207
205
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 );
211
208
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);
217
217
218
- // stbi_image_free(image );
218
+ glBindTexture (GL_TEXTURE_2D, 0 );
219
219
220
- // return textureID;
221
- // }
220
+ stbi_image_free (image);
222
221
222
+ return textureID;
223
+ }
223
224
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
+ }
229
230
230
231
void create () {
231
232
// generar y activar el VAO
@@ -269,6 +270,7 @@ class Model {
269
270
public:
270
271
string directory;
271
272
const aiScene* scene;
273
+ map<string, GLuint> textures;
272
274
273
275
private:
274
276
vector<shared_ptr<Mesh>> meshes;
@@ -291,7 +293,7 @@ class Model {
291
293
// cargar el archivo deseado
292
294
void init (const std::string& file_name) {
293
295
294
- size_t index = file_name.find_last_of (" /" );
296
+ size_t index = file_name.find_last_of (" \\ /" );
295
297
directory = index == string::npos ? " " : file_name.substr (0 , index );
296
298
297
299
Assimp::Importer importer;
0 commit comments