Drawing with a Custom Effect (XNA Game Studio 4.0 Programming)

So you have your first custom effect file but nothing to show for it on the screen. Let’s get started by drawing a quad with the new effect file.

Define the following two member variables to hold the Effect and the array of vertices make up the two triangles in the quad.

tmpD-292_thumb

The Effect type is used with custom effects and is the base type of the five built-in effects.There are a number of built-in vertex types.The VertexPositionColor contains both the vertex position and the color for the vertex. All built-in vertex types implement the IVertexType interface that requires the vertex type to specify the VertexDeclaration.As we discussed before, the VertexDeclaration is used to tell the graphics hardware how the fields of the vertex should be used, such as position and color.

Next, in the games LoadContent method, load the Effect and define each of the vertices.

tmpD-293


 

 

tmpD-294

 

Notice that the Effect is loaded through the content pipeline.After loading the effect, set three properties for the World, View, and Project matrices.Although the built-in effects expose these as properties in the API surface, your Effect can’t because there is no guarantee that the Effect will use those global variables. Instead, use the Parameters collection and specify the parameter by string name.

Finally, in the games Draw method, tell the graphics card that you are using this specific effect and to draw the two triangles.

tmpD-295_thumb

Before using an Effect, call Apply on the specific EffectPass you plan to use. In this case, you know there is only one EffectTechnique, so use the CurrentTechnique and there is only one EffectPass, so use index 0.

Now you are ready to draw. Press F5 and see the glory that is your first effect. Figure 8.3 shows an example of what you should see.

Well that’s not too exciting, but it draws a pure red quad to the screen. Notice that the colors that were defined for each vertex are not used because the template pixel shader just outputs the single red color and does not use the vertex color. Don’t worry about it—it gets better looking from here.

Drawing a quad with your first custom effect

Figure 8.3 Drawing a quad with your first custom effect

Next post:

Previous post: