-
Notifications
You must be signed in to change notification settings - Fork 0
/
gl_anim.c
2297 lines (1921 loc) · 55.2 KB
/
gl_anim.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
/*
Copyright (C) 1996-1997 Id Software, Inc.
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
// gl_anim.c -- handle warping sky/water animation, fog and bloom (2D lighting post process effect) routines
#include "quakedef.h"
cvar_t r_fastsky = {"r_fastsky","0"};
cvar_t r_skyquality = {"r_skyquality","12"};
cvar_t r_skyalpha = {"r_skyalpha","1", true};
cvar_t r_skyfog = {"r_skyfog","0.5", true};
cvar_t r_oldsky = {"r_oldsky", "0"};
cvar_t r_waterquality = {"r_waterquality", "12"};
// Nehahra
cvar_t gl_fogenable = {"gl_fogenable", "0"};
cvar_t gl_fogdensity = {"gl_fogdensity", "0"};
cvar_t gl_fogred = {"gl_fogred","0.5"};
cvar_t gl_foggreen = {"gl_foggreen","0.5"};
cvar_t gl_fogblue = {"gl_fogblue","0.5"};
//==================================================================================================================
/*
=================================================================
IMAGE LOADING ROUTINES
=================================================================
*/
char loadfilename[MAX_OSPATH]; // file scope so that error messages can use it
//==============================================================================
//
// TGA
//
//==============================================================================
typedef struct targaheader_s {
byte id_length, colormap_type, image_type;
unsigned short colormap_index, colormap_length;
byte colormap_size;
unsigned short x_origin, y_origin, width, height;
byte pixel_size, attributes;
} targaheader_t;
#define TARGAHEADERSIZE 18 // size on disk
targaheader_t targa_header;
int fgetLittleShort (FILE *f)
{
byte b1, b2;
b1 = fgetc(f);
b2 = fgetc(f);
return (short)(b1 + b2*256);
}
int fgetLittleLong (FILE *f)
{
byte b1, b2, b3, b4;
b1 = fgetc(f);
b2 = fgetc(f);
b3 = fgetc(f);
b4 = fgetc(f);
return b1 + (b2<<8) + (b3<<16) + (b4<<24);
}
/*
=============
Image_LoadTGA
=============
*/
byte *Image_LoadTGA (FILE *fin, int *width, int *height)
{
int columns, rows, numPixels;
byte *pixbuf;
int row, column;
byte *targa_rgba;
int realrow; //johnfitz -- fix for upside-down targas
qboolean upside_down; //johnfitz -- fix for upside-down targas
targa_header.id_length = fgetc(fin);
targa_header.colormap_type = fgetc(fin);
targa_header.image_type = fgetc(fin);
targa_header.colormap_index = fgetLittleShort(fin);
targa_header.colormap_length = fgetLittleShort(fin);
targa_header.colormap_size = fgetc(fin);
targa_header.x_origin = fgetLittleShort(fin);
targa_header.y_origin = fgetLittleShort(fin);
targa_header.width = fgetLittleShort(fin);
targa_header.height = fgetLittleShort(fin);
targa_header.pixel_size = fgetc(fin);
targa_header.attributes = fgetc(fin);
if (targa_header.image_type!=2 && targa_header.image_type!=10)
Sys_Error ("Image_LoadTGA: %s is not a type 2 or type 10 targa\n", loadfilename);
if (targa_header.colormap_type !=0 || (targa_header.pixel_size!=32 && targa_header.pixel_size!=24))
Sys_Error ("Image_LoadTGA: %s is not a 24bit or 32bit targa\n", loadfilename);
columns = targa_header.width;
rows = targa_header.height;
numPixels = columns * rows;
upside_down = !(targa_header.attributes & 0x20); //johnfitz -- fix for upside-down targas
// targa_rgba = malloc (numPixels*4);
targa_rgba = Hunk_Alloc (numPixels*4);
// if (!targa_rgba)
// Sys_Error ("Image_LoadTGA: error allocating %d bytes for %s", numPixels*4, loadfilename);
if (targa_header.id_length != 0)
fseek(fin, targa_header.id_length, SEEK_CUR); // skip TARGA image comment
if (targa_header.image_type==2) // Uncompressed, RGB images
{
for(row=rows-1; row>=0; row--)
{
//johnfitz -- fix for upside-down targas
realrow = upside_down ? row : rows - 1 - row;
pixbuf = targa_rgba + realrow*columns*4;
//johnfitz
for(column=0; column<columns; column++)
{
byte red,green,blue,alphabyte;
switch (targa_header.pixel_size)
{
case 24:
blue = getc(fin);
green = getc(fin);
red = getc(fin);
*pixbuf++ = red;
*pixbuf++ = green;
*pixbuf++ = blue;
*pixbuf++ = 255;
break;
case 32:
blue = getc(fin);
green = getc(fin);
red = getc(fin);
alphabyte = getc(fin);
*pixbuf++ = red;
*pixbuf++ = green;
*pixbuf++ = blue;
*pixbuf++ = alphabyte;
break;
}
}
}
}
else if (targa_header.image_type==10) // Runlength encoded RGB images
{
byte red,green,blue,alphabyte,packetHeader,packetSize,j;
for(row=rows-1; row>=0; row--)
{
//johnfitz -- fix for upside-down targas
realrow = upside_down ? row : rows - 1 - row;
pixbuf = targa_rgba + realrow*columns*4;
//johnfitz
for(column=0; column<columns; )
{
packetHeader=getc(fin);
packetSize = 1 + (packetHeader & 0x7f);
if (packetHeader & 0x80) // run-length packet
{
switch (targa_header.pixel_size)
{
case 24:
blue = getc(fin);
green = getc(fin);
red = getc(fin);
alphabyte = 255;
break;
case 32:
blue = getc(fin);
green = getc(fin);
red = getc(fin);
alphabyte = getc(fin);
break;
default: /* avoid compiler warnings */
blue = red = green = alphabyte = 0;
}
for(j=0;j<packetSize;j++)
{
*pixbuf++=red;
*pixbuf++=green;
*pixbuf++=blue;
*pixbuf++=alphabyte;
column++;
if (column==columns) // run spans across rows
{
column=0;
if (row>0)
row--;
else
goto breakOut;
//johnfitz -- fix for upside-down targas
realrow = upside_down ? row : rows - 1 - row;
pixbuf = targa_rgba + realrow*columns*4;
//johnfitz
}
}
}
else // non run-length packet
{
for(j=0;j<packetSize;j++)
{
switch (targa_header.pixel_size)
{
case 24:
blue = getc(fin);
green = getc(fin);
red = getc(fin);
*pixbuf++ = red;
*pixbuf++ = green;
*pixbuf++ = blue;
*pixbuf++ = 255;
break;
case 32:
blue = getc(fin);
green = getc(fin);
red = getc(fin);
alphabyte = getc(fin);
*pixbuf++ = red;
*pixbuf++ = green;
*pixbuf++ = blue;
*pixbuf++ = alphabyte;
break;
default: /* avoid compiler warnings */
blue = red = green = alphabyte = 0;
}
column++;
if (column==columns) // pixel packet run spans across rows
{
column=0;
if (row>0)
row--;
else
goto breakOut;
//johnfitz -- fix for upside-down targas
realrow = upside_down ? row : rows - 1 - row;
pixbuf = targa_rgba + realrow*columns*4;
//johnfitz
}
}
}
}
breakOut:;
}
}
fclose(fin);
*width = (int)(targa_header.width);
*height = (int)(targa_header.height);
return targa_rgba;
}
//==============================================================================
//
// PCX
//
//==============================================================================
typedef struct
{
char signature;
char version;
char encoding;
char bits_per_pixel;
unsigned short xmin,ymin,xmax,ymax;
unsigned short hdpi,vdpi;
byte colortable[48];
char reserved;
char color_planes;
unsigned short bytes_per_line;
unsigned short palette_type;
char filler[58];
} pcxheader_t;
/*
============
Image_LoadPCX
============
*/
byte *Image_LoadPCX (FILE *f, int *width, int *height)
{
pcxheader_t pcx;
int x, y, w, h, readbyte, runlength, start;
byte *p, *pcx_rgb;
byte palette[768];
start = ftell (f); // save start of file (since we might be inside a pak file, SEEK_SET might not be the start of the pcx)
fread(&pcx, sizeof(pcx), 1, f);
pcx.xmin = (unsigned short)LittleShort (pcx.xmin);
pcx.ymin = (unsigned short)LittleShort (pcx.ymin);
pcx.xmax = (unsigned short)LittleShort (pcx.xmax);
pcx.ymax = (unsigned short)LittleShort (pcx.ymax);
pcx.bytes_per_line = (unsigned short)LittleShort (pcx.bytes_per_line);
if (pcx.signature != 0x0A)
Sys_Error ("Image_LoadPCX: '%s' is not a valid PCX file", loadfilename);
if (pcx.version != 5)
Sys_Error ("Image_LoadPCX: '%s' is version %i, should be 5", loadfilename, pcx.version);
if (pcx.encoding != 1 || pcx.bits_per_pixel != 8 || pcx.color_planes != 1)
Sys_Error ("Image_LoadPCX: '%s' has wrong encoding or bit depth", loadfilename);
w = pcx.xmax - pcx.xmin + 1;
h = pcx.ymax - pcx.ymin + 1;
// pcx_rgb = malloc ((w*h+1)*4); // +1 to allow reading padding byte on last line
pcx_rgb = Hunk_Alloc ((w*h+1)*4); // +1 to allow reading padding byte on last line
// if (!pcx_rgb)
// Sys_Error ("Image_LoadPCX: error allocating %d bytes for %s", (w*h+1)*4, loadfilename);
// load palette
fseek (f, start + com_filesize - 768, SEEK_SET);
fread (palette, 1, 768, f);
// back to start of image data
fseek (f, start + sizeof(pcx), SEEK_SET);
for (y=0; y<h; y++)
{
p = pcx_rgb + y * w * 4;
for (x=0; x<(pcx.bytes_per_line); ) // read the extra padding byte if necessary
{
readbyte = fgetc(f);
if(readbyte >= 0xC0)
{
runlength = readbyte & 0x3F;
readbyte = fgetc(f);
}
else
runlength = 1;
while(runlength--)
{
p[0] = palette[readbyte*3];
p[1] = palette[readbyte*3+1];
p[2] = palette[readbyte*3+2];
p[3] = 255;
p += 4;
x++;
}
}
}
fclose(f);
*width = w;
*height = h;
return pcx_rgb;
}
/*
============
GL_LoadImage
returns a pointer to hunk allocated RGBA data
TODO: search order: tga png jpg pcx lmp
============
*/
byte *GL_LoadImage (char *name, int *width, int *height)
{
FILE *f;
sprintf (loadfilename, "%s.tga", name);
COM_FOpenFile (loadfilename, &f, NULL);
if (f)
return Image_LoadTGA (f, width, height);
sprintf (loadfilename, "%s.pcx", name);
COM_FOpenFile (loadfilename, &f, NULL);
if (f)
return Image_LoadPCX (f, width, height);
return NULL;
}
//==================================================================================================================
/*
==============================================================================
RENDER-TO-FRAMEBUFFER WATER (adapted from fitzquake)
==============================================================================
*/
// speed up sin calculations - Ed
float turbsin[] =
{
#include "gl_warp_sin.h"
};
/*
=============
R_UpdateWarpTextures
each frame, update warping textures
=============
*/
void R_UpdateWarpTextures (void)
{
texture_t *tx;
int i;
float x, y, x2, warptess;
warptess = 128.0/CLAMP (4.0, floor(r_waterquality.value), 64.0);
for (i=0; i<cl.worldmodel->numtextures; i++)
{
if (!(tx = cl.worldmodel->textures[i]))
continue;
if (tx->name[0] != '*')
continue;
if (!tx->update_warp)
continue;
// render warp
glViewport (glx, gly + glheight - gl_warpimage_size, gl_warpimage_size, gl_warpimage_size);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
glOrtho (0, 128, 0, 128, -99999, 99999);
glMatrixMode (GL_MODELVIEW);
glLoadIdentity ();
glDisable (GL_ALPHA_TEST); //FX new
glEnable (GL_BLEND); //FX
GL_Bind (tx->gltexture);
for (x=0.0; x<128.0; x=x2)
{
x2 = x + warptess;
glBegin (GL_TRIANGLE_STRIP);
for (y=0.0; y<128.01; y+=warptess) // .01 for rounding errors
{
glTexCoord2f (WARPCALC(x,y), WARPCALC(y,x));
glVertex2f (x,y);
glTexCoord2f (WARPCALC(x2,y), WARPCALC(y,x2));
glVertex2f (x2,y);
}
glEnd ();
}
glEnable (GL_ALPHA_TEST); //FX new
glDisable (GL_BLEND); //FX
// copy to texture
GL_Bind (tx->warpimage);
glCopyTexSubImage2D (GL_TEXTURE_2D, 0, 0, 0, glx, gly + glheight - gl_warpimage_size, gl_warpimage_size, gl_warpimage_size);
tx->update_warp = false;
}
// if warp render went down into sbar territory, we need to be sure to refresh it next frame
if (gl_warpimage_size + sb_lines > vid.height)
Sbar_Changed ();
}
/*
==============================================================================
FOG DRAW ROUTINES
==============================================================================
*/
/*
=============
R_FogUpdate
update internal variables
=============
*/
void R_FogUpdate (float density, float red, float green, float blue, float time)
{
Cvar_SetValue ("gl_fogenable", density ? 1 : 0);
Cvar_SetValue ("gl_fogdensity", density);
Cvar_SetValue ("gl_fogred", red);
Cvar_SetValue ("gl_foggreen", green);
Cvar_SetValue ("gl_fogblue", blue);
}
/*
=============
R_FogParseServerMessage
handle an 'svc_fog' message from server
=============
*/
void R_FogParseServerMessage (void)
{
float density, red, green, blue, time;
density = MSG_ReadByte(net_message) / 255.0;
red = MSG_ReadByte(net_message) / 255.0;
green = MSG_ReadByte(net_message) / 255.0;
blue = MSG_ReadByte(net_message) / 255.0;
time = max(0.0, MSG_ReadShort(net_message) / 100.0);
R_FogUpdate (density, red, green, blue, time);
}
/*
=============
R_FogParseServerMessage2 - parse Nehahra fog
handle an 'svc_fogn' message from server
=============
*/
void R_FogParseServerMessage2 (void)
{
float density, red, green, blue;
density = MSG_ReadFloat(net_message);
red = MSG_ReadByte(net_message) / 255.0;
green = MSG_ReadByte(net_message) / 255.0;
blue = MSG_ReadByte(net_message) / 255.0;
R_FogUpdate (density, red, green, blue, 0.0);
}
/*
=============
R_Fog_f
handle the 'fog' console command
=============
*/
void R_Fog_f (void)
{
switch (Cmd_Argc())
{
default:
case 1:
Con_Printf ("usage:\n");
Con_Printf (" fog <density>\n");
Con_Printf (" fog <density> <rgb>\n");
Con_Printf (" fog <red> <green> <blue>\n");
Con_Printf (" fog <density> <red> <green> <blue>\n");
Con_Printf("current values:\n");
Con_Printf(" fog is %sabled\n", gl_fogenable.value ? "en" : "dis");
Con_Printf(" density is %f\n", gl_fogdensity.value);
Con_Printf(" red is %f\n", gl_fogred.value);
Con_Printf(" green is %f\n", gl_foggreen.value);
Con_Printf(" blue is %f\n", gl_fogblue.value);
break;
case 2:
R_FogUpdate(max(0.0, atof(Cmd_Argv(1))),
gl_fogred.value,
gl_foggreen.value,
gl_fogblue.value,
0.0);
break;
case 3:
R_FogUpdate(max(0.0, atof(Cmd_Argv(1))),
CLAMP(0.0, atof(Cmd_Argv(2)), 1.0),
CLAMP(0.0, atof(Cmd_Argv(2)), 1.0),
CLAMP(0.0, atof(Cmd_Argv(2)), 1.0),
0.0);
break;
case 4:
R_FogUpdate(gl_fogdensity.value,
CLAMP(0.0, atof(Cmd_Argv(1)), 1.0),
CLAMP(0.0, atof(Cmd_Argv(2)), 1.0),
CLAMP(0.0, atof(Cmd_Argv(3)), 1.0),
0.0);
break;
case 5:
R_FogUpdate(max(0.0, atof(Cmd_Argv(1))),
CLAMP(0.0, atof(Cmd_Argv(2)), 1.0),
CLAMP(0.0, atof(Cmd_Argv(3)), 1.0),
CLAMP(0.0, atof(Cmd_Argv(4)), 1.0),
0.0);
break;
}
}
/*
=============
R_FogGetColor
calculates fog color for this frame
=============
*/
float *R_FogGetColor (void)
{
static float c[4];
int i;
c[0] = gl_fogred.value;
c[1] = gl_foggreen.value;
c[2] = gl_fogblue.value;
c[3] = 1.0;
// find closest 24-bit RGB value, so solid-colored sky can match the fog perfectly
for (i=0;i<3;i++)
c[i] = (float)(Q_rint(c[i] * 255)) / 255.0f;
return c;
}
/*
=============
R_FogGetDensity
returns current density of fog
=============
*/
float R_FogGetDensity (void)
{
if (gl_fogenable.value)
return gl_fogdensity.value;
else
return 0;
}
/*
=============
R_FogSetupFrame
called at the beginning of each frame
=============
*/
void R_FogSetupFrame (void)
{
glFogfv(GL_FOG_COLOR, R_FogGetColor());
glFogf(GL_FOG_DENSITY, R_FogGetDensity() / 256.0f); // was 64.0
}
/*
=============
R_FogEnableGFog
called before drawing stuff that should be fogged
=============
*/
void R_FogEnableGFog (void)
{
if (R_FogGetDensity() > 0)
glEnable(GL_FOG);
}
/*
=============
R_FogDisableGFog
called after drawing stuff that should be fogged
=============
*/
void R_FogDisableGFog (void)
{
if (R_FogGetDensity() > 0)
glDisable(GL_FOG);
}
/*
=============
R_FogStartAdditive
called before drawing stuff that is additive blended -- sets fog color to black
=============
*/
void R_FogStartAdditive (void)
{
vec3_t color = {0,0,0};
if (R_FogGetDensity() > 0)
glFogfv(GL_FOG_COLOR, color);
}
/*
=============
R_FogStopAdditive
called after drawing stuff that is additive blended -- restores fog color
=============
*/
void R_FogStopAdditive (void)
{
if (R_FogGetDensity() > 0)
glFogfv(GL_FOG_COLOR, R_FogGetColor());
}
/*
==============================================================================
SKY DRAW ROUTINES
==============================================================================
*/
// skybox
vec3_t skyclip[6] = {
{1,1,0},
{1,-1,0},
{0,-1,1},
{0,1,1},
{1,0,1},
{-1,0,1}
};
int st_to_vec[6][3] =
{
{3,-1,2},
{-3,1,2},
{1,3,2},
{-1,-3,2},
{-2,-1,3}, // 0 degrees yaw, look straight up
{2,-1,-3} // look straight down
};
int vec_to_st[6][3] =
{
{-2,3,1},
{2,3,-1},
{1,3,2},
{-1,3,-2},
{-2,-1,3},
{-2,1,-3}
};
float skymins[2][6], skymaxs[2][6];
int skytexorder[6] = {0,2,1,3,4,5}; // for skybox
/*
=============================================================
RENDER SKYBOX
=============================================================
*/
gltexture_t *skyboxtextures[6];
qboolean oldsky;
char skybox_name[MAX_QPATH] = ""; // name of current skybox, or "" if no skybox
char *suf[6] = {"rt", "bk", "lf", "ft", "up", "dn"}; // for skybox
/*
==================
R_LoadSkyBox
==================
*/
void R_LoadSkyBox (char *skybox)
{
int i, width, height;
int mark;
char name[MAX_OSPATH];
byte *data;
qboolean nonefound = true;
if (!strcmp(skybox_name, skybox))
return; // no change
// purge old textures
for (i = 0; i < 6; i++)
{
if (skyboxtextures[i] && skyboxtextures[i] != notexture)
GL_FreeTexture (skyboxtextures[i]);
skyboxtextures[i] = NULL;
}
// turn off skybox if sky is set to ""
if (skybox[0] == 0)
{
skybox_name[0] = 0;
oldsky = (skybox_name[0] == 0);
Con_DPrintf ("skybox set to \"\"\n");
return;
}
// load textures
for (i = 0 ; i < 6 ; i++)
{
mark = Hunk_LowMark ();
sprintf (name, "gfx/env/%s%s", skybox, suf[i]);
data = GL_LoadImage (name, &width, &height);
if (data)
{
skyboxtextures[i] = GL_LoadTexture (cl.worldmodel, name, width, height, SRC_RGBA, data, name, 0, TEXPREF_NONE);
nonefound = false;
}
else
{
Con_Printf ("Couldn't load %s\n", name);
skyboxtextures[i] = notexture;
}
// free (data);
Hunk_FreeToLowMark (mark);
}
if (nonefound) // go back to scrolling sky if skybox is totally missing
{
for (i = 0; i < 6; i++)
{
if (skyboxtextures[i] && skyboxtextures[i] != notexture)
GL_FreeTexture (skyboxtextures[i]);
skyboxtextures[i] = NULL;
}
skybox_name[0] = 0;
}
else
{
strcpy (skybox_name, skybox);
Con_DPrintf ("skybox set to %s\n", skybox);
}
// enable/disable skybox
oldsky = (skybox_name[0] == 0);
}
/*
=================
R_InitMapGlobals
called when quake initializes
=================
*/
void R_InitMapGlobals (void)
{
int i;
// clear skyboxtextures pointers
for (i=0; i<6; i++)
skyboxtextures[i] = NULL;
// set up global fog
glFogi(GL_FOG_MODE, GL_EXP2);
}
float globalwateralpha = 0.0;
/*
=================
R_ParseWorldspawnNewMap
=================
*/
void R_ParseWorldspawnNewMap (void)
{
char key[MAX_KEY], value[MAX_VALUE];
char *data;
int i;
// initially no skybox
oldsky = true;
strcpy (skybox_name, "");
for (i=0; i<6; i++)
skyboxtextures[i] = NULL;
// initially no fog enabled
Cvar_SetValue ("gl_fogenable", 0);
// initially no wateralpha
globalwateralpha = 0.0;
data = cl.worldmodel->entities;
if (!data)
return;
data = COM_Parse(data);
if (!data) // should never happen
return; // error
if (com_token[0] != '{') // should never happen
return; // error
while (1)
{
data = COM_Parse(data);
if (!data)
return; // error
if (com_token[0] == '}')
break; // end of worldspawn
if (com_token[0] == '_')
strcpy(key, com_token + 1); // Support "_sky" and "_fog" also
else
strcpy(key, com_token);
while (key[strlen(key)-1] == ' ') // remove trailing spaces
key[strlen(key)-1] = 0;
data = COM_Parse(data);
if (!data)
return; // error
strcpy(value, com_token);
if (!strcmp("sky", key) && value[0])
R_LoadSkyBox(value);
// also accept non-standard keys
else if (!strcmp("skyname", key) && value[0]) // half-life
R_LoadSkyBox(value);
else if (!strcmp("qlsky", key) && value[0]) // quake lives
R_LoadSkyBox(value);
else if (!strcmp("fog", key) && value[0])
{
float density, red, green, blue;
sscanf(value, "%f %f %f %f", &density, &red, &green, &blue);
R_FogUpdate (density, red, green, blue, 0.0);
}
else if (!strcmp("wateralpha", key) && value[0])
{
globalwateralpha = atof (value);
}
else if (!strcmp("mapversion", key) && value[0])
{
Con_DPrintf("mapversion is %i\n", atoi(value));
}
}
}
/*
=================
R_Sky_f
=================
*/
void R_Sky_f (void)
{
switch (Cmd_Argc())
{
case 1:
Con_Printf("\"sky\" is \"%s\"\n", skybox_name);
break;
case 2:
R_LoadSkyBox(Cmd_Argv(1));
break;
default:
Con_Printf("usage: sky <skyname>\n");
}
}
/*
==============
R_SkyEmitSkyBoxVertex
==============
*/
void R_SkyEmitSkyBoxVertex (float s, float t, int axis)
{
vec3_t v, b;
int j, k;
float w, h;
b[0] = s * FARCLIP / sqrt(3.0);
b[1] = t * FARCLIP / sqrt(3.0);
b[2] = FARCLIP / sqrt(3.0);
for (j=0 ; j<3 ; j++)
{
k = st_to_vec[axis][j];
if (k < 0)
v[j] = -b[-k - 1];
else
v[j] = b[k - 1];