动画COLLADA模型问题
|
我在为加载的COLLADA模型设置动画时遇到一些问题。我已经编写了自己的解析器,现在我也想编写自己的绘制例程。问题在于,一旦在模型上启用动画,手,腿和头就会从模型的原点拉开。 (加载器是根据此处的教程实现的:COLLADA教程)
在模型的绘制功能中,我要做的第一件事是使用读取块中的给定目标设置关节矩阵(不是世界矩阵!),
例如,如果我阅读类似的频道:
,和动画了。随时在gear3d.de上查看我的实现(下载SVN干线)
我希望这可以帮助一些人针对这个奇妙的话题实施自己的解决方案:)
<channel source=\"#some_sampler\" target=\"some_joint/transform(3)(2)\"/>
在第一步中,我将使用sid = \“ transform \”从joint \的jointMatrix修改矩阵分量(3)(2):
if( mCurrentAnimations_.size() > 0 ) {
unsigned currentFrame = GEAR::Root::getSingleton().getFrameEvent().frame;
bool updateTime = false;
if( currentFrame != mLastFrameUpdate_ ) {
if( timeSinceLastFrame < 1.0f )
updateTime = true;
mLastFrameUpdate_ = currentFrame;
}
/****************************************************
* If we have an active animation, *
* we animate it in each of it\'s defined channels *
***************************************************/
std::list<DAEAnimation*>::iterator it = mCurrentAnimations_.begin();
while( it != mCurrentAnimations_.end() ) {
for( int c = 0; c < (*it)->animation->channels.size(); ++c ) {
// update the time of the channelanimation if requested
if( updateTime ) {
(*it)->channelStates[c].elapsedTime += timeSinceLastFrame;
}
GEAR::COLLADA::Channel* channel = (*it)->animation->channels[c];
// read the two indices depending on the time we\'re
int firstKeyframeTimeIndex = 0;
int secondKeyframeTimeIndex = 0;
for( int i = 0; i < channel->sampler->inputSource->mFloatArray_->mCount_; ++i ) {
float time = channel->sampler->inputSource->mFloatArray_->mFloats_[i];
if( firstKeyframeTimeIndex == secondKeyframeTimeIndex && time > (*it)->channelStates[c].elapsedTime && i > 0) {
firstKeyframeTimeIndex = i-1;
secondKeyframeTimeIndex = i;
break;
}
if( firstKeyframeTimeIndex == secondKeyframeTimeIndex && i == channel->sampler->inputSource->mFloatArray_->mCount_-1 ) {
(*it)->channelStates[c].elapsedTime = 0.0f;
firstKeyframeTimeIndex = i;
secondKeyframeTimeIndex = 0;
break;
}
}
// look what kind of TargetAccessor we have
if( channel->targetAccessor != NULL && channel->targetAccessor->type == GEAR::MATRIX_ACCESSOR ) {
// ok we have to read 1 value for first and second index
float firstValue = channel->sampler->outputSource->mFloatArray_->mFloats_[firstKeyframeTimeIndex];
float secondValue = channel->sampler->outputSource->mFloatArray_->mFloats_[secondKeyframeTimeIndex];
float firstTime = channel->sampler->inputSource->mFloatArray_->mFloats_[firstKeyframeTimeIndex];
float secondTime = channel->sampler->inputSource->mFloatArray_->mFloats_[secondKeyframeTimeIndex];
float interpolateValue = 1.0f / (secondTime - firstTime) * (secondTime - (*it)->channelStates[c].elapsedTime);
// now we calculate an linear interpolated value
float value = (secondValue*interpolateValue) + (firstValue*(1.0-interpolateValue));
// now we have to write this value to the Joint\'s Matrix
int entry = ((COLLADA::MatrixTargetAccessor*)channel->targetAccessor)->firstAccessor*4+((COLLADA::MatrixTargetAccessor*)channel->targetAccessor)->secondAccessor;
channel->targetJoint->matrix->jointSpaceMatrix.entries[entry] = channel->targetJoint->matrix->matrix.entries[entry] + value;
}
}
++it;
}
}
在所有通道修改了jointMatrices之后,我通过在根Joint上调用以下函数来重新计算joint的worldMatrices:
void
COLLADA::Joint::recalcWorldSpaceTransMat() {
GEAR::Mat4 parentMat;
if( parent != NULL )
parentMat = parent->worldSpaceTransformationMatrix;
// @todo Here we have to test against NULL!
if( matrix != NULL )
this->worldSpaceTransformationMatrix = parentMat * matrix->jointSpaceMatrix;
else {
this->worldSpaceTransformationMatrix = parentMat;
}
//std::cout << \"Joint \" << sid << \" recalculated\\n\";
for( int i = 0; i < mChildJoints_.size(); ++i )
mChildJoints_[i]->recalcWorldSpaceTransMat();
}
现在一切都准备就绪,可以在我的draw函数的最后一部分下面绘制模型宽度:
for( int i = 0; i < mSubMeshes_.size(); ++i ) {
for( int k = 0; k < mSubMeshes_[i]->mSubMeshes_.size(); ++k ) {
// first we animate it
GEAR::DAESubMesh* submesh = mSubMeshes_[i]->mSubMeshes_[k];
submesh->buffer->lock( true );
{
for( unsigned v = 0; v < submesh->buffer->getNumVertices(); ++v ) {
// get the array of joints, which influence the current vertex
DAEVertexInfo* vertexInfo = submesh->vertexInfo[v];
GEAR::Vec3 vertex; // do not init the vertex with any value!
float totalWeight = 0.0f;
for( int j = 0; j < vertexInfo->joints.size(); ++j ) {
Mat4& invBindPoseMatrix = vertexInfo->joints[j]->joint->invBindPoseMatrix;
Mat4& transMat = vertexInfo->joints[j]->joint->worldSpaceTransformationMatrix;
totalWeight += vertexInfo->joints[j]->weight;
vertex += (transMat*invBindPoseMatrix*(submesh->skin->bindShapeMatrix*vertexInfo->vertex))*vertexInfo->joints[j]->weight;
}
if( totalWeight != 1.0f ) {
float normalizedWeight = 1.0f / totalWeight;
vertex *= normalizedWeight;
}
submesh->buffer->bufferVertexPos( v, vertex );
}
}
submesh->buffer->unlock();
mSubMeshes_[i]->mSubMeshes_[k]->buffer->draw( GEAR::TRIANGLES, 0, mSubMeshes_[i]->mSubMeshes_[k]->buffer->getNumVertices() );
}
}
现在的问题是,输出如下所示:
我肯定正确执行了数据加载例程,因为可以看到行走的人的一般动画,但是网格变形了:
正如我所说,当我取消注释时:
channel->targetJoint->matrix->jointSpaceMatrix.entries[entry] = channel->targetJoint->matrix->matrix.entries[entry] + value;
动画被禁用,并且模型以其标准姿势显示:
现在,另外,当我在重新计算关节的worldMatrix之前,向jointMatrices的前3列添加规范化内容时:
GEAR::Vec3 row1( matrix->jointSpaceMatrix.entries[0], matrix->jointSpaceMatrix.entries[1], matrix->jointSpaceMatrix.entries[2] );
row1.normalize();
matrix->jointSpaceMatrix.entries[0] = row1.x;
matrix->jointSpaceMatrix.entries[1] = row1.y;
matrix->jointSpaceMatrix.entries[2] = row1.z;
GEAR::Vec3 row2( matrix->jointSpaceMatrix.entries[4], matrix->jointSpaceMatrix.entries[5], matrix->jointSpaceMatrix.entries[6] );
row2.normalize();
matrix->jointSpaceMatrix.entries[4] = row2.x;
matrix->jointSpaceMatrix.entries[5] = row2.y;
matrix->jointSpaceMatrix.entries[6] = row2.z;
GEAR::Vec3 row3( matrix->jointSpaceMatrix.entries[8], matrix->jointSpaceMatrix.entries[9], matrix->jointSpaceMatrix.entries[10] );
row3.normalize();
matrix->jointSpaceMatrix.entries[8] = row3.x;
matrix->jointSpaceMatrix.entries[9] = row3.y;
matrix->jointSpaceMatrix.entries[10] = row3.z;
问题仍然存在,但是这次在另一个输出中。 Man现在看起来像一个外星人:D,但这减少了缩放比例:
我现在不完全正确,是否已经按照正确的方式进行了标准化。真的需要这种规范化吗?本教程中没有对此进行描述,而且我也找不到任何相关的内容。
毕竟,我从教程页面的代码中了解了插值的实现。 AND:他们根本不使用任何四元数来插入孔矩阵。他们的工作如下(这对我不起作用):
Mat4 temp;
for (int i = 0; i < 16; ++i)
temp.entries[i] = interpolatef(matrix->jointSpaceMatrixStart.entries[i],matrix->jointSpaceMatrixFinish.entries[i],matrix->delta);
Vec3 forward,up,right,translation;
forward = Vec3(temp.entries[8], temp.entries[9], temp.entries[10]);
up= Vec3(temp.entries[4], temp.entries[5], temp.entries[6]);
right = Vec3(temp.entries[0], temp.entries[1], temp.entries[2]);
forward.normalize();
up.normalize();
right.normalize();
temp.entries[8] = forward.x; temp.entries[9] = forward.y; temp.entries[10] = forward.z;
temp.entries[4] = up.x; temp.entries[5] = up.y; temp.entries[6] = up.z;
temp.entries[0] = right.x; temp.entries[1] = right.y; temp.entries[2] = right.z;
matrix->jointSpaceMatrix = GEAR::Mat4(temp);
然后我以其他方式使用四元数(也不适用于我):
// wat we need for interpolation: rotMatStart, rotMatFinish, delta
// create rotation matrices from our 2 given matrices
GEAR::Mat4 rotMatStart = matrix->jointSpaceMatrixStart;
rotMatStart.setTranslationPart( GEAR::VEC3_ZERO );
GEAR::Mat4 rotMatFinish = matrix->jointSpaceMatrixFinish;
rotMatFinish.setTranslationPart( GEAR::VEC3_ZERO );
rotMatStart.transpose();
rotMatFinish.transpose();
// create Quaternions, which represent these 2 matrices
float w = GEAR::Tools::sqr(1.0 + rotMatStart.entries[0] + rotMatStart.entries[5] + rotMatStart.entries[10]) / 2.0;
float w4 = (4.0 * w);
float x = (rotMatStart.entries[6] - rotMatStart.entries[9]) / w4 ;
float y = (rotMatStart.entries[8] - rotMatStart.entries[2]) / w4 ;
float z = (rotMatStart.entries[1] - rotMatStart.entries[4]) / w4 ;
GEAR::Quaternion rotQuadStart(x, y, z, w);
rotQuadStart.normalize();
w = GEAR::Tools::sqr(1.0 + rotMatFinish.entries[0] + rotMatFinish.entries[5] + rotMatFinish.entries[10]) / 2.0;
w4 = (4.0 * w);
x = (rotMatFinish.entries[6] - rotMatFinish.entries[9]) / w4 ;
y = (rotMatFinish.entries[8] - rotMatFinish.entries[2]) / w4 ;
z = (rotMatFinish.entries[1] - rotMatFinish.entries[4]) / w4 ;
GEAR::Quaternion rotQuadFinish(x, y, z, w);
rotQuadFinish.normalize();
// create the interpolated rotation matrix
GEAR::Quaternion slerpedRotQuat = slerp(rotQuadStart, rotQuadFinish, matrix->delta );
slerpedRotQuat.normalize();
GEAR::Mat4 rotMat;
slerpedRotQuat.createMatrix( rotMat );
// interpolate the translation part
GEAR::Vec3 transVecStart(0.0,0.0,0.0);
matrix->jointSpaceMatrixStart.getTranslatedVector3D( transVecStart );
GEAR::Vec3 transVecFinish(0.0,0.0,0.0);
matrix->jointSpaceMatrixFinish.getTranslatedVector3D( transVecFinish );
GEAR::Mat4 transMat;
transMat.setTranslation( transVecFinish*matrix->delta + (transVecStart*(1.0f-matrix->delta)) );
// now write the resulting Matrix back to the Joint
matrix->jointSpaceMatrix = transMat * rotMat;
它也对我不起作用。似乎没有任何作用。我真的不知道这是怎么回事。
现在过了两天,由于datenwolf的回答,我开始工作了
我想告诉我所有的工作方式。现在,一切似乎都很清楚,而且一直都只有一小步。
现在我们从动画部分开始。我遍历所有通道,并将起点和终点值以及插值增量值(范围0.0 1.0)保存到关节,该通道会进行动画处理:
if( mCurrentAnimations_.size() > 0 ) {
unsigned currentFrame = GEAR::Root::getSingleton().getFrameEvent().frame;
bool updateTime = false;
if( currentFrame != mLastFrameUpdate_ ) {
if( timeSinceLastFrame < 1.0f )
updateTime = true;
mLastFrameUpdate_ = currentFrame;
}
/****************************************************
* If we have an active animation, *
* we animate it in each of it\'s defined channels *
***************************************************/
std::list<DAEAnimation*>::iterator it = mCurrentAnimations_.begin();
while( it != mCurrentAnimations_.end() ) {
for( int c = 0; c < (*it)->animation->channels.size(); ++c ) {
// update the time of the channelanimation if requested
if( updateTime ) {
(*it)->channelStates[c].elapsedTime += timeSinceLastFrame;
}
GEAR::COLLADA::Channel* channel = (*it)->animation->channels[c];
// read the two indices depending on the time we\'re
int firstIndex = 0;
int secondIndex = 1;
for( int i = 0; i < channel->sampler->inputSource->mFloatArray_->mCount_; ++i ) {
float time = channel->sampler->inputSource->mFloatArray_->mFloats_[i];
if( time > (*it)->channelStates[c].elapsedTime ) {
firstIndex = i-1;
secondIndex = i;
if( firstIndex == -1 ) // set to last frame
firstIndex = channel->sampler->inputSource->mFloatArray_->mCount_ - 1;
break;
}
else if( i == channel->sampler->inputSource->mFloatArray_->mCount_ - 1 ) {
(*it)->channelStates[c].elapsedTime -= channel->sampler->inputSource->mFloatArray_->mFloats_[i];
firstIndex = 0;
secondIndex = 1;
break;
}
}
// look what kind of TargetAccessor we have
if( channel->targetAccessor != NULL && channel->targetAccessor->type == GEAR::MATRIX_ACCESSOR ) {
/************************************************************************
* Matrix accessors, which are read from a COLLADA <channel> block *
* will always target one matrix component they animate. *
* Such accessors are for example: *
* <channel source\"#someSource\" target=\"someJoint/transform(0)(2)\"/> *
* *
* @TODO: *
* In a pre processing step, we have to group all channels, which *
* operate on the same joint. In order to accelerate the processing of *
* grouped channels, we have to expand the number of keyframes of all *
* channels to the maximum of all channels. *
************************************************************************/
unsigned entry = ((COLLADA::MatrixTargetAccessor*)channel->targetAccessor)->index;
float firstTime = channel->sampler->inputSource->mFloatArray_->mFloats_[firstIndex];
float secondTime = channel->sampler->inputSource->mFloatArray_->mFloats_[secondIndex];
// in case of matrix accessor, we write the startMatrix and the endMatrix to the Joints accessor, who finally will do the animation interpolation
channel->targetJoint->matrix->interpolationRequired = true;
// write out the start and end value to the jointSpaceMatrix
// this matrix will later be interpolated
channel->targetJoint->matrix->jointSpaceMatrixStart.entries[entry] = channel->sampler->outputSource->mFloatArray_->mFloats_[firstIndex];
channel->targetJoint->matrix->jointSpaceMatrixFinish.entries[entry] = channel->sampler->outputSource->mFloatArray_->mFloats_[secondIndex];
// the delta value is in the range [0.0,1.0]
channel->targetJoint->matrix->delta = 1.0f / (secondTime - firstTime) * (secondTime - (*it)->channelStates[c].elapsedTime);
}
}
++it;
}
}
如您所见,这里根本没有插值。我们只需缓存所有动画关节的开始值和结束值以及增量(并在每个修改的关节上设置一个标志)
现在,在完成所有动画之后,我们在所有根关节上调用函数interpolateMatrices():
for( int i = 0; i < mSourceModel_->mVisualSceneLibrary_.mVisualScenes_.size(); ++i ) {
for( int v = 0; v < mSourceModel_->mVisualSceneLibrary_.mVisualScenes_[i]->mSkeleton_.size(); ++v ) {
if( mSourceModel_->mVisualSceneLibrary_.mVisualScenes_[i]->mSkeleton_[v]->mRootJoint_ != NULL ) {
/************************************************************************************
* Now we have constructed all jointSpaceMatrixces for the start and the end and *
* we\'re ready to interpolate them and to also recalculate the joint\'s *
* worldSpaceMatrix. *
***********************************************************************************/
mSourceModel_->mVisualSceneLibrary_.mVisualScenes_[i]->mSkeleton_[v]->mRootJoint_->interpolateMatrices();
}
}
}
这并不是什么新鲜事物,但是有趣的部分是插值的实现。根本没有四元数:
void COLLADA::Joint::interpolateMatrices() {
if( matrix != NULL && matrix->interpolationRequired ) {
for (unsigned i = 0; i < 16; ++i)
matrix->jointSpaceMatrix.entries[i] = interpolatef(matrix->jointSpaceMatrixStart.entries[i],matrix->jointSpaceMatrixFinish.entries[i],matrix->delta);
Vec3 forward,up,right,translation;
forward = Vec3(matrix->jointSpaceMatrix.entries[8], matrix->jointSpaceMatrix.entries[9], matrix->jointSpaceMatrix.entries[10]);
up= Vec3(matrix->jointSpaceMatrix.entries[4], matrix->jointSpaceMatrix.entries[5], matrix->jointSpaceMatrix.entries[6]);
right = Vec3(matrix->jointSpaceMatrix.entries[0], matrix->jointSpaceMatrix.entries[1], matrix->jointSpaceMatrix.entries[2]);
forward.normalize();
up.normalize();
right.normalize();
matrix->jointSpaceMatrix.entries[8] = forward.x; matrix->jointSpaceMatrix.entries[9] = forward.y; matrix->jointSpaceMatrix.entries[10] = forward.z;
matrix->jointSpaceMatrix.entries[4] = up.x; matrix->jointSpaceMatrix.entries[5] = up.y; matrix->jointSpaceMatrix.entries[6] = up.z;
matrix->jointSpaceMatrix.entries[0] = right.x; matrix->jointSpaceMatrix.entries[1] = right.y; matrix->jointSpaceMatrix.entries[2] = right.z;
matrix->jointSpaceMatrix.entries[15] = 1.0f; // this component is always 1.0! In some files, this is exported the wrong way, which causes bugs!
}
/********************************************************
* After the interpolation is finished, *
* we have to recalculate the joint\'s worldSpaceMatrix. *
********************************************************/
GEAR::Mat4 parentMat;
if( parent != NULL )
parentMat = parent->worldSpaceTransformationMatrix;
if( matrix != NULL )
worldSpaceTransformationMatrix = (parentMat * matrix->jointSpaceMatrix);
else
worldSpaceTransformationMatrix = parentMat;
skinningMatrix = worldSpaceTransformationMatrix*invBindPoseMatrix;
// also interpolate and recalculate all childs
for( unsigned k = 0; k < mChildJoints_.size(); ++k )
mChildJoints_[k]->interpolateMatrices();
}
如您所见,我们简单地插入矩阵的所有值,然后对矩阵的前三列进行归一化。
之后,我们立即重新计算该关节的worldSpaceMatrix以及完整的蒙皮矩阵以节省性能。
现在,我们几乎完成了所有工作。最后要做的是对顶点进行动画处理,然后绘制网格:
for( int i = 0; i < mSubMeshes_.size(); ++i ) {
for( int k = 0; k < mSubMeshes_[i]->mSubMeshes_.size(); ++k ) {
// first we animate it
GEAR::DAESubMesh* submesh = mSubMeshes_[i]->mSubMeshes_[k];
submesh->buffer->lock( true );
{
for( unsigned v = 0; v < submesh->buffer->getNumVertices(); ++v ) {
// get the array of joints, which influence the current vertex
DAEVertexInfo* vertexInfo = submesh->vertexInfo[v];
GEAR::Vec3 vertex; // do not init the vertex with any value!
float totalWeight = 0.0f;
for( int j = 0; j < vertexInfo->joints.size(); ++j ) {
totalWeight += vertexInfo->joints[j]->weight;
vertex += ((vertexInfo->joints[j]->joint->skinningMatrix*(vertexInfo->vertex))*vertexInfo->joints[j]->weight);
}
// since it isn\'t guaranteed that the total weight is exactly 1.0, we have no normalize it
// @todo this should be moved to the parser
if( totalWeight != 1.0f ) {
float normalizedWeight = 1.0f / totalWeight;
vertex *= normalizedWeight;
}
submesh->buffer->bufferVertexPos( v, vertex );
}
}
submesh->buffer->unlock();
mSubMeshes_[i]->mSubMeshes_[k]->buffer->draw( GEAR::TRIANGLES, 0, mSubMeshes_[i]->mSubMeshes_[k]->buffer->getNumVertices() );
}
}
总而言之,它几乎与我开始的代码相同。
但是现在对我而言,事情变得更加清晰了,我也可以开始支持没有找到相关结果
已邀请:
2 个回复
弦砂牧扁
朗排谎纷瘟