3

I'm making a CAD-like 3D viewer with PyOpenGL. I managed to pan, zoom and rotate using mouse event. The problem is that I need the scene to always rotate around the center of the viewport even when the origin is translated.

With the code I have, I obtain a rotation around the translated origin:

sketch of the problem here

Here is the code snippet of those transformation:

def paintGL(self):
    aspectRatio = self.height() / self.width()

    gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT)
    gl.glLoadIdentity()
    gl.glScalef(self.zoom * aspectRatio, self.zoom , 1.0);
    gl.glTranslatef(self.xTrans, self.yTrans, -10.0)
    gl.glRotatef(self.xRot , 1.0, 0.0, 0.0)
    gl.glRotatef(self.yRot , 0.0, 1.0, 0.0)
    gl.glRotatef(self.zRot , 0.0, 0.0, 1.0)
    gl.glCallList(self.object) #drawing the objects
genpfault
  • 51,148
  • 11
  • 85
  • 139
Peter Nad
  • 31
  • 1
  • 2
    you need to translate your viewport center to `(0,0,0)` then rotate and then translate back... However You are using Euler angles which will make you mad pretty quick ... I prefer using [4x4 homogenous transform matrices](https://stackoverflow.com/a/28084380/2521214) – Spektre Oct 19 '20 at 08:01

0 Answers0