带索引的 pyOpenGL VBO

发布于 2024-12-25 05:22:17 字数 2665 浏览 2 评论 0原文

我想使用带有索引的 VBO 在 pyOpenGL 中绘制一个矩形。 行中遇到相同的错误

我正在使用 glDrawRangeElements() 函数,但我总是在 glDrawRangeElements: WindowsError: 异常:访问冲突读取 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()

我必须承认,还没有真正了解所有这些事情,只是试图理解它,因为我需要它来完成项目,所以如果有到目前为止我忽略的任何其他错误请告诉我;)

提前致谢

I want to draw a Rectangle in pyOpenGL using VBOs with indices. I am using the glDrawRangeElements() function for that but I always get the same mistake in the line glDrawRangeElements:

WindowsError: exception: access violation reading 0x00000000

I tried a lot of things and was looking on the internet for the solution and was studying code examples all day so now I really do not know how to go on. It would be nice if someone here could help me.

This is should be the part of the code in which the error is created:

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

I have to admit that do not really have a clue of all these things yet, just trying to understand it because I need it for a project, so if there are any additional mistakes I have overlooked so far please tell my ;)

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

另类 2025-01-01 05:22:17

您的电话不应该是:

GL.glBindVertexArray (glGenVertexArray())

另外

GL.glBindVertexArray (GL.glGenVertexArray())

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

Shouldn't your call:

GL.glBindVertexArray (glGenVertexArray())

be

GL.glBindVertexArray (GL.glGenVertexArray())

Also, you probably want to keep track of that vertex array so you can delete it later and free up the resources it uses.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文