I am new to opengl. i want to rotate camera around world up axis(0,1,0). but i get wrong orientation.
First
- 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. - viewDirection vector is normalized
- rightDirection = crossProduct(viewDirection, upDirection)
rightDirection.normalize(); - 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 + tviewDirection.x;
pos.z = pos.z + tviewDirection.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] = txx + c; temp[3] = txy - sz; temp[6] = txz + sy;
temp[1] = txy + sz; temp[4] = tyy + c; temp[7] = tyz - sx;
temp[2] = txz - sy; temp[5] = tyz + sx; temp[8] = tzz + 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();