答案 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()