<p>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?</p>

[{'answer_id': 14379, 'author': 'Ashwin Nanjappa', 'author_id': 1630, 'author_profile': 'https://Stackoverflow.com/users/1630', 'pm_score': 6, 'selected': True, 'text': '<p>Note that venerable <a href="http://www.xmission.com/~nate/glut.html" rel="noreferrer">Nate Robin\'s GLUT</a> library doesn\'t support the scrollwheel. But, later implementations of GLUT like <a href="http://freeglut.sourceforge.net/" rel="noreferrer">FreeGLUT</a> do.</p>\n\n<p>Using the scroll wheel in FreeGLUT is dead simple. Here is how:</p>\n\n<p>Declare a callback function that shall be called whenever the scroll wheel is scrolled. This is the prototype:</p>\n\n<pre><code>void mouseWheel(int, int, int, int);\n</code></pre>\n\n<p>Register the callback with the (Free)GLUT function <strong>glutMouseWheelFunc()</strong>.</p>\n\n<pre><code>glutMouseWheelFunc(mouseWheel);\n</code></pre>\n\n<p>Define the callback function. The second parameter gives the direction of the scroll. Values of +1 is forward, -1 is backward.</p>\n\n<pre><code>void mouseWheel(int button, int dir, int x, int y)\n{\n    if (dir &gt; 0)\n    {\n        // Zoom in\n    }\n    else\n    {\n        // Zoom out\n    }\n\n    return;\n}\n</code></pre>\n\n<p>That\'s it!</p>\n'}, {'answer_id': 7885789, 'author': 'BentFX', 'author_id': 710913, 'author_profile': 'https://Stackoverflow.com/users/710913', 'pm_score': 5, 'selected': False, 'text': '<p>Freeglut\'s glutMouseWheelFunc callback is version dependant and not reliable in X. Use standard mouse function and test for buttons 3 and 4.</p>\n\n<p>The OpenGlut notes on glutMouseWheelFunc state:</p>\n\n<blockquote>\n  <p>Due to lack of information about the mouse, it is impossible to\n  implement this correctly on X at this time. Use of this function\n  limits the portability of your application. (This feature does work on\n  X, just not reliably.) You are encouraged to use the standard,\n  reliable mouse-button reporting, rather than wheel events.</p>\n</blockquote>\n\n<p>Using standard GLUT mouse reporting:</p>\n\n<pre><code>#include &lt;GL/glut.h&gt;\n\n&lt;snip...&gt;\n\nvoid mouse(int button, int state, int x, int y)\n{\n   // Wheel reports as button 3(scroll up) and button 4(scroll down)\n   if ((button == 3) || (button == 4)) // It\'s a wheel event\n   {\n       // Each wheel event reports like a button click, GLUT_DOWN then GLUT_UP\n       if (state == GLUT_UP) return; // Disregard redundant GLUT_UP events\n       printf("Scroll %s At %d %d\\n", (button == 3) ? "Up" : "Down", x, y);\n   }else{  // normal button event\n       printf("Button %s At %d %d\\n", (state == GLUT_DOWN) ? "Down" : "Up", x, y);\n   }\n}\n\n&lt;snip...&gt;\n\nglutMouseFunc(mouse);\n</code></pre>\n\n<p>As the OP stated, it is "dead simple". He was just wrong.</p>\n'}, {'answer_id': 53304965, 'author': 'StackAttack', 'author_id': 4513646, 'author_profile': 'https://Stackoverflow.com/users/4513646', 'pm_score': 2, 'selected': False, 'text': '<p>observe case 3 and 4 in the switch statement below in the mouseClick callback</p>\n\n<pre><code>glutMouseFunc(mouseClick);\n</code></pre>\n\n<p>...    </p>\n\n<pre><code>void mouseClick(int btn, int state, int x, int y) {\n  if (state == GLUT_DOWN) {\n    switch(btn) {\n    case GLUT_LEFT_BUTTON:\n      std::cout &lt;&lt; "left click at: (" &lt;&lt; x &lt;&lt; ", " &lt;&lt; y &lt;&lt; ")\\n";\n      break;\n    case GLUT_RIGHT_BUTTON:\n      std::cout &lt;&lt; "right click at: (" &lt;&lt; x &lt;&lt; ", " &lt;&lt; y &lt;&lt; ")\\n";\n      break;\n    case GLUT_MIDDLE_BUTTON:\n      std::cout &lt;&lt; "middle click at: (" &lt;&lt; x &lt;&lt; ", " &lt;&lt; y &lt;&lt; ")\\n";\n      break;\n    case 3:  //mouse wheel scrolls\n      std::cout &lt;&lt; "mouse wheel scroll up\\n";\n      break;\n    case 4:\n      std::cout &lt;&lt; "mouse wheel scroll down\\n";\n      break;\n    default:\n      break;\n    }\n  }\n  glutPostRedisplay();\n}\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/14378', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1630/']