Model Viewer Part 1

Introduction

This is a project I’m using to learn about OpenGL. It’s a revisit of some old, old code from about 18 months ago. This time around things seem a lot easier.

I'm a teapot
I’m a teapot

In it’s current state, the program is really pretty basic, not really having even the absolute minimum requirements for messing about with OpenGL (zoom or auto-zoom are pretty crucial, for example). But as a starting point it’s acceptable.

OpenGL lacks any model loading ability and I felt the glut primitives were a bit limited, so as an exercise I decided to write this Alias|Wavefront’s lightwave .obj format reader and viewer. I’m aware of Nate Robbins glm and its variants, but I found that it had a couple of bugs and other shortcomings. I’m not saying that I can do better, but I’ll learn more from my own version and when things go wrong (as they inevitably do) I’ll have a better insight into what’s wrong and how to fix it.

The obj format is quite extensive so I’ve only written the minimum necessary to have something display. Any additional features can be added as and when needed (bezier curves, negative/relative vertex indices, etc.).

I implemented several different parsing methods since I was initially getting v-e-r-y slow read times on any reasonably sized file. This turned out to be due to still being in debug mode, d’oh! Release is a lot quicker, but still, big files take a while to load (53 seconds for a 5MB file, 223,000 face, 342,500 vertices). So, I’ve experimented with different parsing methods.

My original parsing code used find and its variants and was a mess, so I experimented with streams and tokenising. Tokens seem an elegant and efficient solution, but it looks as though the find method is the fatest of the three.

Once I got it up and running I went slightly crazy and downloaded a few dozen obj files to test it out. To save having to keep typing in new filenames they can be passed to the program… either from a command line or by dragging and dropping onto the .exe.

Additionally, I added a few helper functions… the code does a simple search on the immediate file structure for the named model file; this allows me to work on different machines and not worry too much which directory the model files are kept in.

As much as I hate to admit it my grasp of OpenGL is still not quite encyclopedic, most of the settings used are grabbed off the net.

Orientation

I’m quite pleased with this bit. This implementation provides rotation of the model about the x and y axes, without quaternions and without gimbal lock.

Future Enhancements – Next

The load code needs tidying up, it’s a nightmare to follow, there are still a couple of bugs in there. I’m trying to use OO as far possible.