Directx 9 Lighting Help? C#

Started by
3 comments, last by gunhawk 18 years, 8 months ago
So far I have created an indexed primitive terrain and have set up my camera above it looking down. But I cannot seem to get point light or directional light working (have not tried spot light). Ambient light works, but I really need a directional light. I have tried 0,-1,0 as the direction to make it come from above, but that doesn't work. And I'm also trying to make it come diagonally down from the left but that isn't working either. Can anyone help? Code here:

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 GunHawk
{
 
	public class WinForm : System.Windows.Forms.Form
	{
		private Device device;
		private System.ComponentModel.Container components = null;
		private VertexBuffer vb;
		private IndexBuffer ib;
		private int[] indices;
		private CustomVertex.PositionNormal[] vertices;
	
		private int WIDTH = 10;
		private int HEIGHT = 10;
		private string sRandom;

		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.EnableAutoDepthStencil = true; // Turn on a Depth stencil
			presentParams.AutoDepthStencilFormat = DepthFormat.D16; // And the stencil format
			device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParams);

		}

		private void VertexDeclaration()
		{
			vertices = new CustomVertex.PositionNormal[WIDTH*HEIGHT];
			vb = new VertexBuffer(typeof(CustomVertex.PositionNormal), WIDTH * HEIGHT, device, Usage.Dynamic | Usage.WriteOnly, CustomVertex.PositionNormal.Format, Pool.Default);
			for (int x=0;x<WIDTH;x++)
			{
				for (int z=0;z<HEIGHT;z++)
				{
					Application.DoEvents();
					vertices[x+(z*WIDTH)].SetPosition(new Vector3(x, RandomNumber(1,10), z));
					//vertices[x+z*WIDTH].Color = Color.White.ToArgb();
				}
			}
			vb.SetData(vertices,0,LockFlags.None);
		}

		private void IndicesDeclaration()
		{
			ib = new IndexBuffer(typeof(int),(WIDTH)*(HEIGHT)*6,device,Usage.WriteOnly, Pool.Default);
			indices = new int[WIDTH*HEIGHT*6];
			
			for (int x=0;x<WIDTH-1;x++)
			{
				for (int z=0;z<HEIGHT-1;z++)
				{
					// Right topped triangle /|
					indices[(x+z*(WIDTH))*6] = (x+1)+(z+1)*WIDTH;
					indices[(x+z*(WIDTH))*6+1] = (x+1)+z*WIDTH;
					indices[(x+z*(WIDTH))*6+2] = x+z*WIDTH;

					// Left bottomed triangle |/
					indices[(x+z*(WIDTH))*6+3] = (x+1)+(z+1)*WIDTH;
					indices[(x+z*(WIDTH))*6+4] = x+z*WIDTH;
					indices[(x+z*(WIDTH))*6+5] = x+(z+1)*WIDTH;
 
				}
			}
			ib.SetData(indices, 0, LockFlags.None);
		}
 
		private void LightSetup()
		{
			Color col = Color.White;
    
			// Set up a material. The material here just has the diffuse and ambient
			// colors set to white. Note that only one material can be used at a time.
			Material mtrl = new Material();
			mtrl.Diffuse = col;
			mtrl.Ambient = col;
			device.Material = mtrl;


			
			//device.Lights[0].Ambient = Color.FromArgb(255,255,255);
			device.Lights[0].Type = LightType.Directional;
			device.Lights[0].Diffuse = Color.White;
			device.Lights[0].Direction = new Vector3(100f,-10f,0f);

			device.Lights[0].Commit();
			device.Lights[0].Enabled = true;
			   
			// Finally, turn on some ambient light.
			// Ambient light is light that scatters and lights all objects evenly.
			device.RenderState.Ambient = System.Drawing.Color.FromArgb(0x606060);
			
		}
		private void Render()
		{

			device.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI/4, this.Width/this.Height, 1f, 150f);
			device.Transform.View = Matrix.LookAtLH(new Vector3(5,25,5), new Vector3(5,0,5), new Vector3(0,0,1));

			
			device.RenderState.FillMode = FillMode.Solid;
			device.RenderState.CullMode = Cull.None;
			device.RenderState.ZBufferEnable = true;
			device.RenderState.Lighting = true;

			device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.DarkBlue , 1.0f, 0);
 
			device.BeginScene();
			
			device.SetStreamSource(0,vb,0);
			device.Indices = ib;

			device.VertexFormat = CustomVertex.PositionNormal.Format;
			device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, WIDTH*HEIGHT, 0, indices.Length/3);
			device.EndScene();      
			device.Present();
 
			this.Invalidate();
		}
		protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
		{
			this.Render();
		}
 
		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.VertexDeclaration();
				our_directx_form.IndicesDeclaration();
				our_directx_form.LightSetup();
				Application.Run(our_directx_form);
				
			}

		}

		public int RandomNumber(int min, int max)
		{
			int i;
			Random random = new Random();
			i = random.Next(min, max);
			sRandom += i.ToString() + ":";
			System.Console.Write(sRandom);
			return i;
			
		}
		
	}
}
Advertisement
Seems like you're not defining normals for your vertices.
Great, thank you.

Edited: I've now learned what a normal is, thanks again!
Normal is just a vector that defines which direction the vertex is pointing. There's some info on Toymaker's site about calculating terrain normals. It's not in C#, but implementing that algorithm with C# shouldn't be very difficult.

EDIT: I just noticed this, there's an article about this right here in gamedev.net. [smile]

[Edited by - centipede on August 10, 2005 4:28:43 PM]
Thanks so much those articles are a gold mine to me right now. I'm reading up on them now!

This topic is closed to new replies.

Advertisement