Jump to content
  • Advertisement
Sign in to follow this  
Synex

Z Values

This topic is 5218 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ok... I am being really REALLY thick here. Situation is, i've got 2 triangles, one which should appear in front of the other (because of Z values), however, DirectX seems to be ignoring the Z values and just rendering them in order and whichever gets rendered last comes to the top. Code listing is as follows:
using System;
using System.Text;
using System.Collections;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
using System.Drawing;


namespace TestConsole
{

	class CMain : System.Windows.Forms.Form
	{

		Device D3DDevice;
		VertexBuffer vertices;
		VertexBuffer vertices2;

		protected void InitGraphics()
		{
			PresentParameters pres = new PresentParameters (); 

			pres.Windowed = true;
			pres.SwapEffect = SwapEffect.Discard ; 
			pres.BackBufferCount = 1;
			// pres.EnableAutoDepthStencil = true;
			// pres.AutoDepthStencilFormat = DepthFormat.D16;

			D3DDevice = new Device(0, DeviceType.Hardware , this, CreateFlags.SoftwareVertexProcessing , pres);

			// Vertex Buffer Stuff
			vertices = CreateVertexBuffer(D3DDevice); 
			vertices2 = CreateVertexBuffer2(D3DDevice);
		}


		protected VertexBuffer CreateVertexBuffer2 (Device device) 
		{ 

			CustomVertex.TransformedColored [] verts = 
				new CustomVertex.TransformedColored [3]; 

			verts [0] = 
				new CustomVertex.TransformedColored ( 
				this .Width / 2, this.Height / 4, 0.75F, 1, 
				Color.Blue.ToArgb ()); 
			verts [1] = 
				new CustomVertex.TransformedColored ( 
				this .Width * 3 / 4, this .Height * 3 / 4, 0.75F, 1, 
				Color.Green.ToArgb ()); 
			verts [2] = 
				new CustomVertex.TransformedColored ( 
				this .Width / 4, this .Height * 3 / 4, 0.75F, 1, 
				Color.Red.ToArgb ()); 

			VertexBuffer buf = 
				new VertexBuffer ( 
				typeof ( CustomVertex.TransformedColored ), 
				verts.Length , 
				device, 
				0, 
				CustomVertex.TransformedColored.Format , 
				Pool.Default 
				);

			GraphicsStream stm = buf.Lock (0, 0, 0); 
			stm.Write ( verts ); 
			buf.Unlock (); 
			return buf ; 
 
		}

		protected VertexBuffer CreateVertexBuffer (Device device) 
		{ 

			CustomVertex.TransformedColored [] verts = 
				new CustomVertex.TransformedColored [3]; 

			verts [0] = 
				new CustomVertex.TransformedColored ( 
				this .Width / 3, this.Height / 6, 0.5F, 1, 
				Color.Blue.ToArgb ()); 
			verts [1] = 
				new CustomVertex.TransformedColored ( 
				this .Width * 3 / 6, this .Height * 3 / 6, 0.5F, 1, 
				Color.Green.ToArgb ()); 
			verts [2] = 
				new CustomVertex.TransformedColored ( 
				this .Width / 6, this .Height * 3 / 6, 0.5F, 1, 
				Color.Red.ToArgb ()); 

			VertexBuffer buf = 
				new VertexBuffer ( 
				typeof ( CustomVertex.TransformedColored ), 
				verts.Length , 
				device, 
				0, 
				CustomVertex.TransformedColored.Format , 
				Pool.Default 
				);

			GraphicsStream stm = buf.Lock (0, 0, 0); 
			stm.Write ( verts ); 
			buf.Unlock (); 
			return buf ; 
 
		}


		protected void RenderGraphics()
		{

			D3DDevice.VertexFormat = CustomVertex.TransformedColored.Format;
			D3DDevice.RenderState.ZBufferEnable = true;
			D3DDevice.RenderState.ZBufferFunction = Compare.Greater;

			D3DDevice.BeginScene();
			D3DDevice.Clear(ClearFlags.Target, Color.Black, 1.0f, 0);

			D3DDevice.SetStreamSource(0, vertices2, 0);
			D3DDevice.DrawPrimitives(PrimitiveType.TriangleList, 0, 1);

			D3DDevice.SetStreamSource(0, vertices, 0);
			D3DDevice.DrawPrimitives(PrimitiveType.TriangleList, 0, 1);

			D3DDevice.EndScene();
			D3DDevice.Present();

		}

		// Program Entrypoint
		static void Main()
		{

			CMain app = new CMain();
			
			app.Width = 800;
			app.Height = 600;

			app.InitGraphics();
			

			app.Text = "Managed DirectX Test Sample";
			app.Show();


			while(app.Created)
			{
				app.RenderGraphics();
				Application.DoEvents();
			};


		}
	}
}

I tried turning on the Depth Buffer but that just makes it so the triangles flash up for the briefest of periods and then it all goes black. What am I doing wrong? Just how thick am I being?!!? Tech Dude Edited by Coder: Use source tags. Check GDNet Forums FAQ for formatting guidelines [Edited by - Coder on August 26, 2004 12:35:27 PM]

Share this post


Link to post
Share on other sites
Advertisement
You're rendering transformed and lit (transformed and colored) triangles. Those don't get processed by the geometry or lighting pipelines at all - they're not culled or affected by Z-values. They're drawn directly to the screen.

Share this post


Link to post
Share on other sites
Ah HA! Wrong...

I just fixed this and so feeling quite chuffed with myself.

I fixed it by enabled the Depth Buffer, Z Buffer which meant when I started the application the triangles flashed up for a second and then disappeared.

I then added a .Clear command to clear the ZBuffer to 0.0f each frame it works a treat!

I'll post some code in a minute

Tech Dude
*feels chuffed with himself*

Share this post


Link to post
Share on other sites
By the way - how do I use source tags?

Is it :

 Blah blah blah 



Oooo it is! Sweet - thanks bub

Share this post


Link to post
Share on other sites
Yes, as you noted, the Z buffer works with RHW coords, however they do skip transform, and viewport scaling.

One thing that's wrong though, you should clear the Z buffer to 1.0f (1.0f being the furthest possible Z after w divide), not 0. I'm surprised you see anything at all if you clear it to 0, unless your clear fails (by asking it to clear non-existant stencil bits for example).

Share this post


Link to post
Share on other sites
Yeah, i thought that was a bit weird as well, but when i clear my ZBuffer to 1.0f, everything just goes black.

And I checked and the .Clear function call is definatly working.

Weird huh?

Share this post


Link to post
Share on other sites
Quote:
Original post by Namethatnobodyelsetook
Yes, as you noted, the Z buffer works with RHW coords, however they do skip transform, and viewport scaling.

Man, that's news. Never knew they were z-tested... [smile]

Share this post


Link to post
Share on other sites
Are they tho? Cos i've tried putting the Z value in the RHW vertex component - doesn't make a blind bit of difference.

Can someone plug my code into a project and have a quick play around with it to figure out what the hell i'm doing wrong?!?!

Cheers,

Synex

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!