scanf/fscanf problem with an OBJ reader - Printable Version
+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Game Programming Fundamentals (/forum-7.html)
+--- Thread: scanf/fscanf problem with an OBJ reader (/thread-1139.html)
scanf/fscanf problem with an OBJ reader - mikey - Jun 12, 2009 10:56 AM
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
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?). )
scanf/fscanf problem with an OBJ reader - ThemsAllTook - Jun 12, 2009 11:13 AM
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.
scanf/fscanf problem with an OBJ reader - mikey - Jun 12, 2009 11:16 AM
Ohh, so I have to supply more parameters...
Is this right?
scanf/fscanf problem with an OBJ reader - unknown - Jun 12, 2009 11:22 AM
mikey Wrote:Ohh, so I have to supply more parameters...
You can double check against the manual page. Open Terminal.app and type man scanf
scanf/fscanf problem with an OBJ reader - mikey - Jun 12, 2009 12:28 PM
So, that's a ... yes?
scanf/fscanf problem with an OBJ reader - mikey - Jun 13, 2009 03:22 AM
It still doesn't work. I can't see why not!?!
(I am not using OBJ-style commenting, so I felt it'd be proper to give the model it's own file format.)
This code doesn't return comments, but only returns the first (x) coordinate in each line.
scanf/fscanf problem with an OBJ reader - unknown - Jun 13, 2009 05:24 AM
how about: while(!feof(objfile))
scanf/fscanf problem with an OBJ reader - mikey - Jun 13, 2009 06:12 AM
OK thanks unknown, I've got that part of the OBJ Loader working, so how do I actuallly draw the vertices? Should I (or could I) store them as 3 arrays:
...Then? Wouldn't 3 arrays of 1000+ be overkill? Is there an easier way?
EDIT: Could I use one 3*dynamic array for the whole model?
scanf/fscanf problem with an OBJ reader - unknown - Jun 13, 2009 06:29 AM
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).
scanf/fscanf problem with an OBJ reader - mikey - Jun 13, 2009 06:37 AM
What's a display list? Because I need to redraw the model every frame, I'd need to store the arrays somehow?
scanf/fscanf problem with an OBJ reader - AnotherJake - Jun 13, 2009 06:56 AM
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.
scanf/fscanf problem with an OBJ reader - unknown - Jun 13, 2009 07:23 AM
mikey Wrote:What's a display list? Because I need to redraw the model every frame, I'd need to store the arrays somehow?
opengl.org has various infos, http://www.opengl.org/resources/faq/technical/displaylist.htm
AnotherJake Wrote:display lists will be going away in future versions
Yikes! That sucks.
scanf/fscanf problem with an OBJ reader - mikey - Jun 13, 2009 07:48 AM
OK then, I won't use display lists, I'll use glDrawArrays. How would I do this?
I think I've mastered file I/O for now.
PS. I'm using GLUT.
scanf/fscanf problem with an OBJ reader - AnotherJake - Jun 13, 2009 08:18 AM
You really should read the Red Book or some other authoritative source for learning how to use glDrawArrays. Getting answers only from forum folks such as us isn't ideal
That said, here are the "Cliff's Notes" on how to use glDrawArrays:
To set up your data, you just need some arrays, like here are two arrays initialized on the spot, one for texture coordinates and one for vertices. There are enough in each to make up a quad:
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:
Then you might also want to call these, depending on your needs:
Then you need to set the pointer for each type of array, like so:
glVertexPointer(3, GL_FLOAT, 0, myVerts);
and also others as needed:
glNormalPointer(GL_FLOAT, 0, myNormals);
glColorPointer(4, GL_UNSIGNED_BYTE, 0, myColors);
glTexCoordPointer(2, GL_SHORT, 0, myTexCoords);
Then, when you're ready to draw you simply call glDrawArrays, perhaps like so for the equivalent of a quad using a triangle strip instead of GL_QUADS:
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
For an actual example this in use, see that old example I gave you for drawing a cube a while back: here's the post
scanf/fscanf problem with an OBJ reader - mikey - Jun 13, 2009 09:27 AM
OK, does it matter if I call
And I should call everything bar drawArrays in init()? (Once in a program)?
Quote:Cliff's Notes. Don't remind me of that time my english teacher caught me out...