Jump to content
  • Advertisement
Sign in to follow this  

Problem with Depth Buffer (i think)

This topic is 3949 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

I keep staring at this but just can't quite figure out where my problem is. Top View: Top View Bottom View Bottom View As you can see the cockpit's showing through the bottom and the missile ports are not showing up (can't really see it from these ss's). This screams depth buffer issue, to me at least, but I have been unable to locate the problem. Also there's plenty of space between the bottom of the cockpit and the bottom of the craft, so it shouldn't be the hardware confused about what's in front of what. Coding in C++, Direct3D9, and HLSL (the Tech doesn't set any render states atm) DX initialise:
pd3d9 = Direct3DCreate9 (D3D_SDK_VERSION);
if (!pd3d9)
	::MessageBoxA (0, "Direct3DCreate9 failed, Exiting", 0, 0);
	return false;

pd3d9->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &displayMode);
pd3d9->CheckDeviceType (D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, displayMode.Format, displayMode.Format, true);
pd3d9->CheckDeviceType (D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, D3DFMT_X8R8G8B8, D3DFMT_X8R8G8B8, false);

D3DCAPS9 caps;
pd3d9->GetDeviceCaps (D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &caps);

if (FAILED(pd3d9->CheckDeviceFormat (caps.AdapterOrdinal, caps.DeviceType, displayMode.Format, D3DUSAGE_RENDERTARGET, D3DRTYPE_TEXTURE, D3DFMT_A8R8G8B8)))
	return false;
rtFormat = D3DFMT_A8R8G8B8;

ZeroMemory (&d3dPP, sizeof (d3dPP));
d3dPP.BackBufferWidth            = 1; 
d3dPP.BackBufferHeight           = 1;
d3dPP.BackBufferFormat           = rtFormat;
d3dPP.BackBufferCount            = 1;
d3dPP.MultiSampleType            = D3DMULTISAMPLE_NONE;
d3dPP.MultiSampleQuality         = 0;
d3dPP.SwapEffect                 = D3DSWAPEFFECT_DISCARD; 
d3dPP.hDeviceWindow              = hWnd;
d3dPP.Windowed                   = true;
d3dPP.EnableAutoDepthStencil     = true; 
d3dPP.AutoDepthStencilFormat     = D3DFMT_D16;
d3dPP.Flags                      = 0;
d3dPP.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
d3dPP.PresentationInterval       = D3DPRESENT_INTERVAL_ONE;


pd3dDevice->SetRenderState (D3DRS_CULLMODE, D3DCULL_CCW);
pd3dDevice->SetRenderState (D3DRS_ZWRITEENABLE, TRUE);
if (!pd3dDevice)
	::MessageBoxA (0, "CreateDevice failed, Exiting", 0, 0);
	return false;

I have another function which is called after this one which Toggles whether the window is fullscreen or not. That's where the actual size of the current window is set. Effect:
PhongOutputVS PhongVS (float3 position : POSITION, float3 normal : NORMAL, float2 texCoord : TEXCOORD)
	PhongOutputVS outVS;
	float4 pos4 = float4 (position, 1.0f);
	outVS.position = mul (pos4, mWVP);
	outVS.texCoord = texCoord;
	outVS.normal = mul (float4(normal, 0.0f), mWIT).xyz;
	float3 worldPos = mul (pos4, mW).xyz;
	outVS.view = normalize (float4 (vViewPos, 0.0f) - worldPos);
	return outVS;

float4 PhongPS (float2 texCoord : TEXCOORD0, float3 normal : TEXCOORD1, float3 view : TEXCOORD2) : COLOR
	float3 norm = normalize (normal);
	float  NDotL = dot (norm, lightDir); 
	float3 reflection = normalize (((2.0f * norm) * NDotL) - lightDir); 
	float  RDotV = max (0.0f, dot (reflection, view));
	float4 baseColor = tex2D (TextureSampler, texCoord);
	float3 totalAmbient = daseMaterial[1].rgb * baseColor.rgb; 
	float3 totalDiffuse = daseMaterial[0].rgb * NDotL * baseColor.rgb; 
	float3 totalSpecular = daseMaterial[2].rgb * pow (RDotV, matPower);
	return float4 (saturate (totalAmbient + totalDiffuse + totalSpecular + daseMaterial[3].rgb), baseColor.a);
	return tex2D (TextureSampler, texCoord);

technique DirPhongTech
	pass P0
		VertexShader = compile vs_2_0 PhongVS();
		PixelShader  = compile ps_2_0 PhongPS();

Thanks for the help!

Share this post

Link to post
Share on other sites
Try D24X8 instead of D16.

Check your projection matrix. If the near plane is very small, you'll only get high precision right next to the camera. If the near plane is 0, nothing will work at all.

In addition to ZWRITEENABLE there is ZENABLE (set to true) and ZFUNC (set to D3CMP_LESS or D3DCMP_LESSEQUAL to accept lower or lower and equal depths).

Share this post

Link to post
Share on other sites
Thanks alot Namethatnobodyelsetook and Sc4Freak for responding. It was indeed the projection matrix, it was from 0.001 to 10000. Set it to .1 to 10000 (want it far as this is a space shooter) and it works again!

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!