基本的openGL,顶点缓冲区和pyglet [英] basic openGL, vertex buffers and pyglet

查看:99
本文介绍了基本的openGL,顶点缓冲区和pyglet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

rotoglup在我的代码中发现了问题,添加了我删除的着色器完成了解决方案.请参阅下面的答案以获取正确的代码(带有着色器).

rotoglup found the problems in my code, adding the shaders I had removed completed the solution. See my answer below for the correct code (with shaders).

大家好!

我正在尝试从本教程中学习现代OpenGL的一些基础知识.

I'm trying to learn some basics of modern OpenGL from this tutorial.

我想用python/pyglet代替C ++.我知道pyglet可以将许多底层的OpenGL抽象化.我想先了解一些基础知识,然后再将其隐藏在抽象层后面.

I'd like to do it with python/pyglet instead of C++ though. I know pyglet can abstract much of the low level OpenGL away; I want to understand some of the basics before moving on to hiding them behind layers of abstraction though.

我的问题非常简单:下面的代码仅画了一点,而不是我期望的3点. 据我所知,我的代码与本教程中的C ++相同,除了删除了顶点着色器和片段着色器(通过

My problem is extremely simple: the code below only draws a single point instead of the 3 I am expecting. My code is, as far as I can tell, identical to the C++ in the tutorial, except for the removal of vertex and fragment shaders (done via gletools in python), which appears to make no difference to my problem.

将事情简化为一个点会显示我不了解的行为(第一个坐标似乎是唯一会影响任何事情的坐标),这使我回到我的信念,即我只是无法理解有关任一pyglet的一些非常基本的知识,OpenGL甚至一般是3D:p

Simplifying things to a single point shows behaviour I do not understand (the first coordinate appears to be the only one that affects anything), leading me back to my belief that I've simply failed to understand something very basic about either pyglet, OpenGL, or even 3D in general :p

以下是相关代码:

import pyglet
from pyglet.gl import *

window = pyglet.window.Window()

positionBufferObject = GLuint()
vao = GLuint()

vertexPositions = [0.0, 0.0, 0.0,
                   0.25, 0.0, 0.0,
                   1.75, 1.75, 0.0]

vertexPositionsGl = (GLfloat * len(vertexPositions))(*vertexPositions)

@window.event
def on_draw():
    glBindBuffer(GL_ARRAY_BUFFER, positionBufferObject)
    glEnableVertexAttribArray(0)
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0)
    glDrawArrays(GL_POINTS, 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)

glClearColor(0.0, 0.0, 0.0, 0.0)
pyglet.app.run()

推荐答案

glDrawArrays(GL_POINTS, 0, 1)

指示绘制1个点,在您的教程中,1是3:

instructs to draw 1 point, in your tutorial, 1 is 3 :

glDrawArrays(GL_POINTS, 0, 3)

还请注意,顶点的第4个(w)分量应为1,而不是0:

Notice also that the 4th (w) component of your vertices should be 1, not 0 :

  vertexPositions = [0.0, 0.0, 0.0, 1.0,
                     0.25, 0.0, 0.0, 1.0,
                     1.75, 1.75, 0.0, 1.0]

或者,您可以删除w组件,

Alternatively, you could remove the w component,

  vertexPositions = [0.0, 0.0, 0.0,
                     0.25, 0.0, 0.0,
                     1.75, 1.75, 0.0]

,然后将以下调用更改为:

and change the following call to :

glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0)

另一件事,我不是pyglet的专家,但是glBufferData可能像C语言一样,以字节为单位,而不是以元素为单位.每个浮点数均为4个字节,您可以尝试:

Another thing, I'm not an expert in pyglet, but it's possible that glBufferData, like its C counterpart, take a size in bytes, not in elements. Each float being 4 bytes, you could try :

glBufferData(GL_ARRAY_BUFFER, len(vertexPositionsGl)*4, vertexPositionsGl, GL_STATIC_DRAW)

这篇关于基本的openGL,顶点缓冲区和pyglet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆