A better version …
//******************************************************************************
//******************** OpenGL Triad Demo ************************
//******************** Max H - July 25, 2010 ************************
// This demo shows how to position a small triad in the lower left corner of
// the graphics window that rotates the same way as the objects in the scene.
// To keep the code to a minimum, no lighting or Z-buffering is used.
// To translate the objects, use h,H and v,V.
// To rotate the objects, use x,y,z and X,Y,Z.
// To scale object, use s, or S.
// Use Esc key to quit program.
#include <glut.h>
#include <stdlib.h>
#include <math.h>
float xpos = 0.0, ypos = 0.0, xrot = 0.0, yrot = 0.0, zrot = 0.0, scal = 1.0;
// -----------------------------------------------------------------------------
void keyboard (unsigned char key, int x, int y)
{
switch (key) {
case 'x': xrot += 5.0; break;
case 'y': yrot += 5.0; break;
case 'z': zrot += 5.0; break;
case 'X': xrot -= 5.0; break;
case 'Y': yrot -= 5.0; break;
case 'Z': zrot -= 5.0; break;
case 's': scal *= 1.1; break;
case 'S': scal *= 0.9; break;
case 'h': xpos += 0.1; break;
case 'H': xpos -= 0.1; break;
case 'v': ypos += 0.1; break;
case 'V': ypos -= 0.1; break;
case 27: exit (0);
default : printf (" key = %c -> %d
", key, key);
}
glutPostRedisplay();
}
// -----------------------------------------------------------------------------
// Draws a 3D axis in the Z direction using two cylinders.
void Axis_3D (void)
{
GLUquadric* cyl = gluNewQuadric();
gluCylinder (cyl, 0.02, 0.02, 0.8, 16, 1); // Body of axis.
glColor3f (1,1,1); // Make arrow head white.
glPushMatrix ();
glTranslatef (0.0, 0.0, 0.8);
gluCylinder (cyl, 0.04, 0.001, 0.1, 12, 1); // Cone at end of axis.
glPopMatrix ();
}
// -----------------------------------------------------------------------------
void Triad_3D (void)
{
glColor3f (0.3, 0.3, 1.0); // Z axis in blue.
glRasterPos3f (0.0, 0.0, 1.1);
glutBitmapCharacter (GLUT_BITMAP_HELVETICA_18, 'Z');
Axis_3D ();
glColor3f (0.3, 1.0, 0.3); // Y axis is green.
glRasterPos3f (0.0, 1.1, 0.0);
glutBitmapCharacter (GLUT_BITMAP_HELVETICA_18, 'Y');
glPushMatrix ();
glRotatef (-90, 1,0,0);
Axis_3D ();
glPopMatrix ();
glColor3f (1.0, 0.3, 0.3); // X axis is red.
glRasterPos3f (1.1, 0.0, 0.0);
glutBitmapCharacter (GLUT_BITMAP_HELVETICA_18, 'X');
glPushMatrix ();
glRotatef (90, 0,1,0);
Axis_3D ();
glPopMatrix ();
}
// -----------------------------------------------------------------------------
void Corner_Triad (void)
{
glViewport (0, 0, 150, 150);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluPerspective (65.0, 1.0, 1, 20);
glMatrixMode (GL_MODELVIEW);
glLoadIdentity ();
glTranslatef (0, 0, -3); // Place small triad between clipping planes.
glRotatef (zrot, 0,0,1); // Viewing rotations.
glRotatef (yrot, 0,1,0);
glRotatef (xrot, 1,0,0);
Triad_3D ();
}
// -----------------------------------------------------------------------------
void display (void)
{
glViewport (0, 0, 800, 600);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluPerspective (65.0, 1.33, 1.0, 20.0);
glMatrixMode (GL_MODELVIEW);
glClear (GL_COLOR_BUFFER_BIT);
glLoadIdentity ();
glTranslatef (xpos, ypos, -3); // Translations.
glRotatef (zrot, 0,0,1); // Rotations.
glRotatef (yrot, 0,1,0);
glRotatef (xrot, 1,0,0);
glScalef (scal, scal, scal); // Sizing.
Triad_3D ();
Corner_Triad ();
glutSwapBuffers ();
}
// -----------------------------------------------------------------------------
int main (int argc, char** argv)
{
glutInit (&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize (800, 600);
glutInitWindowPosition (200, 100);
glutCreateWindow (argv[0]);
glutDisplayFunc (display);
glutKeyboardFunc (keyboard);
glutMainLoop ();
}
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------