gpt4 book ai didi

python - pyopengl - 动态更新顶点缓冲区对象中的值

转载 作者:行者123 更新时间:2023-11-28 17:47:40 30 4
gpt4 key购买 nike

<分区>

我想在 PyOpenGL 中创建具有可拖动顶点的多边形。仔细阅读了一下,VBO 似乎是实现这一目标的明智方法。

以前从未使用过 VBO,我无法弄清楚如何动态更新它们 - 理想情况下,我只想修改 numpy 顶点数组的元素,然后仅传播元素这改变了 GPU。我曾假设 OpenGL.arrays.vbo.VBO 包装器使用其 copy_data() 方法自动执行此操作,但似乎不是。

这是一个愚蠢的例子:

from OpenGL import GL as gl
from OpenGL import GLUT as glut
from OpenGL.arrays import vbo
import numpy as np

class VBOJiggle(object):

def __init__(self,nvert=100,jiggliness=0.01):
self.nvert = nvert
self.jiggliness = jiggliness

verts = 2*np.random.rand(nvert,2) - 1
self.verts = np.require(verts,np.float32,'F')
self.vbo = vbo.VBO(self.verts)

def draw(self):

gl.glClearColor(0,0,0,0)
gl.glClear(gl.GL_COLOR_BUFFER_BIT)

gl.glEnableClientState(gl.GL_VERTEX_ARRAY)

self.vbo.bind()

gl.glVertexPointer(2,gl.GL_FLOAT,0,self.vbo)
gl.glColor(0,1,0,1)
gl.glDrawArrays(gl.GL_LINE_LOOP,0,self.vbo.data.shape[0])

gl.glDisableClientState(gl.GL_VERTEX_ARRAY)

self.vbo.unbind()

self.jiggle()

glut.glutSwapBuffers()

def jiggle(self):

# jiggle half of the vertices around randomly
delta = (np.random.rand(self.nvert//2,2) - 0.5)*self.jiggliness
self.verts[:self.nvert:2] += delta

# the data attribute of the vbo is the same as the numpy array
# of vertices
assert self.verts is self.vbo.data

# # Approach 1:
# # it seems like this ought to work, but it doesn't - all the
# # vertices remain static even though the vbo's data gets updated
# self.vbo.copy_data()

# Approach 2:
# this works, but it seems unnecessary to copy the whole array
# up to the GPU, particularly if the array is large and I have
# modified only a small subset of vertices
self.vbo.set_array(self.verts)

if __name__ == '__main__':
glut.glutInit()
glut.glutInitDisplayMode( glut.GLUT_DOUBLE | glut.GLUT_RGB )
glut.glutInitWindowSize( 250, 250 )
glut.glutInitWindowPosition( 100, 100 )
glut.glutCreateWindow( None )

demo = VBOJiggle()
glut.glutDisplayFunc( demo.draw )
glut.glutIdleFunc( demo.draw )

glut.glutMainLoop()

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com