hello everyone, i'm kinda new to opengl programming, so forgive me the uncorrect things i'm going to write. i also tried looking around for a solution, but didn't find anything...
i'm writing a program, and by now i'm trying to perfect the navigation in the world, using keyboard and mouse input. i'd like to use asdw to move forward/backward and rotate left/right, and doing that with the following code:
void key(unsigned char c, int x, int y)
{
int i;
float new_position[3];
float direction[3],direction_n[3],n;
direction[0]=target[0]-eye[0];
direction[1]=target[1]-eye[1];
direction[2]=target[2]-eye[2];
n=(float)sqrt(direction[0]*direction[0]+direction[1]*direction[1]+direction[2]*direction[2]);
direction_n[0]=direction[0]/n;
direction_n[1]=direction[1]/n;
direction_n[2]=direction[2]/n;
if (c == 27)
exit(0);
/* move forward */
if (c=='w' || c=='s')
{
float sign;
sign=c=='w'?+1.0f:-1.0f;
new_position[0]=eye[0]+sign*2*direction_n[0];
new_position[1]=eye[1]+sign*2*direction_n[1];
new_position[2]=eye[2]+sign*2*direction_n[2];
/* rotate */
if (c=='a' || c=='d')
{
const float angle=(c=='a'?1.0f:-1.0f)*M_PI/180.0f;
/* X e Y esprimono il target in funzione dell'eye */
float X=direction[0]*(float)cos(angle)-direction[1]*(float)sin(angle);
float Y=direction[1]*(float)cos(angle)+direction[0]*(float)sin(angle);
target[0]=eye[0]+X;
target[1]=eye[1]+Y;
printf("x e y: %f, %f\n", X, Y);
last_crossed_wall=0;
}
glutPostRedisplay();
}
... and this works well in what it does.
the problem comes with mouse input: the movement of the mouse when it's clicked increments the anglex and y, which will be used to do two glRotatef() in the redraw function [glutDisplayFunc(redraw)].
i control mouse input like this:
void mouse(int button, int state, int x, int y)
{
if (button == GLUT_LEFT_BUTTON)
{
if (state == GLUT_DOWN)
{
moving = 1;
startx = x;
starty = y;
}
if (state == GLUT_UP)
{
moving = 0;
}
}
}
/* ---------------------------------------------------------- */
void motion(int x, int y)
{
if (moving)
{
/* since y goes up... */
angley = angley - (y - starty);
anglex = anglex + (x - startx);
float direction[3],direction_n[3],n;
direction[0]=target[0]-eye[0];
direction[1]=target[1]-eye[1];
direction[2]=target[2]-eye[2];
/* ************ i think this is the part i am messing up *********************** */
//float X=direction[0]*(float)cos(anglex)-direction[1]*(float)sin(anglex);
//float Y=direction[1]*(float)cos(anglex)+direction[0]*(float)sin(anglex);
//float Z=direction[2]*(float)cos(angley)+direction[2]*(float)sin(angley);
//target[0]=eye[0]+X;
//target[1]=eye[1]+Y;
target[2]=eye[2]+Z;
printf("angoli- y:%f, x:%f\n", angley, anglex);
startx = x;
starty = y;
glutPostRedisplay();
}
}
(anglex and angley are global vars init=0)
actually i just use two glRotatef() to take the angles and rotate the world
glPushMatrix();
/* devo modificare il target a partire da anglex e angley */
glRotatef(-angley, 1.0, 0.0, 0.0);
glRotatef(anglex, 0.0, 1.0, 0.0);
gluLookAt(
eye[0],eye[1],eye[2],
target[0],target[1],target[2],
0.0, 0.0, 1.0);
(...sorry for the bunch of code but i'm trying to be precise)
my problem is this:
turn 90° left with keyboard 'a';
turn 90° right with mouse;
at this point, walking forward with 'w' moves the camera in the "old" direction (the one reached pressing 'a'), not the new one reached with the mouse.
basically, i need to change the target[] from mouse input, but didn't find the operations to do that.
(thanks for the patience..)
---
[Added source tags. /Yann]
[Edited by - Yann L on June 4, 2007 6:08:49 PM]