Skip to main content
 首页 » 编程设计

python之带索引的 pyOpenGL VBO

2025年05月04日7over140

我想在 pyOpenGL 中使用带索引的 VBO 绘制一个矩形。我为此使用了 glDrawRangeElements() 函数,但我总是在 glDrawRangeElements:

行中遇到同样的错误

WindowsError: exception: access violation reading 0x00000000

我尝试了很多东西,在互联网上寻找解决方案,整天都在研究代码示例,所以现在我真的不知道如何继续下去。如果这里有人可以帮助我,那就太好了。

这应该是产生错误的代码部分:

vertexPositions = [[-0.75, -0.75,  0.0], 
                    [0.75, -0.75,  0.0], 
                    [0.75,  0.75,  0.0], 
                   [-0.75,  0.75,  0.0] ] 
vertexIndices = [0, 1, 2, 
                 1, 2, 3] 
vertexComponents = 3 
 
positionBufferObject = None 
indexBufferObject = None 
 
x = 0 
 
def glGenVertexArray(): 
    vao_id = GL.GLuint(0) 
    vertex_array_object.glGenVertexArrays(1, vao_id) 
    return vao_id.value 
 
def initialize_vertex_buffer(): 
    global positionBufferObject, indexBufferObject 
 
    indexBufferObject = GL.glGenBuffers(1) 
    GL.glBindBuffer(GL.GL_ELEMENT_ARRAY_BUFFER, indexBufferObject) 
 
    array_type = (GL.GLushort * len(vertexIndices)) 
    GL.glBufferData(GL.GL_ELEMENT_ARRAY_BUFFER, len(vertexIndices) * 2,  
                         array_type(*vertexIndices), GL.GL_STATIC_DRAW) 
    GL.glBindBuffer(GL.GL_ELEMENT_ARRAY_BUFFER, 0) 
 
    positionBufferObject = GL.glGenBuffers(1) 
    GL.glBindBuffer(GL.GL_ARRAY_BUFFER, positionBufferObject) 
 
    array_type = (GL.GLfloat * len(vertexPositions)) 
    GL.glBufferData(GL.GL_ARRAY_BUFFER, 
                    len(vertexPositions[0])*len(vertexPositions)*sizeOfFloat, 
                    array_type(*vertexPositions), GL.GL_STATIC_DRAW 
    ) 
    GL.glBindBuffer(GL.GL_ARRAY_BUFFER, 0) 
    glBindVertexArray( glGenVertexArray() ) 
 
 
def init(): 
    initialize_vertex_buffer() 
 
 
def display(): 
    global x 
    GL.glClearColor(0.0, 0.0, 0.0, 0.0) 
    GL.glClear(GL.GL_COLOR_BUFFER_BIT) 
 
    GL.glMatrixMode(GL.GL_MODELVIEW) 
    GL.glEnableClientState(GL.GL_VERTEX_ARRAY) 
    GL.glEnableClientState(GL.GL_INDEX_ARRAY) 
    GL.glLoadIdentity() 
    GL.glTranslate(0, 0, -5) 
    GL.glRotate(x, 0, 1, 0) 
 
    GL.glBindVertexArray(glGenVertexArray()) 
    GL.glBindBuffer(GL.GL_ARRAY_BUFFER, positionBufferObject) 
    GL.glBindBuffer(GL.GL_ELEMENT_ARRAY_BUFFER, indexBufferObject) 
    GL.glEnableVertexAttribArray(0) 
 
    GL.glDrawRangeElements(GL.GL_TRIANGLES,0, 4, 6, GL.GL_UNSIGNED_SHORT, c_void_p(0)) 
 
    #GL.glVertexAttribPointer(0, vertexComponents, GL.GL_FLOAT, False, 0, null) 
 
    #GL.glDrawArrays(GL.GL_QUADS, 0, len(vertexPositions) / vertexComponents) 
 
    GL.glDisableVertexAttribArray(0) 
    GL.glDisableClientState(GL.GL_VERTEX_ARRAY) 
    GL.glDisableClientState(GL.GL_INDEX_ARRAY) 
 
    pygame.display.flip() 

我不得不承认,我还没有真正了解所有这些事情,只是试图理解它,因为我需要它用于一个项目,所以如果到目前为止我还遗漏了任何其他错误,请告诉我的 ;)

提前致谢

请您参考如下方法:

你的电话不应该:

GL.glBindVertexArray (glGenVertexArray()) 

成为

GL.glBindVertexArray (GL.glGenVertexArray()) 

此外,您可能希望跟踪该顶点数组,以便稍后删除它并释放它使用的资源。