简体   繁体   中英

Pyopengl - VBO with texture

I use Python 3.6. Originally I use code like this:

    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()

and it works. It draw square with texture in 3D space.

Now I want to use VBO to do the trick. To draw walls (with fixed color) is possible to use following code:

    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)

That works perfectly. The major question, how can I attach and draw a texture with this approach? How Can I create some kind of buffer for texture coordinates and use it? I really struggle to find some minimal working example.

The minor question is , how is it possible that the glDrawArrays(GL_QUADS, 0, 4) line works, even though the GL_QUADS are not allowed in documentation: http://pyopengl.sourceforge.net/documentation/manual-3.0/glDrawArrays.html

See 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.


This means that vertex coordinates can be specified by glVertexPointer and enabled by glEnableClientState(GL_VERTEX_ARRAY)

glVertexPointer(3, GL_FLOAT, 0, None)
glEnableClientState(GL_VERTEX_ARRAY)

And texture cooridantes can be specified by glTexCoordPointer and enabled by glEnableClientState(GL_TEXTURE_COORD_ARRAY)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM