scanf/fscanf problem with an OBJ reader

I'm writing a model loader for my 3D OpenGL written in C, but I can't get it to work.

The OBJ is listed as

Code:

# comment
v 1.0345 2.434 0.342
v 0.4435 0.234 1.240

Here's the code:

Code:

if ( fscanf(objfile,"%s %f %f" , &fchar) != 1 )

After I run, I get the gdb. It tells me this line is faulty, but I can't see why. scanf (and fscanf) return integers (1), the number of strings matching the layout in the format string, this way I can tell if the line is a comment (just a string) or a proper line (char, then three decimals (floats?). )

You need to pass a corresponding pointer in the fscanf parameter list for each conversion specified in your format string. In your case, you're matching char *, float *, float *, but only providing one of those. If you compile with -Wall in your compiler warning flags, it'll tell you about these errors at compile time.

You can use malloc to create the arrays and read the vertices into them, then make a display list by drawing the triangles with opengl and then free the arrays (so the memory can be reused for something else).

You *can* use display lists, but I wouldn't recommend it since VBOs are preferred now, and display lists will be going away in future versions of OpenGL. Instead, what you should do is learn how to use glDrawArrays first. Once you learn how to use glDrawArrays it'll be much easier to see how you need to organize the data you're loading from your obj file. Don't try to learn glDrawArrays at the same time as figuring out how to load .obj though, since that'll just complicate matters.

You could just as well make up your arrays by loading them from a file.

To actually make use of them, you must first enable all the required client states you will be using to draw your object. You will always want to use vertices, and you might want to use normals, texture coordinates, or even vertex colors. So always call: