Question

I want to build proper camera transformations in OpenGL and I have a question.

I built a simple 3D engine with without OpenGL, just to see how hard it is to make simple projection of the world onto a screen.

In this engine I put wiewer on (0,0,0) with the eye sight on (1,0,0). ASDW make translation of objects. Mouse make rotation around (1,0,0),(0,1,0),(0,0,1).

I learned a lot with programing my own 3D engine. With that knowledge I started to build a proper 3D engine based on OpenGL.

For camera transformation in OpenGL, I made this code...

Camera's axis define where up,right,forward translation will go. Rotation is made around global axis (I want to rotate objects around camera's axis) :(

glRotatef(camera_rot[0],1,0,0)
glRotatef(camera_rot[1],0,1,0)
glRotatef(camera_rot[2],0,0,1)

camera_rot[0]=0
camera_rot[1]=0
camera_rot[2]=0

glGetFloatv(GL_MODELVIEW_MATRIX, matrix)

glTranslatef(
matrix[0]*camera_tra[0],
matrix[4]*camera_tra[0],
matrix[8]*camera_tra[0])

glTranslatef(
matrix[1]*camera_tra[1],
matrix[5]*camera_tra[1],
matrix[9]*camera_tra[1])

glTranslatef(
matrix[2]*camera_tra[2],
matrix[6]*camera_tra[2],
matrix[10]*camera_tra[2])

camera_tra[0]=0
camera_tra[1]=0
camera_tra[2]=0

It's kinda confusing why OpenGL designers didn't put camera in (0,0,0) and make translation and rotation on object not camera.

I'm thinking to manually aplly transformation with out glTranslate or glRotation and put transformation logic from my 3D engine (that I made) to changing glVertexe cordinates. So every time in draw function I will change glVertexes cordinates. But if I do that the vertextes will multiply after my transformation with identical matrix (ModelView matrix), and I don't want meaningless computation in my program.

So what should I do to fix this problem? How can I make the best solution for rotation around camera's axis?

Was it helpful?

Solution

YEEEEEES I made it YAW,ROLL,PITCH rotation and translation for local camera axis :D

Down is my part of code that take care for transformation.

Here is my global constant:

float camera_drot[3]={0,0,0};
float camera_dtra[3]={0,0,0};
float camera_place[3]={0,0,10};
float camera_right[3]={1,0,0};
float camera_up[3]={0,1,0};
float camera_forvard[3]={0,0,-1};

Before i draw objects:

if(camera_drot[0]!=0){    //ROTATE CAMERA_UP AND CAMERA_FORVARD AROUND CAMERA_RIGHT
    MYRotation(camera_right,camera_up,camera_drot[0]);
    MYRotation(camera_right,camera_forvard,camera_drot[0]);
    camera_drot[0]=0;}
if(camera_drot[1]!=0){    //ROTATE CAMERA_RIGHT AND CAMERA_FORVARD AROUND CAMERA_UP
    MYRotation(camera_up,camera_right,camera_drot[1]);
    MYRotation(camera_up,camera_forvard,camera_drot[1]);
    camera_drot[1]=0;}
if(camera_drot[2]!=0){    //ROTATE CAMERA_RIGHT AND CAMERA_UP AROUND CAMERA_FORWARD
    MYRotation(camera_forvard,camera_right,camera_drot[2]);
    MYRotation(camera_forvard,camera_up,camera_drot[2]);
    camera_drot[2]=0;}

if(camera_dtra[0]!=0){MYTranslation(camera_right,camera_dtra[0]);camera_dtra[0]=0;}
if(camera_dtra[1]!=0){MYTranslation(camera_up,camera_dtra[1]);camera_dtra[1]=0;}
if(camera_dtra[2]!=0){MYTranslation(camera_forvard,camera_dtra[2]);camera_dtra[2]=0;}

glLoadIdentity();
gluLookAt(
camera_place[0],camera_place[1],camera_place[2],
camera_place[0]+camera_forvard[0],camera_place[1]+camera_forvard[1],camera_place[2]+camera_forvard[2],
camera_up[0],camera_up[1],camera_up[2]);
}

And here is my function MYRotation and MYTranslation :)

static void MYRotation(float vector[3],float point[3],float fi){
    //ROTATE POINT AROUND VECTOR
    float i = point[0]; 
    float j = point[1];
    float k = point[2];
    float x = vector[0];
    float y = vector[1];
    float z = vector[2];

    point[0] = i*(x*x + (y*y + z*z)*cos(fi)) + k*(-x*z*(-1 + cos(fi)) + y*sin(fi)) + j*(-x*y*(-1 + cos(fi)) - z*sin(fi));
    point[1] = j*(y*y + (x*x + z*z)*cos(fi)) + k*(-y*z*(-1 + cos(fi)) - x*sin(fi)) + i*(-x*y*(-1 + cos(fi)) + z*sin(fi)); 
    point[2] = k*(z*z + (x*x + y*y)*cos(fi)) + j*(-y*z*(-1 + cos(fi)) + x*sin(fi)) + i*(-    x*z*(-1 + cos(fi)) - y*sin(fi));

    float normalize = pow(pow(point[0],2)+pow(point[1],2)+pow(point[2],2),-0.5);
    point[0]=point[0]*normalize;
    point[1]=point[1]*normalize;
    point[2]=point[2]*normalize;    
}
static void MYTranslation(float camera_axe[3],float camera_dtra){
    camera_place[0]+=camera_axe[0]*camera_dtra;
    camera_place[1]+=camera_axe[1]*camera_dtra;
    camera_place[2]+=camera_axe[2]*camera_dtra;}

OTHER TIPS

Oh and i forget to make vector corection before normalize in MYRotation. In program the calculation errors will not give ortogonal vectors. Its critical to make this correction!

I will poste the code after im done :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top