File: r_splats.c

package info (click to toggle)
doomlegacy 1.32beta4-3
  • links: PTS
  • area: contrib
  • in suites: woody
  • size: 6,768 kB
  • ctags: 13,999
  • sloc: ansic: 110,665; asm: 1,413; makefile: 915; pascal: 63
file content (677 lines) | stat: -rw-r--r-- 19,852 bytes parent folder | download
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
// Emacs style mode select   -*- C++ -*- 
//-----------------------------------------------------------------------------
//
// $Id: r_splats.c,v 1.5 2000/11/02 19:49:36 bpereira Exp $
//
// Copyright (C) 1998-2000 by DooM Legacy Team.
//
// 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.
//
//
// $Log: r_splats.c,v $
// Revision 1.5  2000/11/02 19:49:36  bpereira
// no message
//
// Revision 1.4  2000/08/11 19:10:13  metzgermeister
// *** empty log message ***
//
// Revision 1.3  2000/04/30 10:30:10  bpereira
// no message
//
// Revision 1.2  2000/02/27 00:42:11  hurdler
// fix CR+LF problem
//
// Revision 1.1.1.1  2000/02/22 20:32:32  hurdler
// Initial import into CVS (v1.29 pr3)
//
//
// DESCRIPTION:
//      floor and wall splats
//
//-----------------------------------------------------------------------------


#include "r_draw.h"
#include "r_main.h"
#include "r_plane.h"
#include "r_splats.h"
#include "w_wad.h"
#include "z_zone.h"

#ifdef WALLSPLATS
static wallsplat_t  wallsplats[MAXLEVELSPLATS];     // WALL splats
static int          freewallsplat;
#endif

#ifdef FLOORSPLATS
static floorsplat_t floorsplats[MAXLEVELSPLATS];    // FLOOR splats
static int          freefloorsplat;

struct rastery_s {
    fixed_t minx, maxx;     // for each raster line starting at line 0
    fixed_t tx1,ty1;
    fixed_t tx2,ty2;        // start/end points in texture at this line
};
static struct rastery_s rastertab[MAXVIDHEIGHT];

static void prepare_rastertab (void);

//r_plane.c
extern fixed_t     cachedheight[MAXVIDHEIGHT];
extern fixed_t     cacheddistance[MAXVIDHEIGHT];
extern fixed_t     cachedxstep[MAXVIDHEIGHT];
extern fixed_t     cachedystep[MAXVIDHEIGHT];
extern fixed_t     basexscale;
extern fixed_t     baseyscale;
#endif
// for floorsplats, accessed by asm code
struct rastery_s * prastertab;



// --------------------------------------------------------------------------
// setup splat cache
// --------------------------------------------------------------------------
void R_ClearLevelSplats (void)
{
#ifdef WALLSPLATS
    freewallsplat = 0;
    memset (wallsplats, 0, sizeof(wallsplats));
#endif
#ifdef FLOORSPLATS
    freefloorsplat = 0;
    memset (floorsplats, 0, sizeof(floorsplats));

    //setup to draw floorsplats
    prastertab = rastertab;
    prepare_rastertab ();
#endif
}


// ==========================================================================
//                                                                WALL SPLATS
// ==========================================================================
#ifdef WALLSPLATS
// --------------------------------------------------------------------------
// Return a pointer to a splat free for use, or NULL if no more splats are
// available
// --------------------------------------------------------------------------
static wallsplat_t* R_AllocWallSplat (void)
{
    wallsplat_t* splat;
    wallsplat_t* p_splat;
    line_t*  li;

    // clear the splat from the line if it was in use
    splat = &wallsplats[freewallsplat];
    li=splat->line;
    if ( li )
    {
        // remove splat from line splats list
        if (li->splats == splat)
            li->splats = splat->next;   //remove from head
        else
        {
#ifdef PARANOIA
            if(!li->splats)
                I_Error("R_AllocWallSplat : No splat in this line\n");
#endif
            for(p_splat = li->splats;p_splat->next;p_splat = p_splat->next)
                if (p_splat->next == splat)
                {
                    p_splat->next = splat->next;
                    break;
                }
        }
    }

    memset (splat, 0, sizeof(wallsplat_t));

    // for next allocation
    freewallsplat++;
    if (freewallsplat >= MAXLEVELSPLATS)
        freewallsplat = 0;

    return splat;
}


// Add a new splat to the linedef:
// top : top z coord
// wallfrac : frac along the linedef vector (0 to FRACUNIT)
// splatpatchname : name of patch to draw
void R_AddWallSplat (line_t*    wallline,
                     int        sectorside,
                     char*      patchname,
                     fixed_t    top,
                     fixed_t    wallfrac,
                     int        flags)
{
    fixed_t         fracsplat;
    wallsplat_t*    splat;
    wallsplat_t*    p_splat;
    patch_t*        patch;
    fixed_t         linelength;
    sector_t        *backsector=NULL;

    if( demoversion<128 )
        return;

    splat = R_AllocWallSplat ();
    if (!splat)
        return;

    // set the splat
    splat->patch = W_GetNumForName (patchname);
    sectorside^=1;
    if( wallline->sidenum[sectorside]!=-1 )
    {
        backsector = sides[wallline->sidenum[sectorside]].sector;

        if(top<backsector->floorheight)
        {
            splat->yoffset = &backsector->floorheight;
            top -= backsector->floorheight;
        }
        else
        if(top>backsector->ceilingheight)
        {
            splat->yoffset = &backsector->ceilingheight;
            top -= backsector->ceilingheight;
        }
    }
    //splat->sectorside = sectorside;

    splat->top = top;
    splat->flags = flags;

    // bad.. but will be needed for drawing anyway..
    patch = W_CachePatchNum (splat->patch, PU_CACHE);

    
    // offset needed by draw code for texture mapping
    linelength = P_SegLength((seg_t*)wallline);
    splat->offset = FixedMul(wallfrac, linelength) - (patch->width<<(FRACBITS-1));
    //CONS_Printf("offset splat %d\n",splat->offset);
    fracsplat = FixedDiv( ((patch->width<<FRACBITS)>>1) , linelength );
    
    wallfrac -= fracsplat;
    if( wallfrac>linelength )
        return;
    //CONS_Printf("final splat possition %f\n",FIXED_TO_FLOAT(wallfrac));
    splat->v1.x = wallline->v1->x + FixedMul (wallline->dx, wallfrac);
    splat->v1.y = wallline->v1->y + FixedMul (wallline->dy, wallfrac);
    wallfrac += fracsplat + fracsplat;
    if( wallfrac<0 )
        return;
    splat->v2.x = wallline->v1->x + FixedMul (wallline->dx, wallfrac);
    splat->v2.y = wallline->v1->y + FixedMul (wallline->dy, wallfrac);


    if( wallline->frontsector && wallline->frontsector == backsector )
    {
/*  BP: dont work texture mapping problem :(
        // in the other side
        vertex_t p = splat->v1;
        splat->v1 = splat->v2;
        splat->v2 = p;
        CONS_Printf("split\n");
*/
        return;
    }

    // insert splat in the linedef splat list
    // BP: why not insert in head is mush more simple ?
    // BP: because for remove it is more simple !
    splat->line = wallline;
    splat->next = NULL;
    if (wallline->splats)
    {
        p_splat = wallline->splats;
        while (p_splat->next)
            p_splat = p_splat->next;
        p_splat->next = splat;
    }
    else
        wallline->splats = splat;
}
#endif // WALLSPLATS


// ==========================================================================
//                                                               FLOOR SPLATS
// ==========================================================================
#ifdef FLOORSPLATS

// --------------------------------------------------------------------------
// Return a pointer to a splat free for use, or NULL if no more splats are
// available
// --------------------------------------------------------------------------
static floorsplat_t* R_AllocFloorSplat (void)
{
    floorsplat_t* splat;
    floorsplat_t* p_splat;
    subsector_t*  sub;

    // find splat to use
    freefloorsplat++;
    if (freefloorsplat >= MAXLEVELSPLATS)
        freefloorsplat = 0;

    // clear the splat from the line if it was in use
    splat = &floorsplats[freefloorsplat];
    if ( (sub=splat->subsector) )
    {
        // remove splat from subsector splats list
        if (sub->splats == splat)
            sub->splats = splat->next;   //remove from head
        else
        {
            p_splat = sub->splats;
            while (p_splat->next)
            {
                if (p_splat->next == splat)
                    p_splat->next = splat->next;
            }
        }
    }

    memset (splat, 0, sizeof(floorsplat_t));
    return splat;
}


// --------------------------------------------------------------------------
// Add a floor splat to the subsector
// --------------------------------------------------------------------------
void R_AddFloorSplat (subsector_t* subsec, char* picname, fixed_t x, fixed_t y, fixed_t z, int flags)
{
    floorsplat_t*    splat;
    floorsplat_t*    p_splat;

    splat = R_AllocFloorSplat ();
    if (!splat)
        return;

    CONS_Printf ("added a floor splat\n");

    // set the splat
    splat->pic = W_GetNumForName (picname);

    splat->flags = flags;
    
    //

    //for test fix 64x64
    // 3--2
    // |  |
    // 0--1
    //
    splat->z = z;
    splat->verts[0].x = splat->verts[3].x = x - (32<<FRACBITS);
    splat->verts[2].x = splat->verts[1].x = x + (31<<FRACBITS);
    splat->verts[3].y = splat->verts[2].y = y + (31<<FRACBITS);
    splat->verts[0].y = splat->verts[1].y = y - (32<<FRACBITS);

    // insert splat in the subsector splat list
    splat->subsector = subsec;
    splat->next = NULL;
    if (subsec->splats)
    {
        p_splat = subsec->splats;
        while (p_splat->next)
            p_splat = p_splat->next;
        p_splat->next = splat;
    }
    else
        subsec->splats = splat;
}


// --------------------------------------------------------------------------
// Before each frame being rendered, clear the visible floorsplats list
// --------------------------------------------------------------------------
static floorsplat_t*   visfloorsplats;

void R_ClearVisibleFloorSplats (void)
{
    visfloorsplats = NULL;
}


// --------------------------------------------------------------------------
// Add a floorsplat to the visible floorsplats list, for the current frame
// --------------------------------------------------------------------------
void R_AddVisibleFloorSplats (subsector_t* subsec)
{
    floorsplat_t* pSplat;
#ifdef PARANOIA
    if (subsec->splats==NULL)
        I_Error ("R_AddVisibleFloorSplats: call with no splats");
#endif

    pSplat = subsec->splats;
    // the splat is not visible from below
    // FIXME: depending on some flag in pSplat->flags, some splats may be visible from 2 sides (above/below)
    if (pSplat->z < viewz) {
        pSplat->nextvis = visfloorsplats;
        visfloorsplats = pSplat;
    }

    while (pSplat->next)
    {
        pSplat = pSplat->next;
        if (pSplat->z < viewz) {
            pSplat->nextvis = visfloorsplats;
            visfloorsplats = pSplat;
        }
    }
}


// tv1,tv2 = x/y qui varie dans la texture, tc = x/y qui est constant.
void    ASMCALL rasterize_segment_tex (int x1, int y1, int x2, int y2, int tv1, int tv2, int tc, int dir);

// current test with floor tile
#define TEXWIDTH 64
#define TEXHEIGHT 64
//#define FLOORSPLATSOLIDCOLOR

// --------------------------------------------------------------------------
// Rasterize the four edges of a floor splat polygon,
// fill the polygon with linear interpolation, call span drawer for each
// scan line
// --------------------------------------------------------------------------
static void R_RenderFloorSplat (floorsplat_t* pSplat, vertex_t* verts, byte* pTex)
{
    // resterizing
    int     miny = vid.height + 1;
        int     maxy = 0;
        int     x, y;
    int     x1, y1, x2, y2;
    byte*   pDest;
        int     tx, ty, tdx, tdy;

    // rendering
    lighttable_t**  planezlight;
    fixed_t         planeheight;
    angle_t     angle;
    fixed_t     distance;
    fixed_t     length;
    unsigned    index;
    int         light;

    fixed_t     offsetx,offsety;

    offsetx = pSplat->verts[0].x & 0x3fffff;
    offsety = pSplat->verts[0].y & 0x3fffff;

        // do for each segment, starting with the first one
    /*CONS_Printf ("floor splat (%d,%d) (%d,%d) (%d,%d) (%d,%d)\n",
                  verts[3].x,verts[3].y,verts[2].x,verts[2].y,
                  verts[1].x,verts[1].y,verts[0].x,verts[0].y);*/

    // do segment a -> top of texture
            x1 = verts[3].x;
            y1 = verts[3].y;
            x2 = verts[2].x;
            y2 = verts[2].y;
    if (y1<0) y1=0;
    if (y1>=vid.height) y1 = vid.height-1;
    if (y2<0) y2=0;
    if (y2>=vid.height) y2 = vid.height-1;
        rasterize_segment_tex (x1, y1, x2, y2, 0, TEXWIDTH-1, 0, 0);
            if( y1 < miny )
                    miny = y1;
            if( y1 > maxy )
                    maxy = y1;

    // do segment b -> right side of texture
                x1 = x2;
                y1 = y2;
                x2 = verts[1].x;
                y2 = verts[1].y;
    if (y1<0) y1=0;
    if (y1>=vid.height) y1 = vid.height-1;
    if (y2<0) y2=0;
    if (y2>=vid.height) y2 = vid.height-1;
                rasterize_segment_tex (x1, y1, x2, y2, 0, TEXHEIGHT-1, TEXWIDTH-1, 1);
                if( y1 < miny )
                    miny = y1;
                if( y1 > maxy )
                    maxy = y1;
        
    // do segment c -> bottom of texture
                x1 = x2;
                y1 = y2;
                x2 = verts[0].x;
                y2 = verts[0].y;
    if (y1<0) y1=0;
    if (y1>=vid.height) y1 = vid.height-1;
    if (y2<0) y2=0;
    if (y2>=vid.height) y2 = vid.height-1;
                rasterize_segment_tex (x1, y1, x2, y2, TEXWIDTH-1, 0, TEXHEIGHT-1, 0);
                if( y1 < miny )
                    miny = y1;
                if( y1 > maxy )
                    maxy = y1;
        
    // do segment d -> left side of texture
                x1 = x2;
                y1 = y2;
                x2 = verts[3].x;
                y2 = verts[3].y;
    if (y1<0) y1=0;
    if (y1>=vid.height) y1 = vid.height-1;
    if (y2<0) y2=0;
    if (y2>=vid.height) y2 = vid.height-1;
                rasterize_segment_tex (x1, y1, x2, y2, TEXHEIGHT-1, 0, 0, 1);
                if( y1 < miny )
                    miny = y1;
                if( y1 > maxy )
                    maxy = y1;

        // remplissage du polygone a 4 cotes AVEC UNE TEXTURE
        //fill_texture_linear( trashbmp, tex->imgdata, miny, maxy );
        //return;

#ifndef FLOORSPLATSOLIDCOLOR

    // prepare values for all the splat
    ds_source = (byte *)W_CacheLumpNum(pSplat->pic,PU_CACHE);
    planeheight = abs(pSplat->z - viewz);
    light = (pSplat->subsector->sector->lightlevel >> LIGHTSEGSHIFT)+extralight;
    if (light >= LIGHTLEVELS)
        light = LIGHTLEVELS-1;
    if (light < 0)
        light = 0;
    planezlight = zlight[light];

    for (y=miny; y<=maxy; y++)
    {
            x1 = rastertab[y].minx >> FRACBITS;
            x2 = rastertab[y].maxx >> FRACBITS;

        if (x1<0) x1 = 0;
        if (x2>=vid.width) x2 = vid.width-1;

        if (planeheight != cachedheight[y])
        {
            cachedheight[y] = planeheight;
            distance = cacheddistance[y] = FixedMul (planeheight, yslope[y]);
            ds_xstep = cachedxstep[y] = FixedMul (distance,basexscale);
            ds_ystep = cachedystep[y] = FixedMul (distance,baseyscale);
        }
        else
        {
            distance = cacheddistance[y];
            ds_xstep = cachedxstep[y];
            ds_ystep = cachedystep[y];
        }
        length = FixedMul (distance,distscale[x1]);
        angle = (viewangle + xtoviewangle[x1])>>ANGLETOFINESHIFT;
        ds_xfrac = viewx + FixedMul(finecosine[angle], length);
        ds_yfrac = -viewy - FixedMul(finesine[angle], length);
        ds_xfrac -= offsetx;
        ds_yfrac += offsety;

        if (fixedcolormap)
            ds_colormap = fixedcolormap;
        else
        {
            index = distance >> LIGHTZSHIFT;
            if (index >= MAXLIGHTZ )
                index = MAXLIGHTZ-1;
            ds_colormap = planezlight[index];
        }

        ds_y = y;
        ds_x1 = x1;
        ds_x2 = x2;
        spanfunc ();

        // reset for next calls to edge rasterizer
            rastertab[y].minx = MAXINT;
            rastertab[y].maxx = MININT;
        }

#else

    for (y=miny; y<=maxy; y++)
    {
        x1 = rastertab[y].minx >> FRACBITS;
        x2 = rastertab[y].maxx >> FRACBITS;
        /*if ((unsigned)x1 >= vid.width)
            continue;
        if ((unsigned)x2 >= vid.width)
            continue;*/
        if (x1<0) x1 = 0;
        //if (x1>=vid.width) x1 = vid.width-1;
        //if (x2<0) x1 = 0;
        if (x2>=vid.width)
            x2 = vid.width-1;

        pDest = ylookup[y] + columnofs[x1];

        x = (x2-x1) + 1;

        //point de dpart dans la texture
        tx = rastertab[y].tx1;
        ty = rastertab[y].ty1;

        // HORRIBLE BUG!!!
        if(x>0)
        {
             tdx = (rastertab[y].tx2 - tx) / x;
             tdy = (rastertab[y].ty2 - ty) / x;

             while (x-- > 0)
             {
                 *(pDest++) = (y&255);
                 tx += tdx;
                 ty += tdy;
             }
        }

        // rinitialise les minimus maximus pour le prochain appel
        rastertab[y].minx = MAXINT;
        rastertab[y].maxx = MININT;
    }
#endif
}


// --------------------------------------------------------------------------
// R_DrawFloorSplats
// draw the flat floor/ceiling splats
// --------------------------------------------------------------------------
void R_DrawVisibleFloorSplats (void)
{
    floorsplat_t* pSplat;
    int           iCount = 0;
    
    fixed_t       tr_x;
    fixed_t       tr_y;
    fixed_t       rot_x;
    fixed_t       rot_y;
    fixed_t       rot_z;
    fixed_t       xscale;
    fixed_t       yscale;
    vertex_t*     v3d;
    vertex_t      v2d[4];
    int           i;

    pSplat = visfloorsplats;
    while (pSplat)
    {
        iCount++;

        // Draw a floor splat
        // 3--2
        // |  |
        // 0--1
        
        rot_z = pSplat->z - viewz;
        for (i=0; i<4; i++)
        {
            v3d = &pSplat->verts[i];
            
            // transform the origin point
            tr_x = v3d->x - viewx;
            tr_y = v3d->y - viewy;

            // rotation around vertical y axis
            rot_x = FixedMul(tr_x,viewsin) - FixedMul(tr_y,viewcos);
            rot_y = FixedMul(tr_x,viewcos) + FixedMul(tr_y,viewsin);

            if (rot_y < 4*FRACUNIT)
                goto skipit;

            // note: y from view above of map, is distance far away
            xscale = FixedDiv(projection, rot_y);
            yscale = -FixedDiv(projectiony, rot_y);

            // projection
            v2d[i].x = (centerxfrac + FixedMul (rot_x, xscale)) >>FRACBITS;
            v2d[i].y = (centeryfrac + FixedMul (rot_z, yscale)) >>FRACBITS;
        }
        /*
        pSplat->verts[3].x = 100 + iCount;
        pSplat->verts[3].y = 10 + iCount;
        pSplat->verts[2].x = 160 + iCount;
        pSplat->verts[2].y = 80 + iCount;
        pSplat->verts[1].x = 100 + iCount;
        pSplat->verts[1].y = 150 + iCount;
        pSplat->verts[0].x = 8 + iCount;
        pSplat->verts[0].y = 90 + iCount;
        */
        R_RenderFloorSplat (pSplat, v2d, NULL);
skipit:
        pSplat = pSplat->nextvis;
    }
    
    CONS_Printf ("%d floor splats in view\n", iCount);
}


static void prepare_rastertab (void)
{
    int iLine;
    for (iLine=0; iLine<vid.height; iLine++)
    {
         rastertab[iLine].minx = MAXINT;
         rastertab[iLine].maxx = MININT;
    }
}


#endif // FLOORSPLATS