<p>OpenGL itself is purely a graphics library.  It doesn't give you access to keyboard input.</p>

<p><em>However,</em> if you're using OpenGL, you're also probably using a cross-platform <a href="https://www.khronos.org/opengl/wiki/Related_toolkits_and_APIs#Context.2FWindow_Toolkits" rel="nofollow noreferrer">context/window toolkit</a> to create a desktop window and set up an OpenGL context for drawing into that window.  These toolkits usually also give you basic keyboard and mouse input.  (The idea is, I guess, if they're abstracting platform-specific window creation, they may as well abstract platform-specific window input at the same time.)</p>

<p>Your context toolkit looks to be FreeGLUT, so you should refer to the <a href="http://freeglut.sourceforge.net/docs/api.php" rel="nofollow noreferrer">FreeGLUT documentation</a> for details.</p>

CC BY-SA 4.0
2018-10-08T07:44:21.030Z
2018-10-08T07:44:21.030Z
Maxpm
<p>I'm currently taking a Computer Graphics class at university, and have been given an assignment which involves loading an obj file and manipulating it with keyboard and mouse controls (eg. arrow keys translate, mouse wheel for zoom, hold mouse and drag for rotation).</p>

<p>Being new to OpenGL, I'm a bit overwhelmed by the project on hand, and would really appreciate if anyone could give me some tips regarding implementation of this project. Our previous (and first) project involved creating a clock based on system time, which was relatively straightforward, using simple rotation/translation techniques on OpenGL primitive objects created within the draw() function. I was able to easily figure out and understand this.</p>

<p>The main questions I have are:</p>

<ul>
<li><p>Within the draw() function, how do I take the changes made by the keyboard/mouse and apply them to the object? More specifically, what exactly should be going on here in relation to the input methods?</p>

<pre><code>void Draw() 
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);     // Clear The Screen And The Depth Buffer

// Write your own code below (Hint:Translation Rotation &amp; Scaling)


// Write your own code above


if (obj_data != NULL)
    obj_data-&gt;Draw();
else
    glutSolidTeapot(1.0);   //draw a teapot when no argument is provided

glFlush();
glutSwapBuffers();

// Write your own code below (Hint:reset translate and scale value)


// Write your own code above
</code></pre>

<p>}</p></li>
<li><p>How does the mouseMotion() method work? More specifically, how might I go about getting the coordinates of the mouse location and use them within the program?</p>

<pre><code>void mouseMotion(int x, int y)
{
//code here
glutPostRedisplay();
}
</code></pre></li>
</ul>

<p>I know this may be a vague and/or ambiguous question, but I really do not know where to start on this. My professor has not given much information as to how to implement this, and any pointers or tips that may guide me in the right direction would be incredibly appreciated!</p>

<p>Just so you know, I'm not posting this here for an easy answer so I can copy/paste. I'm mainly looking for a greater understanding of how OpenGL works, especially in relation to user input!</p>

<p>PS: The full source code for the project can be found <a href="https://pastebin.com/fKEH9zkm" rel="nofollow noreferrer">here.</a> There is also an objLoader.cpp file as well used to load a predrawn object into the workspace.</p>

<p>Let me know if more clarification is required. I've tried my best to consolidate this into smaller questions that might help me understand the project as a whole.</p>

<p>Thanks again!</p>

CC BY-SA 4.0
2018-10-08T07:45:43.490Z
You might find the [Transformations](https://learnopengl.com/Getting-started/Transformations), [Coordinate Systems](https://learnopengl.com/Getting-started/Coordinate-Systems) and [Camera](https://learnopengl.com/Getting-started/Camera) chapters of LearnOpenGL helpful.
Maxpm
CC BY-SA 4.0
2018-10-08T12:55:46.307Z
Definately check out GFLW and ImGui (super easy GUI, speedy and small footprint).
beyond
<opengl><3d><c++>
Relatively new to OpenGL, need guidance/tips on an assignment with interactive user input
Brandon Martinez
# Relatively new to OpenGL, need guidance/tips on an assignment with interactive user input

(Asked by: username_0 on 2018-10-07)

I'm currently taking a Computer Graphics class at university, and have been given an assignment which involves loading an obj file and manipulating it with keyboard and mouse controls (eg. arrow keys translate, mouse wheel for zoom, hold mouse and drag for rotation).

Being new to OpenGL, I'm a bit overwhelmed by the project on hand, and would really appreciate if anyone could give me some tips regarding implementation of this project. Our previous (and first) project involved creating a clock based on system time, which was relatively straightforward, using simple rotation/translation techniques on OpenGL primitive objects created within the draw() function. I was able to easily figure out and understand this.

The main questions I have are:

* Within the draw() function, how do I take the changes made by the keyboard/mouse and apply them to the object? More specifically, what exactly should be going on here in relation to the input methods?

  ```
  void Draw() 
  {
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);     // Clear The Screen And The Depth Buffer

  // Write your own code below (Hint:Translation Rotation & Scaling)

  // Write your own code above

  if (obj_data != NULL)
      obj_data->Draw();
  else
      glutSolidTeapot(1.0);   //draw a teapot when no argument is provided

  glFlush();
  glutSwapBuffers();

  // Write your own code below (Hint:reset translate and scale value)

  // Write your own code above

  ```

  }
* How does the mouseMotion() method work? More specifically, how might I go about getting the coordinates of the mouse location and use them within the program?

  ```
  void mouseMotion(int x, int y)
  {
  //code here
  glutPostRedisplay();
  }

  ```

I know this may be a vague and/or ambiguous question, but I really do not know where to start on this. My professor has not given much information as to how to implement this, and any pointers or tips that may guide me in the right direction would be incredibly appreciated!

Just so you know, I'm not posting this here for an easy answer so I can copy/paste. I'm mainly looking for a greater understanding of how OpenGL works, especially in relation to user input!

PS: The full source code for the project can be found [here.](https://pastebin.com/fKEH9zkm) There is also an objLoader.cpp file as well used to load a predrawn object into the workspace.

Let me know if more clarification is required. I've tried my best to consolidate this into smaller questions that might help me understand the project as a whole.

Thanks again!

**Tags:** opengl, 3d, c++

> **Comments:**
> **username_1**: You might find the [Transformations](https://learnopengl.com/Getting-started/Transformations), [Coordinate Systems](https://learnopengl.com/Getting-started/Coordinate-Systems) and [Camera](https://learnopengl.com/Getting-started/Camera) chapters of LearnOpenGL helpful.  
> **username_2**: Definately check out GFLW and ImGui (super easy GUI, speedy and small footprint).  

---

## 1 answers

### Answer by username_1 on 2018-10-08. Score: 2

OpenGL itself is purely a graphics library. It doesn't give you access to keyboard input.

*However,* if you're using OpenGL, you're also probably using a cross-platform [context/window toolkit](https://www.khronos.org/opengl/wiki/Related_toolkits_and_APIs#Context.2FWindow_Toolkits) to create a desktop window and set up an OpenGL context for drawing into that window. These toolkits usually also give you basic keyboard and mouse input. (The idea is, I guess, if they're abstracting platform-specific window creation, they may as well abstract platform-specific window input at the same time.)

Your context toolkit looks to be FreeGLUT, so you should refer to the [FreeGLUT documentation](http://freeglut.sourceforge.net/docs/api.php) for details.


---

