# Character Animation with Direct3D- P10

Chia sẻ: Cong Thanh | Ngày: | Loại File: PDF | Số trang:20

0
45
lượt xem
5

## Character Animation with Direct3D- P10

Mô tả tài liệu

Character Animation with Direct3D- P10:This book is primarily aimed at teaching indie and hobby game developers how to create character animation with Direct3D. Also, the seasoned professional game developer may find some interesting things in this book. You will need a solid understanding of the C++ programming language as well as general object-oriented programming skills.

Chủ đề:

Bình luận(0)

Lưu

## Nội dung Text: Character Animation with Direct3D- P10

2. 8 Morphing Animation So far this book has looked only at skeletal animation. In today’s games this method is used almost exclusively to animate the game characters’ movements. However, it wasn’t always so. For example, the first Quake game used characters animated using morphing animation instead. This chapter covers the basics of morphing animation (also known as per-vertex animation). This concept will also be taken one step further by combining morphing animation with skeletal animation In this chapter, you'll find: Introduction to morphing animation Morphing animation on the GPU with vertex shaders Combining morphing animation with skeletal animation 167 ease purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
3. 168 Character Animation with Direct3D BASICS OF MORPHING ANIMATION In skeletal animation, each vertex was linked to one or more bones with associated weights. In morphing animation, however, two or more positions are stored per vertex and are simply blended using linear interpolation (LERP). Each predefined vertex position is called a morph target. Once you have a list of morph targets, you can blend between them using weights (just as in skeletal animation), as shown in the following formula: v1 = [x1, y1, z1] v2 = [x2, y2, z2] v = v2 • p + v1 • (1 – p) The equation above describes how to create a blended vertex v between the two morph targets v1 and v2 (using simple LERP). This same example is also illustrated in Figure 8.1, where a new vertex position is calculated with a weight of 32%: FIGURE 8.1 Blending the position of a single vertex using two morph targets and one weight. In the same way the position of the vertex is animated, you can also animate the vertex normal, UV coordinates, etc. The following code is an excerpt from Example 8.1, where a morphed mesh is created from two target meshes. A morphed mesh is created from two target meshes by performing the blend before rendering in the CPU: Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
4. Chapter 8 Morphing Animation 169 BYTE *target01, *target02, *face; //Lock morph target vertex buffers m_pTarget01->LockVertexBuffer(D3DLOCK_READONLY, (void**)&target01); m_pTarget02->LockVertexBuffer(D3DLOCK_READONLY, (void**)&target02); //Lock destination vertex buffer m_pFace->LockVertexBuffer(0, (void**)&face); //Blend the morph targets and store in the destination mesh for(int i=0; iGetNumVertices(); i++) { //Get position of the two target vertices D3DXVECTOR3 t1 = *((D3DXVECTOR3*)target01); D3DXVECTOR3 t2 = *((D3DXVECTOR3*)target02); D3DXVECTOR3 *f = (D3DXVECTOR3*)face; //Perform morphing *f = t2 * m_blend + t1 * (1.0f - m_blend); //Move to next vertex target01 += m_pTarget01->GetNumBytesPerVertex(); target02 += m_pTarget01->GetNumBytesPerVertex(); face += m_pFace->GetNumBytesPerVertex(); } //Unlock all vertex buffers m_pTarget01->UnlockVertexBuffer(); m_pTarget02->UnlockVertexBuffer(); m_pFace->UnlockVertexBuffer(); Since the position element is always the first thing in a vertex, you don’t really need to know the actual vertex format of the mesh. You can simply cast the BYTE pointer to a D3DXVECTOR3 object to get the position element of a vertex. Then you add the number of bytes per vertex to the BYTE pointer to access the next vertex. However, this is a hack that you shouldn’t use in a “proper” application. Instead you should cast to whatever vertex structure you are using and perform the blending on all elements: position, normal, UV coordinate, etc. This code shows how morphing can be done easily on the CPU. First you lock all the vertex buffers (both target meshes and destination mesh). Then you iterate through all the vertices in the destination mesh and set its new vertex positions to ease purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
5. 170 Character Animation with Direct3D a blend between the two target meshes. The blend amount is defined by the m_blend variable. As you can see, it doesn’t require that much code to get a basic example of morphing animation up and running. Have a look at Example 8.1 on the CD-ROM for the full code. EXAMPLE 8.1 In this example, software morphing is implemented, performing the mor- phing calculation using the CPU. Use the Up and Down keys to change the blend amount used. As you can see in this example, it is possible to have weights outside the range [0.0–1.0]. USING MULTIPLE MORPH TARGETS In the previous example you learned how to blend between two morph targets. The next step is to blend between more than just two morph targets. Imagine, for example, that you have a set of mouth shapes you want to use to make the character look like he’s talking. You also have a second set of morph targets controlling the blinking of the eyelids. If it were only possible to blend two Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
6. Chapter 8 Morphing Animation 171 morph targets simultaneously, it would be impossible to have the character blink his eyes and talk at the same time. Luckily, of course, this is not the case. To blend more than one morph target, you need a base mesh from which all the morph targets are compared. In the case of a character face, the base mesh would be the face without expressions and emotions, etc. Figure 8.2 shows the expressionless base mesh and the different target meshes: FIGURE 8.2 Blending more than two morph targets to produce the final mesh. ease purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
7. 172 Character Animation with Direct3D In mathematical terms, this could be described something like this: Base = [xb, yb, zb] Morph1= [x1, y1, z1] Morph2= [x2, y2, z2] … Morphn= [xn, yn, zn] W= [w1, w2,…wn] Morphn= [xn, yn, zn] i=1 v = base + n ((Morphi – base)*wi) Base denotes a vertex from the base mesh. Morph1 to Morphn describes the cor- responding vertices in the morph targets. W is the collection of weights—one weight for each morph target. The final vertex v is then calculated by adding the weighted difference between the base and the morph targets to the original base vertex. As shown in Figure 8.2, the morph targets are weighted before being added to the base mesh. To blend more than one morph target, you then use the following algorithm: ID3DXMesh *pBaseMesh; ID3DXMesh *pDestMesh; vector morphTargets; vector weights; //Load base mesh, morph target meshes, and set weights //Also create the destination mesh as a clone of the base mesh //For each vertex in the base mesh for(int vertex = 0; vertex < pBaseMesh->GetNumVertices(); vertex++) { //Get vertex position D3DXVECTOR3 pos = GetVertexPosition(pBaseMesh, vertex); Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
8. Chapter 8 Morphing Animation 173 //Create a new position //(which will be the final blended vertex position) D3DXVECTOR3 newPos = pos; //For each active morph target (it’s weight != zero) for(int target = 0; target < morphTargets.size(); target++) { If(weights[vertex] == 0.0f) continue; //Get morph targets vertex position D3DXVECTOR3 targetPos; targetPos = GetVertexPosition(morphTargets[target], vertex); //Add the weighted difference to the final position newPos += (targetPos – pos) * weights[vertex]; } //Assign the new position to the destination mesh SetVertexPosition(pDestMesh, vertex, newPos); } The preceding code demonstrates how to blend multiple morph targets to pro- duce the final morphed mesh. For each vertex of the mesh, you iterate through the morph targets; compare the vertex position of the base mesh and the target mesh. Then add the weighted difference to the final vertex position. This means that if the weight is zero or the vertex position of the target mesh is the same as the vertex position of the base mesh, then the final position of the vertex won’t be changed. Revisit Example 8.1. On the CD-ROM you will also find a third morph target (face03.x) in the resource folder of Example 8.1. Try to edit Example 8.1 to blend between all three morph targets on the CD using the pseudo-code above. Remember that “face01.x” is the base mesh to which you should compare “face02.x” and “face03.x.” M ORPHING A NIMATION ON THE GPU So far, all the morphing has been done in software, which, as you can imagine, can be pretty slow (especially for large meshes with many morph targets). Instead, here’s how you can do the morphing animation in the GPU. The problem with ease purchase PDF Split-Merge on www.verypdf.com to remove this watermark.