Problem with Managed c# Rendering Meshes order

Started by
5 comments, last by Tape_Worm 18 years, 10 months ago
HI im new in directx i have previosly made a game in directx c++ and wanted to try using managed c# and i have a BIIIGGGG PROBLEMMMM and I cant see what it is . my problem is that I have 2 meshes one in (0,0,1) coordinate and the other one in (0,0,-3) and my camara is in (0,0,5) and when i render them the last mesh that is the fardest(0,0,-3) i see it in front of the first mesh(0,0,1), its like the device doesnt use the Zbuffer for ordering my meshes and I have it enable: device.RenderState.Zbuffer = true; I dont understand WHY is it rendering in the order the meshes are getting to the effect , the device is not ordering my meshes and I cant figure it out. PLS HELP ME , what am I doing wrong????? here is the code::::::::::

		public Device device;
		public ExtendedMaterial[] materials;
		public Material[] meshMaterials ;
		public Texture[] meshTextures ;
		public ExtendedMaterial[] materials2;
		public Material[] meshMaterials2 ;
		public Texture[] meshTextures2 ;
		Effect effect,effect2;
		Mesh m,m2;

		public Form1()
		{
			this.Setstyle(Controlstyles.AllPaintingInWmPaint | Controlstyles.Opaque, true);	
			InitializeComponent();
		}
		#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.components = new System.ComponentModel.Container();
			this.Size = new System.Drawing.Size(300,300);
			this.Text = "Form1";
		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			using (Form1 frm = new Form1())
			{
				frm.Show();
				frm.InitializeDevice();
				Application.Run(frm);
			}
		}
		public void InitializeDevice()
		{
			PresentParameters presentParams = new PresentParameters();
			presentParams.Windowed = true;
			presentParams.SwapEffect = SwapEffect.Discard;
			device = new Microsoft.DirectX.Direct3D.Device(0, Microsoft.DirectX.Direct3D.DeviceType.Hardware, this, CreateFlags.HardwareVertexProcessing, presentParams);
			effect = Effect.FromFile(device,"simple.fx",null, null, ShaderFlags.None,null);
			effect2 = Effect.FromFile(device,"simple.fx",null, null, ShaderFlags.None,null);	
			device.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4, this.Width / this.Width, 0.250f, 100.0f);
			device.Transform.View = Matrix.LookAtLH(new Vector3(0,0,5f), new Vector3(), new Vector3(0,1,0));
			device.Transform.World = Matrix.Identity;	
			device.RenderState.ZBufferEnable = true;
			crearMeshes();
			crearMeshes2();
		}	
		public void crearMeshes()
		{
		    m = Mesh.FromFile("asteroid1.x",MeshFlags.Managed,device,out materials);
			meshMaterials = new Material[materials.Length];
			meshTextures  = new Texture[materials.Length];
			for( int i=0; i< materials.Length; i++ )
			{
				meshMaterials = materials.Material3D;
				if ((materials.TextureFilename != null) && ((materials.TextureFilename != string.Empty)))
				{
					meshTextures = TextureLoader.FromFile(device, materials.TextureFilename);
				}
			}
		}
		public void crearMeshes2()
		{
			m2 = Mesh.FromFile("sphere.x",MeshFlags.Managed,device,out materials2);
			meshMaterials2 = new Material[materials2.Length];
			meshTextures2  = new Texture[materials2.Length];
			for( int i=0; i< materials2.Length; i++ )
			{
				meshMaterials2 = materials2.Material3D;
				if ((materials2.TextureFilename != null) && ((materials2.TextureFilename != string.Empty)))
				{
					meshTextures2 = TextureLoader.FromFile(device, materials2.TextureFilename);
				}
			}
		}
		protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
		{
			device.Clear(ClearFlags.Target, System.Drawing.Color.White, 1.0f, 0);
			device.BeginScene();
			effect.SetValue("g_mWorldViewProjection", device.Transform.World  * Matrix.Translation(0,0f,3f) * device.Transform.View  * device.Transform.Projection );		
			effect.SetValue("g_mWorld", Matrix.Identity);
			effect.Technique = "RenderScene";		
			int numPass = effect.Begin(0);
			for (int pass=0; pass<numPass; pass++)
			{
				effect.BeginPass(pass);
				for (int i = 0; i < materials.Length; i++)
				{
					device.Material = meshMaterials;
					device.SetTexture(pass,meshTextures);
					m.DrawSubset(i);
				}
					effect.EndPass();
			}
			effect.End();		
			//effect2.SetValue("g_mWorldViewProjection", device.Transform.World  * device.Transform.View * device.Transform.Projection );
			effect2.SetValue("g_mWorldViewProjection", device.Transform.World * Matrix.Translation(0,0f,-3f) * device.Transform.View * device.Transform.Projection );		
			effect2.SetValue("g_mWorld", device.Transform.World);
			effect2.Technique = "RenderScene";
			numPass = effect2.Begin(0);
			for (int pass=0; pass<numPass; pass++)
			{
				effect2.BeginPass(pass);
				for (int i = 0; i < materials2.Length; i++)
				{
					device.Material = meshMaterials2;
					device.SetTexture(pass,meshTextures2);
					m2.DrawSubset(i);
				}
				effect2.EndPass();
			}
			effect2.End();		
			device.EndScene();
			device.Present();
			this.Invalidate();
		}
[Edited by - Coder on June 21, 2005 3:00:25 PM]
Advertisement
You didn't setup your depth buffer in the presentation parameters.

PP.EnableAutoDepthStencil = true;
PP.AutoDepthStencilFormat = (a valid depth buffer format);

It's all well and good to turn on the zbuffer, but without an actual z buffer to use, it'll do very little.
Quote:Original post by Tape_Worm
You didn't setup your depth buffer in the presentation parameters.

PP.EnableAutoDepthStencil = true;
PP.AutoDepthStencilFormat = (a valid depth buffer format);

It's all well and good to turn on the zbuffer, but without an actual z buffer to use, it'll do very little.


HI , can you please tell me like a default deth format ???
i have tried DepthFormat.D16 but dont really know what im doing... :S
Unfortunately I think a 'default' depth format depends on your hardware/drivers/etc... I think the most common one is D24X8 or D24S8 (for a stencil buffer). The procedure I use is to check multiple depth formats until one is deemed acceptable by D3D and go with that.

There are functions in the API to do this sort of testing for you: Manager.CheckDeviceFormat and Manager.CheckDepthStencilMatch.

You can look up these functions in the MSDN/Managed DX9 documentation. Also look through the sample code in the SDK as well to see how functionality like this can be implemented.

Quote:Original post by Tape_Worm
Unfortunately I think a 'default' depth format depends on your hardware/drivers/etc... I think the most common one is D24X8 or D24S8 (for a stencil buffer). The procedure I use is to check multiple depth formats until one is deemed acceptable by D3D and go with that.

There are functions in the API to do this sort of testing for you: Manager.CheckDeviceFormat and Manager.CheckDepthStencilMatch.

You can look up these functions in the MSDN/Managed DX9 documentation. Also look through the sample code in the SDK as well to see how functionality like this can be implemented.


HI thnks again but i tried to use them and they dont give me an error but
the image appears all mest up, i can see just some dots but not the mesh itself
:S what do u think it could be ??? i have tried all the other ones.
HEY Tape_Worm
THANKKKKK YOU VERYYYYYYYYYYYY MUCHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH
my problem was that in the device.Clear
i havent put the option ClearFlags.ZBuffer
and that was it .
thank you very much,
Camilo.
You're kindly welcome.

This topic is closed to new replies.

Advertisement