python - Python OpenGL VAO - 如何为顶点和颜色数据使用单独的数组
问题描述
我很难找到有关如何正确绑定和取消绑定顶点缓冲区对象和颜色缓冲区对象的信息/解释,以及当顶点和颜色数据存储在单独的数组中时对绘图调用所做的更改。
我正确的工作代码(我希望)创建 VBO,将其绑定到 VAO,并传递顶点数据以使其显示:
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *
import pygame
import numpy as np
import time
vertex_Data = [
-1.0, -1.0, -1.0,
-1.0, -1.0, 1.0,
-1.0, 1.0, 1.0,
1.0, 1.0, -1.0,
-1.0,-1.0, -1.0,
-1.0, 1.0, -1.0,
1.0, -1.0, 1.0,
-1.0, -1.0, -1.0,
1.0, -1.0, -1.0,
1.0, 1.0, -1.0,
1.0, -1.0, -1.0,
-1.0,- 1.0, -1.0,
-1.0, -1.0, -1.0,
-1.0, 1.0, 1.0,
-1.0, 1.0, -1.0,
1.0, -1.0, 1.0,
-1.0, -1.0, 1.0,
-1.0, -1.0, -1.0,
-1.0, 1.0, 1.0,
-1.0, -1.0, 1.0,
1.0, -1.0, 1.0,
1.0, 1.0, 1.0,
1.0, -1.0, -1.0,
1.0, 1.0, -1.0,
1.0, -1.0, -1.0,
1.0, 1.0, 1.0,
1.0,-1.0, 1.0,
1.0, 1.0, 1.0,
1.0, 1.0, -1.0,
-1.0, 1.0, -1.0,
1.0, 1.0, 1.0,
-1.0, 1.0, -1.0,
-1.0, 1.0, 1.0,
1.0, 1.0, 1.0,
-1.0, 1.0, 1.0,
1.0, -1.0, 1.0
]
color_Data = [
0.583, 0.771, 0.014,
0.609, 0.115, 0.436,
0.327, 0.483, 0.844,
0.822, 0.569, 0.201,
0.435, 0.602, 0.223,
0.310, 0.747, 0.185,
0.597, 0.770, 0.761,
0.559, 0.436, 0.730,
0.359, 0.583, 0.152,
0.483, 0.596, 0.789,
0.559, 0.861, 0.639,
0.195, 0.548, 0.859,
0.014, 0.184, 0.576,
0.771, 0.328, 0.970,
0.406, 0.615, 0.116,
0.676, 0.977, 0.133,
0.971, 0.572, 0.833,
0.140, 0.616, 0.489,
0.997, 0.513, 0.064,
0.945, 0.719, 0.592,
0.543, 0.021, 0.978,
0.279, 0.317, 0.505,
0.167, 0.620, 0.077,
0.347, 0.857, 0.137,
0.055, 0.953, 0.042,
0.714, 0.505, 0.345,
0.783, 0.290, 0.734,
0.722, 0.645, 0.174,
0.302, 0.455, 0.848,
0.225, 0.587, 0.040,
0.517, 0.713, 0.338,
0.053, 0.959, 0.120,
0.393, 0.621, 0.362,
0.673, 0.211, 0.457,
0.820, 0.883, 0.371,
0.982, 0.099, 0.879
]
if __name__ == '__main__':
# create and initialize window
pygame.init()
display = (1920, 1080)
pygame.display.set_mode(display, DOUBLEBUF | OPENGL)
# set perspective and near and far view limits
gluPerspective(10, (display[0] / display[1]), 0.1, 500.0)
# move and rotate world space
glTranslatef(0, 0, -80)
glRotatef(-10, 1, 0, 0)
# Enables Z depth testing
glEnable(GL_DEPTH_TEST)
# ____Vertex____
# Enables capability
# (capability to enable)
glEnableClientState(GL_VERTEX_ARRAY)
# Generates n buffer objects
# (number of names to generate)
vertex_buffer_object = glGenBuffers(1)
# Binds the buffer to a target
# (target = Vertex Array Object, name of buffer object)
# GL_ARRAY_BUFFER = vertex attributes
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer_object)
# Copies data into target (currently bound buffer)
# (Target, size in bites, data, usage)
glBufferData(GL_ARRAY_BUFFER, len(vertex_Data) * 4, np.array(vertex_Data, dtype="float32"), GL_DYNAMIC_DRAW)
# defines the array of vertex data
# (number of coordinates per vertex, data type, stride = offset between end of vertex and start of next, pointer to first coordinate = must be None for python?)
glVertexPointer(3, GL_FLOAT, 0, None)
# ____Color____
# glEnableClientState(GL_COLOR_ARRAY)
# color_buffer_object = glGenBuffers(1)
# glBindBuffer(GL_ARRAY_BUFFER, color_buffer_object)
# glBufferData(GL_ARRAY_BUFFER, len(color_Data) * 4, np.array(color_Data, dtype="float32"), GL_DYNAMIC_DRAW)
# glBindBuffer(GL_ARRAY_BUFFER, color_buffer_object)
# glColorPointer(3, GL_FLOAT, 0, None)
while True:
# enable event detection for closing the window
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
# rotate the world space
glRotatef(-10, 0, 1, 0)
# clears target buffers
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
# render primitives
# (type of primitives to be rendered, starting index, number of indices to be rendered)
glDrawArrays(GL_TRIANGLES, 0, int(len(vertex_Data) / 3))
# update window and sleep
pygame.display.flip()
time.sleep(.1)
我的代码生成了一个旋转的白色立方体,但我不完全掌握 gl 调用如何相互连接,因此我不知道如何将第二个缓冲区对象集成到 VAO 中,或者如果这就是我应该是打算做的。
从 gl 文档中,我相信我需要启用 GL_COLOR_ARRAY,创建另一个缓冲区对象(它仍将被称为 VBO 吗?),将该对象绑定到 VAO,传递颜色数据,并使用定义颜色数据数组glColorPointer。但是关于那些与 vertex_Data 的现有代码相关的顺序以及何时/如果我解除绑定,我不知所措。
任何帮助或见解将不胜感激。
解决方案
像对顶点所做的那样创建一个数组缓冲区:
color_buffer_object = glGenBuffers(1)
glBindBuffer(GL_ARRAY_BUFFER, color_buffer_object)
glBufferData(GL_ARRAY_BUFFER, len(color_Data) * 4, np.array(color_Data, dtype="float32"), GL_DYNAMIC_DRAW)
定义颜色属性数组glColorPointer
并启用客户端状态:
glBindBuffer(GL_ARRAY_BUFFER, color_buffer_object)
glColorPointer(3, GL_FLOAT, 0, None)
glEnableClientState(GL_COLOR_ARRAY)
OpenGL 是一个状态引擎。一个状态被保持直到它再次被改变。当前的数组缓冲区绑定是一个全局状态(用 设置glBindBuffer(GL_ARRAY_BUFFER, object)
)。glVertexPointer
如果使用or指定数组glColorPointer
,则必须绑定相应的缓冲区对象。调用此指令时,当前绑定到GL_ARRAY_BUFFER
目标的缓冲区与固定函数属性(顶点分别为颜色)相关联。
推荐阅读
- android - 在 onCreate() 期间通过 AsyncTask 加载数据
- c - 为什么整数隐式转换为指针?
- python - 如何在pygame中实现声音?
- java - 使用基于 java 的配置时,Dispatcherservlet 无法找到 Config 类
- python - 熊猫用同一列中的下一个可用值填充列值
- angular - 离子:this.http.get(...).map 不是函数
- python - 带同步代码的 asyncio
- c# - 实体框架核心导航属性错误
- rxjs - 使用 debounceTime 在 Angular 6 中更改来自多个范围输入的值
- android - 通过 okhttp 向服务器发送文件