bool InitializeGraphics()
{
try
{
Caps caps = Manager.GetDeviceCaps( Manager.Adapters.Default.Adapter,
DeviceType.Hardware );
CreateFlags flags;
if( caps.DeviceCaps.SupportsHardwareTransformAndLight )
flags = CreateFlags.HardwareVertexProcessing;
else
flags = CreateFlags.SoftwareVertexProcessing;
presentParams[0] = new PresentParameters();
presentParams[1] = new PresentParameters();
presentParams[0].Windowed = true;
presentParams[0].SwapEffect = SwapEffect.Discard;
DisplayMode mode = new DisplayMode();
presentParams[0].BackBufferFormat = mode.Format;
presentParams[0].BackBufferWidth = panel1.Width;
presentParams[0].BackBufferHeight = panel1.Height;
presentParams[0].EnableAutoDepthStencil = true;
presentParams[0].AutoDepthStencilFormat = DepthFormat.D16;
presentParams[1].Windowed = true;
presentParams[1].SwapEffect = SwapEffect.Discard;
presentParams[1].BackBufferFormat = mode.Format;
presentParams[1].BackBufferWidth = panel2.Width;
presentParams[1].BackBufferHeight = panel2.Height;
presentParams[1].EnableAutoDepthStencil = true;
presentParams[1].AutoDepthStencilFormat = DepthFormat.D16;
// create the device
device = new Device(0, DeviceType.Hardware, this, flags, presentParams);
device.DeviceReset += new System.EventHandler(this.OnResetDevice);
device.DeviceLost += new EventHandler(this.OnLostDevice);
this.OnResetDevice(device, null);
initializedDirect3D = true;
}
catch(Direct3DXException)
{
return false;
}
return true;
}
public void OnResizingDevice()
{
if(initializedDirect3D)
{
pause = false;
Reset_Present_Params();
device.Reset(presentParams);
}
}
private void Reset_Present_Params()
{
DisplayMode mode = new DisplayMode();
presentParams[0].Windowed = true;
presentParams[0].SwapEffect = SwapEffect.Discard;
presentParams[0].BackBufferFormat = mode.Format;
presentParams[0].BackBufferWidth = panel1.Width;
presentParams[0].BackBufferHeight = panel1.Height;
presentParams[0].EnableAutoDepthStencil = true;
presentParams[0].AutoDepthStencilFormat = DepthFormat.D16;
presentParams[1].Windowed = true;
presentParams[1].SwapEffect = SwapEffect.Discard;
presentParams[1].BackBufferFormat = mode.Format;
presentParams[1].BackBufferWidth = panel2.Width;
presentParams[1].BackBufferHeight = panel2.Height;
presentParams[1].EnableAutoDepthStencil = true;
presentParams[1].AutoDepthStencilFormat = DepthFormat.D16;
}
public void OnResetDevice(object sender, EventArgs e)
{
sc_0 = device.GetSwapChain(0);
sc_1 = new SwapChain(device, presentParams[1]);
device.RenderState.ZBufferEnable = true;
device.RenderState.Ambient = System.Drawing.Color.White;
}
void SetupMatrices()
{
float scale = 1.0f; // The gun is gigantic, so let's scale it down a bit
Vector3 pos = new Vector3(); // Let's display at 0, 0,
Vector3 camPos = new Vector3(0.0f, 9.5f, 17.0f);
Vector3 lookAt = new Vector3(); // We are facing 0, 0, 0, so we can see the model
Vector3 camUp = new Vector3(0, 1, 0); // Up vector of the cam
// Setup the camera
device.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4, this.Width / this.Height, 1.0f, 1000.0f);
device.Transform.View = Matrix.LookAtLH(camPos, lookAt, camUp);
// Display our model
device.Transform.World = Matrix.Scaling(scale, scale, scale) *
Matrix.Translation(0f, 0f, 0f)*Matrix.RotationY(Environment.TickCount/1000.0f);
}
private void Render()
{
if (device == null)
return;
if (pause)
return;
if(x_file_list.Count > 0)
{
// panel1
Surface pBackBuffer = sc_0.GetBackBuffer(0, BackBufferType.Mono);
device.SetRenderTarget(0, pBackBuffer);
//Clear the backbuffer to a blue color
device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, System.Drawing.Color.DarkGray, 1.0f, 0);
device.BeginScene();
// Setup the world, view, and projection matrices
SetupMatrices();
// Meshes are divided into subsets, one for each material. Render them in
// a loop
for( int i=0; i<meshMaterials.Length; i++ )
{
// Set the material and texture for this subset
device.Material = meshMaterials;
device.SetTexture(0, meshTextures);
// Draw the mesh subset
xMesh.DrawSubset(i);
}
if(active_panel == panel1)
Draw_Active_Rect(panel1);
//End the scene
device.EndScene();
sc_0.Present(panel1);
//device.Present();
pBackBuffer.ReleaseGraphics();
// panel2
Surface pBackBuffer2 = sc_1.GetBackBuffer(0, BackBufferType.Mono);
device.SetRenderTarget( 0, pBackBuffer2 );
//Clear the backbuffer to a blue color
device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, System.Drawing.Color.DarkGray, 1.0f, 0);
device.BeginScene();
// Setup the world, view, and projection matrices
SetupMatrices();
// Meshes are divided into subsets, one for each material. Render them in
// a loop
for( int i=0; i<meshMaterials.Length; i++ )
{
// Set the material and texture for this subset
device.Material = meshMaterials;
device.SetTexture(0, meshTextures);
// Draw the mesh subset
xMesh.DrawSubset(i);
}
if(active_panel == panel2)
Draw_Active_Rect(panel2);
//End the scene
device.EndScene();
//device.Present();
sc_1.Present(panel2);
pBackBuffer2.ReleaseGraphics();
//pause = true;
}
}
[Edited by - Coder on October 23, 2005 4:41:17 PM]