This repository has been archived by the owner on Dec 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
r_main.c
1778 lines (1481 loc) · 57.4 KB
/
r_main.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.
Copyright (C) 1999-2012 other authors as noted in code comments
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 3 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. */
// r_main.c
#include "quakedef.h"
#include "r_local.h"
#define DITHER_NUMRANDS 3947 //qb: number of random floats for fog dithering
void MakeMy15to8();
void ParseWorldspawn (void);
void R_LoadPalette_f (void); //qb: load an alternate palette
extern short *d_pzbuffer;
extern unsigned int d_zwidth;
extern int d_scantable[MAXHEIGHT];
void *colormap;
//vec3_t viewlightvec; // Manoel Kasimier - changed alias models lighting - removed
//alight_t r_viewlighting = {128, 192, viewlightvec}; // Manoel Kasimier - changed alias models lighting - removed
float r_time1;
unsigned int r_numallocatededges;
//qb: remove qboolean r_drawpolys;
//qb: remove qboolean r_drawculledpolys;
//qb: remove qboolean r_worldpolysbacktofront;
qboolean r_recursiveaffinetriangles = true;
int r_pixbytes = 1;
float r_aliasuvscale = 1.0;
int r_outofsurfaces;
int r_outofedges;
qboolean r_dowarp, r_dowarpold, r_viewchanged;
int numbtofpolys;
btofpoly_t *pbtofpolys;
mvertex_t *r_pcurrentvertbase;
int c_surf;
unsigned int r_maxsurfsseen, r_maxedgesseen, r_cnumsurfs;
qboolean r_surfsonstack;
int r_clipflags;
//qb: from engoo
// COLOR Translation stuff
// Came straight out of image.c of Quake2 tools
byte palmap[32][32][32]; // For fast 15-bit lookup
byte palmapnofb[32][32][32]; // No fullbrights
#if !defined(FLASH)
byte *r_stack_start;
#endif
byte *r_warpbuffer;
//qboolean r_fov_greater_than_90;
//
// view origin
//
vec3_t vup, base_vup;
vec3_t vpn, base_vpn;
vec3_t vright, base_vright;
vec3_t r_origin;
//
// screen size info
//
refdef_t r_refdef;
float xcenter, ycenter;
float xscale, yscale;
float xscaleinv, yscaleinv;
float xscaleshrink, yscaleshrink;
float aliasxscale, aliasyscale, aliasxcenter, aliasycenter;
int screenwidth;
float pixelAspect;
float screenAspect;
float verticalFieldOfView;
float xOrigin, yOrigin;
mplane_t screenedge[4];
//qb: move to d_scan.c byte *warpbuffer = NULL; // Manoel Kasimier - hi-res waterwarp & buffered video
float ditherfog[DITHER_NUMRANDS]; //qb: pseudorandom dither
//
// refresh flags
//
int r_framecount = 1; // so frame counts initialized to 0 don't match
int r_visframecount;
int d_spanpixcount;
int r_polycount;
int r_drawnpolycount;
int r_wholepolycount;
#define VIEWMODNAME_LENGTH 256
char viewmodname[VIEWMODNAME_LENGTH+1];
int modcount;
int *pfrustum_indexes[4];
int r_frustum_indexes[4*6];
int reinit_surfcache = 1; // if 1, surface cache is currently empty and
// must be reinitialized for current cache size
mleaf_t *r_viewleaf, *r_oldviewleaf;
texture_t *r_notexture_mip;
float r_aliastransition, r_resfudge;
int d_lightstylevalue[256]; // 8.8 fraction of base light value
float dp_time1, dp_time2, db_time1, db_time2, rw_time1, rw_time2;
float se_time1, se_time2, de_time1, de_time2, dv_time1, dv_time2;
void R_MarkLeaves (void);
extern cvar_t vid_windowed_mode; //qb
cvar_t r_draworder = {"r_draworder","0", "r_draworder[0/1] Toggle draw backward spans."};
cvar_t r_speeds = {"r_speeds","0", "r_speeds[0/1] Toggle display of drawing time, face clips, polygon count, drawn polygon count, and surfaces per frame."};
//qb: fte stain cvars
cvar_t r_stainfadeamount = {"r_stainfadeamount", "0.5", "r_stainfadeamount[value] Amount to fade stain each cycle."};
cvar_t r_stainfadetime = {"r_stainfadetime", "5.0", "r_stainfadetime[time] How long a stain will stay before fading."};
cvar_t r_stains = {"r_stains", "0.75", "r_stains[0.0 - 1.0] Stainmap opacity. Set to 0 to turn off stains."}; //zero to one
cvar_t r_timegraph = {"r_timegraph","0", "r_timegraph[0/1] Toggle display of a performance graph. (Lower levels means better performance.)"};
cvar_t r_graphheight = {"r_graphheight","10", "r_graphheight[value] Set the number of lines displayed in the timegraph."};
cvar_t r_clearcolor = {"r_clearcolor","2", "r_clearcolor[palette index] The color for areas outside of the current map. Only seen if moving with noclip."};
cvar_t r_waterwarp = {"r_waterwarp","1", "r_waterwarp[0/1] Toggles whether the view is warped when in a liquid."};
cvar_t r_fullbright = {"r_fullbright","0", "r_fullbright[0/1] Toggles shading off."};
cvar_t r_drawentities = {"r_drawentities","1", "r_drawentities[0/1] Toggles the drawing of all objects (entities)."};
cvar_t r_drawviewmodel = {"r_drawviewmodel","1", "r_drawviewmodel[0/1] Toggles drawing of first-person model.", true}; // Manoel Kasimier - saved in the config file - edited
cvar_t r_aliasstats = {"r_polymodelstats","0", "r_polymodelstats[0/1] Show number of polygon models drawn per frame."};
cvar_t r_dspeeds = {"r_dspeeds","0", "r_dspeeds[0/1]-Toggles the display of drawing times per frame: render time, particles, world, brushes, scan edges, entities, and viewmodel."};
cvar_t r_drawflat = {"r_drawflat", "0", "r_drawflat[0/1] Toggles the drawing of texture maps (0=use texture maps.)"};
cvar_t r_ambient = {"r_ambient", "15", "r_ambient[value] Set minimum value for map lighting."}; //qb: avoid total black
//qb: nolerp list from FQ
cvar_t r_nolerp_list = {"r_nolerp_list", "progs/flame.mdl,progs/flame2.mdl,progs/braztall.mdl,progs/brazshrt.mdl,progs/longtrch.mdl,progs/flame_pyre.mdl,progs/v_saw.mdl,progs/v_xfist.mdl,progs/h2stuff/newfire.mdl",
"r_nolerp_list[models] Do not smooth animation for these models."
};
cvar_t r_coloredlights = {"r_coloredlights", "1", "r_coloredlights[0/1] Toggle use of colored lighting.", true}; //qb:
cvar_t r_clbaseweight = {"r_clbaseweight", "0.8", "r_clbaseweight[0.0 - 1.0] Importance of texture color in colored lighting precalculation.", true}; //qb: base pixel weight for color map blending
cvar_t r_clcolorweight= {"r_clcolorweight", "0.5", "r_clcolorweight[0.0 - 1.0] Importance of lighting color in colored lighting precalculation.", true}; //qb: color weight for color map blending
cvar_t r_fog = {"r_fog", "1", "r_fog[0/1] Toggle rendering of fog.", true}; //qb: draw fog?
cvar_t r_reportsurfout = {"r_reportsurfout", "0", "r_reportsurfout[0/1] Toggle report of surfaces dropped because > r_maxsurfs."};
cvar_t r_maxsurfs = {"r_maxsurfs", "63000", "r_maxsurfs[value] Sets the maximum number of surfaces. Setting take effect on map restart."};
cvar_t r_numsurfs = {"r_numsurfs", "0", "r_numsurfs[0/1] Toggles display of number of surfaces in current view."};
cvar_t r_reportedgeout = {"r_reportedgeout", "0", "r_reportedgeout[0/1] Toggle report of edges dropped because > r_maxedges."};
cvar_t r_maxedges = {"r_maxedges", "63000", "r_maxedges[value] Sets the maximum number of edges. Setting take effect on map restart."};
cvar_t r_numedges = {"r_numedges", "0", "r_numedges[0/1] Toggles display of number of surfaces in current view."};
//cvar_t r_letterbox = {"r_letterbox","0"}; // Manoel Kasimier - r_letterbox
// Manoel Kasimier - changed alias models lighting - begin
cvar_t r_light_vec_x = {"r_light_vec_x", "-1", "r_light_vec_x[value] X vector when r_light_style is active."};
cvar_t r_light_vec_y = {"r_light_vec_y", "0", "r_light_vec_y[value] Y vector when r_light_style is active."};
cvar_t r_light_vec_z = {"r_light_vec_z", "-1", "r_light_vec_z[value] Z vector when r_light_style is active."};
cvar_t r_light_style = {"r_light_style", "1", "r_light_style[0/1] Toggle dramatic lighting of models.", true};
// Manoel Kasimier - changed alias models lighting - end
cvar_t r_wateralpha = {"r_wateralpha","0.50", "r_wateralpha[0.0 - 1.0] Alpha of water surfaces.", true}; // Manoel Kasimier - translucent water
cvar_t r_glassalpha = {"r_glassalpha","0.33", "r_glassalpha[0.0 - 1.0] Alpha of glass surfaces.", true}; //qb: *glass
cvar_t r_shadowhack = {"r_shadowhack", "0", "r_shadowhack[0/1] Toggle use of darklights to fake entity shadows.", false};
cvar_t r_shadowhacksize = {"r_shadowhacksize", "2.7", "r_shadowhacksize[value] Radius factor of fake entity shadows.", true};
//qb: particle cvars
cvar_t r_part_scale = {"r_part_scale", "1.0", "r_part_scale[value] Particle scale.", true};
cvar_t r_part_blob_count = {"r_part_blob_count", "256", "r_part_blob_count[value] Number of particles in blob explosion.", true};
cvar_t r_part_blob_time = {"r_part_blob_time", "0.2", "r_part_blob_time[value] Particle lifespan for blob explosion.", true};
cvar_t r_part_blob_vel = {"r_part_blob_vel", "200", "r_part_blob_vel[value] Particle velocity for blob explosion.", true};
cvar_t r_part_explo1_count = {"r_part_explo1_count", "400", "r_part_explo1_count[value] Number of particles for explo1 effect.", true};
cvar_t r_part_explo1_time = {"r_part_explo1_time", "5", "r_part_explo1_time[value] Particle lifespan for explo1 effect.", true};
cvar_t r_part_explo1_vel = {"r_part_explo1_vel", "400", "r_part_explo1_vel[value] Particle velocity for explo1 effect.", true};
cvar_t r_part_explo2_count = {"r_part_explo2_count", "200", "r_part_explo2_count[value] Number of particles for explo2 effect.", true};
cvar_t r_part_explo2_time = {"r_part_explo2_time", "0.3", "r_part_explo2_time[value] Particle lifespan for explo2 effect.", true};
cvar_t r_part_explo2_vel = {"r_part_explo2_vel", "300", "r_part_explo2_vel[value] Particle velocity for explo2 effect.", true};
cvar_t r_part_sticky_time = {"r_part_sticky_time", "24", "r_part_sticky_time[value] Lifespan for sticky particles.", true};
cvar_t thread_warp = {"thread_warp","4", "thread_warp[value] Number of threads to use for waterwarp, up to 16. Values less than 2 are unthreaded", true}; // Manoel Kasimier - saved in the config file - edited
cvar_t thread_flip = {"thread_flip","2", "thread_flip[value] Number of threads to use for flipping graphics to video, up to 16. Values less than 2 are unthreaded", true}; // Manoel Kasimier - saved in the config file - edited
cvar_t thread_fog = {"thread_fog","4", "thread_fog[value] Number of threads to use for fog effect, up to 16. Values less than 2 are unthreaded", true}; // Manoel Kasimier - saved in the config file - edited
//void CreatePassages (void); // Manoel Kasimier - removed
//void SetVisibilityByPassages (void); // Manoel Kasimier - removed
int sintable[SIN_BUFFER_SIZE];
void R_InitSin (void)
{
int x;
// run this only once, at engine startup
for (x = 0 ; x < SIN_BUFFER_SIZE ; x++)
sintable[x] = (int) (AMP + sin ( (double)x * 3.14159 * 2.0 / CYCLE) * AMP);
}
/*
==================
R_InitTextures
==================
*/
void R_InitTextures (void)
{
int x,y, m;
byte *dest;
// create a simple checkerboard texture for the default
r_notexture_mip = Hunk_AllocName (sizeof(texture_t) + 16*16+8*8+4*4+2*2, "notexture");
r_notexture_mip->width = r_notexture_mip->height = 16;
r_notexture_mip->offsets[0] = sizeof(texture_t);
r_notexture_mip->offsets[1] = r_notexture_mip->offsets[0] + 16*16;
r_notexture_mip->offsets[2] = r_notexture_mip->offsets[1] + 8*8;
r_notexture_mip->offsets[3] = r_notexture_mip->offsets[2] + 4*4;
for (m=0 ; m<4 ; m++)
{
dest = (byte *)r_notexture_mip + r_notexture_mip->offsets[m];
for (y=0 ; y< (16>>m) ; y++)
for (x=0 ; x< (16>>m) ; x++)
{
if ( (y< (8>>m) ) ^ (x< (8>>m) ) )
*dest++ = 0;
else
*dest++ = 0xff;
}
}
}
/*
===============
R_Init
===============
*/
void FogDitherInit(void)
{
int i;
float dfactor;
dfactor = max(1.0-fog_density,0.01)* 0.33; //qb: would rather hand-tune this multiplier than make cvar.
for (i=0; i<DITHER_NUMRANDS; i++)
ditherfog[i] = dfactor + (float)(rand()%10000)/100000.0;
}
int R_LoadPalette (char *name); //qb: load an alternate palette
void R_LoadSky_f (void); // Manoel Kasimier - skyboxes // Code taken from the ToChriS engine - Author: Vic (vic@quakesrc.org) (http://hkitchen.quakesrc.org/)
void R_Init (void)
{
int i;
#if !defined(FLASH)
int dummy;
// get stack position so we can guess if we are going to overflow
r_stack_start = (byte *)&dummy;
#endif
MakeMy15to8(); //qb: from engoo
//qb: R_InitTurb ();
R_InitSin(); //qb: from MK tute on inside3d
Cmd_AddCommand ("loadpalette", R_LoadPalette_f);
Cmd_AddCommand ("timerefresh", R_TimeRefresh_f);
Cmd_AddCommand ("pointfile", R_ReadPointFile_f);
Cmd_AddCommand ("loadsky", R_LoadSky_f); // Manoel Kasimier - skyboxes // Code taken from the ToChriS engine - Author: Vic (vic@quakesrc.org) (http://hkitchen.quakesrc.org/)
Cmd_AddCommand ("fog", Fog_FogCommand_f); // qb: fog commands from FitzQuake
Cmd_AddCommand ("gl_texturemode", Placebo_f); //qb: ignore common commands with no equivalent, stuffed by mod
Cmd_AddCommand ("gl_clear", Placebo_f);
Cmd_AddCommand ("gl_overbright", Placebo_f);
//set up global fog defaults
fog_density = 0.0;
fog_red = 0.17;
fog_green = 0.17;
fog_blue = 0.17;
srand(8); //leave nothing to chance.
FogDitherInit();
Cvar_RegisterVariable (&r_draworder);
Cvar_RegisterVariable (&r_speeds);
Cvar_RegisterVariable (&r_timegraph);
Cvar_RegisterVariable (&r_graphheight);
Cvar_RegisterVariable (&r_drawflat);
Cvar_RegisterVariable (&r_ambient);
Cvar_RegisterVariable (&r_coloredlights); //qb:
Cvar_RegisterVariable (&r_clbaseweight); //qb:
Cvar_RegisterVariable (&r_clcolorweight); //qb:
//qb: Cvar_RegisterVariable (&r_colmaprange); //qb:
Cvar_RegisterVariable (&r_fog); //qb:
Cvar_RegisterVariable (&r_clearcolor);
Cvar_RegisterVariable (&r_waterwarp);
Cvar_RegisterVariable (&r_fullbright);
Cvar_RegisterVariable (&r_drawentities);
Cvar_RegisterVariable (&r_drawviewmodel);
Cvar_RegisterVariable (&r_aliasstats);
Cvar_RegisterVariable (&r_dspeeds);
Cvar_RegisterVariable (&r_reportsurfout);
Cvar_RegisterVariable (&r_maxsurfs);
Cvar_RegisterVariable (&r_numsurfs);
Cvar_RegisterVariable (&r_reportedgeout);
Cvar_RegisterVariable (&r_maxedges);
Cvar_RegisterVariable (&r_skyname); // Manoel Kasimier - skyboxes // Code taken from the ToChriS engine - Author: Vic (vic@quakesrc.org) (http://hkitchen.quakesrc.org/)
// Cvar_RegisterVariableWithCallback (&r_letterbox, SCR_Adjust); // Manoel Kasimier - r_letterbox
// Manoel Kasimier - changed alias models lighting - begin
Cvar_RegisterVariable (&r_light_vec_x);
Cvar_RegisterVariable (&r_light_vec_y);
Cvar_RegisterVariable (&r_light_vec_z);
Cvar_RegisterVariable (&r_light_style);
// Manoel Kasimier - changed alias models lighting - end
Cvar_RegisterVariable (&r_interpolation); // Manoel Kasimier - model interpolation
Cvar_RegisterVariable (&r_wateralpha); // Manoel Kasimier - translucent water
Cvar_RegisterVariable (&r_glassalpha); //qb: *glass
Cvar_RegisterVariable (&sw_stipplealpha); // Manoel Kasimier
// Cvar_RegisterVariable (&r_sprite_addblend); // Manoel Kasimier
Cvar_RegisterVariable (&r_shadowhack); //qb: engoo shadowhack
Cvar_RegisterVariable (&r_shadowhacksize); //qb
Cvar_RegisterVariable (&r_nolerp_list); //qb: from FQ
//qb: ftestain cvars
Cvar_RegisterVariable(&r_stains);
Cvar_RegisterVariable(&r_stainfadetime);
Cvar_RegisterVariable(&r_stainfadeamount);
//qb: particle cvars
Cvar_RegisterVariable(&r_part_scale);
Cvar_RegisterVariable(&r_part_blob_count);
Cvar_RegisterVariable(&r_part_blob_time);
Cvar_RegisterVariable(&r_part_blob_vel);
Cvar_RegisterVariable(&r_part_explo1_count);
Cvar_RegisterVariable(&r_part_explo1_time);
Cvar_RegisterVariable(&r_part_explo1_vel);
Cvar_RegisterVariable(&r_part_explo2_count);
Cvar_RegisterVariable(&r_part_explo2_time);
Cvar_RegisterVariable(&r_part_explo2_vel);
Cvar_RegisterVariable(&r_part_sticky_time);
//qb: threads
Cvar_RegisterVariable(&thread_warp);
Cvar_RegisterVariable(&thread_flip);
Cvar_RegisterVariable(&thread_fog);
view_clipplanes[0].leftedge = true;
view_clipplanes[1].rightedge = true;
view_clipplanes[1].leftedge = view_clipplanes[2].leftedge =
view_clipplanes[3].leftedge = false;
view_clipplanes[0].rightedge = view_clipplanes[2].rightedge =
view_clipplanes[3].rightedge = false;
r_refdef.xOrigin = XCENTERING;
r_refdef.yOrigin = YCENTERING;
R_InitParticles ();
D_Init ();
}
// Manoel Kasimier - skyboxes - begin
// Code taken from the ToChriS engine - Author: Vic (vic@quakesrc.org) (http://hkitchen.quakesrc.org/)
void CL_ParseEntityLump (char *entdata)
{
char *data;
char key[128], value[4096];
extern cvar_t r_skyname;
data = entdata;
if (!data)
return;
data = COM_Parse (data);
if (!data || com_token[0] != '{')
return; // error
while (1)
{
data = COM_Parse (data);
if (!data)
return; // error
if (com_token[0] == '}')
break; // end of worldspawn
if (com_token[0] == '_')
Q_strcpy(key, com_token + 1);
else
Q_strcpy(key, com_token);
while (key[Q_strlen(key)-1] == ' ')
key[Q_strlen(key)-1] = 0; // remove trailing spaces
data = COM_Parse (data);
if (!data)
return; // error
Q_strcpy (value, com_token);
if (Q_strcmp (key, "sky") == 0 || Q_strcmp (key, "skyname") == 0 ||
Q_strcmp (key, "qlsky") == 0)
// Cvar_Set ("r_skyname", value);
// Manoel Kasimier - begin
{
Cbuf_AddText(va("wait;loadsky %s\n", value));
// R_LoadSky(value);
return; // Manoel Kasimier
}
// Manoel Kasimier - end
// more checks here..
if (Q_strcmp (key, "palette") == 0) //qb: use palette field in worldspawn.
{
Cbuf_AddText(va("wait;loadpalette %s\n", value));
return;
}
}
// Manoel Kasimier - begin
if (r_skyname.string[0])
Cbuf_AddText(va("wait;loadsky %s\n", r_skyname.string));
// R_LoadSky(r_skyname.string); // crashes the engine if the r_skyname cvar is set before the game boots
else
R_LoadSky("");
// Manoel Kasimier - end
}
// Manoel Kasimier - skyboxes - end
/*
=============================================================================
COLORMAP GRABBING
=============================================================================
*/
//qb: 15to8 stealed from engoo.
void MakeMy15to8()
{
int r, g, b, beastcolor, beefstcolor;
for (r=0 ; r<256 ; r+=8)
{
for (g=0 ; g<256 ; g+=8)
{
for (b=0 ; b<256 ; b+=8)
{
beastcolor = BestColor (r, g, b, 0, 254);
beefstcolor = BestColor (r, g, b, 0, 223);
palmap[r>>3][g>>3][b>>3] = beastcolor;
palmapnofb[r>>3][g>>3][b>>3] = beefstcolor;
}
}
}
}
/*
===============
BestColor - qb: from qlumpy
===============
*/
int BestColor (int r, int g, int b, int start, int stop)
{
int i;
int dr, dg, db;
int bestdistortion, distortion;
int bestcolor;
byte *pal;
r = bound (0,r,254);
g = bound (0,g,254);
b = bound (0,b,254);
//
// let any color go to 0 as a last resort
//
bestdistortion = ( (int)r + (int)g + (int)b )*2; //qb: ( (int)r*r + (int)g*g + (int)b*b )*2;
bestcolor = 0;
pal = host_basepal + start*3;
for (i=start ; i<= stop ; i++)
{
dr = abs(r - (int)pal[0]);
dg = abs(g - (int)pal[1]);
db = abs(b - (int)pal[2]);
pal += 3;
distortion = dr + dg + db; //qb: more weight on value. dr*dr + dg*dg + db*db;
if (distortion < bestdistortion)
{
if (!distortion)
return i; // perfect match
bestdistortion = distortion;
bestcolor = i;
}
}
return bestcolor;
}
void GrabAlphamap (void) //qb: based on Engoo
{
int c,l, r,g,b;
float ay, ae;
byte *colmap;
ay = 0.666667;
ae = 1.0 - ay; // base pixels
colmap = alphamap;
for (l=0; l<256; l++)
{
for (c=0 ; c<256 ; c++)
{
r = (int)(((float)host_basepal[c*3]*ae) + ((float)host_basepal[l*3] *ay));
g = (int)(((float)host_basepal[c*3+1]*ae) + ((float)host_basepal[l*3+1] *ay));
b = (int)(((float)host_basepal[c*3+2]*ae) + ((float)host_basepal[l*3+2] *ay));
*colmap++ =BestColor(r,g,b, 0, 254); // High quality color tables get best color
}
}
}
void GrabAlpha50map (void) //qb: 50% / 50% alpha
{
int c,l, r,g,b;
byte *colmap;
colmap = alpha50map;
for (l=0; l<256; l++)
{
for (c=0 ; c<256 ; c++)
{
r = (int)(((float)host_basepal[c*3]*0.5) + ((float)host_basepal[l*3] *0.5));
g = (int)(((float)host_basepal[c*3+1]*0.5) + ((float)host_basepal[l*3+1] *0.5));
b = (int)(((float)host_basepal[c*3+2]*0.5) + ((float)host_basepal[l*3+2] *0.5));
*colmap++ =BestColor(r,g,b, 0, 254); // High quality color tables get best color
}
}
}
void GrabFogmap (void) //qb: yet another lookup
{
int c,l, r,g,b;
byte *colmap;
colmap = fogmap;
for (l=0; l<256; l++)
{
for (c=0 ; c<256 ; c++)
{
r = (host_basepal[c*3] + host_basepal[l*3] + (c/32.0));
g = (host_basepal[c*3+1] + host_basepal[l*3+1] + (c/32.0));
b = (host_basepal[c*3+2] + host_basepal[l*3+2] + (c/32.0));
*colmap++ = BestColor(r,g,b, 0, 254); // High quality color tables get best color
}
}
}
void GrabLightcolormap (void) //qb: for colored lighting, fullbrights show through
{
int c,p,i, r,g,b;
float rc,gc,bc, rp,gp,bp, coloravg, brightscale;
float ay, ae;
byte *colmap;
if(coloredlights == 1)
{
ae = bound (0, r_clbaseweight.value, 1.0); // base pixels
ay = bound (0, r_clcolorweight.value, 1.0); //color
}
else
{
ae = 1.0;
ay = 0.0;
}
colmap = lightcolormap;
for (c=0; c<256; c++)
{
rc=host_basepal[c*3];
gc=host_basepal[c*3+1];
bc=host_basepal[c*3+2];
coloravg = (rc+gc+bc+sqrt(rc+gc+bc))/4.0;
rc = (rc-coloravg)*ay;
if (rc < 0) rc = 0;
bc = (bc-coloravg)*ay;
if (bc < 0) bc = 0;
gc = (gc-coloravg)*ay;
if (gc < 0) gc = 0;
for (p=0 ; p<256 ; p++)
{
if (p>223)
*colmap++ = p;
else
{
rp=host_basepal[p*3];
gp=host_basepal[p*3+1];
bp=host_basepal[p*3+2];
// brightscale = (float)(rp+gp+bp)/(brightcolor+0.5);
//bright = sqrt((rl+gl+bl)/(host_basepal[c*3]+host_basepal[c*3+1]+host_basepal[c*3+2]+1.0));
r = rp+rc ; //(rc * rc * ay)+ rp * (ae + (rc * ay));
g = gp+gc ; //(gc * gc * ay)+ gp * (ae + (gc * ay));
b = bp+bc ; //(bc * bc * ay)+ bp * (ae + (bc * ay));
*colmap++ = BestColor(r,g,b, 0, 223);
}
}
}
}
void GrabAdditivemap (void) //qb: based on Engoo
{
int c,l, red, green, blue;
float ay, ae;
byte *colmap;
ay = 1.0;
ae = 1.0; // base pixels
colmap = additivemap;
for (l=0; l<256; l++)
{
for (c=0 ; c<256 ; c++)
{
red = (int)(((float)host_basepal[c*3]*ae) + ((float)host_basepal[l*3] *ay));
green = (int)(((float)host_basepal[c*3+1]*ae) + ((float)host_basepal[l*3+1] *ay));
blue = (int)(((float)host_basepal[c*3+2]*ae) + ((float)host_basepal[l*3+2] *ay));
*colmap++ = BestColor(red,green,blue, 0, 254); // High quality color tables get best color
}
}
}
/*
==============
GrabColormap - qb: from qlumpy
filename COLORMAP levels fullbrights
the first map is an identiy 0-255
the final map is all black except for the fullbrights
the remaining maps are evenly spread
fullbright colors start at the top of the palette.
==============
*/
void GrabColormap (void) //qb: fixed, was a little screwy
{
int l, c, red, green, blue;
float frac, fracscaled;
float rscaled, gscaled, bscaled;
byte *colmap;
colmap = host_colormap;
// identity lump
// for (l=0 ; l<256 ; l++)
// *colmap++ = l;
// shaded levels
for (l=0; l<COLORLEVELS; l++)
{
frac = (float)l/(COLORLEVELS-1);
frac = 1.0 - (frac );
for (c=0 ; c<256-PALBRIGHTS ; c++)
{
red = (int)((float)host_basepal[c*3]*frac); //+ rscaled);
green = (int)((float)host_basepal[c*3+1]*frac); //+ gscaled);
blue = (int)((float)host_basepal[c*3+2]*frac); // + bscaled);
//
// note: 254 instead of 255 because 255 is the transparent color, and we
// don't want anything remapping to that
//
*colmap++ = BestColor(red,green,blue, 0, 254);
}
for ( ; c<256 ; c++)
{
red = (int)host_basepal[c*3];
green = (int)host_basepal[c*3+1];
blue = (int)host_basepal[c*3+2];
*colmap++ = BestColor(red,green,blue, 0, 254);
}
}
}
/*
=============================================================================
MIPTEX GRABBING
=============================================================================
*/
/*
===============
R_LoadPalette
===============
*/
int R_LoadPalette (char *name) //qb: load an alternate palette
{
loadedfile_t *fileinfo;
char pathname[MAX_QPATH];
Q_snprintfz (pathname, sizeof(pathname), "gfx/%s.lmp", name);
fileinfo = COM_LoadHunkFile (pathname);
if (!fileinfo)
{
Con_Printf("Palette %s not found.\n", name);
return 0;
}
memcpy (host_basepal, fileinfo->data, 768);
coloredlights = (int)r_coloredlights.value; // sanity check
GrabColormap();
GrabAlphamap();
GrabLightcolormap();
GrabAdditivemap();
GrabFogmap();
VID_SetPalette (host_basepal);
Con_Printf("Palette %s loaded.\n", name);
return 1;
}
/*
===============
R_LoadPalette_f
===============
*/
void R_LoadPalette_f (void) //qb: load an alternate palette
{
if (Cmd_Argc() != 2)
{
Con_Printf ("loadpalette <name> : load a color palette\n");
return;
}
R_LoadPalette(Cmd_Argv(1));
}
/*
===============
R_NewMap
===============
*/
void R_NewMap (void)
{
int i;
// clear out efrags in case the level hasn't been reloaded
// FIXME: is this one short?
for (i=0 ; i<cl.worldmodel->numleafs ; i++)
cl.worldmodel->leafs[i].efrags = NULL;
CL_ParseEntityLump (cl.worldmodel->entities); // Manoel Kasimier - skyboxes // Code taken from the ToChriS engine - Author: Vic (vic@quakesrc.org) (http://hkitchen.quakesrc.org/)
r_viewleaf = NULL;
R_ClearParticles ();
R_BuildLightmaps(); //qb: ftestain
ParseWorldspawn (); //qb: based on Fitzquake
R_InitSkyBox (); // Manoel Kasimier - skyboxes // Code taken from the ToChriS engine - Author: Vic (vic@quakesrc.org) (http://hkitchen.quakesrc.org/)
r_cnumsurfs = r_maxsurfs.value;
if (r_cnumsurfs <= MINSURFACES)
r_cnumsurfs = MINSURFACES;
if (r_cnumsurfs > NUMSTACKSURFACES)
{
surfaces = Hunk_AllocName (r_cnumsurfs * sizeof(surf_t), "surfaces");
surface_p = surfaces;
surf_max = &surfaces[r_cnumsurfs];
r_surfsonstack = false;
// surface 0 doesn't really exist; it's just a dummy because index 0
// is used to indicate no edge attached to surface
surfaces--;
}
else
{
r_surfsonstack = true;
}
r_maxedgesseen = 0;
r_maxsurfsseen = 0;
r_numallocatededges = r_maxedges.value;
if (r_numallocatededges < MINEDGES)
r_numallocatededges = MINEDGES;
if (r_numallocatededges <= NUMSTACKEDGES)
{
auxedges = NULL;
}
else
{
auxedges = Hunk_AllocName (r_numallocatededges * sizeof(edge_t),
"edges");
}
r_dowarpold = false;
r_viewchanged = false;
V_BonusFlash_f(); //qb: hack to remove cshift hangover, plus looks good on respawn. Is there a better place for this?
#ifdef PASSAGES
CreatePassages ();
#endif
}
/*
===============
R_SetVrect
===============
*/
void R_SetVrect (vrect_t *pvrectin, vrect_t *pvrect, int lineadj)
{
int h;
float size;
size = scr_viewsize.value > 100 ? 100 : scr_viewsize.value;
if (cl.intermission)
{
size = 100;
lineadj = 0;
}
size /= 100.0; // fixed
h = pvrectin->height - lineadj;
pvrect->width = pvrectin->width * size;
if (pvrect->width < 96)
{
size = 96.0 / pvrectin->width;
pvrect->width = 96; // min for icons
}
pvrect->width &= ~7;
pvrect->height = pvrectin->height * size;
if (pvrect->height > h)//pvrectin->height - lineadj) // optimized
pvrect->height = h;//pvrectin->height - lineadj; // optimized
pvrect->height &= ~1;
pvrect->x = (pvrectin->width - pvrect->width)/2;
pvrect->y = (h - pvrect->height)/2;
{
// Manoel Kasimier - svc_letterbox - start
if (cl.letterbox)
{
int letterbox_height = (int)((float)pvrect->height * (cl.letterbox/2.0));
pvrect->y += letterbox_height; // - sb_lines/2
pvrect->height -= letterbox_height*2;
}
// Manoel Kasimier - svc_letterbox - end
}
}
/*
===============
R_ViewChanged
Called every time the vid structure or r_refdef changes.
Guaranteed to be called before the first refresh
===============
*/
void R_ViewChanged (vrect_t *pvrect, int lineadj)
{
int i;
float res_scale;
r_viewchanged = true;
R_SetVrect (pvrect, &r_refdef.vrect, lineadj);
r_refdef.horizontalFieldOfView = 2.0 * tan (r_refdef.fov_x/360*M_PI);
r_refdef.fvrectx = (float)r_refdef.vrect.x;
r_refdef.fvrectx_adj = (float)r_refdef.vrect.x - 0.5;
r_refdef.vrect_x_adj_shift20 = (r_refdef.vrect.x<<20) + (1<<19) - 1;
r_refdef.fvrecty = (float)r_refdef.vrect.y;
r_refdef.fvrecty_adj = (float)r_refdef.vrect.y - 0.5;
r_refdef.vrectright = r_refdef.vrect.x + r_refdef.vrect.width;
r_refdef.vrectright_adj_shift20 = (r_refdef.vrectright<<20) + (1<<19) - 1;
r_refdef.fvrectright = (float)r_refdef.vrectright;
r_refdef.fvrectright_adj = (float)r_refdef.vrectright - 0.5;
r_refdef.vrectrightedge = (float)r_refdef.vrectright - 0.99;
r_refdef.vrectbottom = r_refdef.vrect.y + r_refdef.vrect.height;
r_refdef.fvrectbottom = (float)r_refdef.vrectbottom;
r_refdef.fvrectbottom_adj = (float)r_refdef.vrectbottom - 0.5;
r_refdef.aliasvrect.x = (int)(r_refdef.vrect.x * r_aliasuvscale);
r_refdef.aliasvrect.y = (int)(r_refdef.vrect.y * r_aliasuvscale);
r_refdef.aliasvrect.width = (int)(r_refdef.vrect.width * r_aliasuvscale);
r_refdef.aliasvrect.height = (int)(r_refdef.vrect.height * r_aliasuvscale);
r_refdef.aliasvrectright = r_refdef.aliasvrect.x +
r_refdef.aliasvrect.width;
r_refdef.aliasvrectbottom = r_refdef.aliasvrect.y +
r_refdef.aliasvrect.height;
//qb: Aardappel fisheye
if(r_fisheye.value)
{
pixelAspect = (float)r_refdef.vrect.height/(float)r_refdef.vrect.width;
screenAspect = (float)(r_refdef.vrect.width*pixelAspect) /(float)r_refdef.vrect.height;
}
else
{
pixelAspect = vid_nativeaspect/((float)(r_refdef.vrect.width) /(float)(r_refdef.vrect.height)); //qb Lavent correction
if(vid_windowed_mode.value)
screenAspect = 1;
else
screenAspect = ((float)r_refdef.vrect.width)*pixelAspect /(float)r_refdef.vrect.height;
}
xOrigin = r_refdef.xOrigin;
yOrigin = r_refdef.yOrigin;
// 320*200 1.0 pixelAspect = 1.6 screenAspect
// 320*240 1.0 pixelAspect = 1.3333 screenAspect
// proper 320*200 pixelAspect = 0.8333333
verticalFieldOfView = r_refdef.horizontalFieldOfView / screenAspect;
// values for perspective projection
// if math were exact, the values would range from 0.5 to to range+0.5
// hopefully they wll be in the 0.000001 to range+.999999 and truncate
// the polygon rasterization will never render in the first row or column
// but will definately render in the [range] row and column, so adjust the
// buffer origin to get an exact edge to edge fill
xcenter = ((float)r_refdef.vrect.width * XCENTERING) +
r_refdef.vrect.x - 0.5;
aliasxcenter = xcenter * r_aliasuvscale;
ycenter = ((float)r_refdef.vrect.height * YCENTERING) +
r_refdef.vrect.y - 0.5;
aliasycenter = ycenter * r_aliasuvscale;
xscale = r_refdef.vrect.width / r_refdef.horizontalFieldOfView;
aliasxscale = xscale * r_aliasuvscale;
xscaleinv = 1.0 / xscale;
yscale = xscale * pixelAspect;
aliasyscale = yscale * r_aliasuvscale;
yscaleinv = 1.0 / yscale;
xscaleshrink = (r_refdef.vrect.width-6)/r_refdef.horizontalFieldOfView;
yscaleshrink = xscaleshrink*pixelAspect;
// left side clip
screenedge[0].normal[0] = -1.0 / (xOrigin*r_refdef.horizontalFieldOfView);
screenedge[0].normal[1] = 0;
screenedge[0].normal[2] = 1;
screenedge[0].type = PLANE_ANYZ;
// right side clip
screenedge[1].normal[0] =
1.0 / ((1.0-xOrigin)*r_refdef.horizontalFieldOfView);
screenedge[1].normal[1] = 0;
screenedge[1].normal[2] = 1;
screenedge[1].type = PLANE_ANYZ;
// top side clip
screenedge[2].normal[0] = 0;
screenedge[2].normal[1] = -1.0 / (yOrigin*verticalFieldOfView);
screenedge[2].normal[2] = 1;
screenedge[2].type = PLANE_ANYZ;
// bottom side clip
screenedge[3].normal[0] = 0;
screenedge[3].normal[1] = 1.0 / ((1.0-yOrigin)*verticalFieldOfView);
screenedge[3].normal[2] = 1;
screenedge[3].type = PLANE_ANYZ;
for (i=0 ; i<4 ; i++)
VectorNormalize (screenedge[i].normal);
res_scale = sqrt ((double)(r_refdef.vrect.width * r_refdef.vrect.height) /