Feel free to send me an e-mail. If you do, be sure to include an email so that I can get back to you









Please type what is in the image or answer the question.


OBJ Model Viewer

This is a simple model viewer for the OBJ format. The OBJ format "is a geometry definition file format first developed by Wavefront Technologies for its Advanced Visualizer animation package" (Wikipedia). This project uses the OpenGL utility toolkit (GLUT) and the GLUI library.

How do I load an OBJ model?

The OBJ file format is a 3D geometry file format that organizes vertex, normal, texture coordinates, and face data by placing each piece of data on a single line. Each line is first marked with an idenitying token (v, vn, vt, f, et cetera) that denotes what kind of data is specified on the current line. These identifying tokens also imply how many arguments should follow. The only way to get access to this data is to parse it.

One of the simplest ways to solve a parsing problem is to implement what is commonly called a recursive descent parser (RDP). An RDP works by parsing each token and calling recursive functions that implement a part of the grammar. The recursive function should know what kind of tokens should follow and how many. If the expected tokens are missing, then a parsing error occurs. By now you may be wondering how all this RDP talk helps us with getting to the data within an OBJ file? Well, the OBJ file format lends itself very nicely to this parsing technique. Consider the following OBJ fragment:

v -9.360900 0.000000 7.247677

If we were to parse the above line, the first token would be a v. Furthermore, the v token implies we should expect 3 floating point numbers that are seperated by spaces. If we didn't get three floating point numbers, then the OBJ file is malformed and the error should be handled (I leave this as an exercise to you). The actual code could call a recursive function, vertex( ), that reads in three tokens seperated by spaces. Furthermore, consider the next fragment:

f 19/31/1 20/32/2 18/30/3

On this line, we have a description of a face. If we were to parse this line, the first token would be the f token. That means we should expect face data. So we could then hand of the parsing work to a recursive function, face( ), that reads in as many face components as possible. A face component is a triplet of 3 unsigned integers seperated by a '/' character. These unsigned integers are the indices into the array of vertices, texture coordinates, and normals, respectively. These arrays were constructed because vertex, tecture coordinates, and normals come before face data. Note that it is possible to have more v, vn, and vt lines after a sequence of f lines. It just means your arrays will need to grow larger. Therefore, the above line describes a triangle face.

This should be enough info to be able to parse OBJ files. There are other tokens (g for groups, et cetera) that I did not mention. However, I have presented enough information to get you started. If you have any questions, feel free to contact me.



Screenshots

Screenshot 16 Image Screenshot 17 Image Screenshot 18 Image

Comments

No comments.

Add a comment