Arcball 3D camera - how to convert from camera to object coordinates

Posted by user38873 on Game Development See other posts from Game Development or by user38873
Published on 2013-11-10T15:47:01Z Indexed on 2013/11/10 16:13 UTC
Read the original article Hit count: 363

Filed under:
|
|
|

I have checked multiple threads before posting, but i havent been able to figure this one out.

Ok so i have been following this tutorial, but im not using glm, ive been implementing everything up until now, like lookat etc. http://en.wikibooks.org/wiki/OpenGL_Programming/Modern_OpenGL_Tutorial_Arcball

So i can rotate with the click and drag of the mouse, but when i rotate 90º degrees around Y and then move the mouse upwards or donwwards, it rotates on the wrong axis, this problem is demonstrated on this part of the tutorial

An extra trick is converting the rotation axis from camera coordinates to object coordinates. It's useful when the camera and object are placed differently. For instace, if you rotate the object by 90° on the Y axis ("turn its head" to the right), then perform a vertical move with your mouse, you make a rotation on the camera X axis, but it should become a rotation on the Z axis (plane barrel roll) for the object. By converting the axis in object coordinates, the rotation will respect that the user work in camera coordinates (WYSIWYG). To transform from camera to object coordinates, we take the inverse of the MV matrix (from the MVP matrix triplet).

What i have to do acording to the tutorial is convert my axis_in_camera_coordinates to object coordinates, and the rotation is done well, but im confused on what matrix i use to do just that. The tutorial talks about converting the axis from camera to object coordinates by using the inverse of the MV. Then it shows these 3 lines of code witch i havent been able to understand.

glm::mat3 camera2object = glm::inverse(glm::mat3(transforms[MODE_CAMERA]) *       
glm::mat3(mesh.object2world));
glm::vec3 axis_in_object_coord = camera2object * axis_in_camera_coord;

So what do i aply to my calculated axis?, the inverse of what, i supose the inverse of the model view? So my question is how do you transform camera axis to object axis. Do i apply the inverse of the lookat matrix? My code:

if (cur_mx != last_mx || cur_my != last_my) {
    va = get_arcball_vector(last_mx, last_my);
    vb = get_arcball_vector( cur_mx,  cur_my);
    angle = acos(min(1.0f, dotProduct(va, vb)))*20;

            axis_in_camera_coord = crossProduct(va, vb);

    axis.x = axis_in_camera_coord[0];
    axis.y = axis_in_camera_coord[1];
    axis.z = axis_in_camera_coord[2];
    axis.w = 1.0f;

    last_mx = cur_mx;
    last_my = cur_my;
}
Quaternion q = qFromAngleAxis(angle, axis);
Matrix m;
qGLMatrix(q,m);

vi = mMultiply(m, vi);
up = mMultiply(m, up);

ViewMatrix = ogLookAt(vi.x, vi.y, vi.z,0,0,0,up.x,up.y,up.z);

© Game Development or respective owner

Related posts about opengl

Related posts about c++