Main Page | Modules | Namespace List | Class Hierarchy | Class List | File List | Class Members | File Members

client/joy.c

Go to the documentation of this file.
00001 #include <GL/openglut.h>
00002 
00003 #include <stdarg.h>
00004 #include <stdio.h>
00005 #include <stdlib.h>
00006 
00007 #define MAX_AXES 3
00008 
00009 static float joystickAxis[MAX_AXES];
00010 static int   joystickButtons;
00011 
00012 /* GLUT callback Handlers */
00013 
00024 static void
00025 resize(int width, int height)
00026 {
00027     const float ar = (float) width / (float) height;
00028     const float sf = 0.8f;
00029 
00030     glViewport(0, 0, width, height);
00031 
00032     glMatrixMode(GL_PROJECTION);
00033     glLoadIdentity();
00034     glFrustum(-ar*sf, ar*sf, -sf, sf, 2.0, 8.0);
00035     glMatrixMode(GL_MODELVIEW);
00036     glLoadIdentity() ;
00037 }
00038 
00049 static void
00050 drawButton(int button, int down)
00051 {
00052     glPushMatrix();
00053         glTranslated(-0.9+0.2*button,0.05,0.8);
00054         glScaled(0.16,0.6,0.16);
00055         if ( down )
00056             glColor3d(1.0,0.0,0.0);
00057         else
00058             glColor3d(0.4,0.3,0.3);
00059         glutSolidCube(1.0);
00060    glPopMatrix();
00061 }
00062 
00073 static void
00074 display(void)
00075 {
00076     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00077 
00078     glEnable(GL_LIGHTING);
00079 
00080     glPushMatrix();
00081         glTranslated(0.0,-0.5,-4.5);
00082         glRotated(30,1.0,0.0,0.0);
00083 
00084         glPushMatrix();
00085             glScaled(2.0,0.5,2.0);
00086             glColor3d(0.6,0.6,0.5);
00087             glutSolidCube(1.0);
00088         glPopMatrix();
00089 
00090         glPushMatrix();
00091             glRotated(-35.0f*joystickAxis[0],0.0,0.0,1.0);
00092             glRotated( 35.0f*joystickAxis[1],1.0,0.0,0.0);
00093             glTranslated(0.0,1.0,0.0);
00094             glScaled(0.5,2.0,0.5);
00095             if (joystickAxis[0]!=0.0f || joystickAxis[1]!=0.0f)
00096                 glColor3d(1.0,0.0,0.0);
00097             else
00098                 glColor3d(0.3,0.3,0.2);
00099             glutSolidCube(1.0);
00100         glPopMatrix();
00101 
00102         glPushMatrix();
00103             glTranslated(0.9*joystickAxis[2],0.0,1.1);
00104             glScaled(0.2,0.5,0.2);
00105             glColor3d(0.5,0.9,0.5);
00106             glutSolidCube(1.0);                        
00107         glPopMatrix();
00108 
00109         drawButton( 0, joystickButtons&0x0001 );
00110         drawButton( 1, joystickButtons&0x0002 );
00111         drawButton( 2, joystickButtons&0x0004 );
00112         drawButton( 3, joystickButtons&0x0008 );
00113         drawButton( 4, joystickButtons&0x0010 );
00114         drawButton( 5, joystickButtons&0x0020 );
00115         drawButton( 6, joystickButtons&0x0040 );
00116         drawButton( 7, joystickButtons&0x0080 );
00117         drawButton( 8, joystickButtons&0x0100 );
00118         drawButton( 9, joystickButtons&0x0200 );
00119             
00120     glPopMatrix();
00121 
00122     glutSwapBuffers();
00123 }
00124 
00135 static void
00136 joystick(unsigned int buttons, int xaxis, int yaxis, int zaxis)
00137 {
00138     joystickAxis[0] = xaxis/1000.0f;
00139     joystickAxis[1] = yaxis/1000.0f;
00140     joystickAxis[2] = zaxis/1000.0f;
00141 
00142     joystickButtons = buttons;
00143 
00144     glutPostRedisplay();
00145 }
00146 
00157 static void
00158 key(unsigned char key, int x, int y)
00159 {
00160     switch (key)
00161     {
00162     case 27 :
00163     case 'Q':
00164     case 'q': exit(0); 
00165 
00166     default:
00167         break;
00168     }
00169 
00170     glutPostRedisplay();
00171 }
00172 
00173 
00174 const GLfloat light_ambient[]  = { 0.0f, 0.0f, 0.0f, 1.0f };
00175 const GLfloat light_diffuse[]  = { 1.0f, 1.0f, 1.0f, 1.0f };
00176 const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
00177 const GLfloat light_position[] = { 2.0f, 5.0f, 5.0f, 0.0f };
00178 
00179 const GLfloat mat_ambient[]    = { 0.7f, 0.7f, 0.7f, 1.0f };
00180 const GLfloat mat_diffuse[]    = { 0.8f, 0.8f, 0.8f, 1.0f };
00181 const GLfloat mat_specular[]   = { 1.0f, 1.0f, 1.0f, 1.0f };
00182 const GLfloat high_shininess[] = { 100.0f };
00183 
00184 /* Program entry point */
00185 
00186 int
00187 main(int argc, char *argv[])
00188 {
00189     int i;
00190 
00191     glutInitWindowSize(640,480);
00192     glutInitWindowPosition(40,40);
00193     glutInit(&argc, argv);
00194     glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
00195 
00196     glutCreateWindow("OpenGLUT Joystick");
00197 
00198     glutReshapeFunc(resize);
00199     glutDisplayFunc(display);
00200     glutJoystickFunc(joystick,50);
00201     glutKeyboardFunc(key);
00202 
00203     glClearColor(1,1,1,1);
00204     glEnable(GL_CULL_FACE);
00205     glCullFace(GL_BACK);
00206 
00207     glEnable(GL_DEPTH_TEST);
00208     glDepthFunc(GL_LESS);
00209 
00210     glEnable(GL_LIGHT0);
00211     glEnable(GL_NORMALIZE);
00212     glEnable(GL_COLOR_MATERIAL);
00213 
00214     glLightfv(GL_LIGHT0, GL_AMBIENT,  light_ambient);
00215     glLightfv(GL_LIGHT0, GL_DIFFUSE,  light_diffuse);
00216     glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
00217     glLightfv(GL_LIGHT0, GL_POSITION, light_position);
00218 
00219     glMaterialfv(GL_FRONT, GL_AMBIENT,   mat_ambient);
00220     glMaterialfv(GL_FRONT, GL_DIFFUSE,   mat_diffuse);
00221     glMaterialfv(GL_FRONT, GL_SPECULAR,  mat_specular);
00222     glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
00223 
00224     if( glutDeviceGet(GLUT_HAS_JOYSTICK) )
00225     {
00226         printf("Joystick detected.\n");
00227         printf("%d joystick axes.\n",glutDeviceGet(GLUT_JOYSTICK_AXES));
00228         printf("Joystick poll rate is %d.\n",glutDeviceGet(GLUT_JOYSTICK_POLL_RATE));
00229         printf("Joystick %savailable.\n",glutDeviceGet(GLUT_OWNS_JOYSTICK) ? "" : "un");
00230     }
00231     else
00232         printf("Joystick not detected.\n");
00233 
00234     for (i=0; i<MAX_AXES; ++i)
00235         joystickAxis[i] = 0.0f;
00236 
00237     joystickButtons = 0;
00238 
00239     glutMainLoop();
00240 
00241     return EXIT_SUCCESS;
00242 }

Generated on Fri Sep 1 14:25:44 2006 for Raptor by  doxygen 1.4.4