Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1838644
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T06:30:25+00:00 2026-05-17T06:30:25+00:00

I’m drawing an object (say, a cube) in OpenGL that a user can rotate

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T06:30:26+00:00Added an answer on May 17, 2026 at 6:30 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.