Pyopengl - 带纹理的 VBO

Pyopengl - VBO with texture

我用的是Python3.6。最初我使用这样的代码:

    array_to_texture(self.board)
    glColor3fv((1.0, 1.0, 1.0))
    glBegin(GL_QUADS)
    for vertex, tex  in zip(self.POINTS, self.TEX):
        glTexCoord2f(*tex)
        glVertex3fv(vertex)
    glEnd()

而且有效。它在 3D space.

中绘制带有纹理的正方形

现在我想用 VBO 来解决这个问题。绘制墙壁(固定颜色)可以使用以下代码:

    vbo = glGenBuffers(1)
    glBindBuffer(GL_ARRAY_BUFFER, vbo)
    glBufferData(GL_ARRAY_BUFFER, len(vertices) * 4, (c_float * len(vertices))(*vertices), GL_STATIC_DRAW)
    glVertexPointer(3, GL_FLOAT, 0, None)
    glDrawArrays(GL_QUADS, 0, 4)

效果很好。 主要问题是,如何使用这种方法附加和绘制纹理?如何为纹理坐标创建某种缓冲区并使用它?我真的很难找到一些最小的工作示例。

次要问题是,即使 GL_QUADS 在文档中不允许,glDrawArrays(GL_QUADS, 0, 4) 行怎么可能起作用:http://pyopengl.sourceforge.net/documentation/manual-3.0/glDrawArrays.html

OpenGL 4.6 API Compatibility Profile Specification; 10.3.3 Specifying Arrays for Fixed-Function Attributes; page 402

The commands

void VertexPointer( int size, enum type, sizei stride, const void *pointer );
void NormalPointer( enum type, sizei stride, const void *pointer );
void ColorPointer( int size, enum type, sizei stride, const void *pointer );
void SecondaryColorPointer( int size, enum type, sizei stride, const void *pointer );
void IndexPointer( enum type, sizei stride, const void *pointer );
void EdgeFlagPointer( sizei stride, const void *pointer );
void FogCoordPointer( enum type, sizei stride, const void *pointer );
void TexCoordPointer( int size, enum type, sizei stride, const void *pointer );

specify the location and organization of arrays to store vertex coordinates, normals, colors, secondary colors, color indices, edge flags, fog coordinates.

...

An individual array is enabled or disabled by calling one of

void EnableClientState( enum array );
void DisableClientState( enum array );

with array set to VERTEX_ARRAY, NORMAL_ARRAY, COLOR_ARRAY, SECONDARY_COLOR_ARRAY, INDEX_ARRAY, EDGE_FLAG_ARRAY, FOG_COORD_ARRAY, or TEXTURE_COORD_ARRAY, for the vertex, normal, color, secondary color, color index, edge flag, fog coordinate, or texture coordinate array, respectively.


这意味着可以通过 glVertexPointer and enabled by glEnableClientState(GL_VERTEX_ARRAY)

指定顶点坐标
glVertexPointer(3, GL_FLOAT, 0, None)
glEnableClientState(GL_VERTEX_ARRAY)

纹理坐标可以由glTexCoordPointer and enabled by glEnableClientState(GL_TEXTURE_COORD_ARRAY)

指定
glTexCoordPointer(3, GL_FLOAT, 0, None)
glEnableClientState(GL_TEXTURE_COORD_ARRAY)