ref: 86d9d6b63def9020078dc2b1796ed0eb2022d8f1
dir: /d_scan.c/
#include <u.h>
#include <libc.h>
#include "dat.h"
#include "quakedef.h"
#include "fns.h"
unsigned char *r_turb_pbase, *r_turb_pdest;
fixed16_t r_turb_s, r_turb_t, r_turb_sstep, r_turb_tstep;
int *r_turb_turb;
int r_turb_spancount;
static uzint *r_turb_z;
/*
=============
D_WarpScreen
// this performs a slight compression of the screen at the same time as
// the sine warp, to keep the edges from wrapping
=============
*/
void D_WarpScreen (void)
{
int w, h;
int u,v;
byte *dest;
int *turb;
int *col;
byte **row;
static byte *rowptr[MAXHEIGHT+(AMP2*2)];
static int column[MAXWIDTH+(AMP2*2)];
float wratio, hratio;
w = r_refdef.vrect.width;
h = r_refdef.vrect.height;
wratio = w / (float)scr_vrect.width;
hratio = h / (float)scr_vrect.height;
for (v=0 ; v<scr_vrect.height+AMP2*2 ; v++)
{
rowptr[v] = d_viewbuffer + (r_refdef.vrect.y * screenwidth) +
(screenwidth * (int)((float)v * hratio * h / (h + AMP2 * 2)));
}
for (u=0 ; u<scr_vrect.width+AMP2*2 ; u++)
{
column[u] = r_refdef.vrect.x +
(int)((float)u * wratio * w / (w + AMP2 * 2));
}
turb = intsintable + ((int)(cl.time*SPEED)&(CYCLE-1));
dest = vid.buffer + scr_vrect.y * vid.rowbytes + scr_vrect.x;
for (v=0 ; v<scr_vrect.height ; v++, dest += vid.rowbytes)
{
col = &column[turb[v]];
row = &rowptr[v];
for (u=0 ; u<scr_vrect.width ; u+=4)
{
dest[u+0] = row[turb[u+0]][col[u+0]];
dest[u+1] = row[turb[u+1]][col[u+1]];
dest[u+2] = row[turb[u+2]][col[u+2]];
dest[u+3] = row[turb[u+3]][col[u+3]];
}
}
}
static byte *alphamap;
static byte mapalpha;
static void
buildalpha(int alpha)
{
extern s32int fbpal[256];
int a, b;
byte *ca, *cb, *p;
int r0, g0, b0;
int r1, g1, b1;
int rr, gg, bb;
int i, dst, x, best;
if(alphamap == nil)
alphamap = malloc(256*256);
for(a = 0; a < 256; a++){
ca = (byte*)&fbpal[a];
r0 = ca[0]; g0 = ca[1]; b0 = ca[2];
for(b = 0; b < 256; b++){
cb = (byte*)&fbpal[b];
r1 = cb[0]; g1 = cb[1]; b1 = cb[2];
rr = (alpha*r0 + (255 - alpha)*r1)/255;
gg = (alpha*g0 + (255 - alpha)*g1)/255;
bb = (alpha*b0 + (255 - alpha)*b1)/255;
dst = 9999999;
best = 255;
p = (byte*)fbpal;
for(i = 0; i < 768; i += 4){
if((x = (rr-p[i])*(rr-p[i])+(gg-p[i+1])*(gg-p[i+1])+(bb-p[i+2])*(bb-p[i+2])) < dst){
dst = x;
best = i;
}
alphamap[a<<8 | b] = best/4;
}
}
}
}
#define blendalpha(a, b, alpha) \
alphamap[(u16int)((a)<<8 | (b))]
/*
=============
D_DrawTurbulent8Span
=============
*/
void D_DrawTurbulent8Span (int izi, byte alpha)
{
int sturb, tturb;
do
{
sturb = ((r_turb_s + r_turb_turb[(r_turb_t>>16)&(CYCLE-1)])>>16)&63;
tturb = ((r_turb_t + r_turb_turb[(r_turb_s>>16)&(CYCLE-1)])>>16)&63;
if (*r_turb_z <= (izi >> 16)){
if(alpha == 255 || alpha == 0)
*r_turb_pdest = *(r_turb_pbase + (tturb<<6) + sturb);
else
*r_turb_pdest = blendalpha(*(r_turb_pbase + (tturb<<6) + sturb), *r_turb_pdest, alpha);
*r_turb_z = (izi >> 16);
}
r_turb_s += r_turb_sstep;
r_turb_t += r_turb_tstep;
r_turb_pdest++;
r_turb_z++;
} while (--r_turb_spancount > 0);
}
/*
=============
Turbulent8
=============
*/
void Turbulent8 (espan_t *pspan, float alpha)
{
int count;
fixed16_t snext, tnext;
float sdivz, tdivz, zi, z, du, dv, spancountminus1;
float sdivz16stepu, tdivz16stepu, zi16stepu;
byte balpha;
r_turb_turb = sintable + ((int)(cl.time*SPEED)&(CYCLE-1));
r_turb_sstep = 0; // keep compiler happy
r_turb_tstep = 0; // ditto
r_turb_pbase = (unsigned char *)cacheblock;
sdivz16stepu = d_sdivzstepu * 16;
tdivz16stepu = d_tdivzstepu * 16;
zi16stepu = d_zistepu * 16;
alpha = clamp(alpha, 0.0, 1.0);
balpha = alpha * 255;
if(balpha != 255 && (alphamap == nil || balpha != mapalpha)){
mapalpha = balpha;
buildalpha(balpha);
}
do
{
r_turb_pdest = (unsigned char *)((byte *)d_viewbuffer +
(screenwidth * pspan->v) + pspan->u);
r_turb_z = d_pzbuffer + (d_zwidth * pspan->v) + pspan->u;
count = pspan->count;
// calculate the initial s/z, t/z, 1/z, s, and t and clamp
du = (float)pspan->u;
dv = (float)pspan->v;
sdivz = d_sdivzorigin + dv*d_sdivzstepv + du*d_sdivzstepu;
tdivz = d_tdivzorigin + dv*d_tdivzstepv + du*d_tdivzstepu;
zi = d_ziorigin + dv*d_zistepv + du*d_zistepu;
z = (float)0x10000 / zi; // prescale to 16.16 fixed-point
r_turb_s = (int)(sdivz * z) + sadjust;
if (r_turb_s > bbextents)
r_turb_s = bbextents;
else if (r_turb_s < 0)
r_turb_s = 0;
r_turb_t = (int)(tdivz * z) + tadjust;
if (r_turb_t > bbextentt)
r_turb_t = bbextentt;
else if (r_turb_t < 0)
r_turb_t = 0;
do
{
// calculate s and t at the far end of the span
if (count >= 16)
r_turb_spancount = 16;
else
r_turb_spancount = count;
count -= r_turb_spancount;
if (count)
{
// calculate s/z, t/z, zi->fixed s and t at far end of span,
// calculate s and t steps across span by shifting
sdivz += sdivz16stepu;
tdivz += tdivz16stepu;
zi += zi16stepu;
z = (float)0x10000 / zi; // prescale to 16.16 fixed-point
snext = (int)(sdivz * z) + sadjust;
if (snext > bbextents)
snext = bbextents;
else if (snext < 16)
snext = 16; // prevent round-off error on <0 steps from
// from causing overstepping & running off the
// edge of the texture
tnext = (int)(tdivz * z) + tadjust;
if (tnext > bbextentt)
tnext = bbextentt;
else if (tnext < 16)
tnext = 16; // guard against round-off error on <0 steps
r_turb_sstep = (snext - r_turb_s) >> 4;
r_turb_tstep = (tnext - r_turb_t) >> 4;
}
else
{
// calculate s/z, t/z, zi->fixed s and t at last pixel in span (so
// can't step off polygon), clamp, calculate s and t steps across
// span by division, biasing steps low so we don't run off the
// texture
spancountminus1 = (float)(r_turb_spancount - 1);
sdivz += d_sdivzstepu * spancountminus1;
tdivz += d_tdivzstepu * spancountminus1;
zi += d_zistepu * spancountminus1;
z = (float)0x10000 / zi; // prescale to 16.16 fixed-point
snext = (int)(sdivz * z) + sadjust;
if (snext > bbextents)
snext = bbextents;
else if (snext < 16)
snext = 16; // prevent round-off error on <0 steps from
// from causing overstepping & running off the
// edge of the texture
tnext = (int)(tdivz * z) + tadjust;
if (tnext > bbextentt)
tnext = bbextentt;
else if (tnext < 16)
tnext = 16; // guard against round-off error on <0 steps
if (r_turb_spancount > 1)
{
r_turb_sstep = (snext - r_turb_s) / (r_turb_spancount - 1);
r_turb_tstep = (tnext - r_turb_t) / (r_turb_spancount - 1);
}
}
r_turb_s = r_turb_s & ((CYCLE<<16)-1);
r_turb_t = r_turb_t & ((CYCLE<<16)-1);
D_DrawTurbulent8Span ((int)(zi * 0x8000 * 0x10000), balpha);
r_turb_s = snext;
r_turb_t = tnext;
} while (count > 0);
} while ((pspan = pspan->pnext) != nil);
}
#define WRITEFENCE(i) do{ \
fencepix = *(pbase + (s >> 16) + (t >> 16) * cachewidth); \
if (pz[i] <= (izi >> 16) && fencepix != 255) \
pdest[i] = fencepix; pz[i] = (izi >> 16); \
izi += izistep; \
s += sstep; \
t += tstep; \
}while(0)
void D_DrawSpans16_Fence (espan_t *pspan)
{
byte fencepix;
byte *pbase = (byte *)cacheblock, *pdest;
int count, spancount, izi, izistep;
fixed16_t s, t, snext, tnext, sstep, tstep;
float sdivz, tdivz, zi, z, du, dv, spancountminus1;
float sdivzstepu, tdivzstepu, zistepu;
uzint *pz;
sdivzstepu = d_sdivzstepu * 16;
tdivzstepu = d_tdivzstepu * 16;
zistepu = d_zistepu * 16;
// we count on FP exceptions being turned off to avoid range problems
izistep = (int)(d_zistepu * 0x8000 * 0x10000);
sstep = tstep = 0;
do
{
pdest = (byte *)((byte *)d_viewbuffer + (screenwidth * pspan->v) + pspan->u);
pz = d_pzbuffer + (d_zwidth * pspan->v) + pspan->u;
count = pspan->count >> 4;
spancount = pspan->count % 16;
// calculate the initial s/z, t/z, 1/z, s, and t and clamp
du = (float)pspan->u;
dv = (float)pspan->v;
sdivz = d_sdivzorigin + dv*d_sdivzstepv + du*d_sdivzstepu;
tdivz = d_tdivzorigin + dv*d_tdivzstepv + du*d_tdivzstepu;
zi = d_ziorigin + dv*d_zistepv + du*d_zistepu;
z = (float)0x10000 / zi; // prescale to 16.16 fixed-point
// we count on FP exceptions being turned off to avoid range problems
izi = (int)(zi * 0x8000 * 0x10000);
s = (int)(sdivz * z) + sadjust;
if (s > bbextents)
s = bbextents;
else if (s < 0)
s = 0;
t = (int)(tdivz * z) + tadjust;
if (t > bbextentt)
t = bbextentt;
else if (t < 0)
t = 0;
while (count--){
// calculate s/z, t/z, zi->fixed s and t at far end of span,
// calculate s and t steps across span by shifting
sdivz += sdivzstepu;
tdivz += tdivzstepu;
zi += zistepu;
z = (float)0x10000 / zi; // prescale to 16.16 fixed-point
snext = (int) (sdivz * z) + sadjust;
if (snext > bbextents)
snext = bbextents;
else if (snext <= 16)
snext = 16; // prevent round-off error on <0 steps causing overstepping & running off the edge of the texture
tnext = (int) (tdivz * z) + tadjust;
if (tnext > bbextentt)
tnext = bbextentt;
else if (tnext < 16)
tnext = 16; // guard against round-off error on <0 steps
sstep = (snext - s) >> 4;
tstep = (tnext - t) >> 4;
pdest += 16;
pz += 16;
WRITEFENCE(-16);
WRITEFENCE(-15);
WRITEFENCE(-14);
WRITEFENCE(-13);
WRITEFENCE(-12);
WRITEFENCE(-11);
WRITEFENCE(-10);
WRITEFENCE(-9);
WRITEFENCE(-8);
WRITEFENCE(-7);
WRITEFENCE(-6);
WRITEFENCE(-5);
WRITEFENCE(-4);
WRITEFENCE(-3);
WRITEFENCE(-2);
WRITEFENCE(-1);
USED(s, t);
s = snext;
t = tnext;
}
if (spancount > 0)
{
// calculate s/z, t/z, zi->fixed s and t at last pixel in span (so can't step off polygon),
// clamp, calculate s and t steps across span by division, biasing steps low so we don't run off the texture
spancountminus1 = (float)(spancount - 1);
sdivz += d_sdivzstepu * spancountminus1;
tdivz += d_tdivzstepu * spancountminus1;
zi += d_zistepu * spancountminus1;
z = (float)0x10000 / zi; // prescale to 16.16 fixed-point
snext = (int)(sdivz * z) + sadjust;
if (snext > bbextents)
snext = bbextents;
else if (snext < 16)
snext = 16; // prevent round-off error on <0 steps from causing overstepping & running off the edge of the texture
tnext = (int)(tdivz * z) + tadjust;
if (tnext > bbextentt)
tnext = bbextentt;
else if (tnext < 16)
tnext = 16; // guard against round-off error on <0 steps
if (spancount > 1){
sstep = (snext - s) / (spancount - 1);
tstep = (tnext - t) / (spancount - 1);
}
pdest += spancount;
pz += spancount;
switch (spancount){
case 16: WRITEFENCE(-16);
case 15: WRITEFENCE(-15);
case 14: WRITEFENCE(-14);
case 13: WRITEFENCE(-13);
case 12: WRITEFENCE(-12);
case 11: WRITEFENCE(-11);
case 10: WRITEFENCE(-10);
case 9: WRITEFENCE(-9);
case 8: WRITEFENCE(-8);
case 7: WRITEFENCE(-7);
case 6: WRITEFENCE(-6);
case 5: WRITEFENCE(-5);
case 4: WRITEFENCE(-4);
case 3: WRITEFENCE(-3);
case 2: WRITEFENCE(-2);
case 1: WRITEFENCE(-1);
USED(izi, s, t);
}
}
}
while ((pspan = pspan->pnext) != nil);
}
/*
=============
D_DrawSpans16
=============
*/
void D_DrawSpans16 (espan_t *pspan, float alpha) //qbism- up it from 8 to 16
{
int count, spancount;
unsigned char *pbase, *pdest;
fixed16_t s, t, snext, tnext, sstep, tstep;
float sdivz, tdivz, zi, z, du, dv, spancountminus1;
float sdivzstepu, tdivzstepu, zistepu;
sstep = 0; // keep compiler happy
tstep = 0; // ditto
pbase = (unsigned char *)cacheblock;
sdivzstepu = d_sdivzstepu * 16;
tdivzstepu = d_tdivzstepu * 16;
zistepu = d_zistepu * 16;
do
{
pdest = (unsigned char *)((byte *)d_viewbuffer +
(screenwidth * pspan->v) + pspan->u);
count = pspan->count;
// calculate the initial s/z, t/z, 1/z, s, and t and clamp
du = (float)pspan->u;
dv = (float)pspan->v;
sdivz = d_sdivzorigin + dv*d_sdivzstepv + du*d_sdivzstepu;
tdivz = d_tdivzorigin + dv*d_tdivzstepv + du*d_tdivzstepu;
zi = d_ziorigin + dv*d_zistepv + du*d_zistepu;
z = (float)0x10000 / zi; // prescale to 16.16 fixed-point
s = (int)(sdivz * z) + sadjust;
if (s > bbextents)
s = bbextents;
else if (s < 0)
s = 0;
t = (int)(tdivz * z) + tadjust;
if (t > bbextentt)
t = bbextentt;
else if (t < 0)
t = 0;
do
{
// calculate s and t at the far end of the span
if (count >= 16)
spancount = 16;
else
spancount = count;
count -= spancount;
if (count)
{
// calculate s/z, t/z, zi->fixed s and t at far end of span,
// calculate s and t steps across span by shifting
sdivz += sdivzstepu;
tdivz += tdivzstepu;
zi += zistepu;
z = (float)0x10000 / zi; // prescale to 16.16 fixed-point
snext = (int)(sdivz * z) + sadjust;
if (snext > bbextents)
snext = bbextents;
else if (snext <= 16)
snext = 16; // prevent round-off error on <0 steps from
// from causing overstepping & running off the
// edge of the texture
tnext = (int)(tdivz * z) + tadjust;
if (tnext > bbextentt)
tnext = bbextentt;
else if (tnext < 16)
tnext = 16; // guard against round-off error on <0 steps
sstep = (snext - s) >> 4;
tstep = (tnext - t) >> 4;
}
else
{
// calculate s/z, t/z, zi->fixed s and t at last pixel in span (so
// can't step off polygon), clamp, calculate s and t steps across
// span by division, biasing steps low so we don't run off the
// texture
spancountminus1 = (float)(spancount - 1);
sdivz += d_sdivzstepu * spancountminus1;
tdivz += d_tdivzstepu * spancountminus1;
zi += d_zistepu * spancountminus1;
z = (float)0x10000 / zi; // prescale to 16.16 fixed-point
snext = (int)(sdivz * z) + sadjust;
if (snext > bbextents)
snext = bbextents;
else if (snext < 16)
snext = 16; // prevent round-off error on <0 steps from
// from causing overstepping & running off the
// edge of the texture
tnext = (int)(tdivz * z) + tadjust;
if (tnext > bbextentt)
tnext = bbextentt;
else if (tnext < 16)
tnext = 16; // guard against round-off error on <0 steps
if (spancount > 1)
{
sstep = (snext - s) / (spancount - 1);
tstep = (tnext - t) / (spancount - 1);
}
}
if(spancount > 0){
void dospan(uchar *, uchar *, int, int, int, int, int, int);
dospan(pdest, pbase, s, t, sstep, tstep, spancount, cachewidth);
}
pdest += spancount;
s = snext;
t = tnext;
} while (count > 0);
} while ((pspan = pspan->pnext) != nil);
}
/*
=============
D_DrawZSpans
=============
*/
void D_DrawZSpans (espan_t *pspan)
{
int count, doublecount, izistep;
int izi;
uzint *pdest;
unsigned ltemp;
double zi;
float du, dv;
// FIXME: check for clamping/range problems
// we count on FP exceptions being turned off to avoid range problems
izistep = (int)(d_zistepu * 0x8000 * 0x10000);
do
{
pdest = d_pzbuffer + (d_zwidth * pspan->v) + pspan->u;
count = pspan->count;
// calculate the initial 1/z
du = (float)pspan->u;
dv = (float)pspan->v;
zi = d_ziorigin + dv*d_zistepv + du*d_zistepu;
// we count on FP exceptions being turned off to avoid range problems
izi = (int)(zi * 0x8000 * 0x10000);
if ((uintptr)pdest & 0x02)
{
*pdest++ = (short)(izi >> 16);
izi += izistep;
count--;
}
if ((doublecount = count >> 1) > 0)
{
do
{
ltemp = izi >> 16;
izi += izistep;
ltemp |= izi & 0xFFFF0000;
izi += izistep;
*(int *)pdest = ltemp;
pdest += 2;
} while (--doublecount > 0);
}
if (count & 1)
*pdest = (short)(izi >> 16);
} while ((pspan = pspan->pnext) != nil);
}