0

I wanted to simulate a 3d model to perform roll, pitch and yaw. I have the code below but my quad is shrinking over time until it is no longer visible. Here is a gif I made to make it clear. I tried to change perpective and translate but it didn't work.

gif

from random import randint
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.uic import *
from OpenGL.GL import *
from OpenGL.GLUT import *
from OpenGL.GLU import *


class mainWindow(QMainWindow):
    def __init__(self, *args):
        super(mainWindow, self).__init__(*args)
        loadUi('UI/gl.ui', self)

    def setupUI(self):
        self.openGLWidget.initializeGL()
        self.init()
        self.openGLWidget.resizeGL(651, 551)
        self.openGLWidget.paintGL = self.paintGL
        timer = QTimer(self)
        timer.timeout.connect(self.openGLWidget.update)
        timer.start(1000)

    def init(self):
        glShadeModel(GL_SMOOTH)
        glClearColor(0.0, 0.0, 0.0, 0.0)
        glClearDepth(1.0)
        glEnable(GL_DEPTH_TEST)
        glDepthFunc(GL_LEQUAL)
        glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST)

    def paintGL(self):
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
        glColor3f(1, 0, 0)

        random = randint(0, 178)
        roll = random  # self.data.OrientRoll[self.index]
        yaw = 0
        pitch = 0

        glRotatef(-roll, 0.00, 0.00, 1.00)
        glRotatef(pitch, 1.00, 0.00, 0.00)
        glRotatef(yaw, 0.00, 1.00, 0.00)

        glBegin(GL_QUADS)
        glColor3f(0.0, 1.0, 0.0)
        glVertex3f(1.0, 0.2, -1.0)
        glVertex3f(-1.0, 0.2, -1.0)
        glVertex3f(-1.0, 0.2, 1.0)
        glVertex3f(1.0, 0.2, 1.0)

        glColor3f(1.0, 0.5, 0.0)
        glVertex3f(1.0, -0.2, 1.0)
        glVertex3f(-1.0, -0.2, 1.0)
        glVertex3f(-1.0, -0.2, -1.0)
        glVertex3f(1.0, -0.2, -1.0)

        glColor3f(1.0, 0.0, 0.0)
        glVertex3f(1.0, 0.2, 1.0)
        glVertex3f(-1.0, 0.2, 1.0)
        glVertex3f(-1.0, -0.2, 1.0)
        glVertex3f(1.0, -0.2, 1.0)

        glColor3f(1.0, 1.0, 0.0)
        glVertex3f(1.0, -0.2, -1.0)
        glVertex3f(-1.0, -0.2, -1.0)
        glVertex3f(-1.0, 0.2, -1.0)
        glVertex3f(1.0, 0.2, -1.0)

        glColor3f(0.0, 0.0, 1.0)
        glVertex3f(-1.0, 0.2, 1.0)
        glVertex3f(-1.0, 0.2, -1.0)
        glVertex3f(-1.0, -0.2, -1.0)
        glVertex3f(-1.0, -0.2, 1.0)

        glColor3f(1.0, 0.0, 1.0)
        glVertex3f(1.0, 0.2, -1.0)
        glVertex3f(1.0, 0.2, 1.0)
        glVertex3f(1.0, -0.2, 1.0)
        glVertex3f(1.0, -0.2, -1.0)
        glEnd()
        gluPerspective(45, 1.0 * (651 / 551), 0.1, 100.0)

        glTranslatef(0.0, 0.0, -5)


app = QApplication(sys.argv)
window = mainWindow()
window.setupUI()
window.show()
sys.exit(app.exec_())
genpfault
  • 51,148
  • 11
  • 85
  • 139
Elif
  • 345
  • 3
  • 14
  • you are missing `glMatrixMode` and `glLoadIdentity` calls so you are changing lastly selected matrix over and over without initialization. if there is any `glScale` call on per frame basis this could happen. Also too many consequent matrix operations without normalization will do this among other things with your matrices ...see [How to render an openGL frame in C++ builder?](https://stackoverflow.com/a/20679773/2521214) however that one is abusing projection matrix (the camera translation should occur on modelview instead of projection but unless you are using advanced rendering stuff its OK) – Spektre Feb 25 '21 at 13:49
  • also having `gluPerspective(45, 1.0 * (651 / 551), 0.1, 100.0)` and translate after the rendering is done makes no sense ... try to replicate the math I did in the lined answer ... – Spektre Feb 25 '21 at 13:55

0 Answers0