|
|
How to get the color of one pixel of shadow map?
I try to lock the m_pShadowMapSurf by LockRect function, but it return error of -2005530516.
LPDIRECT3DTEXTURE9 m_pShadowMapTexQuadNode; // shadow map objects
LPDIRECT3DSURFACE9 m_pShadowMapSurf;
LPDIRECT3DSURFACE9 m_pShadowMapZ;
HRESULT hr;
V_RETURN(pd3dDevice->CreateTexture(
QUADTREE_SM_SIZE,
QUADTREE_SM_SIZE,
1,
D3DUSAGE_RENDERTARGET,
SHADOW_MAP_FORMAT,
D3DPOOL_DEFAULT,
&m_pShadowMapTexQuadNode,
NULL
));
V_RETURN(m_pShadowMapTexQuadNode->GetSurfaceLevel(0, &m_pShadowMapSurf));
V_RETURN(pd3dDevice->CreateDepthStencilSurface(
QUADTREE_SM_SIZE,
QUADTREE_SM_SIZE,
D3DFMT_D24S8,
D3DMULTISAMPLE_NONE,
0,
TRUE,
&m_pShadowMapZ,
NULL
));
void CALLBACK NodeData::OnFrameRender_Shadowmap(IDirect3DDevice9* pd3dDevice,BOOL bSave)
{
HRESULT hr;
LPDIRECT3DSURFACE9 pOldBackBuffer, pOldZBuffer;
D3DXHANDLE hTechnique = g_pEffect->GetTechniqueByName("TShader");
g_pEffect->SetTechnique(hTechnique);
V(g_pEffect->Begin(NULL, 0));
// save old render target and set new render target with depth/stencil buffer
pd3dDevice->GetRenderTarget(0, &pOldBackBuffer);
pd3dDevice->GetDepthStencilSurface(&pOldZBuffer);
pd3dDevice->SetRenderTarget(0, m_pShadowMapSurf);
pd3dDevice->SetDepthStencilSurface(m_pShadowMapZ);
// clear render target
pd3dDevice->Clear(
0L,
NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,
0xffffffff,
1.0f,
0L
);
V(g_pEffect->BeginPass(0));
// render depth values into shadow map
CreateShadowMap();
V(g_pEffect->EndPass());
CalculateShadowPixelNumber();
// restore old backbuffer/stencil/depth-buffer and viewport
pd3dDevice->SetRenderTarget(0, pOldBackBuffer);
pd3dDevice->SetDepthStencilSurface(pOldZBuffer);
pOldBackBuffer->Release();
pOldZBuffer->Release();
for (int i=0;i<4;i++)
{
if (children)
children->data.OnFrameRender_Shadowmap(pd3dDevice,bSave);
}
}
void NodeData::CalculateShadowPixelNumber()
{
#define WHITE D3DCOLOR_XRGB(255, 255, 255)
D3DLOCKED_RECT lrect;
D3DDISPLAYMODE display;
m_nShadowPixelNumber=0;
HRESULT hr=m_pShadowMapSurf->LockRect(&lrect,NULL,D3DLOCK_NO_DIRTY_UPDATE|D3DLOCK_READONLY );
//-2005530516
for (int y=0;y<QUADTREE_SM_SIZE;y++)
{
for (int x=0;x<QUADTREE_SM_SIZE;x++)
{
DWORD dwColor = ((DWORD*)lrect.pBits)[y*(lrect.Pitch/sizeof(DWORD))+x];//Y*Width+X
USHORT r=GetRValue(dwColor);
USHORT g=GetGValue(dwColor);
USHORT b=GetBValue(dwColor);
if (dwColor!=WHITE)
{
m_nShadowPixelNumber++;
}
//DXUTOutputDebugString(L"r=%d,g=%d,b=%d\n",r,g,b);
}
}
m_pShadowMapSurf->UnlockRect();
//DWORD* p=(DWORD*)(lrect.pBits);
//p[y*(lrect.Pitch/sizeof(DWORD))+x]=RGB(255,0,0);
} |
|