OpenGL 顶点数组
我有一个名为 Point
的结构(它恰好是一个 Python 扩展),如下所示:
struct Point {
PyObject_HEAD // Macro that expands to include a few more members
double x;
double y;
};
我还有另一个结构,它将在两个数组中保存一堆这些:
struct Polygon {
int length;
Point **vertex_points;
Point **texcrd_points;
}
我想用它们来映射具有顶点数组的多边形的顶点和纹理坐标。问题是顶点数组需要以下格式的数组:
[x, y, x, y, x, y, etc]
有没有办法可以使用 Polygon->vertex_points
调用 glVertexPointer
和 glTexCoordPointer
以及Polygon->texcrd_points,或者我是否必须构造与 gl*Pointer 期望的内容相匹配的新数组?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用顶点数组的基数调用
glVertexPointer
,使用纹理坐标数组的基数调用glTexCoordPointer
。只要这些数组中的事物数量与正确(相同)的顶点数量对齐,这应该可以正常工作。OpenGL 无法显式访问该结构的成员。成员将在内存中并排排列(请参阅下面的警告)。如果多个 Point 结构在内存中是一系列的,那么实际上你有一个很大的双精度列表。 OpenGL 不了解结构,它只需要一个大的值列表(在本例中为双值)。但是你可以构建一系列点并将 OpenGL 传递给第一个点,如果内存中的布局没有错误或填充,它们将是等效的。
如果您对 C 语言太陌生,无法从概念上理解这一点(以及内存布局注意事项),那么您应该在调用 GL 之前将所有双精度数复制到新数组中。
我确实质疑一些事情:也许这里缺少代码,但是用
Point**
而不是Point*
看起来很可疑,因为glVertexPointer
等会这里期望有一个扁平的值数组。出于同样的原因,尽管它可能已经对齐得很好,但理论上这些结构的数组可能不会在内存中齐平打包在一起。您可以根据您的编译器显式执行此操作,也可以使用 GL 调用中的stride
参数来明确每个顶点之间的内存距离。You can call
glVertexPointer
with the base of your vertex array, andglTexCoordPointer
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 ofPoint*
looks fishy, sinceglVertexPointer
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 thestride
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
两个函数中的参数“size”可能就是您所需要的(并且您需要 2 个顶点 - struct Point,并且“type”=GL_DOUBLE)
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)