Jump to content
  • Advertisement
Sign in to follow this  
vgamemaster

OpenGL Wavefront OBJ loader c++

This topic is 2051 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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] Edited by MarkS

Share this post


Link to post
Share on other sites

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]

Share this post


Link to post
Share on other sites

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. Edited by MarkS

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!