View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# Questions about offset matrix in skinned mesh

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

8 replies to this topic

### #1kgs  Members

Posted 12 November 2013 - 09:34 AM

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

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]);
//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)
{

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);

}

Can anyone help me?
Any help would be appreciated.
Thank you!

Edited by kgs, 12 November 2013 - 09:49 PM.

My english is very poor!

### #2Jason Z  Members

Posted 12 November 2013 - 10:22 AM

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?

Jason Zink :: DirectX MVP

Direct3D 11 engine on CodePlex: Hieroglyph 3

Games: Lunar Rift

### #3kgs  Members

Posted 12 November 2013 - 08:07 PM

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?

My english is very poor!

### #4Jason Z  Members

Posted 12 November 2013 - 09:02 PM

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.

Jason Zink :: DirectX MVP

Direct3D 11 engine on CodePlex: Hieroglyph 3

Games: Lunar Rift

### #5kgs  Members

Posted 12 November 2013 - 09:11 PM

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!

### #6Jason Z  Members

Posted 13 November 2013 - 08:31 PM

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.

Edited by Jason Z, 13 November 2013 - 08:33 PM.

Jason Zink :: DirectX MVP

Direct3D 11 engine on CodePlex: Hieroglyph 3

Games: Lunar Rift

### #7kgs  Members

Posted 15 November 2013 - 07:31 AM

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!

### #8kgs  Members

Posted 16 November 2013 - 02:14 AM

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]);
mBoneOffset[i] = XMFLOATTofloat4x4(tempOffset);
}


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

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

What I saw is this

Edited by kgs, 18 November 2013 - 03:25 AM.

My english is very poor!

### #9kgs  Members

Posted 18 November 2013 - 10:59 PM

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

My english is very poor!

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.