Commit 10e12c16 by Kai Westerkamp

start of Animation/mainwidget.cpp

parent bcc29328
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
MainWidget::MainWidget() MainWidget::MainWidget()
{ {
startTime = QTime::currentTime();
} }
QSize MainWidget::minimumSizeHint() const QSize MainWidget::minimumSizeHint() const
...@@ -104,7 +104,9 @@ void MainWidget::paintGL(){ ...@@ -104,7 +104,9 @@ void MainWidget::paintGL(){
animationShader->setUniformValue("colorTexture",0); animationShader->setUniformValue("colorTexture",0);
animationShader->setUniformValue("LightPos",QVector3D(0,100,100)); animationShader->setUniformValue("LightPos",QVector3D(0,100,100));
mesh->render(animationShader,m_view*rot, m_projection); time = startTime.msecsTo(QTime::currentTime()) /1000.0;
mesh->render(animationShader,m_view*rot, m_projection,(float) time);
animationShader->release(); animationShader->release();
update(); update();
......
...@@ -35,6 +35,7 @@ private: ...@@ -35,6 +35,7 @@ private:
QOpenGLShaderProgram* animationShader; QOpenGLShaderProgram* animationShader;
QMatrix4x4 m_projection; QMatrix4x4 m_projection;
QMatrix4x4 m_view; QMatrix4x4 m_view;
QTime startTime;
float camDistance; float camDistance;
......
...@@ -284,8 +284,14 @@ void Mesh::updateBoneTransform(float TimeInSeconds){ ...@@ -284,8 +284,14 @@ void Mesh::updateBoneTransform(float TimeInSeconds){
if(!scene) if(!scene)
return; return;
const aiAnimation* animation = scene->mAnimations[0]; const aiAnimation* animation = scene->mAnimations[0];
float TicksPerSecond = (float)(animation->mTicksPerSecond != 0 ? animation->mTicksPerSecond : 25.0f);
float TimeInTicks = TimeInSeconds * TicksPerSecond;
float AnimationTime = fmod(TimeInTicks, (float)animation->mDuration);
//TODO calc time and get Anaimation; //TODO calc time and get Anaimation;
updateBoneHeirachy(0.0,animation,this->rootNode,QMatrix4x4()); updateBoneHeirachy(AnimationTime,animation,this->rootNode,QMatrix4x4());
...@@ -345,28 +351,81 @@ const aiNodeAnim* Mesh::findAnimOfNode(const aiAnimation* animation, QString Nod ...@@ -345,28 +351,81 @@ const aiNodeAnim* Mesh::findAnimOfNode(const aiAnimation* animation, QString Nod
QVector3D Mesh::calcInterpolatedScaling(float AnimTime,const aiNodeAnim *nodeAnimation){ QVector3D Mesh::calcInterpolatedScaling(float AnimTime,const aiNodeAnim *nodeAnimation){
int index = nodeAnimation->mNumScalingKeys/2; if(nodeAnimation->mNumScalingKeys == 1){
//index = 0; return convert(nodeAnimation->mScalingKeys[0].mValue);
aiVector3D scale = nodeAnimation->mScalingKeys[index].mValue; }
return QVector3D(scale.x,scale.y,scale.z);
//Index find
int index = 0;
for (uint i = 0 ; i < nodeAnimation->mNumScalingKeys - 1; i++) {
if (AnimTime < (float)nodeAnimation->mScalingKeys[i + 1].mTime) {
index = i;
break;
}
}
float DeltaTime = (float)(nodeAnimation->mScalingKeys[index+1].mTime - nodeAnimation->mScalingKeys[index].mTime);
float Factor = (AnimTime - (float)nodeAnimation->mScalingKeys[index].mTime) / DeltaTime;
QVector3D start = convert(nodeAnimation->mScalingKeys[index].mValue);
QVector3D end = convert(nodeAnimation->mScalingKeys[index+1].mValue);
return linInterpolate(start,end,Factor);
} }
QQuaternion Mesh::calcInterpolatedRotation(float AnimTime,const aiNodeAnim *nodeAnimation){ QQuaternion Mesh::calcInterpolatedRotation(float AnimTime,const aiNodeAnim *nodeAnimation){
int index = nodeAnimation->mNumRotationKeys/2; if(nodeAnimation->mNumRotationKeys == 1){
// index = 0; return convert(nodeAnimation->mRotationKeys[0].mValue);
aiQuaternion quat = nodeAnimation->mRotationKeys[index].mValue; }
return QQuaternion(quat.w,quat.x,quat.y,quat.z);
//Index find
int index = 0;
for (uint i = 0 ; i < nodeAnimation->mNumRotationKeys - 1 ; i++) {
if (AnimTime < (float)nodeAnimation->mRotationKeys[i + 1].mTime) {
index = i;
break;
}
}
float DeltaTime = (float)(nodeAnimation->mRotationKeys[index+1].mTime - nodeAnimation->mRotationKeys[index].mTime);
float Factor = (AnimTime - (float)nodeAnimation->mRotationKeys[index].mTime) / DeltaTime;
// qDebug()<<AnimTime<<DeltaTime<<Factor;
const aiQuaternion start = nodeAnimation->mRotationKeys[index].mValue;
const aiQuaternion end = nodeAnimation->mRotationKeys[index+1].mValue;
aiQuaternion RotationQ;
aiQuaternion::Interpolate(RotationQ,start,end,Factor);
return convert(RotationQ);
//return convert(start);
} }
QVector3D Mesh::calcInterpolatedTranslation(float AnimTime, const aiNodeAnim *nodeAnimation){ QVector3D Mesh::calcInterpolatedTranslation(float AnimTime, const aiNodeAnim *nodeAnimation){
int index = nodeAnimation->mNumScalingKeys/2; if(nodeAnimation->mNumPositionKeys == 1){
// index = 0; convert(nodeAnimation->mPositionKeys[0].mValue);
aiVector3D pos = nodeAnimation->mPositionKeys[index].mValue; }
return QVector3D(pos.x,pos.y,pos.z);
//Index find
int index = 0;
for (uint i = 0 ; i < nodeAnimation->mNumPositionKeys - 1 ; i++) {
if (AnimTime < (float)nodeAnimation->mPositionKeys[i + 1].mTime) {
index = i;
break;
}
}
float DeltaTime = (float)(nodeAnimation->mPositionKeys[index+1].mTime - nodeAnimation->mPositionKeys[index].mTime);
float Factor = (AnimTime - (float)nodeAnimation->mPositionKeys[index].mTime) / DeltaTime;
QVector3D start = convert(nodeAnimation->mPositionKeys[index].mValue);
QVector3D end = convert(nodeAnimation->mPositionKeys[index+1].mValue);
return linInterpolate(start,end,Factor);
} }
void Mesh::render(QOpenGLShaderProgram *shader, QMatrix4x4 V,QMatrix4x4 P){ void Mesh::render(QOpenGLShaderProgram *shader, QMatrix4x4 V,QMatrix4x4 P, float TimeInSeconds){
if(!loaded) if(!loaded)
return; return;
if(f == NULL){ if(f == NULL){
...@@ -381,7 +440,7 @@ void Mesh::render(QOpenGLShaderProgram *shader, QMatrix4x4 V,QMatrix4x4 P){ ...@@ -381,7 +440,7 @@ void Mesh::render(QOpenGLShaderProgram *shader, QMatrix4x4 V,QMatrix4x4 P){
f->glEnableVertexAttribArray(boneweightIndex); f->glEnableVertexAttribArray(boneweightIndex);
updateBoneTransform(0.0); updateBoneTransform(TimeInSeconds);
QVector<QMatrix4x4> boneTransforms = QVector<QMatrix4x4>(bones.size()); QVector<QMatrix4x4> boneTransforms = QVector<QMatrix4x4>(bones.size());
for(int i = 0; i < bones.size(); i++){ for(int i = 0; i < bones.size(); i++){
boneTransforms[i] = bones[i].FinalTransformation; boneTransforms[i] = bones[i].FinalTransformation;
......
...@@ -47,7 +47,7 @@ public: ...@@ -47,7 +47,7 @@ public:
void render(QOpenGLShaderProgram *shader, QMatrix4x4 V,QMatrix4x4 P); void render(QOpenGLShaderProgram *shader, QMatrix4x4 V,QMatrix4x4 P, float TimeInSeconds);
private: private:
...@@ -138,6 +138,11 @@ private: ...@@ -138,6 +138,11 @@ private:
void renderNode(QOpenGLShaderProgram *shader, Node &node, QMatrix4x4 V,QMatrix4x4 P,QMatrix4x4 M); void renderNode(QOpenGLShaderProgram *shader, Node &node, QMatrix4x4 V,QMatrix4x4 P,QMatrix4x4 M);
void renderMesh(QOpenGLShaderProgram *shader, int index); void renderMesh(QOpenGLShaderProgram *shader, int index);
QVector3D convert(aiVector3D in){ return QVector3D(in.x,in.y,in.z);}
QQuaternion convert(aiQuaternion quat){ return QQuaternion(quat.w,quat.x,quat.y,quat.z);}
QVector3D linInterpolate(QVector3D start, QVector3D end, float Factor){ return (1.0-Factor)*start + Factor*end;}
}; };
#endif // MESH_H #endif // MESH_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