Jump to content
  • Advertisement
Sign in to follow this  

Directx 9 Lighting Help? C#

This topic is 4844 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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()
			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++)
					vertices[x+(z*WIDTH)].SetPosition(new Vector3(x, RandomNumber(1,10), z));
					//vertices[x+z*WIDTH].Color = Color.White.ToArgb();

		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].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.Indices = ib;

			device.VertexFormat = CustomVertex.PositionNormal.Format;
			device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, WIDTH*HEIGHT, 0, indices.Length/3);
		protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
		protected override void Dispose (bool disposing)
			if (disposing)
				if (components != null)
		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())


		public int RandomNumber(int min, int max)
			int i;
			Random random = new Random();
			i = random.Next(min, max);
			sRandom += i.ToString() + ":";
			return i;

Share this post

Link to post
Share on other sites
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]

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!