/
r_main.cpp
317 lines (264 loc) · 10.3 KB
/
r_main.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
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/** @file r_main.cpp
*
* @authors Copyright © 2003-2013 Jaakko Keränen <jaakko.keranen@iki.fi>
* @authors Copyright © 2006-2015 Daniel Swanson <danij@dengine.net>
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* <small>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 2 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, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA</small>
*/
#include "de_platform.h"
#include "render/r_main.h"
#include <de/vector1.h>
#include <de/GLState>
#include <doomsday/defs/sprite.h>
#include "dd_def.h" // finesine
#include "clientapp.h"
#include "render/billboard.h"
#include "render/rend_main.h"
#include "render/rend_model.h"
#include "render/vissprite.h"
#include "world/map.h"
#include "world/p_players.h"
#include "BspLeaf"
#include "ConvexSubspace"
#include "SectorCluster"
using namespace de;
dint levelFullBright;
dint weaponOffsetScaleY = 1000;
dint psp3d;
dfloat pspLightLevelMultiplier = 1;
dfloat pspOffset[2];
/*
* Console variables:
*/
dfloat weaponFOVShift = 45;
dfloat weaponOffsetScale = 0.3183f; // 1/Pi
dbyte weaponScaleMode = SCALEMODE_SMART_STRETCH;
static inline ResourceSystem &resSys()
{
return ClientApp::resourceSystem();
}
static inline WorldSystem &worldSys()
{
return ClientApp::worldSystem();
}
static MaterialVariantSpec const &pspriteMaterialSpec()
{
return resSys().materialSpec(PSpriteContext, 0, 1, 0, 0, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE,
0, -2, 0, false, true, true, false);
}
static void setupPSpriteParams(rendpspriteparams_t &parm, vispsprite_t const &vs)
{
static dint const WEAPONTOP = 32; /// @todo Currently hardcoded here and in the plugins.
dfloat const offScaleY = ::weaponOffsetScaleY / 1000.0f;
DENG2_ASSERT(vs.psp);
ddpsprite_t const &psp = *vs.psp;
DENG2_ASSERT(psp.statePtr);
state_t const &state = *psp.statePtr;
Record const &spriteView = defn::Sprite(resSys().sprite(state.sprite, state.frame)).view(0);
// Lookup the Material for this Sprite and prepare the animator.
MaterialAnimator &matAnimator = resSys().material(de::Uri(spriteView.gets("material"), RC_NULL))
.getAnimator(pspriteMaterialSpec());
matAnimator.prepare();
TextureVariant const &tex = *matAnimator.texUnit(MaterialAnimator::TU_LAYER0).texture;
Vector2i const &texOrigin = tex.base().origin();
variantspecification_t const &texSpec = tex.spec().variant;
parm.pos[0] = psp.pos[0] + texOrigin.x + pspOffset[0] - texSpec.border;
parm.pos[1] = WEAPONTOP + offScaleY * (psp.pos[1] - WEAPONTOP) + texOrigin.y
+ pspOffset[1] - texSpec.border;
Vector2i const dimensions = matAnimator.dimensions() + Vector2i(texSpec.border, texSpec.border) * 2;
parm.width = dimensions.x;
parm.height = dimensions.y;
tex.glCoords(&parm.texOffset[0], &parm.texOffset[1]);
parm.texFlip[0] = spriteView.getb("mirrorX");
parm.texFlip[1] = false;
parm.mat = &matAnimator.material();
parm.ambientColor[3] = vs.data.sprite.alpha;
if(vs.data.sprite.isFullBright)
{
parm.ambientColor[0] =
parm.ambientColor[1] =
parm.ambientColor[2] = 1;
parm.vLightListIdx = 0;
}
else
{
DENG2_ASSERT(vs.data.sprite.bspLeaf);
Map const &map = worldSys().map();
if(useBias && map.hasLightGrid())
{
// Evaluate the position in the light grid.
Vector4f color = map.lightGrid().evaluate(vs.origin);
// Apply light range compression.
for(dint i = 0; i < 3; ++i)
{
color[i] += Rend_LightAdaptationDelta(color[i]);
}
V3f_Set(parm.ambientColor, color.x, color.y, color.z);
}
else
{
Vector4f const color = vs.data.sprite.bspLeaf->subspace().cluster().lightSourceColorfIntensity();
// No need for distance attentuation.
dfloat lightLevel = color.w;
// Add extra light plus bonus.
lightLevel += Rend_ExtraLightDelta();
lightLevel *= pspLightLevelMultiplier;
// The last step is to compress the resultant light value by
// the global lighting function.
Rend_ApplyLightAdaptation(lightLevel);
// Determine the final ambientColor.
for(dint i = 0; i < 3; ++i)
{
parm.ambientColor[i] = lightLevel * color[i];
}
}
Rend_ApplyTorchLight(parm.ambientColor, 0);
parm.vLightListIdx =
Rend_CollectAffectingLights(vs.origin, Vector3f(parm.ambientColor),
vs.data.sprite.bspLeaf->subspacePtr());
}
}
void Rend_Draw2DPlayerSprites()
{
if(!viewPlayer) return;
ddplayer_t const &ddpl = viewPlayer->publicData();
// Cameramen have no HUD sprites.
if(ddpl.flags & DDPF_CAMERA ) return;
if(ddpl.flags & DDPF_CHASECAM) return;
if(usingFog)
{
glEnable(GL_FOG);
}
// Draw HUD vissprites.
for(vispsprite_t const &vs : visPSprites)
{
// We are only interested in sprites (models are handled elsewhere).
if(vs.type != VPSPR_SPRITE) continue; // No...
// We require PSprite and State info.
if(!vs.psp || !vs.psp->statePtr) continue;
try
{
rendpspriteparams_t parm; setupPSpriteParams(parm, vs);
Rend_DrawPSprite(parm);
}
catch(res::System::MissingResourceManifestError const &er)
{
// Log but otherwise ignore this error.
state_t const &state = *vs.psp->statePtr;
LOG_GL_WARNING("Drawing psprite '%i' frame '%i': %s")
<< state.sprite << state.frame << er.asText();
}
}
if(usingFog)
{
glDisable(GL_FOG);
}
}
static void setupModelParamsForVisPSprite(vissprite_t &vis, vispsprite_t const &spr)
{
drawmodelparams_t *params = VS_MODEL(&vis);
params->mf = spr.data.model.mf;
params->nextMF = spr.data.model.nextMF;
params->inter = spr.data.model.inter;
params->alwaysInterpolate = false;
params->id = spr.data.model.id;
params->selector = spr.data.model.selector;
params->flags = spr.data.model.flags;
vis.pose.origin = spr.origin;
vis.pose.srvo[0] = spr.data.model.visOff[0];
vis.pose.srvo[1] = spr.data.model.visOff[1];
vis.pose.srvo[2] = spr.data.model.visOff[2] - spr.data.model.floorClip;
vis.pose.topZ = spr.data.model.topZ;
vis.pose.distance = -10;
vis.pose.yaw = spr.data.model.yaw;
vis.pose.extraYawAngle = 0;
vis.pose.yawAngleOffset = spr.data.model.yawAngleOffset;
vis.pose.pitch = spr.data.model.pitch;
vis.pose.extraPitchAngle = 0;
vis.pose.pitchAngleOffset = spr.data.model.pitchAngleOffset;
vis.pose.extraScale = 0;
vis.pose.viewAligned = spr.data.model.viewAligned;
vis.pose.mirrored = (mirrorHudModels? true : false);
params->shineYawOffset = -vang;
params->shinePitchOffset = vpitch + 90;
params->shineTranslateWithViewerPos = false;
params->shinepspriteCoordSpace = true;
vis.light.ambientColor[3] = spr.data.model.alpha;
if((levelFullBright || spr.data.model.stateFullBright) &&
!spr.data.model.mf->testSubFlag(0, MFF_DIM))
{
vis.light.ambientColor[0] = vis.light.ambientColor[1] = vis.light.ambientColor[2] = 1;
vis.light.vLightListIdx = 0;
}
else
{
Map &map = ClientApp::worldSystem().map();
if(useBias && map.hasLightGrid())
{
Vector4f color = map.lightGrid().evaluate(vis.pose.origin);
// Apply light range compression.
for(dint i = 0; i < 3; ++i)
{
color[i] += Rend_LightAdaptationDelta(color[i]);
}
vis.light.ambientColor.x = color.x;
vis.light.ambientColor.y = color.y;
vis.light.ambientColor.z = color.z;
}
else
{
Vector4f const color = spr.data.model.bspLeaf->subspace().cluster().lightSourceColorfIntensity();
// No need for distance attentuation.
dfloat lightLevel = color.w;
// Add extra light.
lightLevel += Rend_ExtraLightDelta();
// The last step is to compress the resultant light value by
// the global lighting function.
Rend_ApplyLightAdaptation(lightLevel);
// Determine the final ambientColor.
for(dint i = 0; i < 3; ++i)
{
vis.light.ambientColor[i] = lightLevel * color[i];
}
}
Rend_ApplyTorchLight(vis.light.ambientColor, vis.pose.distance);
vis.light.vLightListIdx =
Rend_CollectAffectingLights(spr.origin, vis.light.ambientColor,
spr.data.model.bspLeaf->subspacePtr(),
true /*stark world light*/);
}
}
void Rend_Draw3DPlayerSprites()
{
// Setup the modelview matrix.
Rend_ModelViewMatrix(false /* don't apply view angle rotation */);
static GLTexture localDepth; // note: static!
GLTarget::AlternativeBuffer altDepth(GLState::current().target(), localDepth,
GLTarget::DepthStencil);
// Draw HUD vissprites.
for(vispsprite_t const &spr : visPSprites)
{
// We are only interested in models (sprites are handled elsewhere).
if(spr.type != VPSPR_MODEL) continue;
if(altDepth.init())
{
// Clear the depth before first use.
altDepth.target().clear(GLTarget::DepthStencil);
}
vissprite_t vs; de::zap(vs);
setupModelParamsForVisPSprite(vs, spr);
Rend_DrawModel(vs);
}
}