Search code examples
openglglutscrollwheel

Using the mouse scrollwheel in GLUT


I want to use the mouse scrollwheel in my OpenGL GLUT program to zoom in and out of a scene? How do I do that?


Solution

  • Note that venerable Nate Robin's GLUT library doesn't support the scrollwheel. But, later implementations of GLUT like FreeGLUT do.

    Using the scroll wheel in FreeGLUT is dead simple. Here is how:

    Declare a callback function that shall be called whenever the scroll wheel is scrolled. This is the prototype:

    void mouseWheel(int, int, int, int);
    

    Register the callback with the (Free)GLUT function glutMouseWheelFunc().

    glutMouseWheelFunc(mouseWheel);
    

    Define the callback function. The second parameter gives the direction of the scroll. Values of +1 is forward, -1 is backward.

    void mouseWheel(int button, int dir, int x, int y)
    {
        if (dir > 0)
        {
            // Zoom in
        }
        else
        {
            // Zoom out
        }
    
        return;
    }
    

    That's it!