时间:2011-01-06 19:24:20

标签: python opengl pyglet

2 个答案:

答案 0 :(得分:5)

答案 1 :(得分:4)

我终于做对了!

似乎我被顶点,glDrawArrays和glVertexAttribPointer摆弄的事实误导了有时会显示一个点。 这似乎是一个意外:由rotoglup的答案提供的更正,根本没有绘制。这是我原始问题中代码的“正确”行为。

我的错误清单以防万一:

  • 将w坐标设置为0而不是1,完全没有教程中剪辑空间转换的解释。

  • 当glBufferData需要字节时,它会在顶点中给出缓冲区的大小。

  • 删除顶点和片段着色器(以便在查找上述两个问题时简化代码)。这确保了什么都不会被渲染......除了(有点儿?)单点我有时会得到。

对于记录,下面的代码显示一个三角形,通过着色器,我希望是最新的和正确的OpenGL。要求是pyglet和gletools。

import pyglet
from pyglet.gl import *
from gletools import ShaderProgram, FragmentShader, VertexShader

window = pyglet.window.Window()

positionBufferObject = GLuint()

vertexPositions = [0.75, 0.75, 0.0, 1.0,
                   0.75, -0.75, 0.0, 1.0,
                   -0.75, -0.75, 0.0, 1.0]
vertexPositionsGl = (GLfloat * len(vertexPositions))(*vertexPositions)

program = ShaderProgram(
    FragmentShader('''
    #version 330
    out vec4 outputColor;
    void main()
    {
       outputColor = vec4(1.0f, 1.0f, 1.0f, 1.0f);
    }'''),
    VertexShader('''
    #version 330
    layout(location = 0) in vec4 position;
    void main()
    {
        gl_Position = position;
    }''')
)

@window.event
def on_draw():
    with program:
        glBindBuffer(GL_ARRAY_BUFFER, positionBufferObject)
        glEnableVertexAttribArray(0)
        glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, 0)
        glDrawArrays(GL_TRIANGLES, 0, 3)
        glDisableVertexAttribArray(0)

glGenBuffers(1, positionBufferObject)
glBindBuffer(GL_ARRAY_BUFFER, positionBufferObject)
glBufferData(GL_ARRAY_BUFFER, len(vertexPositionsGl)*4, vertexPositionsGl, GL_STATIC_DRAW)
glBindBuffer(GL_ARRAY_BUFFER, 0)

pyglet.app.run()