I am trying to understand how to write a program that will display polygons defined in an external text file. The text file will contain the vertices for GL_QUAD elements, and I’m getting stuck since this file is not defined at compile time. Does anyone have an idea of how to parse this file and generate the shapes in realtime? Thanks.
You could use either a linked list to add vertices as you go, or you could do a “prepass” of the text file, count the number of vertices, dynamically allocate an array, read the file again and fill that array with that info.
Does that help?
To get the number of polys in the file do:
fseek( file, 0, SEEK_END );
int length = ftell( file );
num_polys = length / sizeof(float) / num_of_components_per_vertex / num_of_vertices_per_polygon
Obviously this will only work when the file ONLY contains quads and no header.
Thanks. This makes the theory much clearer and will definately get me started.