z-buffer & direct draw
here is some code
///////////////////////////////////////////////////////////
HRESULT hr;
//get the size of the soon to be zbuffer
DDSURFACEDESC2 ddsd;
_DDRAW_INIT_STRUCT(ddsd);
lpddsback->GetSurfaceDesc(&ddsd);
ddsd.dwFlags = DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS | DDSD_PIXELFORMAT;
ddsd.ddsCaps.dwCaps = DDSCAPS_ZBUFFER | DDSCAPS_VIDEOMEMORY;
ddsd.ddpfPixelFormat.dwSize = 0;
//now enumerate the z buffer formats
lpd3d7->EnumZBufferFormats(*lpdevGUID,ZBufferEnumCallback,(void*)&ddsd.ddpfPixelFormat);
if(ddsd.ddpfPixelFormat.dwSize == 0)
{
//return some error here cause no formats were found;
}
//create and attach the z buffer
if(FAILED(hr = this->lpdd7->CreateSurface(&ddsd,&lpddszbuffer,NULL)))
{
return hr;
}
if(FAILED(hr = this->lpddsback->AddAttachedSurface(lpddszbuffer)))
{
return hr;
}
////////////////////////////////////////////////////////////
You have to write your own Callback function to accept or reject the zbuffer formats that were found when enumerating them. here is a simple one
HRESULT CALLBACK ZBufferEnumCallback(DDPIXELFORMAT *lpddpf, LPVOID lpcontext)
{
DDPIXELFORMAT *lpddpf_prim = (DDPIXELFORMAT*)lpcontext;
//if the pixel format of the zbuffer equals the current colordepth, then ok
if(lpddpf_prim->dwRGBBitCount == lpddpf->dwRGBBitCount)
{
memcpy(lpcontext,lpddpf,sizeof(DDPIXELFORMAT));
return D3DENUMRET_CANCEL;
}
//gets here if there is a confirm func but this one did not pass
return D3DENUMRET_OK;
}
Then, when all is said and done.. you should do this...
lpd3ddevice7->SetRenderTarget(lpddsback). I forgot to do this after attaching my zbuffer to the back buffer and D3D did not know the zbuffer was there.. It drove me nuts for a couple days.
that should do it. you can toggle whether D3D uses the zbuffer by the following
lpd3ddevice7->->SetRenderState(D3DRENDERSTATE_ZENABLE, D3DZB_TRUE);
enjoy,
BrianH
///////////////////////////////////////////////////////////
HRESULT hr;
//get the size of the soon to be zbuffer
DDSURFACEDESC2 ddsd;
_DDRAW_INIT_STRUCT(ddsd);
lpddsback->GetSurfaceDesc(&ddsd);
ddsd.dwFlags = DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS | DDSD_PIXELFORMAT;
ddsd.ddsCaps.dwCaps = DDSCAPS_ZBUFFER | DDSCAPS_VIDEOMEMORY;
ddsd.ddpfPixelFormat.dwSize = 0;
//now enumerate the z buffer formats
lpd3d7->EnumZBufferFormats(*lpdevGUID,ZBufferEnumCallback,(void*)&ddsd.ddpfPixelFormat);
if(ddsd.ddpfPixelFormat.dwSize == 0)
{
//return some error here cause no formats were found;
}
//create and attach the z buffer
if(FAILED(hr = this->lpdd7->CreateSurface(&ddsd,&lpddszbuffer,NULL)))
{
return hr;
}
if(FAILED(hr = this->lpddsback->AddAttachedSurface(lpddszbuffer)))
{
return hr;
}
////////////////////////////////////////////////////////////
You have to write your own Callback function to accept or reject the zbuffer formats that were found when enumerating them. here is a simple one
HRESULT CALLBACK ZBufferEnumCallback(DDPIXELFORMAT *lpddpf, LPVOID lpcontext)
{
DDPIXELFORMAT *lpddpf_prim = (DDPIXELFORMAT*)lpcontext;
//if the pixel format of the zbuffer equals the current colordepth, then ok
if(lpddpf_prim->dwRGBBitCount == lpddpf->dwRGBBitCount)
{
memcpy(lpcontext,lpddpf,sizeof(DDPIXELFORMAT));
return D3DENUMRET_CANCEL;
}
//gets here if there is a confirm func but this one did not pass
return D3DENUMRET_OK;
}
Then, when all is said and done.. you should do this...
lpd3ddevice7->SetRenderTarget(lpddsback). I forgot to do this after attaching my zbuffer to the back buffer and D3D did not know the zbuffer was there.. It drove me nuts for a couple days.
that should do it. you can toggle whether D3D uses the zbuffer by the following
lpd3ddevice7->->SetRenderState(D3DRENDERSTATE_ZENABLE, D3DZB_TRUE);
enjoy,
BrianH
Thank you for the answer, but I am not using Direct 3D.
I want to implement a z-buffer by software only with
Direct Draw.
Greetings
I want to implement a z-buffer by software only with
Direct Draw.
Greetings
quote:Original post by ajfg
Thank you for the answer, but I am not using Direct 3D.
I want to implement a z-buffer by software only with
Direct Draw.
Greetings
Ues Overlay.
One thing you could do is to give every 2D object a z value.
Then you have to make an algorithm that draws every object in the order you want.
Lets say that you have two objects one with z value 0 and one with z value 1. Then you draw all objects with z value 0 first, then all with z value 1 and continue. Then an object with a lower z value than the other will be drawn on top of the other.
Then you have to make an algorithm that draws every object in the order you want.
Lets say that you have two objects one with z value 0 and one with z value 1. Then you draw all objects with z value 0 first, then all with z value 1 and continue. Then an object with a lower z value than the other will be drawn on top of the other.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement