Commit 063c2e60 by Kai Westerkamp

camera.cpp

parent e3c9a72d
...@@ -16,12 +16,14 @@ SOURCES += main.cpp\ ...@@ -16,12 +16,14 @@ SOURCES += main.cpp\
mainwindow.cpp \ mainwindow.cpp \
mainwidget.cpp \ mainwidget.cpp \
mesh.cpp \ mesh.cpp \
texture.cpp texture.cpp \
camera.cpp
HEADERS += mainwindow.h \ HEADERS += mainwindow.h \
mainwidget.h \ mainwidget.h \
mesh.h \ mesh.h \
texture.h texture.h \
camera.h
FORMS += FORMS +=
......
#include "camera.h"
Camera::Camera()
{
//setHome(new QQuaternion(45,0,1,0), new QVector3D(-100,0,0));
setHome(new QQuaternion(QQuaternion::fromAxisAndAngle(1,0,0,20)), new QVector3D(0,-25,-150));
}
void Camera::home()
{
this->rotation = homeRotation;
this->translation = homeTranslation;
}
void Camera::setHome(QQuaternion *rotation, QVector3D *translation){
this->homeRotation = rotation;
this->homeTranslation = translation;
home();
}
void Camera::rotate(QQuaternion newPos ){
QQuaternion newRot = newPos * *rotation;
rotation = new QQuaternion(newRot.toVector4D());
}
void Camera::move(QVector3D newPos ){
QVector3D newTrans = newPos + *translation;
translation = new QVector3D(newTrans);
}
QMatrix4x4 Camera::getMatrix(){
QMatrix4x4 mat = QMatrix4x4();
mat.translate(*translation);
mat.rotate(*rotation);
return mat;
}
#ifndef CAMERA_H
#define CAMERA_H
#include <QtGui>
#include <QtOpenGL>
#include <QMatrix4x4>
class Camera: public QObject
{
Q_OBJECT
private:
QQuaternion *rotation;
QVector3D *translation;
QQuaternion *homeRotation;
QVector3D *homeTranslation;
public slots:
void home();
public:
Camera();
void setHome(QQuaternion *rotation, QVector3D *translation);
void rotate(QQuaternion newPos );
void move(QVector3D newPos );
QMatrix4x4 getMatrix();
};
#endif // CAMERA_H
#include "mainwidget.h" #include "mainwidget.h"
MainWidget::MainWidget() MainWidget::MainWidget(Camera *cam)
{ {
this->cam = cam;
startTime = QTime::currentTime(); startTime = QTime::currentTime();
rotTime = QTime::currentTime(); rotTime = QTime::currentTime();
...@@ -23,17 +24,75 @@ QSize MainWidget::sizeHint() const ...@@ -23,17 +24,75 @@ QSize MainWidget::sizeHint() const
void MainWidget::wheelEvent(QWheelEvent *event ) void MainWidget::wheelEvent(QWheelEvent *event )
{ {
if(event->delta()<0) if(event->delta()<0)
camDistance -=10.0; cam->move(QVector3D(0.0,0.0,10));
else else
camDistance +=10.0; cam->move(QVector3D(0.0,0.0,-10));
}
m_view.setToIdentity(); void MainWidget::mousePressEvent(QMouseEvent *event ){
m_view.lookAt(QVector3D(0.0,camDistance,camDistance),QVector3D(0.0,0.0,0.0),QVector3D(0.0,1.0,0.0)); lastSpeherePos = trackballPoint(event->pos().x(),event->pos().y());
lastScreenPos = new QPointF(event->screenPos());
event->accept();
} }
void MainWidget::mouseMoveEvent(QMouseEvent *event ){
if (event->buttons() & Qt::LeftButton) {
rotate(trackballPoint(event->pos().x(),event->pos().y()));
event->accept();
} else if (event->buttons() & Qt::RightButton) {
move(new QPointF(event->screenPos()));
event->accept();
}
}
void MainWidget::rotate(QVector3D *newPos )
{
QVector3D axis = QVector3D::crossProduct(*lastSpeherePos,*newPos);
float angle = 180 / M_PI * asin(sqrt(QVector3D::dotProduct(axis, axis)));
axis.normalize();
cam->rotate(QQuaternion::fromAxisAndAngle(axis, angle));
lastSpeherePos = newPos;
}
void MainWidget::move(QPointF * newPos){
QPointF dt = *newPos;
dt -= *lastScreenPos;
dt *= 0.1f;
float dx = dt.x();
float dy = dt.y()*-1.0;
cam->move(QVector3D(dx,dy,0.0));
lastScreenPos = newPos;
}
QVector3D* MainWidget::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 MainWidget::initializeGL(){ void MainWidget::initializeGL(){
bool glFunctionsOK = initializeOpenGLFunctions(); bool glFunctionsOK = initializeOpenGLFunctions();
...@@ -50,10 +109,6 @@ void MainWidget::initializeGL(){ ...@@ -50,10 +109,6 @@ void MainWidget::initializeGL(){
// Shader // Shader
animationShader = initShader(QLatin1String(":/animate.frag"),QLatin1String(":/animate.vert")); animationShader = initShader(QLatin1String(":/animate.frag"),QLatin1String(":/animate.vert"));
camDistance = 100.0;
m_view = QMatrix4x4();
m_view.lookAt(QVector3D(0.0,camDistance,camDistance),QVector3D(0.0,0.0,0.0),QVector3D(0.0,1.0,0.0));
loadNewMesh(); loadNewMesh();
} }
...@@ -81,6 +136,8 @@ void MainWidget::setInterpolation(bool linear){ ...@@ -81,6 +136,8 @@ void MainWidget::setInterpolation(bool linear){
void MainWidget::setRotation(bool started){ void MainWidget::setRotation(bool started){
qDebug()<<"rotation"<<started; qDebug()<<"rotation"<<started;
this->rotation = started; this->rotation = started;
rotTime = QTime::currentTime();
} }
...@@ -152,7 +209,7 @@ void MainWidget::paintGL(){ ...@@ -152,7 +209,7 @@ void MainWidget::paintGL(){
ftime = 0; ftime = 0;
mesh->render(animationShader,m_view*rot, m_projection,(float) ftime,Animation); mesh->render(animationShader,cam->getMatrix()*rot, m_projection,(float) ftime,Animation);
animationShader->release(); animationShader->release();
update(); update();
......
...@@ -10,13 +10,14 @@ ...@@ -10,13 +10,14 @@
#include <assimp/scene.h> #include <assimp/scene.h>
#include <assimp/postprocess.h> #include <assimp/postprocess.h>
#include "mesh.h" #include "mesh.h"
#include "camera.h"
class MainWidget : public QOpenGLWidget, public QOpenGLFunctions_4_3_Core class MainWidget : public QOpenGLWidget, public QOpenGLFunctions_4_3_Core
{ {
Q_OBJECT Q_OBJECT
public: public:
MainWidget(); MainWidget(Camera *cam);
QSize minimumSizeHint() const; QSize minimumSizeHint() const;
QSize sizeHint() const; QSize sizeHint() const;
...@@ -33,18 +34,29 @@ protected: ...@@ -33,18 +34,29 @@ protected:
void initializeGL(); void initializeGL();
void paintGL(); void paintGL();
void resizeGL(int width, int height); void resizeGL(int width, int height);
void mousePressEvent(QMouseEvent *event ) ;
void mouseMoveEvent(QMouseEvent *event ) ;
void wheelEvent(QWheelEvent *event ) ; void wheelEvent(QWheelEvent *event ) ;
private: private:
QVector3D* MainWidget::trackballPoint(int x, int y);
void rotate(QVector3D *newPos );
void move(QPointF *newPos );
Camera *cam;
QOpenGLShaderProgram* animationShader; QOpenGLShaderProgram* animationShader;
QMatrix4x4 m_projection; QMatrix4x4 m_projection;
QMatrix4x4 m_view;
QTime startTime; QTime startTime;
QTime rotTime; QTime rotTime;
float camDistance; float camDistance;
QVector3D *lastSpeherePos;
QPointF * lastScreenPos;
boolean linear; boolean linear;
boolean rotation; boolean rotation;
boolean animate; boolean animate;
......
...@@ -3,7 +3,10 @@ ...@@ -3,7 +3,10 @@
MainWindow::MainWindow(QWidget *parent) : MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent) QMainWindow(parent)
{ {
m_centralWidget = new MainWidget(); Camera *cam = new Camera();
m_centralWidget = new MainWidget(cam);
toolBar = new QToolBar("Animation",this); toolBar = new QToolBar("Animation",this);
addToolBar(toolBar); addToolBar(toolBar);
...@@ -36,6 +39,12 @@ MainWindow::MainWindow(QWidget *parent) : ...@@ -36,6 +39,12 @@ MainWindow::MainWindow(QWidget *parent) :
connect(nextAnimation, SIGNAL(triggered()), m_centralWidget, SLOT(nextAnimation())); connect(nextAnimation, SIGNAL(triggered()), m_centralWidget, SLOT(nextAnimation()));
toolBar->addAction(nextAnimation); toolBar->addAction(nextAnimation);
CamHome = new QAction("Cam",this);
CamHome->setCheckable(false);
connect(CamHome, SIGNAL(triggered()), cam, SLOT(home()));
toolBar->addAction(CamHome);
this->setCentralWidget(m_centralWidget); this->setCentralWidget(m_centralWidget);
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
#include <QMainWindow> #include <QMainWindow>
#include <mainwidget.h> #include <mainwidget.h>
#include <mesh.h> #include <mesh.h>
#include "camera.h"
namespace Ui { namespace Ui {
class MainWindow; class MainWindow;
...@@ -26,6 +27,7 @@ private: ...@@ -26,6 +27,7 @@ private:
QAction *switchRotation; QAction *switchRotation;
QAction *stopAnimation; QAction *stopAnimation;
QAction *nextAnimation; QAction *nextAnimation;
QAction *CamHome;
}; };
......
...@@ -138,55 +138,55 @@ Mesh::Mesh(QOpenGLFunctions_4_3_Core *f,QString fileName) ...@@ -138,55 +138,55 @@ Mesh::Mesh(QOpenGLFunctions_4_3_Core *f,QString fileName)
globalInverseTransform = rootNode.transformation.inverted(); globalInverseTransform = rootNode.transformation.inverted();
// //Test Interpolation: // //Test Interpolation:
// if(false){ // if(false){
// const aiAnimation* animation = scene->mAnimations[0]; // const aiAnimation* animation = scene->mAnimations[0];
// const aiNodeAnim *nodeAnimation = findAnimOfNode(animation,"upperarm.R"); // const aiNodeAnim *nodeAnimation = findAnimOfNode(animation,"upperarm.R");
// qDebug()<<"KeyFrames"; // qDebug()<<"KeyFrames";
// for(uint i = 0; i < nodeAnimation->mNumPositionKeys; i++){ // for(uint i = 0; i < nodeAnimation->mNumPositionKeys; i++){
// qDebug()<<nodeAnimation->mPositionKeys[i].mTime<<convert(nodeAnimation->mPositionKeys[i].mValue); // qDebug()<<nodeAnimation->mPositionKeys[i].mTime<<convert(nodeAnimation->mPositionKeys[i].mValue);
// } // }
// qDebug()<<"Interpolated"; // qDebug()<<"Interpolated";
// for(float f = 0.0; f < (float)animation->mDuration; f += 0.5f){ // for(float f = 0.0; f < (float)animation->mDuration; f += 0.5f){
// qDebug()<<f<<calcInterpolatedTranslation(f,nodeAnimation); // qDebug()<<f<<calcInterpolatedTranslation(f,nodeAnimation);
// } // }
// } // }
// for (int i = 0; i < scene->mNumAnimations; ++i) { // for (int i = 0; i < scene->mNumAnimations; ++i) {
// const aiAnimation* animation = scene->mAnimations[i]; // const aiAnimation* animation = scene->mAnimations[i];
// qDebug()<<"Animation"<<animation->mName.C_Str(); // qDebug()<<"Animation"<<animation->mName.C_Str();
// switch (animation->mChannels[0]->mPreState) { // switch (animation->mChannels[0]->mPreState) {
// case aiAnimBehaviour_CONSTANT: // case aiAnimBehaviour_CONSTANT:
// qDebug()<<"Pre"<<"aiAnimBehaviour_CONSTANT"; // qDebug()<<"Pre"<<"aiAnimBehaviour_CONSTANT";
// break; // break;
// case aiAnimBehaviour_REPEAT: // use start or end // case aiAnimBehaviour_REPEAT: // use start or end
// qDebug()<<"Pre"<<"aiAnimBehaviour_REPEAT"; // qDebug()<<"Pre"<<"aiAnimBehaviour_REPEAT";
// break; // break;
// case aiAnimBehaviour_LINEAR: // linear interpolate // case aiAnimBehaviour_LINEAR: // linear interpolate
// qDebug()<<"Pre"<<"aiAnimBehaviour_LINEAR"; // qDebug()<<"Pre"<<"aiAnimBehaviour_LINEAR";
// break; // break;
// case aiAnimBehaviour_DEFAULT: // take default node transformation // case aiAnimBehaviour_DEFAULT: // take default node transformation
// qDebug()<<"Pre"<<"aiAnimBehaviour_DEFAULT"; // qDebug()<<"Pre"<<"aiAnimBehaviour_DEFAULT";
// break; // break;
// default: // default:
// qDebug()<<"Pre"<<"not"; // qDebug()<<"Pre"<<"not";
// } // }
// switch (animation->mChannels[0]->mPostState) { // switch (animation->mChannels[0]->mPostState) {
// case aiAnimBehaviour_CONSTANT: // case aiAnimBehaviour_CONSTANT:
// qDebug()<<"Post"<<"aiAnimBehaviour_CONSTANT"; // qDebug()<<"Post"<<"aiAnimBehaviour_CONSTANT";
// break; // break;
// case aiAnimBehaviour_REPEAT: // use start or end // case aiAnimBehaviour_REPEAT: // use start or end
// qDebug()<<"Post"<<"aiAnimBehaviour_REPEAT"; // qDebug()<<"Post"<<"aiAnimBehaviour_REPEAT";
// break; // break;
// case aiAnimBehaviour_LINEAR: // linear interpolate // case aiAnimBehaviour_LINEAR: // linear interpolate
// qDebug()<<"Post"<<"aiAnimBehaviour_LINEAR"; // qDebug()<<"Post"<<"aiAnimBehaviour_LINEAR";
// break; // break;
// case aiAnimBehaviour_DEFAULT: // take default node transformation // case aiAnimBehaviour_DEFAULT: // take default node transformation
// qDebug()<<"Post"<<"aiAnimBehaviour_DEFAULT"; // qDebug()<<"Post"<<"aiAnimBehaviour_DEFAULT";
// break; // break;
// default: // default:
// qDebug()<<"Post"<<"not"; // qDebug()<<"Post"<<"not";
// } // }
// } // }
loaded = true; loaded = true;
} }
...@@ -422,6 +422,7 @@ void Mesh::renderMesh(QOpenGLShaderProgram *shader, int index) ...@@ -422,6 +422,7 @@ void Mesh::renderMesh(QOpenGLShaderProgram *shader, int index)
f->glDrawElements(GL_TRIANGLES, entries[index].numIndex, GL_UNSIGNED_INT, 0); f->glDrawElements(GL_TRIANGLES, entries[index].numIndex, GL_UNSIGNED_INT, 0);
} }
//Bone Update
void Mesh::updateBoneTransform(float TimeInSeconds, int animIndex){ void Mesh::updateBoneTransform(float TimeInSeconds, int animIndex){
if(!scene || bones.size() == 0|| !scene->HasAnimations()) if(!scene || bones.size() == 0|| !scene->HasAnimations())
return; return;
...@@ -450,9 +451,9 @@ void Mesh::updateBoneHeirachy(float AnimTime,const aiAnimation* animation, Node ...@@ -450,9 +451,9 @@ void Mesh::updateBoneHeirachy(float AnimTime,const aiAnimation* animation, Node
//calc Tcurrent Trasnformation; //calc Tcurrent Trasnformation;
if (nodeAnimation) { if (nodeAnimation) {
QVector3D scale = calcInterpolatedScaling(AnimTime,nodeAnimation,node); QVector3D scale = calcInterpolatedScaling(AnimTime,nodeAnimation);
QQuaternion rot = calcInterpolatedRotation(AnimTime,nodeAnimation); QQuaternion rot = calcInterpolatedRotation(AnimTime,nodeAnimation);
QVector3D trans = calcInterpolatedTranslation(AnimTime,nodeAnimation,node); QVector3D trans = calcInterpolatedTranslation(AnimTime,nodeAnimation);
//qDebug()<<"calc"<<scale<<rot<<trans; //qDebug()<<"calc"<<scale<<rot<<trans;
nodeTransformation.setToIdentity(); nodeTransformation.setToIdentity();
...@@ -489,8 +490,6 @@ const aiNodeAnim* Mesh::findAnimOfNode(const aiAnimation* animation, QString Nod ...@@ -489,8 +490,6 @@ const aiNodeAnim* Mesh::findAnimOfNode(const aiAnimation* animation, QString Nod
return NULL; return NULL;
} }
//Interpolarisation //Interpolarisation
QQuaternion Mesh::calcInterpolatedRotation(float AnimTime,const aiNodeAnim *nodeAnimation){ QQuaternion Mesh::calcInterpolatedRotation(float AnimTime,const aiNodeAnim *nodeAnimation){
if(nodeAnimation->mNumRotationKeys == 1){ if(nodeAnimation->mNumRotationKeys == 1){
...@@ -560,10 +559,12 @@ QVector3D Mesh::calcInterpolatedVectorKey(float AnimTime,const aiNodeAnim *nodeA ...@@ -560,10 +559,12 @@ QVector3D Mesh::calcInterpolatedVectorKey(float AnimTime,const aiNodeAnim *nodeA
case aiAnimBehaviour_CONSTANT: // nearest Key case aiAnimBehaviour_CONSTANT: // nearest Key
return Factor <0.5f ? start : end; return Factor <0.5f ? start : end;
break; break;
case aiAnimBehaviour_REPEAT: // use start or end case aiAnimBehaviour_REPEAT:// use start or end
QVector3D before = convert(keys[index-1].mValue); {
QVector3D after = convert(keys[numberOfKeys - 1].mValue); // QVector3D before = convert(keys[index-1].mValue);
return Catmul(before,start,end,after,Factor); // QVector3D after = convert(keys[numberOfKeys - 1].mValue);
// return Catmul(before,start,end,after,Factor);
}
case aiAnimBehaviour_LINEAR: // linear interpolate case aiAnimBehaviour_LINEAR: // linear interpolate
return linInterpolate(start,end,Factor); return linInterpolate(start,end,Factor);
case aiAnimBehaviour_DEFAULT: // take default node transformation case aiAnimBehaviour_DEFAULT: // take default node transformation
...@@ -577,9 +578,11 @@ QVector3D Mesh::calcInterpolatedVectorKey(float AnimTime,const aiNodeAnim *nodeA ...@@ -577,9 +578,11 @@ QVector3D Mesh::calcInterpolatedVectorKey(float AnimTime,const aiNodeAnim *nodeA
return Factor <0.5f ? start : end; return Factor <0.5f ? start : end;
break; break;
case aiAnimBehaviour_REPEAT: // use start or end case aiAnimBehaviour_REPEAT: // use start or end
QVector3D before = convert(keys[index-1].mValue); {
QVector3D after = convert(keys[0].mValue); // QVector3D before = convert(keys[index-1].mValue);
return Catmul(before,start,end,after,Factor); // QVector3D after = convert(keys[0].mValue);
// return Catmul(before,start,end,after,Factor);
}
case aiAnimBehaviour_LINEAR: // linear interpolate case aiAnimBehaviour_LINEAR: // linear interpolate
return linInterpolate(start,end,Factor); return linInterpolate(start,end,Factor);
case aiAnimBehaviour_DEFAULT: // take default node transformation case aiAnimBehaviour_DEFAULT: // take default node transformation
......
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