Jump to content
  • Advertisement
Sign in to follow this  
mox601

OpenGL problem controlling camera using glulookat()

This topic is 4129 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
Advertisement
noone can help? :(

i just need a way to transform my target point of glulookat() from the mouse input...

Share this post


Link to post
Share on other sites
Hi,

I don't have the time to debug your program, but if it can help, I use an another technique.
I rotate the target and up the vector in software and then just use gluLookAt. So that i can always have access to the exact position and orientation of the camera.

This is an example in old engine:
http://texel3d.free.fr/projets/progs/abysse_src.zip
Exe can be found here:
http://texel3d.free.fr/projets/index_en.html

Share this post


Link to post
Share on other sites
thanks for the code, i will check that later!

(in what our techniques are different? i move the target around the camera using mouse input and then call gluLookat(camera, target, ... ), isn't the same?)

Share this post


Link to post
Share on other sites
>in what our techniques are different?

You use glRotate to rotate your camera.
I don't use glRotate. I use sin and cos to compute the position of the target and up vector. After that, i just have to give Position,Target,Up to gluLookAt.

Share this post


Link to post
Share on other sites
that's exactly what i intend to do!
now i am using glRotate, but i'm trying to switch to glulookat() using sin and cos as you do.
i think it will work if i'll study your code!

thanks again

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!