简体   繁体   中英

Pyglet Opengl VBOs

I'm new in pyglet, B4, I've tried to use PyOpenGL with Pygame, but PyOpenGL creates weird NuffFunctionErrors, so I've moved to Pyglet.

I've tried out this code, it runs perfectly:

from pyglet.gl import *

window = pyglet.window.Window()

vertices = [
    0, 0,
    window.width, 0,
    window.width, window.height]
vertices_gl = (GLfloat * len(vertices))(*vertices)

glEnableClientState(GL_VERTEX_ARRAY)
glVertexPointer(2, GL_FLOAT, 0, vertices_gl)

@window.event
def on_draw():
    glClear(GL_COLOR_BUFFER_BIT)
    glLoadIdentity()
    glDrawArrays(GL_TRIANGLES, 0, len(vertices) // 2)

pyglet.app.run()

I've tried to rewrite this to use VBOs, but I've got a black window. What's wrong with my code?

from pyglet.gl import *

window = pyglet.window.Window()

vertices = [
    0, 0,
    window.width, 0,
    window.width, window.height]
vertices_gl = (GLfloat * len(vertices))(*vertices)


glEnableClientState(GL_VERTEX_ARRAY)

buffer=(GLuint)(0)
glGenBuffers(1,buffer)
glBindBuffer(GL_ARRAY_BUFFER_ARB, buffer)
glBufferData(GL_ARRAY_BUFFER_ARB, 4*3,
                    vertices_gl, GL_STATIC_DRAW)


glVertexPointer(2, GL_FLOAT, 0, 0)

@window.event
def on_draw():
    glClear(GL_COLOR_BUFFER_BIT)
    glLoadIdentity()
    glDrawArrays(GL_TRIANGLES, 0, len(vertices) // 2)

@window.event
def on_resize(width, height):
    glViewport(0, 0, width, height)
    glMatrixMode(gl.GL_PROJECTION)
    glLoadIdentity()
    glOrtho(0, width, 0, height, -1, 1)
    glMatrixMode(gl.GL_MODELVIEW)

pyglet.app.run()

Ok, I received the answer in comment, but the problem was that 12 bytes wan't enough 4 six floats.

Each float uses 4 byte.

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