OpenGL Vertex Arrays
I have a struct called Point
(which happens to be a Python Extension) that looks like this:
struct Point {
PyObject_HEAD // Macro that expands to include a few more members
double x;
double y;
};
And I have an开发者_开发问答other struct that will hold a bunch of these in two arrays:
struct Polygon {
int length;
Point **vertex_points;
Point **texcrd_points;
}
I want to use these to map both the Vertices and Texture Coordinates of a Polygon with Vertex Arrays. The problem is that Vertex Arrays expect arrays in the format:
[x, y, x, y, x, y, etc]
Is there a way I can call glVertexPointer
and glTexCoordPointer
with Polygon->vertex_points
and Polygon->texcrd_points
, or do I have to construct new arrays that match what gl*Pointer is expecting?
You can call glVertexPointer
with the base of your vertex array, and glTexCoordPointer
with the base of the texture coords array. As long as the number of things in those arrays line up to the right (same) number of verts, this should work fine.
OpenGL can't access members of the structure explicitly. The members will be laid out in memory next to eachother (see caveat below). If multiple Point structures are in a series in memory, effectively you have a big flat list of doubles. OpenGL doesn't know about structures, it just wants one big list of (double in this case) values. But you can build a series of Points and hand OpenGL a pointer to the first one, and if the layout in memory isn't wrong or padded, they'll be equivalent.
If you're too new to C to have this conceptually under your belt (along with the memory layout caveats), then you should just copy all the doubles into a new array before calling GL.
I do question a couple things: maybe there's missing code here, but having Point**
instead of Point*
looks fishy, since glVertexPointer
etc will expect a flat array of values here. By the same token, although it's probably aligned fine already, in theory an array of those structs might not be flush-packed together in memory. You can do this explicitly depending on your compiler, or you can play with the stride
param in the GL calls to be explicit about the distance in memory between each vert.
http://www.opengl.org/sdk/docs/man/xhtml/glVertexPointer.xml
http://www.opengl.org/sdk/docs/man/xhtml/glTexCoordPointer.xml
parameter 'size' in both functions is probably what you need (and you need 2 for vertices - struct Point, and 'type'=GL_DOUBLE)
精彩评论