Jump to content
Xtreme .Net Talk

Recommended Posts

Posted (edited)

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[i] = materials[i].Material3D;
			if ((materials[i].TextureFilename != null) && ((materials[i].TextureFilename != string.Empty)))
			{
				meshTextures[i] = TextureLoader.FromFile(device, materials[i].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[i] = materials2[i].Material3D;
			if ((materials2[i].TextureFilename != null) && ((materials2[i].TextureFilename != string.Empty)))
			{
				meshTextures2[i] = TextureLoader.FromFile(device, materials2[i].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[i];
				device.SetTexture(pass,meshTextures[i]);
				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[i];
				device.SetTexture(pass,meshTextures2[i]);
				m2.DrawSubset(i);
			}
			effect2.EndPass();
		}
		effect2.End();		
		device.EndScene();
		device.Present();
		this.Invalidate();
	}

Edited by PlausiblyDamp
Posted

Just a quick scan of your code -- I see that you're not clearing the ZBuffer.

device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, System.Drawing.Color.White, 1.0f, 0);

"Who is John Galt?"
  • *Experts*
Posted
You are not enabling the depth buffer in presentation parameters. Look into EnableAutoDepthStencil and AutoDepthStencilFormat properties of present params.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...