Rendering Shader Programs with Techniques

Now that you have the vertex program written, how can you actually use it? There isn't an "entry point" into the vertex program, just a method somewhere. How do you get this method called, though? Quite simply, you can use something called a "technique." A technique consists of one to many passes, where each pass can set device state and set the vertex and pixel shaders to use your HLSL code. Look at the technique you can use for this application. Add this to your simple.fx file:

 technique TransformDiffuse {     pass P0     {         CullMode = None;         // shaders         VertexShader = compile vs_1_1 Transform();         PixelShader  = NULL;     } } 

Here, you've declared a technique called TransformDiffuse, since this method will transform the vertices and add a diffuse color. This name is purely decorative and has no real purpose. In this technique, one pass has been defined. The cull mode is set to none, so you can see both the front and back of the single triangle. If you remember back to the original sample, you had to do this in the actual C# code. However, passes can be thought of as an excellent place to store (and update) device state, so it makes plenty of sense to add this here.

Next, you will want to use the vertex program for the vertices that will be rendered here, so the method is compiled using the vs_1_1 target. This target string should be used based on what you know your device supports. Since you already know your device supports at least vertex shader 1.1 (based on the capabilities check), you can use this target. If you were using vertex shader 2.0, the target would be vs_2_0. Pixel shader targets are defined such as ps_2_0.

Since there are no pixel programs, you can set the pixel shader member to null here, and that's the end of the pass. Nothing much going on, but now you will need to update the actual C# code to use this technique and pass.

First, you will use the Effect object that has been declared, since this is the main object used when dealing with your HLSL code. Add the following to the InitializeGraphics method (after device creation):

 // Create our effect effect = Effect.FromFile(device, @"..\..\simple.fx", null, ShaderFlags.None,     null); effect.Technique = "TransformDiffuse"; 

As you see, you are creating the effect object from a file (the one you've just created). You also set the technique here since only one technique will be used for the entire program. This effect object will be used for all of your dealings with the HLSL code you've written thus far. If you remember, you had two variables that needed to be updated each frame. Add the following code to the end of the UpdateWorld method:

 Matrix worldViewProj = worldMatrix * viewMatrix * projMatrix; effect.SetValue("Time", (float)Math.Sin(angle / 5.0f)); effect.SetValue("WorldViewProj", worldViewProj); 

Here, you take the stored matrices and combine them, and update the variables in the HLSL code. This method is called every frame, so the variables will be updated this often as well. The only thing you have left to do is actually update the rendering to draw using the vertex program. Update your DrawPrimitives call as follows:

 int numPasses = effect.Begin(0); for (int i = 0; i < numPasses; i++) {     effect.Pass(i);     device.DrawPrimitives(PrimitiveType.TriangleList, 0, 1); } effect.End(); 

Since the technique has already been selected for this effect, when it's time to render, you need to call the Begin method. The flags for this method allow you to choose to not save a particular state, but for this example aren't very important. The method will also return the number of passes in the current technique. Even though there is only one pass in the technique this example is using, it is a good idea to set up a loop similar to the one here to ensure that you render each technique as it was intended. Before you do any drawing, though, you need to call the Pass method on the effect object. The single parameter this takes is the pass index you will be rendering. This method gets the device ready to render the pass indicated, updates the device state, and sets the vertex and pixel shader methods. Finally, you simply draw the primitives just as you did before.

Once your rendering has been completed, you will need to call End on the effect, just like you called Begin earlier. This signals that (for now) you are done with this effect object. Running the application now should show you a rotating triangle that is consistently changing color. See Figure 11.1.

Figure 11.1. Rotating colorful triangle.

graphics/11fig01.jpg



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