Sign in to follow this  
Kai Jackson

[Solved] IDirect3DDevice9::Clear error

Recommended Posts

I've been learning DirectX over the last few days, and I seem to be having a problem with the Clear method for clearing the backbuffer: 

 

bool Draw()
{
	//clear the scene
	if (d3ddev->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,
		D3DCOLOR_XRGB(0, 0, 100), 1.0f, 0) == D3DERR_INVALIDCALL)
		return false;

	d3ddev->ColorFill(backbuffer, NULL, D3DCOLOR_XRGB(0, 0, 100));
	...
        return true;
}

 

d3ddev is of type LPDIRECT3DDEVICE9 and otherwise works fine - if I comment out the d3d->Clear(...) line the rest will function as intended, no other issues elsewhere in the code (ColorFill will run and fill the screen dark blue, which seems to achieve the same effect). Curious as to why Clear doesn't work though. Any ideas?

Share this post


Link to post
Share on other sites

Are you sure you have a zbuffer attached?

 

http://msdn.microsoft.com/en-us/library/windows/desktop/bb174352%28v=vs.85%29.aspx

 

I recall it misbehaving but I can't remember what I did to fix it. Is that fill there because it's not working or are you intending to do both? (that would be silly)

Edited by Khatharr

Share this post


Link to post
Share on other sites

I put the ColorFill in there as a test, it would indeed be silly to do both!

 

I removed D3DCLEAR_ZBUFFER and it works now. I'll be honest, I don't actually know what a zbuffer [i]is[/i] at this point. I'm sure I'll revisit this code in a week or two and think "oh, well duh" but for now I'm content that it works. Thanks!

Share this post


Link to post
Share on other sites

If you check a return value from a DirectX function, always use the FAILED or SUCCEEDED macro!

 

There are a lot more error codes than that single one. And there's a few border cases when there's more than one success value.

 

 

A z buffer is basically a buffer with a depth value for every pixel. Quite important if you go real 3d and objects overlap. Every single pixel will be depth checked.

Share this post


Link to post
Share on other sites

If you have a zbuffer (aka depth buffer) then whenever you render something the following steps are taken on a per-pixel basis:

new_z = //z value for this pixel based on what you're rendering
depth_z = //z value for that pixel in the zbuffer
if(depth_z > new_z) { //this test is configurable - it's called the 'depth test' (see MSDN)
  depth_z = new_z
  pixel_color = new_pixel_color
}
else {
  //skip to next pixel
}

Unless you're doing some special effects with the buffer you typically want to clear it every frame before drawing. The common use is simply to make sure that if you draw something and then draw another thing that overlaps the same pixels the correct pixel colors will be 'on top'.

 

http://en.wikipedia.org/wiki/Z-buffering

Edited by Khatharr

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this