/
materialsnapshot.cpp
582 lines (502 loc) · 20.1 KB
/
materialsnapshot.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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
/** @file materialsnapshot.cpp Logical material state snapshot.
*
* @authors Copyright © 2011-2013 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 <cstring> // memset
#include "de_base.h"
#ifdef __CLIENT__
# include "de_defs.h"
# include "render/lumobj.h" // Rend_LightmapTextureSpec()
# include "render/rend_halo.h" // Rend_HaloTextureSpec()
# include "render/rend_main.h" // detailFactor, detailScale, smoothTexAnim, etc...
# include "gl/gl_texmanager.h"
# include "gl/sys_opengl.h"
#endif
#include "Material"
#include "Texture"
#include <de/Log>
#include "MaterialSnapshot"
namespace de {
struct Store {
/// @c true= this material is completely opaque.
bool opaque;
/// Glow strength factor.
float glowStrength;
/// Dimensions in the world coordinate space.
Vector2i dimensions;
/// Minimum ambient light color for shine texture.
Vector3f shineMinColor;
/// Textures used on each logical material texture unit.
Texture::Variant *textures[NUM_MATERIAL_TEXTURE_UNITS];
#ifdef __CLIENT__
/// Decoration configuration.
MaterialSnapshot::Decoration decorations[Material::max_decorations];
/// Prepared render texture unit configuration. These map directly
/// to the texture units supplied to the render lists module.
rtexmapunit_t units[NUM_TEXMAP_UNITS];
#endif
Store() { initialize(); }
void initialize()
{
dimensions = Vector2i(0, 0);
shineMinColor = Vector3f(0, 0, 0);
opaque = true;
glowStrength = 0;
std::memset(textures, 0, sizeof(textures));
#ifdef __CLIENT__
std::memset(decorations, 0, sizeof(decorations));
for(int i = 0; i < NUM_TEXMAP_UNITS; ++i)
{
Rtu_Init(&units[i]);
}
#endif
}
#ifdef __CLIENT__
void writeTexUnit(rtexmapunitid_t unit, Texture::Variant *texture,
blendmode_t blendMode, Vector2f scale, Vector2f offset,
float opacity)
{
DENG2_ASSERT(unit >= 0 && unit < NUM_TEXMAP_UNITS);
rtexmapunit_t &tu = units[unit];
tu.texture.variant = texture;
tu.texture.flags = TUF_TEXTURE_IS_MANAGED;
tu.opacity = de::clamp(0.f, opacity, 1.f);
tu.blendMode = blendMode;
V2f_Set(tu.scale, scale.x, scale.y);
V2f_Set(tu.offset, offset.x, offset.y);
}
#endif // __CLIENT__
};
DENG2_PIMPL(MaterialSnapshot)
{
/// Variant material used to derive this snapshot.
MaterialVariant *variant;
Store stored;
Instance(Public *i, MaterialVariant &_variant) : Base(i),
variant(&_variant),
stored()
{}
void takeSnapshot();
};
MaterialSnapshot::MaterialSnapshot(MaterialVariant &materialVariant)
: d(new Instance(this, materialVariant))
{}
MaterialSnapshot::~MaterialSnapshot()
{
delete d;
}
MaterialVariant &MaterialSnapshot::materialVariant() const
{
return *d->variant;
}
Vector2i const &MaterialSnapshot::dimensions() const
{
return d->stored.dimensions;
}
bool MaterialSnapshot::isOpaque() const
{
return d->stored.opaque;
}
float MaterialSnapshot::glowStrength() const
{
return d->stored.glowStrength;
}
Vector3f const &MaterialSnapshot::shineMinColor() const
{
return d->stored.shineMinColor;
}
bool MaterialSnapshot::hasTexture(int index) const
{
if(index < 0 || index >= NUM_MATERIAL_TEXTURE_UNITS) return false;
return d->stored.textures[index] != 0;
}
Texture::Variant &MaterialSnapshot::texture(int index) const
{
if(!hasTexture(index))
{
/// @throw UnknownUnitError Attempt to dereference with an invalid index.
throw UnknownUnitError("MaterialSnapshot::texture", QString("Invalid texture index %1").arg(index));
}
return *d->stored.textures[index];
}
#ifdef __CLIENT__
rtexmapunit_t const &MaterialSnapshot::unit(rtexmapunitid_t id) const
{
if(id < 0 || id >= NUM_TEXMAP_UNITS)
{
/// @throw UnknownUnitError Attempt to obtain a reference to a unit with an invalid id.
throw UnknownUnitError("MaterialSnapshot::unit", QString("Invalid unit id %1").arg(id));
}
return d->stored.units[id];
}
MaterialSnapshot::Decoration &MaterialSnapshot::decoration(int index) const
{
if(index < 0 || index >= Material::max_decorations)
{
/// @throw UnknownDecorationError Attempt to obtain a reference to a decoration with an invalid index.
throw UnknownDecorationError("MaterialSnapshot::decoration", QString("Invalid decoration index %1").arg(index));
}
return d->stored.decorations[index];
}
static DGLuint prepareLightmap(Texture *texture)
{
if(texture)
{
if(TextureVariant *variant = GL_PrepareTexture(*texture, *Rend_LightmapTextureSpec()))
{
return variant->glName();
}
// Dang...
}
// Prepare the default lightmap instead.
return GL_PrepareLSTexture(LST_DYNAMIC);
}
/**
* Attempt to locate and prepare a flare texture.
* Somewhat more complicated than it needs to be due to the fact there
* are two different selection methods.
*
* @param texture Logical texture to prepare an variant of.
* @param oldIdx Old method of flare texture selection, by id.
*
* @return @c 0= Use the automatic selection logic.
*/
static DGLuint prepareFlaremap(Texture *texture, int oldIdx)
{
if(texture)
{
if(TextureVariant const *variant = GL_PrepareTexture(*texture, *Rend_HaloTextureSpec()))
{
return variant->glName();
}
// Dang...
}
else if(oldIdx > 0 && oldIdx < NUM_SYSFLARE_TEXTURES)
{
return GL_PrepareSysFlaremap(flaretexid_t(oldIdx - 1));
}
return 0; // Use the automatic selection logic.
}
#endif // __CLIENT__
/// @todo Implement more useful methods of interpolation. (What do we want/need here?)
void MaterialSnapshot::Instance::takeSnapshot()
{
#define LERP(start, end, pos) (end * pos + start * (1 - pos))
Material *material = &variant->generalCase();
Material::Layers const &layers = material->layers();
#ifdef __CLIENT__
Material::DetailLayer const *detailLayer = material->isDetailed()? &material->detailLayer() : 0;
Material::ShineLayer const *shineLayer = material->isShiny()? &material->shineLayer() : 0;
#endif
Texture::Variant *prepTextures[NUM_MATERIAL_TEXTURE_UNITS][2];
std::memset(prepTextures, 0, sizeof prepTextures);
// Reinitialize the stored values.
stored.initialize();
#ifdef __CLIENT__
/*
* Ensure all resources needed to visualize this have been prepared.
*
* If skymasked, we only need to update the primary tex unit (due to
* it being visible when skymask debug drawing is enabled).
*/
for(int i = 0; i < layers.count(); ++i)
{
MaterialAnimation::LayerState const &l = material->animation(variant->context())->layer(i);
Material::Layer::Stage const *lsCur = layers[i]->stages()[l.stage];
if(Texture *tex = lsCur->texture)
{
// Pick the instance matching the specified context.
preparetextureresult_t result;
prepTextures[i][0] = GL_PrepareTexture(*tex, *variant->spec().primarySpec, &result);
// Primary texture was (re)prepared?
if(i == 0 && l.stage == 0 &&
(PTR_UPLOADED_ORIGINAL == result || PTR_UPLOADED_EXTERNAL == result))
{
// Are we inheriting the logical dimensions from the texture?
if(material->width() == 0 && material->height() == 0)
{
material->setDimensions(tex->dimensions());
}
}
}
// Smooth Texture Animation?
if(!smoothTexAnim || layers[i]->stageCount() < 2) continue;
Material::Layer::Stage const *lsNext = layers[i]->stages()[(l.stage + 1) % layers[i]->stageCount()];
if(Texture *tex = lsNext->texture)
{
// Pick the instance matching the specified context.
preparetextureresult_t result;
prepTextures[i][1] = GL_PrepareTexture(*tex, *variant->spec().primarySpec, &result);
}
}
// Do we need to prepare detail texture(s)?
if(!material->isSkyMasked() && material->isDetailed())
{
MaterialAnimation::LayerState const &l = material->animation(variant->context())->detailLayer();
Material::DetailLayer::Stage const *lsCur = detailLayer->stages()[l.stage];
float const contrast = de::clamp(0.f, lsCur->strength, 1.f) * detailFactor /*Global strength multiplier*/;
texturevariantspecification_t &texSpec = GL_DetailTextureVariantSpecificationForContext(contrast);
if(Texture *tex = lsCur->texture)
{
// Pick the instance matching the specified context.
prepTextures[MTU_DETAIL][0] = GL_PrepareTexture(*tex, texSpec);
}
// Smooth Texture Animation?
if(smoothTexAnim && detailLayer->stageCount() > 1)
{
Material::DetailLayer::Stage const *lsNext = detailLayer->stages()[(l.stage + 1) % detailLayer->stageCount()];
if(Texture *tex = lsNext->texture)
{
// Pick the instance matching the specified context.
prepTextures[MTU_DETAIL][1] = GL_PrepareTexture(*tex, texSpec);
}
}
}
// Do we need to prepare a shiny texture (and possibly a mask)?
if(!material->isSkyMasked() && material->isShiny())
{
MaterialAnimation::LayerState const &l = material->animation(variant->context())->shineLayer();
Material::ShineLayer::Stage const *lsCur = shineLayer->stages()[l.stage];
if(Texture *tex = lsCur->texture)
{
texturevariantspecification_t &texSpec =
GL_TextureVariantSpec(TC_MAPSURFACE_REFLECTION, TSF_NO_COMPRESSION,
0, 0, 0, GL_REPEAT, GL_REPEAT, 1, 1, -1,
false, false, false, false);
// Pick the instance matching the specified context.
prepTextures[MTU_REFLECTION][0] = GL_PrepareTexture(*tex, texSpec);
}
// We are only interested in a mask if we have a shiny texture.
if(prepTextures[MTU_REFLECTION][0])
if(Texture *tex = lsCur->maskTexture)
{
texturevariantspecification_t &texSpec =
GL_TextureVariantSpec(TC_MAPSURFACE_REFLECTIONMASK, 0,
0, 0, 0, GL_REPEAT, GL_REPEAT, -1, -1, -1,
true, false, false, false);
// Pick the instance matching the specified context.
prepTextures[MTU_REFLECTION_MASK][0] = GL_PrepareTexture(*tex, texSpec);
}
}
#endif // __CLIENT__
stored.dimensions = material->dimensions();
#ifdef __CLIENT__
stored.opaque = (prepTextures[MTU_PRIMARY][0] && !prepTextures[MTU_PRIMARY][0]->isMasked());
#endif
if(stored.dimensions.x == 0 && stored.dimensions.y == 0) return;
MaterialAnimation::LayerState const &l = material->animation(variant->context())->layer(0);
Material::Layer::Stage const *lsCur = layers[0]->stages()[l.stage];
Material::Layer::Stage const *lsNext = layers[0]->stages()[(l.stage + 1) % layers[0]->stageCount()];
// Glow strength is presently taken from layer #0.
if(l.inter == 0)
{
stored.glowStrength = lsCur->glowStrength;
}
else // Interpolate.
{
stored.glowStrength = LERP(lsCur->glowStrength, lsNext->glowStrength, l.inter);
}
// Setup the primary texture unit.
if(TextureVariant *tex = prepTextures[MTU_PRIMARY][0])
{
stored.textures[MTU_PRIMARY] = tex;
#ifdef __CLIENT__
Vector2f offset;
if(l.inter == 0)
{
offset = lsCur->texOrigin;
}
else // Interpolate.
{
offset.x = LERP(lsCur->texOrigin.x, lsNext->texOrigin.x, l.inter);
offset.y = LERP(lsCur->texOrigin.y, lsNext->texOrigin.y, l.inter);
}
stored.writeTexUnit(RTU_PRIMARY, tex, BM_NORMAL,
Vector2f(1.f / stored.dimensions.x,
1.f / stored.dimensions.y),
offset, 1);
#endif
}
#ifdef __CLIENT__
// Setup the inter primary texture unit.
if(TextureVariant *tex = prepTextures[MTU_PRIMARY][1])
{
// If fog is active, inter=0 is accepted as well. Otherwise
// flickering may occur if the rendering passes don't match for
// blended and unblended surfaces.
if(!(!usingFog && l.inter == 0))
{
stored.writeTexUnit(RTU_INTER, tex, BM_NORMAL,
Vector2f(stored.units[RTU_PRIMARY].scale[0],
stored.units[RTU_PRIMARY].scale[1]),
Vector2f(stored.units[RTU_PRIMARY].offset[0],
stored.units[RTU_PRIMARY].offset[1]),
l.inter);
}
}
#endif
if(!material->isSkyMasked() && material->isDetailed())
{
#ifdef __CLIENT__
MaterialAnimation::LayerState const &l = material->animation(variant->context())->detailLayer();
Material::DetailLayer::Stage const *lsCur = detailLayer->stages()[l.stage];
Material::DetailLayer::Stage const *lsNext = detailLayer->stages()[(l.stage + 1) % detailLayer->stageCount()];
#endif
// Setup the detail texture unit.
if(TextureVariant *tex = prepTextures[MTU_DETAIL][0])
{
stored.textures[MTU_DETAIL] = tex;
#ifdef __CLIENT__
float scale;
if(l.inter == 0)
{
scale = lsCur->scale;
}
else // Interpolate.
{
scale = LERP(lsCur->scale, lsNext->scale, l.inter);
}
// Apply the global scale factor.
if(detailScale > .0001f)
scale *= detailScale;
stored.writeTexUnit(RTU_PRIMARY_DETAIL, tex, BM_NORMAL,
Vector2f(1.f / tex->generalCase().width() * scale,
1.f / tex->generalCase().height() * scale),
Vector2f(), 1);
#endif
}
#ifdef __CLIENT__
// Setup the inter detail texture unit.
if(TextureVariant *tex = prepTextures[MTU_DETAIL][1])
{
// If fog is active, inter=0 is accepted as well. Otherwise
// flickering may occur if the rendering passes don't match for
// blended and unblended surfaces.
if(!(!usingFog && l.inter == 0))
{
stored.writeTexUnit(RTU_INTER_DETAIL, tex, BM_NORMAL,
Vector2f(stored.units[RTU_PRIMARY_DETAIL].scale[0],
stored.units[RTU_PRIMARY_DETAIL].scale[1]),
Vector2f(stored.units[RTU_PRIMARY_DETAIL].offset[0],
stored.units[RTU_PRIMARY_DETAIL].offset[1]),
l.inter);
}
}
#endif
}
if(!material->isSkyMasked() && material->isShiny())
{
#ifdef __CLIENT__
MaterialAnimation::LayerState const &l = material->animation(variant->context())->shineLayer();
Material::ShineLayer::Stage const *lsCur = shineLayer->stages()[l.stage];
Material::ShineLayer::Stage const *lsNext = shineLayer->stages()[(l.stage + 1) % shineLayer->stageCount()];
#endif
// Setup the shine texture unit.
if(TextureVariant *tex = prepTextures[MTU_REFLECTION][0])
{
stored.textures[MTU_REFLECTION] = tex;
#ifdef __CLIENT__
Vector3f minColor;
for(int i = 0; i < 3; ++i)
{
if(l.inter == 0)
{
minColor[i] = lsCur->minColor[i];
}
else // Interpolate.
{
minColor[i] = LERP(lsCur->minColor[i], lsNext->minColor[i], l.inter);
}
minColor[i] = de::clamp(0.0f, minColor[i], 1.0f);
}
stored.shineMinColor = minColor;
float shininess;
if(l.inter == 0)
{
shininess = lsCur->shininess;
}
else // Interpolate.
{
shininess = LERP(lsCur->shininess, lsNext->shininess, l.inter);
}
shininess = de::clamp(0.0f, shininess, 1.0f);
stored.writeTexUnit(RTU_REFLECTION, tex, lsCur->blendMode,
Vector2f(1, 1), Vector2f(), shininess);
#endif
}
// Setup the shine mask texture unit.
if(prepTextures[MTU_REFLECTION][0])
if(TextureVariant *tex = prepTextures[MTU_REFLECTION_MASK][0])
{
stored.textures[MTU_REFLECTION_MASK] = tex;
#ifdef __CLIENT__
stored.writeTexUnit(RTU_REFLECTION_MASK, tex, BM_NORMAL,
Vector2f(1.f / (stored.dimensions.x * tex->generalCase().width()),
1.f / (stored.dimensions.y * tex->generalCase().height())),
Vector3f(stored.units[RTU_PRIMARY].offset[0],
stored.units[RTU_PRIMARY].offset[1]), 1);
#endif
}
}
#ifdef __CLIENT__
uint idx = 0;
Material::Decorations const &decorations = material->decorations();
for(Material::Decorations::const_iterator it = decorations.begin();
it != decorations.end(); ++it, ++idx)
{
MaterialAnimation::DecorationState const &l = material->animation(variant->context())->decoration(idx);
MaterialDecoration const *lDef = *it;
MaterialDecoration::Stage const *lsCur = lDef->stages()[l.stage];
MaterialDecoration::Stage const *lsNext = lDef->stages()[(l.stage + 1) % lDef->stageCount()];
MaterialSnapshot::Decoration &decor = stored.decorations[idx];
if(l.inter == 0)
{
decor.pos = lsCur->pos;
decor.elevation = lsCur->elevation;
decor.radius = lsCur->radius;
decor.haloRadius = lsCur->haloRadius;
decor.lightLevels[0] = lsCur->lightLevels.min;
decor.lightLevels[1] = lsCur->lightLevels.max;
decor.color = lsCur->color;
}
else // Interpolate.
{
decor.pos.x = LERP(lsCur->pos.x, lsNext->pos.x, l.inter);
decor.pos.y = LERP(lsCur->pos.y, lsNext->pos.y, l.inter);
decor.elevation = LERP(lsCur->elevation, lsNext->elevation, l.inter);
decor.radius = LERP(lsCur->radius, lsNext->radius, l.inter);
decor.haloRadius = LERP(lsCur->haloRadius, lsNext->haloRadius, l.inter);
decor.lightLevels[0] = LERP(lsCur->lightLevels.min, lsNext->lightLevels.min, l.inter);
decor.lightLevels[1] = LERP(lsCur->lightLevels.max, lsNext->lightLevels.max, l.inter);
for(int c = 0; c < 3; ++c)
{
decor.color[c] = LERP(lsCur->color[c], lsNext->color[c], l.inter);
}
}
decor.tex = prepareLightmap(lsCur->sides);
decor.ceilTex = prepareLightmap(lsCur->up);
decor.floorTex = prepareLightmap(lsCur->down);
decor.flareTex = prepareFlaremap(lsCur->flare, lsCur->sysFlareIdx);
}
#endif // __CLIENT__
#undef LERP
}
void MaterialSnapshot::update()
{
d->takeSnapshot();
}
} // namespace de