Wavefront OBJ loader c++

Started by
4 comments, last by MarkS_ 11 years, 4 months ago
Hey guys,

I've been working all day to try and get an object loader working. I've been using this file as a reference.

http://openglsamples.sourceforge.net/files/glut_obj.cpp

Can anyone see whats wrong with this? I am just getting a blank screen.

Thanks
Advertisement
The most common mistake when loading Wavefront Obj files is being off by one in the indices because Wavefront indices are one-based while practically every renderer (including OpenGL) expect zero-based indices.

That said, have you tried stepping through the code with a debugger and having a look at how the loaded mesh data looks?

If neither of that helps, we will be needing more information (and actually used code). "using this file as a reference" can mean too many things to give any sensible advise.
Sorry, I worded that poorly. I believe I have copied everything exactly from that file. I do think it accounts for the off by one in Model_OBJ::Load. In the middle of the method there is:

vertexNumber[0] -= 1; // OBJ file starts counting from 1
vertexNumber[1] -= 1; // OBJ file starts counting from 1
vertexNumber[2] -= 1; // OBJ file starts counting from 1

Still stumped
When I wrote my OBJ loader, I was having the same issues. What I found to be the best method of debugging the loader is to create a print function that prints the contents of the loaded OBJ to the console in OBJ format. This way you can open the OBJ file in a text editor and compare it with the console side by side. Once you see what parts of the file are not loading correctly, it becomes much easier to debug the loader.

And would suggest that you make plans to dump the OBJ format at some point and stick with binary file formats, even if that means creating your own. As you've seen, writing a loader for a human readable format SUCKS!

Also, looking at your code, this "vertexBuffer = (float*) malloc (fileSize);" needs to be "vertexBuffer = (float*) malloc (fileSize*sizeof(float));". You are allocating an array of fileSize bytes as written, not an array of fileSize floats.

To that end, skip malloc all together. Use new/delete.

[source]
vertexBuffer = new float[fileSize];
.
.
.
detete [] vertexBuffer;
[/source]

To that end, skip malloc all together. Use new/delete.

[source]
vertexBuffer = new float[fileSize];
.
.
.
detete [] vertexBuffer;
[/source]

Or even better:
[source]
std::vector<float> vertexBuffer(fileSize);
[/source]
OpenGL fanboy.

Or even better:
[source]
std::vector<float> vertexBuffer(fileSize);
[/source]


I use vectors in my OBJ loader. I was going to mention it, but didn't want to complicate the issues. And I have a cold and am typing through a fog...

OBJ Loader.h

OBJ Loader.cpp

This will only load OBJ files with triangular faces. The interesting thing is that since I use vectors, I can load the OBJ file data in any order. Cut a vertex line and paste it between two face lines and the file will still load properly. I have a newer version that replaces the older c-style file handling functions (fread, fgetc, etc.) for the newer C++ versions, but I cannot find the code at the moment. Must be on another computer.

This topic is closed to new replies.

Advertisement