Python Qt + OpenGL绘图区域bug

时间:2017-11-12 18:25:11

标签: python qt opengl pyqt4 pyopengl

我有简单的OpenGl代码,但是绘图区域没有正确显示,当我想要全尺寸时,它大约是100x30。但是我的OpenGl小部件有明确的行为。我该如何解决这个问题?

from OpenGL.GL import *
from OpenGL.GLUT import *
from OpenGL.GLU import *
from PyQt4 import QtCore, QtGui, QtOpenGL
import sys
try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

try:
    from OpenGL import GL
except ImportError:
    app = QtGui.QApplication(sys.argv)
    QtGui.QMessageBox.critical(None, "OpenGL hellogl",
            "PyOpenGL must be installed to run this example.")
    sys.exit(1)


class Ui_Form(QtGui.QWidget):
    def __init__(self):
        QtGui.QWidget.__init__(self)
        self.setupUi(self)
    def setupUi(self, Form):
        Form.setObjectName(_fromUtf8("Form"))
        Form.resize(700, 532)


        self.openGLWidget = GLWidget(Form)
        self.openGLWidget.setGeometry(QtCore.QRect(10 , 10, 512, 512))
        self.openGLWidget.setObjectName("openGLWidget")

        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        Form.setWindowTitle(_translate("Form", "Form", None))

class GLWidget(QtOpenGL.QGLWidget):
    def __init__(self, parent=None):

        super(GLWidget, self).__init__(parent)


    def initializeGL(self):
        GL.glClearColor(0,255,0,255)

    def paintGL(self):


        glEnable(GL_TEXTURE_2D)
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

        glLoadIdentity()

        gluOrtho2D(0, 1, 0, 1);

        glColor3f(1, 0, 0)
        glDisable(GL_TEXTURE_2D)
        glBegin(GL_QUADS)
        glVertex2i(0, 0)
        glVertex2i(1000, 0)
        glVertex2i(1000, 1000)
        glVertex2i(0, 1000)
        glEnd();

        glFlush()




if __name__ == '__main__':

    app = QtGui.QApplication(sys.argv)
    window = Ui_Form()
    window.show()
    sys.exit(app.exec_())

我试图更改glOrtho并添加一些过剩功能,但没有结果。此外,我尝试添加矩阵乘法来缩放它,似乎显示区域有一些错误的值。

enter image description here

1 个答案:

答案 0 :(得分:2)

您必须设置视口大小。见glViewport

  

void glViewport( GLint x, GLint y, GLsizei width, GLsizei height);

     

xy指定视口矩形的左下角(以像素为单位)。初始值为(0,0)   widthheight指定视口的宽度和高度。

     

glViewport指定xy从标准化设备坐标到窗口坐标的仿射转换。


你的代码应该看起来像这样:

def paintGL(self):

    width = 512
    height = 512

    glViewport(0, 0, width, height)
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    gluOrtho2D(0, width, 0, height)

    glMatrixMode(GL_MODELVIEW)
    glLoadIdentity()

    glColor3f(1, 0, 0)
    glBegin(GL_QUADS)
    glVertex2i(0, 0)
    glVertex2i(width, 0)
    glVertex2i(width, height)
    glVertex2i(0, height)
    glEnd()

    glFlush()

注意,在OpenGL中,每种矩阵模式都有一个矩阵堆栈(参见glMatrixMode)。矩阵模式为GL_MODELVIEWGL_PROJECTIONGL_TEXTURE。因此,您应该设置投影矩阵(GL_PROJECTION)和模型视图矩阵(GL_MODELVIEW),因为每个顶点都由模型视图矩阵和投影矩阵进行变换。


见未来QT + OPENGL CODE EXAMPLE


顺便说一下,glClearColor的参数是[0,1]范围内的浮点值。更改方法initializeGL

def initializeGL(self):
    GL.glClearColor(0,1,0,1)