OpenGL Transformation Example

The following example shows how to transform a colourful shape (square) in OpenGL/GLUT by using glRotatef(),glTranslatef() and timer functions. You can change the values of glRotatef and glTranslate for it to experience the different transformation. The shape is still in 2D. I will give a separate post later for 3D object rotation.

* TransformColourSquare.cpp
* 	This program transform a colour Square in a dark background
*	by using
*	Transformation Functions : glRotatef(),glTranslatef()
*	Timer Function : glutTimerFunc() was called every 25 milliseconds
* To compile:
*	gcc TransformColourSquare.cpp -lglut -lGL -lGLU
#include <iostream>
#include <stdlib.h>
#include <GL/glut.h>

using namespace std;

//Called when a key is pressed
void handleKeypress(unsigned char key, int x, int y) {
	switch (key) {
		case 27: //Escape key

//Initializes 3D rendering
void initRendering() {

//Called when the window is resized
void handleResize(int w, int h) {
	glViewport(0, 0, w, h);
	gluPerspective(45.0, (double)w / (double)h, 1.0, 200.0);

float _angle = 30.0f;
float _cameraAngle = 0.0f;

//Draws the 3D scene
void drawScene() {

	glMatrixMode(GL_MODELVIEW); //Switch to the drawing perspective
	glLoadIdentity(); //Reset the drawing perspective
	glRotatef(-_cameraAngle, 0.0f, 1.0f, 0.0f); //Rotate the camera
	glTranslatef(0.0f, 0.0f, -5.0f); //Move forward 5 units

	glPushMatrix(); //Save the transformations performed thus far
	glTranslatef(0.0f, 0.0f, 0.0f); //You should experience this function
	glRotatef(_angle, 0.0f, 0.0f, 1.0f); //Rotate about the z-axis

	/* draw unit square polygon */
	glColor3f(0.5f, 0.0f, 0.8f);
	glVertex3f(-0.5f, -0.5f, 0.0f);
	glColor3f(0.0f, 0.9f, 0.0f);
	glVertex3f(0.5f, -0.5f, 0.0f);
	glColor3f(1.0f, 0.0f, 0.0f);
	glVertex3f(0.5f, 0.5f, 0.0f);
	glColor3f(0.0f, 0.65f, 0.65f);
	glVertex3f(-0.5f, 0.5f, 0.0f);



void update(int value) {
	_angle += 2.0f;
	if (_angle > 360) {
		_angle -= 360;

	glutPostRedisplay(); //Tell GLUT that the display has changed

	//Tell GLUT to call update again in 25 milliseconds
	glutTimerFunc(25, update, 0);

int main(int argc, char** argv) {
	//Initialize GLUT
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
	glutInitWindowSize(400, 400);

	//Create the window
	glutCreateWindow("Colour Square Transformation");

	//Set handler functions

	//Add a timer
	glutTimerFunc(25, update, 0);

Rotating Colour Square

Rotating Colour Square

About henry416
I am a computer technology explorer and an university student based on Toronto. If you have any question, please feel free to discuss and comment here

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s