-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgl_surf.c
1893 lines (1570 loc) · 44.5 KB
/
gl_surf.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_surf.c -- surface-related refresh code
#include "quakedef.h"
#define BLOCK_WIDTH 128
#define BLOCK_HEIGHT 128
// was 18*18, added lit support (*3 for RGB) and loosened surface extents maximum (BLOCK_WIDTH*BLOCK_HEIGHT)
#define BLOCKL_SIZE (BLOCK_WIDTH*BLOCK_HEIGHT*3)
unsigned blocklights[BLOCKL_SIZE];
#define MAX_LIGHTMAPS 1024 // was 512 (orig. 64)
gltexture_t *lightmap_textures[MAX_LIGHTMAPS]; // changed to an array
typedef struct glRect_s
{
byte l,t,w,h;
} glRect_t;
glpoly_t *lightmap_polys[MAX_LIGHTMAPS];
qboolean lightmap_modified[MAX_LIGHTMAPS];
glRect_t lightmap_rectchange[MAX_LIGHTMAPS];
int allocated[MAX_LIGHTMAPS][BLOCK_WIDTH];
int last_lightmap_allocated; //ericw -- optimization: remember the index of the last lightmap Lightmap_AllocBlock stored a surf in
// the lightmap texture data needs to be kept in
// main memory so texsubimage can update properly
byte lightmaps[4*MAX_LIGHTMAPS*BLOCK_WIDTH*BLOCK_HEIGHT]; // (4)lightmap_bytes*MAX_LIGHTMAPS*BLOCK_WIDTH*BLOCK_HEIGHT
int d_overbright = 1;
float d_overbrightscale = OVERBRIGHT_SCALE;
/*
============================================================================================================
ALPHA SORTING
============================================================================================================
*/
gl_alphalist_t gl_alphalist[MAX_ALPHA_ITEMS];
int gl_alphalist_num = 0;
/*
===============
R_SetAlphaSurface
===============
*/
qboolean R_SetAlphaSurface(msurface_t *s, float alpha)
{
if (alpha < 1.0) {
// do nothing
} else if (s->flags & SURF_DRAWALPHA) { // TODO: trans 33/66 values
alpha = 0.5f;
} else if (s->flags & SURF_DRAWTURB) {
alpha = R_GetTurbAlpha(s);
}
s->alpha = alpha;
return (alpha < 1.0);
}
/*
===============
R_GetTurbAlpha
===============
*/
float R_GetTurbAlpha (msurface_t *s)
{
float alpha = 1.0f;
if (!r_lockalpha.value) // override water alpha for certain surface types
{
if (s->flags & SURF_DRAWLAVA)
alpha = CLAMP(0.0, r_lavaalpha.value, 1.0);
else if (s->flags & SURF_DRAWSLIME)
alpha = CLAMP(0.0, r_slimealpha.value, 1.0);
else if (s->flags & SURF_DRAWTELEPORT)
alpha = CLAMP(0.0, r_teleportalpha.value, 1.0);
}
if (s->flags & SURF_DRAWWATER)
{
if (globalwateralpha > 0)
alpha = globalwateralpha;
else
alpha = CLAMP(0.0, r_wateralpha.value, 1.0);
}
return alpha;
}
/*
===============
R_GetAlphaDist
===============
*/
vec_t R_GetAlphaDist (vec3_t origin)
{
vec3_t result;
VectorSubtract (origin, r_origin, result);
// no need to sqrt these as all we're concerned about is relative distances
return DotProduct (result, result);
// return VectorLength (result);
}
/*
===============
R_AddToAlpha
===============
*/
void R_AddToAlpha (int type, vec_t dist, void *data, entity_t *entity, float alpha)
{
if (gl_alphalist_num == MAX_ALPHA_ITEMS)
return;
gl_alphalist[gl_alphalist_num].type = type;
gl_alphalist[gl_alphalist_num].dist = dist;
gl_alphalist[gl_alphalist_num].data = data;
gl_alphalist[gl_alphalist_num].entity = entity;
gl_alphalist[gl_alphalist_num].alpha = alpha;
gl_alphalist_num++;
}
static inline int alphadistcompare (const void *arg1, const void *arg2)
{
// Sort in descending dist order, i.e. back to front
// Sorted in reverse order
const gl_alphalist_t *a1 = (gl_alphalist_t *)arg1;
const gl_alphalist_t *a2 = (gl_alphalist_t *)arg2;
// back to front ordering
// this is more correct as it will order surfs properly if less than 1 unit separated
if (a2->dist > a1->dist)
return 1;
else if (a2->dist < a1->dist)
return -1;
else
return 0;
}
/*
=============
R_DrawAlpha
=============
*/
void R_DrawAlpha (void)
{
int i;
gl_alphalist_t a;
if (gl_alphalist_num == 0)
return;
//
// sort
//
if (gl_alphalist_num == 1)
{
// do nothing, no need to sort
}
else if (gl_alphalist_num == 2)
{
// exchange if necessary
if (gl_alphalist[1].dist > gl_alphalist[0].dist)
{
gl_alphalist_t temp = gl_alphalist[0];
gl_alphalist[0] = gl_alphalist[1];
gl_alphalist[1] = temp;
}
}
else
{
// sort fully
qsort((void *)gl_alphalist, gl_alphalist_num, sizeof(gl_alphalist_t), alphadistcompare);
}
//
// draw
//
for (i=0 ; i<gl_alphalist_num ; i++)
{
if ((i + 1) % 100 == 0)
S_ExtraUpdateTime (); // don't let sound get messed up if going slow
a = gl_alphalist[i];
switch (a.type)
{
case ALPHA_SURFACE:
{
if (a.entity)
{
glPushMatrix ();
glLoadMatrixf (a.entity->matrix); // load entity matrix
R_DrawSequentialPoly ((msurface_t *)a.data, a.alpha, a.entity->frame); // draw entity surfaces
glPopMatrix ();
}
else
{
R_DrawSequentialPoly ((msurface_t *)a.data, a.alpha, 0); // draw world surfaces
}
}
break;
case ALPHA_ALIAS:
R_DrawAliasModel ((entity_t *)a.data);
break;
case ALPHA_SPRITE:
R_DrawSpriteModel ((entity_t *)a.data);
break;
case ALPHA_PARTICLE:
R_DrawParticle ((particle_t *)a.data);
break;
case ALPHA_DLIGHTS:
R_RenderDlight ((dlight_t *)a.data);
break;
default:
break;
}
}
gl_alphalist_num = 0;
}
// ----------------------------------------------------
/*
===============
R_AddDynamicLights
===============
*/
void R_AddDynamicLights (msurface_t *surf)
{
int lnum;
int sd, td;
float dist, rad, minlight;
vec3_t impact, local;
int s, t;
int i;
int smax, tmax;
mtexinfo_t *tex;
// lit support via lordhavoc
float r, g, b, brightness;
unsigned *bl;
float scale;
if (!r_dynamic.value) // EER1
return;
scale = CLAMP(1.0, r_dynamicscale.value, 32.0) * 256.0f;
smax = (surf->extents[0]>>4)+1;
tmax = (surf->extents[1]>>4)+1;
tex = surf->texinfo;
for (lnum=0 ; lnum<MAX_DLIGHTS ; lnum++)
{
if ( !(surf->dlightbits[lnum >> 5] & (1U << (lnum & 31))) )
continue; // not lit by this light
rad = cl_dlights[lnum].radius;
dist = DotProduct (cl_dlights[lnum].origin, surf->plane->normal) - surf->plane->dist;
rad -= fabs(dist);
minlight = cl_dlights[lnum].minlight;
if (rad < minlight)
continue;
minlight = rad - minlight;
for (i=0 ; i<3 ; i++)
{
impact[i] = cl_dlights[lnum].origin[i] - surf->plane->normal[i]*dist;
}
local[0] = DotProduct (impact, tex->vecs[0]) + tex->vecs[0][3];
local[1] = DotProduct (impact, tex->vecs[1]) + tex->vecs[1][3];
local[0] -= surf->texturemins[0];
local[1] -= surf->texturemins[1];
// lit support via lordhavoc
bl = blocklights;
r = cl_dlights[lnum].color[0] * scale;//256.0f;
g = cl_dlights[lnum].color[1] * scale;//256.0f;
b = cl_dlights[lnum].color[2] * scale;//256.0f;
for (t = 0 ; t<tmax ; t++)
{
td = local[1] - t*16;
if (td < 0)
td = -td;
for (s=0 ; s<smax ; s++)
{
sd = local[0] - s*16;
if (sd < 0)
sd = -sd;
if (sd > td)
dist = sd + (td>>1);
else
dist = td + (sd>>1);
if (dist < minlight)
// lit support via lordhavoc
{
brightness = rad - dist;
bl[0] += (int) (brightness * r);
bl[1] += (int) (brightness * g);
bl[2] += (int) (brightness * b);
}
bl += 3;
}
}
}
}
/*
===============
R_BuildLightMap
combine and scale multiple lightmaps into the 8.8 format in blocklights
===============
*/
void R_BuildLightMap (msurface_t *surf, byte *dest, int stride)
{
int smax, tmax;
int t;
int i, j, size;
byte *lightmap;
unsigned scale;
int maps;
unsigned *bl;
int shift;
surf->cached_dlight = (surf->dlightframe == r_framecount);
smax = (surf->extents[0]>>4)+1;
tmax = (surf->extents[1]>>4)+1;
size = smax*tmax;
lightmap = surf->samples;
if (size > BLOCKL_SIZE)
Sys_Error ("R_BuildLightMap: too large blocklight size (%d, max = %d)", size, BLOCKL_SIZE);
if (!r_fullbright.value && cl.worldmodel->lightdata)
{
// clear to no light
memset (&blocklights[0], 0, size * 3 * sizeof (unsigned int)); // lit support via lordhavoc
// add all the lightmaps
if (lightmap)
for (maps = 0 ; maps < MAXLIGHTMAPS && surf->styles[maps] != 255 ; maps++)
{
scale = d_lightstyle[surf->styles[maps]];
surf->cached_light[maps] = scale; // 8.8 fraction
// lit support via lordhavoc
bl = blocklights;
for (i=0 ; i<size ; i++)
{
*bl++ += *lightmap++ * scale;
*bl++ += *lightmap++ * scale;
*bl++ += *lightmap++ * scale;
}
}
// add all the dynamic lights
if (surf->dlightframe == r_framecount)
R_AddDynamicLights (surf);
}
else
{
// set to full bright if no light data
memset (&blocklights[0], 255, size * 3 * sizeof (unsigned int)); // lit support via lordhavoc
}
// bound, invert, and shift
stride -= smax * 4;
shift = 7 + d_overbright;
bl = blocklights;
for (i=0 ; i<tmax ; i++, dest += stride)
{
for (j=0 ; j<smax ; j++)
{
t = *bl++ >> shift;if (t > 255) t = 255;dest[0] = t;
t = *bl++ >> shift;if (t > 255) t = 255;dest[1] = t;
t = *bl++ >> shift;if (t > 255) t = 255;dest[2] = t;
dest[3] = 255;
dest += 4;
}
}
}
/*
===============
R_TextureAnimation
returns the proper texture for a given time and base texture
===============
*/
texture_t *R_TextureAnimation (texture_t *base, int frame)
{
int relative;
int count;
if (frame)
if (base->alternate_anims)
base = base->alternate_anims;
if (!base->anim_total)
return base;
relative = (int)(cl.time*10) % base->anim_total;
count = 0;
while (base->anim_min > relative || base->anim_max <= relative)
{
base = base->anim_next;
if (!base)
Host_Error ("R_TextureAnimation: broken cycle");
if (++count > 100)
Host_Error ("R_TextureAnimation: infinite cycle");
}
return base;
}
/*
=============================================================
BRUSH MODELS
=============================================================
*/
/*
================
R_RenderDynamicLightmaps
================
*/
void R_RenderDynamicLightmaps (msurface_t *s)
{
byte *base;
int maps;
glRect_t *rect;
int smax, tmax;
if (s->flags & SURF_DRAWTILED) // not a lightmapped surface
return;
// add to lightmap chain
s->polys->chain = lightmap_polys[s->lightmaptexture];
lightmap_polys[s->lightmaptexture] = s->polys;
// check for lightmap modification
for (maps = 0 ; maps < MAXLIGHTMAPS && s->styles[maps] != 255 ; maps++)
if (d_lightstyle[s->styles[maps]] != s->cached_light[maps])
goto dynamic;
if (s->dlightframe == r_framecount // dynamic this frame
|| s->cached_dlight) // dynamic previously
{
dynamic:
if (!r_fullbright.value) // EER1
{
lightmap_modified[s->lightmaptexture] = true;
rect = &lightmap_rectchange[s->lightmaptexture];
if (s->light_t < rect->t)
{
if (rect->h)
rect->h += rect->t - s->light_t;
rect->t = s->light_t;
}
if (s->light_s < rect->l)
{
if (rect->w)
rect->w += rect->l - s->light_s;
rect->l = s->light_s;
}
smax = (s->extents[0]>>4)+1;
tmax = (s->extents[1]>>4)+1;
if ((rect->w + rect->l) < (s->light_s + smax))
rect->w = (s->light_s-rect->l)+smax;
if ((rect->h + rect->t) < (s->light_t + tmax))
rect->h = (s->light_t-rect->t)+tmax;
base = lightmaps + s->lightmaptexture*lightmap_bytes*BLOCK_WIDTH*BLOCK_HEIGHT;
base += s->light_t * BLOCK_WIDTH * lightmap_bytes + s->light_s * lightmap_bytes;
R_BuildLightMap (s, base, BLOCK_WIDTH*lightmap_bytes);
}
}
}
/*
===============
R_UploadLightmaps
uploads the modified lightmap to opengl if necessary
assumes lightmap texture is already bound
===============
*/
void R_UploadLightmaps (void)
{
int lmap;
glRect_t *rect;
for (lmap = 0; lmap < MAX_LIGHTMAPS; lmap++)
{
if (!lightmap_modified[lmap])
continue;
GL_Bind (lightmap_textures[lmap]);
lightmap_modified[lmap] = false;
rect = &lightmap_rectchange[lmap];
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, rect->t, BLOCK_WIDTH, rect->h, GL_RGBA,
GL_UNSIGNED_BYTE, lightmaps+(lmap* BLOCK_HEIGHT + rect->t) *BLOCK_WIDTH*lightmap_bytes);
rect->l = BLOCK_WIDTH;
rect->t = BLOCK_HEIGHT;
rect->h = 0;
rect->w = 0;
// r_speeds
rs_c_dynamic_lightmaps++;
}
}
/*
================
R_DrawGLPoly34
================
*/
void R_DrawGLPoly34 (glpoly_t *p)
{
float *v;
int i;
glBegin (GL_POLYGON);
v = p->verts[0];
for (i=0 ; i<p->numverts ; i++, v+= VERTEXSIZE)
{
glTexCoord2f (v[3], v[4]);
glVertex3fv (v);
}
glEnd ();
}
/*
================
R_DrawGLPoly56
================
*/
void R_DrawGLPoly56 (glpoly_t *p)
{
float *v;
int i;
glBegin (GL_POLYGON);
v = p->verts[0];
for (i=0 ; i<p->numverts ; i++, v+= VERTEXSIZE)
{
glTexCoord2f (v[5], v[6]);
glVertex3fv (v);
}
glEnd ();
}
/*
================
R_DrawSequentialPoly
Systems that have fast state and texture changes can
just do everything as it passes with no need to sort
================
*/
void R_DrawSequentialPoly (msurface_t *s, float alpha, int frame)
{
glpoly_t *p;
texture_t *t;
float *v;
float lfog = 0; // keep compiler happy
int i;
//
// sky poly
//
if (s->flags & SURF_DRAWSKY)
return; // skip it, already handled
p = s->polys;
//
// water poly
//
if (s->flags & SURF_DRAWTURB)
{
if (alpha < 1.0)
{
glDepthMask(GL_FALSE);
glEnable(GL_BLEND);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glColor4f(1, 1, 1, alpha);
}
GL_Bind (s->texinfo->texture->warpimage);
if ( !(s->flags & (SURF_DRAWLAVA | SURF_DRAWSLIME)) )
{
R_DrawGLPoly34 (p);
rs_c_brush_passes++; // r_speeds
}
else
{
R_FogDisableGFog ();
R_DrawGLPoly34 (p);
rs_c_brush_passes++; // r_speeds
R_FogEnableGFog ();
if (s->flags & SURF_DRAWLAVA)
lfog = CLAMP(0.0, r_lavafog.value, 1.0);
else if (s->flags & SURF_DRAWSLIME)
lfog = CLAMP(0.0, r_slimefog.value, 1.0);
if (R_FogGetDensity() > 0 && lfog > 0)
{
float *c = R_FogGetColor();
glEnable (GL_BLEND);
glColor4f (c[0],c[1],c[2], lfog);
R_DrawGLPoly34 (p);
rs_c_brush_passes++; // r_speeds
glColor3f (1, 1, 1);
glDisable (GL_BLEND);
}
}
if (alpha < 1.0)
{
glDepthMask(GL_TRUE);
glDisable(GL_BLEND);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glColor3f(1, 1, 1);
}
return;
}
t = R_TextureAnimation (s->texinfo->texture, frame);
//
// missing texture
//
if (s->flags & SURF_NOTEXTURE)
{
if (alpha < 1.0)
{
glDepthMask(GL_FALSE);
glEnable(GL_BLEND);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glColor4f(1, 1, 1, alpha);
}
GL_Bind (t->gltexture);
R_DrawGLPoly34 (p);
rs_c_brush_passes++; // r_speeds
if (alpha < 1.0)
{
glDepthMask(GL_TRUE);
glDisable(GL_BLEND);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glColor3f(1, 1, 1);
}
return;
}
//
// lightmapped poly
//
if ( !(s->flags & SURF_DRAWTILED) )
{
if (alpha < 1.0)
{
glDepthMask (GL_FALSE);
glEnable (GL_BLEND);
glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glColor4f (1, 1, 1, alpha);
}
else
glColor3f (1, 1, 1);
if (s->flags & SURF_DRAWFENCE)
glEnable (GL_ALPHA_TEST); // Flip on alpha test
if (gl_mtexable && gl_texture_env_combine) // case 1: texture and lightmap in one pass, overbright using texture combiners
{
// Binds world to texture env 0
GL_DisableMultitexture (); // selects TEXTURE0
GL_Bind (t->gltexture);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
// Binds lightmap to texture env 1
GL_EnableMultitexture (); // selects TEXTURE1
GL_Bind (lightmap_textures[s->lightmaptexture]);
R_RenderDynamicLightmaps (s);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_EXT);
glTexEnvf(GL_TEXTURE_ENV, GL_COMBINE_RGB_EXT, GL_MODULATE);
glTexEnvf(GL_TEXTURE_ENV, GL_SOURCE0_RGB_EXT, GL_PREVIOUS_EXT);
glTexEnvf(GL_TEXTURE_ENV, GL_SOURCE1_RGB_EXT, GL_TEXTURE);
glTexEnvf(GL_TEXTURE_ENV, GL_RGB_SCALE_EXT, d_overbrightscale);
glBegin(GL_POLYGON);
v = p->verts[0];
for (i=0 ; i<p->numverts ; i++, v+= VERTEXSIZE)
{
qglMultiTexCoord2f (GL_TEXTURE0_ARB, v[3], v[4]);
qglMultiTexCoord2f (GL_TEXTURE1_ARB, v[5], v[6]);
glVertex3fv (v);
}
glEnd ();
rs_c_brush_passes++; // r_speeds
glTexEnvf(GL_TEXTURE_ENV, GL_RGB_SCALE_EXT, 1.0f);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
GL_DisableMultitexture (); // selects TEXTURE0
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);//FX
}
else if (alpha < 1.0 || (s->flags & SURF_DRAWFENCE)) // case 2: can't do multipass if brush has alpha, so just draw the texture
{
GL_Bind (t->gltexture);
R_DrawGLPoly34 (p);
rs_c_brush_passes++; // r_speeds
}
else // case 3: texture in one pass, lightmap in second pass using 2x modulation blend func, fog in third pass
{
// first pass -- texture with no fog
R_FogDisableGFog ();
GL_Bind (t->gltexture);
R_DrawGLPoly34 (p);
rs_c_brush_passes++; // r_speeds
R_FogEnableGFog ();
// second pass -- lightmap with black fog, modulate blended
GL_Bind (lightmap_textures[s->lightmaptexture]);
R_RenderDynamicLightmaps (s);
glDepthMask (GL_FALSE); // don't bother writing Z
glEnable (GL_BLEND);
glBlendFunc (GL_DST_COLOR, GL_SRC_COLOR); // 2x modulate
R_FogStartAdditive ();
R_DrawGLPoly56 (p);
rs_c_brush_passes++; // r_speeds
R_FogStopAdditive ();
// third pass -- black geo with normal fog, additive blended
if (R_FogGetDensity() > 0)
{
glBlendFunc(GL_ONE, GL_ONE); //add
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glColor3f(0,0,0);
R_DrawGLPoly34 (p);
rs_c_brush_passes++; // r_speeds
glColor3f(1,1,1);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
}
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glDisable (GL_BLEND);
glDepthMask (GL_TRUE); // back to normal Z buffering
}
if (alpha < 1.0)
{
glDepthMask(GL_TRUE);
glDisable(GL_BLEND);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glColor3f(1, 1, 1);
}
if (s->flags & SURF_DRAWFENCE)
glDisable (GL_ALPHA_TEST); // Flip alpha test back off
if (t->fullbright)
{
GL_Bind (t->fullbright);
glDepthMask (GL_FALSE); // don't bother writing Z
glEnable (GL_BLEND);
glBlendFunc (GL_ONE, GL_ONE);
glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glColor3f (alpha, alpha, alpha);
R_FogStartAdditive ();
R_DrawGLPoly34 (p);
rs_c_brush_passes++; // r_speeds
R_FogStopAdditive ();
glColor3f (1, 1, 1);
glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glDisable (GL_BLEND);
glDepthMask (GL_TRUE); // back to normal Z buffering
}
}
}
/*
=================
R_DrawBrushModel
=================
*/
void R_DrawBrushModel (entity_t *e)
{
int k, i;
msurface_t *psurf;
float dot;
mplane_t *pplane;
model_t *clmodel;
qboolean rotated = false;
float alpha;
qboolean hasalpha = false;
if (R_CullModelForEntity(e))
return;
clmodel = e->model;
alpha = ENTALPHA_DECODE(e->alpha);
VectorSubtract (r_refdef.vieworg, e->origin, modelorg);
if (e->angles[0] || e->angles[1] || e->angles[2])
{
vec3_t temp;
vec3_t forward, right, up;
rotated = true;
VectorCopy (modelorg, temp);
AngleVectors (e->angles, forward, right, up);
modelorg[0] = DotProduct (temp, forward);
modelorg[1] = -DotProduct (temp, right);
modelorg[2] = DotProduct (temp, up);
}
e->rotated = rotated;
// calculate dynamic lighting for bmodel if it's not an instanced model
if (clmodel->firstmodelsurface != 0) // EER1
{
for (k=0 ; k<MAX_DLIGHTS ; k++)
{
if ((cl_dlights[k].die < cl.time) ||
(!cl_dlights[k].radius))
continue;
R_MarkLights (&cl_dlights[k], k, clmodel->nodes + clmodel->hulls[0].firstclipnode);
}
}
glPushMatrix ();
glTranslatef (e->origin[0], e->origin[1], e->origin[2]);
if (rotated)
{
glRotatef (e->angles[1], 0, 0, 1);
glRotatef (e->angles[0], 0, 1, 0);
glRotatef (e->angles[2], 1, 0, 0);
}
//
// set all chains to null
//
R_ClearTextureChains(clmodel, chain_model);
//
// draw it
//
psurf = &clmodel->surfaces[clmodel->firstmodelsurface];
for (i=0 ; i<clmodel->nummodelsurfaces ; i++, psurf++)
{
// find which side of the node we are on
pplane = psurf->plane;
dot = DotProduct (modelorg, pplane->normal) - pplane->dist;
// draw the polygon
if (((psurf->flags & SURF_PLANEBACK) && (dot < -BACKFACE_EPSILON)) ||
(!(psurf->flags & SURF_PLANEBACK) && (dot > BACKFACE_EPSILON)))
{
if (psurf->texinfo->texture->warpimage)
psurf->texinfo->texture->update_warp = true; // FIXME: one frame too late!
hasalpha = R_SetAlphaSurface(psurf, alpha);
R_ChainSurface (psurf, chain_model);
rs_c_brush_polys++; // r_speeds
}
}
if (hasalpha)
glGetFloatv (GL_MODELVIEW_MATRIX, e->matrix); // save entity matrix
R_DrawTextureChains (clmodel, e, chain_model);
glPopMatrix ();
}
/*
=============================================================
WORLD MODEL
=============================================================
*/
int recursivecount;
/*
================
R_RecursiveWorldNode
this now only builds a surface chains and leafs
================
*/
void R_RecursiveWorldNode (mnode_t *node)
{
int c, side;
mplane_t *plane;
msurface_t *surf, **mark;
mleaf_t *pleaf;
float dot;
// float alpha = 1.0;
restart:
if (node->contents == CONTENTS_SOLID)
return; // solid
if (node->visframe != r_visframecount)
return;
if (R_CullBox (node->mins, node->maxs))
return;
// if a leaf node, draw stuff
if (node->contents < 0)
{
pleaf = (mleaf_t *)node;
mark = pleaf->firstmarksurface;
c = pleaf->nummarksurfaces;
if (c)
{
do
{
(*mark)->visframe = r_framecount;
mark++;
} while (--c);
}
// deal with model fragments in this leaf