/
rend_model.cpp
1131 lines (944 loc) · 32.8 KB
/
rend_model.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
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/** @file rend_model.cpp 3D Model Rendering.
*
* @note Light vectors and triangle normals are in an entirely independent,
* right-handed coordinate system.
*
* There is some more confusion with Y and Z axes as the game uses Z as the
* vertical axis and the rendering code and model definitions use the Y axis.
*
* @authors Copyright © 2003-2013 Jaakko Keränen <jaakko.keranen@iki.fi>
* @authors Copyright © 2006-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 "clientapp.h"
#include "render/rend_model.h"
#include "render/rend_main.h"
#include "render/vlight.h"
#include "gl/gl_main.h"
#include "gl/gl_texmanager.h"
#include "network/net_main.h" // gametic
#include "MaterialSnapshot"
#include "MaterialVariantSpec"
#include "Texture"
#include "con_main.h"
#include "dd_def.h"
#include "dd_main.h" // App_WorldSystem()
#include <de/Log>
#include <de/binangle.h>
#include <de/memory.h>
#include <cstdlib>
#include <cmath>
#include <cstring>
using namespace de;
#define QATAN2(y,x) qatan2(y,x)
#define QASIN(x) asin(x) // @todo Precalculate arcsin.
static inline float qatan2(float y, float x)
{
float ang = BANG2RAD(bamsAtan2(y * 512, x * 512));
if(ang > PI) ang -= 2 * (float) PI;
return ang;
}
enum rendcmd_t
{
RC_COMMAND_COORDS,
RC_OTHER_COORDS,
RC_BOTH_COORDS
};
byte useModels = true;
int modelLight = 4;
int frameInter = true;
float modelAspectMod = 1 / 1.2f; //.833334f;
int mirrorHudModels;
int modelShinyMultitex = true;
float modelShinyFactor = 1.0f;
float modelSpinSpeed = 1;
int maxModelDistance = 1500;
float rend_model_lod = 256;
byte precacheSkins = true;
static bool inited;
struct array_t
{
bool enabled;
void *data;
};
#define MAX_ARRAYS (2 + MAX_TEX_UNITS)
static array_t arrays[MAX_ARRAYS];
// The global vertex render buffer.
static Vector3f *modelPosCoords;
static Vector3f *modelNormCoords;
static Vector4ub *modelColorCoords;
static Vector2f *modelTexCoords;
// Global variables for ease of use. (Egads!)
static Vector3f modelCenter;
static ModelDetailLevel *activeLod;
static uint vertexBufferMax; ///< Maximum number of vertices we'll be required to render per submodel.
static uint vertexBufferSize; ///< Current number of vertices supported by the render buffer.
#ifdef DENG_DEBUG
static bool announcedVertexBufferMaxBreach; ///< @c true if an attempt has been made to expand beyond our capability.
#endif
/*static void modelAspectModChanged()
{
/// @todo Reload and resize all models.
}*/
void Rend_ModelRegister()
{
C_VAR_BYTE ("rend-model", &useModels, 0, 0, 1);
C_VAR_INT ("rend-model-lights", &modelLight, 0, 0, 10);
C_VAR_INT ("rend-model-inter", &frameInter, 0, 0, 1);
C_VAR_FLOAT("rend-model-aspect", &modelAspectMod, CVF_NO_MAX | CVF_NO_MIN, 0, 0);
C_VAR_INT ("rend-model-distance", &maxModelDistance, CVF_NO_MAX, 0, 0);
C_VAR_BYTE ("rend-model-precache", &precacheSkins, 0, 0, 1);
C_VAR_FLOAT("rend-model-lod", &rend_model_lod, CVF_NO_MAX, 0, 0);
C_VAR_INT ("rend-model-mirror-hud", &mirrorHudModels, 0, 0, 1);
C_VAR_FLOAT("rend-model-spin-speed", &modelSpinSpeed, CVF_NO_MAX | CVF_NO_MIN, 0, 0);
C_VAR_INT ("rend-model-shiny-multitex", &modelShinyMultitex, 0, 0, 1);
C_VAR_FLOAT("rend-model-shiny-strength", &modelShinyFactor, 0, 0, 10);
}
void Rend_ModelInit()
{
if(inited) return; // Already been here.
modelPosCoords = 0;
modelNormCoords = 0;
modelColorCoords = 0;
modelTexCoords = 0;
vertexBufferMax = vertexBufferSize = 0;
#ifdef DENG_DEBUG
announcedVertexBufferMaxBreach = false;
#endif
inited = true;
}
void Rend_ModelShutdown()
{
if(!inited) return;
M_Free(modelPosCoords); modelPosCoords = 0;
M_Free(modelNormCoords); modelNormCoords = 0;
M_Free(modelColorCoords); modelColorCoords = 0;
M_Free(modelTexCoords); modelTexCoords = 0;
vertexBufferMax = vertexBufferSize = 0;
#ifdef DENG_DEBUG
announcedVertexBufferMaxBreach = false;
#endif
inited = false;
}
bool Rend_ModelExpandVertexBuffers(uint numVertices)
{
DENG2_ASSERT(inited);
LOG_AS("Rend_ModelExpandVertexBuffers");
if(numVertices <= vertexBufferMax) return true;
// Sanity check a sane maximum...
if(numVertices >= RENDER_MAX_MODEL_VERTS)
{
#ifdef DENG_DEBUG
if(!announcedVertexBufferMaxBreach)
{
LOGDEV_GL_WARNING("Attempted to expand to %u vertices (max %u)")
<< numVertices << RENDER_MAX_MODEL_VERTS;
announcedVertexBufferMaxBreach = true;
}
#endif
return false;
}
// Defer resizing of the render buffer until draw time as it may be repeatedly expanded.
vertexBufferMax = numVertices;
return true;
}
/// @return @c true= Vertex buffer is large enough to handle @a numVertices.
static bool resizeVertexBuffer(uint numVertices)
{
// Mark the vertex buffer if a resize is necessary.
Rend_ModelExpandVertexBuffers(numVertices);
// Do we need to resize the buffers?
if(vertexBufferMax != vertexBufferSize)
{
/// @todo Align access to this memory along a 4-byte boundary?
modelPosCoords = (Vector3f *) M_Realloc(modelPosCoords, sizeof(*modelPosCoords) * vertexBufferMax);
modelNormCoords = (Vector3f *) M_Realloc(modelNormCoords, sizeof(*modelNormCoords) * vertexBufferMax);
modelColorCoords = (Vector4ub *) M_Realloc(modelColorCoords, sizeof(*modelColorCoords) * vertexBufferMax);
modelTexCoords = (Vector2f *) M_Realloc(modelTexCoords, sizeof(*modelTexCoords) * vertexBufferMax);
vertexBufferSize = vertexBufferMax;
}
// Is the buffer large enough?
return vertexBufferSize >= numVertices;
}
static void disableArrays(int vertices, int colors, int coords)
{
DENG_ASSERT_IN_MAIN_THREAD();
DENG_ASSERT_GL_CONTEXT_ACTIVE();
if(vertices)
{
arrays[AR_VERTEX].enabled = false;
}
if(colors)
{
arrays[AR_COLOR].enabled = false;
}
for(int i = 0; i < numTexUnits; ++i)
{
if(coords & (1 << i))
{
arrays[AR_TEXCOORD0 + i].enabled = false;
}
}
DENG_ASSERT(!Sys_GLCheckError());
}
static inline void enableTexUnit(byte id)
{
DENG_ASSERT_IN_MAIN_THREAD();
DENG_ASSERT_GL_CONTEXT_ACTIVE();
glActiveTexture(GL_TEXTURE0 + id);
glEnable(GL_TEXTURE_2D);
}
static inline void disableTexUnit(byte id)
{
DENG_ASSERT_IN_MAIN_THREAD();
DENG_ASSERT_GL_CONTEXT_ACTIVE();
glActiveTexture(GL_TEXTURE0 + id);
glDisable(GL_TEXTURE_2D);
// Implicit disabling of texcoord array.
disableArrays(0, 0, 1 << id);
}
/**
* The first selected unit is active after this call.
*/
static void selectTexUnits(int count)
{
for(int i = numTexUnits - 1; i >= count; i--)
{
disableTexUnit(i);
}
// Enable the selected units.
for(int i = count - 1; i >= 0; i--)
{
if(i >= numTexUnits) continue;
enableTexUnit(i);
}
}
/**
* Enable, set and optionally lock all enabled arrays.
*/
static void configureArrays(void *vertices, void *colors, int numCoords = 0,
void **coords = 0, int lock = 0)
{
DENG_ASSERT_IN_MAIN_THREAD();
DENG_ASSERT_GL_CONTEXT_ACTIVE();
if(vertices)
{
arrays[AR_VERTEX].enabled = true;
arrays[AR_VERTEX].data = vertices;
}
if(colors)
{
arrays[AR_COLOR].enabled = true;
arrays[AR_COLOR].data = colors;
}
for(int i = 0; i < numCoords && i < MAX_TEX_UNITS; ++i)
{
if(coords[i])
{
arrays[AR_TEXCOORD0 + i].enabled = true;
arrays[AR_TEXCOORD0 + i].data = coords[i];
}
}
DENG_ASSERT(!Sys_GLCheckError());
}
static void drawArrayElement(int index)
{
DENG_ASSERT_IN_MAIN_THREAD();
DENG_ASSERT_GL_CONTEXT_ACTIVE();
for(int i = 0; i < numTexUnits; ++i)
{
if(!arrays[AR_TEXCOORD0 + i].enabled) continue;
Vector2f const &texCoord = ((Vector2f const *)arrays[AR_TEXCOORD0 + i].data)[index];
glMultiTexCoord2f(GL_TEXTURE0 + i, texCoord.x, texCoord.y);
}
if(arrays[AR_COLOR].enabled)
{
Vector4ub const &colorCoord = ((Vector4ub const *) arrays[AR_COLOR].data)[index];
glColor4ub(colorCoord.x, colorCoord.y, colorCoord.z, colorCoord.w);
}
if(arrays[AR_VERTEX].enabled)
{
Vector3f const &posCoord = ((Vector3f const *) arrays[AR_VERTEX].data)[index];
glVertex3f(posCoord.x, posCoord.y, posCoord.z);
}
}
/**
* Return a pointer to the visible model frame.
*/
static ModelFrame &visibleModelFrame(ModelDef &modef, int subnumber, int mobjId)
{
if(subnumber >= int(modef.subCount()))
{
throw Error("Rend_DrawModel.visibleFrame",
QString("Model has %1 submodels, but submodel #%2 was requested")
.arg(modef.subCount()).arg(subnumber));
}
SubmodelDef const &sub = modef.subModelDef(subnumber);
int curFrame = sub.frame;
if(modef.flags & MFF_IDFRAME)
{
curFrame += mobjId % sub.frameRange;
}
return App_ResourceSystem().model(sub.modelId).frame(curFrame);
}
/**
* Render a set of 3D model primitives using the given data.
*/
static void drawPrimitives(rendcmd_t mode, Model::Primitives const &primitives,
Vector3f *posCoords, Vector4ub *colorCoords, Vector2f *texCoords = 0)
{
DENG_ASSERT_IN_MAIN_THREAD();
DENG_ASSERT_GL_CONTEXT_ACTIVE();
// Disable all vertex arrays.
disableArrays(true, true, DDMAXINT);
// Load the vertex array.
void *coords[2];
switch(mode)
{
case RC_OTHER_COORDS:
coords[0] = texCoords;
configureArrays(posCoords, colorCoords, 1, coords);
break;
case RC_BOTH_COORDS:
coords[0] = NULL;
coords[1] = texCoords;
configureArrays(posCoords, colorCoords, 2, coords);
break;
default:
configureArrays(posCoords, colorCoords);
break;
}
foreach(Model::Primitive const &prim, primitives)
{
// The type of primitive depends on the sign.
glBegin(prim.triFan? GL_TRIANGLE_FAN : GL_TRIANGLE_STRIP);
foreach(Model::Primitive::Element const &elem, prim.elements)
{
if(mode != RC_OTHER_COORDS)
{
glTexCoord2f(elem.texCoord.x, elem.texCoord.y);
}
drawArrayElement(elem.index);
}
// The primitive is complete.
glEnd();
}
}
/**
* Interpolate linearly between two sets of vertices.
*/
static void Mod_LerpVertices(float inter, int count, ModelFrame const &from,
ModelFrame const &to, Vector3f *posOut, Vector3f *normOut)
{
DENG2_ASSERT(&from.model == &to.model); // sanity check.
DENG2_ASSERT(!activeLod || &activeLod->model == &from.model); // sanity check.
DENG2_ASSERT(from.vertices.count() == to.vertices.count()); // sanity check.
ModelFrame::VertexBuf::const_iterator startIt = from.vertices.begin();
ModelFrame::VertexBuf::const_iterator endIt = to.vertices.begin();
if(&from == &to || de::fequal(inter, 0))
{
for(int i = 0; i < count; ++i, startIt++, posOut++, normOut++)
{
if(!activeLod || activeLod->hasVertex(i))
{
*posOut = startIt->pos;
*normOut = startIt->norm;
}
}
}
else
{
for(int i = 0; i < count; ++i, startIt++, endIt++, posOut++, normOut++)
{
if(!activeLod || activeLod->hasVertex(i))
{
*posOut = de::lerp(startIt->pos, endIt->pos, inter);
*normOut = de::lerp(startIt->norm, endIt->norm, inter);
}
}
}
}
static void Mod_MirrorCoords(int count, Vector3f *coords, int axis)
{
for(; count-- > 0; coords++)
{
(*coords)[axis] = -(*coords)[axis];
}
}
struct lightmodelvertexworker_params_t
{
Vector3f color, extra;
float rotateYaw, rotatePitch;
Vector3f normal;
uint numProcessed, max;
bool invert;
};
static void lightModelVertex(VectorLight const &vlight, lightmodelvertexworker_params_t &parms)
{
// We must transform the light vector to model space.
float vlightDirection[3] = { vlight.direction.x, vlight.direction.y, vlight.direction.z };
M_RotateVector(vlightDirection, parms.rotateYaw, parms.rotatePitch);
// Quick hack: Flip light normal if model inverted.
if(parms.invert)
{
vlightDirection[VX] = -vlightDirection[VX];
vlightDirection[VY] = -vlightDirection[VY];
}
float strength = Vector3f(vlightDirection).dot(parms.normal)
+ vlight.offset; // Shift a bit towards the light.
// Ability to both light and shade.
if(strength > 0)
{
strength *= vlight.lightSide;
}
else
{
strength *= vlight.darkSide;
}
Vector3f &dest = vlight.affectedByAmbient? parms.color : parms.extra;
dest += vlight.color * de::clamp(-1.f, strength, 1.f);
}
static int lightModelVertexWorker(VectorLight const *vlight, void *context)
{
lightmodelvertexworker_params_t &parms = *static_cast<lightmodelvertexworker_params_t *>(context);
lightModelVertex(*vlight, parms);
parms.numProcessed += 1;
// Time to stop?
return parms.max && parms.numProcessed == parms.max;
}
/**
* Calculate vertex lighting.
* @todo construct a rotation matrix once and use it for all vertices.
*/
static void Mod_VertexColors(Vector4ub *out, int count, Vector3f const *normCoords,
uint vLightListIdx, uint maxLights, Vector4f const &ambient, bool invert,
float rotateYaw, float rotatePitch)
{
Vector4f const saturated(1, 1, 1, 1);
lightmodelvertexworker_params_t parms;
for(int i = 0; i < count; ++i, out++, normCoords++)
{
if(activeLod && !activeLod->hasVertex(i))
continue;
// Begin with total darkness.
parms.color = Vector3f();
parms.extra = Vector3f();
parms.normal = *normCoords;
parms.invert = invert;
parms.rotateYaw = rotateYaw;
parms.rotatePitch = rotatePitch;
parms.max = maxLights;
parms.numProcessed = 0;
// Add light from each source.
VL_ListIterator(vLightListIdx, lightModelVertexWorker, &parms);
// Check for ambient and convert to ubyte.
Vector4f color(parms.color.max(ambient) + parms.extra, ambient[3]);
*out = (color.min(saturated) * 255).toVector4ub();
}
}
/**
* Set all the colors in the array to bright white.
*/
static void Mod_FullBrightVertexColors(int count, Vector4ub *colorCoords, float alpha)
{
DENG2_ASSERT(colorCoords != 0);
for(; count-- > 0; colorCoords++)
{
*colorCoords = Vector4ub(255, 255, 255, 255 * alpha);
}
}
/**
* Set all the colors into the array to the same values.
*/
static void Mod_FixedVertexColors(int count, Vector4ub *colorCoords, Vector4ub const &color)
{
DENG2_ASSERT(colorCoords != 0);
for(; count-- > 0; colorCoords++)
{
*colorCoords = color;
}
}
/**
* Calculate cylindrically mapped, shiny texture coordinates.
*/
static void Mod_ShinyCoords(Vector2f *out, int count, Vector3f const *normCoords,
float normYaw, float normPitch, float shinyAng, float shinyPnt, float reactSpeed)
{
for(int i = 0; i < count; ++i, out++, normCoords++)
{
if(activeLod && !activeLod->hasVertex(i))
continue;
float rotatedNormal[3] = { normCoords->x, normCoords->y, normCoords->z };
// Rotate the normal vector so that it approximates the
// model's orientation compared to the viewer.
M_RotateVector(rotatedNormal,
(shinyPnt + normYaw) * 360 * reactSpeed,
(shinyAng + normPitch - .5f) * 180 * reactSpeed);
*out = Vector2f(rotatedNormal[0] + 1, rotatedNormal[2]);
}
}
static int chooseSelSkin(ModelDef &mf, int submodel, int selector)
{
if(mf.def->hasSub(submodel))
{
int i = (selector >> DDMOBJ_SELECTOR_SHIFT) &
mf.def->sub(submodel).selSkinBits[0]; // Selskin mask
int c = mf.def->sub(submodel).selSkinBits[1]; // Selskin shift
if(c > 0) i >>= c;
else i <<= -c;
if(i > 7) i = 7; // Maximum number of skins for selskin.
if(i < 0) i = 0; // Improbable (impossible?), but doesn't hurt.
return mf.def->sub(submodel).selSkins[i];
}
return 0;
}
static int chooseSkin(ModelDef &mf, int submodel, int id, int selector, int tmap)
{
if(submodel >= int(mf.subCount()))
{
return 0;
}
SubmodelDef &smf = mf.subModelDef(submodel);
Model &mdl = App_ResourceSystem().model(smf.modelId);
int skin = smf.skin;
// Selskin overrides the skin range.
if(smf.testFlag(MFF_SELSKIN))
{
skin = chooseSelSkin(mf, submodel, selector);
}
// Is there a skin range for this frame?
// (During model setup skintics and skinrange are set to >0.)
if(smf.skinRange > 1)
{
// What rule to use for determining the skin?
int offset;
if(smf.testFlag(MFF_IDSKIN))
{
offset = id;
}
else
{
offset = SECONDS_TO_TICKS(App_WorldSystem().time()) / mf.skinTics;
}
skin += offset % smf.skinRange;
}
// Need translation?
if(smf.testFlag(MFF_SKINTRANS))
{
skin = tmap;
}
if(skin < 0 || skin >= mdl.skinCount())
{
skin = 0;
}
return skin;
}
static void drawSubmodel(uint number, drawmodelparams_t const &parm)
{
int const zSign = (parm.mirror? -1 : 1);
ModelDef *mf = parm.mf, *mfNext = parm.nextMF;
SubmodelDef const &smf = mf->subModelDef(number);
Model &mdl = App_ResourceSystem().model(smf.modelId);
// Do not bother with infinitely small models...
if(mf->scale == Vector3f(0, 0, 0))
return;
float alpha = parm.ambientColor[CA];
// Is the submodel-defined alpha multiplier in effect?
// With df_brightshadow2, the alpha multiplier will be applied anyway.
if(smf.testFlag(MFF_BRIGHTSHADOW2) ||
!(parm.flags & (DDMF_BRIGHTSHADOW|DDMF_SHADOW|DDMF_ALTSHADOW)))
{
alpha *= smf.alpha / 255.f;
}
// Would this be visible?
if(alpha <= 0) return;
blendmode_t blending = smf.blendMode;
// Is the submodel-defined blend mode in effect?
if(parm.flags & DDMF_BRIGHTSHADOW)
{
blending = BM_ADD;
}
int useSkin = chooseSkin(*mf, number, parm.id, parm.selector, parm.tmap);
// Scale interpos. Intermark becomes zero and endmark becomes one.
// (Full sub-interpolation!) But only do it for the standard
// interrange. If a custom one is defined, don't touch interpos.
float endPos = 0;
float inter = parm.inter;
if((mf->interRange[0] == 0 && mf->interRange[1] == 1) || smf.testFlag(MFF_WORLD_TIME_ANIM))
{
endPos = (mf->interNext ? mf->interNext->interMark : 1);
inter = (parm.inter - mf->interMark) / (endPos - mf->interMark);
}
ModelFrame *frame = &visibleModelFrame(*mf, number, parm.id);
ModelFrame *nextFrame = 0;
// Do we have a sky/particle model here?
if(parm.alwaysInterpolate)
{
// Always interpolate, if there's animation.
// Used with sky and particle models.
nextFrame = &mdl.frame((smf.frame + 1) % mdl.frameCount());
mfNext = mf;
}
else
{
// Check for possible interpolation.
if(frameInter && mfNext && !smf.testFlag(MFF_DONT_INTERPOLATE))
{
if(mfNext->hasSub(number) && mfNext->subModelId(number) == smf.modelId)
{
nextFrame = &visibleModelFrame(*mfNext, number, parm.id);
}
}
}
// Clamp interpolation.
inter = de::clamp(0.f, inter, 1.f);
if(!nextFrame)
{
// If not interpolating, use the same frame as interpolation target.
// The lerp routines will recognize this special case.
nextFrame = frame;
mfNext = mf;
}
// Determine the total number of vertices we have.
int numVerts = mdl.vertexCount();
// Ensure our vertex render buffers can accommodate this.
if(!resizeVertexBuffer(numVerts))
{
// No can do, we aint got the power!
return;
}
// Setup transformation.
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
// Model space => World space
glTranslatef(parm.origin[VX] + parm.srvo[VX] +
de::lerp(mf->offset.x, mfNext->offset.x, inter),
parm.origin[VZ] + parm.srvo[VZ] +
de::lerp(mf->offset.y, mfNext->offset.y, inter),
parm.origin[VY] + parm.srvo[VY] + zSign *
de::lerp(mf->offset.z, mfNext->offset.z, inter));
if(parm.extraYawAngle || parm.extraPitchAngle)
{
// Sky models have an extra rotation.
glScalef(1, 200 / 240.0f, 1);
glRotatef(parm.extraYawAngle, 1, 0, 0);
glRotatef(parm.extraPitchAngle, 0, 0, 1);
glScalef(1, 240 / 200.0f, 1);
}
// Model rotation.
glRotatef(parm.viewAlign ? parm.yawAngleOffset : parm.yaw,
0, 1, 0);
glRotatef(parm.viewAlign ? parm.pitchAngleOffset : parm.pitch,
0, 0, 1);
// Scaling and model space offset.
glScalef(de::lerp(mf->scale.x, mfNext->scale.x, inter),
de::lerp(mf->scale.y, mfNext->scale.y, inter),
de::lerp(mf->scale.z, mfNext->scale.z, inter));
if(parm.extraScale)
{
// Particle models have an extra scale.
glScalef(parm.extraScale, parm.extraScale, parm.extraScale);
}
glTranslatef(smf.offset.x, smf.offset.y, smf.offset.z);
// Determine the suitable LOD.
if(mdl.lodCount() > 1 && rend_model_lod != 0)
{
float lodFactor = rend_model_lod * DENG_GAMEVIEW_WIDTH / 640.0f / (Rend_FieldOfView() / 90.0f);
if(!de::fequal(lodFactor, 0))
{
lodFactor = 1 / lodFactor;
}
// Determine the LOD we will be using.
activeLod = &mdl.lod(de::clamp<int>(0, lodFactor * parm.distance, mdl.lodCount() - 1));
}
else
{
activeLod = 0;
}
// Interpolate vertices and normals.
Mod_LerpVertices(inter, numVerts, *frame, *nextFrame,
modelPosCoords, modelNormCoords);
if(zSign < 0)
{
Mod_MirrorCoords(numVerts, modelPosCoords, 2);
Mod_MirrorCoords(numVerts, modelNormCoords, 1);
}
// Coordinates to the center of the model (game coords).
modelCenter = Vector3f(parm.origin[VX], parm.origin[VY], (parm.origin[VZ] + parm.gzt) * 2)
+ Vector3d(parm.srvo) + Vector3f(mf->offset.x, mf->offset.z, mf->offset.y);
// Calculate lighting.
Vector4f ambient;
if(smf.testFlag(MFF_FULLBRIGHT) && !smf.testFlag(MFF_DIM))
{
// Submodel-specific lighting override.
ambient = Vector4f(1, 1, 1, 1);
Mod_FullBrightVertexColors(numVerts, modelColorCoords, alpha);
}
else if(!parm.vLightListIdx)
{
// Lit uniformly.
ambient = Vector4f(parm.ambientColor, alpha);
Mod_FixedVertexColors(numVerts, modelColorCoords,
(ambient * 255).toVector4ub());
}
else
{
// Lit normally.
ambient = Vector4f(parm.ambientColor, alpha);
Mod_VertexColors(modelColorCoords, numVerts,
modelNormCoords, parm.vLightListIdx, modelLight + 1,
ambient, (mf->scale[VY] < 0), -parm.yaw, -parm.pitch);
}
TextureVariant *shinyTexture = 0;
float shininess = 0;
if(mf->def->hasSub(number))
{
shininess = de::clamp(0.f, mf->def->sub(number).shiny * modelShinyFactor, 1.f);
// Ensure we've prepared the shiny skin.
if(shininess > 0)
{
if(Texture *tex = mf->subModelDef(number).shinySkin)
{
shinyTexture = tex->prepareVariant(Rend_ModelShinyTextureSpec());
}
else
{
shininess = 0;
}
}
}
Vector4f color;
if(shininess > 0)
{
// Calculate shiny coordinates.
Vector3f shinyColor = mf->def->sub(number).shinyColor;
// With psprites, add the view angle/pitch.
float offset = parm.shineYawOffset;
// Calculate normalized (0,1) model yaw and pitch.
float normYaw = M_CycleIntoRange(((parm.viewAlign ? parm.yawAngleOffset
: parm.yaw) + offset) / 360, 1);
offset = parm.shinePitchOffset;
float normPitch = M_CycleIntoRange(((parm.viewAlign ? parm.pitchAngleOffset
: parm.pitch) + offset) / 360, 1);
float shinyAng = 0;
float shinyPnt = 0;
if(parm.shinepspriteCoordSpace)
{
// This is a hack to accommodate the psprite coordinate space.
shinyPnt = 0.5;
}
else
{
Vector3f delta = modelCenter;
if(!parm.shineTranslateWithViewerPos)
{
delta -= vOrigin.xzy();
}
shinyAng = QATAN2(delta.z, M_ApproxDistancef(delta.x, delta.y)) / PI + 0.5f; // shinyAng is [0,1]
shinyPnt = QATAN2(delta.y, delta.x) / (2 * PI);
}
Mod_ShinyCoords(modelTexCoords, numVerts,
modelNormCoords, normYaw, normPitch, shinyAng, shinyPnt,
mf->def->sub(number).shinyReact);
// Shiny color.
if(smf.testFlag(MFF_SHINY_LIT))
{
color = Vector4f(ambient * shinyColor, shininess);
}
else
{
color = Vector4f(shinyColor, shininess);
}
}
TextureVariant *skinTexture = 0;
if(renderTextures == 2)
{
// For lighting debug, render all surfaces using the gray texture.
MaterialVariantSpec const &spec = ClientApp::resourceSystem()
.materialSpec(ModelSkinContext, 0, 0, 0, 0, GL_REPEAT, GL_REPEAT,
1, -2, -1, true, true, false, false);
MaterialSnapshot const &ms = ClientApp::resourceSystem()
.material(de::Uri("System", Path("gray"))).prepare(spec);
skinTexture = &ms.texture(MTU_PRIMARY);
}
else
{
skinTexture = 0;
if(Texture *tex = mdl.skin(useSkin).texture)
{
skinTexture = tex->prepareVariant(Rend_ModelDiffuseTextureSpec(mdl.flags().testFlag(Model::NoTextureCompression)));
}
}
// If we mirror the model, triangles have a different orientation.
if(zSign < 0)
{
glFrontFace(GL_CCW);
}
// Twosided models won't use backface culling.
if(smf.testFlag(MFF_TWO_SIDED))
{
glDisable(GL_CULL_FACE);
}
glEnable(GL_TEXTURE_2D);
Model::Primitives const &primitives =
activeLod? activeLod->primitives : mdl.primitives();
// Render using multiple passes?
if(!modelShinyMultitex || shininess <= 0 || alpha < 1 ||
blending != BM_NORMAL || !smf.testFlag(MFF_SHINY_SPECULAR) ||
numTexUnits < 2 || !envModAdd)
{
// The first pass can be skipped if it won't be visible.
if(shininess < 1 || smf.testFlag(MFF_SHINY_SPECULAR))
{
selectTexUnits(1);
GL_BlendMode(blending);
GL_BindTexture(renderTextures? skinTexture : 0);
drawPrimitives(RC_COMMAND_COORDS, primitives,
modelPosCoords, modelColorCoords);
}
if(shininess > 0)
{
glDepthFunc(GL_LEQUAL);
// Set blending mode, two choices: reflected and specular.
if(smf.testFlag(MFF_SHINY_SPECULAR))
GL_BlendMode(BM_ADD);
else
GL_BlendMode(BM_NORMAL);
// Shiny color.
Mod_FixedVertexColors(numVerts, modelColorCoords,
(color * 255).toVector4ub());
if(numTexUnits > 1 && modelShinyMultitex)
{
// We'll use multitexturing to clear out empty spots in
// the primary texture.
selectTexUnits(2);
GL_ModulateTexture(11);
glActiveTexture(GL_TEXTURE1);
GL_BindTexture(renderTextures? shinyTexture : 0);
glActiveTexture(GL_TEXTURE0);
GL_BindTexture(renderTextures? skinTexture : 0);
drawPrimitives(RC_BOTH_COORDS, primitives,
modelPosCoords, modelColorCoords, modelTexCoords);
selectTexUnits(1);
GL_ModulateTexture(1);
}
else
{