opengl - Changing camera direction without affecting its location -
i'm changing camera location , direction according mouse movement , buttons pressed.
in order move camera 5 units forward(for example) according direction, i'm using following code:
float mview[16]; glgetfloatv(gl_modelview_matrix, mview); translate_forward_camera += 5; //i have translate_right_camera , //translate_up_camera variables glloadidentity(); gltranslatef(0, 0, 5); glmultmatrixf(mview);
same moving sides.
however, have not idea how change direction of camera without changing location. rotating camera around @ (0,0,0). previous y , x rotation angles are:
old_x_ang, old_y_ang
the new y , x rotation angles (i need rotate camera @ (0,0,0) using angles) are:
x_ang, y_ang
if i'll use following code, camera location change:
glloadidentity(); gltranslatef(translate_right_camera,translate_up_camera,translate_forward_camera); glrotatef(x_ang,1,0,0); glrotatef(y_ang,0,1,0);
how can 1 rotate camera without changing location?
in typical case, view matrix can decomposed intwo 2 separate transformations:
- a translation t defining camera postion, and
- a rotation r defining camera orientation
you might have learned object placement in world space, typically roate first (around object's origin), , translate object final position on world space. due fact fixed-function gl transformation uses matrix * colum-vector convention, had define operation t * r effect of rotating first , translating.
and has been doing here. , not work. reason view matrix not place camera in world, part of vertex transformation transfrom vertices world space view space - hence view matrix inverse of that. , matrices (a*b)^-1 = b^-1 * a^-1.
so means t in our case inverse of camera world position, , r inverse rotations, , have apply r * t. makes total sense since t transforms vertices view space, origin camera position, , rotate around local origin rotate camera without changing position. have swithcing gltranslate()
call end if transformation sequence.
i recommend stop using gl matrix stack, not because deprecated , not available in modern gl, because inefficient read matrix gl.
Comments
Post a Comment