//Device creation-------------------------
PresentParameters prm = new PresentParameters();
prm.Windowed = true;
prm.SwapEffect = SwapEffect.Discard;
prm.EnableAutoDepthStencil = true;
prm.AutoDepthStencilFormat = DepthFormat.D24S8;
prm.BackBufferCount = 1;
prm.BackBufferFormat = Format.X8R8G8B8;
prm.BackBufferHeight = pnlMoon.ClientRectangle.Bottom - pnlMoon.ClientRectangle.Top;
prm.BackBufferWidth = pnlMoon.ClientRectangle.Right - pnlMoon.ClientRectangle.Left;
//pnlMoon is a panel on my form used to display the renderings
m_dev = new Device(
0, DeviceType.Hardware, pnlMoon,
CreateFlags.SoftwareVertexProcessing |
CreateFlags.FpuPreserve,
prm);
m_dev.DeviceReset += new System.EventHandler(this.OnResetDevice);
//I found CreateFlags.FpuPreserve necessary because DirectX
//has trouble telling if a moon is in front of or behind a
//planet without the extra precision in the Z buffer
//OnReset-------------------------------------
//Only one light (the sun) that does not drop off with distance
if (this.WindowState != FormWindowState.Minimized) {
m_dev.RenderState.Lighting = true;
m_dev.RenderState.ZBufferEnable = true;
m_dev.RenderState.SourceBlend = Blend.SourceAlpha;
m_dev.RenderState.DestinationBlend = Blend.InvSourceAlpha;
m_dev.Lights[0].Type = LightType.Point;
m_dev.Lights[0].Diffuse = Color.White;
m_dev.Lights[0].Attenuation0 = 1;
m_dev.Lights[0].Attenuation1 = 0;
m_dev.Lights[0].Attenuation2 = 0;
m_dev.Lights[0].Enabled = true;
//The location of the light is set later
}
//Rendering---------------------------------------
//I use indexed PositionNormalTextured vertices
//to draw the moons and the planet
m_dev.Clear(ClearFlags.Target | ClearFlags.ZBuffer | ClearFlags.Stencil,
pnlMoon.BackColor, 1f, 0);
m_dev.BeginScene();
m_dev.Transform.Projection = Matrix.PerspectiveFovLH(
intSign1 * pnlMoon.Height / plt.Zoom,
((float) intSign2 * pnlMoon.Width) / pnlMoon.Height,
(float) (Form.Factor * (plt.r - 0.25)), (float) (Form.Factor * (plt.r + 0.25)));
//Clipping planes are set up slighty in front of and behind
//of the planet and its moons
if (this.Vertices == null) {
Shape.Geodesic(
this.Radius, this.Flattening,
m_bytOrder, out m_vrt, out m_sht);
m_vbf = new VertexBuffer(
typeof(CustomVertex.PositionNormalTextured),
this.Vertices.Length, this.Device, Usage.WriteOnly,
CustomVertex.PositionNormalTextured.Format,
Pool.Default);
m_vbf.Created +=
new EventHandler(this.OnCreateVertexBuffer);
m_idx = new IndexBuffer(
typeof(short), this.Indices.Length, this.Device, 0, Pool.Default);
}
if (m_blnLoad) {
m_vbf.SetData(this.Vertices, 0, 0);
m_idx.SetData(this.Indices, 0, 0);
}
this.Device.Transform.World = this.World;
this.Device.Material = this.Material;
this.Device.SetTexture(0, m_txt);
this.Device.Indices = m_idx;
this.Device.SetStreamSource(0, m_vbf, 0);
this.Device.VertexFormat = CustomVertex.PositionNormalTextured.Format;
this.Device.DrawIndexedPrimitives(
PrimitiveType.TriangleList, 0, 0, this.Vertices.Length, 0, this.Indices.Length / 3);
m_dev.EndScene();
m_dev.Present();