XNA for C#
DirectX 9 for C#
DirectX 9 for C++
DirectX 9 for VB
Forum
   July 19: Series4 - 2 Pass Renderstates
My Book: Out Now!
      
       Go to section on this site

Additional info


Latest Forum posts

 WinForm not found
  Posted by: riemer
  When: 20/07/2008 at 14:30:03

 Bounding Boxes for Maya Data
  Posted by: riemer
  When: 20/07/2008 at 14:27:31

 QuadTree
  Posted by: riemer
  When: 20/07/2008 at 14:23:48

 Bounding Boxes for Maya Data
  Posted by: vijay
  When: 20/07/2008 at 12:25:40

 Error on compiling
  Posted by: libia
  When: 20/07/2008 at 02:36:16

 Terrain From RAW
  Posted by: reiko
  When: 19/07/2008 at 20:36:39

 WinForm not found
  Posted by: kapil.rajak
  When: 19/07/2008 at 16:24:52

 human skin colors
  Posted by: aguacate
  When: 19/07/2008 at 13:34:55

 Bounding Boxes for Maya Data
  Posted by: Archenon
  When: 19/07/2008 at 09:19:16

 Volunteer to write VB.net tutorial
  Posted by: riemer
  When: 19/07/2008 at 07:00:19


Ads

The effect file

One of the main differences between DirectX 9 and in XNA we need an effect for everything we draw. So what exactly is an effect?

In 3D programming, all objects are represented using triangles. Even spheres can be represented using triangles, if you use enough of them. An effect is some code that instructs your hardware (the graphics card) how it should display these triangles. An effect file contains one or more techniques, for example technique A and technique B. Drawing triangles using technique A will for example draw them semi-transparent, while drawing them using technique B will for example draw all objects using only blue-gray colors as seen in some horror movies.

Don’t worry too much about this, as this is already more advanced stuff which we will handle in Series 3. However, XNA needs an effect file to draw even the simplest triangles, so I’ve written an effect file that contains some very basic techniques. You can download it here. Right-click on the link, and select “Save As”. You should put the file in the same map as your code files. If you don’t know where that is, first go back to your code, and to the File menu, and select “Save [project name] as..” to see where your code is located on your drive. By default, the code files will be saved in the map “C:\Documents and Settings\[user name]\My Documents\Visual Studio 2005\Projects\[project name]\[project name]”.

Now you have downloaded the effect file to the same map as your code files, we will import the file into our XNA project. In your Solution Explorer on the right site of your XNA window, find the Content entry and right-click on it. Select Add->Existing Item and browse to your .fx file. After you’ve clicked the OK button, the .fx file should be added to your Content entry as shown below:



Next, we’ll link this effect file to a variable, so we can actually use it in our code. We will declare a new Effect object, so put this at the beginning of your class:

 Effect effect;

In your LoadContent method, add this line to have XNA load the .fx file into the effect variable for you:


effect = Content.Load<Effect> ("effects");
The “effects” name refers to the part of the file before the .fx extension.

With all the necessary variables loaded, we can concentrate on the Draw method. You’ll notice the first line start with graphics.GraphicsDevice. We can replace this by the shortcut we made in the previous chapter. This line clears the buffer of our window to a specified color. Let’s set this to DarkSlateBlue, just for fun:

 device.Clear(Color.DarkSlateBlue);

XNA uses a buffer to draw to, instead of drawing directly to the window. At the end of the Draw method, the contents of the buffer is drawn on the screen in one time. This way, the screen will not flicker as it would when we would draw each part of our scene separately to the screen.

Running this code will already give you the image you see below, but I would first like to add some additional code. As discussed above, to draw something we first need to specify a technique from an Effect object. We will immediately activate our effect, so next chapter we are ready to render something to the screen!

 effect.CurrentTechnique = effect.Techniques["Pretransformed"];
 effect.Begin();

You see we select the Pretransformed technique from the effects.fx file. This technique will be used and discussed in the next chapter. The last line tells the effect and the graphics card to get ready for some work.

A technique can be made up of multiple passes, so we need to iterate through them. Add this code below the code you just entered:

 foreach (EffectPass pass in effect.CurrentTechnique.Passes)
 {
     pass.Begin();
 
     pass.End();
 }
 effect.End();

You see each pass needs a call to Begin and a call to End. The scene must be drawn between these 2 calls. In the end, we need to put a call to effect.End to tell our effect no more objects will be drawn using this technique.

Finally, we’re through the initialization part! If you don’t understand everything about effects and techniques, there’s no need to worry as we will discuss them in detail in Series 3. With all of this code set up, we’re finally ready to start drawing things on the screen, which is what we will in do next chapter.




DirectX Tutorial 2 - The effect file

If you appreciate the amount of time I spend creating and updating
these pages, feel free to donate -- any amount is welcome !



Click here to go to the forum on this chapter!

Or click on one of the topics on this chapter to go there:
  • help
          I am new to programing, i am at the part of the ef...
  • Xna 2.0
          Hello, I was working through the tutorials, a...
  • InvalidOperationException
          I'm getting an InvalidOperationException that say...
  • Effect error
          whenever i try to run this it gives me the follow...
  • Graphics Device Problems
          Hi, I tried to do the walk through tutorial an...
  • Program.CS Error
          Um hey great tutorials dude but I tried compiling ...
  • Path to effects.fx
          Thanks for your nice tutorials. I have suggesti...
  • Effects file not required
          Hi, Following your fantastic XNA tutorial - thank...
  • loading effects file
          Hi everyone, I am a newbie and this is my first...
  • Effect Transform Trouble
          Hi, I tried to add this line effects.Tr...



    You can try these exercises to practice what you've learned:
  • No homework today.
    Our code so far:

     using System;
     using System.Collections.Generic;
     using Microsoft.Xna.Framework;
     using Microsoft.Xna.Framework.Audio;
     using Microsoft.Xna.Framework.Content;
     using Microsoft.Xna.Framework.GamerServices;
     using Microsoft.Xna.Framework.Graphics;
     using Microsoft.Xna.Framework.Input;
     using Microsoft.Xna.Framework.Net;
     using Microsoft.Xna.Framework.Storage;
     
     namespace XNAtutorial
     {
         public class Game1 : Microsoft.Xna.Framework.Game
         {
             GraphicsDeviceManager graphics;
             SpriteBatch spriteBatch;
             GraphicsDevice device;
             Effect effect;
     
             public Game1()
             {
                 graphics = new GraphicsDeviceManager(this);
                 Content.RootDirectory = "Content";
             }
     
             protected override void Initialize()
             {
                 graphics.PreferredBackBufferWidth = 500;
                 graphics.PreferredBackBufferHeight = 500;
                 graphics.IsFullScreen = false;
                 graphics.ApplyChanges();
                 Window.Title = "Riemer's XNA Tutorials -- Series 1";
     
                 base.Initialize();
             }
     
             protected override void LoadContent()
             {
                 device = graphics.GraphicsDevice;
                 spriteBatch = new SpriteBatch(GraphicsDevice);

                effect = Content.Load<Effect> ("effects");
             }
     
             protected override void UnloadContent()
             {
             }
     
             protected override void Update(GameTime gameTime)
             {
                 if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                     this.Exit();
     
                 base.Update(gameTime);
             }
     
             protected override void Draw(GameTime gameTime)
             {
                 device.Clear(Color.DarkSlateBlue);
     
                 effect.CurrentTechnique = effect.Techniques["Pretransformed"];
                 effect.Begin();
                 foreach (EffectPass pass in effect.CurrentTechnique.Passes)
                 {
                     pass.Begin();
     
                     pass.End();
                 }
                 effect.End();
     
                 base.Draw(gameTime);
             }
         }
     }
     


    Google
     
    Webwww.riemers.net


    If you appreciate the amount of time I spend creating and updating
    these pages, feel free to donate -- any amount is welcome !



    - Website design & XNA + DirectX code : Riemer Grootjans -
    ©2003 - 2008 Riemer Grootjans
  • Translations

    This site in English
    This site in Korean
    This site in Czech

    Microsoft MVP Award



    2007 - 2008 MVP Award
    DirectX - XNA

    Contents

    News
    Home
    Forum
    XNA 2.0 Recipes Book (8)
    Downloads
    Extra Reading (3)
    Matrices: geometrical
    Matrix Mathematics
    Homogenous matrices
    Tutorials (136)
    XNA 2.0 using C# (65)
    Series 1: Terrain (13)
    Starting a project
    The effect file
    The first triangle
    World space
    Rotation - translation
    Indices
    Terrain basics
    Terrain from file
    Keyboard
    Adding colors
    Lighting basics
    Terrain lighting
    VertexBuffer & IndexBuffer
    Series 2:Flightsim (14)
    Series 3: HLSL (18)
    Series 4: Advanced terrain (17)
    Short Tuts (3)
    DirectX using C# (54)
    DirectX using C++ (15)
    DirectX using VB (2)
    -- Expand all --


    Thank you!

    Support this site --
    any amount is welcome !

    Stay up-to-date

    I don't have the time to keep a News section, so stay informed about the updates by clicking on this RSS file!