This repository has been archived by the owner on Jan 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RenderHardwareShadow.cpp
158 lines (125 loc) · 3.64 KB
/
RenderHardwareShadow.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include "RenderHardwareShadow.h"
#include "DrawingUtil.h"
#include "CameraCtrl.h"
#include "RenderScene.h"
RenderHardwareShadow::RenderHardwareShadow(void)
{
m_pShadowMap = NULL;
m_pEffect = NULL;
m_pColorSurface = NULL;
}
RenderHardwareShadow::~RenderHardwareShadow(void)
{
destroy();
}
bool RenderHardwareShadow::create(IDirect3DDevice9 *pD3DDevice)
{
HRESULT hr;
//-- create shadow map
hr = pD3DDevice->CreateTexture(SHADOW_MAP_SIZE, SHADOW_MAP_SIZE,
1, D3DUSAGE_DEPTHSTENCIL, D3DFMT_D24X8,
D3DPOOL_DEFAULT, &m_pShadowMap, NULL);
if(FAILED(hr))
return false;
hr = pD3DDevice->CreateRenderTarget(SHADOW_MAP_SIZE, SHADOW_MAP_SIZE,
D3DFMT_X8R8G8B8,D3DMULTISAMPLE_NONE,0, FALSE,
&m_pColorSurface, NULL);
if(FAILED(hr))
return false;
//-- load effect
m_pEffect = DrawingUtil::getInst()->loadEffect(pD3DDevice,_T("Hardware.fx"));
return m_pEffect!=NULL;
}
void RenderHardwareShadow::destroy()
{
SAFE_RELEASE(m_pShadowMap);
SAFE_RELEASE(m_pEffect);
SAFE_RELEASE(m_pColorSurface);
}
class SetShadowMapZ
{
IDirect3DSurface9 *m_pLastRT;
IDirect3DSurface9 *m_pLastDepth;
IDirect3DDevice9 *m_pD3DDevice;
public:
SetShadowMapZ(IDirect3DDevice9 *pD3DDevice, IDirect3DTexture9* pDepth, IDirect3DSurface9* pColor)
{
m_pD3DDevice = pD3DDevice;
HRESULT hr;
hr = pD3DDevice->GetRenderTarget(0, &m_pLastRT);
if(FAILED(hr))
return;
hr = pD3DDevice->GetDepthStencilSurface(&m_pLastDepth);
if(FAILED(hr))
return;
IDirect3DSurface9 *pSurface = NULL;
hr = pDepth->GetSurfaceLevel(0, &pSurface );
if(FAILED(hr))
return;
hr = pD3DDevice->SetDepthStencilSurface(pSurface);
hr = pSurface->Release();
hr = pD3DDevice->SetRenderTarget(0, pColor);
hr = pD3DDevice->Clear(0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, 0, 1, 0);
}
~SetShadowMapZ()
{
HRESULT hr;
hr = m_pD3DDevice->SetRenderTarget(0, m_pLastRT);
hr = m_pD3DDevice->SetDepthStencilSurface(m_pLastDepth);
}
};
void RenderHardwareShadow::render(IDirect3DDevice9 *pD3DDevice, RenderScene* pScene)
{
if(m_pEffect == NULL)
return;
//-- 更新灯光view
updateLightView();
//-- 设置Effect的灯光参数
setupLightParam(m_pEffect);
//-- 生成Shadow map
m_pEffect->SetTechnique("genShadowMap");
{
SetShadowMapZ srt(pD3DDevice, m_pShadowMap, m_pColorSurface);
drawSceneMesh(m_pEffect, pScene, 0);
}
//-- 渲染整个场景
m_pEffect->SetTechnique("shdScene");
m_pEffect->SetTexture("g_texShadowMap", m_pShadowMap);
m_pEffect->SetMatrix("g_texScaleBiasMat",&m_texScaleBiasMat);
drawSceneMesh(m_pEffect, pScene, 1);
//-- 画出灯光的位置
DrawingUtil::getInst()->drawLight(m_lightPos);
DrawingUtil::getInst()->drawText(_T("Hardware Shadow Mapping"), 2, 16);
}
void RenderHardwareShadow::onScreenShoot()
{
HRESULT hr ;
if(m_pShadowMap)
{
hr = D3DXSaveTextureToFile(_T("shadowmap.dds"),D3DXIFF_DDS,m_pShadowMap, NULL);
}
}
void RenderHardwareShadow::preDrawMesh(RenderScene* pScene,int meshIndex, DWORD flag)
{
if(flag == 0)// gen shadow map
{
//-- 设置effect参数
D3DXMATRIX matWorld;
D3DXMatrixIdentity(&matWorld);
pScene->getMeshWorldMat(meshIndex, matWorld);
D3DXMATRIX matWorldViewProj = matWorld * m_lightView * m_lightProject;
m_pEffect->SetMatrix("g_lgtWorldViewPrj",&matWorldViewProj);
}
else
{
//-- 设置effect参数
D3DXMATRIX matWorld;
D3DXMatrixIdentity(&matWorld);
pScene->getMeshWorldMat(meshIndex, matWorld);
m_pEffect->SetMatrix("g_world",&matWorld);
D3DXMATRIX matWorldViewProj = matWorld * g_camera.getViewMat() * g_camera.getProjectMat();
m_pEffect->SetMatrix("g_worldViewPrj", &matWorldViewProj);
matWorldViewProj = matWorld * m_lightView * m_lightProject;
m_pEffect->SetMatrix("g_lgtWorldViewPrj", &matWorldViewProj);
}
}