-
Notifications
You must be signed in to change notification settings - Fork 209
Expand file tree
/
Copy pathiphone_render.c
More file actions
1751 lines (1502 loc) · 53.6 KB
/
iphone_render.c
File metadata and controls
1751 lines (1502 loc) · 53.6 KB
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) 2009-2011 id Software LLC, a ZeniMax Media company.
Copyright (C) 2009 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.
*/
#include <stdio.h>
#include <string.h>
#include <math.h>
#include "SDL_opengl.h"
#include "doomtype.h"
#include "w_wad.h"
#include "m_argv.h"
#include "d_event.h"
#include "v_video.h"
#include "doomstat.h"
#include "r_bsp.h"
#include "r_main.h"
#include "r_draw.h"
#include "r_sky.h"
#include "r_plane.h"
#include "r_data.h"
#include "r_things.h"
#include "r_fps.h"
#include "p_maputl.h"
#include "m_bbox.h"
#include "lprintf.h"
#include "gl_intern.h"
#include "gl_struct.h"
// If the Doom levels had been built with realistic visibility
// taken into account for the sky areas, we could just draw the
// sky first and then the walls, but that gives artifacts where
// you see some sectors floating in the sky now. This causes
// the walls to draw extended top and bottom sections for skies.
#define SKYWALLS
#define MINZ (FRACUNIT*4)
#define BASEYCENTER 100
#define MINZ_FLOAT 4
typedef struct {
GLTexture *tex;
side_t *side;
int flag; // GLDWF_TOP, GLDWF_M1S, etc
} sortLine_t;
#define MAX_SORT_LINES 4096
sortLine_t sortLines[MAX_SORT_LINES];
int numSortLines;
typedef struct {
GLTexture *texture;
sector_t *sector;
boolean ceiling;
} sortSectorPlane_t;
#define MAX_SECTOR_PLANES 1024
sortSectorPlane_t sectorPlanes[MAX_SECTOR_PLANES];
int numSectorPlanes;
typedef struct {
GLTexture *tex;
} sortSprite_t;
// Cleared to 0 at frame start.
// Individual columns will be set to 1 as occluding segments are processed.
// An occluding segment is either a one-sided line, a line that has a back
// sector with equal floor and ceiling heights, a line with a back ceiling
// height lower than the fron floor height, or a line with a back floor height
// higher than the front ceiling height.
// Entire nodes are culled when their bounds does not include a 0 column.
// Individual line segments are culled when their span does not include 0 columns.
// Sprites could be checked against it, but it may not be worth it.
char occlusion[MAX_SCREENWIDTH+2]; // +2 for guard columns to avoid clamping
// when the iphone is upside down, the occlusion segments are reversed
boolean reversedLandscape;
// this matrix is exactly what GL uses, but there will still
// be floating point differences between the GPU and CPU
float glMVPmatrix[16];
// if any sector textures are the sky texture, we will draw the sky and
// ignore those sector geometries
boolean skyIsVisible;
// these should really be initialized based on viewwidth somewhere...
float halfWidthFloat = 240.0f;
// used during debugging to isolate incorrect culling
int failCount;
// Some of the two sided line segments in the original game don't have a valid
// texture, so stick something there instead of leaving a gaping hole in the world.
GLTexture *defaultTexture;
// just for the sky texture setup
float yaw;
// counters
int c_occludedSprites;
int c_sectors;
int c_subsectors;
// test options
int testClear = 0;
int testNewRenderer = 1;
int showRenderTime;
int blendAll;
void BuildIndexedTriangles();
void BuildSideSegs();
void IR_MergeSectors( int fromSector, int intoSector ) {
// E3M8 (and possibly others somewhere) has a bad sector
// classification with two stray lines in sector 2 that
// should be a part of sector 1. This makes both of the
// sectors "broken" and unable to be properly tesselated.
assert( (unsigned)fromSector < numsectors );
assert( (unsigned)intoSector < numsectors );
sector_t *fromSectorPtr = §ors[fromSector];
sector_t *intoSectorPtr = §ors[intoSector];
int moveLines = 0;
for ( int i = 0 ; i < numlines ; i++ ) {
if ( lines[i].frontsector == fromSectorPtr ) {
moveLines++;
} else if ( lines[i].backsector == fromSectorPtr ) {
moveLines++;
}
}
// add these lines to intoSector
// Unfortunately, the sector->lines list is not allocated per-sector, but
// is a single block for the entire level, so we can't realloc it. I'm
// going to just let the new table leak.
line_t **newLines = Z_Malloc( ( intoSectorPtr->linecount + moveLines ) * sizeof( *intoSectorPtr->lines ),
PU_LEVEL,0);
memcpy( newLines, intoSectorPtr->lines, intoSectorPtr->linecount * sizeof( *newLines ) );
intoSectorPtr->lines = newLines;
for ( int i = 0 ; i < numlines ; i++ ) {
if ( lines[i].frontsector == fromSectorPtr ) {
intoSectorPtr->lines[intoSectorPtr->linecount++] = &lines[i];
lines[i].frontsector = intoSectorPtr;
} else if ( lines[i].backsector == fromSectorPtr ) {
intoSectorPtr->lines[intoSectorPtr->linecount++] = &lines[i];
lines[i].backsector = intoSectorPtr;
}
}
// change all the segs
for ( int i = 0 ; i < numsegs ; i++ ) {
if ( segs[i].frontsector == fromSectorPtr ) {
segs[i].frontsector = intoSectorPtr;
}
if ( segs[i].backsector == fromSectorPtr ) {
segs[i].backsector = intoSectorPtr;
}
}
// change all the sides to point to the new one
for ( int i = 0 ; i < numsides ; i++ ) {
if ( sides[i].sector == fromSectorPtr ) {
sides[i].sector = intoSectorPtr;
}
}
// change all the subsectors to point to the new one
for ( int i = 0 ; i < numsubsectors ; i++ ) {
if ( subsectors[i].sector == fromSectorPtr ) {
subsectors[i].sector = intoSectorPtr;
}
}
// make fromSector vestigial so it doesn't get tesselated
fromSectorPtr->linecount = 0;
}
void IR_InitLevel() {
BuildIndexedTriangles(); // convert the loops into indexed triangles
BuildSideSegs(); // create a seg_t for each side_t so we can draw the
// unclipped versions that fit perfectly with the sectors
// find something else used in the level for a default texture
for ( int i = 0 ; i < numsides ; i++ ) {
if ( sides[i].toptexture ) {
defaultTexture=gld_RegisterTexture(sides[i].toptexture, true, false);
if ( defaultTexture ) {
break;
}
}
}
assert( defaultTexture );
}
float lightDistance = 10.0f; // in prBoom MAP_SCALE units, increasing this makes things get dimmer faster
#define MAX_LIGHT_DROP 96
float lightingVector[3]; // transform and scale [ x y 1 ] to get color units to subtract
static int FadedLighting( float x, float y, int sectorLightLevel ) {
// Ramp down the lightover lightDistance world units.
// Triangles that extend across behind the view origin and past
// the lightDistance clamping boundary will not have completely linear fading,
// but nobody should notice.
// A proportional drop in lighting sounds like a better idea, but
// this linear drop seems to look nicer. It's not like Doom's
// lighting is realistic in any case...
int idist = x * lightingVector[0] + y * lightingVector[1] + lightingVector[2];
if ( idist < 0 ) {
idist = 0;
} else if ( idist > MAX_LIGHT_DROP ) {
idist = MAX_LIGHT_DROP;
}
sectorLightLevel -= idist;
if ( sectorLightLevel < 0 ) {
sectorLightLevel = 0;
}
if ( sectorLightLevel > 255 ) {
sectorLightLevel = 255;
}
return sectorLightLevel | (sectorLightLevel<<8) | (sectorLightLevel<<16) | (255<<24);
}
//
// IR_ProjectSprite
// Generates a vissprite for a thing if it might be visible.
//
static void IR_ProjectSprite (mobj_t* thing, int lightlevel)
{
fixed_t gzt; // killough 3/27/98
fixed_t tx;
fixed_t xscale;
int x1;
int x2;
spritedef_t *sprdef;
spriteframe_t *sprframe;
int lump;
boolean flip;
// transform the origin point
fixed_t tr_x, tr_y;
fixed_t fx, fy, fz;
fixed_t gxt, gyt;
fixed_t tz;
int width;
fx = thing->x;
fy = thing->y;
fz = thing->z;
tr_x = fx - viewx;
tr_y = fy - viewy;
gxt = FixedMul(tr_x,viewcos);
gyt = -FixedMul(tr_y,viewsin);
tz = gxt-gyt;
// thing is behind view plane?
if (tz < MINZ)
return;
xscale = FixedDiv(projection, tz);
gxt = -FixedMul(tr_x,viewsin);
gyt = FixedMul(tr_y,viewcos);
tx = -(gyt+gxt);
// too far off the side?
if (D_abs(tx)>(tz<<2))
return;
// decide which patch to use for sprite relative to player
#ifdef RANGECHECK
if ((unsigned) thing->sprite >= (unsigned)numsprites)
I_Error ("R_ProjectSprite: Invalid sprite number %i", thing->sprite);
#endif
sprdef = &sprites[thing->sprite];
#ifdef RANGECHECK
if ((thing->frame&FF_FRAMEMASK) >= sprdef->numframes)
I_Error ("R_ProjectSprite: Invalid sprite frame %i : %i", thing->sprite,
thing->frame);
#endif
if (!sprdef->spriteframes)
I_Error ("R_ProjectSprite: Missing spriteframes %i : %i", thing->sprite,
thing->frame);
sprframe = &sprdef->spriteframes[thing->frame & FF_FRAMEMASK];
if (sprframe->rotate)
{
// choose a different rotation based on player view
// JDC: this could be better...
angle_t ang = R_PointToAngle(fx, fy);
unsigned rot = (ang-thing->angle+(unsigned)(ANG45/2)*9)>>29;
lump = sprframe->lump[rot];
flip = (boolean) sprframe->flip[rot];
}
else
{
// use single rotation for all views
lump = sprframe->lump[0];
flip = (boolean) sprframe->flip[0];
}
{
const rpatch_t* patch = R_CachePatchNum(lump+firstspritelump);
/* calculate edges of the shape
* cph 2003/08/1 - fraggle points out that this offset must be flipped
* if the sprite is flipped; e.g. FreeDoom imp is messed up by this. */
if (flip) {
tx -= (patch->width - patch->leftoffset) << FRACBITS;
} else {
tx -= patch->leftoffset << FRACBITS;
}
x1 = (centerxfrac + FixedMul(tx,xscale)) >> FRACBITS;
tx += patch->width<<FRACBITS;
x2 = ((centerxfrac + FixedMul (tx,xscale) ) >> FRACBITS) - 1;
gzt = fz + (patch->topoffset << FRACBITS);
width = patch->width;
// JDC: we don't care if they never get freed,
// so don't bother changing the zone tag status each time
//R_UnlockPatchNum(lump+firstspritelump);
}
// off the side?
if (x1 > viewwidth || x2 < 0)
return;
// killough 4/9/98: clip things which are out of view due to height
// e6y: fix of hanging decoration disappearing in Batman Doom MAP02
// centeryfrac -> viewheightfrac
if (fz > viewz + FixedDiv(viewheightfrac, xscale) ||
gzt < viewz - FixedDiv(viewheightfrac-viewheight, xscale))
return;
// JDC: clip to the occlusio buffer
int testLow = x1 < 0 ? 0 : x1;
int testHigh = x2 >= viewwidth ? viewwidth - 1 : x2;
if ( reversedLandscape ) {
testLow = viewwidth-1-testLow;
testHigh = viewwidth-1-testHigh;
}
if ( !memchr( occlusion+testLow, 0, testHigh - testLow ) ) {
c_occludedSprites++;
return;
}
// ------------ gld_AddSprite ----------
mobj_t *pSpr= thing;
GLSprite sprite;
float voff,hoff;
sprite.scale= FixedDiv(projectiony, tz);
if (pSpr->frame & FF_FULLBRIGHT)
sprite.light = 255;
else
sprite.light = pSpr->subsector->sector->lightlevel+(extralight<<5);
sprite.cm=CR_LIMIT+(int)((pSpr->flags & MF_TRANSLATION) >> (MF_TRANSSHIFT));
sprite.gltexture=gld_RegisterPatch(lump+firstspritelump,sprite.cm);
if (!sprite.gltexture)
return;
sprite.shadow = (pSpr->flags & MF_SHADOW) != 0;
sprite.trans = (pSpr->flags & MF_TRANSLUCENT) != 0;
if (movement_smooth)
{
sprite.x = (float)(-pSpr->PrevX + FixedMul (tic_vars.frac, -pSpr->x - (-pSpr->PrevX)))/MAP_SCALE;
sprite.y = (float)(pSpr->PrevZ + FixedMul (tic_vars.frac, pSpr->z - pSpr->PrevZ))/MAP_SCALE;
sprite.z = (float)(pSpr->PrevY + FixedMul (tic_vars.frac, pSpr->y - pSpr->PrevY))/MAP_SCALE;
}
else
{
sprite.x=-(float)pSpr->x/MAP_SCALE;
sprite.y= (float)pSpr->z/MAP_SCALE;
sprite.z= (float)pSpr->y/MAP_SCALE;
}
sprite.vt=0.0f;
sprite.vb=(float)sprite.gltexture->height/(float)sprite.gltexture->tex_height;
if (flip)
{
sprite.ul=0.0f;
sprite.ur=(float)sprite.gltexture->width/(float)sprite.gltexture->tex_width;
}
else
{
sprite.ul=(float)sprite.gltexture->width/(float)sprite.gltexture->tex_width;
sprite.ur=0.0f;
}
hoff=(float)sprite.gltexture->leftoffset/(float)(MAP_COEFF);
voff=(float)sprite.gltexture->topoffset/(float)(MAP_COEFF);
sprite.x1=hoff-((float)sprite.gltexture->realtexwidth/(float)(MAP_COEFF));
sprite.x2=hoff;
sprite.y1=voff;
sprite.y2=voff-((float)sprite.gltexture->realtexheight/(float)(MAP_COEFF));
// JDC: don't let sprites poke below the ground level.
// Software rendering Doom didn't use depth buffering,
// so sprites always got drawn on top of the flat they
// were on, but in GL they tend to get a couple pixel
// rows clipped off.
if ( sprite.y2 < 0 ) {
sprite.y1 -= sprite.y2;
sprite.y2 = 0;
}
if (gld_drawinfo.num_sprites>=gld_drawinfo.max_sprites)
{
gld_drawinfo.max_sprites+=128;
gld_drawinfo.sprites=Z_Realloc(gld_drawinfo.sprites,gld_drawinfo.max_sprites*sizeof(GLSprite),PU_LEVEL,0);
}
gld_drawinfo.sprites[gld_drawinfo.num_sprites++]=sprite;
}
// JDC: removed the 0.001f epsilons that were presumably added
// to try to hide T-junction cracks, but now that we are drawing
// source lines instead of clipped segs, it is a non-problem.
#define LINE seg->linedef
#define CALC_Y_VALUES(w, lineheight, floor_height, ceiling_height)\
(w).ytop=((float)(ceiling_height)/(float)MAP_SCALE);\
(w).ybottom=((float)(floor_height)/(float)MAP_SCALE);\
lineheight=((float)fabs(((ceiling_height)/(float)FRACUNIT)-((floor_height)/(float)FRACUNIT)))
#define OU(w,seg) (((float)((seg)->sidedef->textureoffset+(seg)->offset)/(float)FRACUNIT)/(float)(w).gltexture->buffer_width)
#define OV(w,seg) (((float)((seg)->sidedef->rowoffset)/(float)FRACUNIT)/(float)(w).gltexture->buffer_height)
#define OV_PEG(w,seg,v_offset) (OV((w),(seg))-(((float)(v_offset)/(float)FRACUNIT)/(float)(w).gltexture->buffer_height))
#define CALC_TEX_VALUES_TOP(w, seg, peg, linelength, lineheight)\
(w).flag=GLDWF_TOP;\
(w).ul=OU((w),(seg))+(0.0f);\
(w).ur=OU((w),(seg))+((linelength)/(float)(w).gltexture->buffer_width);\
(peg)?\
(\
(w).vb=OV((w),(seg))+((float)(w).gltexture->height/(float)(w).gltexture->tex_height),\
(w).vt=((w).vb-((float)(lineheight)/(float)(w).gltexture->buffer_height))\
):(\
(w).vt=OV((w),(seg))+(0.0f),\
(w).vb=OV((w),(seg))+((float)(lineheight)/(float)(w).gltexture->buffer_height)\
)
#define CALC_TEX_VALUES_MIDDLE1S(w, seg, peg, linelength, lineheight)\
(w).flag=GLDWF_M1S;\
(w).ul=OU((w),(seg))+(0.0f);\
(w).ur=OU((w),(seg))+((linelength)/(float)(w).gltexture->buffer_width);\
(peg)?\
(\
(w).vb=OV((w),(seg))+((float)(w).gltexture->height/(float)(w).gltexture->tex_height),\
(w).vt=((w).vb-((float)(lineheight)/(float)(w).gltexture->buffer_height))\
):(\
(w).vt=OV((w),(seg))+(0.0f),\
(w).vb=OV((w),(seg))+((float)(lineheight)/(float)(w).gltexture->buffer_height)\
)
#define CALC_TEX_VALUES_MIDDLE2S(w, seg, peg, linelength, lineheight)\
(w).flag=GLDWF_M2S;\
(w).ul=OU((w),(seg))+(0.0f);\
(w).ur=OU((w),(seg))+((linelength)/(float)(w).gltexture->buffer_width);\
(peg)?\
(\
(w).vb=((float)(w).gltexture->height/(float)(w).gltexture->tex_height),\
(w).vt=((w).vb-((float)(lineheight)/(float)(w).gltexture->buffer_height))\
):(\
(w).vt=(0.0f),\
(w).vb=((float)(lineheight)/(float)(w).gltexture->buffer_height)\
)
#define CALC_TEX_VALUES_BOTTOM(w, seg, peg, linelength, lineheight, v_offset)\
(w).flag=GLDWF_BOT;\
(w).ul=OU((w),(seg))+(0.0f);\
(w).ur=OU((w),(seg))+((linelength)/(float)(w).gltexture->realtexwidth);\
(peg)?\
(\
(w).vb=OV_PEG((w),(seg),(v_offset))+((float)(w).gltexture->height/(float)(w).gltexture->tex_height),\
(w).vt=((w).vb-((float)(lineheight)/(float)(w).gltexture->buffer_height))\
):(\
(w).vt=OV((w),(seg))+(0.0f),\
(w).vb=OV((w),(seg))+((float)(lineheight)/(float)(w).gltexture->buffer_height)\
)
// e6y
// Sky textures with a zero index should be forced
// See third episode of requiem.wad
#define SKYTEXTURE_PRBOOM(sky1,sky2)\
if ((sky1) & PL_SKYFLAT)\
{\
const line_t *l = &lines[sky1 & ~PL_SKYFLAT];\
const side_t *s = *l->sidenum + sides;\
wall.gltexture=gld_RegisterTexture(texturetranslation[s->toptexture], false, texturetranslation[s->toptexture]==skytexture);\
wall.skyyaw=-2.0f*((-(float)((viewangle+s->textureoffset)>>ANGLETOFINESHIFT)*360.0f/FINEANGLES)/90.0f);\
wall.skyymid = 200.0f/319.5f*(((float)s->rowoffset/(float)FRACUNIT - 28.0f)/100.0f);\
wall.flag = l->special==272 ? GLDWF_SKY : GLDWF_SKYFLIP;\
}\
else\
if ((sky2) & PL_SKYFLAT)\
{\
const line_t *l = &lines[sky2 & ~PL_SKYFLAT];\
const side_t *s = *l->sidenum + sides;\
wall.gltexture=gld_RegisterTexture(texturetranslation[s->toptexture], false, texturetranslation[s->toptexture]==skytexture);\
wall.skyyaw=-2.0f*((-(float)((viewangle+s->textureoffset)>>ANGLETOFINESHIFT)*360.0f/FINEANGLES)/90.0f);\
wall.skyymid = 200.0f/319.5f*(((float)s->rowoffset/(float)FRACUNIT - 28.0f)/100.0f);\
wall.flag = l->special==272 ? GLDWF_SKY : GLDWF_SKYFLIP;\
}\
else\
{\
wall.gltexture=gld_RegisterTexture(skytexture, false, true);\
wall.skyyaw=-2.0f*((yaw+90.0f)/90.0f);\
wall.skyymid = 200.0f/319.5f*((100.0f)/100.0f);\
wall.flag = GLDWF_SKY;\
};
#define SKYTEXTURE(sky1,sky2)\
wall.gltexture=NULL;\
wall.flag = GLDWF_SKY;
#define ADDWALL(wall)\
{\
if (gld_drawinfo.num_walls>=gld_drawinfo.max_walls)\
{\
gld_drawinfo.max_walls+=128;\
gld_drawinfo.walls=Z_Realloc(gld_drawinfo.walls,gld_drawinfo.max_walls*sizeof(GLWall),PU_LEVEL,0);\
}\
gld_drawinfo.walls[gld_drawinfo.num_walls++]=*wall;\
};
extern GLSeg *gl_segs;
extern byte rendermarker;
extern byte *segrendered;
void IR_AddWall(seg_t *seg)
{
GLWall wall;
GLTexture *temptex;
sector_t *frontsector;
sector_t *backsector;
float lineheight;
int rellight = 0;
wall.glseg=NULL;
wall.side = seg->sidedef;
frontsector = seg->frontsector;
// JDC: improve this lighting tweak
rellight = seg->linedef->dx==0? +8 : seg->linedef->dy==0 ? -8 : 0;
int light = frontsector->lightlevel+rellight+(extralight<<5);
wall.light = MAX(MIN((light),255),0);
wall.alpha=1.0f;
wall.gltexture=NULL;
if (!seg->backsector) /* onesided */
{
#ifdef SKYWALLS
if (frontsector->ceilingpic==skyflatnum)
{
wall.ytop=255.0f;
wall.ybottom=(float)frontsector->ceilingheight/MAP_SCALE;
SKYTEXTURE(frontsector->sky,frontsector->sky);
ADDWALL(&wall);
}
if (frontsector->floorpic==skyflatnum)
{
wall.ytop=(float)frontsector->floorheight/MAP_SCALE;
wall.ybottom=-255.0f;
SKYTEXTURE(frontsector->sky,frontsector->sky);
ADDWALL(&wall);
}
#endif
temptex=gld_RegisterTexture(texturetranslation[seg->sidedef->midtexture], true, false);
if (temptex)
{
wall.gltexture=temptex;
CALC_Y_VALUES(wall, lineheight, frontsector->floorheight, frontsector->ceilingheight);
CALC_TEX_VALUES_MIDDLE1S(
wall, seg, (LINE->flags & ML_DONTPEGBOTTOM)>0,
seg->length, lineheight
);
ADDWALL(&wall);
}
}
else /* twosided */
{
int floor_height,ceiling_height;
backsector=seg->backsector;
/* toptexture */
ceiling_height=frontsector->ceilingheight;
floor_height=backsector->ceilingheight;
#ifdef SKYWALLS
if (frontsector->ceilingpic==skyflatnum)
{
wall.ytop=255.0f;
if (
// e6y
// Fix for HOM in the starting area on Memento Mori map29 and on map30.
// old code: (backsector->ceilingheight==backsector->floorheight) &&
(backsector->ceilingheight==backsector->floorheight||(backsector->ceilingheight<=frontsector->floorheight)) &&
(backsector->ceilingpic==skyflatnum)
)
{
wall.ybottom=(float)backsector->floorheight/MAP_SCALE;
SKYTEXTURE(frontsector->sky,backsector->sky);
ADDWALL(&wall);
}
else
{
if ( (texturetranslation[seg->sidedef->toptexture]!=NO_TEXTURE) )
{
// e6y
// It corrects some problem with sky, but I do not remember which one
// old code: wall.ybottom=(float)frontsector->ceilingheight/MAP_SCALE;
wall.ybottom=(float)MAX(frontsector->ceilingheight,backsector->ceilingheight)/MAP_SCALE;
SKYTEXTURE(frontsector->sky,backsector->sky);
ADDWALL(&wall);
}
else
if ( (backsector->ceilingheight <= frontsector->floorheight) ||
(backsector->ceilingpic != skyflatnum) )
{
wall.ybottom=(float)backsector->ceilingheight/MAP_SCALE;
SKYTEXTURE(frontsector->sky,backsector->sky);
ADDWALL(&wall);
}
}
}
#endif
if (floor_height<ceiling_height)
{
if (!((frontsector->ceilingpic==skyflatnum) && (backsector->ceilingpic==skyflatnum)))
{
temptex=gld_RegisterTexture(texturetranslation[seg->sidedef->toptexture], true, false);
if ( !temptex ) {
temptex = defaultTexture; // it seems some line segments have bad data...
}
wall.gltexture=temptex;
CALC_Y_VALUES(wall, lineheight, floor_height, ceiling_height);
CALC_TEX_VALUES_TOP(
wall, seg, (LINE->flags & (ML_DONTPEGBOTTOM | ML_DONTPEGTOP))==0,
seg->length, lineheight
);
ADDWALL(&wall);
}
}
/* midtexture */
//e6y
if (comp[comp_maskedanim])
temptex=gld_RegisterTexture(seg->sidedef->midtexture, true, false);
else
// e6y
// Animated middle textures with a zero index should be forced
// See spacelab.wad (http://www.doomworld.com/idgames/index.php?id=6826)
temptex=gld_RegisterTexture(texturetranslation[seg->sidedef->midtexture], true, true);
if (temptex && seg->sidedef->midtexture != NO_TEXTURE)
{
wall.gltexture=temptex;
if ( (LINE->flags & ML_DONTPEGBOTTOM) >0)
{
if (seg->backsector->ceilingheight<=seg->frontsector->floorheight)
goto bottomtexture;
floor_height=MAX(seg->frontsector->floorheight,seg->backsector->floorheight)+(seg->sidedef->rowoffset);
ceiling_height=floor_height+(wall.gltexture->realtexheight<<FRACBITS);
}
else
{
if (seg->backsector->ceilingheight<=seg->frontsector->floorheight)
goto bottomtexture;
ceiling_height=MIN(seg->frontsector->ceilingheight,seg->backsector->ceilingheight)+(seg->sidedef->rowoffset);
floor_height=ceiling_height-(wall.gltexture->realtexheight<<FRACBITS);
}
// e6y
// The fix for wrong middle texture drawing
// if it exceeds the boundaries of its floor and ceiling
/*CALC_Y_VALUES(wall, lineheight, floor_height, ceiling_height);
CALC_TEX_VALUES_MIDDLE2S(
wall, seg, (LINE->flags & ML_DONTPEGBOTTOM)>0,
segs[seg->iSegID].length, lineheight
);*/
{
int floormax, ceilingmin, linelen;
float mip;
mip = (float)wall.gltexture->realtexheight/(float)wall.gltexture->buffer_height;
// if ( (texturetranslation[seg->sidedef->bottomtexture]!=R_TextureNumForName("-")) )
if (seg->sidedef->bottomtexture)
floormax=MAX(seg->frontsector->floorheight,seg->backsector->floorheight);
else
floormax=floor_height;
if (seg->sidedef->toptexture)
ceilingmin=MIN(seg->frontsector->ceilingheight,seg->backsector->ceilingheight);
else
ceilingmin=ceiling_height;
linelen=abs(ceiling_height-floor_height);
wall.ytop=((float)MIN(ceilingmin, ceiling_height)/(float)MAP_SCALE);
wall.ybottom=((float)MAX(floormax, floor_height)/(float)MAP_SCALE);
wall.flag=GLDWF_M2S;
wall.ul=OU((wall),(seg))+(0.0f);
wall.ur=OU(wall,(seg))+((seg->length)/(float)wall.gltexture->buffer_width);
if (floormax<=floor_height)
wall.vb=mip*1.0f;
else
wall.vb=mip*((float)(ceiling_height - floormax))/linelen;
if (ceilingmin>=ceiling_height)
wall.vt=0.0f;
else
wall.vt=mip*((float)(ceiling_height - ceilingmin))/linelen;
}
wall.alpha=1.0f;
ADDWALL(&wall);
}
bottomtexture:
/* bottomtexture */
ceiling_height=backsector->floorheight;
floor_height=frontsector->floorheight;
#ifdef SKYWALLS
if (frontsector->floorpic==skyflatnum)
{
wall.ybottom=-255.0f;
if (
(backsector->ceilingheight==backsector->floorheight) &&
(backsector->floorpic==skyflatnum)
)
{
wall.ytop=(float)backsector->floorheight/MAP_SCALE;
SKYTEXTURE(frontsector->sky,backsector->sky);
ADDWALL(&wall);
}
else
{
if ( (texturetranslation[seg->sidedef->bottomtexture]!=NO_TEXTURE) )
{
wall.ytop=(float)frontsector->floorheight/MAP_SCALE;
SKYTEXTURE(frontsector->sky,backsector->sky);
ADDWALL(&wall);
}
else
if ( (backsector->floorheight >= frontsector->ceilingheight) ||
(backsector->floorpic != skyflatnum) )
{
wall.ytop=(float)backsector->floorheight/MAP_SCALE;
SKYTEXTURE(frontsector->sky,backsector->sky);
ADDWALL(&wall);
}
}
}
#endif
if (floor_height<ceiling_height)
{
temptex=gld_RegisterTexture(texturetranslation[seg->sidedef->bottomtexture], true, false);
if ( !temptex ) {
temptex = defaultTexture; // it seems some line segments have bad data...
}
wall.gltexture=temptex;
CALC_Y_VALUES(wall, lineheight, floor_height, ceiling_height);
CALC_TEX_VALUES_BOTTOM(
wall, seg, (LINE->flags & ML_DONTPEGBOTTOM)>0,
seg->length, lineheight,
floor_height-frontsector->ceilingheight
);
ADDWALL(&wall);
}
}
}
#undef LINE
#undef CALC_Y_VALUES
#undef OU
#undef OV
#undef OV_PEG
#undef CALC_TEX_VALUES_TOP
#undef CALC_TEX_VALUES_MIDDLE1S
#undef CALC_TEX_VALUES_MIDDLE2S
#undef CALC_TEX_VALUES_BOTTOM
#undef SKYTEXTURE
#undef ADDWALL
/*
TransformAndClipSegment
Converts a world coordinate line segment to screen space.
Returns false if the segment is off screen.
There would be some savings if all the points in a subsector
were transformed and clip tested as a unit, instead of as discrete segments.
*/
boolean TransformAndClipSegment( float v[2][2], float ends[2] ) {
float clip[2][4];
// if we are in iphone reverse-landscape mode, we need
// to flip the coordinates around
float *v0, *v1;
if ( reversedLandscape ) {
v0 = v[1];
v1 = v[0];
} else {
v0 = v[0];
v1 = v[1];
}
// transform from model to clip space
// because the iPhone screen hardware is portrait mode,
// we need to look at the Y axis for the segment ends,
// not the X axis.
clip[0][1] = v0[0] * glMVPmatrix[1] + v0[1] * glMVPmatrix[2*4+1] + glMVPmatrix[3*4+1];
clip[0][3] = v0[0] * glMVPmatrix[3] + v0[1] * glMVPmatrix[2*4+3] + glMVPmatrix[3*4+3];
clip[1][1] = v1[0] * glMVPmatrix[1] + v1[1] * glMVPmatrix[2*4+1] + glMVPmatrix[3*4+1];
clip[1][3] = v1[0] * glMVPmatrix[3] + v1[1] * glMVPmatrix[2*4+3] + glMVPmatrix[3*4+3];
float d0, d1;
// clip to the near plane
float nearClip = 0.01f;
d0 = clip[0][3] - nearClip;
d1 = clip[1][3] - nearClip;
if ( d0 < 0 && d1 < 0 ) {
// near clipped
return false;
}
if ( d0 < 0 ) {
float f = d0 / ( d0 - d1 );
clip[0][1] = clip[0][1] + f * ( clip[1][1] - clip[0][1] );
clip[0][3] = nearClip;
} else if ( d1 < 0 ) {
float f = d1 / ( d1 - d0 );
clip[1][1] = clip[1][1] + f * ( clip[0][1] - clip[1][1] );
clip[1][3] = nearClip;
}
if ( clip[0][1] > clip[0][3] ) {
// entire segment is off the right side of the screen
return false;
}
if ( clip[1][1] < -clip[1][3] ) {
// entire segment is off the left side of the screen
return false;
}
// project
for ( int i = 0 ; i < 2 ; i++ ) {
float x = viewwidth * ( ( clip[i][1] / clip[i][3] ) * 0.5 + 0.5 );
if ( x < 0 ) {
x = 0;
} else if ( x > viewwidth ) {
x = viewwidth;
}
ends[i] = x;
}
// part of the segment is on screen
return true;
}
/*
IR_Subsector
All possible culling should be performed here, but most calculations should be
deferred until draw time, rather than storing intermediate values that are
later referenced.
Don't make this static, or the compiler inlines it in the recursive node
function, which bloats the stack.
*/
void IR_Subsector(int num)
{
subsector_t *sub = &subsectors[num];
c_subsectors++;
// at this point we know that at least part of the subsector is
// not covered in the occlusion array
// if the sector that this subsector is a part of has not already had its
// planes and sprites added, add them now.
sector_t *frontsector = sub->sector;
int lightlevel = frontsector->lightlevel+(extralight<<5);
// There can be several subsectors in each sector due to non-convex
// sectors or BSP splits, but we draw the floors, ceilings and lines
// with a single draw call for the entire thing, so ensure that they
// are only added once per frame.
if ( frontsector->validcount != validcount ) {
frontsector->validcount = validcount;
c_sectors++;
GLFlat flat;
flat.sectornum = frontsector->iSectorID;
flat.light = lightlevel;
flat.uoffs= 0; // no support in standard doom
flat.voffs= 0;
if ( frontsector->floorheight < viewz ) {
if (frontsector->floorpic == skyflatnum) {
skyIsVisible = true;
} else {
// get the texture. flattranslation is maintained by doom and
// contains the number of the current animation frame
GLTexture *tex = gld_RegisterFlat(flattranslation[frontsector->floorpic], true);
if ( tex ) {
sectorPlanes[numSectorPlanes].texture = tex;
sectorPlanes[numSectorPlanes].ceiling = false;
sectorPlanes[numSectorPlanes].sector = frontsector;
numSectorPlanes++;
}
}
}
if ( frontsector->ceilingheight > viewz ) {
if (frontsector->ceilingpic == skyflatnum) {
skyIsVisible = true;
} else {
// get the texture. flattranslation is maintained by doom and
// contains the number of the current animation frame
GLTexture *tex = gld_RegisterFlat(flattranslation[frontsector->ceilingpic], true);
if ( tex ) {
sectorPlanes[numSectorPlanes].texture = tex;
sectorPlanes[numSectorPlanes].ceiling = true;
sectorPlanes[numSectorPlanes].sector = frontsector;
numSectorPlanes++;
}
}
}
// Add all the sprites in this sector.
// It would be better if they were linked into all the subsectors, because
// we could do more accurate occlusion culling. With non-convex sectors,
// occasionally a sprite will be added in a rear portion of the sector that
// would have been occluded away if everything was done in BSP subsector order.
for ( mobj_t *thing = frontsector->thinglist; thing; thing = thing->snext) {
IR_ProjectSprite( thing, lightlevel );
}
}
// If a segment in this subsector is not fully occluded, mark
// the line that it is a part of as needing to be drawn. Because
// we are using a depth buffer, we can draw complete line segments
// instead of just segments.
for ( int i = 0 ; i < sub->numlines ; i++ ) {
seg_t *seg = &segs[sub->firstline+i];
line_t *line = seg->linedef;
// Determine if it will completely occlude farther objects.
// Given that changing sector heights is much less common than
// traversing lines during every render, it would be marginally better if
// lines had an "occluder" flag on them that was updated as sectors
// moved, but it hardly matters.
boolean occluder;
if ( seg->backsector == NULL ||
seg->backsector->floorheight >= seg->backsector->ceilingheight ||
seg->backsector->floorheight >= seg->frontsector->ceilingheight ||
seg->backsector->ceilingheight <= seg->frontsector->floorheight ) {
// this segment can't be seen past, so fill in the occlusion table
occluder = true;
} else {
// If the line has already been made visible and we don't need to
// update the occlusion buffer, we don't need to do anything else here.
// This happens when a line is split into multiple segs, and also
// when the line is reached from the backsector. In the backsector
// case, it would be back-face culled, but this test throws it out
// without having to transform and clip the ends.
if ( line->validcount == validcount ) {
continue;
}
// check to see if the seg won't draw any walls at all
// we won't fill in the occlusion table for this
occluder = false;
}