When we want to paint an animated model we are going to use Cal 3D Instance Actor. In the end Cal 3D Instance Actor will also have a Cal Core Actor, as this is where it takes the Core information we have said before from. But in aim to paint our animated models we will need a Cal3D Instance Actor. Cal 3D Instance Actor again encapsulates a class or Cal 3D's library. So in aim to be able to render our animated model we need Cal Model, Cal Model is the instance of a Cal3D model. We also need Cal Hardware Model, an object's instance which will allow us to render our model through shaders, in this case, through a Perfect Shader, which we will need in aim to calculate what I'm telling you before the vertexes' positions based on their bones and on their weights and indexes. On the other side we have the textures vector we are getting from Texture Manager based on the texture files names we have previously saved in Cal 3D Core Actor, the current cycle, the current animation ID, the current animation time, the Renderable Vertex class which allows us to paint vertexes with indexes. As we learned in the previous session, the effect we are going to use to paint this model is DirectX 11's shader effect, the number of vertexes and the number of faces our geometry contains and the following methods. The Destroy method will clean all the dynamic elements from the memory, the Load Vertex Buffer method will load and create Renderable Vertex, it will create Index Buffer vertexes for our model. The Load Texture method will take care of loading all the textures we are going to use in our model, and then we have the builder-debuilder, the 3D DirectX initializing part, the Render, the Update, the initialization part which has many implications. Three methods which are the ones which will execute one or another animation. Execute Action executes an action animation, where the action identifier gets it and the entrance time. The entrance time is the time which will interpolate between the current animation and time, and the new animation we are going to insert, that is, if we are walking and we want to skip to a hitting animation, if we set entrance time at zero we have given it a walking Frame Start, and then a hitting Frame Start next. So we want the animation to interpolate, thanks to the use of bones and quaternions we can make that, for a while, it will go from one animation's state to the other animation's state in an interpolated way, making a blend between animations, making them mix instead of skipping from one frame to another. Blend Cycle interpolates between one animation cycle and another, that is, if we are in an idle animation cycle, we can go to the walking animation cycle, then we insert the 10 at walking and we give it the entrance time of the animation which will interpolate the two animations, as we said before, and then if we want to, to exemplify, delete an active animation cycle, we should give it the animation time to delete it and then the Get Current Cycle and the Get Animation ID, which gives us back the ID of the action or animation which is being executed or the animation cycle which is being currently executed. If we go to the code we have the builder, where the member variables are built, the destroyer calls the Destroy method to destroy it. I will explain the Render part later. First we will go to the initialization part, then Update and then Render. What the initialization part does is receiving the Cal Core Model from which we depend, it creates a Cal Core Model object and also a Cal Hardware type, Cal Hardware Model, attaches all the meshes from Cal Core Model to our Cal Model instance, we insert a first animation cycle, which is set to zero as default, with zero entrance time, and we make an Update of zero. What it will do is putting our model in the first animation frame, if we didn't, we would get the default pose, which is the one called T-pose, typical from the animated model. Then later we have the 3D initialization part, in the starting part, which will first tell Effect Manager to give us back the effect we have to use for the animated models' vertexes type. So, we use the Get Effect method and the Vertex Type, KG POSITION WEIGHT INDICES NORMAL TEXTURE VERTEX is the type of vertex we are going to use to paint an animated Cal 3D model. It calls the methods Load Vertex Buffer and Load Texture, which would be the DirectX part. Load Texture method will go through all the textures in Cal Core Model and tell Texture Manager to load them and give us back the texture. Once it gives us back the texture object, we add it to the textures list we are going to use later in the Render method. Destroy method will take care of destroying the elements we have created dynamically, as I said before. Execute Action executes an action-type animation using Cal Model's mixer, giving it the animation's ID, the entrance time, exit time, weight and finally if we want to lock or unlock the animation at the end, the Autolock, which is set to false automatically. We could modify this method to get more parameters, where we could define all the parameters Execute Action uses, but to simplify it we have only used these two parameters. Blend Cycle uses the mixer once again, and in this case it cleans the previous animation cycle and puts the new cycle's blend depending on the ID we give it, the weight, which is set at 100%, and the animation entrance time. Again, this method simplifies Cal3D's options. We could do a more complex method, so that we could be able to keep both cycles instead of taking off the animation cycle we have right now, even though we should make the Clear Cycle in another call. We do it this way, although we won't be able to blend different animation cycles, but normally we will only use one animation cycle at once, as it will simplify our job a little, so we have decided to do it this way. Again, we can do it in many ways, but then it would complicate a bit the code encapsulating, that is, we wouldn't be encapsulating as much. Clear Cycle method deletes the animation's current cycle based on the entrance time. Current Cycle gives us back the active animation cycle, and Get Animation Id gives us back the Current Animation Id, that is, the action animation we are executing right now. From this part of the code we could say that we have two complex parts to implement Cal 3D's library. On one side, we have the Load Vertex Buffer method, and then we have the Render method. Load Vertex Buffer method will load or create a Vertex Buffer and an Index Buffer with all the information from our vertexes and our animated model indexes, and create a Renderable Vertex class object as the one we are going to use. So, to do so, we use Cal Hardware Model. First of all we calculate the number of faces and the number of vertexes the Cal Model has. We save enough data with enough memory to store as many vertexes and faces as our Cal 3D model has. The number of vertexes is multiplied by the bytes size of each of the vertexes and we multiply it by three. Multiplying it by three is an easy way to do it. The fact is, when we use Cal 3D, and give it to Cal Hardware Model, this amount of vertexes degenerates. This means if we have x vertexes initially, when we give it to Cal Hardware Model, due to the calculations on the indexes, there are vertexes which degenerate, and one vertex can become two or three of them. We multiply it by three to calculate enough memory to be able to have all the vertexes, even if they degenerate. Once this is done, this memory we captured in the beginning is deleted, so, we don't keep it in the memory. That is, we don't disregard the memory we aren't going to use, because later we are going to create Renderable Vertex, with the vertexes and indexes the Cal Hardware Model actually has. This is a halftime step. Inside Cal Hardware Model's object, in line 156, we are going to establish the information where we find the vertexes, the weights, the Matrix Index, which are the bones indexes, the information where the rule is and the information where the texture coordinates are. So we are going to fill our vertexes, we tell it where they should be filled, which in this case is l_Vertexs, where it begins, and Stride, where you can find the vertex's size. For the vertex, the geometry, the method is Vertex Buffer in line 156. The geometry, the XYZ position are at the start of the vertex. However, the weights will be at the beginning of the vertex plus 12 bytes. That is, each geometrical position has 4 bytes, if we have XYZ, it will be 4 multiplied by 3, 12 bytes. This means the weights will be at the beginning of the vertex plus 12. The bones' IDs will be 12 plus 16, as we said we had up to four weights, up to four bones, this means four weights for four bones, so we have four floats. Each float has four bytes, so they are 16, and we go from 12 to 28. And in the indexes happens exactly the same. We have each index which is defined as an entire number, which is 4 bytes, so 16 more. 4 indexes, 4 multiplied by 4 equals 16. So we go from 28 to 44. The rule is in the vertex from where it begins the vertex plus 44 bytes. There are usually 3 XYZ float components, so 3 multiplied by 4 equals 12 bytes again. We go from 44 to 56. Which means that the texture coordinates, in this case, the first coordinates couple begins where the vertex starts plus 56 bytes. And finally we set the setEditBuffer, to which we tell where we have the information where the indexes and faces will be loaded. So, to finish, we go to the Load method and fill all our vertexes and faces information inside the local variables we have created here. These local variables are L Vertex and L Faces. Once the Load method ends our Cal3D model will be built in HardwareMode. So we have our vertexes and our indexes. Now, if we calculate, we ask it the number of faces and real vertexes each HardwareMode has, and we build our RenderableVertex based on Cal3D index's size based on the library, depending on a Define the library has we can either use 16bit or 32bit indexes. That is, we can use up to 65,535 indexes, or if we are using 4 bytes, 2 raised to the 32nd power indexes. So usually 65,535 would be enough, we will usually use 2byte indexes, as they are quite less heavy. So we build a RenderableVertex type object calling the new CTriangleListRenderableIndexed 16 or 32 Vertex, giving it the kind of vertex, the Device, the memory direction where the vertexes can be found, the number of total vertexes this vertexes net has, the path of the memory where the faces are placed and the total number of indexes those faces have, that is, number of faces multiplied by 3. With this, we will have created our Vertex and our IndexBuffer inside the RenderableVertex structure. And now we delete from our memory the L Lever from L Faces, which are the two local variables we have initially created with the degenerated information. That means it will have enough space in aim to be able to store all this data. Now finally we have done the initialization part. Let's go to the Update method. Update method will simply update CalModel in Cal3D's stay, and depending on the time that has passed we will calculate the ID action-type animation. We will run AnimationId, which will tell us whether the animation is over, and then we will go to CurrentAnimationId, the action-type animation currently being executed will be minus 1. And we go back to the Render method, which would be the last part. Render method's function for each painting is going through all our HardwareMode's bones and establishing their morph matrices. These bones are represented as a quaternion. So, we turn this quaternion into a matrix which we are going to use inside DirectX, inside the Shader. So the first thing we said is converting this quaternion into a matrix. We do this in line 40, where we assign the quaternion. And finally, we assign the position of the matrix. These lines 42, 43 and 44 depend on the position where the bone the matrix from we are creating begins. And we prepare it by making the matrix's transposition in aim to be able to give it to our Shader's parameters. So, to do this we use EffectManager, M Parameters variable, the bones where we copy all our bone's info in the EffectManager Bones, in line 47. And the maximum number of bones we can have is the macro MAXBONES multiplied by the size of a float by 4 multiplied by 4. Each DirectX11 matrix has 4 multiplied by 4 elements, that is, 4 files and 4 columns. And each element is float-type. So once we have done this, once we have established the parameters in the EffectManager, we go to the DrawIndexed method in the natural Vertex class, giving the Device the effect we want to use, EffectManager's parameters we want to use to paint this effect, the number of faces multiplied by 3 which this HardwareMode uses, the start index this HardwareMode uses, and BaseVertex's index, that is, the index of the beginning of the vertex base, which we are going to use in aim to paint this HardwareMode. So, we are going to do this for each HardwareMode block, and with this, we are going to follow the coloring of all the elements which form our animated object.