Commit 25092db6 by Kai Westerkamp

addet translation

parent 026bab16
......@@ -72,7 +72,7 @@ void CubeWidget::paintGL ( )
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0.0f,0.0f,-zoom);
glMultMatrixf(translation->data());
QMatrix4x4 mat = QMatrix4x4();
mat.rotate(*rotation);
......@@ -238,49 +238,35 @@ void CubeWidget::setTessellation(int t)
void CubeWidget::home()
{
zoom = 3;
translation = new QMatrix4x4();
translation->translate(0.0,0.0,-3.0);
rotation = new QQuaternion();
updateGL();
}
void CubeWidget::mousePressEvent(QMouseEvent *event )
{
lastSpeherePos = trackballPoint(event->pos().x(),event->pos().y());
lastScreenPos = new QPointF(event->screenPos());
event->accept();
// if (event->isAccepted())
// return;
if (event->buttons() & Qt::LeftButton) {
lastPos = trackballPoint(event->pos().x(),event->pos().y());
event->accept();
} else {
}
}
void CubeWidget::mouseMoveEvent(QMouseEvent *event )
{
//http://web.cse.ohio-state.edu/~hwshen/781/Site/Slides_files/trackball.pdf
//http://doc.qt.io/qt-5/qtwidgets-graphicsview-boxes-example.html
// if (event->isAccepted())
// return;
if (event->buttons() & Qt::LeftButton) {
move(trackballPoint(event->pos().x(),event->pos().y()));
//m_trackBalls[0].move(pixelPosToViewPos(event->scenePos()), m_trackBalls[2].rotation().conjugate());
rotate(trackballPoint(event->pos().x(),event->pos().y()));
event->accept();
} else if (event->buttons() & Qt::RightButton) {
move(new QPointF(event->screenPos()));
event->accept();
} else {
//m_trackBalls[0].release(pixelPosToViewPos(event->scenePos()), m_trackBalls[2].rotation().conjugate());
}
}
void CubeWidget::move(QVector3D *newPos )
void CubeWidget::rotate(QVector3D *newPos )
{
QVector3D axis = QVector3D::crossProduct(*lastPos,*newPos);
QVector3D axis = QVector3D::crossProduct(*lastSpeherePos,*newPos);
//warum so besser
float angle = 180 / M_PI * asin(sqrt(QVector3D::dotProduct(axis, axis)));
......@@ -290,15 +276,29 @@ void CubeWidget::move(QVector3D *newPos )
QQuaternion newRot = QQuaternion::fromAxisAndAngle(axis, angle) * *rotation;
rotation = new QQuaternion(newRot.toVector4D());
lastPos = newPos;
lastSpeherePos = newPos;
updateGL();
}
void CubeWidget::move(QPointF * newPos){
QPointF dt = *newPos;
dt -= *lastScreenPos;
dt *= 0.01f;
float dx = dt.x();
float dy = dt.y()*-1.0;
translation->translate(dx,dy,0.0);
lastScreenPos = newPos;
updateGL();
}
QVector3D* CubeWidget::trackballPoint(int x, int y){
float xo,yo,zo;
// qDebug()<<"x:"<< x << " y:"<<y;
// qDebug()<<"x:"<< x << " y:"<<y;
xo = ((2.0*x)-width())/ height();
yo = (height()-(2.0*y))/width();
......@@ -309,16 +309,17 @@ QVector3D* CubeWidget::trackballPoint(int x, int y){
QVector3D *pos = new QVector3D(xo,yo,zo);
pos->normalize();
// qDebug()<<"x:"<< xo << " y:"<<yo<<" z:"<<zo;
// qDebug()<<"x:"<< xo << " y:"<<yo<<" z:"<<zo;
return pos;
}
void CubeWidget::wheelEvent(QWheelEvent *event )
{
if(event->delta()<0)
zoom++;
//TODO
translation->translate(0.0,0.0,1.0);
else
zoom--;
translation->translate(0.0,0.0,-1.0);
updateGL();
}
......@@ -42,8 +42,12 @@ public slots:
private:
int tesselation;
int zoom;
QVector3D *lastSpeherePos;
QPointF * lastScreenPos;
QQuaternion *rotation;
QVector3D *lastPos;
QMatrix4x4 * translation;
QGLShaderProgram *shader;
......@@ -51,7 +55,8 @@ private:
void perspective(GLdouble fovy, GLdouble aspect, GLdouble zNear, GLdouble zFar);
void setMaterial(GLfloat *color );
void move(QVector3D *newPos );
void rotate(QVector3D *newPos );
void move(QPointF *newPos );
QVector3D *trackballPoint(int x, int y);
};
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment