2

I'm trying to mimick the 3dsmax behaviour when you zoom in/out by moving the mouse wheel. In 3ds max this zooming will be towards the mouse position. So far I've come up with this little mcve:

import math
from ctypes import c_void_p

import numpy as np
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
from glm import *


class Camera():

    def __init__(
        self,
        eye=None, target=None, up=None,
        fov=None, near=0.1, far=100000,
        **kwargs
    ):
        self.eye = vec3(eye) or vec3(0, 0, 1)
        self.target = vec3(target) or vec3(0, 0, 0)
        self.up = vec3(up) or vec3(0, 1, 0)
        self.original_up = vec3(self.up)
        self.fov = fov or radians(45)
        self.near = near
        self.far = far

    def update(self, aspect):
        self.view = lookAt(self.eye, self.target, self.up)
        self.projection = perspective(self.fov, aspect, self.near, self.far)

    def zoom(self, *args):
        delta = -args[1] * 0.1
        distance = length(self.target - self.eye)
        self.eye = self.target + (self.eye - self.target) * (delta + 1)

    def zoom_towards_cursor(self, *args):
        x = args[2]
        y = args[3]
        v = glGetIntegerv(GL_VIEWPORT)
        viewport = vec4(float(v[0]), float(v[1]), float(v[2]), float(v[3]))
        height = viewport.z

        p0 = vec3(x, height - y, 0.0)
        p1 = vec3(x, height - y, 1.0)
        v1 = unProject(p0, self.view, self.projection, viewport)
        v2 = unProject(p1, self.view, self.projection, viewport)

        world_from = vec3(
            (-v1.z * (v2.x - v1.x)) / (v2.z - v1.z) + v1.x,
            (-v1.z * (v2.y - v1.y)) / (v2.z - v1.z) + v1.y,
            0.0
        )

        self.eye.z = self.eye.z * (1.0 + 0.1 * args[1])

        view = lookAt(self.eye, self.target, self.up)
        v1 = unProject(p0, view, self.projection, viewport)
        v2 = unProject(p1, view, self.projection, viewport)

        world_to = vec3(
            (v1.z * (v2.x - v1.x)) / (v2.z - v1.z) + v1.x,
            (-v1.z * (v2.y - v1.y)) / (v2.z - v1.z) + v1.y,
            0.0
        )

        offset = world_to - world_from
        print(self.eye.z, world_from, world_to, offset)

        self.eye += offset
        self.target += offset


class GlutController():

    def __init__(self, camera):
        self.camera = camera
        self.zoom = self.camera.zoom

    def glut_mouse_wheel(self, *args):
        self.zoom(*args)


class MyWindow:

    def __init__(self, w, h):
        self.width = w
        self.height = h

        glutInit()
        glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH)
        glutInitWindowSize(w, h)
        glutCreateWindow('OpenGL Window')

        self.startup()

        glutReshapeFunc(self.reshape)
        glutDisplayFunc(self.display)
        glutMouseWheelFunc(self.controller.glut_mouse_wheel)
        glutKeyboardFunc(self.keyboard_func)
        glutIdleFunc(self.idle_func)

    def keyboard_func(self, *args):
        try:
            key = args[0].decode("utf8")

            if key == "\x1b":
                glutLeaveMainLoop()

            if key in ['1']:
                self.controller.zoom = self.camera.zoom
                print("Using normal zoom")
            elif key in ['2']:
                self.controller.zoom = self.camera.zoom_towards_cursor
                print("Using zoom towards mouse")

        except Exception as e:
            import traceback
            traceback.print_exc()

    def startup(self):
        glEnable(GL_DEPTH_TEST)

        aspect = self.width / self.height
        params = {
            "eye": vec3(10, 10, 10),
            "target": vec3(0, 0, 0),
            "up": vec3(0, 1, 0)
        }
        self.cameras = [
            Camera(**params)
        ]
        self.camera = self.cameras[0]
        self.model = mat4(1)
        self.controller = GlutController(self.camera)

    def run(self):
        glutMainLoop()

    def idle_func(self):
        glutPostRedisplay()

    def reshape(self, w, h):
        glViewport(0, 0, w, h)
        self.width = w
        self.height = h

    def display(self):
        self.camera.update(self.width / self.height)

        glClearColor(0.2, 0.3, 0.3, 1.0)
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

        glMatrixMode(GL_PROJECTION)
        glLoadIdentity()
        gluPerspective(degrees(self.camera.fov), self.width / self.height, self.camera.near, self.camera.far)
        glMatrixMode(GL_MODELVIEW)
        glLoadIdentity()
        e = self.camera.eye
        t = self.camera.target
        u = self.camera.up
        gluLookAt(e.x, e.y, e.z, t.x, t.y, t.z, u.x, u.y, u.z)
        glColor3f(1, 1, 1)
        glBegin(GL_LINES)
        for i in range(-5, 6):
            if i == 0:
                continue
            glVertex3f(-5, 0, i)
            glVertex3f(5, 0, i)
            glVertex3f(i, 0, -5)
            glVertex3f(i, 0, 5)
        glEnd()

        glBegin(GL_LINES)
        glColor3f(1, 1, 1)
        glVertex3f(-5, 0, 0)
        glVertex3f(0, 0, 0)
        glVertex3f(0, 0, -5)
        glVertex3f(0, 0, 0)

        glColor3f(1, 0, 0)
        glVertex3f(0, 0, 0)
        glVertex3f(5, 0, 0)
        glColor3f(0, 1, 0)
        glVertex3f(0, 0, 0)
        glVertex3f(0, 5, 0)
        glColor3f(0, 0, 1)
        glVertex3f(0, 0, 0)
        glVertex3f(0, 0, 5)
        glEnd()

        glutSwapBuffers()


if __name__ == '__main__':
    window = MyWindow(800, 600)
    window.run()

In this snippet you can switch between 2 zooming modes by pressing keys '1' or '2' keys.

When pressing '1' I'm doing an standard zooming, so far so good.

Problem is when pressing '2', in this case I've tried to adapt code from this thread to python/pyopengl/pygml but because I didn't understand very well the underlying maths of that answer I don't know very well how to fix the bad behaviour.

How would you fix the posted code so it will zoom in/out towards the mouse properly like 3dsmax?

BPL
  • 9,632
  • 9
  • 59
  • 117

1 Answers1

4

A possible solution is to move the camera along a ray, from the camera position through the cursor (mouse) position and to move the target position in parallel.

self.eye    = self.eye    + ray_cursor * delta
self.target = self.target + ray_cursor * delta

For this the window position of the cursor has to be "un-projected" (unProject).

Calculate the cursor position in world space (e.g. on the far plane):

pt_wnd   = vec3(x, height - y, 1.0)
pt_world = unProject(pt_wnd, self.view, self.projection, viewport)

The ray from the eye position through the cursor is given by the the normalized vector from the eye position to the world space cursor position:

ray_cursor = normalize(pt_world - self.eye)

There is an issue in your code when you get the window height from the viewport rectangle, because the height is the .w component rather than the .z component:

v = glGetIntegerv(GL_VIEWPORT)
viewport = vec4(float(v[0]), float(v[1]), float(v[2]), float(v[3]))
width  = viewport.z
height = viewport.w

Full code listing of the function zoom_towards_cursor:

def zoom_towards_cursor(self, *args):
    x = args[2]
    y = args[3]
    v = glGetIntegerv(GL_VIEWPORT)
    viewport = vec4(float(v[0]), float(v[1]), float(v[2]), float(v[3]))
    width  = viewport.z
    height = viewport.w

    pt_wnd     = vec3(x, height - y, 1.0)
    pt_world   = unProject(pt_wnd, self.view, self.projection, viewport)
    ray_cursor = normalize(pt_world - self.eye)

    delta = -args[1]
    self.eye    = self.eye    + ray_cursor * delta
    self.target = self.target + ray_cursor * delta 

See also Python OpenGL 4.6, GLM navigation

Preview:

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • 1
    Awesome, when i come back home i'll give it a shot to check possible differences with max's... but so far your method looks both simple and correct. Btw, I wont validate your answer straightaway so we'll give the chance to readers to upvote your answer properly, cos it deserves it. Once again, tyvm ;) – BPL Jan 12 '19 at 16:05
  • Fast question, would you know how to convert this code to work with Ortho cameras? Asking cos in that case there isn't the concept of `eye` or `target` and you just have `left`, `right`, `bottom`, `up`, `near`, `far` parameters instead – BPL Aug 18 '19 at 18:09
  • @BPL Zoom with orthographic projection works completely different. Since there is no perspective, the objects are not "smaller" when they are far away from the camera. You've to change the field of view. This means you've to linearly scale `left`, `right`, `bottom` and `up` dependent on a zoom factor. – Rabbid76 Aug 18 '19 at 18:26
  • That's right.. I was in the middle of refactoring my perspective camera into Camera and {OrthoCamera, PerspectiveCamera} and this is one of the first challenges I've found. Adjusting panning was easy but this one is trickier... Btw, in max you can switch from ortho to perspective so ortho cameras must also keep the eye and targets somehow... – BPL Aug 18 '19 at 18:32
  • @BPL for 1 well defined depth the orthographic projection can be switched to a perspective projection and vice versa. You can define a distance to the camera (depth) where the sides of the perspective view frustum intersect the sides of the orthographic cuboid view volume. – Rabbid76 Aug 18 '19 at 18:37
  • Interesting... Btw, if you'd to define what are the common attributes between these type of cameras, which one would you say they are? You know what... When I'm back home I'll create a proper question about this one as it's extremely interesting. I've probably implemented zillion of camera classes over the years but never figured out how to code a proper "abstract" one... As well as the common manipulation operators – BPL Aug 18 '19 at 18:43
  • [This](https://stackoverflow.com/a/23720633/3809375) looks a reasonably way to convert between perspective and orthographic planar projections. Although not sure why he chose the focus distance to be in the middle of the near and far plane. That said... It seems is reasonable to keep the camera position in the abstract camera and theorically all navigation operations would be common, panning, zomming, tilt, orbiting... – BPL Aug 18 '19 at 19:46
  • @BPL That is what I tried to explain in the last comment. It is up to you which distance you choose. Probably the distance to the object "under" the mouse. – Rabbid76 Aug 18 '19 at 19:48
  • Funny how Max switches from ortho to perspective... you'd expected the same results all the time, right? That's not the [case](https://dl.dropboxusercontent.com/s/dy105exhd1hwvas/2019-08-19_11-28-22.mp4), in that showcase you can see how I'm switching perspective-ortho-perspective-ortho-... , just look the coordinate system at the bottom-left. It's not always the same :/ – BPL Aug 19 '19 at 09:31