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

 Tutorial 3 for Windows Phone 7
  Posted by: Anonymous
  When: 19/05/2013 at 11:27:50

 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


Ads

Adding lighting to our city

This will be a pretty short chapter, as we’ve seen most of lighting in the first Series.

As a first type of lighting, we’re simply going to use 2 directional lights, such as we’ve used in Series 1. Simply find the light where you turned off lights, and replace it with this code:

 device.RenderState.Lighting = true;
 
 device.Lights[0].Type = LightType.Directional;
 device.Lights[0].Diffuse = Color.White;
 device.Lights[0].Direction = new Vector3(1, 1, -1);
 device.Lights[0].Update();
 device.Lights[0].Enabled = true;
 
 device.Lights[1].Type = LightType.Directional;
 device.Lights[1].Diffuse = Color.White;
 device.Lights[1].Direction = new Vector3(-1, -1, -1);
 device.Lights[1].Update();
 device.Lights[1].Enabled = true;

Try running this code. First of all, you should notice our airplane is being lit nicely now. The rest of the city, however, still could use some more light. To achieve this, simply put this line in your OnPaint method, immediately before you draw your city:

 device.RenderState.Ambient = Color.DarkGray;

This will cause some light to be present throughout the whole 3D city. The reason we put the line here, is that later on we’ll have to draw objects that require fewer of more ambient light.




DirectX Tutorial 6 - Ambient light

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!



Although it might be possible to achieve better-looking lighting by experiencing with other types of lights, don’t bother. This because up till now we’ve been using the ‘fixed-function pipeline’, which is composed of the basic functions we’ve been using. A more detailed approach would be making use of the High Level Shader Language, where you can specify everything completely how you want it. This count especially for lighting. This is more advanced stuff, so this will be introduced in Series 5 or 6 something one day….

 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
 {
     public class WinForm : System.Windows.Forms.Form
     {
         private int[,] int_Floorplan;
         private int WIDTH;
         private int HEIGHT;
         private int differentbuildings = 5;
         private int[] buildingheights = new int[] { 0, 10, 1, 3, 2, 5 };
 
         private System.ComponentModel.Container components = null;
         private D3D.Device device;
         private Texture scenerytexture;
         private Material material;
         private CustomVertex.PositionNormalTextured[] verticesarray;
         ArrayList verticeslist = new ArrayList();
         private Mesh spacemesh;
         private Material[] spacemeshmaterials;
         private Texture[] spacemeshtextures;
         private float spacemeshradius;
         private float scaling = 0.0005f;
 
         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;
             device = new D3D.Device(0, D3D.DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParams);
 
         device.RenderState.Lighting = true;
 
         device.Lights[0].Type = LightType.Directional;
         device.Lights[0].Diffuse = Color.White;
         device.Lights[0].Direction = new Vector3(1, 1, -1);
         device.Lights[0].Update();
         device.Lights[0].Enabled = true;
 
         device.Lights[1].Type = LightType.Directional;
         device.Lights[1].Diffuse = Color.White;
         device.Lights[1].Direction = new Vector3(-1, -1, -1);
         device.Lights[1].Update();
         device.Lights[1].Enabled = true;
         }
 
         protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
         {
             device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.DarkSlateBlue, 1.0f, 0);
             device.BeginScene();
 
         device.RenderState.Ambient = Color.DarkGray;
             device.Transform.World = Matrix.Identity;
             device.VertexFormat = CustomVertex.PositionNormalTextured.Format;
             device.SetTexture(0, scenerytexture);
             device.DrawUserPrimitives(PrimitiveType.TriangleList, verticeslist.Count / 3, verticesarray);
 
             device.Transform.World = Matrix.Scaling(scaling, scaling, scaling) * Matrix.RotationX((float)Math.PI / 2) * Matrix.Translation(19,5,12);
             DrawMesh(spacemesh, spacemeshmaterials, spacemeshtextures);
 
             device.EndScene();
             device.Present();
             this.Invalidate();            
         }
 
         private void SetUpCamera()
         {
             device.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4, (float)this.Width / (float)this.Height, 0.3f, 500f);
             device.Transform.View = Matrix.LookAtLH(new Vector3(20, 5, 13), new Vector3(8, 7, 0), new Vector3(0, 0, 1));
         }        
 
         private void LoadTexturesAndMaterials()
         {
             material = new Material();
 
             material.Diffuse = Color.White;
             material.Ambient = Color.White;
 
             device.Material = material;
 
             scenerytexture = TextureLoader.FromFile(device, "texturemap.jpg");
         }
 
         private void LoadFloorplan()
         {
             WIDTH = 20;
             HEIGHT = 15;
 
             int_Floorplan = new int[,]
             {
                 {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
                 {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
                 {1,0,0,1,1,0,0,0,1,1,0,0,1,0,1},
                 {1,0,0,1,1,0,0,0,1,0,0,0,1,0,1},
                 {1,0,0,0,1,1,0,1,1,0,0,0,0,0,1},
                 {1,0,0,0,0,0,0,0,0,0,0,1,0,0,1},
                 {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
                 {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
                 {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
                 {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
                 {1,0,1,1,0,0,0,1,0,0,0,0,0,0,1},
                 {1,0,1,0,0,0,0,0,0,0,0,0,0,0,1},
                 {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
                 {1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
                 {1,0,0,0,0,1,0,0,0,0,0,0,0,0,1},
                 {1,0,0,0,0,1,0,0,0,1,0,0,0,0,1},
                 {1,0,1,0,0,0,0,0,0,1,0,0,0,0,1},
                 {1,0,1,1,0,0,0,0,1,1,0,0,0,1,1},
                 {1,0,0,0,0,0,0,0,1,1,0,0,0,1,1},
                 {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
             };        
 
             Random random = new Random();
             for (int x = 0; x < WIDTH; x++)
             {
                 for (int y = 0; y < HEIGHT; y++)
                 {
                     if (int_Floorplan[x, y] == 1)
                     {
                         int_Floorplan[x, y] = random.Next(differentbuildings) + 1;
                     }
                 }
             }
         }
 
         private void LoadMesh(string filename, ref Mesh mesh, ref Material[] meshmaterials, ref Texture[] meshtextures, ref float meshradius)
         {
             ExtendedMaterial[] materialarray;
             mesh = Mesh.FromFile(filename, MeshFlags.Managed, device, 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();
 
             VertexBuffer vertices = mesh.VertexBuffer;
             GraphicsStream stream = vertices.Lock(0, 0, LockFlags.None);
             Vector3 meshcenter;
             meshradius = Geometry.ComputeBoundingSphere(stream, mesh.NumberVertices, mesh.VertexFormat, out meshcenter) * scaling;
         }
 
         private void LoadMeshes()
         {        
             LoadMesh("xwing.x", ref spacemesh, ref spacemeshmaterials, ref spacemeshtextures, ref spacemeshradius);
         }
         
         private void DrawMesh(Mesh mesh, Material[] meshmaterials, Texture[] meshtextures)
         {
             for (int i = 0; i < meshmaterials.Length; i++)
             {
                 device.Material = meshmaterials[i];
                 device.SetTexture(0, meshtextures[i]);
                 mesh.DrawSubset(i);
             }
         }
 
         private void VertexDeclaration()
         {
             float imagesintexture = 1 + differentbuildings * 2;
 
             for (int x = 0; x < WIDTH; x++)
             {
                 for (int y = 0; y < HEIGHT; y++)
                 {
                     int currentbuilding = int_Floorplan[x, y];
                     verticeslist.Add(new CustomVertex.PositionNormalTextured(new Vector3(x, y, buildingheights[currentbuilding]), new Vector3(0, 0, 1), (currentbuilding * 2 + 1) / imagesintexture, 1));
                     verticeslist.Add(new CustomVertex.PositionNormalTextured(new Vector3(x + 1, y, buildingheights[currentbuilding]), new Vector3(0, 0, 1), currentbuilding * 2 / imagesintexture, 1));
                     verticeslist.Add(new CustomVertex.PositionNormalTextured(new Vector3(x + 1, y + 1, buildingheights[currentbuilding]), new Vector3(0, 0, 1), currentbuilding * 2 / imagesintexture, 0));
 
                     verticeslist.Add(new CustomVertex.PositionNormalTextured(new Vector3(x + 1, y + 1, buildingheights[currentbuilding]), new Vector3(0, 0, 1), currentbuilding * 2 / imagesintexture, 0));
                     verticeslist.Add(new CustomVertex.PositionNormalTextured(new Vector3(x, y + 1, buildingheights[currentbuilding]), new Vector3(0, 0, 1), (currentbuilding * 2 + 1) / imagesintexture, 0));
                     verticeslist.Add(new CustomVertex.PositionNormalTextured(new Vector3(x, y, buildingheights[currentbuilding]), new Vector3(0, 0, 1), (currentbuilding * 2 + 1) / imagesintexture, 1));
 
                     if (y > 0)
                     {
                         if (int_Floorplan[x, y - 1] != int_Floorplan[x, y])
                         {
                             if (int_Floorplan[x, y - 1] > 0)
                             {
                                 currentbuilding = int_Floorplan[x, y - 1];
                                 verticeslist.Add(new CustomVertex.PositionNormalTextured(new Vector3(x, y, 0f), new Vector3(0, 1, 0), (currentbuilding * 2 - 1) / imagesintexture, 1));
                                 verticeslist.Add(new CustomVertex.PositionNormalTextured(new Vector3(x + 1, y, buildingheights[currentbuilding]), new Vector3(0, 1, 0), currentbuilding * 2 / imagesintexture, 0));
                                 verticeslist.Add(new CustomVertex.PositionNormalTextured(new Vector3(x + 1, y, 0f), new Vector3(0, 1, 0), currentbuilding * 2 / imagesintexture, 1));
 
                                 verticeslist.Add(new CustomVertex.PositionNormalTextured(new Vector3(x + 1, y, buildingheights[currentbuilding]), new Vector3(0, 1, 0), currentbuilding * 2 / imagesintexture, 0));
                                 verticeslist.Add(new CustomVertex.PositionNormalTextured(new Vector3(x, y, 0f), new Vector3(0, 1, 0), (currentbuilding * 2 - 1) / imagesintexture, 1));
                                 verticeslist.Add(new CustomVertex.PositionNormalTextured(new Vector3(x, y, buildingheights[currentbuilding]), new Vector3(0, 1, 0), (currentbuilding * 2 - 1) / imagesintexture, 0));
 
                             }
                             if (int_Floorplan[x, y] > 0)
                             {
                                 currentbuilding = int_Floorplan[x, y];
                                 verticeslist.Add(new CustomVertex.PositionNormalTextured(new Vector3(x, y, 0f), new Vector3(0, -1, 0), currentbuilding * 2 / imagesintexture, 1));
                                 verticeslist.Add(new CustomVertex.PositionNormalTextured(new Vector3(x + 1, y, 0f), new Vector3(0, -1, 0), (currentbuilding * 2 - 1) / imagesintexture, 1));
                                 verticeslist.Add(new CustomVertex.PositionNormalTextured(new Vector3(x + 1, y, buildingheights[currentbuilding]), new Vector3(0, -1, 0), (currentbuilding * 2 - 1) / imagesintexture, 0));
 
                                 verticeslist.Add(new CustomVertex.PositionNormalTextured(new Vector3(x, y, 0f), new Vector3(0, -1, 0), currentbuilding * 2 / imagesintexture, 1));
                                 verticeslist.Add(new CustomVertex.PositionNormalTextured(new Vector3(x + 1, y, buildingheights[currentbuilding]), new Vector3(0, -1, 0), (currentbuilding * 2 - 1) / imagesintexture, 0));
                                 verticeslist.Add(new CustomVertex.PositionNormalTextured(new Vector3(x, y, buildingheights[currentbuilding]), new Vector3(0, -1, 0), currentbuilding * 2 / imagesintexture, 0));
                             }
 
                         }
                     }
 
                     if (x > 0)
                     {
                         if (int_Floorplan[x - 1, y] != int_Floorplan[x, y])
                         {
                             if (int_Floorplan[x - 1, y] > 0)
                             {
                                 currentbuilding = int_Floorplan[x - 1, y];
                                 verticeslist.Add(new CustomVertex.PositionNormalTextured(new Vector3(x, y, 0f), new Vector3(1, 0, 0), currentbuilding * 2 / imagesintexture, 1));
                                 verticeslist.Add(new CustomVertex.PositionNormalTextured(new Vector3(x, y + 1, 0f), new Vector3(1, 0, 0), (currentbuilding * 2 - 1) / imagesintexture, 1));
                                 verticeslist.Add(new CustomVertex.PositionNormalTextured(new Vector3(x, y + 1, buildingheights[currentbuilding]), new Vector3(1, 0, 0), (currentbuilding * 2 - 1) / imagesintexture, 0));
 
                                 verticeslist.Add(new CustomVertex.PositionNormalTextured(new Vector3(x, y + 1, buildingheights[currentbuilding]), new Vector3(1, 0, 0), (currentbuilding * 2 - 1) / imagesintexture, 0));
                                 verticeslist.Add(new CustomVertex.PositionNormalTextured(new Vector3(x, y, buildingheights[currentbuilding]), new Vector3(1, 0, 0), currentbuilding * 2 / imagesintexture, 0));
                                 verticeslist.Add(new CustomVertex.PositionNormalTextured(new Vector3(x, y, 0f), new Vector3(1, 0, 0), currentbuilding * 2 / imagesintexture, 1));
                             }
                             if (int_Floorplan[x, y] > 0)
                             {
                                 currentbuilding = int_Floorplan[x, y];
                                 verticeslist.Add(new CustomVertex.PositionNormalTextured(new Vector3(x, y, 0f), new Vector3(-1, 0, 0), (currentbuilding * 2 - 1) / imagesintexture, 1));
                                 verticeslist.Add(new CustomVertex.PositionNormalTextured(new Vector3(x, y, buildingheights[currentbuilding]), new Vector3(-1, 0, 0), (currentbuilding * 2 - 1) / imagesintexture, 0));
                                 verticeslist.Add(new CustomVertex.PositionNormalTextured(new Vector3(x, y + 1, 0f), new Vector3(-1, 0, 0), currentbuilding * 2 / imagesintexture, 1));
 
                                 verticeslist.Add(new CustomVertex.PositionNormalTextured(new Vector3(x, y, buildingheights[currentbuilding]), new Vector3(-1, 0, 0), (currentbuilding * 2 - 1) / imagesintexture, 0));
                                 verticeslist.Add(new CustomVertex.PositionNormalTextured(new Vector3(x, y + 1, buildingheights[currentbuilding]), new Vector3(-1, 0, 0), currentbuilding * 2 / imagesintexture, 0));
                                 verticeslist.Add(new CustomVertex.PositionNormalTextured(new Vector3(x, y + 1, 0f), new Vector3(-1, 0, 0), currentbuilding * 2 / imagesintexture, 1));
                             }
                         }
                     }
                 }
             }
             verticesarray = (CustomVertex.PositionNormalTextured[])verticeslist.ToArray(typeof(CustomVertex.PositionNormalTextured));
         }
 
         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 Tutorial using C# -- Season 2";
         }
 
         static void Main()
         {
             using (WinForm our_directx_form = new WinForm())
             {
                 our_directx_form.InitializeDevice();
                 our_directx_form.SetUpCamera();
                 our_directx_form.LoadFloorplan();
                 our_directx_form.VertexDeclaration();
                 our_directx_form.LoadTexturesAndMaterials();
                 our_directx_form.LoadMeshes();
                 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)
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!