you can try my .exe http://www.freestylebitonto.com/debug.rar
this is the complete code:
using System;using System.Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;using System.Data;using Microsoft.DirectX;using Microsoft.DirectX.Direct3D;namespace prova_3d{ /// <summary> /// Summary description for Form1. /// </summary> public class prova_3dForm : System.Windows.Forms.Form { private System.Windows.Forms.Splitter splitter1; /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.Container components = null; Device device = null; SwapChain sc_0 = null; SwapChain sc_1 = null; Mesh mesh = null; Material[] meshMaterials; // Materials for the mesh Texture[] meshTextures; // Textures for the mesh private System.Windows.Forms.Panel panel0; private System.Windows.Forms.Panel panel1; private bool running; private bool initialized = false; private bool can_draw = false; string error_message = ""; struct Vertex { float x, y, z; float nx, ny, nz; int diffuse; /*enum FVF { FVF_flags = D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_DIFFUSE };*/ } public prova_3dForm() { // // Required for Windows Form Designer support // InitializeComponent(); // // TODO: Add any constructor code after InitializeComponent call // Init(); } /// <summary> /// Clean up any resources being used. /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.panel0 = new System.Windows.Forms.Panel(); this.splitter1 = new System.Windows.Forms.Splitter(); this.panel1 = new System.Windows.Forms.Panel(); this.SuspendLayout(); // // panel0 // this.panel0.Dock = System.Windows.Forms.DockStyle.Left; this.panel0.Location = new System.Drawing.Point(0, 0); this.panel0.Name = "panel0"; this.panel0.Size = new System.Drawing.Size(192, 374); this.panel0.TabIndex = 0; this.panel0.Resize += new System.EventHandler(this.panel0_Resize); // // splitter1 // this.splitter1.BackColor = System.Drawing.Color.DimGray; this.splitter1.Location = new System.Drawing.Point(192, 0); this.splitter1.Name = "splitter1"; this.splitter1.Size = new System.Drawing.Size(5, 374); this.splitter1.TabIndex = 1; this.splitter1.TabStop = false; // // panel1 // this.panel1.Dock = System.Windows.Forms.DockStyle.Fill; this.panel1.Location = new System.Drawing.Point(197, 0); this.panel1.Name = "panel1"; this.panel1.Size = new System.Drawing.Size(363, 374); this.panel1.TabIndex = 2; this.panel1.Resize += new System.EventHandler(this.panel1_Resize); // // Sake3DForm // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(560, 374); this.Controls.Add(this.panel1); this.Controls.Add(this.splitter1); this.Controls.Add(this.panel0); this.Name = "Form1"; this.Text = "Form1"; this.Closing += new System.ComponentModel.CancelEventHandler(this.Form1_Closing); this.ResumeLayout(false); } #endregion /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { prova_3dForm sf = new prova_3dForm(); sf.Show(); while(sf.running) { if( sf.can_draw ) sf.Render(); Application.DoEvents(); } } private bool CreateDevice() { try{ PresentParameters presentParams = new PresentParameters(); presentParams.Windowed = true; presentParams.BackBufferCount = 1; presentParams.SwapEffect = SwapEffect.Discard; presentParams.EnableAutoDepthStencil = true; presentParams.AutoDepthStencilFormat = DepthFormat.D24S8; presentParams.DeviceWindowHandle = panel0.Handle; presentParams.BackBufferWidth = panel0.Width; presentParams.BackBufferHeight = panel0.Height; SetDevice(presentParams); sc_0 = new SwapChain(device, presentParams); } catch { error_message += "errore nella creazione del device" + "\n"; return false; } return true; } private void SetDevice(PresentParameters presentParams) { device = new Device(0, DeviceType.Hardware, panel0, CreateFlags.SoftwareVertexProcessing, presentParams); device.SetRenderState(RenderStates.ZEnable, true); device.DeviceLost += new System.EventHandler(this.DeviceLost); device.DeviceResizing += new CancelEventHandler(this.DeviceResizingEventHandler); device.DeviceReset += new System.EventHandler(this.ResetDevice); this.ResetDevice(device, null); } private bool CheckShaderSupport() { Version v1_1 = new Version(1,1); // check version is at least shader 1.1 // retrieve the device caps Caps caps = Manager.GetDeviceCaps(0, DeviceType.Hardware); // check the supported shader version if ((caps.VertexShaderVersion >= v1_1) && (caps.PixelShaderVersion >= v1_1)) { return true; } error_message += "errore nella creazione del supporto" + "\n"; return false; } private bool CreateAdditionalSwapChains() { try { // questo è il primo sc_0 = device.GetSwapChain(0); // ora creo il secondo PresentParameters presentParams = new PresentParameters(); presentParams.BackBufferCount = 1; presentParams.Windowed = true; presentParams.DeviceWindowHandle = panel1.Handle; presentParams.SwapEffect = SwapEffect.Discard; presentParams.EnableAutoDepthStencil = true; presentParams.AutoDepthStencilFormat = DepthFormat.D24S8; presentParams.BackBufferWidth = panel1.Width; presentParams.BackBufferHeight = panel1.Height; sc_1 = new SwapChain(device, presentParams); } catch { error_message += "errore nella creazione dell'additional swap chain" + "\n"; return false; } initialized = true; return true; } Mesh m1; private bool PrepareScene() { try { ExtendedMaterial[] materials = null; // Turn on the z-buffer. device.RenderState.ZBufferEnable = true; // Turn on ambient lighting. device.RenderState.Ambient = System.Drawing.Color.White; device.RenderState.FillMode = (FillMode)0; meshTextures = null; mesh = Mesh.FromFile(Application.StartupPath + @"\mesh.x", MeshFlags.SystemMemory, device, out materials); if (meshTextures == null) { // Extract the material properties and texture names. meshTextures = new Texture[materials.Length]; meshMaterials = new Material[materials.Length]; for( int i=0; i<materials.Length; i++ ) { meshMaterials = materials.Material3D; // Set the ambient color for the material. Direct3D // does not do this by default. meshMaterials.Ambient = meshMaterials.Diffuse; // Create the texture. meshTextures = TextureLoader.FromFile(device, materials.TextureFilename); } } } catch { error_message += "errore nel caricare la mesh" + "\n"; return false; } return true; } private void Init() { if( CreateDevice() && CheckShaderSupport() && CreateAdditionalSwapChains() ) { running = true; can_draw = true; } else MessageBox.Show(error_message); } private void UpdateParameters_0() { PresentParameters presentParams = new PresentParameters(); presentParams.Windowed = true; presentParams.BackBufferCount = 1; presentParams.SwapEffect = SwapEffect.Discard; presentParams.EnableAutoDepthStencil = true; presentParams.AutoDepthStencilFormat = DepthFormat.D24S8; presentParams.DeviceWindowHandle = panel0.Handle; presentParams.BackBufferWidth = panel0.Width; presentParams.BackBufferHeight = panel0.Height; SetDevice(presentParams); sc_0 = new SwapChain(device, presentParams); } private void UpdateParameters_1() { // ora creo il secondo PresentParameters presentParams1 = new PresentParameters(); presentParams1.Windowed = true; presentParams1.BackBufferCount = 1; presentParams1.SwapEffect = SwapEffect.Discard; presentParams1.EnableAutoDepthStencil = true; presentParams1.DeviceWindowHandle = panel1.Handle; presentParams1.AutoDepthStencilFormat = DepthFormat.D24S8; presentParams1.BackBufferWidth = panel1.Width; presentParams1.BackBufferHeight = panel1.Height; sc_1 = new SwapChain(device, presentParams1); } private void Render() { SetupMatrices(panel0.Width, panel0.Height); Render_panel0(); Render_panel1(); } void SetupMatrices(int w, int h) { 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, 0.0f, 5.0f); Vector3 lookAt = new Vector3(0.0f, 0.0f, 0.0f); // We are facing 0, 0, 0, so we can see the model Vector3 camUp = new Vector3(0.0f, 1.0f, 0.0f); // Up vector of the cam // Setup the camera device.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4, 1.0f, 1.0f, 10.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_panel0() { Surface backbuffer; backbuffer = sc_0.GetBackBuffer(0, BackBufferType.Mono); device.SetRenderTarget(0, backbuffer); device.Clear(ClearFlags.Target | ClearFlags.ZBuffer | ClearFlags.Stencil, Color.Gray, 1.0f, 0); device.BeginScene(); try { for (int m = 0; m < meshTextures.Length; ++m) { device.Material = meshMaterials[m]; device.SetTexture(0, meshTextures[m]); mesh.DrawSubset(m); } } catch(Direct3DXException ex) { running = false; MessageBox.Show(ex.ToString()); } finally { device.EndScene(); sc_0.Present(); backbuffer.Dispose(); } } private void Render_panel1() { Surface backbuffer; backbuffer = sc_1.GetBackBuffer(0, BackBufferType.Mono); device.SetRenderTarget(0, backbuffer); device.Clear(ClearFlags.Target | ClearFlags.ZBuffer | ClearFlags.Stencil, Color.Gray, 1.0f, 0); device.BeginScene(); try { for (int m = 0; m < meshTextures.Length; ++m) { device.Material = meshMaterials[m]; device.SetTexture(0, meshTextures[m]); mesh.DrawSubset(m); } } catch(Direct3DXException ex) { running = false; MessageBox.Show(ex.ToString()); } finally { device.EndScene(); sc_1.Present(); backbuffer.Dispose(); } } private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e) { running = false; } private void DeviceLost(object sender, System.EventArgs e) { //TODO: forse andrebbero le seguenti 2 o 4 righe //device = ((Device)sender); //PrepareScene(); //can_draw = false; //device.CheckCooperativeLevel(); } private void DeviceResizingEventHandler(object sender, System.ComponentModel.CancelEventArgs e) { e.Cancel = true; } private void ResetDevice(object sender, System.EventArgs e) { device = ((Device)sender); PrepareScene(); } private void panel0_Resize(object sender, System.EventArgs e) { can_draw = this.WindowState != FormWindowState.Minimized; if(initialized && can_draw) { UpdateParameters_0(); PrepareScene(); } } private void panel1_Resize(object sender, System.EventArgs e) { can_draw = this.WindowState != FormWindowState.Minimized; if(initialized && can_draw) { UpdateParameters_1(); //PrepareScene(); } } }}
please help me to find the bug, thanks.