Skip to main content
GameDev.net gamedev.net
🔒 Locked

Z-Buffer...?....

Started by sascoo Aug 28, 2008 at 1:37 PM 4 replies 700+ views
Original Post
sascoo
sascoo
I am implementing a model loader using FCollada and I finally got it semi-working. My problem is that I think the zbuffer is not working but I have checked everywhere in my code to make sure it is not being reset/messed with. I have way too much code to post so I made an exe which can be downloaded here Z Buffer....? If someone could look at it and tell me what they think the problem is other than the crappy model/texture ;) I would be very appreciative. Thanks, John
MJP
MJP
I can't run your app because I'm at work...but if you're having a z-buffer problem then it will manifest as triangles not being drawn in the proper order. In other words, you'll have triangle that are supposed to be block showing up on top of the other triangles.

If you tell me what API and language you're using, I can show you how to properly set z-buffer states.
sascoo
sascoo
I am using DirectX9 and I would just like your impressions on what you think the problem is. From what I get when running the program, it looks like it is a zbuffer issue but I have looked all over the code and do not see the problem... I was wondering if there was something else I needed to set.

Here is my code for initializing the directx

void DX_Graphics::Initialize(HWND hWnd){	d3d = Direct3DCreate9(D3D_SDK_VERSION);						// Get pointer to Direct3D interface	// Test for hardware capabilities	DWORD mRequestedVP = D3DCREATE_HARDWARE_VERTEXPROCESSING;	D3DDISPLAYMODE mode;	d3d->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &mode);	if(FAILED(d3d->CheckDeviceType(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,mode.Format, mode.Format,true)))		MessageBox(hWnd, "Error","CheckDeviceType failed", MB_ICONEXCLAMATION | MB_OK);	if(FAILED(d3d->CheckDeviceType(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,D3DFMT_X8R8G8B8, D3DFMT_X8R8G8B8,false)))		MessageBox(hWnd, "Error","CheckDeviceType failed", MB_ICONEXCLAMATION | MB_OK);	// Device capabilities	D3DCAPS9 caps;	if(FAILED(d3d->GetDeviceCaps(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,∩︀)))		MessageBox(hWnd, "Error","Device caps failed", MB_ICONEXCLAMATION | MB_OK);		devBehaviorFlags = 0;	if(caps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT)		devBehaviorFlags |= mRequestedVP;	else		devBehaviorFlags |= D3DCREATE_SOFTWARE_VERTEXPROCESSING;	if(caps.DevCaps & D3DDEVCAPS_PUREDEVICE && devBehaviorFlags & D3DCREATE_HARDWARE_VERTEXPROCESSING)		devBehaviorFlags |= D3DCREATE_PUREDEVICE;	bool hardwareVP = false;	bool pure = false;	if(caps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT)		hardwareVP = true;	if(caps.DevCaps & D3DDEVCAPS_PUREDEVICE)		pure = true;	ZeroMemory(&d3dpp, sizeof(d3dpp));							// Set d3dpp struct values to NULL	d3dpp.EnableAutoDepthStencil = TRUE;    d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8;	d3dpp.BackBufferCount = 1;	d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;					// set the back buffer format to 32-bit    d3dpp.BackBufferWidth = WIDTH;								// set the width of the buffer    d3dpp.BackBufferHeight = HEIGHT;							// set the height of the buffer	d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;	// Usually equal to the refresh rate	d3dpp.hDeviceWindow = hWnd;	d3dpp.MultiSampleType = D3DMULTISAMPLE_NONE;				// Anti-alias level	d3dpp.MultiSampleQuality = 0;								// Quality of anti-alias	d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;					// Describes how the SwapEffect deals whith old data in backbuffers.		if(isWindowed)												// Use this for windowed-mode		d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;	else														// Use for full-screen		d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT;	d3dpp.Windowed = isWindowed;								// Set to windowed or fullscreen.  Only sets the device to fullscreen, not the actual Win32 window.	// This is a large function with many parameters but it will probably stay the same for most games which get written.	d3d->CreateDevice(		D3DADAPTER_DEFAULT,										// Find the graphics card		D3DDEVTYPE_HAL,											// Used to tell device to use software or hardware (HAL --> Hardware Abstraction Layer)		hWnd,													// Handle to the window		//D3DCREATE_MULTITHREADED |								// Multithread safe		devBehaviorFlags,										// Use software, hardware, or mixed to calculate vertex		&d3dpp,													// Pointer to the D3DPRESENT_PARAMETERS struct		&d3ddev);												// This is a pointer to a pointer to the graphics device interface.		//------------------------------------------------------------------------------------		InitLight();	d3ddev->SetRenderState(D3DRS_LIGHTING, FALSE);				// turn on the 3D lighting	d3ddev->SetRenderState(D3DRS_ZENABLE, TRUE);				// turn on the z-buffer	d3ddev->SetRenderState(D3DRS_AMBIENT, D3DCOLOR_XRGB(50, 50, 50));    // ambient light	d3ddev->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);		// Turn on alphablending	d3ddev->SetRenderState(D3DRS_ALPHATESTENABLE, TRUE);		// Turn on testing for alpha	d3ddev->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);		// Set the type of blending	SetProjection();	return;}
Evil Steve
Evil Steve
I'm also at work so can't run your app. Any chance of a screenshot?

Two things I can think of, as guesses:
1. Are you clearing your Z-buffer in the call to IDirect3DDevice9::Clear() every frame?
2. Are your near and far clip planes set to something sensible in your projection matrix? The near clip plane must be non-zero (Ideally higher than 0.1), and I wouldn't make the distance between them any more than 10000 (And I'd be inclined to make it more like 1000).
sascoo
sascoo
Quote:
Original post by Evil Steve
I'm also at work so can't run your app. Any chance of a screenshot?

Two things I can think of, as guesses:
1. Are you clearing your Z-buffer in the call to IDirect3DDevice9::Clear() every frame?
2. Are your near and far clip planes set to something sensible in your projection matrix? The near clip plane must be non-zero (Ideally higher than 0.1), and I wouldn't make the distance between them any more than 10000 (And I'd be inclined to make it more like 1000).



AHHHHHH.....
Thanks, I forgot I was playing around with the clip planes a while ago and that was the problem. I set it to zero... I am glad you guys are here or I would not have figured that out.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.