Tuesday, July 14, 2009

OpenGL: 3D Cube, Rotation, Translation and Texts

Second hour with OpenGL, last time I've written a simple 2D example to learn what Vertex are, and now a little step forward using 3D, Rotation, Translation and Texts.

Using GLUT writing 2D texts is really simple, and here is how to do it. Setup Text Color, X and Y Location, pick up a font and set your string, and it's done.


glColor3f(1.0, 0.0, 0.0);
glRasterPos2f(-4.6, 2.3);
glutBitmapString(GLUT_BITMAP_TIMES_ROMAN_24, 'Hello Text');


To handle left, right, up, down keys to move the cube use the glutSpecialFunc() that allows you to handle GLUT_KEY_LEFT, GLUT_KEY_RIGHT, GLUT_KEY_UP, GLUT_KEY_DOWN keys and others. Translation and rotation are made using glTranslatef() and glRotatef().

The source code is available here: Python OpenGL 3D Cube Source Code.

No comments:

Post a Comment