Swiftless Game Programming Site
Home Tutorials Contact
Home
News
OpenGL
GLSL
OpenCL
Maths
Misc
Work in Progress
Resume
 
 

How to rotate a shape in OpenGL

If you would like to see this site updated, please help out and

In this tutorial we are going to look at rotation of an object.

Just to make sure you know, OpenGL uses vectors.
So when using vectors, you can scale, translate and finally rotate.

To do a rotation in OpenGL, you first need to know the angle in
which you wish to rotate the current object.
This can just be held as a number which I have done with:
int angle = 0;

If you scroll down to the Display function, you will see:
glClear (GL_COLOR_BUFFER_BIT);
glLoadIdentity();
gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
glRotatef(angle, 1.0, 0.0, 0.0); //rotate on the x axis
glRotatef(angle, 0.0, 1.0, 0.0); //rotate on the y axis
glRotatef(angle, 0.0, 0.0, 1.0); //rotate on the z axis
glColor3f(1.0, 0.0, 0.0);
glutWireCube(2);
glFlush();
angle += 1;

Just forget the first 3 lines as we have looked at these, and
skip straight to:
glRotatef(angle, 1.0, 0.0, 0.0); //rotate on the x axis
glRotatef(angle, 0.0, 1.0, 0.0); //rotate on the y axis
glRotatef(angle, 0.0, 0.0, 1.0); //rotate on the z axis

The rotate function takes 4 parameters, these are:
angle of rotation
whether or not to rotate around the x axis
whether or not to rotate around the y axis
whether or not to rotate around the z axis

In that order.

So when selecting the axis to rotate around, 1 is yes, and 0 is no.

You will see that I am using the variable angle, this is set to 0
at the beginning, and if you look at the last line in Display:
angle += 1;
You can see that I am increasing the angle by 1(thats 1 degree) every
time OpenGL goes through the Display function.

Therefore:
glRotatef(angle, 1.0, 0.0, 0.0); //rotate on the x axis
glRotatef(angle, 0.0, 1.0, 0.0); //rotate on the y axis
glRotatef(angle, 0.0, 0.0, 1.0); //rotate on the z axis

Translates to:
Rotate angle degrees on the x,y and z axis

Because we have the angle updating in the display function
we call:
glutIdleFunc (display);
in the Main function, underneath the glutDisplayFunc command.

NOTE: the cube will look like a giant mess, check the smooth
rotation tutorial on how to fix this.

If you have any issues with this, please email me at swiftless@gmail.com

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
  #include <GL/gl.h>
#include <GL/glut.h>

GLfloat angle = 0.0; //the rotation value

void cube (void) {
    glRotatef(angle, 1.0, 0.0, 0.0); //rotate on the x axis
    glRotatef(angle, 0.0, 1.0, 0.0); //rotate on the y axis
    glRotatef(angle, 0.0, 0.0, 1.0); //rotate on the z axis
    glColor3f(1.0, 0.0, 0.0);
    glutWireCube(2);
}

void display (void) {
    glClearColor (0.0,0.0,0.0,1.0);
    glClear (GL_COLOR_BUFFER_BIT);
    glLoadIdentity();  
    gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
    cube();
    glFlush();
    angle ++; //update the angle of rotation
}

void reshape (int w, int h) {
    glViewport (0, 0, (GLsizei)w, (GLsizei)h);
    glMatrixMode (GL_PROJECTION);
    glLoadIdentity ();
    gluPerspective (60, (GLfloat)w / (GLfloat)h, 1.0, 100.0)
;
    glMatrixMode (GL_MODELVIEW);
}

int main (int argc, char **argv) {
    glutInit (&argc, argv);
    glutInitDisplayMode (GLUT_SINGLE);
    glutInitWindowSize (500, 500);
    glutInitWindowPosition (100, 100);
    glutCreateWindow ("A basic OpenGL Window");
    glutDisplayFunc (display);
    glutIdleFunc (display); //change any idle values accordingly

    glutReshapeFunc (reshape);
    glutMainLoop ();
    return 0;
}

Download C++ Source Code for this Tutorial

Download Visual Basic Source Code for this Tutorial


Comments:

Name   Email

Reload Image

 
     

 

Copyright 2009, Donald Urquhart AKA Swiftless
Check out: http://www.cdadc.com