00001 #include "joystick.h"
00002
00003
00004
00005
00006 const GLfloat Joystick::light_ambient[] = { 0.0f, 0.0f, 0.0f, 1.0f };
00007 const GLfloat Joystick::light_diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
00008 const GLfloat Joystick::light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
00009 const GLfloat Joystick::light_position[] = { 2.0f, 5.0f, 5.0f, 0.0f };
00010
00011 const GLfloat Joystick::mat_ambient[] = { 0.7f, 0.7f, 0.7f, 1.0f };
00012 const GLfloat Joystick::mat_diffuse[] = { 0.8f, 0.8f, 0.8f, 1.0f };
00013 const GLfloat Joystick::mat_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
00014 const GLfloat Joystick::high_shininess[] = { 100.0f };
00015
00026 void
00027 Joystick::resize(int width, int height)
00028 {
00029 ar = (float) width / (float) height;
00030 }
00031
00042 void
00043 Joystick::drawButton(int button, int down) {
00044 glPushMatrix();
00045 glTranslated(-0.9+0.2*button,0.05,0.8);
00046 glScaled(0.16,0.6,0.16);
00047 if ( down )
00048 glColor3d(1.0,0.0,0.0);
00049 else
00050 glColor3d(0.4,0.3,0.3);
00051 glutSolidCube(1.0);
00052 glPopMatrix();
00053 }
00054
00065 void
00066 Joystick::display(void) {
00067 glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
00068 glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
00069 glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
00070 glLightfv(GL_LIGHT0, GL_POSITION, light_position);
00071
00072 glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
00073 glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
00074 glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
00075 glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
00076
00077 glEnable(GL_LIGHTING);
00078
00079 glMatrixMode(GL_PROJECTION);
00080 glLoadIdentity();
00081 glTranslated(0.6,0.6,0.0);
00082 glFrustum(-ar*sf, ar*sf, -sf, sf, 2.0, 8.0);
00083
00084 glMatrixMode(GL_MODELVIEW);
00085 glLoadIdentity();
00086
00087 glPushMatrix();
00088 glTranslated(0.0,-0.5,-4.5);
00089 glRotated(30,1.0,0.0,0.0);
00090
00091 glPushMatrix();
00092 glScaled(2.0,0.5,2.0);
00093 glColor3d(0.6,0.6,0.5);
00094 glutSolidCube(1.0);
00095 glPopMatrix();
00096
00097 glPushMatrix();
00098 glRotated(-35.0f*joystickAxis[0],0.0,0.0,1.0);
00099 glRotated( 35.0f*joystickAxis[1],1.0,0.0,0.0);
00100 glTranslated(0.0,1.0,0.0);
00101 glScaled(0.5,2.0,0.5);
00102 if (joystickAxis[0]!=0.0f || joystickAxis[1]!=0.0f)
00103 glColor3d(1.0,0.0,0.0);
00104 else
00105 glColor3d(0.3,0.3,0.2);
00106 glutSolidCube(1.0);
00107 glPopMatrix();
00108
00109 glPushMatrix();
00110 glTranslated(0.9*joystickAxis[2],0.0,1.1);
00111 glScaled(0.2,0.5,0.2);
00112 glColor3d(0.5,0.9,0.5);
00113 glutSolidCube(1.0);
00114 glPopMatrix();
00115
00116 drawButton( 0, joystickButtons&0x0001 );
00117 drawButton( 1, joystickButtons&0x0002 );
00118 drawButton( 2, joystickButtons&0x0004 );
00119 drawButton( 3, joystickButtons&0x0008 );
00120 drawButton( 4, joystickButtons&0x0010 );
00121 drawButton( 5, joystickButtons&0x0020 );
00122 drawButton( 6, joystickButtons&0x0040 );
00123 drawButton( 7, joystickButtons&0x0080 );
00124 drawButton( 8, joystickButtons&0x0100 );
00125 drawButton( 9, joystickButtons&0x0200 );
00126
00127 glPopMatrix();
00128
00129
00130 }
00131
00142 void
00143 Joystick::joystick(unsigned int buttons, int xaxis, int yaxis, int zaxis)
00144 {
00145 joystickAxis[0] = xaxis/1000.0f;
00146 joystickAxis[1] = yaxis/1000.0f;
00147 joystickAxis[2] = zaxis/1000.0f;
00148
00149 joystickButtons = buttons;
00150
00151 glutPostRedisplay();
00152 }
00153
00154
00155
00166 Joystick::Joystick(void) {
00167 for (int i=0; i<MAX_AXES; ++i)
00168 joystickAxis[i] = 0.0f;
00169
00170 joystickButtons = 0;
00171
00172 ar = 1;
00173 }