简体   繁体   中英

Pyglet VBOs glVertexPointer

I've used PyOpenGL for a long time, but now, I've switched to PyGlet. PyGlet is more strict the PyOpenGL, so I'm now learning it. I've written a simple code using VBOs to render a triangle(I've took the base from a PyGlet tutorial), but it doesn't works for me.

It shoud render a colored triangle, but instead of, It renders a white triangle. What I does wrong?

Here's my code:

from pyglet.gl import *

window = pyglet.window.Window()
glEnableClientState(GL_VERTEX_ARRAY)
vertices = [
    0,0,0,
    window.width,0,0,
    window.width,window.height,0]

colors=[1.0,0.0,0.0,
        0.0,1.0,0.0,
        0.0,0.0,1.0]

class VBO():
    def __init__(self):
        self.buffer=(GLuint)(0)
        glGenBuffers(1,self.buffer)
    def data(self,data):
        data_gl= (GLfloat * len(data))(*data)
        glBindBuffer(GL_ARRAY_BUFFER_ARB, self.buffer)
        glBufferData(GL_ARRAY_BUFFER_ARB, len(data)*4,
                    data_gl, GL_STATIC_DRAW)

    def bind(self):
        glBindBuffer(GL_ARRAY_BUFFER_ARB, self.buffer)
    def vertex(self):
        self.bind()
        glVertexPointer(3, GL_FLOAT, 0, 0)
    def color(self):
        self.bind()
        glColorPointer(3, GL_FLOAT, 0, 0) 




vbo=VBO()
vbo.data(vertices)
color=VBO()
color.data(colors)

@window.event
def on_draw():
    glClear(GL_COLOR_BUFFER_BIT)
    glLoadIdentity()
    vbo.vertex()
    color.color()
    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()

Thanks in advance!

I was able to get colors displayed by adding the following:

...

glClientStateEnable(GL_COLOR_ARRAY) 

vbo=VBO()
vbo.data(vertices)
color=VBO()
color.data(colors)

...

The embedded documentation for pyglet was singularly unhelpful, however the man page for glColorPointer was quite clear in pointing out that color arrays were ignored by glDrawArray until GL_COLOR_ARRAY was enabled via glClientStateEnable.

用彩色顶点绘制的三角形。

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