-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStageState.cpp
More file actions
249 lines (205 loc) · 7.66 KB
/
StageState.cpp
File metadata and controls
249 lines (205 loc) · 7.66 KB
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#include "StageState.h"
#include "3DSModel.h"
#include "GameEngine.h"
CStageState::CStageState(void) : CGameState("stage")
{
m_model = new C3DSModel();
m_model->LoadFromFile("bunny.3ds");
m_effect = NULL;
m_counter = 0;
m_stepTexture = LoadGameImage("step.png")->GetTexture();
}
CStageState::~CStageState(void)
{
delete m_model;
if (m_effect)
m_effect->Release();
}
void CStageState::Start(CGameState * prior)
{
CGameState::Start(prior);
LPDIRECT3DDEVICE9 device = GetDevice();
//if(FAILED(CheckResourceFormatSupport(device, D3DFMT_D24S8, D3DRTYPE_TEXTURE, D3DUSAGE_DEPTHSTENCIL)))
//{
// MessageBox(NULL, "Device/driver does not support hardware shadow maps!",
// "ERROR", MB_OK | MB_SETFOREGROUND | MB_TOPMOST);
// exit(1);
// return;
//}
if (!m_normalMap.CreateTexture(GetDevice(), GetScreenWidth(), GetScreenHeight()))
return;
if (!m_diffuseMap.CreateTexture(GetDevice(), GetScreenWidth(), GetScreenHeight()))
return;
if (!m_shadowMap.CreateTexture(GetDevice(), GetScreenWidth(), GetScreenHeight()))
return;
float fOffsetX = 0.5f + (0.5f / 640.0f);
float fOffsetY = 0.5f + (0.5f / 480.0f);
D3DXMATRIX texScaleBiasMat(0.5f, 0.0f, 0.0f, 0.0f,
0.0f, -0.5f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
fOffsetX, fOffsetY, 0.0f, 1.0f );
memcpy(&m_texMat, &texScaleBiasMat, sizeof(D3DXMATRIX));
m_effect = CreateEffectFromFile("a.fx");
}
void CStageState::Draw2D(CGame2D * g2d)
{
CGameState::Draw2D(g2d);
SetEffect(fxBlend);
m_effect->SetTexture("g_NormalMap", m_normalMap.GetTexture());
m_effect->SetTexture("g_DiffuseMap", m_diffuseMap.GetTexture());
m_effect->SetTexture("g_StepMap", m_stepTexture);
m_effect->SetTexture("g_ShadowMap", m_shadowMap.GetTexture());
m_effect->SetTechnique("RenderLine");
//m_effect->SetTechnique("RenderNormal");
// Apply the technique contained in the effect
DX_BEGIN_EFFECT(m_effect);
g2d->DrawTexture(m_normalMap.GetTexture(), 0, 0, (float)GetScreenWidth(), (float)GetScreenHeight());
//g2d->DrawTexture(m_normalMap.GetTexture(), 0, 0,
// (float)GetScreenWidth() / 2, (float)GetScreenHeight());
//g2d->DrawTexture(m_diffuseMap.GetTexture(), (float)GetScreenWidth() / 2, 0,
// (float)GetScreenWidth() / 2, (float)GetScreenHeight());
g2d->Flush();
DX_END_EFFECT(m_effect);
LPD3DXFONT font = LoadFont("Arial", 20);
g2d->DrawString(font, "dsadsa", 100, 300, 0xFF00FFFF);
}
void CStageState::PreRender()
{
LPDIRECT3DSURFACE9 backSurface;
GetDevice()->GetRenderTarget(0, &backSurface);
RenderShadowMap();
RenderNormalMap();
GetDevice()->SetRenderTarget(0, backSurface);
}
void CStageState::Draw3D()
{
CGameState::Draw3D();
GetCamera()->SetEye(150.0f, 150.0f, 100.0f);
GetCamera()->SetLookat(0, 0, 30);
GetCamera()->SetUp(0, 0, 1);
GetCamera()->UpdateMatrix();
D3DXMATRIX mat;
D3DXMatrixIdentity(&mat);
GetDevice()->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
GetDevice()->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
GetDevice()->SetTransform(D3DTS_WORLD, &mat);
// TextureSelect(m_target);
// m_model->Render();
}
void CStageState::Animate(float elapsedTime)
{
CGameState::Animate(elapsedTime);
m_counter += elapsedTime;
}
void CStageState::Update( float elapsedTime )
{
CGameState::Update(elapsedTime);
}
void CStageState::DeviceLost()
{
}
void CStageState::DeviceReset()
{
GetDevice()->SetRenderState( D3DRS_ZWRITEENABLE , TRUE );
GetDevice()->SetRenderState( D3DRS_DITHERENABLE , TRUE );
GetDevice()->SetRenderState( D3DRS_ZWRITEENABLE , TRUE );
}
void CStageState::RenderNormalMap()
{
SetEffect(fxCopy);
GetCamera()->SetEye(558.0f * sin(-m_counter / 4), 558.0f * cos(-m_counter/ 4), 100.0f);
GetCamera()->SetLookat(0, 0, 0);
GetCamera()->SetUp(0, 0, 1);
GetCamera()->UpdateMatrix();
D3DXMATRIXA16 mWorld;
D3DXMATRIXA16 mView;
D3DXMATRIXA16 mProj;
D3DXMATRIX mat;
D3DXMatrixRotationZ(&mat, 0);
GetDevice()->SetTransform(D3DTS_WORLD, &mat);
GetDevice()->GetTransform(D3DTS_WORLD, &mWorld);
GetDevice()->GetTransform(D3DTS_VIEW, &mView);
GetDevice()->GetTransform(D3DTS_PROJECTION, &mProj);
D3DXMATRIXA16 mWVP = mWorld * mView * mProj;
D3DXMATRIXA16 mWV = mWorld * mView;
D3DXMATRIXA16 mLT = m_lightMat * m_texMat;
m_effect->SetMatrix("g_mWorldViewProjection", &mWVP);
m_effect->SetMatrix("g_mWorldView", &mWV);
m_effect->SetMatrix("g_mWorld", &mWorld);
m_effect->SetMatrix("g_mLightWorldViewProjection", &mLT);
D3DXVECTOR3 Vec(0, 0, 0);
D3DXVECTOR4 Vec2;
D3DXVec3Transform(&Vec2, &Vec, &mLT);
m_effect->SetTexture("g_ShadowMap", m_shadowMap.GetTexture());
float offset[2] = {1.0f / 640.0f, 1.0f / 480.0f};
m_effect->SetFloatArray("g_fOffset", offset, 2);
m_normalMap.ApplyRenderTarget(GetDevice());
m_effect->SetTechnique("GenNormalMap");
GetDevice()->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_RGBA(0, 0, 0, 1), 1.0f, 0);
if (SUCCEEDED(GetDevice()->BeginScene()))
{
DX_BEGIN_EFFECT(m_effect);
m_model->Render();
DX_END_EFFECT(m_effect);
GetDevice()->EndScene();
}
m_diffuseMap.ApplyRenderTarget(GetDevice());
GetDevice()->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_RGBA(0, 0, 0, 1), 1.0f, 0);
m_effect->SetTechnique("GenDiffuseMap");
if (SUCCEEDED(GetDevice()->BeginScene()))
{
DX_BEGIN_EFFECT(m_effect);
m_model->Render();
DX_END_EFFECT(m_effect);
GetDevice()->EndScene();
}
}
void CStageState::RenderShadowMap()
{
SetEffect(fxCopy);
//GetCamera()->SetEye(358.0f, 358.0f, 100.0f);
//GetCamera()->SetLookat(0, 0, 0);
//GetCamera()->SetUp(0, 0, 1);
//GetCamera()->UpdateMatrix();
D3DXMATRIXA16 mWorld;
D3DXMATRIXA16 mView;
D3DXMATRIXA16 mProj;
D3DXMATRIX mat;
D3DXMatrixRotationZ(&mat, 0);
GetDevice()->SetTransform(D3DTS_WORLD, &mat);
GetDevice()->GetTransform(D3DTS_WORLD, &mWorld);
//GetDevice()->GetTransform(D3DTS_VIEW, &mView);
D3DXVECTOR3 EyePos (358.0f * sin(m_counter / 3), 358.0f * cos(m_counter/ 3), 100.0f);
D3DXVECTOR3 EyeLookat (0.0f, 0.0f, 0.0f);
D3DXVECTOR3 EyeUp (0.0f, 0.0f, 1.0f);
D3DXMatrixLookAtLH(&mView, &EyePos, &EyeLookat, &EyeUp);
GetDevice()->GetTransform(D3DTS_PROJECTION, &mProj);
m_lightMat = mWorld * mView * mProj;
m_effect->SetMatrix("g_mLightWorldViewProjection", &m_lightMat);
m_effect->SetFloatArray("g_LightDir", &EyePos.x, 3);
// LPDIRECT3DSURFACE9 depthSurface;
// GetDevice()->GetDepthStencilSurface(&depthSurface);
m_shadowMap.ApplyRenderTarget(GetDevice());
// GetDevice()->SetDepthStencilSurface(m_surfaceZ);
m_effect->SetTechnique("GenShadowMap");
GetDevice()->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_RGBA(1, 1, 1, 1), 1.0f, 0);
if (SUCCEEDED(GetDevice()->BeginScene()))
{
// Apply the technique contained in the effect
UINT cPasses;
if (SUCCEEDED(m_effect->Begin(&cPasses, 0)))
{
for (UINT iPass = 0; iPass < cPasses; iPass++)
{
if (SUCCEEDED(m_effect->BeginPass(iPass)))
{
m_model->Render();
m_effect->EndPass();
}
}
m_effect->End();
}
GetDevice()->EndScene();
}
// GetDevice()->SetDepthStencilSurface(depthSurface);
}