XNA for C#
DirectX 9 for C#
DirectX 9 for C++
DirectX 9 for VB
Forum
   August 27: 2D-7: Writing text
My Book: Out Now!
      
       Go to section on this site

Additional info


Latest Forum posts

 DX vs XNA vs OGL
  Posted by: Rich_Zap
  When: 28/08/2008 at 10:54:39

 understanding of SetUpIndices
  Posted by: YoYoFreakCJ
  When: 28/08/2008 at 05:51:04

 billboard does not rotate after scaling
  Posted by: kigunda
  When: 28/08/2008 at 04:55:29

 exception error
  Posted by: Rich_Zap
  When: 28/08/2008 at 03:34:29

 exception error
  Posted by: besi
  When: 28/08/2008 at 00:14:11

 Having trouble with effect file
  Posted by: Anonymous
  When: 27/08/2008 at 22:15:15

 Polygon Clipping for Octree
  Posted by: lbmurali
  When: 27/08/2008 at 18:13:55

 understanding of SetUpIndices
  Posted by: vToMy
  When: 27/08/2008 at 18:05:45

 A Typo with loading the Font
  Posted by: riemer
  When: 27/08/2008 at 15:26:43

 mistake found
  Posted by: riemer
  When: 27/08/2008 at 15:22:31


Ads

Series 4 -- Starting code

Once again, we’ll not begin from scratch, as we already seen the very basics on terrain creation in the first series. We’ll start with the code presented below, which is tightly based on the code of the first series.

Because we would otherwise get a too lengthy LoadContent method, this method now calls a LoadVertices method where we will initialize all our vertices and indices.

For now, this LoadVertices method generates the vertices and indices for a terrain. These vertices contain both positional and color information. Next, we pass them to the CalculateNormals method, which generates the correct normals and adds them to the vertices. Finally, both vertices and indices are stored in a VertexBuffer and IndexBuffer for optimal speed.

In the Draw method, they are passed to the Colored technique, which is defined in the Series4Effects.fx file which is displayed below. This file will contain our HLSL code, so we’re going to extend it throughout the following chapters. You can also download the file here.

We’ll also be using a slightly bigger heightmap, which you can download here.

So create a new project and paste the code below into the Game1.cs file. Make sure the namespace in both the Game1.cs and Program.cs files are the same! Then create a file named Series4Effects.fx, and paste the HLSL code found below into it. We’re ready to go!




DirectX Tutorial 1 - Starting code

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:
  • Intel GMA
          Hey riemer First of all, thanks for the great tut...
  • Error with effect
          In the LoadContent() the line listed below throws ...
  • Problems loading effect file
          Hi guys! Wondering why noone else already got t...
  • Error on Series4
          Hi, first of all, congratz with this tutorial. ...
  • map greater than 128 x 128
          hi. i have a problem. When i use a map greater ...
  • Setup XnaDevide is missing
          Hi riemer, just to let you know that the call to t...
  • Nothing. Just a blue screen.
          In all the other series on XNA, I can get it to wo...
  • 2-D Array vs. 1-D Array
          hi, i had this question when i was reading previou...
  • Just Blue
          I have been able to go through all of the tutorial...
  • Minor Bug?
          Hi, Ive been looking at your tutorials for xna a ...
  • Creating Heightmap
          I have been following the code here in the other t...
  • test
          testing.......


    The Game1.cs contents:

     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 XNAseries4
     {
         public struct VertexPositionNormalColored
         {
             public Vector3 Position;
             public Color Color;
             public Vector3 Normal;
     
             public static int SizeInBytes = 7 * 4;
             public static VertexElement[] VertexElements = new VertexElement[]
                  {
                      new VertexElement( 0, 0, VertexElementFormat.Vector3, VertexElementMethod.Default, VertexElementUsage.Position, 0 ),
                      new VertexElement( 0, sizeof(float) * 3, VertexElementFormat.Color, VertexElementMethod.Default, VertexElementUsage.Color, 0 ),
                      new VertexElement( 0, sizeof(float) * 4, VertexElementFormat.Vector3, VertexElementMethod.Default, VertexElementUsage.Normal, 0 ),
                  };
         }
     
         public class Game1 : Microsoft.Xna.Framework.Game
         {
             GraphicsDeviceManager graphics;
             GraphicsDevice device;
     
             int terrainWidth;
             int terrainLength;
             float[,] heightData;
     
             VertexBuffer terrainVertexBuffer;
             IndexBuffer terrainIndexBuffer;
             VertexDeclaration terrainVertexDeclaration;
                     
             Effect effect;
             Matrix viewMatrix;
             Matrix projectionMatrix;
             
             public Game1()
             {
                 graphics = new GraphicsDeviceManager(this);
                 Content.RootDirectory = "Content";
             }
     
             protected override void Initialize()
             {
                 graphics.PreferredBackBufferWidth = 500;
                 graphics.PreferredBackBufferHeight = 500;
             
                 graphics.ApplyChanges();
                 Window.Title = "Riemer's XNA Tutorials -- Series 4";
                 
                 base.Initialize();
             }
     
             protected override void LoadContent()
             {
                 device = GraphicsDevice;

                effect = Content.Load<Effect> ("Series4Effects");
                viewMatrix = Matrix.CreateLookAt(new Vector3(130, 30, -50), new Vector3(0,0,-40), new Vector3(0, 1, 0));
                projectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, device.Viewport.AspectRatio, 0.3f, 1000.0f);

                LoadVertices();
            }

            private void LoadVertices()
            {

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

                VertexPositionNormalColored[] terrainVertices = SetUpTerrainVertices();
                int[] terrainIndices = SetUpTerrainIndices();
                terrainVertices = CalculateNormals(terrainVertices, terrainIndices);
                CopyToTerrainBuffers(terrainVertices, terrainIndices);
                terrainVertexDeclaration = new VertexDeclaration(device, VertexPositionNormalColored.VertexElements);
            }

            private void LoadHeightData(Texture2D heightMap)
            {
                float minimumHeight = float.MaxValue;
                float maximumHeight = float.MinValue;

                terrainWidth = heightMap.Width;
                terrainLength = heightMap.Height;

                Color[] heightMapColors = new Color[terrainWidth * terrainLength];
                heightMap.GetData(heightMapColors);

                heightData = new float[terrainWidth, terrainLength];
                for (int x = 0; x < terrainWidth; x++)
                    for (int y = 0; y < terrainLength; y++)
                    {
                        heightData[x, y] = heightMapColors[x + y * terrainWidth].R;
                        if (heightData[x, y] < minimumHeight) minimumHeight = heightData[x, y];
                        if (heightData[x, y] > maximumHeight) maximumHeight = heightData[x, y];
                    }

                for (int x = 0; x < terrainWidth; x++)
                    for (int y = 0; y < terrainLength; y++)
                        heightData[x, y] = (heightData[x, y] - minimumHeight) / (maximumHeight - minimumHeight) * 30.0f;
            }

            private VertexPositionNormalColored[] SetUpTerrainVertices()
            {
                VertexPositionNormalColored[] terrainVertices = new VertexPositionNormalColored[terrainWidth * terrainLength];

                for (int x = 0; x < terrainWidth; x++)
                {
                    for (int y = 0; y < terrainLength; y++)
                    {
                        terrainVertices[x + y * terrainWidth].Position = new Vector3(x, heightData[x, y], -y);

                        if (heightData[x, y] < 6)
                            terrainVertices[x + y * terrainWidth].Color = Color.Blue;
                        else if (heightData[x, y] < 15)
                            terrainVertices[x + y * terrainWidth].Color = Color.Green;
                        else if (heightData[x, y] < 25)
                            terrainVertices[x + y * terrainWidth].Color = Color.Brown;
                        else
                            terrainVertices[x + y * terrainWidth].Color = Color.White;
                    }
                }

                return terrainVertices;
            }

            private int[] SetUpTerrainIndices()
            {
                int[] indices = new int[(terrainWidth - 1) * (terrainLength - 1) * 6];
                int counter = 0;
                for (int y = 0; y < terrainLength - 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;
                    }
                }

                return indices;
            }

            private VertexPositionNormalColored[] CalculateNormals(VertexPositionNormalColored[] vertices, int[] indices)
            {
                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();

                return vertices;
            }

            private void CopyToTerrainBuffers(VertexPositionNormalColored[] vertices, int[] indices)
            {
                terrainVertexBuffer = new VertexBuffer(device, vertices.Length * VertexPositionNormalColored.SizeInBytes, BufferUsage.WriteOnly);
                terrainVertexBuffer.SetData(vertices);

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

            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)
            {
                float time = (float)gameTime.TotalGameTime.TotalMilliseconds / 100.0f;
                device.RenderState.CullMode = CullMode.None;
                
                device.Clear(ClearOptions.Target | ClearOptions.DepthBuffer, Color.Black, 1.0f, 0);            
                DrawTerrain(viewMatrix);

                base.Draw(gameTime);
            }

            private void DrawTerrain(Matrix currentViewMatrix)
            {
                effect.CurrentTechnique = effect.Techniques["Colored"];
                Matrix worldMatrix = Matrix.Identity;
                effect.Parameters["xWorld"].SetValue(worldMatrix);
                effect.Parameters["xView"].SetValue(currentViewMatrix);
                effect.Parameters["xProjection"].SetValue(projectionMatrix);

                effect.Parameters["xEnableLighting"].SetValue(true);
                effect.Parameters["xAmbient"].SetValue(0.4f);
                effect.Parameters["xLightDirection"].SetValue(new Vector3(-0.5f, -1, -0.5f));

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

                    device.Vertices[0].SetSource(terrainVertexBuffer, 0, VertexPositionNormalColored.SizeInBytes);
                    device.Indices = terrainIndexBuffer;
                    device.VertexDeclaration = terrainVertexDeclaration;

                    int noVertices = terrainVertexBuffer.SizeInBytes / VertexPositionNormalColored.SizeInBytes;
                    int noTriangles = terrainIndexBuffer.SizeInBytes / sizeof(int)/3;
                    device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, noVertices, 0, noTriangles);

                    pass.End();
                }
                effect.End();
            }
        }
    }

    And the contents of the Series4Effects.fx file:

    //----------------------------------------------------
    //--                                                --
    //--             www.riemers.net                 --
    //--         Series 4: Advanced terrain             --
    //--                 Shader code                    --
    //--                                                --
    //----------------------------------------------------

    //------- Constants --------
    float4x4 xView;
    float4x4 xProjection;
    float4x4 xWorld;
    float3 xLightDirection;
    float xAmbient;
    bool xEnableLighting;

    //------- Texture Samplers --------

    Texture xTexture;

    sampler TextureSampler = sampler_state { texture = <xTexture> ; magfilter = LINEAR; minfilter = LINEAR; mipfilter=LINEAR; AddressU = mirror; AddressV = mirror;};
    //------- Technique: Colored --------
    struct ColVertexToPixel
    {
        float4 Position     : POSITION;    
        float4 Color        : COLOR0;
        float LightingFactor: TEXCOORD0;
    };

    struct ColPixelToFrame
    {
        float4 Color : COLOR0;
    };

    ColVertexToPixel ColoredVS( float4 inPos : POSITION, float4 inColor: COLOR, float3 inNormal: NORMAL)
    {    
        ColVertexToPixel Output = (ColVertexToPixel)0;
        float4x4 preViewProjection = mul (xView, xProjection);
        float4x4 preWorldViewProjection = mul (xWorld, preViewProjection);
        
        Output.Position = mul(inPos, preWorldViewProjection);
        Output.Color = inColor;
        
        float3 Normal = normalize(mul(normalize(inNormal), xWorld));    
        Output.LightingFactor = 1;
        if (xEnableLighting)
            Output.LightingFactor = saturate(dot(Normal, -xLightDirection));
        
        return Output;    
    }

    ColPixelToFrame ColoredPS(ColVertexToPixel PSIn)
    {
        ColPixelToFrame Output = (ColPixelToFrame)0;        
        
        Output.Color = PSIn.Color;
        Output.Color.rgb *= saturate(PSIn.LightingFactor + xAmbient);    
        
        return Output;
    }

    technique Colored
    {
        pass Pass0
        {
            VertexShader = compile vs_1_1 ColoredVS();
            PixelShader = compile ps_1_1 ColoredPS();
        }
    }

    //------- Technique: Textured --------
    struct TexVertexToPixel
    {
        float4 Position     : POSITION;    
        float4 Color        : COLOR0;
        float LightingFactor: TEXCOORD0;
        float2 TextureCoords: TEXCOORD1;
    };

    struct TexPixelToFrame
    {
        float4 Color : COLOR0;
    };

    TexVertexToPixel TexturedVS( float4 inPos : POSITION, float3 inNormal: NORMAL, float2 inTexCoords: TEXCOORD0)
    {    
        TexVertexToPixel Output = (TexVertexToPixel)0;
        float4x4 preViewProjection = mul (xView, xProjection);
        float4x4 preWorldViewProjection = mul (xWorld, preViewProjection);
        
        Output.Position = mul(inPos, preWorldViewProjection);    
        Output.TextureCoords = inTexCoords;
        
        float3 Normal = normalize(mul(normalize(inNormal), xWorld));    
        Output.LightingFactor = 1;
        if (xEnableLighting)
            Output.LightingFactor = saturate(dot(Normal, -xLightDirection));
        
        return Output;    
    }

    TexPixelToFrame TexturedPS(TexVertexToPixel PSIn)
    {
        TexPixelToFrame Output = (TexPixelToFrame)0;        
        
        Output.Color = tex2D(TextureSampler, PSIn.TextureCoords);
        Output.Color.rgb *= saturate(PSIn.LightingFactor + xAmbient);

        return Output;
    }

    technique Textured
    {
        pass Pass0
        {
            VertexShader = compile vs_1_1 TexturedVS();
            PixelShader = compile ps_1_1 TexturedPS();
        }
    }




    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 (145)
    XNA 2.0 using C# (74)
    2D Series: Shooters! (7)
    3D Series 1: Terrain (13)
    3D Series 2: Flightsim (14)
    3D Series 3: HLSL (18)
    3D Series 4: Adv. terrain (19)
    Starting code
    Mouse camera
    Textured terrain
    Multitexturing
    Adding detail
    Skydome
    The water technique
    Refraction map
    Reflection map
    Perfect mirror
    Ripples
    The Fresnel term
    Moving water
    Specular highlights
    Billboarding
    Region growing
    Billboarding renderstates
    Perlin noise
    Gradient skybox
    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!