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

 No download link for 2d series: shooter
  Posted by: zaboleq
  When: 07/05/2013 at 15:46:28

 Collision Class?
  Posted by: Anonymous
  When: 05/05/2013 at 19:03:59

 stack overflow
  Posted by: cityguy
  When: 07/04/2013 at 01:58:38

 Meshes looks strange.
  Posted by: ab_saratov
  When: 01/04/2013 at 04:31:08

 Lamppost Not loaded
  Posted by: Anonymous
  When: 22/03/2013 at 06:43:52

 Collision Class?
  Posted by: Da_Boom
  When: 21/03/2013 at 01:23:09

 Math boggles me
  Posted by: cityguy
  When: 17/03/2013 at 03:44:48

 Collision Class?
  Posted by: Da_Boom
  When: 16/03/2013 at 03:44:42

 Tree update
  Posted by: Anonymous
  When: 15/03/2013 at 21:11:22

 XNA 4.0
  Posted by: Anonymous
  When: 15/03/2013 at 19:43:57


Ads

Automatically generating normals

When you’re reading this chapter, you probably already know why we need our vertices to contain normals. If you don’t you can have a look at this chapter on lighting to get an idea.

However, when you define vertices yourself, or sometimes when you import them from a model, they don’t contain this normal data. So how can we create normals? For the mathematical background behind this chapter, you can have a look at the chapter in the ExtraReading section. This page contains the code.

So what we want is the ‘averaged normal in every vertex. If each vertex would be used for only one triangle, things would be very simple: we would simply need to store the normal of the triangle in each of its 4 vertices. However, if a vertex is shared by more than 1 triangle, we need to store the average of the normals of the triangles in our vertex.

The code I will show here starts from a vertexbuffer and an indexbuffer, which we will first convert to arrays of vertices and indices. So, if you have these arrays already at your disposal, you can simply leave out the first lines.

We’ll create a method, which generates the normals. It will start from a vertex- and indexbuffer:

 private void GenerateNormals(VertexBuffer vb, IndexBuffer ib)
 {
     VertexPositionNormalColored[] vertices = new VertexPositionNormalColored[WIDTH * HEIGHT];
     vb.GetData(vertices);
     int[] indices = new int[(WIDTH - 1) * (HEIGHT - 1) * 6];
     ib.GetData(indices);
 }

So the method takes in the vertex- and indexbuffer, and converts them to arrays. While doing this, we need to indicate what type of vertices are contained in the vertex buffer, and how big our arrays must be. The values taken in this example correspond to the final chapter of Series 1.

The first step would be to reset all normals in the vertex array. This is easy to do:

 for (int i = 0; i < vertices.Length; i++)
      vertices[i].Normal = new Vector3(0, 0, 0);

Next comes the main part of the method: calculating the normal. For each triangle, we calculate the normal. Then we add this normal to each of the 3 vertices of that triangle. This is how it’s done:

 for (int i = 0; i < indices.Length / 3; i++)
 {
      Vector3 firstvec = vertices[indices[i*3+1]].Position-vertices[indices[i*3]].Position;
      Vector3 secondvec = vertices[indices[i*3]].Position-vertices[indices[i*3+2]].Position;
      Vector3 normal = Vector3.Cross(firstvec, secondvec);
      normal.Normalize();
      vertices[indices[i * 3]].Normal += normal;
      vertices[indices[i * 3 + 1]].Normal += normal;
      vertices[indices[i * 3 + 2]].Normal += normal;
 }

The indexbuffer contains the order in which vertices are used to create the triangles. Because our indexbuffer describes a trianglelist, the number of triangles equals the length of our indexbuffer divided by 3.

For each triangle, we calculate its normal, by taking the cross product of the 2 planar vectors, and normalizing the result (this is explained in more detail in the ExtreReading section). Note: a ‘normal’ is the vector perpendicular to the triangle, while ‘normalizing a vector’ makes the length exactly 1.

When we have the normal of the triangle, we add it to the normals of the 3 vertices of that triangle. When we do this for each triangle, the resulting normals in each vertex will have the correct direction, but they will be too large, so we need to normalize again:

 for (int i = 0; i < vertices.Length; i++)
     vertices[i].Normal.Normalize();

That’s it! Now each vertex our vertex array contains a normal, which is the average of the normals of all triangles using that vertex. In the case you want to update your vertex-and indexbuffers, you’ll only have to update your vertexbuffer, as nothing has changed to the indices:

 vb.SetData(vertices);

This concludes the code on normal generation. As a sidenote, I should mention that if you have created your arrays from buffers, these buffers must not have been created with the ResourceUsage.WriteOnly flag specified, because in the first lines of our code we’re reading data from our buffers.




DirectX Tutorial 12 - Normal generation

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:
  • Gradient
          Hi All, Is there a function in the system anywh...
  • Generics?
          You might want to make this more flexible for othe...
  • Problem Generating Normals
          Hi, I am getting the following error: An unhan...
  • Code Generate Normals
          Riemers, I have a problem understanding your c...
  • Missing the tuts
          In the XNA Short Tuts for "Automatically generati...


    Here’s the code, with the method integrated into the terrain of our first series:

     using System;
     using System.Collections.Generic;
     using Microsoft.Xna.Framework;
     using Microsoft.Xna.Framework.Audio;
     using Microsoft.Xna.Framework.Content;
     using Microsoft.Xna.Framework.Graphics;
     using Microsoft.Xna.Framework.Input;
     using Microsoft.Xna.Framework.Storage;
     using System.IO;
     
     namespace XNAtutorial
     {
         public class Game1 : Microsoft.Xna.Framework.Game
         {
             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 ),
                  };
             }
     
             GraphicsDeviceManager graphics;
             ContentManager content;
             GraphicsDevice device;
             Effect effect;
             VertexPositionNormalColored[] vertices;
             private float angle = 0f;
             private VertexBuffer vb;
             private IndexBuffer ib;
             private int WIDTH = 64;
             private int HEIGHT = 64;
             private int[,] heightData;
     
             private int MinimumHeight = 255;
             private int MaximumHeight = 0;
     
     
             public Game1()
             {
                 graphics = new GraphicsDeviceManager(this);
                 content = new ContentManager(Services);
             }
     
             protected override void Initialize()
             {
                 base.Initialize();
                 LoadHeightData();
                 SetUpXNADevice();
                 SetUpVertices();
                 SetUpIndices();
                 GenerateNormals(vb, ib);
                 SetUpCamera();
             }
     
             private void SetUpXNADevice()
             {
                 device = graphics.GraphicsDevice;
     
                 graphics.PreferredBackBufferWidth = 500;
                 graphics.PreferredBackBufferHeight = 500;
                 graphics.IsFullScreen = false;
                 graphics.ApplyChanges();
                 Window.Title = "Riemer's XNA Tutorials -- Series 1";
     
                 CompiledEffect compiledEffect = Effect.CompileEffectFromFile("@/../../../../effects.fx", null, null, CompilerOptions.None, TargetPlatform.Windows);
                 effect = new Effect(graphics.GraphicsDevice, compiledEffect.GetEffectCode(), CompilerOptions.None, null);
             }
     
             private void SetUpVertices()
             {
     
                 vertices = new VertexPositionNormalColored[WIDTH * HEIGHT];
     
                 for (int x = 0; x < WIDTH; x++)
                 {
                     for (int y = 0; y < HEIGHT; y++)
                     {
                         vertices[x + y * WIDTH].Position = new Vector3(x, y, heightData[x, y]);
     
                         vertices[x + y * WIDTH].Normal = new Vector3(0, 0, 1);
     
     
                         if (heightData[x, y] < MinimumHeight + (MaximumHeight - MinimumHeight) / 4)
                         {
                             vertices[x + y * WIDTH].Color = Color.Blue;
                         }
                         else if (heightData[x, y] < MinimumHeight + (MaximumHeight - MinimumHeight) * 2 / 4)
                         {
                             vertices[x + y * WIDTH].Color = Color.Green;
                         }
                         else if (heightData[x, y] < MinimumHeight + (MaximumHeight - MinimumHeight) * 3 / 4)
                         {
                             vertices[x + y * WIDTH].Color = Color.Brown;
                         }
                         else
                         {
                             vertices[x + y * WIDTH].Color = Color.White;
                         }
                     }
                 }
     
                 vb = new VertexBuffer(device, VertexPositionNormalColored.SizeInBytes * WIDTH * HEIGHT, ResourceUsage.None, ResourceManagementMode.Automatic);
                 vb.SetData(vertices);
             }
     
             private void SetUpIndices()
             {
                 int[] indices = new int[(WIDTH - 1) * (HEIGHT - 1) * 6];
                 for (int x = 0; x < WIDTH - 1; x++)
                 {
                     for (int y = 0; y < HEIGHT - 1; y++)
                     {
                         indices[(x + y * (WIDTH - 1)) * 6] = (x + 1) + (y + 1) * WIDTH;
                         indices[(x + y * (WIDTH - 1)) * 6 + 1] = (x + 1) + y * WIDTH;
                         indices[(x + y * (WIDTH - 1)) * 6 + 2] = x + y * WIDTH;
     
                         indices[(x + y * (WIDTH - 1)) * 6 + 3] = (x + 1) + (y + 1) * WIDTH;
                         indices[(x + y * (WIDTH - 1)) * 6 + 4] = x + y * WIDTH;
                         indices[(x + y * (WIDTH - 1)) * 6 + 5] = x + (y + 1) * WIDTH;
                     }
                 }
     
                 ib = new IndexBuffer(device, typeof(int), (WIDTH - 1) * (HEIGHT - 1) * 6, ResourceUsage.None, ResourceManagementMode.Automatic);
                 ib.SetData(indices);
             }
     
             private void GenerateNormals(VertexBuffer vb, IndexBuffer ib)
             {
                 VertexPositionNormalColored[] vertices = new VertexPositionNormalColored[WIDTH * HEIGHT];
                 vb.GetData(vertices);
                 int[] indices = new int[(WIDTH - 1) * (HEIGHT - 1) * 6];
                 ib.GetData(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++)
                 {
                     Vector3 firstvec = vertices[indices[i*3+1]].Position-vertices[indices[i*3]].Position;
                     Vector3 secondvec = vertices[indices[i*3]].Position-vertices[indices[i*3+2]].Position;
                     Vector3 normal = Vector3.Cross(firstvec, secondvec);
                     normal.Normalize();
                     vertices[indices[i * 3]].Normal += normal;
                     vertices[indices[i * 3 + 1]].Normal += normal;
                     vertices[indices[i * 3 + 2]].Normal += normal;
                 }
     
                 for (int i = 0; i < vertices.Length; i++)
                     vertices[i].Normal.Normalize();
     
                 vb.SetData(vertices);
             }
     
             private void SetUpCamera()
             {
                 Matrix viewMatrix = Matrix.CreateLookAt(new Vector3(80, 0, 160), new Vector3(-20, 0, 0), new Vector3(0, 0, 1));
                 Matrix projectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, this.Window.ClientBounds.Width / this.Window.ClientBounds.Height, 1.0f, 250.0f);
     
                 effect.Parameters["xView"].SetValue(viewMatrix);
                 effect.Parameters["xProjection"].SetValue(projectionMatrix);
                 effect.Parameters["xWorld"].SetValue(Matrix.Identity);
             }
     
             private void LoadHeightData()
             {
                 int offset;
                 FileStream fs = new FileStream("../../../heightmap.bmp", FileMode.Open, FileAccess.Read);
                 BinaryReader r = new BinaryReader(fs);
     
                 r.BaseStream.Seek(10, SeekOrigin.Current);
                 offset = (int)r.ReadUInt32();
     
                 r.BaseStream.Seek(4, SeekOrigin.Current);
                 WIDTH = (int)r.ReadUInt32();
                 HEIGHT = (int)r.ReadUInt32();
     
                 r.BaseStream.Seek(offset - 26, SeekOrigin.Current);
                 heightData = new int[WIDTH, HEIGHT];
                 for (int i = 0; i < HEIGHT; i++)
                 {
                     for (int y = 0; y < WIDTH; y++)
                     {
                         int height = (int)(r.ReadByte());
                         height += (int)(r.ReadByte());
                         height += (int)(r.ReadByte());
                         height /= 8;
                         heightData[WIDTH - 1 - y, HEIGHT - 1 - i] = height;
     
                         if (height < MinimumHeight)
                         {
                             MinimumHeight = height;
                         }
                         if (height > MaximumHeight)
                         {
                             MaximumHeight = height;
                         }
     
                     }
                 }
             }
     
             protected override void LoadGraphicsContent(bool loadAllContent)
             {
                 if (loadAllContent)
                 {
                 }
             }
     
             protected override void UnloadGraphicsContent(bool unloadAllContent)
             {
                 if (unloadAllContent == true)
                 {
                     content.Unload();
                 }
             }
     
             protected override void Update(GameTime gameTime)
             {
                 if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                     this.Exit();
                 base.Update(gameTime);
     
                 ProcessKeyboard();
             }
     
             private void ProcessKeyboard()
             {
                 KeyboardState keys = Keyboard.GetState();
                 if (keys.IsKeyDown(Keys.Delete))
                 {
                     angle += 0.05f;
                 }
                 if (keys.IsKeyDown(Keys.PageDown))
                 {
                     angle -= 0.05f;
                 }
             }
     
             protected override void Draw(GameTime gameTime)
             {
                 device.RenderState.FillMode = FillMode.Solid;
                 device.RenderState.CullMode = CullMode.None;
     
                 device.Clear(ClearOptions.Target | ClearOptions.DepthBuffer, Color.Black, 1.0f, 0);
     
                 effect.CurrentTechnique = effect.Techniques["Colored"];
                 Matrix worldMatrix = Matrix.CreateTranslation(-HEIGHT / 2, -WIDTH / 2, 0) * Matrix.CreateRotationZ(angle); ;
                 effect.Parameters["xWorld"].SetValue(worldMatrix);
     
                 effect.Parameters["xEnableLighting"].SetValue(true);
                 effect.Parameters["xLightDirection"].SetValue(new Vector3(0f, 0.4f, -0.7f));
     
                 effect.Begin();
                 foreach (EffectPass pass in effect.CurrentTechnique.Passes)
                 {
                     pass.Begin();
     
     
                     device.Vertices[0].SetSource(vb, 0, VertexPositionNormalColored.SizeInBytes);
                     device.Indices = ib;
                     device.VertexDeclaration = new VertexDeclaration(device, VertexPositionNormalColored.VertexElements);
     
                     device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, WIDTH * HEIGHT, 0, (WIDTH - 1) * (HEIGHT - 1) * 2);
     
                     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 - 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)
    Chapter 1
    Chapter 2
    Chapter 3
    Chapter 4
    Chapter 5
    Chapter 6
    Chapter 7
    Chapter 8
    XNA 3.0 Recipes Book (8)
    Chapter 1
    Chapter 2
    Chapter 3
    Chapter 4
    Chapter 5
    Chapter 6
    Chapter 7
    Chapter 8
    Downloads
    Extra Reading (3)
    Matrices: geometrical
    Matrix Mathematics
    Homogenous matrices
    Community Projects (1)
    Team Project (1)
    News
    Tutorials (160)
    XNA 4.0 using C# (89)
    2D Series: Shooters (22)
    Starting a project
    Drawing fullscreen images
    Positioning images
    SpriteBatch.Draw()
    Rotation
    Keyboard input
    Writing text
    Angle to Direction
    Direction to Angle
    Smoke trail
    Manual texture creation
    Random terrain
    Texture to Colors
    Coll Detection Overview
    Coll Detection Matrices
    Putting CD into practice
    Particles
    Additive alpha blending
    Particle engine
    Adding craters
    Sound in XNA
    Resolution independency
    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)
    Starting point
    Textures
    Loading the floorplan
    Creating the 3D city
    Loading a Model
    Ambient and diffuse
    Quaternion camera
    Flight kinematics
    Collision detection
    Adding targets
    Point sprites
    Alpha blending
    Skybox
    Camera delay
    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
    Preshaders
    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)
    Run XNA on older pcs
    MessageBox in XNA
    Normal generation
    DirectX using C# (54)
    Series 1:Terrain (14)
    Opening a window
    Linking to the Device
    Drawing a triangle
    Camera
    Rotation - Translation
    Indices
    Terrain creation
    Terrain from file
    DirectInput
    Importing bmp files
    Colored vertices
    DirectX Light basics
    Mesh creation
    Mesh lighting
    Series 2: Flightsim (19)
    Starting code
    Textures
    The floorplan
    Creating the 3D City
    Meshloading from file
    Ambient light
    Action
    Flight kinematics
    Collision detection
    Skybox
    Texture filtering
    Adding targets
    Point sprites
    Alpha blending
    DirectSound
    Sounds in 3D
    Playing MP3 files
    Displaying text
    Going fullscreen
    Series 3: HLSL (19)
    Starting point
    HLSL Introduction
    Vertex Shader
    Shaded triangle
    Pixel Shader
    Textured Triangle
    Triangle Strip
    World transform
    Adding normals
    The first light
    Shadow mapping
    Render To Texture
    Projective texturing
    The first shadow
    Shaping the light
    Preshaders
    Multiple lights
    Adjusting Z values
    Finishing touch
    Short Tuts (2)
    Resizing problem
    Checking Device caps
    DirectX using C++ (15)
    Series 1: Terrain (15)
    Opening a window
    Ending the game loop
    Linking to the Device
    Clearing your window
    Drawing a triangle
    Culling
    Camera
    Rotation - Translation
    Indices
    Terrain creation
    Terrain from file
    DirectInput
    Importing .bmp files
    Adding colors
    DirectX Light basics
    DirectX using VB (2)
    Series 1: Intro (2)
    The first triangle
    Rotation - translation
    -- Tree view --


    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!