PDA

View Full Version : question.



taztoooz
02-16-2009, 12:46 AM
hi, my code is working and compiling, but i dont know why the keyboard inputs are not working right, i am trying to use arrows to go back and front and right and left
but instead E and G are doing that which i never added in my switch case !!!! plz help




#include <gl\glut.h>

#include <gl\glu.h>

#include <math.h>

void myDisplay();
void resize(int w, int h);
void myKeyboard(unsigned char key, int x , int y);
void Skimmer();
void axis();
void DrawWheels();
void DrawRims();

static float lx=0.0f,ly=0.0f,lz=-1.0f;
GLint angle = 0;
GLint angle1 = 0;
GLint angle2 = 0;
GLint angle3 = 0;
static float x=0.0f,y=1.75f,z=5.0f;


int main(int argc, char ** argv)
{
// initialize the display window
glutInit(&argc, argv);

glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
glutInitWindowSize(1200, 1200);
glutInitWindowPosition(100, 100);
glutCreateWindow("Skimmer ");

// Callback functions
glutDisplayFunc(myDisplay);
glutReshapeFunc(resize);
glutKeyboardFunc(myKeyboard);


// opengl loop
glutMainLoop();

return 0;
}

void myDisplay()
{
glClearColor(0.66, 0.83, 0.91, 1);
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(x, y, z,
x + lx,y + ly,z + lz,
0.0f,1.0f,0.0f);

glTranslatef(0, 0, -8);

Skimmer();
glutSwapBuffers();
}
void DrawWheels()
{
glColor3f(0, 0, 0);
glutSolidTorus(0.3, 1.0, 100 ,100);
}

void DrawRims()
{
glColor3f(0.75, 0.75, 0.75);
glutSolidTorus(0.2, 0.5, 100 ,100);
}

void DrawGasTank()
{
glColor3f(0.0, 0.0, 0.41);
glutSolidSphere(0.7, 20.0, 100.0);
}

void TankLock()
{
glColor3f(0.55, 0.55, 0.55);

glutSolidTorus(0.1, 0.15, 100 ,100);
}

void DrawShaft()

{
glColor3f(0.00, 0.00, 0.00);

GLUquadricObj * qobj;
qobj = gluNewQuadric();
gluQuadricDrawStyle(qobj,GLU_FILL);
gluCylinder(qobj, 0.4, 0.4, 1.5, 80,80);

}



void Skimmer()
{
// draw front wheel & front Rims
glPushMatrix();
glTranslatef(-8, 0, 0);
DrawWheels();
DrawRims();
DrawShaft();
glPopMatrix();


glPushMatrix();
glTranslatef(6, 0, 0);
DrawWheels();
DrawRims();
DrawShaft();
glPopMatrix();

glPushMatrix();
glTranslatef(-2.0, 1.0, 0);
glScalef(1.4, 1.0,1.0);
DrawGasTank();

glPopMatrix();

glPushMatrix();
glTranslatef(-2.0, 1.2, 0);

TankLock();


glPopMatrix();

glRotatef(angle1, 1, 0, 0);
glRotatef(angle2, 0, 1, 0);
glRotatef(angle3, 0, 0, 1);
axis();


}

void axis()
{
glBegin(GL_LINES);
// x axis
glColor3f(1, 0, 0);
glVertex3f(0, 0, 0);
glVertex3f(5, 0, 0);

// y axis
glColor3f(0, 1, 0);
glVertex3f(0, 0, 0);
glVertex3f(0, 5, 0);

// z axis
glColor3f(0, 0, 1);
glVertex3f(0, 0, 0);
glVertex3f(0, 0, 5);

glEnd();
}

void orientMe(float ang) {

lx = sin(ang);
lz = -cos(ang);
glLoadIdentity();
gluLookAt(x, y, z,
x + lx,y + ly,z + lz,
0.0f,1.0f,0.0f);
}


void moveMeFlat(int direction) {
x = x + direction*(lx)*0.1;
z = z + direction*(lz)*0.1;
glLoadIdentity();
gluLookAt(x, y, z,
x + lx,y + ly,z + lz,
0.0f,1.0f,0.0f);
}



void resize(int w, int h)
{
if(h == 0)
h = 1;


glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60, (GLfloat)w/(GLfloat)h, 1, 100);
glMatrixMode(GL_MODELVIEW);
}

void myKeyboard(unsigned char key, int x, int y)
{
if (key == 27)
exit(0);

switch (key)
{
case 'q' : axis(); break;
case 'x':
angle1 = (angle1 +5 )% 360;
break;
case 'X':
angle1 = (angle1 - 5)%360;
break;
case 'y':
angle2 = (angle2 +5)%360;
break;
case 'Y':
angle2 = (angle2 -5)%360;
break;
case 'z':
angle3 = (angle3 +5)%360;
break;
case 'Z':
angle3 = (angle3 - 5)%360;
break;
case 'c':
case 'C':
gluLookAt(0.0,0.0,8.0, 0.0,0.0,0.0, 0.0, 1.0, 0.0);

break;
case GLUT_KEY_LEFT :
angle -= 0.01f;
orientMe(angle);break;
case GLUT_KEY_RIGHT :
angle +=0.01f;
orientMe(angle);break;
case GLUT_KEY_UP :
moveMeFlat(1);break;
case GLUT_KEY_DOWN :
moveMeFlat(-1);break;


}
myDisplay();

}

knackered
02-16-2009, 04:13 AM
void callbackSpecial(int k, int x, int y)
{
switch (k)
{
case GLUT_KEY_LEFT:
// etc.etc.
break;
}
}

...

int main(int argc, char ** argv)
{
...

glutSpecialFunc(callbackSpecial);

...
}