Skip to main content
 首页 » 编程设计

Python Qt+OpenGL绘图区域bug

2024年10月01日8jyk

我有简单的 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 并添加一些过剩函数但没有结果。我也尝试添加矩阵乘法来缩放它,似乎显示区域有一些错误的值。

请您参考如下方法:

您必须设置视口(viewport)大小。参见 glViewport :

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

x, y Specify the lower left corner of the viewport rectangle, in pixels. The initial value is (0,0).
width, height Specify the width and height of the viewport.

glViewport specifies the affine transformation of x and y from normalized device coordinates to window coordinates.


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

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_MODELVIEW、GL_PROJECTION 和 GL_TEXTURE。所以你应该设置投影矩阵(GL_PROJECTION)和模型 View 矩阵(GL_MODELVIEW),因为每个顶点都由模型 View 矩阵和投影矩阵转换。


详见QT + OPENGL CODE EXAMPLE


对了glClearColor的参数是 [0, 1] 范围内的浮点值。更改方法 initializeGL:

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