#include "Shadow.h"
CShadow::CShadow(LPDIRECT3DDEVICE9 device):m_pZPassPS(NULL),m_pZPassVS(NULL), m_pShadowmapVS(NULL), m_pShadowmapPS(NULL),m_pCShader(NULL),m_pD3DDevice(device), m_pShadowmap(NULL), m_pShadowmapDepthStencil(NULL), m_pShadowmapDepthStencilSurface(NULL), m_pShadowmapSurface(NULL)
{
}
CShadow::~CShadow(void)
{
Release(m_pD3DDevice);
Release(m_pShadowmap);
Release(m_pShadowmapDepthStencil);
Release(m_pShadowmapDepthStencilSurface);
Release(m_pShadowmapSurface);
Release(m_pShadowmapPS);
Release(m_pShadowmapVS);
Release(m_pZPassPS);
Release(m_pZPassVS);
Delete(m_pCShader);
}
void CShadow::init(){
m_pD3DDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
m_pD3DDevice->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
//init mesh
D3DXCreateTeapot(m_pD3DDevice, &m_pD3DMesh,0);
//init CShader
m_pCShader = new CShader(m_pD3DDevice);
//create texture
m_pD3DDevice->CreateTexture(1024,1024, 1, D3DUSAGE_RENDERTARGET, D3DFMT_R32F, D3DPOOL_DEFAULT, &m_pShadowmap,0);
m_pD3DDevice->CreateTexture(1024, 1024, 1, D3DUSAGE_DEPTHSTENCIL, D3DFMT_D24S8, D3DPOOL_DEFAULT, &m_pShadowmapDepthStencil,0);
m_pShadowmap->GetSurfaceLevel(0, &m_pShadowmapSurface);
m_pShadowmapDepthStencil->GetSurfaceLevel(0, &m_pShadowmapDepthStencilSurface);
m_pD3DDevice->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
m_pD3DDevice->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
m_pD3DDevice->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_NONE);
//load shader
m_pZPassVS = m_pCShader->LoadVertexShader("ZPass.hlsl", "VS", "vs_2_0");
m_pZPassPS = m_pCShader->LoadPixelShader("ZPass.hlsl", "PS", "ps_2_0");
m_pShadowmapVS = m_pCShader->LoadVertexShader("Shadowmap.hlsl", "VS", "vs_2_0");
m_pShadowmapPS = m_pCShader->LoadPixelShader("Shadowmap.hlsl", "PS_R32F", "ps_2_0");
//init lightmatrix
D3DXMatrixPerspectiveFovRH(&m_lightProjMatrix,45.0f, 1.0f, 0.5f, 50.0f);
D3DXMatrixLookAtRH(&m_lightViewMatrix,&D3DXVECTOR3(0.0f,10.0f, 10.0f), &D3DXVECTOR3(0.0f, 0.0f, 0.0f), &D3DXVECTOR3(0.0f, 1.0f, 0.0f));
float offset = 0.5f / 1024;
D3DXMatrixIdentity(&m_remapMatrix);
D3DXMatrixScaling(&m_remapMatrix, 0.5f, -0.5f, 1.0f);
m_remapMatrix._41 = 0.5f + offset;
m_remapMatrix._42 = 0.5f + offset;
m_remapMatrix._43 = 0.0f;
m_remapMatrix._44 = 1.0f;
}
void CShadow::Render(float timeDelta){
m_pD3DDevice->BeginScene();
m_pD3DDevice->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, true);
m_pD3DDevice->SetRenderState(D3DRS_LIGHTING, true);
LPDIRECT3DSURFACE9 pRenderTargetBackup(NULL);
LPDIRECT3DSURFACE9 pZSurfaceBackup(NULL);
m_pD3DDevice->GetRenderTarget(0, &pRenderTargetBackup);
m_pD3DDevice->GetDepthStencilSurface(&pZSurfaceBackup);
//init matrix
D3DXMATRIX matrix;
D3DXMatrixIdentity(&matrix);
D3DXMATRIX lightMatrix = matrix * m_lightViewMatrix * m_lightProjMatrix;
//pre render
m_pD3DDevice->SetRenderTarget(0, m_pShadowmapSurface);
m_pD3DDevice->SetDepthStencilSurface(m_pShadowmapDepthStencilSurface);
m_pD3DDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER | D3DCLEAR_STENCIL, D3DCOLOR_RGBA(255,255,255,255), 1.0f, 0);
m_pD3DDevice->SetVertexShaderConstantF(0, (float*)&lightMatrix, 4);
m_pD3DDevice->SetVertexShader(m_pZPassVS);
m_pD3DDevice->SetPixelShader(m_pZPassPS);
//render mesh to texture
m_pD3DMesh->DrawSubset(0);
//reset
m_pD3DDevice->SetVertexShader(NULL);
m_pD3DDevice->SetPixelShader(NULL);
m_pD3DDevice->SetRenderTarget(0, pRenderTargetBackup);
m_pD3DDevice->SetDepthStencilSurface(pZSurfaceBackup);
//clear buffer
m_pD3DDevice->Clear(0, NULL, D3DCLEAR_STENCIL | D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_RGBA(32,32,32,255),1.0f, 0);
//update matrix
D3DXMATRIX wvpMatrix =matrix * CCamera::GetInstance()->GetViewMatrix() * CCamera::GetInstance()->GetProjMatrix();
D3DXMATRIX zbias;
D3DXMatrixTranslation(&zbias, 0.0f, 0.0f, 0.01f);
D3DXMATRIX lightWvpMatrix = matrix* m_lightViewMatrix * zbias * m_lightProjMatrix * m_remapMatrix;
D3DXMATRIX lightWvMatrix = matrix * m_lightViewMatrix;
//set shader
m_pD3DDevice->SetVertexShaderConstantF(0, (float*)&wvpMatrix, 4);
m_pD3DDevice->SetVertexShaderConstantF(4, (float*)&lightWvpMatrix, 4);
m_pD3DDevice->SetVertexShaderConstantF(8, (float*)&lightWvMatrix,4);
m_pD3DDevice->SetVertexShader(m_pShadowmapVS);
m_pD3DDevice->SetPixelShader(m_pShadowmapPS);
//set texture
m_pD3DDevice->SetTexture(0, m_pShadowmap);
//render
m_pD3DMesh->DrawSubset(0);
//reset
m_pD3DDevice->SetVertexShader(NULL);
m_pD3DDevice->SetPixelShader(NULL);
m_pD3DDevice->SetTexture(0, NULL);
m_pD3DDevice->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, false);
m_pD3DDevice->EndScene();
m_pD3DDevice->Present(0,0,0,0);
}
- 1
- 2
- 3
前往页