我正在使用PyOpenGL为Python和PyQt5编写3d图形工具包。如果有帮助,我正在编写自己的着色器以配合使用。我想要做的是从使用glBegin到使用顶点缓冲区数组。在使用VBO时,我发现以下内容:
http://www.songho.ca/opengl/gl_vbo.html-我只能从中收集一些信息,因为它在C / C ++中。
How to get VBOs to work with Python and PyOpenGL-这是在Python2中,因此受到很大限制。
但是,我无法拼凑需要将每个形状对象的顶点并将其编译为场景VBO的东西。我也不知道数组中数据的布局方式。下面是我的initGL和paintGL函数,以及我的顶点和片段着色器的GLSL代码。
def initGL(self):
self.vertProg = open(self.vertPath, 'r')
self.fragProg = open(self.fragPath, 'r')
self.vertCode = self.vertProg.read()
self.fragCode = self.fragProg.read()
self.vertShader = shaders.compileShader(self.vertCode, GL_VERTEX_SHADER)
self.fragShader = shaders.compileShader(self.fragCode, GL_FRAGMENT_SHADER)
self.shader = shaders.compileProgram(self.vertShader, self.fragShader)
#paintGL uses shape objects, such as cube() or mesh(). Shape objects require the following:
#a list named 'vertices' - This list is a list of points, from which edges and faces are drawn.
#a list named 'wires' - This list is a list of tuples which refer to vertices, dictating where to draw wires.
#a list named 'facets' - This list is a list of tuples which refer to vertices, ditating where to draw facets.
#a bool named 'render' - This bool is used to dictate whether or not to draw the shape.
#a bool named 'drawWires' - This bool is used to dictate whether wires should be drawn.
#a bool named 'drawFaces' - This bool is used to dictate whether facets should be drawn.
def paintGL(self):
shaders.glUseProgram(self.shader)
glLoadIdentity()
gluPerspective(45, self.sizeX / self.sizeY, 0.1, 110.0) #set perspective?
glTranslatef(0, 0, self.zoomLevel) #I used -10 instead of -2 in the PyGame version.
glRotatef(self.rotateDegreeV + self.vOffset, 1, 0, 0) #I used 2 instead of 1 in the PyGame version.
glRotatef(self.rotateDegreeH, 0, 0, 1)
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
for s in self.shapes:
if s.drawWires:
glBegin(GL_LINES)
for w in s.wires:
for v in w:
glVertex3fv(s.vertices[v])
glEnd()
if s.drawFaces:
glBegin(GL_QUADS)
for f in s.facets:
for v in f:
glVertex3fv(s.vertices[v])
glEnd()
顶点着色器:
#version 120
void main() {
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}
片段着色器:
#version 120
void main() {
gl_FragColor = vec4( 0, 1, 0, 1 );
}
在该项目的最终形式中,我希望在缓冲区中保存有关顶点位置,颜色甚至发光的信息。 (这最终将在我进行光线行进时实现。)我还需要一种方法来指定是否应该绘制导线和面。
如何设置和配置一个或多个VBO,以将所有这些信息传输到GPU和OpenGL?
Python 3.7.6,Windows 10
经过一段时间的研究,我决定尝试使用不太具体的搜索字词。我最终偶然发现了这个网站:https://www.metamost.com/opengl-with-python/