PyOpenGL 3.1.0 Pygame
PyOpenGL 3.1.0 with Pygame
我正在使用 Python 学习 OpenGL 库,所以我将 PyOpenGL 3.1.0 与 Python 3.6.4(和 pygame 1.9.4 用于窗口)
我看了一些视频来学习如何用 VBO 和 VAO 渲染基本的三角形,所以我写了下面的代码,但我不明白为什么我的代码没有从顶点数组渲染一个简单的矩形。 .
我想我在 vbo 中遗漏了一些关于数组属性的东西,但我不确定......有人吗?
import pygame,numpy
from OpenGL.GL import *
from OpenGL.GLU import *
display = (800,600)
#pygame
pygame.init()
pygame.display.set_mode(display,pygame.DOUBLEBUF|pygame.OPENGL)
#opengl
"""
glMatrixMode(GL_PROJECTION)
gluPerspective(45, (display[0]/display[1]), 0.1, 4000)"""
vertices = [-0.5,0.5,0,
-0.5,-0.5,0,
0.5,-0.5,0,
0.5,-0.5,0,
0.5,0.5,0,
-0.5,0.5,0]
vertices = numpy.array(vertices,dtype=numpy.float32)
vao = GLuint()
glGenVertexArrays(1,vao)
glBindVertexArray(vao)
vbo = GLuint()
glGenBuffers(1,vbo)
glBindBuffer(GL_ARRAY_BUFFER,vbo)
glBufferData(GL_ARRAY_BUFFER,len(vertices)*4,vertices,GL_STATIC_DRAW)
glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,0,0)
glBindBuffer(GL_ARRAY_BUFFER,0)
glBindVertexArray(0)
a=1
while a:
for event in pygame.event.get():
if event.type == pygame.QUIT:
a = 0
glClearColor(0, 0, 1, 0)
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT )
glBindVertexArray(vao)
glEnableVertexAttribArray(0)
glDrawArrays(GL_TRIANGLES,0,len(vertices)//3)
glDisableVertexAttribArray(0)
glBindVertexArray(0)
pygame.display.flip()
pygame.time.wait(10)
pygame.quit()
问题是对 glVertexAttribPointer
的调用。
如果绑定了命名数组缓冲区对象,则最后一个参数(第 6 个参数)将被视为缓冲区对象数据存储中的字节偏移量。参数的数据类型必须是 ctypes.c_void_p
。
这意味着您必须使用 ctypes.cast
:
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, ctypes.cast(0, ctypes.c_void_p))
或None
:
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, None)
这几天使用 shader program.
很常见
如果你不使用shader program,那么你必须使用固定功能属性(glEnableClientState
,glVertexPointer
...).
唯一的例外是顶点属性0。设置顶点属性0完全等同于设置固定函数顶点坐标数组(glVertexPointer
).
另见 What are the Attribute locations for fixed function pipeline in OpenGL 4.0++ core profile?.
我正在使用 Python 学习 OpenGL 库,所以我将 PyOpenGL 3.1.0 与 Python 3.6.4(和 pygame 1.9.4 用于窗口)
我看了一些视频来学习如何用 VBO 和 VAO 渲染基本的三角形,所以我写了下面的代码,但我不明白为什么我的代码没有从顶点数组渲染一个简单的矩形。 .
我想我在 vbo 中遗漏了一些关于数组属性的东西,但我不确定......有人吗?
import pygame,numpy
from OpenGL.GL import *
from OpenGL.GLU import *
display = (800,600)
#pygame
pygame.init()
pygame.display.set_mode(display,pygame.DOUBLEBUF|pygame.OPENGL)
#opengl
"""
glMatrixMode(GL_PROJECTION)
gluPerspective(45, (display[0]/display[1]), 0.1, 4000)"""
vertices = [-0.5,0.5,0,
-0.5,-0.5,0,
0.5,-0.5,0,
0.5,-0.5,0,
0.5,0.5,0,
-0.5,0.5,0]
vertices = numpy.array(vertices,dtype=numpy.float32)
vao = GLuint()
glGenVertexArrays(1,vao)
glBindVertexArray(vao)
vbo = GLuint()
glGenBuffers(1,vbo)
glBindBuffer(GL_ARRAY_BUFFER,vbo)
glBufferData(GL_ARRAY_BUFFER,len(vertices)*4,vertices,GL_STATIC_DRAW)
glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,0,0)
glBindBuffer(GL_ARRAY_BUFFER,0)
glBindVertexArray(0)
a=1
while a:
for event in pygame.event.get():
if event.type == pygame.QUIT:
a = 0
glClearColor(0, 0, 1, 0)
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT )
glBindVertexArray(vao)
glEnableVertexAttribArray(0)
glDrawArrays(GL_TRIANGLES,0,len(vertices)//3)
glDisableVertexAttribArray(0)
glBindVertexArray(0)
pygame.display.flip()
pygame.time.wait(10)
pygame.quit()
问题是对 glVertexAttribPointer
的调用。
如果绑定了命名数组缓冲区对象,则最后一个参数(第 6 个参数)将被视为缓冲区对象数据存储中的字节偏移量。参数的数据类型必须是 ctypes.c_void_p
。
这意味着您必须使用 ctypes.cast
:
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, ctypes.cast(0, ctypes.c_void_p))
或None
:
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, None)
这几天使用 shader program.
很常见
如果你不使用shader program,那么你必须使用固定功能属性(glEnableClientState
,glVertexPointer
...).
唯一的例外是顶点属性0。设置顶点属性0完全等同于设置固定函数顶点坐标数组(glVertexPointer
).
另见 What are the Attribute locations for fixed function pipeline in OpenGL 4.0++ core profile?.