How to update data using VBO and Pyglet

I would like to create a grid with Pyglet that changes every frame. So I need to update the vertices very often and I thought VBO would be the fastest way to go here (correct me if I'm wrong). Below is an example for glasses. Is it correct? I read that the number of glBindBuffer calls should be kept to a minimum, but here it is called each frame. also included GL_DYNAMIC_DRAW, but if I change it to GL_STATIC_DRAW it still works. This makes me wonder if this is the correct setup for a quick calculation

import pyglet
import numpy as np
from pyglet.gl import *
from ctypes import pointer, sizeof

vbo_id = GLuint()
glGenBuffers(1, pointer(vbo_id))

window = pyglet.window.Window(width=800, height=800)

glClearColor(0.2, 0.4, 0.5, 1.0)

glEnableClientState(GL_VERTEX_ARRAY)

c = 0

def update(dt):
    global c
    c+=1
    data = (GLfloat*4)(*[500+c, 100+c,300+c,200+c])
    glBindBuffer(GL_ARRAY_BUFFER, vbo_id)
    glBufferData(GL_ARRAY_BUFFER, sizeof(data), 0, GL_DYNAMIC_DRAW)
    glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(data), data)


pyglet.clock.schedule(update)

glPointSize(10)

@window.event
def on_draw():

    glClear(GL_COLOR_BUFFER_BIT)
    glColor3f(0, 0, 0)

    glVertexPointer(2, GL_FLOAT, 0, 0)
    glDrawArrays(GL_POINTS, 0, 2)


pyglet.app.run()

      

+3


source to share


1 answer


You don't need to call glBufferData

every time on update - create and populate the VBO once (see setup_initial_points

) and update it with just glBufferSubData

. If you are working with only one VBO, you can also comment out the call glBindBuffer

in update()

(see code below). GL_DYNAMIC_DRAW

vs GL_STATIC_DRAW

won't make much of a difference in this example as you are pushing very little data to the GPU.



import pyglet
from pyglet.gl import *
from ctypes import pointer, sizeof

window = pyglet.window.Window(width=800, height=800)

''' update function  '''
c = 0
def update(dt):
    global c
    c+=1
    data = calc_point(c)
    # if there only on VBO, you can comment out the 'glBindBuffer' call
    glBindBuffer(GL_ARRAY_BUFFER, vbo_id)
    glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(data), data)

pyglet.clock.schedule(update)


''' draw function  '''
@window.event
def on_draw():

    glClear(GL_COLOR_BUFFER_BIT)
    glColor3f(0, 0, 0)

    glVertexPointer(2, GL_FLOAT, 0, 0)
    glDrawArrays(GL_POINTS, 0, 2)


''' calculate coordinates given counter 'c' '''
def calc_point(c):
    data = (GLfloat*4)(*[500+c, 100+c, 300+c, 200+c])
    return data


''' setup points '''
def setup_initial_points(c):
    vbo_id = GLuint()
    glGenBuffers(1, pointer(vbo_id))

    data = calc_point(c)
    glBindBuffer(GL_ARRAY_BUFFER, vbo_id)
    glBufferData(GL_ARRAY_BUFFER, sizeof(data), 0, GL_DYNAMIC_DRAW)

    return vbo_id


############################################

vbo_id = setup_initial_points(c)

glClearColor(0.2, 0.4, 0.5, 1.0)
glEnableClientState(GL_VERTEX_ARRAY)

glPointSize(10)
pyglet.app.run()

      

+9


source







All Articles