2
我想在pyOpenGL中使用帶有索引的VBO繪製矩形。我現在用的是glDrawRangeElements()函數,該函數,但我總是在行glDrawRangeElements了同樣的錯誤:pyOpenGL VBOs索引
WindowsError:異常:訪問衝突閱讀00000000
我嘗試了很多事情,並期待在互聯網上爲解決方案,並且整天研究代碼示例,所以現在我真的不知道如何繼續。如果這裏有人能幫助我,這將是很好的。
這是應該在其中創建錯誤的代碼的一部分:
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()
我不得不承認,真的沒有把這些事都線索呢,只是想了解它,因爲我需要它的一個項目,因此,如果有任何額外的錯誤我都忽略到目前爲止請告訴我;)
在此先感謝
實際上我定義了自己的函數glGenVertexArray() – 2012-01-05 08:47:02
但是,您不希望每次都在'display'函數中創建一個新函數。只需在'init'中創建一次並重用它。 – kloffy 2016-07-26 06:19:40