Arc control with Qt and OpenGL

I am trying to implement an arcball / trackball controller using Open GL and Qt. However, I am completely new to OpenGL. I have a terrible, scary, scary time to get to work.

I started by watching this video: https://www.youtube.com/watch?v=3IQV65ApWGs

I use Qt for my window using the QtWidget class.

Basically, I have a cube around the beginning. I want us to rotate the camera around the cube with the mouse. Right now, when I drag the camera, it seems to remain in place while the cube rotates around a sphere. Kind of the opposite of what I need.

Hope you guys can help. It seems to me that I tried almost everything here.

My mouse processing first:

void GLWidget::wheelEvent(QWheelEvent *e){
    scrollDelta +=  e->delta() / 120;
}

void GLWidget::mousePressEvent(QMouseEvent *e){
    rotate=false;
    if(e->button() == Qt::LeftButton){
        oldX = e->x(); // Set this to the mouse position
        oldY = e->y(); // Set this to the mouse position

        newX = e->x();
        newY = e->y();

        qDebug() << oldX << oldY << newX << newY;

        rotate = true;

        useArcBall = true;
     }
}

void GLWidget::mouseMoveEvent(QMouseEvent *e){
    if(e->buttons() & Qt::LeftButton){
        //qDebug() << QString::number(e->x());
        if(rotate){
            newX = e->x();
            newY = e->y();
            updateMouse();

        }
        oldX = e->x();
        oldY = e->y();
    }

}

void GLWidget::mouseReleaseEvent(QMouseEvent *e){
    if(e->button() == Qt::LeftButton)
        useArcBall = false;

}

void GLWidget::updateMouse()
{

        QVector3D v = getArcBallVector(oldX,oldY); // from the mouse
        QVector3D u = getArcBallVector(newX, newY);

        float angle = std::acos(std::min(1.0f, QVector3D::dotProduct(u,v)));

        QVector3D rotAxis = QVector3D::crossProduct(v,u);

        QMatrix4x4 eye2ObjSpaceMat = rotationMat.inverted();

        QVector3D objSpaceRotAxis = eye2ObjSpaceMat * rotAxis;

        qDebug() << 4 * qRadiansToDegrees(angle);


        //modelview.rotate(4 * qRadiansToDegrees(angle), rotAxis);

        //oldRot = newRot;

        //oldX = newX;
        //oldY = newY;

        //qDebug() << objSpaceRotAxis.normalized();


    if(true){
    rotationMat.rotate(4 * qRadiansToDegrees(angle), objSpaceRotAxis);
    }

}

Now the math related to arcball:

QVector3D GLWidget::getArcBallVector(int x, int y)
{
   QVector3D pt = QVector3D(2.0 * x / GLWidget::width() - 1.0, 2.0 * y / GLWidget::height() - 1.0 , 0);
   pt.setY(pt.y() * -1);

   // compute z-coordinates

   float xySquared = pt.x() * pt.x() + pt.y() * pt.y();

   if(xySquared <= 1.0)

       pt.setZ(std::sqrt(1.0 - xySquared));
   else
       pt.normalize();

   return pt;

}

And this is the part where I do everything:

void GLWidget:: paintGL() {   QMatrix4x4 modelview;

QPainter painter;
painter.begin(this);

painter.beginNativePainting();

glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glFrontFace(GL_CW);
glCullFace(GL_FRONT);
glEnable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);


 modelview.perspective(90.0f, 4.0f / 3.0f, 0.1f, 3000.0f);

modelview.lookAt(QVector3D(eyeX,eyeY,eyeZ), QVector3D(0,0,0), QVector3D(0,1,0));


// New Trackball code

modelview = rotationMat * modelview;

modelview.scale(1 - scrollDelta / 10);

? ?

update , . , , . , lookat?

, , . ?

+4
1

,

paintGL :

modelview *= rotationMat;

updateMouse

QMatrix4x4 tmp;
tmp.rotate(4 * qRadiansToDegrees(angle), objSpaceRotAxis);
rotationMat = tmp * rotationMat;
0

Source: https://habr.com/ru/post/1541178/


All Articles