• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
DgekGD

GPU Skinning problem (MD5 Doom)

2 posts in this topic

Hello!

I'm trying to implement GPU skinning using md5 format. But after many hours of trying to figure out what's wrong I still get incorrect results for animation.

Firstly, I calculate Bind Pose and Inverse Bind Pose matrices for each bone:
[source lang="cpp"]Mat4x4 trans; trans.BuildTranslation(joints[i].posObjectSpace); //build translation from joint position in object space
Mat4x4 rot = joints[i].rot.BuildMatrix(); //joints[i].rot is unit quaternion
Mat4x4 bindPoseTransform = rot *trans;
(**ppBones)[i].bindPoseTransform = rot * trans;
(**ppBones)[i].inverseBindPoseTransform = bindPoseTransforms.Inverse();[/source]

After that I calculate vertex positions in object space and fill vertex buffer with positions, indices and weights
[source lang="cpp"]
for (int j = 0; j < mesh.numVertices; j++)
{
//calculate position
Vec3 pos(0.0f, 0.0f, 0.0f);
md5_vertex vertex = mesh.vertices[j];
for (int k = 0; k < vertex.numWeights; k++)
{
md5_weight weight = mesh.weights[vertex.startWeight+k];
md5_joint joint = joints[weight.jointId];
//calculate vertex position in object space

temp = Vec4(weight.posJointSpace); temp.w = 0.0f;
Quaternion q = joint.rot;
Quaternion revq = joint.rot.Inverse();

Vec3 temp3 = q * temp * revq;
temp3 = temp3 + joint.posObjectSpace;
temp3 *= weight.weight;
pos += temp3;// * weight.weight;
}
//finally fill vertex structure with data
v[j].pos = pos;
v[j].texCoord = vertex.texCoords;
for (int k = 0; k < vertex.numWeights; k++)
{
v[j].bonesIndices[k] = mesh.weights[vertex.startWeight+k].jointId;
}

v[j].bonesWeights = Vec4(
vertex.numWeights > 0 ? mesh.weights[vertex.startWeight].weight : 0.0f,
vertex.numWeights > 1 ? mesh.weights[vertex.startWeight+1].weight : 0.0f,
vertex.numWeights > 2 ? mesh.weights[vertex.startWeight+2].weight : 0.0f,
vertex.numWeights > 3 ? mesh.weights[vertex.startWeight+3].weight : 0.0f);
assert( fabs(v[j].bonesWeights.x + v[j].bonesWeights.y + v[j].bonesWeights.z + v[j].bonesWeights.w - 1.0f) < 0.001f);
[/source]
Next step is to calculate joints transforms in object space for each frame in animation:
[source lang="cpp"]//pos - current joint position relative to the parent (if have any)
//rot - quaternion representng rotation of current joint
//if we have parent joint...
if (joint.id >= 0)
{
md5_joint parent = joints[joint.id];

//calculate joint pos
Quaternion parentOrient = parent.rot;
Vec4 temp( pos.x, pos.y, pos.z, 0.0f);

pos = parentOrient * temp * parentOrient.Inverse();
pos = pos + parent.posObjectSpace;

//concatenate rotation
rot = parentOrient * rot';
rot.Normalize();
}

//update joint position and orientation
joints[j].posObjectSpace = pos;
joints[j].rot = rot;

Mat4x4 trans;
trans.BuildTranslation(pos);
Mat4x4 rotation = rot.BuildMatrix();
Mat4x4 transform = rotation * trans;

newFrame.m_positions.push_back(pos);
newFrame.m_rotations.push_back(rot);
newFrame.m_transforms.push_back(transform); //use just fixed frames for debugging, no interpolation for now...
[/source]
//Finally I render animation!
[source lang="cpp"]
SkinningBuffer skinningBuffer;

for (int j = 0; j < m_numBones; j++)
{
Mat4x4 world = pScene->GetWorld(); //world transform. identity for now
Mat4x4 inverseBindPose = m_pBones[j].m_inverseBindPose; //inverse bind pose matrix

Mat4x4 curObjectSpace = m_pAnimations[0].GetTransform(j); //current object space transform for given bone
skinningBuffer.bonesTransforms[j] = Transpose(inverseBindPose * curObjectSpace * world);


}
//next frame...
m_pAnimations[0].NextFrame();
//update constant buffer for shader
m_pSkinningBuffer->UpdateAndSet(&amp;amp;skinningBuffer, ST_Vertex, 3);
[/source]

Here is my vertex shader code:
[source lang="cpp"]////////////////////////////////////////////////
//Constant Buffers
////////////////////////////////////////////////
cbuffer MatrixBuffer : register(b0)
{
float4x4 m_ViewProjection;
}

cbuffer LightBuffer : register(b1)
{
float4 ambientColor : packoffset(c0);
float4 diffuseColor : packoffset(c1);
float3 lightDir : packoffset(c2.x);
float padb1 : packoffset(c2.w);
};

cbuffer CameraBuffer : register(b2)
{
float3 eyePos : packoffset(c0.x);
float padb2 : packoffset(c0.w);
};

cbuffer SkinningBuffer : register(b3)
{
float4x4 m_BoneWorld[50];
};

///////////////////////////////////////////////
//Texture Resources and Sampler States
///////////////////////////////////////////////
Texture2D objTexture : register(t0);
Texture2D bumpMap : register(t1);
Texture2D heightMap : register(t2);

SamplerState LinearSampler : register(s0);

///////////////////////////////////////////////
//Shaders' input/output layouts
///////////////////////////////////////////////
struct vs_input
{
float3 pos : POSITION;
uint4 bones : BONEIDS;
float4 weights : BONEWEIGHTS;
float2 texCoord : TEXCOORD;
float3 normal : NORMAL;
float3 tangent : TANGENT;
float3 binormal : BINORMAL;
};

struct ps_input
{
float4 pos : SV_POSITION;
float2 texCoord : TEXCOORD;
float4 color : COLOR;
float3 normal : NORMAL;
float3 tangent : TANGENT;
float3 binormal : BINORMAL;
};

//////////////////////////////////////////////
//Vertex Shader
//////////////////////////////////////////////
ps_input VS(vs_input input)
{
ps_input output;

//apply bone transforms to position
output.pos = mul(float4(input.pos, 1.0f), m_BoneWorld[input.bones.x] * input.weights.x);
output.pos += mul(float4(input.pos, 1.0f), m_BoneWorld[input.bones.y] * input.weights.y);
output.pos += mul(float4(input.pos, 1.0f), m_BoneWorld[input.bones.z] * input.weights.z);
output.pos += mul(float4(input.pos, 1.0f), m_BoneWorld[input.bones.w] * input.weights.w);

output.pos = mul(output.pos, m_ViewProjection);

//apply bone transforms to normal
output.normal = mul(float4(input.normal, 1.0f), m_BoneWorld[input.bones.x]) * input.weights.x;
output.normal += mul(float4(input.normal, 1.0f), m_BoneWorld[input.bones.y]) * input.weights.y;
output.normal += mul(float4(input.normal, 1.0f), m_BoneWorld[input.bones.z]) * input.weights.z;
output.normal += mul(float4(input.normal, 1.0f), m_BoneWorld[input.bones.w]) * input.weights.w;

//apply bone transforms to tangent
output.tangent = mul(float4(input.tangent, 1.0f), m_BoneWorld[input.bones.x]) * input.weights.x;
output.tangent += mul(float4(input.tangent, 1.0f), m_BoneWorld[input.bones.y]) * input.weights.y;
output.tangent += mul(float4(input.tangent, 1.0f), m_BoneWorld[input.bones.z]) * input.weights.z;
output.tangent += mul(float4(input.tangent, 1.0f), m_BoneWorld[input.bones.w]) * input.weights.w;

//apply bone transforms to normal
output.binormal = mul(float4(input.binormal, 1.0f), m_BoneWorld[input.bones.x]) * input.weights.x;
output.binormal += mul(float4(input.binormal, 1.0f), m_BoneWorld[input.bones.y]) * input.weights.y;
output.binormal += mul(float4(input.binormal, 1.0f), m_BoneWorld[input.bones.z]) * input.weights.z;
output.binormal += mul(float4(input.binormal, 1.0f), m_BoneWorld[input.bones.w]) * input.weights.w;

//transfer texture coordinates
output.texCoord = input.texCoord;

//output result
return output;
}[/source]
I'm pretty sure, that vertex object space positions are calculated correctly and shaders code works ok as I get correct results for Bind Pose. But when animation starts it doesn't work:
[img]http://cs301102.userapi.com/v301102066/cec0/4O_4Cv2cnBQ.jpg[/img]

[img]http://cs301102.userapi.com/v301102066/cec9/TH5f7gf2clY.jpg[/img]

The model is taken from http://www.katsbits.com/download/models/ .

To simplify debugging, I created simple model from 2 joints and 4 vertices with two frames of animation and still it doesn't work, but it seems that i checked everything...
[img]http://cs301102.userapi.com/v301102066/ced7/pYoSqfVWw84.jpg[/img]

This is capture of the second frame(of two). The first frame is rendering correctly showing this black plane lies straight on the floor. In the second frame I just have left part lifted up a bit. Obvisously, I renders incorrectly for me. I attached file with this plane to my post.

Could you give me some ideas why I get wrong result? If you already have implemented GPU skinning could you type your matrices for inverseBindPose of each bone and curObjectTransform of bones for each frame, so that i can compare them with mine(for that black plane model)? I would really appreciate any help!

Thanks! Edited by DgekGD
0

Share this post


Link to post
Share on other sites
As I've implemented MD5 skinning on GPU, I can give you few tips (I don't have time right now to move through your code as I'm being quite in time-pressure till thursday)...

1.) Start from simple MD5 file, that contains only translation and single bone. If it works, create only rotation, then combine them together, then build double-bone - e.g. trace where the problems lie
2.) Double check your math. I was sitting for few hours on problem with inverse matrices, then I found that VS actually *removes* few instructions from my SSE2 matrix inversion code (... forgot volatiles *huh* .. problem hadn't appeared on my main dev platform - linux & gcc)
3.) And triple check your quaternion <-> matrices. Often the problem lies only in rotations, and doing row-major matrix out of quaternion instead of column-major matrix is totally wrong (because of nature of 3x3 rotational matrix, you will actually get inverse rotation, and this can create mess you see).

And last point - try your skinning (e.g. inverse-bind-pose and matrix transformation) on CPU first, it's a lot easier to debug there, than on GPU. [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]

If you'll still have troubles implement it, I can post fragments of my code (with little how-to) here, although not till Thursday evening.
1

Share this post


Link to post
Share on other sites
Thanks for your reply, [b]Vilem Otte[/b]!

[quote]3.) And triple check your quaternion <-> matrices. Often the problem lies only in rotations, and doing row-major matrix out of quaternion instead of column-major matrix is totally wrong (because of nature of 3x3 rotational matrix, you will actually get inverse rotation, and this can create mess you see).[/quote]

This is where the problem was coming from! So, i changed my code to transpose rotation matrices after they were built from quaternios and it works perfectly!
[source lang="cpp"]Mat4x4 rotation = rot.BuildMatrix();[/source]
to:
[source lang="cpp"]Mat4x4 rotation = rot.BuildMatrix();
rotation.Transpose();[/source]
Thanks again for your help, you saved me from many hours of trying to solve this problem!
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0