|
|
|
|
Experimenting with Lights in DirectX |
Even when using colors and a Z buffer, your terrain seems to miss some depth detail when you turn on the Solid FillMode. By adding some lights, it will look much better. This chapter we will see the impact of a light on 2 simple triangles, so we can have a better understanding of how lights work in DirectX. We will be using the code from the Rotation & Translation chapter, so reload that code now. Start by letting DirectX know we will be using lights, by finding the line where you turn off lights and replacing it by this line:
device.RenderState.Lighting = true;
Immediately below this line, you can simply start defining your lights. They start at index 0, and we’ll only be defining one:
device.Lights[0].Type = LightType.Directional; device.Lights[0].Diffuse = Color.White; device.Lights[0].Direction = new Vector3(0.8f, 0, -1); device.Lights[0].Enabled = true;
As an example, I used the simplest case, a directional light. Imagine this as the sunlight: the light will travel in one particular direction. There are a few more types of light, I'll discuss them later on. The diffuse color is simply the color of the light. Of course you need to define the direction your light shines, and enable it. It is possible that your video card doesn't support lights, later I'll show you how to check on that. Now try running this code. Very nice, your screen has gone black again. Why's that? To perform its calculations, DirectX needs another input: the 'normal' in every vertex. Consider next figure:

If you have a light source a), and you shine it on the shown 3 surfaces, how is DirectX supposed to know that surface 1 should be lit more intensely than surface 3? (Actually, this could have been programmed by hard, but soon you'll see why it's not) If you look at the thin red lines in figure b), you'll notice that THEY are a nice indication of how much light you would want to be reflected (and thus seen) on every surface. So how can we calculate the length of these lines? Actually, DirectX does the job for us. All we have to do is give the blue arrow perpendicular (with an angle of 90 degrees, the thin blue lines) to every surface and DirectX does the rest (a simple cosinus projection) for us! Because we know every surface consists of triangles, we can define these perpendicular directions together with our vertices, simply by using the CustomVertex.PositionNormalColored vertextype in your VertexDeclaration method:
vertices = new CustomVertex.PositionNormalColored[6];
You also need to reflect this change in your OnPaint method:
device.VertexFormat = CustomVertex.PositionNormalColored.Format;
We could start defining these arrows. But first, have a look at the next picture, where the arrows above represent the direction of the light and the color bar below the drawing represents the color of every pixel along our surface:

If we simply define the perpendicular vectors, it is easy to see there will be an 'edge' in the lighting (see the bar directly above the a)). This is because the right surface receives (and thus also reflects) 'more' of the light then the left surface. So it will be easy to see the surface is made of separate triangles. However, if we place in the shared top vertex a 'normal' as shown in figure b), DirectX automatically interpolates the lighting in every point of our surface! This will give a much smoother effect, as you can see in the bar above the b). This vector is of course half of the sum of the 2 top vectors of a). To demonstrate this example, reset the camera position:
device.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI/4, this.Width/this.Height, 1f, 200f); device.Transform.View = Matrix.LookAtLH(new Vector3(0,-40,100), new Vector3(0,50,0), new Vector3(0,1,0));
Notice how the direction of the light corresponds with the one in the previous example: positive x coordinate means 'to the left' and negative z coordinate means 'downward'. Then change your VertexDeclaration method like this:
vertices = new CustomVertex.PositionNormalColored[6]; vertices[0].Position = new Vector3(0f, 0f, 50f); vertices[0].Color = Color.Blue.ToArgb(); vertices[0].Normal = new Vector3(1,0,1); vertices[1].Position = new Vector3(50f, 0f, 00f); vertices[1].Color = Color.Blue.ToArgb(); vertices[1].Normal = new Vector3(1,0,1); vertices[2].Position = new Vector3(0f, 50f, 50f); vertices[2].Color = Color.Blue.ToArgb(); vertices[2].Normal = new Vector3(1,0,1); vertices[3].Position = new Vector3(-50f, 0f, 0f); vertices[3].Color = Color.Blue.ToArgb(); vertices[3].Normal = new Vector3(-1,0,1); vertices[4].Position = new Vector3(0f, 0f, 50f); vertices[4].Color = Color.Blue.ToArgb(); vertices[4].Normal = new Vector3(-1,0,1); vertices[5].Position = new Vector3(0f, 50f, 50f); vertices[5].Color = Color.Blue.ToArgb(); vertices[5].Normal = new Vector3(-1,0,1);
This defines the 2 surfaces of the picture above. By adding a Z (other than 0), the triangles are now 3D. You can notice that I’ve defined the normal vectors perpendicular, as to reflect example a) of the image above. Don't forget to change this line in your variables block at the top of your page:
private CustomVertex.PositionNormalColored[] vertices;
All there's left to do is change your OnPaint method so this will be your new method:
device.Clear(ClearFlags.Target, Color.Black , 1.0f, 0); device.BeginScene(); device.VertexFormat = CustomVertex.PositionNormalColored.Format; device.DrawUserPrimitives(PrimitiveType.TriangleList, 2, vertices); device.EndScene(); device.Present(); this.Invalidate();
Now run this code and you'll see what I mean with 'edged lighting': the light shines brightly on the right panel and the left panel is darker. You can see clearly the difference between the two triangles! Now it's time to combine the vectors on the edge from (-1,0,1) and (1,0,1) to (-1+1,0,1+1)/2 = (0,0,1):
vertices = new CustomVertex.PositionNormalColored[6]; vertices[0].Position = new Vector3(0f, 0f, 50f); vertices[0].Color = Color.Blue.ToArgb(); vertices[0].Normal = new Vector3(0,0,1); vertices[1].Position = new Vector3(50f, 0f, 00f); vertices[1].Color = Color.Blue.ToArgb(); vertices[1].Normal = new Vector3(1,0,1); vertices[2].Position = new Vector3(0f, 50f, 50f); vertices[2].Color = Color.Blue.ToArgb(); vertices[2].Normal = new Vector3(0,0,1); vertices[3].Position = new Vector3(-50f, 0f, 0f); vertices[3].Color = Color.Blue.ToArgb(); vertices[3].Normal = new Vector3(-1,0,1); vertices[4].Position = new Vector3(0f, 0f, 50f); vertices[4].Color = Color.Blue.ToArgb(); vertices[4].Normal = new Vector3(0,0,1); vertices[5].Position = new Vector3(0f, 50f, 50f); vertices[5].Color = Color.Blue.ToArgb(); vertices[5].Normal = new Vector3(0,0,1);
When you run this code, you'll see that the reflection is nicely distributed from the bright right panel to the darker left tip. It's not difficult to imagine that this effect will give a much nicer effect on a large number of triangles, such as our terrain.

Click here to go to the forum on this chapter!
Or click on one of the topics on this chapter to go there: lighting a cube Hi this is my first post, I am going to get strait...flahing screen im tryiong to look into flashing different colours...Oh my Screen ... Black plz help me hello everybody i got some problem in this tutoria...Light Direction Hi ,
I'm a bit confused about your following s...Light Direction Hi ,
I'm a bit confused about your following s...I don't understand how to get Normal Hi, 1st of all I wanna congrat u because of these ...
Also, you can see that since the middle vertices use the SAME normal, we could again combine the 2x2 shared vertices to 2x1 vertices using an index buffer. Notice however, that in the case you really WANT to create an edge, you need to specify the 2 separate normal vectors.
Your 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; namespace DirectX_Tutorial { public class WinForm : System.Windows.Forms.Form { private Device device; private System.ComponentModel.Container components = null; private float angle = 0f;
private CustomVertex.PositionNormalColored[] vertices;
public WinForm() { InitializeComponent(); this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque, true); } public void InitializeDevice() { PresentParameters presentParams = new PresentParameters(); presentParams.Windowed = true; presentParams.SwapEffect = SwapEffect.Discard; device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParams); } private void CameraPositioning() {
device.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4, this.Width / this.Height, 1f, 200f); device.Transform.View = Matrix.LookAtLH(new Vector3(0, -40, 100), new Vector3(0, 50, 0), new Vector3(0, 1, 0)); device.RenderState.Lighting = true;
device.RenderState.CullMode = Cull.None;
device.Lights[0].Type = LightType.Directional; device.Lights[0].Diffuse = Color.White; device.Lights[0].Direction = new Vector3(0.8f, 0, -1); device.Lights[0].Enabled = true;
} private void VertexDeclaration() {
vertices = new CustomVertex.PositionNormalColored[6]; vertices[0].Position = new Vector3(0f, 0f, 50f); vertices[0].Color = Color.Blue.ToArgb(); vertices[0].Normal = new Vector3(0, 0, 1); vertices[1].Position = new Vector3(50f, 0f, 00f); vertices[1].Color = Color.Blue.ToArgb(); vertices[1].Normal = new Vector3(1, 0, 1); vertices[2].Position = new Vector3(0f, 50f, 50f); vertices[2].Color = Color.Blue.ToArgb(); vertices[2].Normal = new Vector3(0, 0, 1); vertices[3].Position = new Vector3(-50f, 0f, 0f); vertices[3].Color = Color.Blue.ToArgb(); vertices[3].Normal = new Vector3(-1, 0, 1); vertices[4].Position = new Vector3(0f, 0f, 50f); vertices[4].Color = Color.Blue.ToArgb(); vertices[4].Normal = new Vector3(0, 0, 1); vertices[5].Position = new Vector3(0f, 50f, 50f); vertices[5].Color = Color.Blue.ToArgb(); vertices[5].Normal = new Vector3(0, 0, 1);
} protected override void OnPaint(System.Windows.Forms.PaintEventArgs e) { device.Clear(ClearFlags.Target, Color.Black, 1.0f, 0); device.BeginScene();
device.VertexFormat = CustomVertex.PositionNormalColored.Format; device.DrawUserPrimitives(PrimitiveType.TriangleList, 2, vertices);
device.EndScene(); device.Present(); this.Invalidate(); } 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 = "DirectX Tutorial"; } static void Main() { using (WinForm our_directx_form = new WinForm()) { our_directx_form.InitializeDevice(); our_directx_form.CameraPositioning(); our_directx_form.VertexDeclaration(); Application.Run(our_directx_form); } } } }
- Website design & XNA + DirectX code : Riemer Grootjans - ©2003 - 2011 Riemer Grootjans
|
|
|
|
|