-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgl_rsurf.pas
1674 lines (1427 loc) · 39.3 KB
/
gl_rsurf.pas
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
// ------------------------------------------------------------------------------
// DelphiQuake, Copyright (C) 2005-2011 by Jim Valavanis
// E-Mail: jimmyvalavanis@yahoo.gr
//
// 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.
//
// ------------------------------------------------------------------------------
{$I dquake.inc}
{$Z4}
unit gl_rsurf;
// r_surf.c: surface-related refresh code
interface
uses
q_delphi,
gl_planes,
gl_model_h;
var
skytexturenum: integer;
procedure R_AddDynamicLights(surf: Pmsurface_t);
procedure R_BuildLightMap(surf: Pmsurface_t; dest: PByteArray; stride: integer);
function R_TextureAnimation(base: Ptexture_t): Ptexture_t;
procedure GL_DisableMultitexture;
procedure GL_EnableMultitexture;
procedure R_DrawSequentialPoly(s: Pmsurface_t);
procedure DrawGLWaterPoly(p: Pglpoly_t);
procedure DrawGLWaterPolyLightmap(p: Pglpoly_t);
procedure DrawGLPoly(p: Pglpoly_t);
procedure R_BlendLightmaps;
procedure R_RenderBrushPoly(fa: Pmsurface_t);
procedure R_RenderDynamicLightmaps(fa: Pmsurface_t);
procedure R_MirrorChain(s: Pmsurface_t);
procedure R_DrawWaterSurfaces;
procedure DrawTextureChains;
procedure R_DrawBrushModel(e: Pentity_t);
procedure R_RecursiveWorldNode(node: Pmnode_t);
procedure R_DrawWorld;
procedure R_MarkLeaves;
function AllocBlock(w, h: integer; var x, y: integer): integer;
procedure BuildSurfaceDisplayList(fa: Pmsurface_t);
procedure GL_CreateSurfaceLightmap(surf: Pmsurface_t);
procedure GL_BuildLightmaps;
implementation
uses
q_vector,
quakedef,
OpenGL12,
mathlib,
client,
gl_rmain,
bspconst,
gl_texture,
sys_win,
gl_defs,
gl_vidnt,
gl_warp,
gl_rlight,
cl_main_h,
gl_rmain_h,
host_h,
gl_refrag,
gl_model,
gl_sky,
zone,
common;
const
GL_RGBA4 = 0;
var
lightmap_bytes: integer; // 1, 2, or 4
lightmap_textures: integer;
blocklights: array[0..256 * 256 - 1] of unsigned;
const
BLOCK_WIDTH = 128;
BLOCK_HEIGHT = 128;
const
MAX_LIGHTMAPS = 256;
type
glRect_t = record
l, t, w, h: byte; // JVAL was unsigned char
end;
PglRect_t = ^glRect_t;
var
lightmap_polys: array[0..MAX_LIGHTMAPS - 1] of Pglpoly_t;
lightmap_modified: array[0..MAX_LIGHTMAPS - 1] of qboolean;
lightmap_rectchange: array[0..MAX_LIGHTMAPS - 1] of glRect_t;
allocated: array[0..MAX_LIGHTMAPS - 1, 0..BLOCK_WIDTH - 1] of integer;
// the lightmap texture data needs to be kept in
// main memory so texsubimage can update properly
lightmaps: array[0..4 * MAX_LIGHTMAPS * BLOCK_WIDTH * BLOCK_HEIGHT - 1] of byte;
// For gl_texsort 0
skychain: Pmsurface_t = nil;
waterchain: Pmsurface_t = nil;
(*
===============
R_AddDynamicLights
===============
*)
procedure R_AddDynamicLights(surf: Pmsurface_t);
var
lnum: integer;
sd, td: integer;
dist, rad, minlight: single;
impact, local: TVector3f;
s, t: integer;
i: integer;
smax, tmax: integer;
tex: Pmtexinfo_t;
begin
smax := (surf.extents[0] div 16) + 1;
tmax := (surf.extents[1] div 16) + 1;
tex := surf.texinfo;
for lnum := 0 to MAX_DLIGHTS - 1 do
begin
if (surf.dlightbits and (1 shl lnum)) = 0 then
continue; // not lit by this light
rad := cl_dlights[lnum].radius;
dist := VectorDotProduct(@cl_dlights[lnum].origin, @surf.plane.normal) - surf.plane.dist;
rad := rad - abs(dist);
minlight := cl_dlights[lnum].minlight;
if rad < minlight then
continue;
minlight := rad - minlight;
for i := 0 to 2 do
begin
impact[i] := cl_dlights[lnum].origin[i] - surf.plane.normal[i] * dist;
end;
// JVAL mayby loop for code below?
local[0] := VectorDotProduct(@impact, @tex.vecs[0]) + tex.vecs[0][3];
local[1] := VectorDotProduct(@impact, @tex.vecs[1]) + tex.vecs[1][3];
local[0] := local[0] - surf.texturemins[0];
local[1] := local[1] - surf.texturemins[1];
for t := 0 to tmax - 1 do
begin
td := intval(local[1] - t * 16);
if td < 0 then
td := -td;
for s := 0 to smax - 1 do
begin
sd := intval(local[0] - s * 16);
if sd < 0 then
sd := -sd;
if sd > td then
dist := sd + (td / 2) // JVAL mayby not div, just /
else
dist := td + (sd / 2);
if dist < minlight then
blocklights[t * smax + s] := blocklights[t * smax + s] + uintval((rad - dist) * 256);
end;
end;
end;
end;
(*
===============
R_BuildLightMap
Combine and scale multiple lightmaps into the 8.8 format in blocklights
===============
*)
procedure R_BuildLightMap(surf: Pmsurface_t; dest: PByteArray; stride: integer);
label
store;
var
smax, tmax: integer;
t: integer;
i, j, size: integer;
lightmap: PByteArray;
scale: unsigned;
maps: integer;
bl: Punsigned;
r, g, b: Integer;
begin
surf.cached_dlight := (surf.dlightframe = r_framecount);
smax := (surf.extents[0] div 16) + 1;
tmax := (surf.extents[1] div 16) + 1;
size := smax * tmax;
lightmap := surf.samples;
if gl_lightmap_format = GL_RGB then size := size * 3;
// set to full bright if no light data
if (r_fullbright.value <> 0) or (cl.worldmodel.lightdata = nil) then
begin
for i := 0 to size - 1 do
blocklights[i] := 255 * 256;
goto store;
end;
// clear to no light
for i := 0 to size - 1 do
blocklights[i] := 0;
// add all the lightmaps
if lightmap <> nil then
begin
maps := 0;
while (maps < MAXLIGHTMAPS) and (surf.styles[maps] <> 255) do
begin
scale := d_lightstylevalue[surf.styles[maps]];
surf.cached_light[maps] := scale; // 8.8 fraction
for i := 0 to size - 1 do
blocklights[i] := blocklights[i] + lightmap[i] * scale;
lightmap := PByteArray(@lightmap[size]); // skip to next lightmap
inc(maps);
end;
end;
// add all the dynamic lights
if surf.dlightframe = r_framecount then
R_AddDynamicLights(surf);
// bound, invert, and shift
store:
case gl_lightmap_format of
GL_RGBA:
begin
stride := stride - (smax shl 2);
bl := @blocklights[0];
for i := 0 to tmax - 1 do
begin
for j := 0 to smax - 1 do
begin
t := bl^;
inc(bl);
t := (t shr 7);
if t > 255 then
t := 255;
dest[3] := 255 - t;
dest := PByteArray(@dest[4]);
end;
dest := PByteArray(@dest[stride]);
end;
end;
GL_RGB:
begin
begin
stride := stride - (smax * 3);
bl := @blocklights[0];
for i := 0 to tmax - 1 do
begin
for j := 0 to smax - 1 do
begin
r := bl^ shr 7; inc(bl);
g := bl^ shr 7; inc(bl);
b := bl^ shr 7; inc(bl);
if r > 255 then r := 255;
if g > 255 then g := 255;
if b > 255 then b := 255;
dest[0] := 255 - r;
dest[1] := 255 - g;
dest[2] := 255 - b;
dest := PByteArray(@dest[3]);
end;
dest := PByteArray(@dest[stride]);
end;
end;
end;
GL_ALPHA,
GL_LUMINANCE,
GL_INTENSITY:
begin
bl := @blocklights[0];
for i := 0 to tmax - 1 do
begin
for j := 0 to smax - 1 do
begin
t := bl^ shr 7; inc(bl);
if t > 255 then t := 255;
dest[j] := 255 - t;
end;
dest := PByteArray(@dest[stride]);
end;
end;
else
Sys_Error('Bad lightmap format');
end;
end;
(*
===============
R_TextureAnimation
Returns the proper texture for a given time and base texture
===============
*)
function R_TextureAnimation(base: Ptexture_t): Ptexture_t;
var
reletive: integer;
count: integer;
begin
if currententity.frame <> 0 then
begin
if base.alternate_anims <> nil then
base := base.alternate_anims;
end;
if base.anim_total = 0 then
begin
result := base;
exit;
end;
reletive := intval(cl.time * 10) mod base.anim_total;
count := 0;
while (base.anim_min > reletive) or (base.anim_max <= reletive) do
begin
base := base.anim_next;
if base = nil then Sys_Error('R_TextureAnimation: broken cycle');
if count > 100 then Sys_Error('R_TextureAnimation: infinite cycle');
inc(count);
end;
result := base;
end;
(*
=============================================================
BRUSH MODELS
=============================================================
*)
procedure GL_DisableMultitexture;
begin
if mtexenabled then
begin
glDisable(GL_TEXTURE_2D);
GL_SelectTexture(TEXTURE0_SGIS);
mtexenabled := false;
end;
end;
procedure GL_EnableMultitexture;
begin
if gl_mtexable then
begin
GL_SelectTexture(TEXTURE1_SGIS);
glEnable(GL_TEXTURE_2D);
mtexenabled := true;
end;
end;
(*
================
R_DrawSequentialPoly
Systems that have fast state and texture changes can
just do everything as it passes with no need to sort
================
*)
procedure R_DrawSequentialPoly(s: Pmsurface_t);
var
p: Pglpoly_t;
v: PfloatArray;
i: integer;
t: Ptexture_t;
nv: TVector3f;
theRect: PglRect_t;
begin
//
// normal lightmaped poly
//
if s.flags and (SURF_DRAWSKY or SURF_DRAWTURB or SURF_UNDERWATER) = 0 then
begin
R_RenderDynamicLightmaps(s);
if gl_mtexable then
begin
p := s.polys;
t := R_TextureAnimation(s.texinfo.texture);
// Binds world to texture env 0
GL_SelectTexture(TEXTURE0_SGIS);
GL_Bind(t.gl_texturenum);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
// Binds lightmap to texenv 1
GL_EnableMultitexture; // Same as SelectTexture (TEXTURE1)
GL_Bind(lightmap_textures + s.lightmaptexturenum);
i := s.lightmaptexturenum;
if lightmap_modified[i] then
begin
lightmap_modified[i] := false;
theRect := @lightmap_rectchange[i];
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, theRect.t,
BLOCK_WIDTH, theRect.h, gl_lightmap_format, GL_UNSIGNED_BYTE,
@lightmaps[(i * BLOCK_HEIGHT + theRect.t) * BLOCK_WIDTH * lightmap_bytes]);
theRect.l := BLOCK_WIDTH;
theRect.t := BLOCK_HEIGHT;
theRect.h := 0;
theRect.w := 0;
end;
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_BLEND);
glBegin(GL_POLYGON);
v := @p.verts[0]; // JVAL Check this
for i := 0 to p.numverts - 1 do
begin
qglMTexCoord2fSGIS(TEXTURE0_SGIS, v[3], v[4]);
qglMTexCoord2fSGIS(TEXTURE1_SGIS, v[5], v[6]);
glVertex3fv(@v[0]);
v := @v[VERTEXSIZE]; // JVAL check this
end;
glEnd;
exit;
end
else
begin
p := s.polys;
t := R_TextureAnimation(s.texinfo.texture);
GL_Bind(t.gl_texturenum);
glBegin(GL_POLYGON);
v := @p.verts[0];
for i := 0 to p.numverts - 1 do
begin
glTexCoord2f(v[3], v[4]);
glVertex3fv(@v[0]);
v := @v[VERTEXSIZE]; // JVAL check this
end;
glEnd;
GL_Bind(lightmap_textures + s.lightmaptexturenum);
glEnable(GL_BLEND);
glBegin(GL_POLYGON);
v := @p.verts[0]; // JVAL Check this
for i := 0 to p.numverts - 1 do
begin
glTexCoord2f(v[5], v[6]);
glVertex3fv(@v[0]);
v := @v[VERTEXSIZE]; // JVAL check this
end;
glEnd;
glDisable(GL_BLEND);
end;
exit;
end;
//
// subdivided water surface warp
//
if s.flags and SURF_DRAWTURB <> 0 then
begin
GL_DisableMultitexture;
GL_Bind(s.texinfo.texture.gl_texturenum);
EmitWaterPolys(s);
exit;
end;
//
// subdivided sky warp
//
if s.flags and SURF_DRAWSKY <> 0 then
begin
if gl_drawskydome.value = 0 then
begin
GL_DisableMultitexture;
GL_Bind(solidskytexture);
speedscale := realtime * 8;
speedscale := speedscale - (intval(speedscale) and (not 127));
EmitSkyPolys(s);
glEnable(GL_BLEND);
GL_Bind(alphaskytexture);
speedscale := realtime * 16;
speedscale := speedscale - (intval(speedscale) and (not 127));
EmitSkyPolys(s);
glDisable(GL_BLEND);
end
else
dodrawsky := true;
exit;
end;
//
// underwater warped with lightmap
//
R_RenderDynamicLightmaps(s);
if gl_mtexable then
begin
p := s.polys; // JVAL check this
t := R_TextureAnimation(s.texinfo.texture);
GL_SelectTexture(TEXTURE0_SGIS);
GL_Bind(t.gl_texturenum);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
GL_EnableMultitexture;
GL_Bind(lightmap_textures + s.lightmaptexturenum);
i := s.lightmaptexturenum;
if lightmap_modified[i] then
begin
lightmap_modified[i] := false;
theRect := @lightmap_rectchange[i];
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, theRect.t,
BLOCK_WIDTH, theRect.h, gl_lightmap_format, GL_UNSIGNED_BYTE,
@lightmaps[(i * BLOCK_HEIGHT + theRect.t) * BLOCK_WIDTH * lightmap_bytes]);
theRect.l := BLOCK_WIDTH;
theRect.t := BLOCK_HEIGHT;
theRect.h := 0;
theRect.w := 0;
end;
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_BLEND);
glBegin(GL_TRIANGLE_FAN);
v := @p.verts[0]; // JVAL Check this
for i := 0 to p.numverts - 1 do
begin
qglMTexCoord2fSGIS(TEXTURE0_SGIS, v[3], v[4]);
qglMTexCoord2fSGIS(TEXTURE1_SGIS, v[5], v[6]);
// JVAL mayby optimize sin() ??
nv[0] := v[0] + 8 * sin(v[1] * 0.05 + realtime) * sin(v[2] * 0.05 + realtime);
nv[1] := v[1] + 8 * sin(v[0] * 0.05 + realtime) * sin(v[2] * 0.05 + realtime);
nv[2] := v[2];
glVertex3fv(@nv);
v := @v[VERTEXSIZE]; // JVAL check this
end;
glEnd;
end
else
begin
p := s.polys; // JVAL check this
t := R_TextureAnimation(s.texinfo.texture);
GL_Bind(t.gl_texturenum);
DrawGLWaterPoly(p);
GL_Bind(lightmap_textures + s.lightmaptexturenum);
glEnable(GL_BLEND);
DrawGLWaterPolyLightmap(p);
glDisable(GL_BLEND);
end;
end;
(*
================
DrawGLWaterPoly
Warp the vertex coordinates
================
*)
procedure DrawGLWaterPoly(p: Pglpoly_t);
var
i: integer;
v: PFloatArray;
// nv: TVector3f;
begin
GL_DisableMultitexture;
glBegin(GL_TRIANGLE_FAN);
v := @p.verts[0];
for i := 0 to p.numverts - 1 do
begin
glTexCoord2f(v[3], v[4]);
// JVAL mayby optimize this -> eg tmp := 8 * sin(v[2] * 0.05 + realtime)
{ nv[0] := v[0] + 8 * sin(v[1] * 0.05 + realtime) * sin(v[2] * 0.05 + realtime);
nv[1] := v[1] + 8 * sin(v[0] * 0.05 + realtime) * sin(v[2] * 0.05 + realtime);
nv[2] := v[2];}
// glVertex3fv(@nv);
glVertex3fv(@v[0]);
v := @v[VERTEXSIZE];
end;
glEnd;
end;
procedure DrawGLWaterPolyLightmap(p: Pglpoly_t);
var
i: integer;
v: PFloatArray;
// nv: TVector3f;
begin
GL_DisableMultitexture;
glBegin(GL_TRIANGLE_FAN);
v := @p.verts[0];
for i := 0 to p.numverts - 1 do
begin
glTexCoord2f(v[5], v[6]);
// JVAL mayby optimize this -> tmp := 8 * sin(v[2]*0.05+realtime) ??
{ nv[0] := v[0] + 8 * sin(v[1] * 0.05 + realtime) * sin(v[2] * 0.05 + realtime);
nv[1] := v[1] + 8 * sin(v[0] * 0.05 + realtime) * sin(v[2] * 0.05 + realtime);
nv[2] := v[2];}
// glVertex3fv(@nv);
glVertex3fv(@v[0]);
v := @v[VERTEXSIZE];
end;
glEnd;
end;
(*
================
DrawGLPoly
================
*)
procedure DrawGLPoly(p: Pglpoly_t);
var
i: integer;
v: PFloatArray;
begin
glBegin(GL_POLYGON);
v := @p.verts[0];
for i := 0 to p.numverts - 1 do
begin
glTexCoord2f(v[3], v[4]);
glVertex3fv(@v[0]);
v := @v[VERTEXSIZE];
end;
glEnd;
end;
(*
================
R_BlendLightmaps
================
*)
procedure R_BlendLightmaps;
var
i, j: integer;
p: Pglpoly_t;
v: PFloatArray;
theRect: PglRect_t;
begin
if r_fullbright.value <> 0 then
exit;
if gl_texsort.value = 0 then
exit;
glDepthMask(false); // don't bother writing Z
if gl_lightmap_format = GL_RGBA then
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
else if gl_lightmap_format = GL_RGB then
glBlendFunc(GL_ZERO, GL_ONE_MINUS_SRC_COLOR)
else if gl_lightmap_format = GL_LUMINANCE then
glBlendFunc(GL_ZERO, GL_ONE_MINUS_SRC_COLOR)
else if gl_lightmap_format = GL_INTENSITY then
begin
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glColor4f(0, 0, 0, 1);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
end;
if r_lightmap.value = 0 then
glEnable(GL_BLEND);
for i := 0 to MAX_LIGHTMAPS - 1 do
begin
p := lightmap_polys[i];
if p = nil then
continue;
GL_Bind(lightmap_textures + i);
if lightmap_modified[i] then
begin
lightmap_modified[i] := false;
theRect := @lightmap_rectchange[i];
// glTexImage2D (GL_TEXTURE_2D, 0, lightmap_bytes
// , BLOCK_WIDTH, BLOCK_HEIGHT, 0,
// gl_lightmap_format, GL_UNSIGNED_BYTE, lightmaps+i*BLOCK_WIDTH*BLOCK_HEIGHT*lightmap_bytes);
// glTexImage2D (GL_TEXTURE_2D, 0, lightmap_bytes
// , BLOCK_WIDTH, theRect->h, 0,
// gl_lightmap_format, GL_UNSIGNED_BYTE, lightmaps+(i*BLOCK_HEIGHT+theRect->t)*BLOCK_WIDTH*lightmap_bytes);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, theRect.t,
BLOCK_WIDTH, theRect.h, gl_lightmap_format, GL_UNSIGNED_BYTE,
@lightmaps[(i * BLOCK_HEIGHT + theRect.t) * BLOCK_WIDTH * lightmap_bytes]);
theRect.l := BLOCK_WIDTH;
theRect.t := BLOCK_HEIGHT;
theRect.h := 0;
theRect.w := 0;
end;
while p <> nil do
begin
if p.flags and SURF_UNDERWATER <> 0 then
DrawGLWaterPolyLightmap(p)
else
begin
glBegin(GL_POLYGON);
v := @p.verts[0];
for j := 0 to p.numverts - 1 do
begin
glTexCoord2f(v[5], v[6]);
glVertex3fv(@v[0]);
v := @v[VERTEXSIZE];
end;
glEnd;
end;
p := p.chain;
end;
end;
glDisable(GL_BLEND);
if gl_lightmap_format = GL_LUMINANCE then glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
else if gl_lightmap_format = GL_INTENSITY then
begin
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glColor4f(1, 1, 1, 1);
end;
glDepthMask(true); // back to normal Z buffering
end;
(*
================
R_RenderBrushPoly
================
*)
procedure R_RenderBrushPoly(fa: Pmsurface_t);
label
dynamic1;
var
t: Ptexture_t;
base: PByte;
maps: integer;
theRect: PglRect_t;
smax, tmax: integer;
begin
inc(c_brush_polys);
if fa.flags and SURF_DRAWSKY <> 0 then
begin // warp texture, no lightmaps
EmitBothSkyLayers(fa);
exit;
end;
t := R_TextureAnimation(fa.texinfo.texture);
GL_Bind(t.gl_texturenum);
if fa.flags and SURF_DRAWTURB <> 0 then
begin // warp texture, no lightmaps
EmitWaterPolys(fa);
exit;
end;
if fa.flags and SURF_UNDERWATER <> 0 then
DrawGLWaterPoly(fa.polys)
else
DrawGLPoly(fa.polys);
// add the poly to the proper lightmap chain
fa.polys.chain := lightmap_polys[fa.lightmaptexturenum];
lightmap_polys[fa.lightmaptexturenum] := fa.polys;
// check for lightmap modification
maps := 0;
while (maps < MAXLIGHTMAPS) and (fa.styles[maps] <> 255) do
begin
if d_lightstylevalue[fa.styles[maps]] <> fa.cached_light[maps] then
goto dynamic1;
inc(maps);
end;
if (fa.dlightframe = r_framecount) or // dynamic this frame
fa.cached_dlight then // dynamic previously
begin
dynamic1:
if r_dynamic.value <> 0 then
begin
lightmap_modified[fa.lightmaptexturenum] := true;
theRect := @lightmap_rectchange[fa.lightmaptexturenum];
if fa.light_t < theRect.t then
begin
if theRect.h <> 0 then
theRect.h := theRect.h + theRect.t - fa.light_t;
theRect.t := fa.light_t;
end;
if fa.light_s < theRect.l then
begin
if theRect.w <> 0 then
theRect.w := theRect.w + theRect.l - fa.light_s;
theRect.l := fa.light_s;
end;
smax := (fa.extents[0] div 16) + 1;
tmax := (fa.extents[1] div 16) + 1;
if (theRect.w + theRect.l) < (fa.light_s + smax) then
theRect.w := (fa.light_s - theRect.l) + smax;
if (theRect.h + theRect.t) < (fa.light_t + tmax) then
theRect.h := (fa.light_t - theRect.t) + tmax;
base := @lightmaps[fa.lightmaptexturenum * lightmap_bytes * BLOCK_WIDTH * BLOCK_HEIGHT];
inc(base, fa.light_t * BLOCK_WIDTH * lightmap_bytes + fa.light_s * lightmap_bytes);
R_BuildLightMap(fa, PByteArray(base), BLOCK_WIDTH * lightmap_bytes);
end;
end;
end;
(*
================
R_RenderDynamicLightmaps
Multitexture
================
*)
procedure R_RenderDynamicLightmaps(fa: Pmsurface_t);
label
dynamic1;
var
base: PByte;
maps: integer;
theRect: PglRect_t;
smax, tmax: integer;
begin
inc(c_brush_polys);
if fa.flags and (SURF_DRAWSKY or SURF_DRAWTURB) <> 0 then
exit;
fa.polys.chain := lightmap_polys[fa.lightmaptexturenum];
lightmap_polys[fa.lightmaptexturenum] := fa.polys;
// check for lightmap modification
maps := 0;
while (maps < MAXLIGHTMAPS) and (fa.styles[maps] <> 255) do
begin
if d_lightstylevalue[fa.styles[maps]] <> fa.cached_light[maps] then
goto dynamic1;
inc(maps);
end;
if (fa.dlightframe = r_framecount) or // dynamic this frame
fa.cached_dlight then // dynamic previously
begin
dynamic1:
if boolval(r_dynamic.value) then
begin
lightmap_modified[fa.lightmaptexturenum] := true;
theRect := @lightmap_rectchange[fa.lightmaptexturenum];
if fa.light_t < theRect.t then
begin
if boolval(theRect.h) then
theRect.h := theRect.h + theRect.t - fa.light_t;
theRect.t := fa.light_t;
end;
if fa.light_s < theRect.l then
begin
if boolval(theRect.w) then
theRect.w := theRect.w + theRect.l - fa.light_s;
theRect.l := fa.light_s;
end;
smax := (fa.extents[0] div 16) + 1;
tmax := (fa.extents[1] div 16) + 1;
if (theRect.w + theRect.l) < (fa.light_s + smax) then
theRect.w := (fa.light_s - theRect.l) + smax;
if (theRect.h + theRect.t) < (fa.light_t + tmax) then
theRect.h := (fa.light_t - theRect.t) + tmax;
base := @lightmaps[fa.lightmaptexturenum * lightmap_bytes * BLOCK_WIDTH * BLOCK_HEIGHT];
inc(base, fa.light_t * BLOCK_WIDTH * lightmap_bytes + fa.light_s * lightmap_bytes);
R_BuildLightMap(fa, PByteArray(base), BLOCK_WIDTH * lightmap_bytes);
end;
end;
end;
(*
================
R_MirrorChain
================
*)
procedure R_MirrorChain(s: Pmsurface_t);
begin
if mirror then
exit;
mirror := true;
mirror_plane := s.plane;
end;
(*
================
R_DrawWaterSurfaces
================
*)
procedure R_DrawWaterSurfaces;
var
i: integer;
s: Pmsurface_t;
t: Ptexture_t;
begin
if (r_wateralpha.value = 1.0) and (gl_texsort.value <> 0) then
exit;
//
// go back to the world matrix
//
glLoadMatrixf(@r_world_matrix);
if r_wateralpha.value < 1.0 then
begin
glEnable(GL_BLEND);
glColor4f(1, 1, 1, r_wateralpha.value);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
end;
if gl_texsort.value = 0 then
begin
if waterchain = nil then
exit;
s := waterchain;
while s <> nil do
begin
GL_Bind(s.texinfo.texture.gl_texturenum);
EmitWaterPolys(s);
s := s.texturechain;
end;
waterchain := nil;
end
else
begin
for i := 0 to cl.worldmodel.numtextures - 1 do
begin
t := cl.worldmodel.textures[i];
if t = nil then
continue;
s := t.texturechain;
if s = nil then
continue;
if s.flags and SURF_DRAWTURB = 0 then
continue;
// set modulate mode explicitly
GL_Bind(t.gl_texturenum);
while s <> nil do
begin
EmitWaterPolys(s);
s := s.texturechain;
end;
t.texturechain := nil;
end;
end;