Using Simple Formulas to Simulate Animation

Rather than wasting space here duplicating the same code that has already been covered, you should copy the code that was used to render the cylinder in the previous chapter. This code will be modified slightly to show how easily simple animation can be added once you've begun using the programmable pipeline. This example will be modified to allow the mesh to "wobble" as it's being rendered.

Once the code has been loaded from the previous example, the first thing you'll want to do is to switch to using a sphere rather than the cylinder. It's not that this update wouldn't work with the cylinder; it would. However, the sphere happens to just look better. Replace the cylinder creation code with this:

 // Create our sphere mesh = Mesh.Sphere(device, 3.0f, 36, 36); 

Since the sphere's size is larger than the cylinder that was being rendered before, you will also need to update the view matrix. You'll need to move the camera position back some, as in the following:

 viewMatrix = Matrix.LookAtLH(new Vector3(0,0, 9.0f), new Vector3(),     new Vector3(0,1,0)); 

With that, you should be able to run the application and see a (quite boring) spinning sphere being lit by a single directional light. What do you think could be done to make the sphere perform some simple animation? In your shader code, replace the single line that does transformation with the following code:

 // Store our local position float4 tempPos = Pos; // Make the sphere 'wobble' some tempPos.y += cos(Pos + (Time * 2.0f)); // Transform our position Out.pos = mul(tempPos, WorldViewProj); 

You'll notice that the Time variable is used in this example once more. Since that variable wasn't used in the example this was based on, you will need to add it to your shader code:

 float Time = 0.0f; 

In addition, you will need to update the variable every frame, so in your main code at the end of the UpdateWorld method, add the following code to update this variables value:

 effect.SetValue("Time", angle); 

So what exactly does the shader code accomplish? First, the input position of the vertex is stored in a temporary variable. Then the y component of this variable is modulated by adding the cosine of the current position of the vertex added with the constantly updating Time variable. Since the cosine method will always be within the range of -1 to 1, the effect will repeat indefinitely. With the vertex now being updated to simulate our animation, the temporary variable is used to perform the transformation rather than the original input position.

While this updated code does look a lot nicer than the boring old sphere, there's yet another easy thing you can do to make it look even better. Why not add some "animating" color as well? Replace the Out.diff initialization code with the following:

 // Set our color Out.diff = sin(Pos + Time); 

Obviously, this code is based directly on the vertex animation code that was just done. The only major difference is the use of the sine method rather than cosine. You can see a major difference in the quality of the rendered scene while the application is running, though. This just goes to show you how you can quickly turn a "boring" sphere into something exciting. HLSL allows you to quickly change the way your scene is rendered with little to no changes to your main code.



Managed DirectX 9 Graphics and Game Programming, Kick Start
Managed DirectX 9 Kick Start: Graphics and Game Programming
ISBN: B003D7JUW6
EAN: N/A
Year: 2002
Pages: 180
Authors: Tom Miller

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net