Questions about offset matrix in skinned mesh

Started by
7 comments, last by kgstation 10 years, 5 months ago

I'm working on a skinned mesh demo.

And I have successfully export my own mesh from 3ds max.

But in my meshviewer program,only first frame is correct.

Like this,the right image is the correct animation, the cube in left image means the bone

temp.png

I have got all bone matrix saved in mBoneTransforms[m][n].The m indicate m-th bone, the n means the n-th frame, each data in mBoneTransforms is a matrix

following is my code:

The drawCube(float4x4 world) method is used to drawing bones.And this completely represent the animation correctly.

So my bone animation is correctly, skinned animation is wrong


static int frameCount = 0;
for (int i = 0; i < mYHMMesh.boneInfo.BoneCount; i++)
{
	XMFLOAT4X4 boneInv = floatToXMFLOAT(mBoneTransforms[i][0]);
	XMStoreFloat4x4(&boneInv, XMMatrixInverse(NULL, XMLoadFloat4x4(&boneInv)));
	//boneInv is what I think offset matrix, is it wrong?
	m_constantBufferData.boneTransform[i] = mul(mBoneTransforms[i][frameCount % 120], XMFLOATTofloat4x4(boneInv));
	//drawBone(mBoneTransforms[i][frameCount % 120);
}
frameCount++;
//Draw mesh
//HLSL Code

PixelShaderInput SimpleVertexShader(VertexShaderInput input)
{
    PixelShaderInput vertexShaderOutput;

    float weights[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
    weights[0] = input.Weights.x;
    weights[1] = input.Weights.y;
    weights[2] = input.Weights.z;
    weights[3] = 1.0f - weights[0] - weights[1] - weights[2];
	
    float4 pos = float4(input.pos, 1.0f);
    float4 norm = float4(normalize(input.norm), 0.0f);
    for (int i = 0; i < 4; ++i)
    {
        pos += weights[i] * mul(pos, gBoneTransform[input.BoneIndices[i]]);
	norm += weights[i] * mul(norm, gBoneTransform[input.BoneIndices[i]]);
    }

    pos = mul(pos, model);
    pos = mul(pos, view);
    pos = mul(pos, projection);
    norm = mul(norm, model);

    vertexShaderOutput.pos = pos;
    vertexShaderOutput.tex = input.tex;
    vertexShaderOutput.norm = normalize(norm.xyz);
    return vertexShaderOutput;
}
Can anyone help me?
Any help would be appreciated.
Thank you!
My english is very poor!
Advertisement

Can you show us some images of what you are seeing? That might help to diagnose the problem...

What are you seeing on the incorrect frames? Have you tried to debug the issue in PIX/Graphics Debugger yet?

Can you show us some images of what you are seeing? That might help to diagnose the problem...

What are you seeing on the incorrect frames? Have you tried to debug the issue in PIX/Graphics Debugger yet?

Thanks for your reply, I have add compare image to my post.

My english is very poor!

How many bones per vertex are you working with? In your code you are declaring 5 weights (where the fifth one is the remaining weight left over from the others) but when you apply the weights you are only applying the first 4 weights (looping from 0 to less than 4). Is this correct?

If not, it could explain why the skeleton is correct but the vertices don't seem to be following them.

How many bones per vertex are you working with? In your code you are declaring 5 weights (where the fifth one is the remaining weight left over from the others) but when you apply the weights you are only applying the first 4 weights (looping from 0 to less than 4). Is this correct?

If not, it could explain why the skeleton is correct but the vertices don't seem to be following them.

My mistake, and now I have fixed the bug in hlsl code, but it seem not effect what I have seem.Is it could be possible that the bone number per vertex working with is not enough?

My english is very poor!


float4 pos = float4(input.pos, 1.0f);
float4 norm = float4(normalize(input.norm), 0.0f);
for (int i = 0; i < 4; ++i)
{
    pos += weights[i] * mul(pos, gBoneTransform[input.BoneIndices[i]]);
    norm += weights[i] * mul(norm, gBoneTransform[input.BoneIndices[i]]);
}

This doesn't look correct to me either - you are modifying the pos variable in the same line that it is being used to update the current position. Instead, you do something more like this:


float4 pos = float4(input.pos, 1.0f);
float4 skinnedPos = float4( 0.0f, 0.0f, 0.0f, 0.0f );
float4 norm = float4(normalize(input.norm), 0.0f);
for (int i = 0; i < 4; ++i)
{
    skinnedPos += weights[i] * mul(pos, gBoneTransform[input.BoneIndices[i]]);
    norm += weights[i] * mul(norm, gBoneTransform[input.BoneIndices[i]]);
}

skinnedPos.w = 1.0f;

The way you have it now, you would be starting with a different position as the input to each of the weighted transformed positions, which isn't correct. Instead you should retain the input position and accumulate the transformed positions into another variable.



float4 pos = float4(input.pos, 1.0f);
float4 norm = float4(normalize(input.norm), 0.0f);
for (int i = 0; i < 4; ++i)
{
    pos += weights[i] * mul(pos, gBoneTransform[input.BoneIndices[i]]);
    norm += weights[i] * mul(norm, gBoneTransform[input.BoneIndices[i]]);
}

This doesn't look correct to me either - you are modifying the pos variable in the same line that it is being used to update the current position. Instead, you do something more like this:


float4 pos = float4(input.pos, 1.0f);
float4 skinnedPos = float4( 0.0f, 0.0f, 0.0f, 0.0f );
float4 norm = float4(normalize(input.norm), 0.0f);
for (int i = 0; i < 4; ++i)
{
    skinnedPos += weights[i] * mul(pos, gBoneTransform[input.BoneIndices[i]]);
    norm += weights[i] * mul(norm, gBoneTransform[input.BoneIndices[i]]);
}

skinnedPos.w = 1.0f;

The way you have it now, you would be starting with a different position as the input to each of the weighted transformed positions, which isn't correct. Instead you should retain the input position and accumulate the transformed positions into another variable.

You are right,thank you very much.Now my skinned animation demo is almost done.

My english is very poor!

Sorry for bother you again, I met a problem when I add rotation matrix to my skin.

This is my file shows the translation,rotation and scale data:

Time:0 Trans:22.721792 0.512012 -0.000000 Qart:1.000000 -0.000000 -0.000000 1.570796 Scale:1.000000 1.000000 1.000000
Time:1 Trans:0.092348 23.141449 -0.000000 Qart:-0.577350 -0.577350 0.577350 4.188790 Scale:1.000000 1.000000 1.000000
Time:2 Trans:-22.537085 0.512007 -0.000000 Qart:0.000000 -0.707107 0.707107 3.141593 Scale:1.000000 1.000000 1.000000
Time:3 Trans:0.092356 -22.117428 -0.000000 Qart:-0.577350 0.577350 -0.577350 4.188790 Scale:1.000000 1.000000 1.000000
And it's a simple animation to a bone, this animation is a bone around z-axis rotate 90 degree, 180 degree, 270 degree and 360 degree
I read the data like this
mYHMMesh.boneInfo.yhmBone[m].frame[n] contains transform data above

for (int i = 0; i < mYHMMesh.boneInfo.BoneCount; i++)
{
     for (int j = 0; j < 120; j++)
     {
	  float4x4 cubeTrans = translation(mYHMMesh.boneInfo.yhmBone[i].frame[j].trans.x, mYHMMesh.boneInfo.yhmBone[i].frame[j].trans.y, mYHMMesh.boneInfo.yhmBone[i].frame[j].trans.z);
	  float4x4 cubeRot = rotationArbitrary(float3(mYHMMesh.boneInfo.yhmBone[i].frame[j].rot.x, mYHMMesh.boneInfo.yhmBone[i].frame[j].rot.y, mYHMMesh.boneInfo.yhmBone[i].frame[j].rot.z), mYHMMesh.boneInfo.yhmBone[i].frame[j].rot.w / 3.1415926 * 180);
	  float4x4 cubeScale = scale(mYHMMesh.boneInfo.yhmBone[i].frame[j].scale.x, mYHMMesh.boneInfo.yhmBone[i].frame[j].scale.y, mYHMMesh.boneInfo.yhmBone[i].frame[j].scale.z);
	  mBoneTransforms[i][j] = mul(mul(cubeScale, cubeRot), cubeTrans);
          //mBoneTransforms[i][j] = mul(cubeScale, cubeTrans);
          //If I use only scale and trans data, I would see the animation is on the right way except for rotation animation
     }
     //Here I calculate the offset matrix
     XMFLOAT4X4 tempOffset = floatToXMFLOAT(mBoneTransforms[i][0]);
     XMStoreFloat4x4(&tempOffset, XMMatrixInverse(NULL, XMLoadFloat4x4(&tempOffset)));
     mBoneOffset[i] = XMFLOATTofloat4x4(tempOffset);
}

and then I draw it with the matrix mul(mBoneOffset, mBoneTransform[frameCount % 120])

Is there any thing wrong?The some vertex didn't follow the bone.

What I saw is this

3tb_131116161317ac1w512293.png

My english is very poor!

All problem solved!Problem is my mul function...

My english is very poor!

This topic is closed to new replies.

Advertisement