1

I would like to switch the Y and Z axis orientation in PyOpenGL. I have tried using matrix transformations but I have not been able to do it.

Code:

glMatrixMode(GL_PROJECTION)
glLoadIdentity()
glOrtho(self.zoom, -self.zoom, -self.zoom, self.zoom, -5000, 5000)
glMatrixMode(GL_MODELVIEW)
glClearColor(1, 1, 1, 0)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glLoadMatrixf(self.m)

Where:

self.zoom = 150
self.m = [[1, 0, 0, 0],
          [0, 0, 1, 0],
          [0, 1, 0, 0],
          [0, 0, 0, 1]]

Wrong result: enter image description here

Using identity matrix: enter image description here

Expected: enter image description here

Edit: This works:

        glMatrixMode(GL_PROJECTION)
        glLoadIdentity()
        glOrtho(self.zoom, -self.zoom, -self.zoom, self.zoom, -5000, 5000)
        up = 1
        if self.theta == 360:
            up = -1
        gluLookAt(self.x, self.y, self.z, 0, 0, 0, 0, up, 0)
        glMatrixMode(GL_MODELVIEW)
        glClearColor(1, 1, 1, 0)
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
        glLoadMatrixf(self.m)
Jaime02
  • 299
  • 7
  • 21
  • *"[...] but I have not been able to do it."* - You've to be more specific. What exactly is the issue? The operations seems to be syntactically and semantically correct. – Rabbid76 Dec 16 '19 at 10:36
  • @Rabbid76 I have added pictures of the result I get. The third one is edited using GIMP, it is not a real pic, it is what I would like to achieve – Jaime02 Dec 16 '19 at 13:00
  • What you mean is that the call to `glLoadMatrixf(self.m)` works, but it doesn't do what you expect it to do? – Rabbid76 Dec 16 '19 at 13:04
  • @Rabbid76 Yes, the elements in the scene are not rendered properly as you can see in the first pic – Jaime02 Dec 16 '19 at 13:05

1 Answers1

1

A 2 dimensional vector can be rotated by 90°, by swapping the components and inverting one of the components:

  • Rotate (x, y) left is (-y, x)
  • Rotate (x, y) right is (y, -x)

What you actually do is to turn the right handed matrix to a left handed matrix. It is a concatenation of a rotation by 90° and mirroring.

Change the matrix:

Either

self.m = [[1, 0,  0, 0],
          [0, 0, -1, 0],
          [0, 1,  0, 0],
          [0, 0,  0, 1]]

or

self.m = [[1,  0, 0, 0],
          [0,  0, 1, 0],
          [0, -1, 0, 0],
          [0,  0, 0, 1]]

Note, the same can be achieved by a rotation around the x-axis. e.g:

glLoadIdentity()
glRotatef(90, 1, 0, 0)

If you've a view matrix and a model matrix, then you've to multiply the modle matrix to the view matrix by glMultMatrix:

glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
gluLookAt(self.x, self.y, self.z, 0, 0, 0, 0, up, 0)
glMultMatrixf(self.m)
Rabbid76
  • 202,892
  • 27
  • 131
  • 174