Following through a book called Managed DirectX9 Kickstart by Tom Miller.. the books a few years old, and the API has changed a bit. A few things (like the SetPosition() function now being the Position property) were trivial to fix, but there's one problem
that I can't find a solution for.
The problem is with lighting. Specifically, I have a Microsoft.DirectX.Direct3D.Device object device, and when I call:
devices.Lights[0].Commit()
I get an error that the function doesn't exist. I've heard that this function was replaced with the Update() function, but when I put that in, the lighting does not work as expected.
Any ideas?
Here's the relevant code (some functions omitted):
public partial class Form1 : Form
{
private Device device = null;
private float angle = 0.0f;
public void InitializeGraphics()
{
PresentParameters presentParams = new PresentParameters();
presentParams.Windowed = true;
presentParams.SwapEffect = SwapEffect.Discard;
device = new Device(0, DeviceType.Hardware, this, CreateFlags.HardwareVertexProcessing, presentParams);
}
private void SetupCamera()
{
device.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4, this.Width / this.Height, 1.0f, 100.0f);
device.Transform.View = Matrix.LookAtLH(new Vector3(0, 0, 5.0f), new Vector3(), new Vector3(0, 1, 0));
device.Transform.World = Matrix.RotationAxis(new Vector3(angle / ((float)Math.PI * 2.0f), angle / ((float)Math.PI * 4.0f), angle / ((float)Math.PI * 6.0f)), angle / (float)Math.PI);
angle += 0.1f;
device.RenderState.Lighting = true;
device.RenderState.CullMode = Cull.None;
}
protected override void OnPaint(PaintEventArgs e)
{
device.Clear(ClearFlags.Target, System.Drawing.Color.CornflowerBlue, 1.0f, 0);
SetupCamera();
CustomVertex.PositionNormalColored[] verts = new CustomVertex.PositionNormalColored[3];
verts[0].Position = new Vector3(0.0f, 1.0f, 1.0f);
verts[0].Normal = new Vector3(0.0f, 0.0f, -1.0f);
verts[0].Color = System.Drawing.Color.White.ToArgb();
verts[1].Position = new Vector3(-1.0f, -1.0f, 1.0f);
verts[1].Normal = new Vector3(0.0f, 0.0f, -1.0f);
verts[1].Color = System.Drawing.Color.White.ToArgb();
verts[2].Position = new Vector3(1.0f, -1.0f, 1.0f);
verts[2].Normal = new Vector3(0.0f, 0.0f, -1.0f);
verts[2].Color = System.Drawing.Color.White.ToArgb();
device.Lights[0].Type = LightType.Point;
device.Lights[0].Position = new Vector3();
device.Lights[0].Diffuse = System.Drawing.Color.White;
device.Lights[0].Attenuation0 = 0.2f;
device.Lights[0].Range = 10000.0f;
device.Lights[0].Enabled = true;
device.Lights[0].Update();
device.BeginScene();
device.VertexFormat = CustomVertex.PositionColored.Format;
device.DrawUserPrimitives(PrimitiveType.TriangleList, 1, verts);
device.EndScene();
device.Present();
this.Invalidate();
}
}
}
Thread Closed
This thread is kinda stale and has been closed but if you'd like to continue the conversation, please create a new thread in our Forums,
or Contact Us and let us know.