Rotate Camera

This a really simple question… but I couldn’t manage to get the math equations right.

I managed to write the code to rotate the camera around the y-axis (looking left and right).
However, I cant make my camera to rotate around the x-axis (like if i was looking up and down with my camera).

void Camera::RS_Camera_RotateSideWays(float speed)
{

	//vVector = View - Pos;	// Get the view vector
	Vec3 vVector = Camera->View -Camera->Pos;	// Get the view vector
	
	Camera->View.z = (float)(Camera->Pos.z + SINE(speed)*vVector.x + COSINE(speed)*vVector.z);
	Camera->View.x = (float)(Camera->Pos.x + COSINE(speed)*vVector.x - SINE(speed)*vVector.z);

}  

So I tried this but it doesn’t work as intended:

void Camera::RS_Camera_RotateVertical(float speed)
{

	//vVector = View - Pos;	// Get the view vector
	Vec3 vVector = Camera->View -Camera->Pos;	// Get the view vector
	
	Camera->View.y = (float)(Camera->Pos.y + SINE(speed)*vVector.x + COSINE(speed)*vVector.y);
	Camera->View.x = (float)(Camera->Pos.x + COSINE(speed)*vVector.x - SINE(speed)*vVector.y);

}  

Could anybody correct me?!
Thanks so much in advance,
Rod

When rotating around the x axis, you should not modify your x axis. Your code looks like it’s rotating around the z axis.

Other than that, the equations look ok.

As a side note: I think you should really learn to use matrices for that sort of thing, it makes transformations a lot simpler.

Yes… I considered using matrices, but I read in SIGGRAPH course 2001 Advanced Rendering Techniques that using glLoadMatrix produces a performance drop in contrast to OpenGL glRotate, glTranslate, glScale.

 void Camera::RS_Camera_RotateVertical(float speed)
{

	//vVector = View - Pos;	// Get the view vector
	Vec3 vVector = Camera->View -Camera->Pos;	// Get the view vector
	
	LookAt->Orientation_Now.z = (float)(LookAt->Position_Now.z + SINE(speed)*vVector.y + COSINE(speed)*vVector.z);
	LookAt->Orientation_Now.y = (float)(LookAt->Position_Now.y + COSINE(speed)*vVector.y - SINE(speed)*vVector.z);


}   

You are right, x shouldn’t be included…but I tried this one and I am still getting it wrong… I am kind of lost with this math actually… :frowning:
Any tips?!

Thanks so much,
Rod

If no one know the answer, could you atleast point me to a camera tutorial that uses glLookAt?!
Thanks so much in advance, I would really appreciate it.
Rod

What’s the error you are getting? Your code looks correct now, so it should work.

I’m not really sure about the meaning of your variables. You read from Camera->View and Camera->Pos, but you write LookAt->Orientation_Now and LookAt->Position_Now. Do you write the values back into Camera at some point?

but I read in SIGGRAPH course 2001 Advanced Rendering Techniques that using glLoadMatrix produces a performance drop in contrast to OpenGL glRotate, glTranslate, glScale
I doubt that. Especially because you won’t load the camera matrix very often per frame :wink:

Originally posted by Overmind:
[QB] What’s the error you are getting? Your code looks correct now, so it should work.

Hi Overmind! About the variable names, they were meant to be Camera->View and Camera->Pos (i wrote it incorrectly in the post).

It was meant to be:

Especially because you won’t load the camera matrix very often per frame :wink:
…good point :slight_smile:

Thanks so much in advance!
Cheers!
Rod

no one knows?! :frowning:
Please any tips would be welcome, i really want to make this work and I can’t find info anywhere on doing camera rotations of this type with glulookat.

Rod

Assuming:
Camera->Pos : Eye position in gluLookAt
Camera->View : Center position in gluLookAt

gluLookAt (Camera->Pos, Camera->View, …)

'After that

Vec3 vVector = Camera->Pos-Camera->View; // Get the view vector
Camera->Pos.z = (float)(Camera->Pos.z + SINE(degrees)*vVector.x + COSINE(degrees)*vVector.z);
Camera->Pos.x = (float)(Camera->Pos.x + COSINE(degrees)*vVector.x - SINE(degrees)*vVector.z);

gluLookAt (Camera->Pos, Camera->View, …)
(and you should be rotated x degrees around y-axis placed in Camera->View)

If you want you could search for “gluLookAt implementation” on google. Software “drivers” like MesaGL have implementations that emulate the behaviour of GLUT. Just a thought if you don’t want to have GLUT as a dependency in your code…

I made it work now by looking at a demo source.

Thanks so much everyone!