在python中使用VBOs

0 投票
1 回答
1147 浏览
提问于 2025-04-16 15:29

我正在尝试通过使用VBO(顶点缓冲对象)来优化我的OpenGL应用程序。目前,使用顶点数组时一切都运行得很好,但当我使用VBO时,它什么都不显示,也没有抛出任何异常。我看过的所有教程都使用类似的方法。

这是上传和渲染这些VBO的代码。

    def uploadToVRAM(self):
    usage = GL_STATIC_DRAW
    if self.vertices_VBO == None:
        self.vertices_VBO = glGenBuffers(1)
    glBindBuffer(GL_ARRAY_BUFFER, self.vertices_VBO)
    glBufferData(GL_ARRAY_BUFFER, ADT.arrayByteCount(self.vertices), ADT.voidDataPointer(self.vertices), usage)
    #glBufferData(GL_ARRAY_BUFFER, self.vertices, usage)
    #print "Bytes:",ADT.arrayByteCount(self.vertices)

    if self.normals != None:
        if self.normals_VBO == None:
            self.normals_VBO = glGenBuffers(1)
        glBindBuffer(GL_ARRAY_BUFFER, self.normals_VBO)
        glBufferData(GL_ARRAY_BUFFER, ADT.arrayByteCount(self.normals), ADT.voidDataPointer(self.normals), usage)
        #glBufferData(GL_ARRAY_BUFFER, self.normals, usage)

    if self.colors != None:
        if self.colors_VBO == None:
            self.colors_VBO = glGenBuffers(1)
        glBindBuffer(GL_ARRAY_BUFFER, self.colors_VBO)
        glBufferData(GL_ARRAY_BUFFER, ADT.arrayByteCount(self.colors), ADT.voidDataPointer(self.colors), usage)
        #glBufferData(GL_ARRAY_BUFFER, self.colors, usage)

    if self.uvs != None:
        if self.uvs_VBO == None:
            self.uvs_VBO = glGenBuffers(1)
        glBindBuffer(GL_ARRAY_BUFFER, self.uvs_VBO)
        glBufferData(GL_ARRAY_BUFFER, ADT.arrayByteCount(self.uvs), ADT.voidDataPointer(self.uvs), usage)
        #glBufferData(GL_ARRAY_BUFFER, self.uvs, usage)

    glBindBuffer(GL_ARRAY_BUFFER,0)

这是渲染部分的代码:

    def renderVBOs(self, primitive):
        #active arrays
        glEnableClientState(GL_VERTEX_ARRAY)
        glBindBuffer(GL_ARRAY_BUFFER, self.vertices_VBO)
        glVertexPointer(3, GL_FLOAT, 0, 0) #offset in bytes

        if self.normals != None:
            glEnableClientState(GL_NORMAL_ARRAY)
            glBindBuffer(GL_ARRAY_BUFFER_ARB, self.normals_VBO)
            glNormalPointer(GL_FLOAT,0, 0)

        if self.colors != None:
            glEnableClientState(GL_COLOR_ARRAY)
            glBindBuffer(GL_ARRAY_BUFFER_ARB, self.colors_VBO)
            glColorPointer(4,GL_FLOAT,0, 0 )

        if self.uvs != None:
            glEnableClientState(GL_TEXTURE_COORD_ARRAY)
            glBindBuffer(GL_ARRAY_BUFFER_ARB, self.uvs_VBO)
            glTexCoordPointer(2,GL_FLOAT,0, 0 )
        glBindBuffer(GL_ARRAY_BUFFER_ARB,0)

        #render    
        glDrawArrays(primitive, 0, self.num_vertex)

        #clear
        glDisableClientState(GL_VERTEX_ARRAY)            
        if self.normals != None:
            glDisableClientState(GL_NORMAL_ARRAY)
        if self.colors != None:
            glDisableClientState(GL_COLOR_ARRAY)
        if self.uvs != None:
            glDisableClientState(GL_TEXTURE_COORD_ARRAY)
        glBindBuffer(GL_ARRAY_BUFFER_ARB,0)

1 个回答

0

通过查看你的代码,我发现了一个可能的问题和一个很有可能的问题:

  1. 在你上传数据的时候,你没有使用GL常量的ARB变体(就像你在渲染时那样)
  2. 在你调用glDrawArrays之前,你把数组缓冲区解绑了

如果这些建议没有帮助,试着做一个简单的可运行示例,这样我们可以一起看看怎么让它工作。

撰写回答