user463474
user463474

Reputation: 13

Preserving rotations in OpenGL

I'm drawing an object (say, a cube) in OpenGL that a user can rotate by clicking / dragging the mouse across the window. The cube is drawn like so:


void CubeDrawingArea::redraw()
{ 
    Glib::RefPtr gl_drawable = get_gl_drawable();

    gl_drawable->gl_begin(get_gl_context());

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glPushMatrix();
    {
        glRotated(m_angle, m_rotAxis.x, m_rotAxis.y, m_rotAxis.z);
        glCallList(m_cubeID);
    }
    glPopMatrix();

    gl_drawable->swap_buffers();

    gl_drawable->gl_end();
}

and rotated with this function:

bool CubeDrawingArea::on_motion_notify_event(GdkEventMotion* motion)
{
    if (!m_leftButtonDown)
        return true;

_3V cur_pos;
get_trackball_point((int) motion->x, (int) motion->y, cur_pos);

const double dx = cur_pos.x - m_lastTrackPoint.x;
const double dy = cur_pos.y - m_lastTrackPoint.y;
const double dz = cur_pos.z - m_lastTrackPoint.z;

if (dx || dy || dz)
{
    // Update angle, axis of rotation, and redraw

    m_angle = 90.0 * sqrt((dx * dx) + (dy * dy) + (dz * dz));

    // Axis of rotation comes from cross product of last / cur vectors
    m_rotAxis.x = (m_lastTrackPoint.y * cur_pos.z) - (m_lastTrackPoint.z * cur_pos.y);
    m_rotAxis.y = (m_lastTrackPoint.z * cur_pos.x) - (m_lastTrackPoint.x * cur_pos.z);
    m_rotAxis.z = (m_lastTrackPoint.x * cur_pos.y) - (m_lastTrackPoint.y * cur_pos.x);

    redraw();
}

return true;

}

There is some GTK+ stuff in there, but it should be pretty obvious what it's for. The get_trackball_point() function projects the window coordinates X Y onto a hemisphere (the virtual "trackball") that is used as a reference point for rotating the object. Anyway, this more or less works, but after I'm done rotating, and I go to rotate again, the cube snaps back to the original position, obviously, since m_angle will be reset back to near 0 the next time I rotate. Is there anyway to avoid this and preserve the rotation?

Upvotes: 1

Views: 1725

Answers (1)

LarsH
LarsH

Reputation: 27996

Yeah, I ran into this problem too.

What you need to do is keep a rotation matrix around that "accumulates" the current state of rotation, and use it in addition to the rotation matrix that comes from the current dragging operation.

Say you have two matrices, lastRotMx and currRotMx. Make them members of CubeDrawingArea if you like.

You haven't shown us this, but I assume that m_lastTrackPoint is initialized whenever the mouse button goes down for dragging. When that happens, copy currRotMx into lastRotMx.

Then in on_motion_notify_event(), after you calculate m_rotAxis and m_angle, create a new rotation matrix draggingRotMx based on m_rotAxis and m_angle; then multiply lastRotMx by draggingRotMx and put the result in currRotMx.

Finally, in redraw(), instead of

   glRotated(m_angle, m_rotAxis.x, m_rotAxis.y, m_rotAxis.z);

rotate by currRotMx.

Update: Or instead of all that... I haven't tested this, but I think it would work:

Make cur_pos a class member so it stays around, but it's initialized to zero, as is m_lastTrackPoint. Then, whenever a new drag motion is started, before you initialize m_lastTrackPoint, let _3V dpos = cur_pos - m_lastTrackPoint (pseudocode). Finally, when you do initialize m_lastTrackPoint based on the mouse event coords, subtract dpos from it.

That way, your cur_pos will already be offset from m_lastTrackPoint by an amount based on the accumulation of offsets from past arcball drags.

Probably error would accumulate as well, but it should be gradual enough so as not to be noticeable. But I'd want to test it to be sure... composed rotations are tricky enough that I don't trust them without seeing them.

P.S. your username is demotivating. Suggest picking another one.

P.P.S. For those who come later searching for answers to this question, the keywords to search on are "arcball rotation". An definitive article is Ken Shoemake's section in Graphical Gems IV. See also this arcball tutorial for JOGL.

Upvotes: 1

Related Questions