# Use assimp for skeletal animation HELP!

## Recommended Posts

Guys, I've spent a lot of time to load skeletal animation

but  It is very difficult...

but I didn't get the results I wanted

This is my codes

void LoadAnimation::BoneTransform(float time, vector<XMFLOAT4X4>& transforms)
{
XMMATRIX Identity = XMMatrixIdentity();

float TicksPerSecond = (float)(m_pScene->mAnimations[0]->mTicksPerSecond != 0 ?
m_pScene->mAnimations[0]->mTicksPerSecond : 25.0f);
float TimeInTicks = time*TicksPerSecond;
float AnimationTime = fmod(TimeInTicks, (float)m_pScene->mAnimations[0]->mDuration);

transforms.resize(m_NumBones);

for (int i = 0; i < m_NumBones; ++i) {
XMStoreFloat4x4(&transforms[i], m_Bones[i].second.FinalTransformation);
}
}
void LoadAnimation::ReadNodeHeirarchy(float AnimationTime, const aiNode * pNode, const XMMATRIX& ParentTransform)
{
string NodeName(pNode->mName.data);

const aiAnimation* pAnim = m_pScene->mAnimations[0];

XMMATRIX NodeTransformation = XMMATRIX(&pNode->mTransformation.a1);

const aiNodeAnim* pNodeAnim = FindNodeAnim(pAnim, NodeName);

if (pNodeAnim) {
aiVector3D scaling;
CalcInterpolatedScaling(scaling, AnimationTime, pNodeAnim);
XMMATRIX ScalingM = XMMatrixScaling(scaling.x, scaling.y, scaling.z);
ScalingM = XMMatrixTranspose(ScalingM);

aiQuaternion q;
CalcInterpolatedRotation(q, AnimationTime, pNodeAnim);
XMMATRIX RotationM = XMMatrixRotationQuaternion(XMVectorSet(q.x, q.y, q.z, q.w));
RotationM = XMMatrixTranspose(RotationM);

aiVector3D t;
CalcInterpolatedPosition(t, AnimationTime, pNodeAnim);
XMMATRIX TranslationM = XMMatrixTranslation(t.x, t.y, t.z);
TranslationM = XMMatrixTranspose(TranslationM);

NodeTransformation = TranslationM * RotationM * ScalingM;
}

XMMATRIX GlobalTransformation = ParentTransform * NodeTransformation;

int tmp = 0;
for (auto& p : m_Bones) {
if (p.first == NodeName) {
p.second.FinalTransformation = XMMatrixTranspose(
m_GlobalInverse *  GlobalTransformation * p.second.BoneOffset);
break;
}
tmp += 1;
}

for (UINT i = 0; i < pNode->mNumChildren; ++i) {
}
}

CalcInterp~ function and Find~ function are like a tutorial
(http://ogldev.atspace.co.uk/www/tutorial38/tutorial38.html)

I think that I'm doing the multiplication wrong

but I don't know where it went wrong

If you want, i wall post other codes.

here is my result

(hands are stretched, legs are strange)

and it is ideal result

##### Share on other sites

Implementing skeletal animation can be a hard task the first time around. For easier debugging I suggest you do a very simple animation for a very simple model. Create a test scene with a cylinder animate by two bones or something like this. Also drawing the bone matrices as lines is very helpful.

##### Share on other sites

Agree with turanszkij, start with really simple cases. In addition, it can also be really helpful to 'go by the numbers', create the simple joint, maybe just with a few verts to be skinned, then output all the translates, quaternions for each bone and the transformed vert positions.

If you can also export the baked animation from blender you can know the 'ground truth' positions that the vertices should be in, if everything is going perfect. Watch for the order of your transforms, and also differences between 'polarities' of things that blender is spitting out compared to your code. Sometimes flipping an axis or similar can fix things. I spent ages debugging where blender had output quaternions as wxyz and I was expecting xyzw or vice versa lol!

##### Share on other sites

I was able to get assimp animation to work in DirectX. I see you followed an opengl tutorial but in DX matrix multiplication order is reverse of opengl order. In DX if you want to first rotate a vertex by XMMATRIX R1 and then translate by XMMATRIX T1 you do

XMVector3Transform(v, R1 * T1)

p.second.FinalTransformation = p.second.BoneOffset * GlobalTransformation * m_GlobalInverse;

Also make sure you read correctly from aiMatrix4x4 row-major data structure into XMFLOAT4X4 also row-major before loading into XMMATRIX.

##### Share on other sites
On 2018. 1. 28. at 11:02 AM, Ivan Terziev said:

I was able to get assimp animation to work in DirectX. I see you followed an opengl tutorial but in DX matrix multiplication order is reverse of opengl order. In DX if you want to first rotate a vertex by XMMATRIX R1 and then translate by XMMATRIX T1 you do


XMVector3Transform(v, R1 * T1)


p.second.FinalTransformation = p.second.BoneOffset * GlobalTransformation * m_GlobalInverse;

Also make sure you read correctly from aiMatrix4x4 row-major data structure into XMFLOAT4X4 also row-major before loading into XMMATRIX.

I have some questions for you.

you told me to count 'BoneOffset * GlobalTransformation * m_blobalInverse'

but if I calculate that equation, the model is completyely broken

And I use this function to load XMMATRIX from aiMatirx4x4

XMMATRIX(&pNode->mTransformation.a1);

'pNode->mTransformation' is type aiMatrix4x4

I think it is no ploblem

because XMMATRIX can put the argument values of the array

please, I want you to tell me what method you used

thank you

##### Share on other sites
On 2018. 1. 26. at 12:20 AM, turanszkij said:

Implementing skeletal animation can be a hard task the first time around. For easier debugging I suggest you do a very simple animation for a very simple model. Create a test scene with a cylinder animate by two bones or something like this. Also drawing the bone matrices as lines is very helpful.

I'll take your idea and try to test it wiht a simple model

##### Share on other sites
On 2018. 1. 26. at 1:36 AM, lawnjelly said:

Agree with turanszkij, start with really simple cases. In addition, it can also be really helpful to 'go by the numbers', create the simple joint, maybe just with a few verts to be skinned, then output all the translates, quaternions for each bone and the transformed vert positions.

If you can also export the baked animation from blender you can know the 'ground truth' positions that the vertices should be in, if everything is going perfect. Watch for the order of your transforms, and also differences between 'polarities' of things that blender is spitting out compared to your code. Sometimes flipping an axis or similar can fix things. I spent ages debugging where blender had output quaternions as wxyz and I was expecting xyzw or vice versa lol!

I don't know when quaternion is xyzw or wxyz

Many articles just tell them to be careful

It is very difficult...

##### Share on other sites

Generally there are three types of transformations scaling, rotation and translation and you should be consistent with the order of multiplication. Usually it is scaling, rotation and translation, which is expressed in DX math as multiplication order S * R * T. So your NodeTransformation should be

NodeTransformation = ScalingM * RotationM * TranslationM;

and you should not be transposing your matrices when your read them from assimp (depending on your hlsl setting you should transpose your world, view and projection matrices). Then calculating final transformation remains

p.second.FinalTransformation = p.second.BoneOffset * GlobalTransformation * m_GlobalInverse;

The above equations says the final transformation of a vertex to world space is: transform the vertex to bone space, then transform the vertex to scene space (assuming assimp bone transformations are from bone space to scene space) and finally transform it to world space.

It will be simpler if you test with assimp sceneTransform = identity and without scaling and then add them after the bone hierarchy looks good.

I hope it works!

##### Share on other sites
18 hours ago, Ivan Terziev said:

Generally there are three types of transformations scaling, rotation and translation and you should be consistent with the order of multiplication. Usually it is scaling, rotation and translation, which is expressed in DX math as multiplication order S * R * T. So your NodeTransformation should be


NodeTransformation = ScalingM * RotationM * TranslationM;

and you should not be transposing your matrices when your read them from assimp (depending on your hlsl setting you should transpose your world, view and projection matrices). Then calculating final transformation remains


p.second.FinalTransformation = p.second.BoneOffset * GlobalTransformation * m_GlobalInverse;

The above equations says the final transformation of a vertex to world space is: transform the vertex to bone space, then transform the vertex to scene space (assuming assimp bone transformations are from bone space to scene space) and finally transform it to world space.

It will be simpler if you test with assimp sceneTransform = identity and without scaling and then add them after the bone hierarchy looks good.

I hope it works!

I have some questions

First you say that I shouldn't be transposing matrices when read them from assimp,

but I think that assimp is opengl format

so would i have to do transpose to get the correct col/row ?

Actually, I tried removing transpos function when using assimp to read my model,

but the result is very bad (It was completely disassembled)

second I agree that you told me to do S * R * T

because I saw it in my book when i was studying

but strangely the result of S * R * T and T * R * S were same, so i didn't fix it

hmmm..... assimp is very useful library to load fbx format

but load anmation, it's very hardly...

I don't have enough English skills to read it

so you would be inconvenient to read it

##### Share on other sites

Yeah I understand, the problem with linear transformations is that you have to get both the equations and the data right and it is really hard to debug using the standard debugger and on top of this there are the opengl and directx differences. So in my case I only transpose when I read from aiMatrix4x4. I don't transpose when I read quaternions or vectors.

const aiMatrix4x4& offset = bone.mOffsetMatrix;
XMMATRIX meshToBoneTransform = XMMatrixTranspose(
XMMATRIX(offset.a1, offset.a2, offset.a3, offset.a4,
offset.b1, offset.b2, offset.b3, offset.b4,
offset.c1, offset.c2, offset.c3, offset.c4,
offset.d1, offset.d2, offset.d3, offset.d4));

Do the same for (transpose) XMMATRIX(&pNode->mTransformation.a1);

If you are sending FinalTransformation matrices to hlsl for skinning on the GPU you may have to transpose them depending on how you calculate your final vertex world position.

## Create an account

Register a new account

• 10
• 15
• 14
• 18
• 15
• ### Similar Content

• By chiffre
Introduction:
In general my questions pertain to the differences between floating- and fixed-point data. Additionally I would like to understand when it can be advantageous to prefer fixed-point representation over floating-point representation in the context of vertex data and how the hardware deals with the different data-types. I believe I should be able to reduce the amount of data (bytes) necessary per vertex by choosing the most opportune representations for my vertex attributes. Thanks ahead of time if you, the reader, are considering the effort of reading this and helping me.
I found an old topic that shows this is possible in principal, but I am not sure I understand what the pitfalls are when using fixed-point representation and whether there are any hardware-based performance advantages/disadvantages.
(TLDR at bottom)
The Actual Post:
To my understanding HLSL/D3D11 offers not just the traditional floating point model in half-,single-, and double-precision, but also the fixed-point model in form of signed/unsigned normalized integers in 8-,10-,16-,24-, and 32-bit variants. Both models offer a finite sequence of "grid-points". The obvious difference between the two models is that the fixed-point model offers a constant spacing between values in the normalized range of [0,1] or [-1,1], while the floating point model allows for smaller "deltas" as you get closer to 0, and larger "deltas" the further you are away from 0.
To add some context, let me define a struct as an example:
struct VertexData { float[3] position; //3x32-bits float[2] texCoord; //2x32-bits float[3] normals; //3x32-bits } //Total of 32 bytes Every vertex gets a position, a coordinate on my texture, and a normal to do some light calculations. In this case we have 8x32=256bits per vertex. Since the texture coordinates lie in the interval [0,1] and the normal vector components are in the interval [-1,1] it would seem useful to use normalized representation as suggested in the topic linked at the top of the post. The texture coordinates might as well be represented in a fixed-point model, because it seems most useful to be able to sample the texture in a uniform manner, as the pixels don't get any "denser" as we get closer to 0. In other words the "delta" does not need to become any smaller as the texture coordinates approach (0,0). A similar argument can be made for the normal-vector, as a normal vector should be normalized anyway, and we want as many points as possible on the sphere around (0,0,0) with a radius of 1, and we don't care about precision around the origin. Even if we have large textures such as 4k by 4k (or the maximum allowed by D3D11, 16k by 16k) we only need as many grid-points on one axis, as there are pixels on one axis. An unsigned normalized 14 bit integer would be ideal, but because it is both unsupported and impractical, we will stick to an unsigned normalized 16 bit integer. The same type should take care of the normal vector coordinates, and might even be a bit overkill.
struct VertexData { float[3] position; //3x32-bits uint16_t[2] texCoord; //2x16bits uint16_t[3] normals; //3x16bits } //Total of 22 bytes Seems like a good start, and we might even be able to take it further, but before we pursue that path, here is my first question: can the GPU even work with the data in this format, or is all I have accomplished minimizing CPU-side RAM usage? Does the GPU have to convert the texture coordinates back to a floating-point model when I hand them over to the sampler in my pixel shader? I have looked up the data types for HLSL and I am not sure I even comprehend how to declare the vertex input type in HLSL. Would the following work?
struct VertexInputType { float3 pos; //this one is obvious unorm half2 tex; //half corresponds to a 16-bit float, so I assume this is wrong, but this the only 16-bit type I found on the linked MSDN site snorm half3 normal; //same as above } I assume this is possible somehow, as I have found input element formats such as: DXGI_FORMAT_R16G16B16A16_SNORM and DXGI_FORMAT_R16G16B16A16_UNORM (also available with a different number of components, as well as different component lengths). I might have to avoid 3-component vectors because there is no 3-component 16-bit input element format, but that is the least of my worries. The next question would be: what happens with my normals if I try to do lighting calculations with them in such a normalized-fixed-point format? Is there no issue as long as I take care not to mix floating- and fixed-point data? Or would that work as well? In general this gives rise to the question: how does the GPU handle fixed-point arithmetic? Is it the same as integer-arithmetic, and/or is it faster/slower than floating-point arithmetic?
Assuming that we still have a valid and useful VertexData format, how far could I take this while remaining on the sensible side of what could be called optimization? Theoretically I could use the an input element format such as DXGI_FORMAT_R10G10B10A2_UNORM to pack my normal coordinates into a 10-bit fixed-point format, and my verticies (in object space) might even be representable in a 16-bit unsigned normalized fixed-point format. That way I could end up with something like the following struct:
struct VertexData { uint16_t[3] pos; //3x16bits uint16_t[2] texCoord; //2x16bits uint32_t packedNormals; //10+10+10+2bits } //Total of 14 bytes Could I use a vertex structure like this without too much performance-loss on the GPU-side? If the GPU has to execute some sort of unpacking algorithm in the background I might as well let it be. In the end I have a functioning deferred renderer, but I would like to reduce the memory footprint of the huge amount of vertecies involved in rendering my landscape.
TLDR: I have a lot of vertices that I need to render and I want to reduce the RAM-usage without introducing crazy compression/decompression algorithms to the CPU or GPU. I am hoping to find a solution by involving fixed-point data-types, but I am not exactly sure how how that would work.
• By Paszq
Group photo of some of the characters and creatures currently living in Arpago

• Well i found out Here what's the problem and how to solve it (Something about world coordinates and object coordinates) but i can't understand how ti works. Can you show me some examples in code on how you implement this???

Scaling Matrix:
m_Impl->scale = glm::mat4(1.0f); m_Impl->scale = glm::scale(m_Impl->scale, glm::vec3(width, height, 0)); Verticies:
//Verticies. float verticies[] = { //Positions. //Texture Coordinates. 1.0f, 1.0f, 0.0f, 0.0f, 2.0f, 1.0f, 1.0f, 0.0f, 2.0f, 2.0f, 1.0f, 1.0f, 1.0f, 2.0f, 0.0f, 1.0f }; Rendering:
//Projection Matrix. glm::mat4 proj = glm::ortho(0.0f, (float)window->GetWidth(), 0.0f, (float)window->GetHeight(), -1.0f, 1.0f); //Set the uniform. material->program->setUniformMat4f("u_MVP", proj * model); //model is the scale matrix from the previous code. //Draw. glDrawElements(GL_TRIANGLES, material->ibo->GetCount(), GL_UNSIGNED_INT, NULL);
#shader vertex #version 330 core layout(location = 0) in vec4 aPos; layout(location = 1) in vec2 aTexCoord; out vec2 texCoord; uniform mat4 u_MVP; void main() { gl_Position = u_MVP*aPos; texCoord = aTexCoord; } #shader fragment #version 330 core out vec4 colors; in vec2 texCoord; uniform sampler2D u_Texture; void main() { colors = texture(u_Texture, texCoord); }
Before Scaling (It's down there on the bottom left corner as a dot).

After Scaling

Problem: Why does the position also changes?? If you see my Verticies, the first position starts at 1.0f, 1.0f , so when i'm scaling it should stay at that position

• Hey guys!

Ok so I have been developing some ideas to get to work on and I have one specifically that I need some assistance with. The App will be called “A Walk On the Beach.” It’s somewhat of a 3D representation of the Apple app “Calm.” The idea is that you can take a virtual stroll up and down a pier on the beach. Building the level of a pier seems self explanatory to me... but my question is this.... How could I make it so that players can leave notes on the pier for other users to read and or respond to? I was thinking something like a virtual “peg board” at the end of the pier where players can “pin up” pictures or post it’s.

• Hi.
I wanted to experiment D3D12 development and decided to run some tutorials: Microsoft DirectX-Graphics-Samples, Braynzar Soft, 3dgep...Whatever sample I run, I've got the same crash.
All the initialization process is going well, no error, return codes ok, but as soon as the Present method is invoked on the swap chain, I'm encountering a crash with the following call stack:
The crash is an access violation to a null pointer ( with an offset of 0x80 )
I'm working on a notebook, a toshiba Qosmio x870 with two gpu's: an integrated Intel HD 4000 and a dedicated NVIDIA GTX 670M ( Fermi based ). The HD 4000 is DX11 only and as far as I understand the GTX 670M is DX12 with a feature level 11_0.
I checked that the good adapter was chosen by the sample, and when the D3D12 device is asked in the sample with a 11_0 FL, it is created with no problem. Same for all the required interfaces ( swap chain, command queue...).
I tried a lot of things to solve the problem or get some info, like forcing the notebook to always use the NVIDIA gpu, disabling the debug layer, asking for a different feature level ( by the way 11_0 is the only one that allows me to create the device, any other FL will fail at device creation )...
I have the latest NVIDIA drivers ( 391.35 ), the latest Windows 10 sdk ( 10.0.17134.0 ) and I'm working under
Visual Studio 2017 Community.
Thanks to anybody who can help me find the problem...