XNA for C#
DirectX 9 for C#
DirectX 9 for C++
DirectX 9 for VB
My XNA Book
       Go to section on this site

Additional info

Latest Forum posts

 Account settings
  Posted by: Anonymous
  When: 07/05/2014 at 09:48:39

 forced subtitle
  Posted by: Applefly
  When: 07/05/2014 at 06:00:48

 convert DVD into PMS
  Posted by: Applefly
  When: 07/05/2014 at 05:55:25

 DVD to Digital Copy easily
  Posted by: VIKIVannessa
  When: 05/05/2014 at 06:52:29

 DVD on Xbox 360/Xbox One Console
  Posted by: VIKIVannessa
  When: 05/05/2014 at 06:51:47

 Extract .Srt Subtitles
  Posted by: Applefly
  When: 04/05/2014 at 03:54:38

 Encode Movie collection
  Posted by: Applefly
  When: 04/05/2014 at 03:52:41

 Convert DVD to WMV
  Posted by: Applefly
  When: 29/04/2014 at 05:53:50

 rip DVDs into digital files
  Posted by: Applefly
  When: 29/04/2014 at 05:51:20

 iTunes movies/music to Kindle Fire
  Posted by: ciciyu80
  When: 29/04/2014 at 05:10:20


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 on your hard drive, for example in the same map as your code files.

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 Series3D1Content entry. Simply drag-and-drop you .fx file onto this entry. Alternatively, right-click on Series3D1Content entry. 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 a Clear command. This line clears the buffer of our window to a specified color. Let’s set this to DarkSlateBlue, just for fun:


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! Add this line to your Draw method:

 effect.CurrentTechnique = effect.Techniques["Pretransformed"];

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)

All your drawing code must be put after your call to pass.Apply().

Finally, we’re through the initialization part! If you’re not yet 100% clear on 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:
  • Adding alpha to effects file
          Hello everyone, specially Riemers who has created ...
  • Final code problem
          HI at the end of the code i get this error Erro...
  • Syntax Error with effects file
          Hi There Guys! Im new to programming and really h...
  • Output does not change
          Hey, I have just started on your Tutorial using...
  • Another Effects error
          I first want to thank you for the great tutorial. ...
  • effects file and XNA 4.0 (Beta)
          I might be causing myself more problems then its w...
  • error x3000:syntax error
          Hi I am new at this too, and I followed this tuto...
  • Cannot load effect
          HI All , I downloaded and installed GameStudio ...
  • Publishing the effect file
          Hi, I have made an XNA game, using the given ef...
  • 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 System.Linq;
     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.Media;
     namespace Series3D1
         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;
                 Window.Title = "Riemer's XNA Tutorials -- 3D Series 1";
             protected override void LoadContent()
                 spriteBatch = new SpriteBatch(GraphicsDevice);
                 device = graphics.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)
             protected override void Draw(GameTime gameTime)
                 effect.CurrentTechnique = effect.Techniques["Pretransformed"];
                 foreach (EffectPass pass in effect.CurrentTechnique.Passes)


    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 - 2011 Riemer Grootjans
  • Translations

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

    Microsoft MVP Award

    2007 - 2011 MVP Award
    DirectX - XNA


    XNA 2.0 Recipes Book (8)
    XNA 3.0 Recipes Book (8)
    Extra Reading (3)
    Matrices: geometrical
    Matrix Mathematics
    Homogenous matrices
    Community Projects (1)
    Tutorials (160)
    XNA 4.0 using C# (89)
    2D Series: Shooters (22)
    3D Series 1: Terrain (13)
    Starting a project
    The effect file
    The first triangle
    World space
    Rotation - translation
    Terrain basics
    Terrain from file
    Adding colors
    Lighting basics
    Terrain lighting
    VertexBuffer & IndexBuffer
    3D Series 2: Flightsim (14)
    3D Series 3: HLSL (18)
    3D Series 4: Adv. terrain (19)
    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!