/
r_things.cpp
502 lines (424 loc) · 17 KB
/
r_things.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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
/** @file r_things.cpp Map Object => Vissprite Projection.
*
* @authors Copyright © 2003-2013 Jaakko Keränen <jaakko.keranen@iki.fi>
* @authors Copyright © 2006-2013 Daniel Swanson <danij@dengine.net>
* @authors Copyright © 2006 Jamie Jones <jamie_jones_au@yahoo.com.au>
* @authors Copyright © 1993-1996 by id Software, Inc.
*
* @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/vector1.h>
#include "de_platform.h"
#include "de_render.h"
#include "de_resource.h"
#include "dd_main.h" // App_World()
#include "def_main.h" // states
#include "gl/gl_tex.h"
#include "gl/gl_texmanager.h" // GL_PrepareFlaremap
#include "network/net_main.h" // clients[]
#include "world/map.h"
#include "world/p_object.h"
#include "world/p_players.h"
#include "BspLeaf"
#include "render/r_things.h"
using namespace de;
static void evaluateLighting(Vector3d const &origin, BspLeaf *bspLeafAtOrigin,
coord_t distToEye, bool fullbright, Vector4f &ambientColor, uint *vLightListIdx)
{
DENG_ASSERT(bspLeafAtOrigin != 0);
if(fullbright)
{
ambientColor = Vector3f(1, 1, 1);
*vLightListIdx = 0;
}
else
{
if(useBias && bspLeafAtOrigin->map().hasLightGrid())
{
// Evaluate the position in the light grid.
ambientColor = bspLeafAtOrigin->map().lightGrid().evaluate(origin);
}
else
{
SectorCluster const &cluster = bspLeafAtOrigin->cluster();
Vector3f const &secColor = Rend_SectorLightColor(cluster);
float lightLevel = cluster.sector().lightLevel();
/* if(spr->type == VSPR_DECORATION)
{
// Wall decorations receive an additional light delta.
lightLevel += R_WallAngleLightLevelDelta(line, side);
} */
// Apply distance attenuation.
lightLevel = Rend_AttenuateLightLevel(distToEye, lightLevel);
// Add extra light.
lightLevel = de::clamp(0.f, lightLevel + Rend_ExtraLightDelta(), 1.f);
Rend_ApplyLightAdaptation(lightLevel);
// Determine the final color.
ambientColor = secColor * lightLevel;
}
Rend_ApplyTorchLight(ambientColor, distToEye);
collectaffectinglights_params_t parm; zap(parm);
parm.origin[VX] = origin.x;
parm.origin[VY] = origin.y;
parm.origin[VZ] = origin.z;
parm.bspLeaf = bspLeafAtOrigin;
parm.ambientColor[0] = ambientColor.x;
parm.ambientColor[1] = ambientColor.y;
parm.ambientColor[2] = ambientColor.z;
*vLightListIdx = R_CollectAffectingLights(&parm);
}
}
/// @todo use Mobj_OriginSmoothed
static Vector3d mobjOriginSmoothed(mobj_t *mo)
{
DENG_ASSERT(mo != 0);
coord_t moPos[] = { mo->origin[VX], mo->origin[VY], mo->origin[VZ] };
// The client may have a Smoother for this object.
if(isClient && mo->dPlayer && P_GetDDPlayerIdx(mo->dPlayer) != consolePlayer)
{
Smoother_Evaluate(clients[P_GetDDPlayerIdx(mo->dPlayer)].smoother, moPos);
}
return moPos;
}
static inline spriteframe_t *spriteFrame(int sprite, int frame)
{
if(spritedef_t *sprDef = R_SpriteDef(sprite))
{
return SpriteDef_Frame(*sprDef, frame);
}
return 0;
}
struct findmobjzoriginworker_params_t
{
vissprite_t *vis;
mobj_t const *mo;
bool floorAdjust;
};
static int findMobjZOriginWorker(Sector *sector, void *parameters)
{
DENG_ASSERT(sector != 0);
DENG_ASSERT(parameters != 0);
findmobjzoriginworker_params_t *p = (findmobjzoriginworker_params_t *) parameters;
if(p->floorAdjust && p->mo->origin[VZ] == sector->floor().height())
{
p->vis->origin.z = sector->floor().heightSmoothed();
}
if(p->mo->origin[VZ] + p->mo->height == sector->ceiling().height())
{
p->vis->origin.z = sector->ceiling().heightSmoothed() - p->mo->height;
}
return false; // Continue iteration.
}
/**
* Determine the correct Z coordinate for the mobj. The visible Z coordinate
* may be slightly different than the actual Z coordinate due to smoothed
* plane movement.
*
* @todo fixme: Should use the visual plane heights of sector clusters.
*/
static void findMobjZOrigin(mobj_t *mo, bool floorAdjust, vissprite_t *vis)
{
DENG_ASSERT(mo != 0);
DENG_ASSERT(vis != 0);
findmobjzoriginworker_params_t params; zap(params);
params.vis = vis;
params.mo = mo;
params.floorAdjust = floorAdjust;
validCount++;
Mobj_TouchedSectorsIterator(mo, findMobjZOriginWorker, ¶ms);
}
void R_ProjectSprite(mobj_t *mo)
{
if(!mo) return;
// Not all objects can/will be visualized. Skip this object if:
// ...hidden?
if((mo->ddFlags & DDMF_DONTDRAW)) return;
// ...not linked into the map?
if(!Mobj_HasCluster(*mo)) return;
// ...in an invalid state?
if(!mo->state || mo->state == states) return;
// ...no sprite frame is defined?
spriteframe_t *sprFrame = spriteFrame(mo->sprite, mo->frame);
if(!sprFrame) return;
// ...fully transparent?
float const alpha = Mobj_Alpha(mo);
if(alpha <= 0) return;
// ...origin lies in a sector with no volume?
SectorCluster &cluster = Mobj_Cluster(*mo);
if(!cluster.hasWorldVolume()) return;
// Determine distance to object.
Vector3d const moPos = mobjOriginSmoothed(mo);
coord_t const distFromEye = Rend_PointDist2D(moPos);
// Should we use a 3D model?
modeldef_t *mf = 0, *nextmf = 0;
float interp = 0;
if(useModels)
{
interp = Models_ModelForMobj(mo, &mf, &nextmf);
if(mf)
{
// Use a sprite if the object is beyond the maximum model distance.
if(maxModelDistance && !(mf->flags & MFF_NO_DISTANCE_CHECK)
&& distFromEye > maxModelDistance)
{
mf = nextmf = 0;
interp = -1;
}
}
}
// Decide which material to use according to the sprite's angle and position
// relative to that of the viewer.
bool matFlipS, matFlipT;
Material *mat = SpriteFrame_Material(*sprFrame, mo->angle,
R_ViewPointToAngle(mo->origin),
mf != 0, &matFlipS, &matFlipT);
if(!mat) return;
// A valid sprite texture in the "Sprites" scheme is required.
MaterialSnapshot const &ms = mat->prepare(Rend_SpriteMaterialSpec(mo->tclass, mo->tmap));
if(!ms.hasTexture(MTU_PRIMARY))
return;
Texture &tex = ms.texture(MTU_PRIMARY).generalCase();
if(tex.manifest().schemeName().compareWithoutCase("Sprites"))
return;
bool const fullbright = ((mo->state->flags & STF_FULLBRIGHT) != 0 || levelFullBright);
// Align to the view plane? (Means scaling down Z with models)
bool const viewAlign = (!mf && ((mo->ddFlags & DDMF_VIEWALIGN) || alwaysAlign == 1))
|| alwaysAlign == 3;
/*
* Perform visibility checking by projecting a view-aligned line segment
* relative to the viewer and determining if the whole of the segment has
* been clipped away according to the 360 degree angle clipper.
*/
coord_t const visWidth = Mobj_VisualRadius(*mo) * 2; /// @todo ignorant of rotation...
Vector2d v1, v2;
R_ProjectViewRelativeLine2D(moPos, mf || viewAlign, visWidth,
(mf? 0 : coord_t(-tex.origin().x) - (visWidth / 2.0f)),
v1, v2);
// Not visible?
if(!C_CheckRangeFromViewRelPoints(v1, v2))
{
#define MAX_OBJECT_RADIUS 128
// Sprite visibility is absolute.
if(!mf) return;
// If the model is close to the viewpoint we should still to draw it,
// otherwise large models are likely to disappear too early.
viewdata_t const *viewData = R_ViewData(viewPlayer - ddPlayers);
Vector2d delta(distFromEye, moPos.z + (mo->height / 2) - viewData->current.origin[VZ]);
if(M_ApproxDistance(delta.x, delta.y) > MAX_OBJECT_RADIUS)
return;
#undef MAX_OBJECT_RADIUS
}
// Store information in a vissprite.
vissprite_t *vis = R_NewVisSprite(mf? VSPR_MODEL : VSPR_SPRITE);
vis->origin = moPos;
vis->distance = distFromEye;
/*
* The Z origin of the visual should match that of the mobj. When smoothing
* is enabled this requires examining all touched sector planes in the vicinity.
*/
Plane &floor = cluster.visFloor();
Plane &ceiling = cluster.visCeiling();
bool floorAdjust = false;
if(!Mobj_OriginBehindVisPlane(mo))
{
floorAdjust = de::abs(floor.heightSmoothed() - floor.height()) < 8;
findMobjZOrigin(mo, floorAdjust, vis);
}
coord_t gzt = vis->origin.z + -tex.origin().y;
// Determine floor clipping.
coord_t floorClip = mo->floorClip;
if(mo->ddFlags & DDMF_BOB)
{
// Bobbing is applied using floorclip.
floorClip += Mobj_BobOffset(mo);
}
float yaw = 0, pitch = 0;
if(mf)
{
// Determine the rotation angles (in degrees).
if(mf->testSubFlag(0, MFF_ALIGN_YAW))
{
// Transform the origin point.
viewdata_t const *viewData = R_ViewData(viewPlayer - ddPlayers);
Vector2d delta(moPos.y - viewData->current.origin[VY],
moPos.x - viewData->current.origin[VX]);
yaw = 90 - (BANG2RAD(bamsAtan2(delta.x * 10, delta.y * 10)) - PI / 2) / PI * 180;
}
else if(mf->testSubFlag(0, MFF_SPIN))
{
yaw = modelSpinSpeed * 70 * App_World().time() + MOBJ_TO_ID(mo) % 360;
}
else if(mf->testSubFlag(0, MFF_MOVEMENT_YAW))
{
yaw = R_MovementXYYaw(mo->mom[MX], mo->mom[MY]);
}
else
{
yaw = Mobj_AngleSmoothed(mo) / float( ANGLE_MAX ) * -360;
}
// How about a unique offset?
if(mf->testSubFlag(0, MFF_IDANGLE))
{
yaw += MOBJ_TO_ID(mo) % 360; // arbitrary
}
if(mf->testSubFlag(0, MFF_ALIGN_PITCH))
{
viewdata_t const *viewData = R_ViewData(viewPlayer - ddPlayers);
Vector2d delta((vis->origin.z + gzt) / 2 - viewData->current.origin[VZ], distFromEye);
pitch = -BANG2DEG(bamsAtan2(delta.x * 10, delta.y * 10));
}
else if(mf->testSubFlag(0, MFF_MOVEMENT_PITCH))
{
pitch = R_MovementXYZPitch(mo->mom[MX], mo->mom[MY], mo->mom[MZ]);
}
else
{
pitch = 0;
}
}
// Determine possible short-range visual offset.
Vector3d visOff;
if((mf && useSRVO > 0) || (!mf && useSRVO > 1))
{
if(mo->tics >= 0)
{
visOff = Vector3d(mo->srvo) * (mo->tics - frameTimePos) / (float) mo->state->tics;
}
if(!INRANGE_OF(mo->mom[MX], 0, NOMOMENTUM_THRESHOLD) ||
!INRANGE_OF(mo->mom[MY], 0, NOMOMENTUM_THRESHOLD) ||
!INRANGE_OF(mo->mom[MZ], 0, NOMOMENTUM_THRESHOLD))
{
// Use the object's speed to calculate a short-range offset.
visOff += Vector3d(mo->mom) * frameTimePos;
}
}
if(!mf)
{
bool const brightShadow = (mo->ddFlags & DDMF_BRIGHTSHADOW) != 0;
bool const fitTop = (mo->ddFlags & DDMF_FITTOP) != 0;
bool const fitBottom = (mo->ddFlags & DDMF_NOFITBOTTOM) == 0;
// Additive blending?
blendmode_t blendMode;
if(brightShadow)
{
blendMode = BM_ADD;
}
// Use the "no translucency" blending mode?
else if(noSpriteTrans && alpha >= .98f)
{
blendMode = BM_ZEROALPHA;
}
else
{
blendMode = BM_NORMAL;
}
// We must find the correct positioning using the sector floor
// and ceiling heights as an aid.
if(ms.height() < ceiling.heightSmoothed() - floor.heightSmoothed())
{
// Sprite fits in, adjustment possible?
if(fitTop && gzt > ceiling.heightSmoothed())
gzt = ceiling.heightSmoothed();
if(floorAdjust && fitBottom && gzt - ms.height() < floor.heightSmoothed())
gzt = floor.heightSmoothed() + ms.height();
}
// Adjust by the floor clip.
gzt -= floorClip;
Vector3d const origin(vis->origin.x, vis->origin.y, gzt - ms.height() / 2.0f);
Vector4f ambientColor;
uint vLightListIdx = 0;
evaluateLighting(origin, &Mobj_BspLeafAtOrigin(*mo), vis->distance, fullbright,
ambientColor, &vLightListIdx);
ambientColor.w = alpha;
VisSprite_SetupSprite(vis->data.sprite, origin, vis->distance, visOff,
floor.heightSmoothed(), ceiling.heightSmoothed(),
floorClip, gzt, *mat, matFlipS, matFlipT, blendMode,
ambientColor, vLightListIdx,
mo->tclass, mo->tmap,
&Mobj_BspLeafAtOrigin(*mo),
floorAdjust, fitTop, fitBottom, viewAlign);
}
else
{
Vector4f ambientColor;
uint vLightListIdx = 0;
evaluateLighting(vis->origin, &Mobj_BspLeafAtOrigin(*mo), vis->distance,
fullbright, ambientColor, &vLightListIdx);
ambientColor.w = alpha;
VisSprite_SetupModel(vis->data.model, vis->origin, vis->distance,
Vector3d(visOff.x, visOff.y, visOff.z - floorClip),
gzt, yaw, 0, pitch, 0,
mf, nextmf, interp,
ambientColor, vLightListIdx, mo->thinker.id, mo->selector,
&Mobj_BspLeafAtOrigin(*mo),
mo->ddFlags, mo->tmap, viewAlign,
fullbright && !(mf && mf->testSubFlag(0, MFF_DIM)), false);
}
// Do we need to project a flare source too?
if(mo->lumIdx != Lumobj::NoIndex)
{
/// @todo mark this light source visible for LensFx
Material *mat = SpriteFrame_Material(*sprFrame, mo->angle, R_ViewPointToAngle(mo->origin));
if(!mat) return;
// A valid sprite texture in the "Sprites" scheme is required.
MaterialSnapshot const &ms = mat->prepare(Rend_SpriteMaterialSpec(mo->tclass, mo->tmap));
if(!ms.hasTexture(MTU_PRIMARY))
return;
Texture &tex = ms.texture(MTU_PRIMARY).generalCase();
if(tex.manifest().schemeName().compareWithoutCase("Sprites"))
return;
pointlight_analysis_t const *pl = (pointlight_analysis_t const *)
ms.texture(MTU_PRIMARY).generalCase().analysisDataPointer(Texture::BrightPointAnalysis);
DENG_ASSERT(pl != 0);
Lumobj const *lum = cluster.sector().map().lumobj(mo->lumIdx);
vissprite_t *vis = R_NewVisSprite(VSPR_FLARE);
vis->distance = distFromEye;
// Determine the exact center of the flare.
vis->origin = moPos + visOff;
vis->origin.z += lum->zOffset();
float flareSize = pl->brightMul;
// X offset to the flare position.
float xOffset = ms.width() * pl->originX - -tex.origin().x;
// Does the mobj have an active light definition?
ded_light_t const *def = (mo->state? stateLights[mo->state - states] : 0);
if(def)
{
if(def->size)
flareSize = def->size;
if(def->haloRadius)
flareSize = def->haloRadius;
if(def->offset[VX])
xOffset = def->offset[VX];
vis->data.flare.flags = def->flags;
}
vis->data.flare.size = flareSize * 60 * (50 + haloSize) / 100.0f;
if(vis->data.flare.size < 8)
vis->data.flare.size = 8;
// Color is taken from the associated lumobj.
V3f_Set(vis->data.flare.color, lum->color().x, lum->color().y, lum->color().z);
vis->data.flare.factor = mo->haloFactors[viewPlayer - ddPlayers];
vis->data.flare.xOff = xOffset;
vis->data.flare.mul = 1;
vis->data.flare.tex = 0;
if(def && def->flare)
{
de::Uri const &flaremapResourceUri = *reinterpret_cast<de::Uri const *>(def->flare);
if(flaremapResourceUri.path().toStringRef().compareWithoutCase("-"))
{
vis->data.flare.tex = GL_PrepareFlaremap(flaremapResourceUri);
}
}
}
}