Original Post
I'm a bit of a DX noob so maybe I'm doing something dumb, but I can't work out what it is. My sample simply renders a teapot and has a mechanism to catch devicelost. It works fine up until the device is reset and the teapot stops rendering. I don't think that it's because the teapot resource is lost because it's stored in the managed pool which I believe is backed up by system memory. I have a feeling that it's to do with lighting because after the teapot disappears, setting Renderstate.Lighting = false allows the teapot to render correctly, sans realistic lighting. However, the light is reset on device reset so I'm not sure what's going on. Thanks for your help.
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
public partial class Form1 : Form
{
private Device device = null;
private PresentParameters presentparams = null;
private DisplayMode displaymode;
private bool devicelost;
private Mesh teapot;
private Material mtrl;
public Form1()
{
InitializeComponent();
this.Load += new EventHandler(Form1_Load);
this.Paint += new PaintEventHandler(Form1_Paint);
}
void Form1_Load(object sender, EventArgs e)
{
this.Show();
Setstyle(Controlstyles.AllPaintingInWmPaint | Controlstyles.Opaque | Controlstyles.UserPaint, true);
Initialise(this);
SetLight();
SetCamera();
teapot = Mesh.Teapot(device);
mtrl = new Material();
mtrl.Diffuse = Color.White;
}
private void SetCamera()
{
device.Transform.Projection = Matrix.PerspectiveFovLH(Geometry.DegreeToRadian(45), 1, 1, 5000);
device.Transform.View = Matrix.LookAtLH(new Vector3(0, 0, -10), new Vector3(), new Vector3(0, 1, 0));
}
private void SetLight()
{
device.RenderState.Lighting = true;
device.Lights[0].Type = LightType.Directional;
device.Lights[0].Direction = new Vector3(0, 1, 1);
device.Lights[0].Diffuse = System.Drawing.Color.White;
device.Lights[0].Update();
device.Lights[0].Enabled = true;
}
void Form1_Paint(object sender, PaintEventArgs e)
{
if (devicelost)
AttemptRecovery();
if (!devicelost)
{
device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black, 1.0f, 0);
device.BeginScene();
device.Material = mtrl;
teapot.DrawSubset(0);
device.EndScene();
try
{
device.Present();
}
catch (DeviceLostException)
{
devicelost = true;
}
}
this.Invalidate();
}
#region "Device"
private void Initialise(Form Target)
{
displaymode.Height = 480;
displaymode.Width = 640;
this.ClientSize = new Size(640, 480);
displaymode.Format = Format.R5G6B5;
presentparams = new PresentParameters();
presentparams.BackBufferFormat = displaymode.Format;
presentparams.BackBufferWidth = displaymode.Width;
presentparams.BackBufferHeight = displaymode.Height;
presentparams.BackBufferCount = 1;
presentparams.SwapEffect = SwapEffect.Discard;
presentparams.PresentationInterval = PresentInterval.Immediate;
presentparams.EnableAutoDepthStencil = true;
presentparams.AutoDepthStencilFormat = DepthFormat.D16;
presentparams.Windowed = true;
device = new Device(Manager.Adapters.Default.Adapter, DeviceType.Hardware,
Target.Handle, CreateFlags.SoftwareVertexProcessing, presentparams);
device.RenderState.SourceBlend = Blend.SourceAlpha;
device.RenderState.DestinationBlend = Blend.InvSourceAlpha;
device.RenderState.AlphaBlendEnable = true;
device.DeviceReset += new EventHandler(device_DeviceReset);
device.DeviceResizing += new CancelEventHandler(device_DeviceResizing);
}
void device_DeviceResizing(object sender, CancelEventArgs e)
{
e.Cancel = true;
}
void device_DeviceReset(object sender, EventArgs e)
{
SetCamera();
SetLight();
}
#endregion
#region "Device Recovery"
protected void AttemptRecovery()
{
try
{
device.TestCooperativeLevel();
}
catch (DeviceLostException)
{
}
catch (DeviceNotResetException)
{
try
{
device.Reset(presentparams);
devicelost = false;
}
catch (DeviceLostException)
{ }
}
}
#endregion
}