jtg | afb833d | 1999-08-19 00:55:39 +0000 | [diff] [blame] | 1 | /* $Id: glutskel.c,v 1.1 1999/08/19 00:55:42 jtg Exp $ */ |
| 2 | |
| 3 | /* |
| 4 | * A skeleton/template GLUT program |
| 5 | * |
| 6 | * Written by Brian Paul and in the public domain. |
| 7 | */ |
| 8 | |
| 9 | |
| 10 | /* |
| 11 | * $Log: glutskel.c,v $ |
| 12 | * Revision 1.1 1999/08/19 00:55:42 jtg |
| 13 | * Initial revision |
| 14 | * |
| 15 | * Revision 1.2 1998/11/07 14:20:14 brianp |
| 16 | * added simple rotation, animation of cube |
| 17 | * |
| 18 | * Revision 1.1 1998/11/07 14:14:37 brianp |
| 19 | * Initial revision |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | |
| 24 | #include <stdio.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <math.h> |
| 27 | #include <GL/glut.h> |
| 28 | |
| 29 | |
| 30 | static GLfloat Xrot = 0, Yrot = 0, Zrot = 0; |
| 31 | static GLboolean Anim = GL_FALSE; |
| 32 | |
| 33 | |
| 34 | static void Idle( void ) |
| 35 | { |
| 36 | Xrot += 3.0; |
| 37 | Yrot += 4.0; |
| 38 | Zrot += 2.0; |
| 39 | glutPostRedisplay(); |
| 40 | } |
| 41 | |
| 42 | |
| 43 | static void Display( void ) |
| 44 | { |
| 45 | glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); |
| 46 | |
| 47 | glPushMatrix(); |
| 48 | glRotatef(Xrot, 1, 0, 0); |
| 49 | glRotatef(Yrot, 0, 1, 0); |
| 50 | glRotatef(Zrot, 0, 0, 1); |
| 51 | |
| 52 | glutSolidCube(2.0); |
| 53 | |
| 54 | glPopMatrix(); |
| 55 | |
| 56 | glutSwapBuffers(); |
| 57 | } |
| 58 | |
| 59 | |
| 60 | static void Reshape( int width, int height ) |
| 61 | { |
| 62 | glViewport( 0, 0, width, height ); |
| 63 | glMatrixMode( GL_PROJECTION ); |
| 64 | glLoadIdentity(); |
| 65 | glFrustum( -1.0, 1.0, -1.0, 1.0, 5.0, 25.0 ); |
| 66 | glMatrixMode( GL_MODELVIEW ); |
| 67 | glLoadIdentity(); |
| 68 | glTranslatef( 0.0, 0.0, -15.0 ); |
| 69 | } |
| 70 | |
| 71 | |
| 72 | static void Key( unsigned char key, int x, int y ) |
| 73 | { |
| 74 | const GLfloat step = 3.0; |
| 75 | (void) x; |
| 76 | (void) y; |
| 77 | switch (key) { |
| 78 | case 'a': |
| 79 | Anim = !Anim; |
| 80 | if (Anim) |
| 81 | glutIdleFunc(Idle); |
| 82 | else |
| 83 | glutIdleFunc(NULL); |
| 84 | break; |
| 85 | case 'z': |
| 86 | Zrot -= step; |
| 87 | break; |
| 88 | case 'Z': |
| 89 | Zrot += step; |
| 90 | break; |
| 91 | case 27: |
| 92 | exit(0); |
| 93 | break; |
| 94 | } |
| 95 | glutPostRedisplay(); |
| 96 | } |
| 97 | |
| 98 | |
| 99 | static void SpecialKey( int key, int x, int y ) |
| 100 | { |
| 101 | const GLfloat step = 3.0; |
| 102 | (void) x; |
| 103 | (void) y; |
| 104 | switch (key) { |
| 105 | case GLUT_KEY_UP: |
| 106 | Xrot -= step; |
| 107 | break; |
| 108 | case GLUT_KEY_DOWN: |
| 109 | Xrot += step; |
| 110 | break; |
| 111 | case GLUT_KEY_LEFT: |
| 112 | Yrot -= step; |
| 113 | break; |
| 114 | case GLUT_KEY_RIGHT: |
| 115 | Yrot += step; |
| 116 | break; |
| 117 | } |
| 118 | glutPostRedisplay(); |
| 119 | } |
| 120 | |
| 121 | |
| 122 | static void Init( void ) |
| 123 | { |
| 124 | /* setup lighting, etc */ |
| 125 | glEnable(GL_DEPTH_TEST); |
| 126 | glEnable(GL_LIGHTING); |
| 127 | glEnable(GL_LIGHT0); |
| 128 | } |
| 129 | |
| 130 | |
| 131 | int main( int argc, char *argv[] ) |
| 132 | { |
| 133 | glutInit( &argc, argv ); |
| 134 | glutInitWindowPosition( 0, 0 ); |
| 135 | glutInitWindowSize( 400, 400 ); |
| 136 | glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH ); |
| 137 | glutCreateWindow(argv[0]); |
| 138 | glutReshapeFunc( Reshape ); |
| 139 | glutKeyboardFunc( Key ); |
| 140 | glutSpecialFunc( SpecialKey ); |
| 141 | glutDisplayFunc( Display ); |
| 142 | Init(); |
| 143 | glutMainLoop(); |
| 144 | return 0; |
| 145 | } |