OpenGL Programming/Modern OpenGL Tutorial Navigation

< OpenGL Programming

Keyboard input

Beware: GLUT's special() callback is called repetitively when the press is hold down.

Coordinates system

There are several ways to position the (X,Y,Z) axis triplet.

The right hand rule

Using the Blender Z-is-up coordinates system in OpenGL can be tempting, but introduces issues:

Consequently we'll stick with Y-is-up in this book.

Positioning the camera

There's no built-in concept of camera in OpenGL. We have to somewhat cheat to implement it.

Our technique is the following: instead of thinking how to move our camera in a fixed world, we're thinking how to move the entire world around our camera.

For instance, moving the camera for 3 units on the X axis is the same as moving the whole world for -3 units on the X axis. Same for all other axis and for rotations.

Keep this in mind when working with the camera. The end-result will feel completely intuitive, but whenever you work with the camera, if you forget that we've been cheating in the first place, you'll get the wrong transformations.

Camera control

We'll start with an intuitive implementation:

/* code here */

To go forwards/backwards, we use glm::translate. There are two forms:

For instance, if your existing transformation_matrix rotates your camera 90° to the right, and then translate along Z:


This implementation has usability issues: when rotating the camera left/right while it's bent, we're not used to rotate our entire body on this bended axis. Instead, we're used to rotate just our head on the world straight Y axis. So we need to translate the world Y axis to the camera's local coordinates:

      glm::vec3 y_axis_world = glm::mat3(transforms[MODE_CAMERA]) * glm::vec3(0.0, 1.0, 0.0);
      transforms[MODE_CAMERA] = glm::rotate(glm::mat4(1.0), -delta_rotY, y_axis_world) * transforms[MODE_CAMERA];

Last, let's implementing strafing:

/* code here */
- Comment on this page

- Recent stats

< OpenGL Programming

Browse & download complete code
This article is issued from Wikibooks. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.