views:

53

answers:

2

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 another 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?

A: 

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)

fazo
+1  A: 

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.

quixoto
The issue is that the glVertexPointer expects a flat array of numbers, not Points*. I need some way of telling glVertexPointer that the coordinate data is in Point->x and Point->y without having to build a flat array.
Brad Zeis
Oh, and the Polygon has Point**s instead of Point*s is because Point is a Python Extension Type. The preferred way of working with Extension Types is to always manipulate pointers of them.
Brad Zeis
I updated the answer with the C memory management info that I think is the understanding gap here. Use "memory polymorphism" only if you feel comfortable with what you're doing. And I don't know much about Python, so I'll assume you know what you're doing there. :)
quixoto
Well, I should have put this in the original question, but the extension types have other members in the structs, so the coordinate members of Point wouldn't be laid out next to each other. And there wouldn't be a consistent byte offset between the members, so using stride wouldn't work either. It's looking like I'm going to have to build another array with just the coordinate data in it.
Brad Zeis