   Direct3D9 Z-Buffer Problem
  Hello all, I am having problems in direct3d, I think it may be the zbuffer.Heres my problem:Red and Blue triangles are same dimensions, only difference is their Z.Red has a Z of 2.0f and Blue has a Z of 1.0f regarding vertices.So basically, blue is in front of red. But I can still see the red triangle! :(Heres my current settings:EnableAutoDepthStencil is enabled.AutoDepthStencilFormat is D3DFMT_D16.I have specified D3DCLEAR_ZBUFFER when using Clear().I am not using a W-Buffer, just plain ol' Z.Backface culling is on.D3DRS_ZWRITEENABLE is enabled.D3DRS_ZENABLE is enabled.Far and Near clipping planes is: 100.0f and 0.1f respectively.Camera View-Point is at the origin ( 0, 0, 0 ) and Position is ( 0, 0, -1.0 ).I have searched the forums, tried different things. To be honest I never used the Z-Buffer before, so I have no clue on what could be wrong.Also I wanted to know, are you supposed to draw back to front or front to back for Direct3D? Does this even matter when using a Z-Buffer?Please help! TIA   
  It sounds more like your transformation matrices are a bit funky. Can you post the setup code for it?;Quote:Original post by ShockFar and Near clipping planes is: 100.0f and 0.1f respectively.Try setting the near clip-plane to being >= 1.0f.Depth buffers in general can be quite prone to precision issues - which usually manifest themselves in strange artifacts.One of the common precision issues is found by setting the near-plane to be very close to the camera (less than 1.0). Setting it higher can sometimes magically correct everything [smile]hthJack   ;  Try walking through this Depth-Buffer Troubleshooter.  You may find what you are looking for there.The near & far planes look okay, because the ratio between them isn't so high.  It's when you have a higher far plane and really low near plane that you encounter problems.  However, it wouldn't hurt to try it, certainly.   ;  Ok you guys, thanks for the replies. I think there is something wrong with my perspective LH matrix code. If I use D3DXMatrixPerspectiveFovLH() the issue dissapears!Heres my left-handed perspective matrix code://****************************************************************************************////Matrix44_PerspectiveFOV() - Builds a perspective FOV ( left handed )//****************************************************************************************//inline void Matrix44_PerspectiveFOV( const float32 fov_y, const float32 aspect, const float32 _far, const float32 _near ){float32 y = 1.0f  / tanf( fov_y / 2.0f );float32 x = aspect / y;_11 = x;_12 = 0;_13 = 0;_14 = 0;_21 = 0;_22 = y;_23 = 0;_24 = 0;_31 = 0;_32 = 0;_33 = _far/(_far-_near);_34 = 1;_41 = 0;_42 = 0;_43 = -_near*_far/(_far-_near);_44 = 0; }I got the method from the dx9 sdk docs.   ;  This code float32 y = 1.0f  / tanf( fov_y / 2.0f ); should use a cot instead of a tan.;Quote:Original post by jollyjeffersTry setting the near clip-plane to being >= 1.0f....One of the common precision issues is found by setting the near-plane to be very close to the camera (less than 1.0)...That is wrong. The precision of the depth buffer depends on the relative distances to the near and far planes. You can set the near and far planes to 0.000000000000000000001 and 0.0000000000000000001 without any problem with precision.;Quote:This code float32 y = 1.0f / tanf( fov_y / 2.0f ); should use a cot instead of a tan.Heya,I would of used cot() but I couldn't find it in my standard math library. So I did some googling and found out that:cot(x) = 1.0f / tanf(x);Shouldn't this work exactly like cot() ?Thanks!   ;Quote:Original post by JohnBoltonQuote:Original post by jollyjeffersTry setting the near clip-plane to being >= 1.0f....One of the common precision issues is found by setting the near-plane to be very close to the camera (less than 1.0)...That is wrong. The precision of the depth buffer depends on the relative distances to the near and far planes. You can set the near and far planes to 0.000000000000000000001 and 0.0000000000000000001 without any problem with precision.My bad [headshake]Thanks,Jack   ;Quote:Original post by ShockQuote:This code float32 y = 1.0f / tanf( fov_y / 2.0f ); should use a cot instead of a tan.Heya,I would of used cot() but I couldn't find it in my standard math library. So I did some googling and found out that:cot(x) = 1.0f / tanf(x);You're right. I didn't look closely at your code, it seems - just spotted a tan there and thought it's wrong. Sorry [smile]The code looks good to me. Tried comparing the resultant matrix with that of D3DXMatrixPerspectiveLH?