XNA for C#
DirectX 9 for C#
DirectX 9 for C++
DirectX 9 for VB
Forum
   September 24: 2D-19: Particle engine
My Book: Out Now!
      
       Go to section on this site

Additional info


Latest Forum posts

 Next Chapter
  Posted by: jasonbarry
  When: 15/10/2008 at 15:55:47

 HLSL: compilation fails
  Posted by: aguacate
  When: 15/10/2008 at 12:03:19

 my collision sucks!
  Posted by: orgad
  When: 15/10/2008 at 11:25:49

 HLSL: compilation fails
  Posted by: aguacate
  When: 15/10/2008 at 11:16:55

 Flickering/glitches
  Posted by: Danzence
  When: 15/10/2008 at 09:53:00

 Next Chapter
  Posted by: samer
  When: 15/10/2008 at 06:40:32

 Flickering/glitches
  Posted by: samer
  When: 15/10/2008 at 06:39:17

 Texture terrian c++
  Posted by: ruudriem
  When: 15/10/2008 at 05:55:16

 1.7 and 2.8 problem.
  Posted by: riemer
  When: 15/10/2008 at 03:36:34

 Next Chapter
  Posted by: riemer
  When: 15/10/2008 at 03:34:29


Ads



Starting point for the 3rd series of XNA Tutorials

Welcome to this 3rd installment of my Tutorials on XNA. Because this Series will cover a lot of ground, I would like to take a jumpstart by starting from the code presented below. As you can see on the screenshot below, it will only draw a simple triangle. There is nothing in this code that hasn’t been covered yet in the previous 2 series.

At this moment, my standard effects.fx file is loaded so we are able to render the triangle, but soon my effect file will be replaced by one of your own.




DirectX Tutorial 1 - Starting point

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:
  • Drawing Primitives
          Hey Guys, Got time today to start reading the thi...
  • 2.0 anyone?
          Could someone please upload xna 2.0 code for this ...
  • problem with ResourceUsage.WriteOnly
          "ResourceUsage.WriteOnly" was working before in ...
  • I have found a Lamppost
          It´s not Texturen but colored http://reinerstil...
  • Textures applied wrong
          Riemer, Just wondering why the textures are a...
  • Load effect file
          In this series you loaded the effect file with the...
  • PreparingDeviceSettings??
          I didnt understand what exactly does this line doe...


    That’s about all there is to say about our starting code. If you have been following my tutorials up to this point, simply copy-paste this code into XNA Game Studio 2.0. Remember, you might have to change my namespace to yours (or vice versa). The only requirement is that both namespaces in the Game1.cs and Program.cs files are the same.

     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 XNAseries3
     {
         public class Game1 : Microsoft.Xna.Framework.Game
         {
             GraphicsDeviceManager graphics;
             GraphicsDevice device;
             
             Effect effect;
             Matrix viewMatrix;
             Matrix projectionMatrix;
             VertexBuffer vertexBuffer;
             VertexDeclaration vertexDeclaration;
             Vector3 cameraPos;
     
             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 3";
     
                 base.Initialize();
             }
     
             protected override void LoadContent()
             {
                 device = GraphicsDevice;

                effect = Content.Load<Effect> ("effects");            SetUpVertices();
                SetUpCamera();
            }

            private void SetUpVertices()
            {
                VertexPositionColor[] vertices = new VertexPositionColor[3];

                vertices[0] = new VertexPositionColor(new Vector3(-2, 2, 0), Color.Red);
                vertices[1] = new VertexPositionColor(new Vector3(2, -2, -2), Color.Green);
                vertices[2] = new VertexPositionColor(new Vector3(0, 0, 2), Color.Yellow);

                vertexBuffer = new VertexBuffer(device, vertices.Length * VertexPositionColor.SizeInBytes, BufferUsage.WriteOnly);
                vertexBuffer.SetData(vertices);

                vertexDeclaration = new VertexDeclaration(device, VertexPositionColor.VertexElements);
            }

            private void SetUpCamera()
            {
                cameraPos = new Vector3(0, 5, 6);
                viewMatrix = Matrix.CreateLookAt(cameraPos, new Vector3(0, 0, 1), new Vector3(0, 1, 0));
                projectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, device.Viewport.AspectRatio, 1.0f, 200.0f);
            }

            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(ClearOptions.Target | ClearOptions.DepthBuffer, Color.DarkSlateBlue, 1.0f, 0);

                effect.CurrentTechnique = effect.Techniques["Colored"];
                effect.Parameters["xView"].SetValue(viewMatrix);
                effect.Parameters["xProjection"].SetValue(projectionMatrix);
                effect.Parameters["xWorld"].SetValue(Matrix.Identity);
                effect.Begin();
                foreach (EffectPass pass in effect.CurrentTechnique.Passes)
                {
                    pass.Begin();
                    device.VertexDeclaration = vertexDeclaration;
                    device.Vertices[0].SetSource(vertexBuffer, 0, VertexPositionColor.SizeInBytes);
                    device.DrawPrimitives(PrimitiveType.TriangleList, 0, 1);
                    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 (160)
    XNA 2.0 using C# (89)
    2D Series: Shooters! (22)
    3D Series 1: Terrain (13)
    3D Series 2: Flightsim (14)
    3D Series 3: HLSL (18)
    Starting point
    HLSL introduction
    Vertex format
    Vertex shader
    Pixel shader
    Per-pixel colors
    Textured triangle
    Triangle strip
    World transform
    World normals
    Per-pixel lighting
    Shadow map
    Render to texture
    Projective texturing
    Real shadow
    Shaping the light
    2D screen processing
    Preshaders
    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!