Game Development Reference
In-Depth Information
if (r==0) return 0;
count+=r;
}
if (length%2) fseek(fp, 1, SEEK_CUR);
return numpolygons;
}
ters a new polygon. A polygon is defined as a vertex total ' n' and then n
indices into the points list. The vertex total has to have the top 6 bits of a
16-bit short integer masked out, since these are reserved for flags used
by non-standard polygons. The only use currently defined for these is to
store curve information. In the function we use a bitwise 'and' to zero out
the top 6 bits. Then we create storage for the current number of polygons
plus one extra. The existing polygon array is copied if it exists and a
pointer to a polygon is set to point to the last one in the array. This last
polygon is where we will store the data for the polygon being read. We
read the number of vertices and read each vertex from the file. If the value
of the vertex index is less than 4 then we can store the value; if it exceeds
4 then in this simple application we simply ignore the higher vertices. In a
more sophisticated application you could choose to create a new polygon
for vertices above 4, but you need to create a versatile function if it is able
to cope with any polygon it receives. The problem stems from concave
polygons. If a polygon is convex then it can easily be tripled, turned into
triangles, by joining vertex 0 to n and n + 1, as n goes from 1 to the
number of vertices minus 2. If, however, a polygon is concave then this
technique can result in a group of triangles that draw outside the range of
a polygon. It is highly recommended that when exchanging data between
a CGI application and your own application you use just triangles. In this
way, non-planar polygons are avoided and you also avoid any problems
over concave polygons, since a triangle must be convex. In the function
we store the value of the number vertices if it is below 5. If it is above 5
then the result is capped to 4. Finally, the function returns the number of
bytes that have been read from the file while adding the current
polygon.