Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
OpenGL(python)-显示图像?_Python_Opengl - Fatal编程技术网

OpenGL(python)-显示图像?

OpenGL(python)-显示图像?,python,opengl,Python,Opengl,我正在尝试使用PyQT和用于OpenGL的Python绑定在矩形上显示图像。到目前为止,我的代码如下: gVShader = """ attribute vec4 position; attribute vec2 texture_coordinates; varying vec4 dstColor; varying vec2 v_texture_coordinates;

我正在尝试使用PyQT和用于OpenGL的Python绑定在矩形上显示图像。到目前为止,我的代码如下:

gVShader = """
              attribute vec4 position;
              attribute vec2 texture_coordinates;   
             varying vec4 dstColor;
             varying vec2 v_texture_coordinates;

            void main() {    
                v_texture_coordinates = texture_coordinates;
                gl_Position = position;    
            }"""

gFShader = """   
            uniform sampler2D texture1;
            varying vec2 v_texture_coordinates;

            void main() {

                gl_FragColor = texture2D(texture1, v_texture_coordinates);
            }"""   
class ProjectiveGLViewer(QtOpenGL.QGLWidget):

    def __init__(self, parent=None):
        super(ProjectiveGLViewer, self).__init__(parent)


    def initializeGL(self):

    # load the shaders source code

        vshader = QtOpenGL.QGLShader(QtOpenGL.QGLShader.Vertex, self)
        if not vshader.compileSourceCode(gVShader):
            print vshader.log()

        fshader = QtOpenGL.QGLShader(QtOpenGL.QGLShader.Fragment, self)
        if not fshader.compileSourceCode(gFShader):
            print fshader.log()

    # create the shader program, compile, attach shaders to the program, link and use the program

        self._program = QtOpenGL.QGLShaderProgram()
        self._program.addShader(vshader)
        self._program.addShader(fshader)
        self._program.link()
        self._program.bind()

    # data array (2 [position], 2 [texture coordinates])

        data = np.array([-1.0, -1.0, 0.0, 0.0, 1.0, -1.0, 1.0, 0.0, -1.0, 1.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0], dtype=np.float32)

    # create a buffer and bind it to the 'data' array

        self.bufferID = glGenBuffers(1)
        glBindBuffer(GL_ARRAY_BUFFER, self.bufferID)
        glBufferData(GL_ARRAY_BUFFER, data.nbytes, data, GL_DYNAMIC_DRAW)

    # tell OpenGL how to handle the buffer of data that is already on the GPU

        loc = self._program.attributeLocation("position")
        glEnableVertexAttribArray(loc)
        glVertexAttribPointer(loc, 2, GL_FLOAT, False, 16, ctypes.c_void_p(0))

        loc = self._program.attributeLocation("texture_coordinates")
        glEnableVertexAttribArray(loc)
        glVertexAttribPointer(loc, 2, GL_FLOAT, False, 16, ctypes.c_void_p(8))    

        self._imageTextureID = glGenTextures(1)

        image = QtGui.QImage("image.jpg")
        ptr = image.bits()
        ptr.setsize(image.byteCount())
        image_data = np.asarray(ptr).reshape(image.width(), image.height(), 4)

        glBindTexture(GL_TEXTURE_2D,  self._imageTextureID)
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image.width(), image.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, image_data)

        glActiveTexture(GL_TEXTURE0)
        glBindTexture(GL_TEXTURE_2D, self._imageTextureID)

        self._program.setUniformValue('texture1', 0)


    def paintGL(self):
        glBindBuffer(GL_ARRAY_BUFFER, self.bufferID)
        glClearColor(0, 0.2, 0.3, 1.0)
        glClearDepth(1.0)
        glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
        glDrawArrays(GL_TRIANGLE_STRIP, 0, 6)

    def resizeGL(self, w, h):
        glViewport(0, 0, w, h)
不太确定我做错了什么,但我得到了一个黑色矩形。有什么想法吗?

你的质地很好。您没有设置过滤和换行模式。将这些添加到
glTexImage2D
之后:

    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST )
    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST )
    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT )
    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT )

…或上传一套完整的mipmap。这很有效,谢谢!但是它被垂直翻转显示的原因(可能是我在数组中发送的坐标顺序错误?)和颜色空间有点不合适吗?你可以在着色器中翻转v坐标:
v_纹理_坐标。y=1.0-v_纹理_坐标。y
谢谢!知道颜色空间为什么会改变吗?如果颜色颠倒,将
glTexImage2D
中的第二个
GL\u RGBA
替换为
GL\u BGRA
可能会有所帮助。取决于纹理中alpha通道的预见性。