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 )
{
// if (event->isAccepted())
// return;
if (event->buttons() & Qt::LeftButton) {
lastPos = trackballPoint(event->pos().x(),event->pos().y());
lastSpeherePos = trackballPoint(event->pos().x(),event->pos().y());
lastScreenPos = new QPointF(event->screenPos());
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,11 +276,25 @@ 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;
......@@ -316,9 +316,10 @@ QVector3D* CubeWidget::trackballPoint(int x, int y){
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