/
r_data.c
2674 lines (2226 loc) · 73 KB
/
r_data.c
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 r_data.c
*\section License
* License: GPL
* Online License Link: http://www.gnu.org/licenses/gpl.html
*
*\author Copyright © 2003-2011 Jaakko Keränen <jaakko.keranen@iki.fi>
*\author Copyright © 2005-2011 Daniel Swanson <danij@dengine.net>
*\author Copyright © 2006-2007 Jamie Jones <jamie_jones_au@yahoo.com.au>
*
* 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
*/
/**
* Data Structures and Constants for Refresh
*/
// HEADER FILES ------------------------------------------------------------
#include <math.h>
#include "de_base.h"
#include "de_console.h"
#include "de_system.h"
#include "de_network.h"
#include "de_refresh.h"
#include "de_graphics.h"
#include "de_misc.h"
#include "de_audio.h" // For texture, environmental audio properties.
#include "m_stack.h"
// MACROS ------------------------------------------------------------------
#define RAWTEX_HASH_SIZE 128
#define RAWTEX_HASH(x) (rawtexhash + (((unsigned) x) & (RAWTEX_HASH_SIZE - 1)))
#define COLORPALETTENAME_MAXLEN (32)
// TYPES -------------------------------------------------------------------
typedef struct rawtexhash_s {
rawtex_t* first;
} rawtexhash_t;
typedef enum {
RPT_VERT,
RPT_COLOR,
RPT_TEXCOORD
} rpolydatatype_t;
typedef struct {
boolean inUse;
rpolydatatype_t type;
uint num;
void* data;
} rendpolydata_t;
/**
* DGL color palette name bindings.
* These are mainly intended as a public API convenience.
* Internally, we are only interested in the associated DGL name(s).
*/
typedef struct {
char name[COLORPALETTENAME_MAXLEN+1];
DGLuint id;
} colorpalettebind_t;
// EXTERNAL FUNCTION PROTOTYPES --------------------------------------------
// PUBLIC FUNCTION PROTOTYPES ----------------------------------------------
// PRIVATE FUNCTION PROTOTYPES ---------------------------------------------
// EXTERNAL DATA DECLARATIONS ----------------------------------------------
int gameDataFormat; // Use a game-specifc data format where applicable.
extern boolean mapSetup; // We are currently setting up a map.
// PUBLIC DATA DEFINITIONS -------------------------------------------------
byte precacheSkins = true;
byte precacheSprites = true;
int numSpriteTextures = 0;
spritetex_t** spriteTextures = NULL;
uint numDoomPatchDefs = 0;
patchtex_t** doomPatchDefs = NULL;
int numDetailTextures = 0;
detailtex_t** detailTextures = NULL;
int numLightMaps = 0;
lightmap_t** lightMaps = NULL;
int numFlareTextures = 0;
flaretex_t** flareTextures = NULL;
int numShinyTextures = 0;
shinytex_t** shinyTextures = NULL;
int numMaskTextures = 0;
masktex_t** maskTextures = NULL;
// Glowing textures are always rendered fullbright.
float glowingTextures = 1.0f;
byte rendInfoRPolys = 0;
// Skinnames will only *grow*. It will never be destroyed, not even
// at resets. The skin textures themselves will be deleted, though.
// This is because we want to have permanent ID numbers for skins,
// and the ID numbers are the same as indices to the skinNames array.
// Created in r_model.c, when registering the skins.
uint numSkinNames = 0;
skinname_t* skinNames = NULL;
// PRIVATE DATA DEFINITIONS ------------------------------------------------
static int numFlats = 0;
static flat_t** flats = NULL;
static int numDoomTextureDefs;
static doomtexturedef_t** doomTextureDefs;
static rawtexhash_t rawtexhash[RAWTEX_HASH_SIZE];
static unsigned int numrendpolys = 0;
static unsigned int maxrendpolys = 0;
static rendpolydata_t** rendPolys;
static colorpalettebind_t* colorPalettes = NULL;
static uint numColorPalettes = 0;
static colorpaletteid_t defaultColorPalette = 0;
// CODE --------------------------------------------------------------------
static colorpaletteid_t colorPaletteNumForName(const char* name)
{
uint i;
// Linear search (sufficiently fast enough given the probably small set
// and infrequency of searches).
for(i = 0; i < numColorPalettes; ++i)
{
colorpalettebind_t* pal = &colorPalettes[i];
if(!strnicmp(pal->name, name, COLORPALETTENAME_MAXLEN))
return i + 1; // Already registered. 1-based index.
}
return 0; // Not found.
}
/**
* Given a colorpalette id return the associated DGL color palette num.
* If the specified id cannot be found, the default color palette will be
* returned instead.
*
* @param id Id of the color palette to be prepared.
*
* @return The DGL palette number iff found else @c NULL.
*/
DGLuint R_GetColorPalette(colorpaletteid_t id)
{
if(!id || id - 1 >= numColorPalettes)
{
if(numColorPalettes)
return colorPalettes[defaultColorPalette-1].id;
return 0;
}
return colorPalettes[id-1].id;
}
/**
* Change the default color palette.
*
* @param id Id of the color palette to make default.
*
* @return @c true iff successful, else @c NULL.
*/
boolean R_SetDefaultColorPalette(colorpaletteid_t id)
{
if(id && id - 1 < numColorPalettes)
{
defaultColorPalette = id;
return true;
}
VERBOSE(Con_Message("R_SetDefaultColorPalette: Invalid id %u.\n", id))
return false;
}
/**
* Add a new (named) color palette.
* \note Part of the Doomsday public API.
*
* \design The idea with the two-teered implementation is to allow maximum
* flexibility. Within the engine we can create new palettes and manipulate
* them directly via the DGL interface. The underlying implementation is
* wrapped in a similar way to the materials so that publically, there is a
* set of (eternal) names and unique identifiers that survive game and GL
* resets.
*
* @param fmt Format string describes the format of @p data.
* Expected form: "C#C#C"
* C = color component, one of R, G, B.
* # = bits per component.
* @param name Unique name by which the palette will be known.
* @param data Buffer containing the src color data.
* @param num Number of sets of components).
*
* @return Color palette id.
*/
colorpaletteid_t R_CreateColorPalette(const char* fmt, const char* name,
const byte* data, ushort num)
{
#define MAX_BPC (16) // Max bits per component.
static const char* compNames[] = { "red", "green", "blue" };
colorpaletteid_t id;
colorpalettebind_t* pal;
const char* c, *end;
int i, pos, compOrder[3];
byte compSize[3];
if(!name || !name[0])
Con_Error("R_CreateColorPalette: Color palettes require a name.\n");
if(strlen(name) > COLORPALETTENAME_MAXLEN)
Con_Error("R_CreateColorPalette: Failed creating \"%s\", "
"color palette name can be at most %i characters long.\n",
name, COLORPALETTENAME_MAXLEN);
if(!fmt || !fmt[0])
Con_Error("R_CreateColorPalette: Failed creating \"%s\", "
"missing format string.\n", name);
if(!data || !num)
Con_Error("R_CreateColorPalette: Failed creating \"%s\", "
"cannot create a zero-sized palette.\n", name);
// All arguments supplied. Parse the format string.
memset(compOrder, -1, sizeof(compOrder));
pos = 0;
end = fmt + (strlen(fmt) - 1);
c = fmt;
do
{
int comp = -1;
if(*c == 'R' || *c == 'r')
comp = CR;
else if(*c == 'G' || *c == 'g')
comp = CG;
else if(*c == 'B' || *c == 'b')
comp = CB;
if(comp != -1)
{
// Have encountered this component yet?
if(compOrder[comp] == -1)
{ // No.
const char* start;
size_t numDigits;
compOrder[comp] = pos++;
// Read the number of bits.
start = ++c;
while((*c >= '0' && *c <= '9') && ++c < end);
numDigits = c - start;
if(numDigits != 0 && numDigits <= 2)
{
char buf[3];
memset(buf, 0, sizeof(buf));
memcpy(buf, start, numDigits);
compSize[comp] = atoi(buf);
if(pos == 3)
break; // We're done.
// Unread the last character.
c--;
continue;
}
}
}
Con_Error("R_CreateColorPalette: Failed creating \"%s\", "
"invalid character '%c' in format string at "
"position %u.\n", name, *c, (unsigned int) (c - fmt));
} while(++c <= end);
if(pos != 3)
Con_Error("R_CreateColorPalette: Failed creating \"%s\", "
"incomplete format specification.\n", name);
// Check validity of bits per component.
for(i = 0; i < 3; ++i)
if(compSize[i] == 0 || compSize[i] > MAX_BPC)
{
Con_Error("R_CreateColorPalette: Failed creating \"%s\", "
"unsupported bit size %i for %s component.\n",
name, compSize[i], compNames[compOrder[i]]);
}
if((id = colorPaletteNumForName(name)))
{ // We are replacing an existing palette.
pal = &colorPalettes[id-1];
// Destroy the existing DGL color palette.
GL_DeleteColorPalettes(1, &pal->id);
pal->id = 0;
}
else
{ // We are creating a new palette.
if(!numColorPalettes)
{
colorPalettes = Z_Malloc(
++numColorPalettes * sizeof(colorpalettebind_t), PU_APPSTATIC, 0);
defaultColorPalette = numColorPalettes;
}
else
colorPalettes = Z_Realloc(&colorPalettes,
++numColorPalettes * sizeof(colorpalettebind_t), PU_APPSTATIC);
pal = &colorPalettes[numColorPalettes-1];
memset(pal, 0, sizeof(*pal));
strncpy(pal->name, name, COLORPALETTENAME_MAXLEN);
id = numColorPalettes; // 1-based index.
}
// Generate the actual DGL color palette.
pal->id = GL_CreateColorPalette(compOrder, compSize, data, num);
return id; // 1-based index.
#undef MAX_BPC
}
/**
* Given a color palette name, look up the associated identifier.
* \note Part of the Doomsday public API.
*
* @param name Unique name of the palette to locate.
* @return Iff found, identifier of the palette associated
* with this name, ELSE @c 0.
*/
colorpaletteid_t R_GetColorPaletteNumForName(const char* name)
{
if(name && name[0] && strlen(name) <= COLORPALETTENAME_MAXLEN)
return colorPaletteNumForName(name);
return 0;
}
/**
* Given a color palette id, look up the specified unique name.
* \note Part of the Doomsday public API.
*
* @param id Id of the color palette to locate.
* @return Iff found, pointer to the unique name associated
* with the specified id, ELSE @c NULL.
*/
const char* R_GetColorPaletteNameForNum(colorpaletteid_t id)
{
if(id && id - 1 < numColorPalettes)
return colorPalettes[id-1].name;
return NULL;
}
/**
* Given a color palette index, calculate the equivilent RGB color.
* \note Part of the Doomsday public API.
*
* @param id Id of the color palette to use.
* @param rgb Final color will be written back here.
* @param idx Color Palette, color index.
* @param correctGamma @c TRUE if the current gamma ramp should be applied.
*/
void R_GetColorPaletteRGBf(colorpaletteid_t id, float rgb[3], int idx,
boolean correctGamma)
{
if(rgb)
{
if(idx >= 0)
{
DGLuint pal = R_GetColorPalette(id);
DGLubyte rgbUBV[3];
GL_GetColorPaletteRGB(pal, rgbUBV, idx);
if(correctGamma)
{
rgbUBV[CR] = gammaTable[rgbUBV[CR]];
rgbUBV[CG] = gammaTable[rgbUBV[CG]];
rgbUBV[CB] = gammaTable[rgbUBV[CB]];
}
rgb[CR] = rgbUBV[CR] * reciprocal255;
rgb[CG] = rgbUBV[CG] * reciprocal255;
rgb[CB] = rgbUBV[CB] * reciprocal255;
return;
}
rgb[CR] = rgb[CG] = rgb[CB] = 0;
}
}
void R_InfoRendVerticesPool(void)
{
uint i;
if(!rendInfoRPolys)
return;
Con_Printf("RP Count: %-4i\n", numrendpolys);
for(i = 0; i < numrendpolys; ++i)
{
rendpolydata_t* rp = rendPolys[i];
Con_Printf("RP: %-4u %c (vtxs=%u t=%c)\n", i,
(rp->inUse? 'Y':'N'), rp->num,
(rp->type == RPT_VERT? 'v' :
rp->type == RPT_COLOR?'c' : 't'));
}
}
/**
* Called at the start of each map.
*/
void R_InitRendVerticesPool(void)
{
rvertex_t* rvertices;
rcolor_t* rcolors;
rtexcoord_t* rtexcoords;
numrendpolys = maxrendpolys = 0;
rendPolys = NULL;
rvertices = R_AllocRendVertices(24);
rcolors = R_AllocRendColors(24);
rtexcoords = R_AllocRendTexCoords(24);
// Mark unused.
R_FreeRendVertices(rvertices);
R_FreeRendColors(rcolors);
R_FreeRendTexCoords(rtexcoords);
}
/**
* Retrieves a batch of rvertex_t.
* Possibly allocates new if necessary.
*
* @param num The number of verts required.
*
* @return Ptr to array of rvertex_t
*/
rvertex_t* R_AllocRendVertices(uint num)
{
unsigned int idx;
boolean found = false;
for(idx = 0; idx < maxrendpolys; ++idx)
{
if(rendPolys[idx]->inUse || rendPolys[idx]->type != RPT_VERT)
continue;
if(rendPolys[idx]->num >= num)
{
// Use this one.
rendPolys[idx]->inUse = true;
return (rvertex_t*) rendPolys[idx]->data;
}
else if(rendPolys[idx]->num == 0)
{
// There is an unused one but we haven't allocated verts yet.
numrendpolys++;
found = true;
break;
}
}
if(!found)
{
// We may need to allocate more.
if(++numrendpolys > maxrendpolys)
{
uint i, newCount;
rendpolydata_t* newPolyData, *ptr;
maxrendpolys = (maxrendpolys > 0? maxrendpolys * 2 : 8);
rendPolys =
Z_Realloc(rendPolys, sizeof(rendpolydata_t*) * maxrendpolys,
PU_MAP);
newCount = maxrendpolys - numrendpolys + 1;
newPolyData =
Z_Malloc(sizeof(rendpolydata_t) * newCount, PU_MAP, 0);
ptr = newPolyData;
for(i = numrendpolys-1; i < maxrendpolys; ++i, ptr++)
{
ptr->inUse = false;
ptr->num = 0;
ptr->data = NULL;
ptr->type = RPT_VERT;
rendPolys[i] = ptr;
}
}
idx = numrendpolys - 1;
}
rendPolys[idx]->inUse = true;
rendPolys[idx]->type = RPT_VERT;
rendPolys[idx]->num = num;
rendPolys[idx]->data =
Z_Malloc(sizeof(rvertex_t) * num, PU_MAP, 0);
return (rvertex_t*) rendPolys[idx]->data;
}
/**
* Retrieves a batch of rcolor_t.
* Possibly allocates new if necessary.
*
* @param num The number of verts required.
*
* @return Ptr to array of rcolor_t
*/
rcolor_t* R_AllocRendColors(uint num)
{
unsigned int idx;
boolean found = false;
for(idx = 0; idx < maxrendpolys; ++idx)
{
if(rendPolys[idx]->inUse || rendPolys[idx]->type != RPT_COLOR)
continue;
if(rendPolys[idx]->num >= num)
{
// Use this one.
rendPolys[idx]->inUse = true;
return (rcolor_t*) rendPolys[idx]->data;
}
else if(rendPolys[idx]->num == 0)
{
// There is an unused one but we haven't allocated verts yet.
numrendpolys++;
found = true;
break;
}
}
if(!found)
{
// We may need to allocate more.
if(++numrendpolys > maxrendpolys)
{
uint i, newCount;
rendpolydata_t* newPolyData, *ptr;
maxrendpolys = (maxrendpolys > 0? maxrendpolys * 2 : 8);
rendPolys =
Z_Realloc(rendPolys, sizeof(rendpolydata_t*) * maxrendpolys,
PU_MAP);
newCount = maxrendpolys - numrendpolys + 1;
newPolyData =
Z_Malloc(sizeof(rendpolydata_t) * newCount, PU_MAP, 0);
ptr = newPolyData;
for(i = numrendpolys-1; i < maxrendpolys; ++i, ptr++)
{
ptr->inUse = false;
ptr->num = 0;
ptr->data = NULL;
ptr->type = RPT_COLOR;
rendPolys[i] = ptr;
}
}
idx = numrendpolys - 1;
}
rendPolys[idx]->inUse = true;
rendPolys[idx]->type = RPT_COLOR;
rendPolys[idx]->num = num;
rendPolys[idx]->data =
Z_Malloc(sizeof(rcolor_t) * num, PU_MAP, 0);
return (rcolor_t*) rendPolys[idx]->data;
}
/**
* Retrieves a batch of rtexcoord_t.
* Possibly allocates new if necessary.
*
* @param num The number required.
*
* @return Ptr to array of rtexcoord_t
*/
rtexcoord_t* R_AllocRendTexCoords(uint num)
{
unsigned int idx;
boolean found = false;
for(idx = 0; idx < maxrendpolys; ++idx)
{
if(rendPolys[idx]->inUse || rendPolys[idx]->type != RPT_TEXCOORD)
continue;
if(rendPolys[idx]->num >= num)
{
// Use this one.
rendPolys[idx]->inUse = true;
return (rtexcoord_t*) rendPolys[idx]->data;
}
else if(rendPolys[idx]->num == 0)
{
// There is an unused one but we haven't allocated verts yet.
numrendpolys++;
found = true;
break;
}
}
if(!found)
{
// We may need to allocate more.
if(++numrendpolys > maxrendpolys)
{
uint i, newCount;
rendpolydata_t* newPolyData, *ptr;
maxrendpolys = (maxrendpolys > 0? maxrendpolys * 2 : 8);
rendPolys =
Z_Realloc(rendPolys, sizeof(rendpolydata_t*) * maxrendpolys,
PU_MAP);
newCount = maxrendpolys - numrendpolys + 1;
newPolyData =
Z_Malloc(sizeof(rendpolydata_t) * newCount, PU_MAP, 0);
ptr = newPolyData;
for(i = numrendpolys-1; i < maxrendpolys; ++i, ptr++)
{
ptr->inUse = false;
ptr->num = 0;
ptr->data = NULL;
ptr->type = RPT_TEXCOORD;
rendPolys[i] = ptr;
}
}
idx = numrendpolys - 1;
}
rendPolys[idx]->inUse = true;
rendPolys[idx]->type = RPT_TEXCOORD;
rendPolys[idx]->num = num;
rendPolys[idx]->data =
Z_Malloc(sizeof(rtexcoord_t) * num, PU_MAP, 0);
return (rtexcoord_t*) rendPolys[idx]->data;
}
/**
* Doesn't actually free anything. Instead, mark them as unused ready for
* the next time a batch of rendvertex_t is needed.
*
* @param vertices Ptr to array of rvertex_t to mark unused.
*/
void R_FreeRendVertices(rvertex_t* rvertices)
{
uint i;
if(!rvertices)
return;
for(i = 0; i < numrendpolys; ++i)
{
if(rendPolys[i]->data == rvertices)
{
rendPolys[i]->inUse = false;
return;
}
}
#if _DEBUG
Con_Message("R_FreeRendPoly: Dangling poly ptr!\n");
#endif
}
/**
* Doesn't actually free anything. Instead, mark them as unused ready for
* the next time a batch of rendvertex_t is needed.
*
* @param vertices Ptr to array of rcolor_t to mark unused.
*/
void R_FreeRendColors(rcolor_t* rcolors)
{
uint i;
if(!rcolors)
return;
for(i = 0; i < numrendpolys; ++i)
{
if(rendPolys[i]->data == rcolors)
{
rendPolys[i]->inUse = false;
return;
}
}
#if _DEBUG
Con_Message("R_FreeRendPoly: Dangling poly ptr!\n");
#endif
}
/**
* Doesn't actually free anything. Instead, mark them as unused ready for
* the next time a batch of rendvertex_t is needed.
*
* @param rtexcoords Ptr to array of rtexcoord_t to mark unused.
*/
void R_FreeRendTexCoords(rtexcoord_t* rtexcoords)
{
uint i;
if(!rtexcoords)
return;
for(i = 0; i < numrendpolys; ++i)
{
if(rendPolys[i]->data == rtexcoords)
{
rendPolys[i]->inUse = false;
return;
}
}
#if _DEBUG
Con_Message("R_FreeRendPoly: Dangling poly ptr!\n");
#endif
}
void R_DivVerts(rvertex_t* dst, const rvertex_t* src, const walldiv_t* divs)
{
#define COPYVERT(d, s) (d)->pos[VX] = (s)->pos[VX]; \
(d)->pos[VY] = (s)->pos[VY]; \
(d)->pos[VZ] = (s)->pos[VZ];
uint i;
uint numL = 3 + divs[0].num;
uint numR = 3 + divs[1].num;
// Right fan:
COPYVERT(&dst[numL + 0], &src[0])
COPYVERT(&dst[numL + 1], &src[3]);
COPYVERT(&dst[numL + numR - 1], &src[2]);
for(i = 0; i < divs[1].num; ++i)
{
dst[numL + 2 + i].pos[VX] = src[2].pos[VX];
dst[numL + 2 + i].pos[VY] = src[2].pos[VY];
dst[numL + 2 + i].pos[VZ] = divs[1].pos[i];
}
// Left fan:
COPYVERT(&dst[0], &src[3]);
COPYVERT(&dst[1], &src[0]);
COPYVERT(&dst[numL - 1], &src[1]);
for(i = 0; i < divs[0].num; ++i)
{
dst[2 + i].pos[VX] = src[0].pos[VX];
dst[2 + i].pos[VY] = src[0].pos[VY];
dst[2 + i].pos[VZ] = divs[0].pos[i];
}
#undef COPYVERT
}
void R_DivTexCoords(rtexcoord_t* dst, const rtexcoord_t* src,
const walldiv_t* divs, float bL, float tL, float bR,
float tR)
{
#define COPYTEXCOORD(d, s) (d)->st[0] = (s)->st[0]; \
(d)->st[1] = (s)->st[1];
uint i;
uint numL = 3 + divs[0].num;
uint numR = 3 + divs[1].num;
float height;
// Right fan:
COPYTEXCOORD(&dst[numL + 0], &src[0]);
COPYTEXCOORD(&dst[numL + 1], &src[3]);
COPYTEXCOORD(&dst[numL + numR-1], &src[2]);
height = tR - bR;
for(i = 0; i < divs[1].num; ++i)
{
float inter = (divs[1].pos[i] - bR) / height;
dst[numL + 2 + i].st[0] = src[3].st[0];
dst[numL + 2 + i].st[1] = src[2].st[1] +
(src[3].st[1] - src[2].st[1]) * inter;
}
// Left fan:
COPYTEXCOORD(&dst[0], &src[3]);
COPYTEXCOORD(&dst[1], &src[0]);
COPYTEXCOORD(&dst[numL - 1], &src[1]);
height = tL - bL;
for(i = 0; i < divs[0].num; ++i)
{
float inter = (divs[0].pos[i] - bL) / height;
dst[2 + i].st[0] = src[0].st[0];
dst[2 + i].st[1] = src[0].st[1] +
(src[1].st[1] - src[0].st[1]) * inter;
}
#undef COPYTEXCOORD
}
void R_DivVertColors(rcolor_t* dst, const rcolor_t* src,
const walldiv_t* divs, float bL, float tL, float bR,
float tR)
{
#define COPYVCOLOR(d, s) (d)->rgba[CR] = (s)->rgba[CR]; \
(d)->rgba[CG] = (s)->rgba[CG]; \
(d)->rgba[CB] = (s)->rgba[CB]; \
(d)->rgba[CA] = (s)->rgba[CA];
uint i;
uint numL = 3 + divs[0].num;
uint numR = 3 + divs[1].num;
float height;
// Right fan:
COPYVCOLOR(&dst[numL + 0], &src[0]);
COPYVCOLOR(&dst[numL + 1], &src[3]);
COPYVCOLOR(&dst[numL + numR-1], &src[2]);
height = tR - bR;
for(i = 0; i < divs[1].num; ++i)
{
uint c;
float inter = (divs[1].pos[i] - bR) / height;
for(c = 0; c < 4; ++c)
{
dst[numL + 2 + i].rgba[c] = src[2].rgba[c] +
(src[3].rgba[c] - src[2].rgba[c]) * inter;
}
}
// Left fan:
COPYVCOLOR(&dst[0], &src[3]);
COPYVCOLOR(&dst[1], &src[0]);
COPYVCOLOR(&dst[numL - 1], &src[1]);
height = tL - bL;
for(i = 0; i < divs[0].num; ++i)
{
uint c;
float inter = (divs[0].pos[i] - bL) / height;
for(c = 0; c < 4; ++c)
{
dst[2 + i].rgba[c] = src[0].rgba[c] +
(src[1].rgba[c] - src[0].rgba[c]) * inter;
}
}
#undef COPYVCOLOR
}
void R_ShutdownData(void)
{
Materials_Shutdown();
}
/**
* Registers the "system" textures that are part of the Doomsday data resource package.
* @fixme A nuisance really. Why not simply read the contents of e.g., "}doomsday/data/graphics/"
*/
void R_InitSystemTextures(void)
{
struct ddtexdef_s {
char name[9];
uint id;
} static const ddtexdefs[NUM_DD_TEXTURES] = {
{ "DDT_UNKN", DDT_UNKNOWN },
{ "DDT_MISS", DDT_MISSING },
{ "DDT_BBOX", DDT_BBOX },
{ "DDT_GRAY", DDT_GRAY }
};
{ uint i;
for(i = 0; i < NUM_DD_TEXTURES; ++i)
{
GL_CreateGLTexture(ddtexdefs[i].name, ddtexdefs[i].id, GLT_SYSTEM);
}}
}
static patchtex_t* getPatchTex(patchid_t id)
{
if(id != 0 && id <= numDoomPatchDefs)
return doomPatchDefs[id-1];
return NULL;
}
static patchid_t patchForName(const char* name)
{
const gltexture_t* glTex;
if((glTex = GL_GetGLTextureByName(name, GLT_DOOMPATCH)))
return glTex->ofTypeID;
return 0;
}
/**
* Returns a patchtex_t* for the given lump, if one already exists.
*/
patchtex_t* R_FindPatchTex(patchid_t id)
{
patchtex_t* patchTex = getPatchTex(id);
if(!patchTex)
Con_Error("R_FindPatchTex: Unknown patch %i.", id);
return patchTex;
}
void R_ClearPatchTexs(void)
{
Z_FreeTags(PU_PATCH, PU_PATCH);
doomPatchDefs = 0;
numDoomPatchDefs = 0;
}
/**
* Get a patchtex_t data structure for a patch specified with a WAD lump
* number. Allocates a new patchtex_t if it hasn't been loaded yet.
*/
patchid_t R_RegisterAsPatch(const char* name)
{
assert(name);
{
const lumppatch_t* patch;
lumpnum_t lump;
patchid_t id;
patchtex_t* p;
if(!name[0])
return 0;
// Already defined as a patch?
if((id = patchForName(name)) != 0)
return id;
if((lump = W_CheckNumForName(name)) == -1)
return 0;
/// \fixme What about min lump size?
patch = (const lumppatch_t*) W_CacheLumpNum(lump, PU_APPSTATIC);
// An entirely new patch.
p = Z_Calloc(sizeof(*p), PU_PATCH, NULL);
p->lump = lump;