Skip to content

Commit 2daec8b

Browse files
committed
Search .mtl and texture from .obj's base dir.
Fix bad memory access.
1 parent c2ff3f1 commit 2daec8b

1 file changed

Lines changed: 53 additions & 14 deletions

File tree

‎examples/viewer/viewer.cc‎

Lines changed: 53 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -143,15 +143,34 @@ float eye[3], lookat[3], up[3];
143143

144144
GLFWwindow* window;
145145

146-
void CheckErrors(std::string desc) {
146+
static std::string GetBaseDir(const std::string &filepath) {
147+
if (filepath.find_last_of("/\\") != std::string::npos)
148+
return filepath.substr(0, filepath.find_last_of("/\\"));
149+
return "";
150+
}
151+
152+
static bool FileExists(const std::string &abs_filename) {
153+
bool ret;
154+
FILE *fp = fopen(abs_filename.c_str(), "rb");
155+
if (fp) {
156+
ret = true;
157+
fclose(fp);
158+
} else {
159+
ret = false;
160+
}
161+
162+
return ret;
163+
}
164+
165+
static void CheckErrors(std::string desc) {
147166
GLenum e = glGetError();
148167
if (e != GL_NO_ERROR) {
149168
fprintf(stderr, "OpenGL error in \"%s\": %d (%d)\n", desc.c_str(), e, e);
150169
exit(20);
151170
}
152171
}
153172

154-
void CalcNormal(float N[3], float v0[3], float v1[3], float v2[3]) {
173+
static void CalcNormal(float N[3], float v0[3], float v1[3], float v2[3]) {
155174
float v10[3];
156175
v10[0] = v1[0] - v0[0];
157176
v10[1] = v1[1] - v0[1];
@@ -175,7 +194,7 @@ void CalcNormal(float N[3], float v0[3], float v1[3], float v2[3]) {
175194
}
176195
}
177196

178-
bool LoadObjAndConvert(float bmin[3], float bmax[3],
197+
static bool LoadObjAndConvert(float bmin[3], float bmax[3],
179198
std::vector<DrawObject>* drawObjects,
180199
std::vector<tinyobj::material_t>& materials,
181200
std::map<std::string, GLuint>& textures,
@@ -187,9 +206,16 @@ bool LoadObjAndConvert(float bmin[3], float bmax[3],
187206

188207
tm.start();
189208

209+
std::string base_dir = GetBaseDir(filename);
210+
#ifdef _WIN32
211+
base_dir += "\\";
212+
#else
213+
base_dir += "/";
214+
#endif
215+
190216
std::string err;
191217
bool ret =
192-
tinyobj::LoadObj(&attrib, &shapes, &materials, &err, filename, NULL);
218+
tinyobj::LoadObj(&attrib, &shapes, &materials, &err, filename, base_dir.c_str());
193219
if (!err.empty()) {
194220
std::cerr << err << std::endl;
195221
}
@@ -223,9 +249,20 @@ bool LoadObjAndConvert(float bmin[3], float bmax[3],
223249
GLuint texture_id;
224250
int w, h;
225251
int comp;
226-
unsigned char* image = stbi_load(mp->diffuse_texname.c_str(), &w, &h, &comp, STBI_default);
252+
253+
std::string texture_filename = mp->diffuse_texname;
254+
if (!FileExists(texture_filename)) {
255+
// Append base dir.
256+
texture_filename = base_dir + mp->diffuse_texname;
257+
if (!FileExists(texture_filename)) {
258+
std::cerr << "Unable to find file: " << mp->diffuse_texname << std::endl;
259+
exit(1);
260+
}
261+
}
262+
263+
unsigned char* image = stbi_load(texture_filename.c_str(), &w, &h, &comp, STBI_default);
227264
if (image == nullptr) {
228-
std::cerr << "Unable to load texture: " << mp->diffuse_texname << std::endl;
265+
std::cerr << "Unable to load texture: " << texture_filename << std::endl;
229266
exit(1);
230267
}
231268
glGenTextures(1, &texture_id);
@@ -395,7 +432,7 @@ bool LoadObjAndConvert(float bmin[3], float bmax[3],
395432
return true;
396433
}
397434

398-
void reshapeFunc(GLFWwindow* window, int w, int h) {
435+
static void reshapeFunc(GLFWwindow* window, int w, int h) {
399436
int fb_w, fb_h;
400437
// Get actual framebuffer size.
401438
glfwGetFramebufferSize(window, &fb_w, &fb_h);
@@ -411,7 +448,7 @@ void reshapeFunc(GLFWwindow* window, int w, int h) {
411448
height = h;
412449
}
413450

414-
void keyboardFunc(GLFWwindow* window, int key, int scancode, int action,
451+
static void keyboardFunc(GLFWwindow* window, int key, int scancode, int action,
415452
int mods) {
416453
(void)window;
417454
(void)scancode;
@@ -440,7 +477,7 @@ void keyboardFunc(GLFWwindow* window, int key, int scancode, int action,
440477
}
441478
}
442479

443-
void clickFunc(GLFWwindow* window, int button, int action, int mods) {
480+
static void clickFunc(GLFWwindow* window, int button, int action, int mods) {
444481
(void)window;
445482
(void)mods;
446483
if (button == GLFW_MOUSE_BUTTON_LEFT) {
@@ -467,7 +504,7 @@ void clickFunc(GLFWwindow* window, int button, int action, int mods) {
467504
}
468505
}
469506

470-
void motionFunc(GLFWwindow* window, double mouse_x, double mouse_y) {
507+
static void motionFunc(GLFWwindow* window, double mouse_x, double mouse_y) {
471508
(void)window;
472509
float rotScale = 1.0f;
473510
float transScale = 2.0f;
@@ -494,7 +531,7 @@ void motionFunc(GLFWwindow* window, double mouse_x, double mouse_y) {
494531
prevMouseY = mouse_y;
495532
}
496533

497-
void Draw(const std::vector<DrawObject>& drawObjects, std::vector<tinyobj::material_t>& materials, std::map<std::string, GLuint>& textures) {
534+
static void Draw(const std::vector<DrawObject>& drawObjects, std::vector<tinyobj::material_t>& materials, std::map<std::string, GLuint>& textures) {
498535
glPolygonMode(GL_FRONT, GL_FILL);
499536
glPolygonMode(GL_BACK, GL_FILL);
500537

@@ -513,9 +550,11 @@ void Draw(const std::vector<DrawObject>& drawObjects, std::vector<tinyobj::mater
513550
glEnableClientState(GL_COLOR_ARRAY);
514551
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
515552

516-
std::string diffuse_texname = materials[o.material_id].diffuse_texname;
517-
if (diffuse_texname.length() > 0) {
518-
glBindTexture(GL_TEXTURE_2D, textures[diffuse_texname]);
553+
if ((o.material_id < materials.size())) {
554+
std::string diffuse_texname = materials[o.material_id].diffuse_texname;
555+
if (textures.find(diffuse_texname) != textures.end()) {
556+
glBindTexture(GL_TEXTURE_2D, textures[diffuse_texname]);
557+
}
519558
}
520559
glVertexPointer(3, GL_FLOAT, stride, (const void*)0);
521560
glNormalPointer(GL_FLOAT, stride, (const void*)(sizeof(float) * 3));

0 commit comments

Comments
 (0)