OpenGL Programming/Basics/Transformations

Transformations

edit

In preparation for drawing more complex 2D and 3D shapes, we should understand what transformations are. Transformations allow you to move objects around your window, as well as rotate and scale them.

Translations

edit

Translations allow you to draw objects up, down, left, and right in your 2D scene. (When we get to 3D, you'll be drawing using depth coordinates too.) Just like we positioned points and lines, we translate around the scene in preparation of drawing an object in that place.

Translations are accomplished using:

glTranslatef(xTranslation, yTranslation, zTranslation);

Let's demonstrate how translations work by editing the display function of our basic OpenGL template.

void display() {
       glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
       glColor3f(0.0f, 0.0f, 0.0f);
       glTranslatef(0.5f, 0.5f, 0.5f);
       glutSolidTeapot(0.25);
       glutSwapBuffers();
}

Here, we set the color to black using glColor3f, then translate 0.5 up and 0.5 to the right. (We haven't talked about Z much yet, so we'll just leave that 0.0 for now.) The result is a teapot drawn in the upper right hand corner of the screen.

Rotations

edit

Scaling

edit