Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Smooth Rotation Using OpenGL Widget
Qt 6.11 is out! See what's new in the release blog

Smooth Rotation Using OpenGL Widget

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 2.7k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    I am attempting to rotate a character in a widget which uses OpenGL. I posted the relevant code bits below.

    The problem is that with any of the methods I've attempted, the rotation has either been jumpy or very jumpy.

    I've tried merely testing if there was movement and then rotating at a constant speed- but that felt off because the speed was not correspondent to how fast I moved my mouse.

    @ do
    {
    dX += QCursor::pos().x() - mouseSavePosX;
    if (mouseLockedFlag && dX != 0 || mouseLockedFlag && dY !=0)
    {
    QCursor::setPos(mouseSavePosX,mouseSavePosY);
    }
    }
    while(timer.elapsed() < 1000/EXEC_PER_SECOND_CAP);@

    ....

    @ userRotY-=dX;
    dX = 0;@

    I appreciate your help in advance. Thank you!

    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #2

      Probably easiest to simply implement the mousePress(), mouseMove() and mouseRelease() event handlers. Something along these lines:

      @
      void CameraController::mousePressEvent( QMouseEvent* e )
      {
      e->ignore();

      if ( e->button() == Qt::LeftButton )
      {
          m_leftButtonPressed = true;
          m_pos = m_prevPos = e->pos();
          e->accept();
      }
      else if ( e->button() == Qt::RightButton )
      {
          m_orbitMode = true;
          m_pos = m_prevPos = e->pos();
          e->accept();
      }
      

      }

      void CameraController::mouseReleaseEvent( QMouseEvent* e )
      {
      e->ignore();

      if ( e->button() == Qt::LeftButton )
      {
          m_leftButtonPressed = false;
          e->accept();
      }
      else if ( e->button() == Qt::RightButton )
      {
          m_orbitMode = false;
          e->accept();
      }
      

      }

      void CameraController::mouseMoveEvent( QMouseEvent* e )
      {
      e->ignore();

      if (!m_camera )
        return;
      
      if (!m_leftButtonPressed && !m_orbitMode)
          return;
      
      e->accept();
      
      m_pos = e->pos();
      float dx = m_pos.x() - m_prevPos.x();
      float dy = -( m_pos.y() - m_prevPos.y() );
      m_prevPos = m_pos;
      
      if ( m_leftButtonPressed )
      {
          switch (m_controlMode)
          {
          case FreeLook:
              m_camera->pan( dx * m_lookRate );
              break;
      
          case FirstPerson:
              m_camera->pan( dx * m_lookRate, m_firstPersonUp );
              break;
          }
      
          m_camera->tilt( dy * m_lookRate );
      }
      else if ( m_orbitMode )
      {
          switch (m_controlMode)
          {
          case FreeLook:
              m_camera->panAboutViewCenter( dx * m_orbitRate );
              break;
      
          case FirstPerson:
              m_camera->panAboutViewCenter( dx * m_orbitRate, m_firstPersonUp );
              break;
          }
      
          m_camera->tiltAboutViewCenter( dy * m_orbitRate );
      }
      

      }
      @

      Here, m_camera, is a pointer to an instance of a Camera class that has helpful functions such as lookAt(), pan() and tilt().

      Hope this helps.

      1 Reply Last reply
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

        Your implementation will not work for me. I am already using the mouse click events and you seem to be using a sort of click and drag solution whereas I need something more akin to a FPS where the mouse stays in my widget.

        1 Reply Last reply
        0
        • ? Offline
          ? Offline
          A Former User
          wrote on last edited by
          #4

          I managed to get it working and will post the relevant bits of code in case anyone else needs them in the future.

          @mouseLockedFlag = false;
          mouseJustLocked = false;
          userRotSpeedX=userRotSpeedY=userRotSpeedZ=.1;
          @
          @void MyGLWidget::mouseDoubleClickEvent(QMouseEvent * event)
          {
          if (mouseLockedFlag)
          {
          mouseLockedFlag = false;
          MyGLWidget::setCursor(Qt::ArrowCursor);
          }
          else
          {
          mouseJustLocked = true;
          mouseLockedFlag = true;
          MyGLWidget::setCursor(Qt::BlankCursor);
          }
          }@

          @
          QPoint widgetM = QWidget::mapToGlobal(QPoint(QWidget::size().width()/2,QWidget::size().height()/2));
          if (mouseJustLocked)
          {
          QCursor::setPos(widgetM);
          mouseJustLocked = false;
          }
          QPoint cursorP = QCursor::pos();
          float xDiff = cursorP.x()-widgetM.x();
          float yDiff = cursorP.y()-widgetM.y();
          float mouseDist = someMath::squRoot(xDiffxDiff + yDiffyDiff);
          float freeRoom = (QWidget::size().width() > QWidget::size().height() ? QWidget::size().height() : QWidget::size().width())/3;
          if (mouseLockedFlag)
          {
          if (freeRoom < mouseDist)
          {
          float newX = widgetM.x() + xDifffreeRoom/mouseDist;
          float newY = widgetM.y() + yDiff
          freeRoom/mouseDist;
          QCursor::setPos(newX,newY);
          }
          }

          if(timer.elapsed() > 1000/EXEC_PER_SECOND_CAP)
          {
              timer.restart();
              timer.start();
          
              if (mouseLockedFlag)
              {
                  if (mouseDist != 0)
                  {
                      float newX = widgetM.x() + xDiff*qLn(mouseDist)/mouseDist;
                      float newY = widgetM.y() + yDiff*qLn(mouseDist)/mouseDist;
                      userRotX += userRotSpeedX*yDiff;
                      userRotY += userRotSpeedY*xDiff;
                      QCursor::setPos(newX, newY);
                  }
               }
          }@
          

          @
          glRotatef(userRotX, 1.0, 0.0, 0.0);
          glRotatef(userRotY, 0.0, 1.0, 0.0);
          glRotatef(userRotZ, 0.0, 0.0, 1.0);
          glTranslatef(userX, userY, userZ);@

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved