How to store a face in memory
Ive been trying to figure this out but cant find a way to do it which seems reasonable, and nothing seems to actually explain it, I can find a myriad of stuff on how to do modelling, and how to work withthe faces, but nothing on whow they can be stored in memory to work with. Ive been looking at obj mainly, and the rest is easy it's mainly just arrays to store it all, but faces, the only way Ive been able to think of to work with them is to store the details on which vertices in an object, or to read them from file each time, but that开发者_如何学编程 can't be right surely? It seems crazy to be using an object for every face, but I cant think of any other way! Even just a book to look at or anything to just point me in the right direction, because everything just seems to skip over this stuff. Thanks.
A face is usually three or more vertices connected. This means you can store each face as a list of the vertices (or indices to the vertices) it consists of.
f0: [v0 v1 v2]
f1: [v3 v4 v5]
...
Or if the amount of vertices for each face is predetermined (which I would wager is common), you could collapse it into a single list/vector/array:
[/* face 1 */ v0 v1 v2 /* face 2 */ v3 v4 v5]
Wikipedia may convey it better than I do.
What you are looking for is arrays of vertices. How it works is each object contains an array of vertices and a color. The normals for the vertices are stored in this same array, (vertexX, vertexY, vertexZ, normalX, normalY, normalZ). When you want to render your objects, you loop through your array of objects and render each object as shown below:
float plane[] = {
10, 10, 0, 0, 0, 1,
-10, 10, 0, 0, 0, 1,
-10, -10, 0, 0, 0, 1,
10, 10, 0, 0, 0, 1,
-10, 10, 0, 0, 0, 1,
10, -10, 0, 0, 0, 1,
};
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
glVertexPointer(3, GL_FLOAT, sizeof(plane[0])*6, &plane[0]);
glNormalPointer(3, GL_FLOAT, sizeof(plane[0])*6, &plane[3]);
glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
glDrawArrays(GL_TRIANGLES, 0, (sizeof(plane) / sizeof(plane[0]))/6);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
And that should draw a red plane. Only difference is you would want to feed it your array from your object definition. You can also include color information per vertex and just include a glColorPointer in the above code and a glEnableClientState(GL_COLOR_ARRAY).
精彩评论