PDA

View Full Version : about gluLookAt function and how to rotate the camera



zydgyy
06-24-2012, 04:52 AM
Sorry,as i am a newbie to opengl,
if i use the gluLookAt(x,x,x,y,y,y,0,-1,0)
so ,i know the(0,-1,0) is th camera up-vector,but if i use the parameter set the camera in my 3ds max,so how do i roate the camera,is it rotate it -90 degree around y-axis??what it the up-vecotr ralationship with the rotation of camera?

Nick_Miller
06-24-2012, 12:01 PM
Hi there!

Think of the camera as a point, and it's rotational status as Pitch (X-axis), Yaw(Y-Axis) and Roll(Z-Axis).
What I've found is that the up vector closely follows the behavior of roll or rotation along the Z-axis.

If you want to know how to rotate the camera, you're going to need to do some trigonometry. :)
Right now, I'll say what you basically need to do, then I'll reply again with some documentation I have for creating a camera class.

So, basically you need to modify the first six values (which are the camera position and lookat position in that order) so that your camera moves/rotates the way you want it to.

gluLookAt(camera.x, camera.y, camera.z, lookat.x, lookat.y, lookat.z, 0, 1, 0)

To translate, all you do is move the camera's positon and lookat position by the same amount.

For rotation you need to to use Arc-Sines, and Arc-Cosines. When doing rotation, you do not need to change the actual camera coordinate, but you will need to change the lookAt coordinate based on the angle of rotation.
The math is pretty complicated especially in 3D space. So like I said, I will give you the documentation. If you want you can learn from it, or you can just copy-paste ;)

It is in Java so there are a few minor things you'll need to change.


In the time being, take a look at this. This may be what I based my camera class on:
http://www.leolol.com/drupal/tutorials/3d-graphics-jogl-opengl-etc/jogl-lesson-5-cameras-movement-opengl
Also, there is a NeHe tutorial for this:
http://nehe.gamedev.net/article/camera_class_tutorial/18010/

God bless!

cireneikual
06-24-2012, 12:28 PM
Depending on how your camera is supposed to act, you can also use something much simpler. For a mouse control camera, just use:



glRotatef(-yAngle, 0.0f, 1.0, 0.0f);
glRotatef(-xAngle, 1.0f, 0.0f, 0.0f);
glTranslatef(-position.x, -position.y, -position.z);


and not gluLookAt!

You get the y and x angles from the mouse, and just do some bounds checks for y (-90 < y < 90) and wrap x (if x > 360, x = x - 360. if x < 0, x = x + 360)

zydgyy
06-25-2012, 06:35 AM
OK,addtional question:
this is my code:
void RenderScene(void)
{
// Clear the window with current clearing color
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Save the matrix state and do the rotations
//glMatrixMode(GL_MODELVIEW);
//glPushMatrix();
std::stringstream ss;
GLdouble d_matrix[16],d_proj[16];

GLdouble aspect = 1600/realWidth;
GLdouble d_yfov_tan = 0.267949192431123;
GLdouble d_viewDistance = 1600/2.0f/ (aspect * d_yfov_tan);
const GLdouble midx = 1600 * 0.5;
const GLdouble midy = realWidth * 0.5;
glMatrixMode(GL_MODELVIEW_MATRIX);
glPushMatrix();
glLoadIdentity();
gluPerspective(30.0, aspect, d_viewDistance * 0.5, d_viewDistance * 2.0);
glGetDoublev(GL_PROJECTION_MATRIX, d_proj);
//glPopMatrix();

//glMatrixMode(GL_MODELVIEW_MATRIX);
//glPushMatrix();
// glLoadIdentity();
d_viewDistance = 1600.f/2 / (1600.f/realWidth * 0.267949192431123);
gluLookAt(1600/2.f, realWidth/2, -d_viewDistance, 800.f, realWidth/2,1, 0, -1, 0);
glTranslated(755.067810058594,247.161117553711,665 .0654296875);
glGetDoublev(GL_MODELVIEW_MATRIX, d_matrix);

/*ss<<d_matrix[0]<<","<<d_matrix[1]<<","<<d_matrix[2]<<","<<d_matrix[3]<<","
<<d_matrix[4]<<","<<d_matrix[5]<<","<<d_matrix[6]<<","
<<d_matrix[7]<<","<<d_matrix[8]<<","<<d_matrix[9]<<","
<<d_matrix[10]<<","<<d_matrix[11]<<","<<d_matrix[12]<<","
<<d_matrix[13]<<","<<d_matrix[14]<<","<<d_matrix[15]<<","
<<std::endl;
fs.write(ss.str().data(),ss.str().size());*/
GLint viewport[4];
glGetIntegerv(GL_VIEWPORT, viewport);
GLdouble obj1[4]={0.f,0.f,0.f,1.f};
GLdouble obj2[4]={0.f,0.f,0.f,1.f};
GLdouble obj3[4]={0.f,0.f,0.f,1.f};
GLdouble obj4[4]={0.f,0.f,0.f,1.f};
//gluUnProject(0, 0, 0,
// d_matrix,
// d_proj,
// viewport,
// obj1, obj1+1, obj1+2);obj1[2]=10.f;
GetOGLPos(0,0,obj1);
GLdouble world1[4];
GLdouble t_matrix[16],dest[16];
makeTransMatrix(t_matrix,755.067810058594,247.1611 17553711,665.0654296875);
GLdouble v[4] = {1158.66f,826.094f,10,1.0f};
m3dInvertMatrix44(dest,t_matrix);
m44Xv4(t_matrix,obj1,world1);
ss.str("");
ss<<"o(0,0,0)="<<"("<<obj1[0]<<","<<obj1[1]<<","<<obj1[2]<<")\n";
ss<<"w(0,0,0)="<<"("<<world1[0]<<","<<world1[1]<<","<<world1[2]<<")\n";
fs.write(ss.str().data(),ss.str().size());
gluUnProject(0, 900, 0,
d_matrix,
d_proj,
viewport,
obj2, obj2+1, obj2+2);obj2[2]=10.f;
ss.str("");
ss<<"(0,900,0)="<<"("<<obj2[0]<<","<<obj2[1]<<","<<obj2[2]<<")\n";
fs.write(ss.str().data(),ss.str().size());
gluUnProject(1600, 900, 0,
d_matrix,
d_proj,
viewport,
obj3, obj3+1, obj3+2);obj3[2]=10.f;
ss.str("");
ss<<"(1600,800,0)="<<"("<<obj3[0]<<","<<obj3[1]<<","<<obj3[2]<<")\n";
fs.write(ss.str().data(),ss.str().size());
gluUnProject(1600, 0, 0,
d_matrix,
d_proj,
viewport,
obj4, obj4+1, obj4+2);obj4[2]=10.f;
ss.str("");
ss<<"(1600,0,0)="<<"("<<obj4[0]<<","<<obj4[1]<<","<<obj4[2]<<")\n";
fs.write(ss.str().data(),ss.str().size());
GLdouble obj_mid[4]={0.f,0.f,0.f,1.f};
gluUnProject(800, 450, 0,
d_matrix,
d_proj,
viewport,
obj_mid, obj_mid+1, obj_mid+2);
ss.str("");
ss<<"(800,450,0)="<<"("<<obj_mid[0]<<","<<obj_mid[1]<<","<<obj_mid[2]<<")\n";
fs.write(ss.str().data(),ss.str().size());
GLdouble v_1[3] = {1027.31,677.286,10.f};
GLdouble v_2[3] = {399.01,397.958,10.f};
GLdouble v_3[3] = {-225.503,661.406,10.f};
glPointSize(5.0f);
glColor3f(1,0,0);
obj_mid[2] = 10.f;
glBegin(GL_LINE_LOOP);
//for(int i=0;i<8;i++)
{
glVertex3dv(v_1);
glVertex3dv(v_2);
glVertex3dv(v_3);
//glVertex3dv(v_2);

//glVertex3dv(obj_mid);
}
glEnd();
glColor3f(0,1,0);
glBegin(GL_POINTS);
//for(int i=0;i<8;i++)
{
//glVertex3dv(obj1);
//glVertex3dv(obj2);
glVertex3dv(obj3);
//glVertex3dv(obj4);

//glVertex3dv(obj_mid);
}
glEnd();
// Restore the matrix state
glPopMatrix(); // Modelview matrix
// Show the image
glutSwapBuffers();
}



I want get the local object coordinates of four corners of my screen,but it is not correct,please give me some hits,thank u~

thokra
06-25-2012, 06:58 AM
How many times do we have to tell people to use the '['code']' tags?

elisokal
10-29-2012, 11:00 AM
Hello. To get a deep understanding, I suggest reading http://www.glprogramming.com/red/chapter03.html. The material under "Projection Transformations" is particularly helpful. That being said, My approach to the OGL viewing model is structured as follows

- geometric content lives in a space where the XY plane represents the floor and the Z axis points at the ceiling.
- The viewer can be located arbitrarily relative to the content but always looks at the center of the content
- The up vector is always 0,0,1
- I don't rotate the camera, I move its location in order to change the perspective

If you don't have a structured approach, you may have trouble getting off the ground.

If you think this was helpful, please see my post in the drivers forum, b/c I need help!

eli