【发布时间】:2019-06-14 20:21:03
【问题描述】:
我正在使用 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()
【问题讨论】:
标签: python pygame vbo pyopengl