PDA

View Full Version : OpenGL view matrix problem



glsl09
01-22-2010, 07:35 PM
I am new to opengl. i want to rotate camera around world up axis(0,1,0). but i get wrong orientation.

First
1) i rotate viewDirection vector around world y axis by using
rotate(angle, rotated vector, axis) function which takes 3 parameters and returns new rotated viewDirection vector.
2) viewDirection vector is normalized
3) rightDirection = crossProduct(viewDirection, upDirection)
rightDirection.normalize();
4) upDirection = cross( rightDirection, viewDirection)
upDirection.normalize;

Finally;
i call update() fonction which forms ModelViewMatrix like this

| rx ry rz -(r.e) |
| ux uy uz -(u.e) |
| -vx -vy -vz (v.e) |
| 0 0 0 1 |

but i get wrong orientation as if camera rotates around z axis why, can u help me this. thanks in advance. have a nice day.


//////////////////////////////////
// "camera.h"/////////////////////
//////////////////////////////////
class Camera{
public:
float matrix[16];
Vector3d pos;
Vector3d viewDirection;
Vector3d upDirection;
Vector3d rightDirection;
public:
Camera();
void move(float t);
Vector3d rotate(float t, Vector3d v, Vector3d axis);
// rotate around world y
void rotate_w_y(float a);
// rotate around camera rightDirection
//void rotate_c_x(float a);
void update();
};


//////////////////////////////////
// "camera.cpp"///////////////////
//////////////////////////////////
#include "Camera.h"


Camera::Camera() : pos(0,0,0),viewDirection(0,0,-1),upDirection(0,1,0),
rightDirection(1,0,0)
{
update();
}


void Camera::update()
{
matrix[0] = rightDirection.x; matrix[4] = rightDirection.y; matrix[8] = rightDirection.z; matrix[12] = -Vector3d::dot(pos, rightDirection);
matrix[1] = upDirection.x; matrix[5] = upDirection.y; matrix[9] = upDirection.z; matrix[13] = -Vector3d::dot(pos, upDirection);
matrix[2] = -viewDirection.x; matrix[6] = -viewDirection.y; matrix[10] = -viewDirection.z; matrix[14] = Vector3d::dot(pos, viewDirection);
matrix[3] = 0; matrix[7] = 0; matrix[11] = 0; matrix[15] = 1;
}

void Camera::move(float t)
{
pos.x = pos.x + t*viewDirection.x;
pos.z = pos.z + t*viewDirection.z;
update();
}


// This function rotates any v vector around any axis
Vector3d Camera::rotate(float tt, Vector3d v, Vector3d axis)
{
float teta = tt*3.1415/180.0f;
float c = cos(teta);
float s = sin(teta);
float t = (1-c);
float x = axis.x;
float y = axis.y;
float z = axis.z;
float temp[9];


temp[0] = t*x*x + c; temp[3] = t*x*y - s*z; temp[6] = t*x*z + s*y;
temp[1] = t*x*y + s*z; temp[4] = t*y*y + c; temp[7] = t*y*z - s*x;
temp[2] = t*x*z - s*y; temp[5] = t*y*z + s*x; temp[8] = t*z*z + c;

return Vector3d( temp[0]*v.x + temp[3]*v.y + temp[6]*v.z,
temp[1]*v.x + temp[4]*v.y + temp[7]*v.z,
temp[2]*v.x + temp[5]*v.y + temp[8]*v.z);
}

void Camera::rotate_w_y(float a)
{
viewDirection = rotate(a, viewDirection, Vector3d(0,1,0));
viewDirection.normalize();

//upDirection = rotate(a, upDirection, Vector3d(0,1,0));
//upDirection.normalize();

upDirection = Vector3d::crossProduct(rightDirection, viewDirection);
upDirection.normalize();

rightDirection = Vector3d::crossProduct( viewDirection, upDirection );
rightDirection.normalize();

update();
}

//////////////////////////////////
// main.cpp///////////////////////
//////////////////////////////////

case SDLK_LEFT:
{
camera.rotate_w_y(0.05);
break;
}
case SDLK_RIGHT:
{
camera.rotate_w_y(-0.05);
break;
}

draw();

glsl09
01-23-2010, 06:48 AM
i am apologize to you. i finded problem. code is working very well. i writed wrong copy constructor of Vector3D::Vector3d(const Vector3d& )

have a nice day.