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

Adding normals – Triangle Strip revisited

Before we can start defining our own lights, we need to add normals to every vertex. If you’re wondering why, take a quick look at the “DirectX Light Basics” chapter of Series 1.

The normal has to be included in the vertex stream, so let’s first redefine the myownvertexformat structure:

 struct myownvertexformat
 {
     public Vector3 Pos;
     public Vector3 Normal;
     public Vector2 TexCoord;
 
     public myownvertexformat(Vector3 _Pos, Vector3 _Normal, float texx, float texy)
     {
         Pos = _Pos;
         Normal = _Normal;
         TexCoord.X = texx;
         TexCoord.Y = texy;
     }
 }

Now it’s time to expand our vertices with normal data. It’s very easy: the normals of the horizontal quads (such as the street) are pointing upward, and the normals of the vertical quads (such as the wall) are pointing to the left, this is in the positive X direction in our case.

However, because we’re using a triangle strip, some vertices are shared by 2 triangles that should have a different normal! This is a problem of a triangle strip, and because sooner or later you’ll run into problems like this, I’ll cover it here.

One solution would be to give the shared vertices the interpolated normal. This would however give bad results, as we want to clearly see the edges.

A better approach would be to add ‘ghost triangles’. Here we simply add 2 new vertices at the places where 2 triangles with different normals share a side. In the image below, these sides are indicated by a red line, and the extra vertices are also red. Note that vertices 4 and 5 should also be drawn in red, but they didn’t fit into the image :). Note that the coordinates of the red vertices are exactly the same as those of the previous blue vertices, only the normals should be different.



As you can see, we’ll be defining 18 vertices, defining 10 normal triangles and 6 ghost triangles. Even in this almost-worst-case example, we still have to store 12 vertices less than when we would have stored them in a Triangle List.

First update your Vertex Buffer so it can store 18 vertices:

 vb = new VertexBuffer(typeof(myownvertexformat), 18, device, Usage.WriteOnly, VertexFormats.Position | VertexFormats.Normal | VertexFormats.Texture0, Pool.Managed);

And finally your vertex buffer:

 myownvertexformat[] vertices = new myownvertexformat[18];
 
 vertices[0] = new myownvertexformat(new Vector3(20, -10, 0), new Vector3(0, 0, 1), -0.25f, 25.0f);
 vertices[1] = new myownvertexformat(new Vector3(20, 100, 0), new Vector3(0, 0, 1), -0.25f, 0.0f);
 vertices[2] = new myownvertexformat(new Vector3(-2, -10, 0), new Vector3(0, 0, 1), 0.25f, 25.0f);
 vertices[3] = new myownvertexformat(new Vector3(-2, 100, 0), new Vector3(0, 0, 1), 0.25f, 0.0f);
 vertices[4] = new myownvertexformat(new Vector3(-2, -10, 0), new Vector3(1, 0, 0), 0.25f, 25.0f);
 vertices[5] = new myownvertexformat(new Vector3(-2, 100, 0), new Vector3(1, 0, 0), 0.25f, 0.0f);
 vertices[6] = new myownvertexformat(new Vector3(-2, -10, 1), new Vector3(1, 0, 0), 0.375f, 25.0f);
 vertices[7] = new myownvertexformat(new Vector3(-2, 100, 1), new Vector3(1, 0, 0), 0.375f, 0.0f);
 vertices[8] = new myownvertexformat(new Vector3(-2, -10, 1), new Vector3(0, 0, 1), 0.375f, 25.0f);
 vertices[9] = new myownvertexformat(new Vector3(-2, 100, 1), new Vector3(0, 0, 1), 0.375f, 0.0f);
 vertices[10] = new myownvertexformat(new Vector3(-3, -10, 1), new Vector3(0, 0, 1), 0.5f, 25.0f);
 vertices[11] = new myownvertexformat(new Vector3(-3, 100, 1), new Vector3(0, 0, 1), 0.5f, 0.0f);
 vertices[12] = new myownvertexformat(new Vector3(-13, -10, 1), new Vector3(0, 0, 1), 0.75f, 25.0f);
 vertices[13] = new myownvertexformat(new Vector3(-13, 100, 1), new Vector3(0, 0, 1), 0.75f, 0.0f);
 vertices[14] = new myownvertexformat(new Vector3(-13, -10, 1), new Vector3(1, 0, 0), 0.75f, 25.0f);
 vertices[15] = new myownvertexformat(new Vector3(-13, 100, 1), new Vector3(1, 0, 0), 0.75f, 0.0f);
 vertices[16] = new myownvertexformat(new Vector3(-13, -10, 21), new Vector3(1, 0, 0), 1.25f, 25.0f);
 vertices[17] = new myownvertexformat(new Vector3(-13, 100, 21), new Vector3(1, 0, 0), 1.25f, 0.0f);
             
 vb.SetData(vertices, 0, LockFlags.None);

Where you should notice the 6 added vertices, as well as the normal data. Don’t forget to update the number of triangles you’re about to draw:

 device.DrawPrimitives(PrimitiveType.TriangleStrip, 0, 16);

Now run this code! You see something rather strange: although the shape of your scene is correct, the textures have disappeared! Before reading the next paragraph, try to find out yourself what’s wrong.

What happened? We added normal data, and put it after the 3D position and before the texture coordinates. Because we haven’t yet updated our VertexDeclaration, our HLSL uses the normal data as its texture coordinates!

So we need to update our VertexDeclaration. Replace it by this code:

 VertexElement[] velements = new VertexElement[]
 {
     new VertexElement(0, 0, DeclarationType.Float3, DeclarationMethod.Default, DeclarationUsage.Position, 0),
     new VertexElement(0, 12, DeclarationType.Float3, DeclarationMethod.Default, DeclarationUsage.Normal, 0),
     new VertexElement(0, 24, DeclarationType.Float2, DeclarationMethod.Default, DeclarationUsage.TextureCoordinate, 0),
     VertexElement.VertexDeclarationEnd
 };

You see we’ve included an entry for the normal data. This normal data follows after the 12 bytes of the 3D position. Because our normal data also occupies 12 bytes, the texture data will start at byte 24.

When you run this code, you should see the same image as last chapter. This time, however, our vertex stream includes the correct normal data, so we’re finally ready to define our first light!




DirectX Tutorial 9 - Adding normals

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!



So far in the Series, you’ve seen 3 types of self-made vertex formats, and you’ve changed your VertexDeclaration 3 times as well. By now, I hope you get the importance of these concepts, and understand how the vertex buffer is linked to your vertex shader using the VertexDeclaration.

The HLSL hasn’t been changed this chapter:

struct VertexToPixel
{
    float4 Position     : POSITION;
    float2 TexCoords    : TEXCOORD0;
};

struct PixelToFrame
{
    float4 Color : COLOR0;
};

float4x4 xWorldViewProjection;

Texture xColoredTexture;

sampler ColoredTextureSampler = sampler_state { texture = <xColoredTexture> ; magfilter = LINEAR; minfilter = LINEAR; mipfilter=LINEAR; AddressU = mirror; AddressV = mirror;};
VertexToPixel SimplestVertexShader( float4 inPos : POSITION, float2 inTexCoords : TEXCOORD0)
{
    VertexToPixel Output = (VertexToPixel)0;
    
    Output.Position = mul(inPos, xWorldViewProjection);
    Output.TexCoords = inTexCoords;
    
    return Output;    
}

PixelToFrame OurFirstPixelShader(VertexToPixel PSIn)
{
    PixelToFrame Output = (PixelToFrame)0;

    Output.Color = tex2D(ColoredTextureSampler, PSIn.TexCoords);

    return Output;
}

technique Simplest
{
    pass Pass0
    {
        VertexShader = compile vs_1_1 SimplestVertexShader();
        PixelShader = compile ps_1_1 OurFirstPixelShader();
    }
}

The DirectX code:

 using System;
 using System.Drawing;
 using System.Collections;
 using System.ComponentModel;
 using System.Windows.Forms;
 using System.Data;
 using Microsoft.DirectX;
 using Microsoft.DirectX.Direct3D;
 using D3D = Microsoft.DirectX.Direct3D;
 
 namespace DirectX_Tutorial
 {
     struct myownvertexformat
     {
         public Vector3 Pos;
         public Vector3 Normal;
         public Vector2 TexCoord;
 
 
         public myownvertexformat(Vector3 _Pos, Vector3 _Normal, float texx, float texy)
         {
             Pos = _Pos;
             Normal = _Normal;
             TexCoord.X = texx;
             TexCoord.Y = texy;
         }
     }
 
     public class WinForm : System.Windows.Forms.Form
     {        
         private System.ComponentModel.Container components = null;
         private D3D.Device device;        
         private VertexBuffer vb;
         private Vector3 CameraPos;
         private VertexDeclaration vd;
         private Effect effect;
 
         private Texture StreetTexture;
         private Mesh Lamppost;
         private Material[] LamppostMaterials;
         private Texture[] LamppostTextures;
         private Mesh Car;
         private Material[] CarMaterials;
         private Texture[] CarTextures;
 
         private Matrix matView;
         private Matrix matProjection;
 
         private int LastTickCount = 1;
         private int Frames = 0;
         private float LastFrameRate = 0;
         private D3D.Font text;
 
         public WinForm()
         {
             InitializeComponent();
             this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque, true);
         }
 
         public void InitializeDevice()
         {
             PresentParameters presentParams = new PresentParameters();
             presentParams.Windowed = true;
             presentParams.SwapEffect = SwapEffect.Discard;
             presentParams.AutoDepthStencilFormat = DepthFormat.D16;
             presentParams.EnableAutoDepthStencil = true;
 
             Caps DevCaps = D3D.Manager.GetDeviceCaps(0, D3D.DeviceType.Hardware);
             D3D.DeviceType DevType = D3D.DeviceType.Reference;
             CreateFlags DevFlags = CreateFlags.SoftwareVertexProcessing;
             if ((DevCaps.VertexShaderVersion >= new Version(2, 0)) && (DevCaps.PixelShaderVersion >= new Version(2, 0)))
             {
                 DevType = D3D.DeviceType.Hardware;                
                 if (DevCaps.DeviceCaps.SupportsHardwareTransformAndLight)
                 {
                     DevFlags = CreateFlags.HardwareVertexProcessing;
                     if (DevCaps.DeviceCaps.SupportsPureDevice)
                     {
                         DevFlags |= CreateFlags.PureDevice;
                     }
                 }                
             }
 
             device = new D3D.Device(0, DevType, this, DevFlags, presentParams);
             device.DeviceReset += new EventHandler(this.HandleDeviceReset);            
         }
 
         private void HandleDeviceReset(object sender, EventArgs e)
         {
             FillResources();        
         }
 
         private void AllocateResources()
         {
             vb = new VertexBuffer(typeof(myownvertexformat), 18, device, Usage.WriteOnly, VertexFormats.Position | VertexFormats.Normal | VertexFormats.Texture0, Pool.Managed);                        
             InitializeFont();
             effect = D3D.Effect.FromFile(device, @"../../OurHLSLFile.fx", null, null, ShaderFlags.None, null);
         }
 
         private void FillResources()
         {
             myownvertexformat[] vertices = new myownvertexformat[18];
 
             vertices[0] = new myownvertexformat(new Vector3(20, -10, 0), new Vector3(0, 0, 1), -0.25f, 25.0f);
             vertices[1] = new myownvertexformat(new Vector3(20, 100, 0), new Vector3(0, 0, 1), -0.25f, 0.0f);
             vertices[2] = new myownvertexformat(new Vector3(-2, -10, 0), new Vector3(0, 0, 1), 0.25f, 25.0f);                        
             vertices[3] = new myownvertexformat(new Vector3(-2, 100, 0), new Vector3(0, 0, 1), 0.25f, 0.0f);
             vertices[4] = new myownvertexformat(new Vector3(-2, -10, 0), new Vector3(1, 0, 0), 0.25f, 25.0f);                        
             vertices[5] = new myownvertexformat(new Vector3(-2, 100, 0), new Vector3(1, 0, 0), 0.25f, 0.0f);
             vertices[6] = new myownvertexformat(new Vector3(-2, -10, 1), new Vector3(1, 0, 0), 0.375f, 25.0f);
             vertices[7] = new myownvertexformat(new Vector3(-2, 100, 1), new Vector3(1, 0, 0), 0.375f, 0.0f);
             vertices[8] = new myownvertexformat(new Vector3(-2, -10, 1), new Vector3(0, 0, 1), 0.375f, 25.0f);
             vertices[9] = new myownvertexformat(new Vector3(-2, 100, 1), new Vector3(0, 0, 1), 0.375f, 0.0f);
             vertices[10] = new myownvertexformat(new Vector3(-3, -10, 1), new Vector3(0, 0, 1), 0.5f, 25.0f);
             vertices[11] = new myownvertexformat(new Vector3(-3, 100, 1), new Vector3(0, 0, 1), 0.5f, 0.0f);
             vertices[12] = new myownvertexformat(new Vector3(-13, -10, 1), new Vector3(0, 0, 1), 0.75f, 25.0f);
             vertices[13] = new myownvertexformat(new Vector3(-13, 100, 1), new Vector3(0, 0, 1), 0.75f, 0.0f);
             vertices[14] = new myownvertexformat(new Vector3(-13, -10, 1), new Vector3(1, 0, 0), 0.75f, 25.0f);
             vertices[15] = new myownvertexformat(new Vector3(-13, 100, 1), new Vector3(1, 0, 0), 0.75f, 0.0f);
             vertices[16] = new myownvertexformat(new Vector3(-13, -10, 21), new Vector3(1, 0, 0), 1.25f, 25.0f);
             vertices[17] = new myownvertexformat(new Vector3(-13, 100, 21), new Vector3(1, 0, 0), 1.25f, 0.0f);
             
             vb.SetData(vertices, 0, LockFlags.None);
 
             SetUpCamera();
 
             VertexElement[] velements = new VertexElement[]
             {
                 new VertexElement(0, 0, DeclarationType.Float3, DeclarationMethod.Default, DeclarationUsage.Position, 0),
                 new VertexElement(0, 12, DeclarationType.Float3, DeclarationMethod.Default, DeclarationUsage.Normal, 0),
                 new VertexElement(0, 24, DeclarationType.Float2, DeclarationMethod.Default, DeclarationUsage.TextureCoordinate, 0),
                 VertexElement.VertexDeclarationEnd
             };
             vd = new VertexDeclaration(device, velements);
 
             StreetTexture = TextureLoader.FromFile(device, "streettexture.jpg");
 
             LoadMesh("lamppost.x", ref Lamppost, ref LamppostMaterials, ref LamppostTextures);
             LoadMesh("car.x", ref Car, ref CarMaterials, ref CarTextures);
         }
 
         private void InitializeFont()
         {
             System.Drawing.Font systemfont = new System.Drawing.Font("Arial", 12f, FontStyle.Regular);
             text = new D3D.Font(device, systemfont);
         }
 
         private void DrawMesh(Mesh mesh, Material[] meshmaterials, Texture[] meshtextures)
         {
             for (int i = 0; i < meshmaterials.Length; i++)
             {
                 if (meshtextures.Length > 3) effect.SetValue("xColoredTexture", meshtextures[i]);
                 effect.CommitChanges();
                 mesh.DrawSubset(i);
             }
         }
 
         protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
         {
             device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black, 1.0f, 0);
             device.BeginScene();
 
             device.SetStreamSource(0, vb, 0);            
             device.VertexDeclaration = vd;
             effect.Technique = "Simplest";
             effect.SetValue("xWorldViewProjection", Matrix.Identity * matView * matProjection);
             effect.SetValue("xColoredTexture", StreetTexture);
             int numpasses = effect.Begin(0);
             for (int i = 0; i < numpasses; i++)
             {
                 effect.BeginPass(i);
 
                 device.DrawPrimitives(PrimitiveType.TriangleStrip, 0, 16);
 
                 Matrix LamppostWorld = Matrix.Scaling(0.05f, 0.05f, 0.05f) * Matrix.RotationX((float)Math.PI / 2) * Matrix.Translation(-4.0f, 5, 1);
                 effect.SetValue("xWorldViewProjection", LamppostWorld * matView * matProjection);
                 DrawMesh(Lamppost, LamppostMaterials, LamppostTextures);
 
                 LamppostWorld = Matrix.Scaling(0.05f, 0.05f, 0.05f) * Matrix.RotationX((float)Math.PI / 2) * Matrix.Translation(-4.0f, 35, 1);
                 effect.SetValue("xWorldViewProjection", LamppostWorld * matView * matProjection);
                 DrawMesh(Lamppost, LamppostMaterials, LamppostTextures);
 
                 Matrix CarWorld = Matrix.Scaling(4f, 4f, 4f) * Matrix.RotationYawPitchRoll((float)Math.PI / 2, (float)Math.PI / 2, (float)Math.PI / 2) * Matrix.Translation(3, 15, 0f);
                 effect.SetValue("xWorldViewProjection", CarWorld * matView * matProjection);
                 DrawMesh(Car, CarMaterials, CarTextures);
 
                 CarWorld = Matrix.Scaling(4f, 4f, 4f) * Matrix.RotationYawPitchRoll((float)Math.PI / 2, (float)Math.PI / 8, (float)Math.PI / 2) * Matrix.Translation(28, -1.9f, 0f);
                 effect.SetValue("xWorldViewProjection", CarWorld * matView * matProjection);
                 DrawMesh(Car, CarMaterials, CarTextures);
 
                 effect.EndPass();
             }
             effect.End();
 
             UpdateFramerate();
 
             device.EndScene();
             device.Present();
             this.Invalidate();    
         }
 
         private void UpdateFramerate()
         {
             Frames++;
             if (Math.Abs(Environment.TickCount - LastTickCount) > 1000)
             {
                 LastFrameRate = (float)Frames * 1000 / Math.Abs(Environment.TickCount - LastTickCount);
                 LastTickCount = Environment.TickCount;
                 Frames = 0;
             }
             text.DrawText(null, string.Format("Framerate : {0:0.00} fps", LastFrameRate), new Point(10, 430), Color.Red);
         }
 
         private void SetUpCamera()
         {
             CameraPos = new Vector3(25, -18, 13);
             matProjection = Matrix.PerspectiveFovLH((float)Math.PI / 4, this.Width / this.Height, 0.3f, 200f);
             matView = Matrix.LookAtLH(CameraPos, new Vector3(0, 12, 2), new Vector3(0, 0, 1));
         }
 
         private void LoadMesh(string filename, ref Mesh mesh, ref Material[] meshmaterials, ref Texture[] meshtextures)
         {
             ExtendedMaterial[] materialarray;
             GraphicsStream adj = null;
 
             mesh = Mesh.FromFile(filename, MeshFlags.Managed, device, out adj, out materialarray);
 
             if ((materialarray != null) && (materialarray.Length > 0))
             {
                 meshmaterials = new Material[materialarray.Length];
                 meshtextures = new Texture[materialarray.Length];
 
                 for (int i = 0; i < materialarray.Length; i++)
                 {
                     meshmaterials[i] = materialarray[i].Material3D;
                     meshmaterials[i].Ambient = meshmaterials[i].Diffuse;
 
                     if ((materialarray[i].TextureFilename != null) && (materialarray[i].TextureFilename != string.Empty))
                     {
                         meshtextures[i] = TextureLoader.FromFile(device, materialarray[i].TextureFilename);
                     }
                 }                
             }
 
             mesh = mesh.Clone(mesh.Options.Value, CustomVertex.PositionNormalTextured.Format, device);
             mesh.ComputeNormals();
         }
 
         protected override void Dispose(bool disposing)
         {
             if (disposing)
             {
                 if (components != null)
                 {
                     components.Dispose();
                 }
             }
             base.Dispose(disposing);
         }
 
         private void InitializeComponent()
         {
             this.components = new System.ComponentModel.Container();
             this.Size = new System.Drawing.Size(500, 500);
             this.Text = "Riemer's DirectX & HLSL Tutorial using C# -- Season 3";
         }
 
         static void Main()
         {
             using (WinForm our_directx_form = new WinForm())
             {
                 our_directx_form.InitializeDevice();                
                 our_directx_form.AllocateResources();
                 our_directx_form.FillResources();
                 Application.Run(our_directx_form);
             }
         }
     }
 }


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)
DirectX using C# (54)
Series 1:Terrain (14)
Series 2: Flightsim (19)
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)
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!