OpenGL(python)-显示图像?

YaronGh

我正在尝试使用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 )

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类常见问题

使用Python显示图像

来自分类Dev

Android OpenGL ES以宽高比全屏显示图像

来自分类Dev

OpenGL显示图像的速度是否比OpenCV快?

来自分类Dev

Android OpenGL ES以宽高比全屏显示图像

来自分类Dev

纹理图像无法正确显示-OpenGL

来自分类Dev

图像未显示 python tkinter

来自分类Dev

在OpenGL(OpenTK)中显示简单图像的最简单方法?

来自分类Dev

如何显示带有OpenGL纹理的灰度图像

来自分类Dev

我在OpenGL中显示图像时遇到问题

来自分类Dev

在OpenGL(OpenTK)中显示简单图像的最简单方法?

来自分类Dev

使用CUDA OpenGL在单个窗口上显示不同大小的图像

来自分类Dev

在3D空间中显示OpenGL图像

来自分类Dev

OpenGL ES 2.0 2D图像显示

来自分类Dev

OpenGL着色器中的图像径向显示

来自分类Dev

在 openGL 上将深度缓冲区显示为图像

来自分类Dev

Python Tkinter图像标签随机显示一些图像,但不显示其他图像

来自分类Dev

Python(烧瓶)BLOB图像,SQLite,Sqlalchemy-显示图像

来自分类Dev

Python(烧瓶)BLOB图像,SQLite,Sqlalchemy-显示图像

来自分类Dev

使用imshow显示图像-Matplotlib / Python

来自分类Dev

在Python中全屏显示图像的简单示例

来自分类Dev

在视图Django Python中显示模型的图像

来自分类Dev

Python-OpenCV-读取-显示图像

来自分类Dev

Python Pygame无法正确显示图像

来自分类Dev

如何使用OpenCV Python显示图像的轮廓?

来自分类Dev

Python Tkinter .jpeg图像未显示-TclError

来自分类Dev

如何在Python中正确显示图像?

来自分类Dev

Python Gtk显示和隐藏图像

来自分类Dev

在python中渲染图像以在html中显示

来自分类Dev

如何在python中显示RGB图像

Related 相关文章

热门标签

归档