Rendering Meshes Using the Programmable Pipeline

The simple triangle example is just that: simple. It's rare that you would use a vertex program on a single triangle in a real-world example, so instead of using this example, you should switch to rendering an entire mesh. Go ahead and replace the variables for the vertex declaration and the vertex buffer with this one for the mesh:

 private Mesh mesh = null; 

One nice thing about the mesh objects is that when they are rendered, they will automatically set the vertex declaration, so you don't need to worry about it. Naturally, you'll need to remove all instances in the code that dealt with either the vertex buffer or vertex declaration, as well. You will also need to update the initialize method to create the mesh. After the device and effect objects have been created, add the following code:

 // Create our cylinder mesh = Mesh.Cylinder(device, 1.5f, 1.5f, 1.5f, 36, 36); 

Finally, you'll need to change the drawing method. Replace the DrawPrimitives call with the DrawSubset method on the mesh:

 mesh.DrawSubset(0); 

As you can see, there's nothing really special you need to do. Running this application now will cause a (very flat-looking) cylinder to be rendered with the same cycling colors our triangle had. The cylinder doesn't look much like a cylinder, though, since it's shaded with the same color everywhere. If you remember when this was discussed before, you simply added a directional light, and everything looked wonderful.

Using the lighting properties that you used before would defeat the purpose of this chapter, since those lights are implemented as part of the fixed-function pipeline. Since the goal is to show off the programmable pipeline, you can just update the HLSL code to render a single white directional light. First, you'll need a variable to describe the direction of the light. Add the following variable to your HLSL code:

 // The direction of the light in world space float3 LightDir = {0.0f, 0.0f, -1.0f}; 

Now you can simply replace the existing method with the following:

 // Transform our coordinates into world space VS_OUTPUT Transform(     float4 Pos  : POSITION,     float3 Normal : NORMAL ) {     // Declare our return variable     VS_OUTPUT Out = (VS_OUTPUT)0;     // Transform the normal into the same coord system     float4 transformedNormal = mul(Normal, WorldViewProj);     // Set our color     Out.diff.rgba = 1.0f;     Out.diff *= dot(transformedNormal, LightDir);     // Transform our position     Out.pos = mul(Pos, WorldViewProj);     // Return     return Out; } 

You'll notice that another parameter has been included in the method, namely the normal data. You'll need to make sure that any vertices you run through this technique have normal data; otherwise, this will cause an error. The cylinder created by the Mesh class already contains this information, so you're fine here. Next, you will need to transform the normal data into the same coordinate system that the vertices will be in. You can't have lighting calculations for a certain set of vertices be performed on data in a different coordinate system; it just wouldn't make sense.

If you looked at any of the mathematics of lighting section in the DirectX SDK documentation, you'll know that a directional light is simply calculated by taking the dot product between the normal of the vertex and the direction of the light, and multiplying that by the color of the light. You can set your rgba swizzle to 1.0f, and do this calculation to get the final color of the cylinder.

The application should now render a white cylinder rotating around. The cylinder should look much more realistic and properly shaded now that you've added your own lighting calculations. It should appear similar to Figure 11.2.

Figure 11.2. Shaded cylinder using the programmable pipeline.

graphics/11fig02.jpg

Feel free to change the color of the light to see different effects. What if you changed it from a white light to a purple light?



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