Just as a quick test too see if I understood how directX initialization works attempted to creat a new DX window using windows forms from scratch. But I have a problem...
Can anyone explain to me why the triangles rendered in the following code disappear permanently when ever i resize the window I have comparable code that doesn't disappear when I resize but after hours of comparing I just can't see why one code works and this one doesn't.
namespace
DXTest{
public partial class DXForm: Form{
private Device myDevice; private PresentParameters presentparams; protected CustomVertex.PositionColored[] pointData; public Device MyDevice { get { return myDevice; } }
public DXForm()
{
pointData =
new CustomVertex.PositionColored[3];InitializeComponent();
InitializeGraphics();
InitializeRenderData();
}
private void InitializeRenderData(){
pointData[
0] = new CustomVertex.PositionColored(-1.0f, -1.0f, 0.0f, Color.Red.ToArgb());pointData[
1] = new CustomVertex.PositionColored(0.0f, 1.0f, 0.0f, Color.Blue.ToArgb());pointData[
2] = new CustomVertex.PositionColored(1.0f, -1.0f, 0.0f, Color.Green.ToArgb());}
private void InitializeGraphics(){
presentparams =
new PresentParameters();presentparams.Windowed =
true;presentparams.PresentationInterval =
PresentInterval.One;presentparams.SwapEffect =
SwapEffect.Discard;presentparams.EnableAutoDepthStencil =
true;presentparams.AutoDepthStencilFormat =
DepthFormat.D16;myDevice =
new Device(0, DeviceType.Hardware, this, CreateFlags.HardwareVertexProcessing, presentparams);myDevice.RenderState.CullMode =
Cull.None;myDevice.RenderState.ZBufferEnable =
true;myDevice.RenderState.Lighting =
false;}
private void OnResize(object sender, EventArgs e)
{
SetupCamera();
}
public void Render()
{
SetupCamera();
SetupLighting();
myDevice.Clear(
ClearFlags.Target | ClearFlags.ZBuffer, Color.CornflowerBlue, 1.0f, 0);myDevice.BeginScene();
myDevice.VertexFormat =
CustomVertex.PositionColored.Format;DrawScene();
myDevice.EndScene();
myDevice.Present();
}
private void DrawScene(){
myDevice.DrawUserPrimitives(
PrimitiveType.TriangleList, 1, pointData);myDevice.Transform.World =
Matrix.Translation(3.0f, 0.0f, 0.0f);myDevice.DrawUserPrimitives(
PrimitiveType.TriangleList, 1, pointData);myDevice.Transform.World =
Matrix.Translation(-3.0f, 0.0f, 0.0f);myDevice.DrawUserPrimitives(
PrimitiveType.TriangleList, 1, pointData);}
private void SetupCamera(){
myDevice.Transform.View =
Matrix.LookAtRH(new Vector3(0.0f, 0.0f, -5.0f), new Vector3(0.0f, 0.0f, 0.0f), new Vector3(0.0f, 1.0f, 0.0f));myDevice.Transform.Projection =
Matrix.PerspectiveOffCenterRH(-1.0f * (float)this.Width /(float)this.Height, 1.0f * (float)this.Width / (float)this.Height, -1.0f, 1.0f, 1.0f, 100.0f);myDevice.Transform.World =
Matrix.Identity;}
private void SetupLighting(){
myDevice.RenderState.Lighting =
false;}
}
}
and my render loop:
public static void Main(){
demo1.Show();
while (demo1.Created){
if (Form.ActiveForm != demo1){
Thread.Sleep(100);}
demo1.Render();
Application.DoEvents();}
}
}

Why does my window go blank after a resize?
JasonFollas
Where you specify device size I mean after resize you must reinitialize rendering surfaces to new size or I miss something
Greg Hoffman
Sergey, Managed DirectX resizes the back buffer when the Windows size change as a default operation. That’s the reason why you don’t see any code for this job.
Ceres629, this still caused a reset as unmanaged DirectX and all states are lost and need to be set again. In your case it looks like that the cull mode is not set to none after the reset. You should attach an event handler to the OnReset event and set all your default states there.
Derald Smith
You were absolutely correct Ralf, The cull mode was getting reset back to default after a resize and it seems the surface I was looking at was culled.
I never knew resizing caused the device to reset! Thanks for your help!
Sarit Tamir
I hope this helps.
Take care.