Python OSX上的PyOpenGL和PyQt5遇到问题

Python OSX上的PyOpenGL和PyQt5遇到问题,python,macos,graphics,pyqt5,pyopengl,Python,Macos,Graphics,Pyqt5,Pyopengl,本学期我将学习计算机图形学入门,目前正在学习使用PyOpenGL和PyQt5探索重心坐标。我的同学在使用Windows机器时,能够直接运行以下代码而不会出现任何问题,但我们在OSX上的所有人都遇到了同样的问题。我希望在这种环境下有更多经验的其他人可能知道我们如何解决这些问题;我们已经在谷歌上做了很多搜索,但没有结果。谢谢 代码: 导入系统 从数组导入数组 从ctypes导入c\u void\u p 从OpenGL.GL导入* 从OpenGL.GLU导入* 从PyQt5.QtGui导入QImage

本学期我将学习计算机图形学入门,目前正在学习使用PyOpenGL和PyQt5探索重心坐标。我的同学在使用Windows机器时,能够直接运行以下代码而不会出现任何问题,但我们在OSX上的所有人都遇到了同样的问题。我希望在这种环境下有更多经验的其他人可能知道我们如何解决这些问题;我们已经在谷歌上做了很多搜索,但没有结果。谢谢

代码:

导入系统 从数组导入数组 从ctypes导入c\u void\u p 从OpenGL.GL导入* 从OpenGL.GLU导入* 从PyQt5.QtGui导入QImage,qRgb 从PyQt5.QtOpenGL导入QGLWidget 从PyQt5.QtWidgets导入QApplication 从textwrap导入dedent #创建通过软件光栅化绘制三角形的函数 def软件光栅化(宽度、高度、顶点、颜色): image=QImage(宽度、高度、QImage.Format_RGB32) #TODO:计算给定顶点周围的边界框 #TODO:计算框中每个点的重心坐标 #TODO:为三角形内部的点着色 如果image.save(“triangle.jpg”,无,100): 打印(“Output triangle.jpg”) 其他: 打印(“无法保存triangle.jpg”) #扩展QGLWidget,通过硬件光栅化绘制三角形 类硬件擦除小部件(QGLWidget): 定义初始值(自身、顶点、颜色、*args、**kwargs): super() self.vertices=数组('f') #TODO:将输入坐标转换为标准化设备坐标 self.colors=数组('f',颜色) def_sizeof(自身,a): 返回a.itemsize*len(a) def初始化EGL(自身): verticesSize=self.\u sizeof(self.vertices) #在GPU上创建一个新的顶点数组对象,保存属性 #顶点的布局 vao=GLGEnVertexArray(1) glBindVertexArray(vao) #在GPU上为位置和颜色数据创建缓冲区 dataBuffer=glGenBuffers(1) #将数据上传到GPU,并将其存储在我们刚刚创建的缓冲区中 #TODO:将颜色数据也上传到GPU缓冲区 glBindBuffer(GLU数组缓冲区,数据缓冲区) glBufferData( GL_数组_缓冲区, 使变粗, 没有一个 GL\u静态图 ) glBufferSubData( GL_数组_缓冲区, 0, 使变粗, self.vertices.tostring() ) #将顶点和片段着色器加载到GPU上的程序对象中 program=self.loadShaders() #将属性“位置”(在顶点着色器中定义)绑定到 #当前绑定的缓冲区对象,其中包含我们的位置数据 #此信息存储在顶点数组对象中 位置=GLGetAttriblLocation(程序“位置”) GlenableVertexAttributeArray(位置) glvertexattributepointer( 立场,, 3. 浮球, GL_FALSE, 0, c_void_p(0) ) #TODO:将属性“颜色”绑定到缓冲区对象 def加载着色器(自): #创建GL程序对象 program=glCreateProgram() #顶点着色器 #TODO:添加颜色输入和颜色输出 vs_source=dedent(“”) #330版 在vec3位置; void main() { gl_位置=vec4(位置,1.0); }\ """) vs=glCreateShader(GLU顶点着色器) glShaderSource(vs,vs_源) glCompileShader(vs) 格拉塔沙德(项目,vs) 如果glGetShaderiv(vs,GL编译状态)!=GL_TRUE: 引发运行时错误(glGetShaderInfoLog(vs)) #片段着色器 #TODO:添加与顶点输出同名的颜色输入 fs_source=dedent(“”) #330版 void main() { gl_FragColor=vec4(1.0,1.0,1.0,1.0); }\ """) fs=glCreateShader(GL\u片段\u着色器) glShaderSource(fs,fs_源) glCompileShader(fs) 格拉塔沙德(项目,财政司司长) 如果glGetShaderiv(fs,GL编译状态)!=GL_TRUE: 引发运行时错误(glGetShaderInfoLog(fs)) #使用程序 glLinkProgram(程序) glUseProgram(程序) 返回程序 def paintGL(自我): glClear(GL_颜色_缓冲区_位| GL_深度_缓冲区_位) GLDrawArray(GLU三角形,0,3) def resizeGL(自身、宽度、高度): glViewport(0,0,宽度,高度) 如果名称=“\uuuuu main\uuuuuuuu”: 宽度=640 高度=480 #TODO:提示用户输入3个点和以空格分隔的颜色 #TODO:验证输入并解析到顶点和颜色列表中 顶点=[ 50,50,0,#垂直1 600,20,0,2 300,400,0#3 ] 颜色=[ 1,0,0,#颜色1 0,1,0,#颜色2 0,0,1#颜色3 ] 软件光栅化(宽度、高度、顶点、颜色) app=QApplication(sys.argv) w=硬件擦除小部件(顶点、颜色) pRatio=w.设备固定比率() w、 调整大小(宽度/尺寸、高度/尺寸) w、 show() import sys from array import array from ctypes import c_void_p from OpenGL.GL import * from OpenGL.GLU import * from PyQt5.QtGui import QImage, qRgb from PyQt5.QtOpenGL import QGLWidget from PyQt5.QtWidgets import QApplication from textwrap import dedent # create a function that draws a triangle via software rasterization def softwareRasterization(width, height, vertices, colors): image = QImage(width, height, QImage.Format_RGB32) # TODO: compute the bounding box around the given vertices # TODO: compute the barycentric coordinates for each point in the box # TODO: color the points that are inside of the triangle if image.save("triangle.jpg", None, 100): print("Output triangle.jpg") else: print("Unable to save triangle.jpg") # extend QGLWidget to draw a triangle via hardware rasterization class HardwareRasterizationWidget(QGLWidget): def __init__(self, vertices, colors, *args, **kwargs): super().__init__(*args, **kwargs) self.vertices = array('f') # TODO: convert the input coordinate to normalized device coordinates self.colors = array('f', colors) def _sizeof(self, a): return a.itemsize * len(a) def initializeGL(self): verticesSize = self._sizeof(self.vertices) # create a new Vertex Array Object on the GPU which saves the attribute # layout of our vertices vao = glGenVertexArrays(1) glBindVertexArray(vao) # create a buffer on the GPU for position and color data dataBuffer = glGenBuffers(1) # upload the data to the GPU, storing it in the buffer we just created # TODO: upload the color data into the GPU buffer as well glBindBuffer(GL_ARRAY_BUFFER, dataBuffer) glBufferData( GL_ARRAY_BUFFER, verticesSize, None, GL_STATIC_DRAW ) glBufferSubData( GL_ARRAY_BUFFER, 0, verticesSize, self.vertices.tostring() ) # load our vertex and fragment shaders into a program object on the GPU program = self.loadShaders() # bind the attribute "position" (defined in our vertex shader) to the # currently bound buffer object, which contains our position data # this information is stored in our vertex array object position = glGetAttribLocation(program, 'position') glEnableVertexAttribArray(position) glVertexAttribPointer( position, 3, GL_FLOAT, GL_FALSE, 0, c_void_p(0) ) # TODO: bind the attribute "color" to the buffer object def loadShaders(self): # create a GL Program Object program = glCreateProgram() # vertex shader # TODO: add a color input and color output vs_source = dedent(""" #version 330 in vec3 position; void main() { gl_Position = vec4(position, 1.0); }\ """) vs = glCreateShader(GL_VERTEX_SHADER) glShaderSource(vs, vs_source) glCompileShader(vs) glAttachShader(program, vs) if glGetShaderiv(vs, GL_COMPILE_STATUS) != GL_TRUE: raise RuntimeError(glGetShaderInfoLog(vs)) # fragment shader # TODO: add a color input with the same name as the vertex output fs_source = dedent(""" #version 330 void main() { gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0); }\ """) fs = glCreateShader(GL_FRAGMENT_SHADER) glShaderSource(fs, fs_source) glCompileShader(fs) glAttachShader(program, fs) if glGetShaderiv(fs, GL_COMPILE_STATUS) != GL_TRUE: raise RuntimeError(glGetShaderInfoLog(fs)) # use the program glLinkProgram(program) glUseProgram(program) return program def paintGL(self): glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) glDrawArrays(GL_TRIANGLES, 0, 3) def resizeGL(self, width, height): glViewport(0, 0, width, height) if __name__ == "__main__": width = 640 height = 480 # TODO: prompt the user for 3 points and colors separated by spaces # TODO: validate input and parse into the vertices and colors lists vertices = [ 50, 50, 0, # vertice 1 600, 20, 0, # vertice 2 300, 400, 0 # vertice 3 ] colors = [ 1, 0, 0, # color 1 0, 1, 0, # color 2 0, 0, 1 # color 3 ] softwareRasterization(width, height, vertices, colors) app = QApplication(sys.argv) w = HardwareRasterizationWidget(vertices, colors) pRatio = w.devicePixelRatio() w.resize(width/pRatio, height/pRatio) w.show() sys.exit(app.exec_()) Traceback (most recent call last): File "/Users/JesseRichmond/vEnvPyCharm/lib/python3.5/site-packages/OpenGL/latebind.py", line 41, in __call__ return self._finalCall( *args, **named ) TypeError: 'NoneType' object is not callable During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/Users/JesseRichmond/Desktop/Spring 2017 Academics/Projects/310/rasterization.py", line 45, in initializeGL vao = glGenVertexArrays(1) File "/Users/JesseRichmond/vEnvPyCharm/lib/python3.5/site-packages/OpenGL/latebind.py", line 45, in __call__ return self._finalCall( *args, **named ) File "/Users/JesseRichmond/vEnvPyCharm/lib/python3.5/site-packages/OpenGL/wrapper.py", line 657, in wrapperCall result = wrappedOperation( *cArguments ) File "/Users/JesseRichmond/vEnvPyCharm/lib/python3.5/site-packages/OpenGL/platform/baseplatform.py", line 407, in __call__ self.__name__, self.__name__, OpenGL.error.NullFunctionError: Attempt to call an undefined function glGenVertexArrays, check for bool(glGenVertexArrays) before calling