Commit 25092db6 by Kai Westerkamp

addet translation

parent 026bab16
...@@ -72,7 +72,7 @@ void CubeWidget::paintGL ( ) ...@@ -72,7 +72,7 @@ void CubeWidget::paintGL ( )
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity(); glLoadIdentity();
glTranslatef(0.0f,0.0f,-zoom); glMultMatrixf(translation->data());
QMatrix4x4 mat = QMatrix4x4(); QMatrix4x4 mat = QMatrix4x4();
mat.rotate(*rotation); mat.rotate(*rotation);
...@@ -238,49 +238,35 @@ void CubeWidget::setTessellation(int t) ...@@ -238,49 +238,35 @@ void CubeWidget::setTessellation(int t)
void CubeWidget::home() void CubeWidget::home()
{ {
zoom = 3; translation = new QMatrix4x4();
translation->translate(0.0,0.0,-3.0);
rotation = new QQuaternion(); rotation = new QQuaternion();
updateGL(); updateGL();
} }
void CubeWidget::mousePressEvent(QMouseEvent *event ) 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 ) 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) { if (event->buttons() & Qt::LeftButton) {
move(trackballPoint(event->pos().x(),event->pos().y())); rotate(trackballPoint(event->pos().x(),event->pos().y()));
event->accept();
//m_trackBalls[0].move(pixelPosToViewPos(event->scenePos()), m_trackBalls[2].rotation().conjugate()); } else if (event->buttons() & Qt::RightButton) {
move(new QPointF(event->screenPos()));
event->accept(); 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 //warum so besser
float angle = 180 / M_PI * asin(sqrt(QVector3D::dotProduct(axis, axis))); float angle = 180 / M_PI * asin(sqrt(QVector3D::dotProduct(axis, axis)));
...@@ -290,15 +276,29 @@ void CubeWidget::move(QVector3D *newPos ) ...@@ -290,15 +276,29 @@ void CubeWidget::move(QVector3D *newPos )
QQuaternion newRot = QQuaternion::fromAxisAndAngle(axis, angle) * *rotation; QQuaternion newRot = QQuaternion::fromAxisAndAngle(axis, angle) * *rotation;
rotation = new QQuaternion(newRot.toVector4D()); rotation = new QQuaternion(newRot.toVector4D());
lastPos = newPos; lastSpeherePos = newPos;
updateGL(); 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){ QVector3D* CubeWidget::trackballPoint(int x, int y){
float xo,yo,zo; float xo,yo,zo;
// qDebug()<<"x:"<< x << " y:"<<y; // qDebug()<<"x:"<< x << " y:"<<y;
xo = ((2.0*x)-width())/ height(); xo = ((2.0*x)-width())/ height();
yo = (height()-(2.0*y))/width(); yo = (height()-(2.0*y))/width();
...@@ -309,16 +309,17 @@ QVector3D* CubeWidget::trackballPoint(int x, int y){ ...@@ -309,16 +309,17 @@ QVector3D* CubeWidget::trackballPoint(int x, int y){
QVector3D *pos = new QVector3D(xo,yo,zo); QVector3D *pos = new QVector3D(xo,yo,zo);
pos->normalize(); pos->normalize();
// qDebug()<<"x:"<< xo << " y:"<<yo<<" z:"<<zo; // qDebug()<<"x:"<< xo << " y:"<<yo<<" z:"<<zo;
return pos; return pos;
} }
void CubeWidget::wheelEvent(QWheelEvent *event ) void CubeWidget::wheelEvent(QWheelEvent *event )
{ {
if(event->delta()<0) if(event->delta()<0)
zoom++; //TODO
translation->translate(0.0,0.0,1.0);
else else
zoom--; translation->translate(0.0,0.0,-1.0);
updateGL(); updateGL();
} }
...@@ -42,8 +42,12 @@ public slots: ...@@ -42,8 +42,12 @@ public slots:
private: private:
int tesselation; int tesselation;
int zoom; int zoom;
QVector3D *lastSpeherePos;
QPointF * lastScreenPos;
QQuaternion *rotation; QQuaternion *rotation;
QVector3D *lastPos; QMatrix4x4 * translation;
QGLShaderProgram *shader; QGLShaderProgram *shader;
...@@ -51,7 +55,8 @@ private: ...@@ -51,7 +55,8 @@ private:
void perspective(GLdouble fovy, GLdouble aspect, GLdouble zNear, GLdouble zFar); void perspective(GLdouble fovy, GLdouble aspect, GLdouble zNear, GLdouble zFar);
void setMaterial(GLfloat *color ); void setMaterial(GLfloat *color );
void move(QVector3D *newPos ); void rotate(QVector3D *newPos );
void move(QPointF *newPos );
QVector3D *trackballPoint(int x, int y); 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