Hello there!
I am trying to implement an arcball/trackball controller using Open GL and Qt. However, I am quite new to OpenGL. I am having a terrible, terrible, terrible time getting things to work.
I started by following this video: - YouTube
I am using Qt for my window, using their QtWidget class.
Basically, I have a cube around the origin. I want to orbit the camera around the cube with the mouse. Right now, when I drag the camera seems to go NUTS. Goes to random positions.
I hope you guys can help. I feel like I’ve tried nearly everything here.
First my mouse handling:
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 arcball related math:
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 render 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);
// Matrix has to be UPDATED, and not every frame! That's the issue.
//modelview.translate(0.0f, 0.0f, 1.0f);
//modelview.rotate(m_fAngle, 1.0f, 0.0f, 0.0f);
//modelview.rotate(0.1f, 0.0f, 0.0f, 1.0f);
//modelview.scale(m_fScale);
float radius;
radius = 200;
float focus;
focus = 0;
float eyeX = focus + radius*cos(phi)*sin(theta);
float eyeY = focus + radius*sin(phi)*sin(theta);
float eyeZ = focus + radius*cos(theta);
modelview.perspective(90.0f, 4.0f / 3.0f, 0.1f, 3000.0f);
//if(lookatint < 0)
// {
//QVector3D lookAt = QVector3D(eyeX, eyeY, eyeZ + scrollDelta);
modelview.lookAt(QVector3D(eyeX,eyeY,eyeZ), QVector3D(0,0,0), QVector3D(0,1,0));
//lookatbool = false;
// lookatint++;
//}
// New Trackball code
modelview = rotationMat * modelview;
modelview.scale(1 - scrollDelta / 10);