XNA for C#
DirectX 9 for C#
DirectX 9 for C++
DirectX 9 for VB
Forum
   
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


Ads

Improving performance by using VertexBuffers and IndexBuffers

Your terrain is fully working, Each frame again, all vertices and indices are being sent over to our graphics card. This means each frame we are sending over exactly the same data. Obviously, this should be optimized.

We want to send the data over to the graphics card only once, after which the graphics card should store it in its own superfast memory. This can be done by storing our vertices in a VertexBuffer, and our indices in an IndexBuffer.

Recipes 5-4 and 5-5 give explain these buffers in a lot more detail.

Start by declaring these 2 variables at the top of your code:

 VertexBuffer myVertexBuffer;
 IndexBuffer myIndexBuffer;

We will initialize and fill the VertexBuffer and IndexBuffer in a new method, CopyToBuffers. This code does the trick for the VertexBuffer:

 private void CopyToBuffers()
 {
     myVertexBuffer = new VertexBuffer(device, VertexPositionColorNormal.VertexDeclaration, vertices.Length, BufferUsage.WriteOnly);
     myVertexBuffer.SetData(vertices);
 }

The first line creates the VertexBuffer, which comes down to allocating a piece of memory on the graphics card that is large enough to store all our vertices. Therefore, you need to specify how many bytes we need. This is done by specifying the number of vertices in our array, as well the VertexDeclaration (which contains the size in bytes for one vertex, as well see in Series 3).

The second line actually copies the data from our local vertices array into the memory on our graphics card.

We need to do the same for our indices, so put this code at the end of the method:

 myIndexBuffer = new IndexBuffer(device, typeof(int), indices.Length, BufferUsage.WriteOnly);
 myIndexBuffer.SetData(indices);

To find out how many bytes to allocate, we pass in the type of each index as well as how many of them we want to store. The second line copies the indices over to the graphics card.

Dont forget to call this method at the end of our LoadContent method:

 CopyToBuffers();

With that being done, you only need to instruct your graphics card to fetch the vertex and index data from its own memory using the DrawIndexedPrimitives method instead of the DrawUserIndexedPrimitives method. Before our call this method, we need to let your graphics card know it should read from the buffers stored in its own memory:

 device.Indices = myIndexBuffer;
 device.SetVertexBuffer(myVertexBuffer);
 device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, vertices.Length, 0, indices.Length / 3);                

We indicate where the graphics card should get its indices and vertices from and render the triangles.

Running this code will give you the same result as in last chapter. This time however, all vertex and index data is transferred only once to your graphics card!




DirectX Tutorial 14 - VertexBuffer & IndexBuffer

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:
  • Changes and VertexBuffer
          Hi, I am using the VertexBuffer but I got one q...
  • Where to set the buffers...
          Great tutorial! I do have a suggestion unless the...
  • Code for XNA 4.0
          There are some minor changes in setting up the buf...
  • Normal.Normalize() problem (C#)
          I have a problem with Normal.Normalize() method in...
  • 3D Series 1 rendering bug
          Hi! First of all ThanX to Riemer for these realy ...
  • Index-Vertex buffers for .X type models
          Hello there! I've been looking all over the net f...
  • Device does not support 32-bit index
          Hello Riemer When I ran your code I got this ex...
  • Problem with larger heightmaps
          Hello, First off, I'd like to thank you for all ...



    You can try these exercises to practice what you've learned:
  • In this code, were still keeping the vertices and indices arrays as global variables, only because we need to know their lengths in the DrawIndexesPrimitives call. So if you remember their lengths you can get rid of them and free some memore.

    The final code:
     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
         {
             public struct VertexPositionColorNormal
             {
                 public Vector3 Position;
                 public Color Color;
                 public Vector3 Normal;
     
                 public readonly static VertexDeclaration VertexDeclaration = new VertexDeclaration
                 (
                     new VertexElement(0, VertexElementFormat.Vector3, VertexElementUsage.Position, 0),
                     new VertexElement(sizeof(float) * 3, VertexElementFormat.Color, VertexElementUsage.Color, 0),
                     new VertexElement(sizeof(float) * 3 + 4, VertexElementFormat.Vector3, VertexElementUsage.Normal, 0)
                 );
             }
     
             GraphicsDeviceManager graphics;
             SpriteBatch spriteBatch;
             GraphicsDevice device;
             VertexBuffer myVertexBuffer;
             IndexBuffer myIndexBuffer;
     
             Effect effect;
             VertexPositionColorNormal[] vertices;
             Matrix viewMatrix;
             Matrix projectionMatrix;
             int[] indices;
     
             private float angle = 0f;
             private int terrainWidth = 4;
             private int terrainHeight = 3;
             private float[,] heightData;
     
             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 -- 3D Series 1";
     
                 base.Initialize();
             }
     
             protected override void LoadContent()
             {
                 spriteBatch = new SpriteBatch(GraphicsDevice);
     
                 device = graphics.GraphicsDevice;

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


                Texture2D heightMap = Content.Load<Texture2D> ("heightmap");            LoadHeightData(heightMap);
                SetUpVertices();
                SetUpIndices();
                CalculateNormals();

                 CopyToBuffers();
             }
     
             protected override void UnloadContent()
             {
             }
     
             private void SetUpVertices()
             {
                 float minHeight = float.MaxValue;
                 float maxHeight = float.MinValue;
                 for (int x = 0; x < terrainWidth; x++)
                 {
                     for (int y = 0; y < terrainHeight; y++)
                     {
                         if (heightData[x, y] < minHeight)
                             minHeight = heightData[x, y];
                         if (heightData[x, y] > maxHeight)
                             maxHeight = heightData[x, y];
                     }
                 }
     
                 vertices = new VertexPositionColorNormal[terrainWidth * terrainHeight];
                 for (int x = 0; x < terrainWidth; x++)
                 {
                     for (int y = 0; y < terrainHeight; y++)
                     {
                         vertices[x + y * terrainWidth].Position = new Vector3(x, heightData[x, y], -y);
     
                         if (heightData[x, y] < minHeight + (maxHeight - minHeight) / 4)
                             vertices[x + y * terrainWidth].Color = Color.Blue;
                         else if (heightData[x, y] < minHeight + (maxHeight - minHeight) * 2 / 4)
                             vertices[x + y * terrainWidth].Color = Color.Green;
                         else if (heightData[x, y] < minHeight + (maxHeight - minHeight) * 3 / 4)
                             vertices[x + y * terrainWidth].Color = Color.Brown;
                         else
                             vertices[x + y * terrainWidth].Color = Color.White;
                     }
                 }
             }
     
             private void SetUpIndices()
             {
                 indices = new int[(terrainWidth - 1) * (terrainHeight - 1) * 6];
                 int counter = 0;
                 for (int y = 0; y < terrainHeight - 1; y++)
                 {
                     for (int x = 0; x < terrainWidth - 1; x++)
                     {
                         int lowerLeft = x + y * terrainWidth;
                         int lowerRight = (x + 1) + y * terrainWidth;
                         int topLeft = x + (y + 1) * terrainWidth;
                         int topRight = (x + 1) + (y + 1) * terrainWidth;
     
                         indices[counter++] = topLeft;
                         indices[counter++] = lowerRight;
                         indices[counter++] = lowerLeft;
     
                         indices[counter++] = topLeft;
                         indices[counter++] = topRight;
                         indices[counter++] = lowerRight;
                     }
                 }
             }
     
             private void CalculateNormals()
             {
                 for (int i = 0; i < vertices.Length; i++)
                     vertices[i].Normal = new Vector3(0, 0, 0);
     
                 for (int i = 0; i < indices.Length / 3; i++)
                 {
                     int index1 = indices[i * 3];
                     int index2 = indices[i * 3 + 1];
                     int index3 = indices[i * 3 + 2];
     
                     Vector3 side1 = vertices[index1].Position - vertices[index3].Position;
                     Vector3 side2 = vertices[index1].Position - vertices[index2].Position;
                     Vector3 normal = Vector3.Cross(side1, side2);
     
                     vertices[index1].Normal += normal;
                     vertices[index2].Normal += normal;
                     vertices[index3].Normal += normal;
                 }
     
                 for (int i = 0; i < vertices.Length; i++)
                     vertices[i].Normal.Normalize();
             }
     
     
             private void LoadHeightData(Texture2D heightMap)
             {
                 terrainWidth = heightMap.Width;
                 terrainHeight = heightMap.Height;
     
                 Color[] heightMapColors = new Color[terrainWidth * terrainHeight];
                 heightMap.GetData(heightMapColors);
     
                 heightData = new float[terrainWidth, terrainHeight];
                 for (int x = 0; x < terrainWidth; x++)
                     for (int y = 0; y < terrainHeight; y++)
                         heightData[x, y] = heightMapColors[x + y * terrainWidth].R / 5.0f;
             }
     
             private void SetUpCamera()
             {
                 viewMatrix = Matrix.CreateLookAt(new Vector3(60, 80, -80), new Vector3(0, 0, 0), new Vector3(0, 1, 0));
                 projectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, device.Viewport.AspectRatio, 1.0f, 300.0f);
             }
     
             private void CopyToBuffers()
             {
                 myVertexBuffer = new VertexBuffer(device, VertexPositionColorNormal.VertexDeclaration, vertices.Length, BufferUsage.WriteOnly);
                 myVertexBuffer.SetData(vertices);
     
                 myIndexBuffer = new IndexBuffer(device, typeof(int), indices.Length, BufferUsage.WriteOnly);
                 myIndexBuffer.SetData(indices);
             }
     
             protected override void Update(GameTime gameTime)
             {
                 if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                     this.Exit();
     
                 KeyboardState keyState = Keyboard.GetState();
                 if (keyState.IsKeyDown(Keys.Delete))
                     angle += 0.05f;
                 if (keyState.IsKeyDown(Keys.PageDown))
                     angle -= 0.05f;
     
                 base.Update(gameTime);
             }
     
             protected override void Draw(GameTime gameTime)
             {
                 device.Clear(ClearOptions.Target | ClearOptions.DepthBuffer, Color.Black, 1.0f, 0);
     
                 RasterizerState rs = new RasterizerState();
                 rs.CullMode = CullMode.None;
                 device.RasterizerState = rs;
     
                 Matrix worldMatrix = Matrix.CreateTranslation(-terrainWidth / 2.0f, 0, terrainHeight / 2.0f) * Matrix.CreateRotationY(angle);
                 effect.CurrentTechnique = effect.Techniques["Colored"];
                 effect.Parameters["xView"].SetValue(viewMatrix);
                 effect.Parameters["xProjection"].SetValue(projectionMatrix);
                 effect.Parameters["xWorld"].SetValue(worldMatrix);
                 Vector3 lightDirection = new Vector3(1.0f, -1.0f, -1.0f);
                 lightDirection.Normalize();
                 effect.Parameters["xLightDirection"].SetValue(lightDirection);
                 effect.Parameters["xAmbient"].SetValue(0.1f);
                 effect.Parameters["xEnableLighting"].SetValue(true);            
     
                 foreach (EffectPass pass in effect.CurrentTechnique.Passes)
                 {
                     pass.Apply();
     
                     device.Indices = myIndexBuffer;
                     device.SetVertexBuffer(myVertexBuffer);
                     device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, vertices.Length, 0, indices.Length / 3);                
                 }
     
                 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 - 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

    Contents

    News
    Home
    Forum
    XNA 2.0 Recipes Book (8)
    XNA 3.0 Recipes Book (8)
    Downloads
    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
    Indices
    Terrain basics
    Terrain from file
    Keyboard
    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!