我正在使用PyOpenGL为Python和PyQt5编写一个3d图形工具包。如果有帮助的话,我正在编写自己的着色器。我试图做的是从使用glBegin转到使用顶点缓冲区数组。我在使用VBOs时发现了以下几点:
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 );
}
在这个项目的最终形式中,我想在我的缓冲区中包含顶点位置、颜色甚至光晕的信息。(这将在我最终将其放入ray marching时实现。)我还需要一种方法来指定是否应该绘制线和面
如何设置和配置一个或多个VBO以将所有这些信息传输到GPU和OpenGL
Python 3.7.6,Windows 10
经过一段时间的研究,我决定尝试使用不太具体的搜索词。我最终偶然发现了这个网站:https://www.metamost.com/opengl-with-python/
相关问题 更多 >
编程相关推荐