Commit 546c71b8 by Kai Westerkamp

broken Rotation and some clipping bugs

parent 229da88d
...@@ -43,13 +43,16 @@ void CubeWidget::paintGL ( ) ...@@ -43,13 +43,16 @@ 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); glTranslatef(0.0f,0.0f,-zoom);
QMatrix4x4 mat = QMatrix4x4(); QMatrix4x4 mat = QMatrix4x4();
mat.rotate(*rotation); mat.rotate(*rotation);
glMultMatrixf(mat.data()); glMultMatrixf(mat.data());
GLfloat red[] = {1.0,0.0,0.0}; GLfloat red[] = {1.0,0.0,0.0};
GLfloat green[] = {0.0,1.0,0.0}; GLfloat green[] = {0.0,1.0,0.0};
GLfloat blue[] = {0.0,0.0,1.0}; GLfloat blue[] = {0.0,0.0,1.0};
...@@ -123,6 +126,8 @@ void CubeWidget::paintGL ( ) ...@@ -123,6 +126,8 @@ void CubeWidget::paintGL ( )
glEnd(); glEnd();
} }
void CubeWidget::resizeGL(int width , int height ) void CubeWidget::resizeGL(int width , int height )
{ {
glMatrixMode(GL_PROJECTION); glMatrixMode(GL_PROJECTION);
...@@ -153,7 +158,7 @@ void CubeWidget::showWireframe() ...@@ -153,7 +158,7 @@ void CubeWidget::showWireframe()
qDebug("show Wireframe"); qDebug("show Wireframe");
glShadeModel(GL_FLAT); glShadeModel(GL_FLAT);
glDisable(GL_CULL_FACE); // glDisable(GL_CULL_FACE);
glPolygonMode(GL_FRONT_AND_BACK,GL_LINE); glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
updateGL(); updateGL();
...@@ -163,7 +168,7 @@ void CubeWidget::showFlat() ...@@ -163,7 +168,7 @@ void CubeWidget::showFlat()
{ {
qDebug("show Flat"); qDebug("show Flat");
glShadeModel(GL_FLAT); glShadeModel(GL_FLAT);
glEnable(GL_CULL_FACE); // glEnable(GL_CULL_FACE);
glPolygonMode(GL_FRONT_AND_BACK,GL_FILL); glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
updateGL(); updateGL();
} }
...@@ -172,7 +177,7 @@ void CubeWidget::showGouraut() ...@@ -172,7 +177,7 @@ void CubeWidget::showGouraut()
{ {
qDebug("show Gouraut"); qDebug("show Gouraut");
glShadeModel(GL_SMOOTH); glShadeModel(GL_SMOOTH);
glEnable(GL_CULL_FACE); // glEnable(GL_CULL_FACE);
glPolygonMode(GL_FRONT_AND_BACK,GL_FILL); glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
updateGL(); updateGL();
} }
...@@ -201,11 +206,73 @@ void CubeWidget::home() ...@@ -201,11 +206,73 @@ void CubeWidget::home()
void CubeWidget::mousePressEvent(QMouseEvent *event ) void CubeWidget::mousePressEvent(QMouseEvent *event )
{ {
// if (event->isAccepted())
// return;
if (event->buttons() & Qt::LeftButton) {
qDebug("press");
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://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) {
qDebug("move");
move(trackballPoint(event->pos().x(),event->pos().y()));
//m_trackBalls[0].move(pixelPosToViewPos(event->scenePos()), m_trackBalls[2].rotation().conjugate());
event->accept();
} else {
//m_trackBalls[0].release(pixelPosToViewPos(event->scenePos()), m_trackBalls[2].rotation().conjugate());
}
}
void CubeWidget::move(QVector3D *newPos )
{
QVector3D axis = QVector3D::crossProduct(*lastPos,*newPos);
//warum so besser
float angle = 180 / M_PI * std::asin(std::sqrt(QVector3D::dotProduct(axis, axis)));
axis.normalize();
axis = rotation->conjugate().rotatedVector(axis);
QQuaternion newRot = QQuaternion::fromAxisAndAngle(axis, angle) * *rotation;
rotation = new QQuaternion(newRot.toVector4D());
lastPos = newPos;
updateGL();
}
QVector3D* CubeWidget::trackballPoint(int x, int y){
float xo,yo,zo;
qDebug()<<"x:"<< x << " y:"<<y;
xo = ((2.0*x)-width())/ height();
yo = (height()-(2.0*y))/width();
float d = sqrt(xo*xo+yo*yo);
zo = qMax(qCos(M_PI_2*d),qreal(0.0)); //qMin(d,1.0f)
QVector3D *pos = new QVector3D(xo,yo,zo);
pos->normalize();
qDebug()<<"x:"<< xo << " y:"<<yo<<" z:"<<zo;
return pos;
} }
void CubeWidget::wheelEvent(QWheelEvent *event ) void CubeWidget::wheelEvent(QWheelEvent *event )
......
...@@ -8,6 +8,8 @@ ...@@ -8,6 +8,8 @@
#include <math.h> #include <math.h>
#include <QMatrix4x4> #include <QMatrix4x4>
#include <QQuaternion> #include <QQuaternion>
#include <QVector3D>
#include <algorithm>
...@@ -41,9 +43,13 @@ private: ...@@ -41,9 +43,13 @@ private:
int tesselation; int tesselation;
int zoom; int zoom;
QQuaternion *rotation; QQuaternion *rotation;
QPoint *lastPos; QVector3D *lastPos;
bool mousDown = false;
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 );
QVector3D *trackballPoint(int x, int y);
}; };
#endif // CUBEWIDGET_H #endif // CUBEWIDGET_H
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