Tweening Questions

Started by
6 comments, last by markypooch 9 years, 10 months ago

Hello all!

I started to look into tweening since it looked like a fun and sort of quick way to get some basic animations going on my meshes.

Well, due to the sake of simplicity I decided to implement it on the CPU side first with dynamic buffers. While performance is pitifull, thats to be expected when pushing data back and forth from the CPU to the GPU.

However, I've run into a issue that makes me ask

The OBJ format. I've heard that the blender exporter (correct me if im wrong) will rearrange the vertices from export to export. thus negating any tweening attempts. Since I most comfortable with the OBJ format is there a way around it (I see in the mesh export options in blender, keep vertex order) or is this just wishful thinking. If you think OBJ is a no go. What format do you recommend?

Also, my implementation is based on Advanced Animation with DirectX, i just stuffed all the values into XNA types.

I'm including my tweening implementation just in case I missed something silly that someone could hint to. (And also as a validation that this isn't completely off XD) And also because my meshes when tweening wig out and start rendering crazy polygonal shapes and colors! Also as if I'm referencing out-of bounds garbage memory and passing it to D3D


float time = 0.0f;
float length = 5.0f;
 
if (isMoving)
 {
        Time += 5.0f * dt;
        if (Time > 5.0f) Time = 0.0f;
 }
//Note: vertices[x].at(x) represents a vector of pointers which hold all the info of each subset of the overall mesh

scalar = Time / length;

D3D11_MAPPED_SUBRESOURCE ms;
                for (int i = 0; i < mCount; i++) //mCount = subsetMeshCount
                {
                    verticesResult.resize(verticeCount[i]);
                    for (int j = 0; j < verticeCount[i]; j++)
                    {
                        //Interpolate Positions
                        /////////////////////////////////////////////
                        XMVECTOR vecSource = XMLoadFloat3(&vertices[i].at(j).pos);
                        float value = (1.0f - scalar);
                        vecSource *= value;

                        XMVECTOR vecTarget = XMLoadFloat3(&vertices2[i].at(j).pos);
                        vecTarget *= scalar;

                        XMStoreFloat3(&verticesResult.at(j).pos, (vecSource + vecTarget));

                        //Iterpolate Normals
                        //////////////////////////////////////////////
                        XMVECTOR vecSourceNorm = XMLoadFloat3(&vertices[i].at(j).norm);
                        value = (1.0f - scalar);
                        vecSourceNorm *= value;

                        XMVECTOR vecTargetNorm = XMLoadFloat3(&vertices2[i].at(j).norm);
                        vecTargetNorm *= scalar;
                        XMStoreFloat3(&verticesResult.at(j).norm, (vecSourceNorm + vecTargetNorm));

                        verticesResult.at(j).norm               = vertices[i].at(j).norm;

                        //Pass along Materials
                        ///////////////////////////////////////////////
                        verticesResult.at(j).color                = vertices[i].at(j).color;
                    }

                    //Lock the dynamic buffer (block the GPU)
                    ///////////////////////////////////////////////////
                    bro->devcon->Map(vertexBuffer[i], NULL, D3D11_MAP_WRITE_DISCARD, NULL, &ms);

                    //Grab a pointer to the data, pass along update vertice info, and remap the buffer
                    ////////////////////////////////////////////////////////////////////////////////////
                    VertexM *v = reinterpret_cast<VertexM*>(ms.pData);
                    *v          = verticesResult[0];
                    bro->devcon->Unmap(vertexBuffer[i], NULL);
                }

Any replies are appreciated smile.png

-Marcus

Advertisement
What format do you recommend?

If you fell yourself comfortable with all the things that obj file can introduce. Try using Assimp/FBX SDK/wateva to load for example a *.fbx scene files. Those file formats can store huge amount of different types of gemoetry/animations/ect., but it will be fine if you slowly start form those basic things like static meshes/the scene graph/trnasforms, basic node animation and so on. Later on you will probably want your own converter form *fbx/dae/wateva to your own custom format, but those are long and abstract talks.

If you are using Visual Studio 2012/2013 it natively supports *.fbx, compiling them into a *.cmo file which can then be read using the DirectXTK library.

The animations that are saved within the FBX are then available to you. Just need to apply the transforms correctly based on the current time etc...

Although it is in C#, my book linked below has a chapter dedicated to implementing animation with Direct3D (Chapter 4 - Animating Meshes with Vertex Skinning). Using an FBX -> CMO, it shows how to perform interpolation between frames using quaternions and spherical interpolation (Slerp).

The basic approach is:


// animation loaded from FBX/CMO file
foreach (keyFrame in animation)
{
    if (keyFrame.Time <= time) {
        // use this keyFrame transform for the appropriate bone index
        transforms[keyFrame.BoneIndex] = keyFrame.Transform;
    }
    else
    {
        // perform interpolation towards next future keyFrame
    }
}

// Apply bone transforms (i.e. apply transform of parent upon each child bone)

// Convert from bind pose into bone space using inverse of bind pose

Some screenshots showing incorrect and correct bone transforms:

[sharedmedia=gallery:images:5182]
[sharedmedia=gallery:images:5181]

Justin Stenning | Blog | Book - Direct3D Rendering Cookbook (using C# and SharpDX)

Projects: Direct3D Hook, EasyHook, Shared Memory (IPC), SharpDisasm (x86/64 disassembler in C#)

@spazzarama

 

Ok, so I see a strong push for .fbx.

Well, thanks to assimp it should be rather simple to just re-export and reload my mesh as a .fbx.

I see you both are recommending skeletal animation over tweening.

My main reasoning for tweening was to get something up and going (in the animation department) fast!

But other then that, I'm sure I can opt-out for skeletal animation instead. Assimp will handle all the bone and skeletal loading for me.

My only real challenge then is to actually get it up and going. (I've never done anything more complicated then standard matrice transformations and rotations on single meshes)

Thanks for the replies (And especially for the book link!)

-Marcus

P.s. The image were you show the polarity's between a proper animated mesh and the un-proper. The latter could be used for a glitch level ;)


P.s. The image were you show the polarity's between a proper animated mesh and the un-proper. The latter could be used for a glitch level ;)

Wait until you see what that looks like during an animation!

Skeletal animation just makes it so easy once you have it set up! Using Blender, Maya or whatever to do your hardcore animation work and then just bring that in.

Assuming you are importing all your animations correctly, it really is as simple as the pseudo code above - oh and then some shader code of course.

Justin Stenning | Blog | Book - Direct3D Rendering Cookbook (using C# and SharpDX)

Projects: Direct3D Hook, EasyHook, Shared Memory (IPC), SharpDisasm (x86/64 disassembler in C#)

@spazzarama

 

Well I'll have to do it then (as opposed to try to do it)

Ill start by importing my meshes as .fbx today. And go from there.

I'll follow along with the e-book. C# in my opinon looks like Java, which I can translate Java to C++ well enough.

Once again thanks for pointing out some resources. I suppose Ill keep my static meshes. (i.e. Trees, hedges, and statues) in OBJ. While anything I animate I'll

just use .fbx

-Marcus

One other point to consider is that tweening requires an enormous amount of vertex memory, since you are essentially creating a keyframe that includes every vertex. If you make a 10 frame animation, then you have 10x the number of vertices to store! Skeletal animation drastically reduces the amount of required memory, and trades it for a little bit more runtime computation - but it is still very much worth it.

If you aren't at all familiar with skinning, then check out the old Cg Tutorial chapter on it, which is where I first learned about it: http://http.developer.nvidia.com/CgTutorial/cg_tutorial_chapter06.html

Just a status update, thought you guys might like to know.

Though I haven't implmented Skinning yet, I did tweening to work after I implemented a hardware

version (did my tweening in HLSL) So I must have been doing something silly with the dynamic buffers.

Kinda off topic but the results aren't bad. (I still plan on doing skinning)

awd_zpsf7cbe856.png

This topic is closed to new replies.

Advertisement