| |
How to Rotate a Cube with the OpenGL API
Back to OpenGL Tutorial Index
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
#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 |
 |