forked from TheSuperHackers/GeneralsGameCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathW3DShadow.cpp
More file actions
251 lines (207 loc) · 7.33 KB
/
W3DShadow.cpp
File metadata and controls
251 lines (207 loc) · 7.33 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
250
251
/*
** Command & Conquer Generals Zero Hour(tm)
** Copyright 2025 Electronic Arts Inc.
**
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
////////////////////////////////////////////////////////////////////////////////
// //
// (c) 2001-2003 Electronic Arts Inc. //
// //
////////////////////////////////////////////////////////////////////////////////
// FILE: W3DShadow.cpp ///////////////////////////////////////////////////////////
//
// Real time shadow representations
//
// Author: Mark Wilczynski, February 2002
//
//
// USER INCLUDES //////////////////////////////////////////////////////////////
#include "always.h"
#include "GameClient/View.h"
#include "WW3D2/camera.h"
#include "WW3D2/light.h"
#include "WW3D2/dx8wrapper.h"
#include "WW3D2/hlod.h"
#include "WW3D2/mesh.h"
#include "WW3D2/meshmdl.h"
#include "Lib/BaseType.h"
#include "W3DDevice/GameClient/HeightMap.h"
#include "d3dx8math.h"
#include "Common/GlobalData.h"
#include "W3DDevice/GameClient/W3DVolumetricShadow.h"
#include "W3DDevice/GameClient/W3DProjectedShadow.h"
#include "W3DDevice/GameClient/W3DShadow.h"
#include "WW3D2/statistics.h"
#include "Common/Debug.h"
#include "Common/PerfTimer.h"
#define SUN_DISTANCE_FROM_GROUND 10000.0f //distance of sun (our only light source).
// Global Variables and Functions /////////////////////////////////////////////
W3DShadowManager *TheW3DShadowManager=nullptr;
const FrustumClass *shadowCameraFrustum;
Vector3 LightPosWorld[ MAX_SHADOW_LIGHTS ] =
{
Vector3( 94.0161f, 50.499f, 200.0f)
};
void PrepareShadows()
{
if (TheW3DProjectedShadowManager)
TheW3DProjectedShadowManager->prepareShadows();
}
//DECLARE_PERF_TIMER(shadowsRender)
void DoShadows(RenderInfoClass & rinfo, Bool stencilPass)
{
//USE_PERF_TIMER(shadowsRender)
shadowCameraFrustum=&rinfo.Camera.Get_Frustum();
Int projectionCount=0;
//Projected shadows render first because they may fill the stencil buffer
//which will be used by the shadow volumes
if (stencilPass == FALSE && TheW3DProjectedShadowManager)
{
if (TheW3DShadowManager->isShadowScene())
projectionCount=TheW3DProjectedShadowManager->renderShadows(rinfo);
}
if (stencilPass == TRUE && TheW3DVolumetricShadowManager)
{
// TheW3DShadowManager->loadTerrainShadows();
//This function gets called many times by the W3D renderer
//so we use this flag to make sure shadows rendered only once per frame.
if (TheW3DShadowManager->isShadowScene())
TheW3DVolumetricShadowManager->renderShadows(projectionCount);
}
if (TheW3DShadowManager && stencilPass) //reset so no more shadow processing this frame.
TheW3DShadowManager->queueShadows(FALSE);
}
W3DShadowManager::W3DShadowManager()
{
DEBUG_ASSERTCRASH(TheW3DVolumetricShadowManager == nullptr && TheW3DProjectedShadowManager == nullptr,
("Creating new shadow managers without deleting old ones"));
m_shadowColor = 0x7fa0a0a0;
m_isShadowScene = FALSE;
m_stencilShadowMask = 0; //all bits can be used for storing shadows.
Vector3 lightRay(-TheGlobalData->m_terrainLightPos[0].x,
-TheGlobalData->m_terrainLightPos[0].y, -TheGlobalData->m_terrainLightPos[0].z);
lightRay.Normalize();
LightPosWorld[0]=lightRay*SUN_DISTANCE_FROM_GROUND;
TheW3DVolumetricShadowManager = NEW W3DVolumetricShadowManager;
TheProjectedShadowManager = TheW3DProjectedShadowManager = NEW W3DProjectedShadowManager;
}
W3DShadowManager::~W3DShadowManager()
{
delete TheW3DVolumetricShadowManager;
TheW3DVolumetricShadowManager = nullptr;
delete TheW3DProjectedShadowManager;
TheProjectedShadowManager = TheW3DProjectedShadowManager = nullptr;
}
/** Do one-time initilalization of shadow systems that need to be
active for full duration of game*/
Bool W3DShadowManager::init()
{
Bool result=TRUE;
if (TheW3DVolumetricShadowManager && TheW3DVolumetricShadowManager->init())
{
if (TheW3DVolumetricShadowManager->ReAcquireResources())
result = TRUE;
}
if ( TheW3DProjectedShadowManager && TheW3DProjectedShadowManager->init())
{
if (TheW3DProjectedShadowManager->ReAcquireResources())
result = TRUE;
}
return result;
}
/** Do per-map reset. This frees up shadows from all objects since
they may not exist on the next map*/
void W3DShadowManager::Reset()
{
if (TheW3DVolumetricShadowManager)
TheW3DVolumetricShadowManager->reset();
if (TheW3DProjectedShadowManager)
TheW3DProjectedShadowManager->reset();
}
Bool W3DShadowManager::ReAcquireResources()
{
Bool result = TRUE;
if (TheW3DVolumetricShadowManager && !TheW3DVolumetricShadowManager->ReAcquireResources())
result = FALSE;
if (TheW3DProjectedShadowManager && !TheW3DProjectedShadowManager->ReAcquireResources())
result = FALSE;
return result;
}
void W3DShadowManager::ReleaseResources()
{
if (TheW3DVolumetricShadowManager)
TheW3DVolumetricShadowManager->ReleaseResources();
if (TheW3DProjectedShadowManager)
TheW3DProjectedShadowManager->ReleaseResources();
}
Shadow *W3DShadowManager::addShadow( RenderObjClass *robj, Shadow::ShadowTypeInfo *shadowInfo, Drawable *draw)
{
ShadowType type = SHADOW_VOLUME;
if (shadowInfo)
type = shadowInfo->m_type;
switch(type)
{
case SHADOW_VOLUME:
if (TheW3DVolumetricShadowManager)
return (Shadow *)TheW3DVolumetricShadowManager->addShadow(robj, shadowInfo, draw);
break;
case SHADOW_PROJECTION:
case SHADOW_DECAL:
if (TheW3DProjectedShadowManager)
return (Shadow *)TheW3DProjectedShadowManager->addShadow(robj, shadowInfo, draw);
break;
default:
return nullptr;
}
return nullptr;
}
void W3DShadowManager::removeShadow(Shadow *shadow)
{
shadow->release();
}
void W3DShadowManager::removeAllShadows()
{
if (TheW3DVolumetricShadowManager)
TheW3DVolumetricShadowManager->removeAllShadows();
if (TheW3DProjectedShadowManager)
TheW3DProjectedShadowManager->removeAllShadows();
}
/**Force update of all shadows even when light source and object have not moved*/
void W3DShadowManager::invalidateCachedLightPositions()
{
if (TheW3DVolumetricShadowManager)
TheW3DVolumetricShadowManager->invalidateCachedLightPositions();
if (TheW3DProjectedShadowManager)
TheW3DProjectedShadowManager->invalidateCachedLightPositions();
}
Vector3 &W3DShadowManager::getLightPosWorld(Int lightIndex)
{
return LightPosWorld[lightIndex];
}
void W3DShadowManager::setLightPosition(Int lightIndex, Real x, Real y, Real z)
{
if (lightIndex != 0)
return; ///@todo: Add support for multiple lights
LightPosWorld[lightIndex]=Vector3(x,y,z);
}
void W3DShadowManager::setTimeOfDay(TimeOfDay tod)
{
//Ray to light source
const GlobalData::TerrainLighting *ol=&TheGlobalData->m_terrainObjectsLighting[tod][0];
Vector3 lightRay(-ol->lightPos.x,-ol->lightPos.y,-ol->lightPos.z);
lightRay.Normalize();
lightRay *= SUN_DISTANCE_FROM_GROUND;
setLightPosition(0, lightRay.X, lightRay.Y, lightRay.Z);
}