# How to rotate drawings?

Hi all,

I’m beginning in OpenGL development and I was trying to build a simple program using C.
I made a polygon and I’d like to make it rotate using the keyboard, but I can’t figure out how to do that. =<
In which order should I use glRotatef() and the drawing function to make it rotate?

Kind regards,

In order to make the correct transformation you must understand the order how OpenGL multiplies the transformation matrices.

The best resource for you is maybe the 3rd chapter of the red book: http://www.glprogramming.com/red/chapter03.html

Hi there aqnuep,
I have already read about these transformations, but my doubt actually concerns on how should I do it for rotating at runtime.
I’d like to allow my user to rotate the drawing as he wants. I have seen a sample which uses keyboard interaction but I’m not sure if my keyfunction works properly or whether the problem is the order of drawing vs. rotation.

Kind regards,

By glRotate u specify the rotation parameters which are then translated into a matrix by the GL.

The order of matrix multiplication is in reverse order of GL matrix commands.

For example, if you specify the following:

glRotate(…)
glTranslate(…)
glScale(…)

Then the first transformation applied is the scale, followed by translation and then rotation.

the reason is that every time you specify a transformation, you multiply this new transformation matrix by the current transformation matrix, and replace by the result.

New Current Matrix = New Transform Matrix * Current Matrix

Transformed Vertex = Vertex * Current Matrix

Hence the last transformation specified is applied first to the vertex.

Let me know if you need more clarification.

Hi glfreak,

I already used glRotate() and glTranslate() functions, but like I said above I’d like to make these transformations during runtime instead of applying them at compile time. The point is: what I wrote isn’t working, 'cause the glRotate() function isn’t being applied.
My doubt concerns on how should I use them, in which order, to make it rotate when my user wants, using the keyboard.

Kind regards,

I think you may need to set increment the angle of rotation or decrement it based on what rotation key is pressed.

for instance:

main rendering loop:

if (rotate left key pressed)
increment rotation angle by certain amount

if (rotate right key pressed)
decrement rotation angle by certain amount

set model view matrix

rendering calls

end main loop

Hi there,

that’s what i wrote:

``````#include <GL/glut.h>
#include <stdlib.h>

float x = 0,
y = 0;

void draw()
{
glMatrixMode(GL_MODELVIEW);

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

//cor corrente: verde (0, 127, 0)
glColor3f(0.0f, .5f, 0.0f);

glBegin(GL_TRIANGLES);
glVertex3f(0, 0, 0); //vertice superior
glVertex3f(-.2, -.4, -.5); //vertice inferior esquerdo
glVertex3f(.1, -.25, .5); //vertice inferior direito
glEnd();

glColor3f(0.0f, .6f, 0.0f);

glBegin(GL_TRIANGLES);
glVertex3f(0, 0, 0); //vertice superior
glVertex3f(.2, -.4, -.5); //vertice inferior direito
glVertex3f(.1, -.25, .5); //vertice inferior esquerdo
glEnd();

glColor3f(0.0f, .3f, 0.0f);

//base
glBegin(GL_TRIANGLES);
glVertex3f(.1, -.25, .5); //vertice superior
glVertex3f(-.2, -.4, -.5); //vertice inferior esquerdo
glVertex3f(.2, -.4, -.5); //vertice inferior direito
glEnd();

glFlush();
}

void key(unsigned char key, int x, int y)
{
switch (key)
{
case 27 :
exit(0);
break;

case 'A':
case 'a':
printf("%i
", x);
x--;
glRotatef(0, x, y, 0);
break;

case 'S':
case 's':
printf("%i
", y);
y--;
glRotatef(0, x, y, 0);
break;

case 'W':
case 'w':
printf("%i
", y);
y++;
glRotatef(0, x, y, 0);
break;

case 'D':
case 'd':
printf("%i
", x);
x++;
glRotatef(0, x, y, 0);
break;
}

glutPostRedisplay();
}

void idle()
{
glutPostRedisplay();
}

int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB |GLUT_SINGLE);
glutInitWindowSize(400,400);
glutInitWindowPosition(10,10);
glutKeyboardFunc(key);
glutDisplayFunc(draw);
glutIdleFunc(idle);
glutMainLoop();
}
``````

Very obvious you are clearing the matrix before every frame you render.

move the glRotatef’s in the key function to just right after

glMatrixMode(GL_MODELVIEW);

and use only one glRotatef

Oh BTW

glRotate(angle, x, y, z)

where x, y, z is the axis of rotation

u probalby need to change angle as well…