2010-09-09 11:46:38 +00:00
|
|
|
#include "render.h"
|
2003-03-18 19:48:24 +00:00
|
|
|
|
2010-09-29 20:09:11 +00:00
|
|
|
extern vec3_t xy_viewnormal;
|
2003-03-18 19:48:24 +00:00
|
|
|
|
2010-09-29 20:09:11 +00:00
|
|
|
// define NOLIGHT
|
2003-03-18 19:48:24 +00:00
|
|
|
|
2010-09-29 20:09:11 +00:00
|
|
|
vec3_t r_origin, r_matrix[3];
|
2003-03-18 19:48:24 +00:00
|
|
|
|
2010-09-11 10:03:41 +00:00
|
|
|
int t_width, t_height;
|
2010-09-29 20:09:11 +00:00
|
|
|
unsigned *t_data;
|
2010-09-11 10:03:41 +00:00
|
|
|
int t_widthmask, t_heightmask, t_widthshift;
|
|
|
|
float t_widthadd, t_heightadd;
|
2003-03-18 19:48:24 +00:00
|
|
|
|
2010-09-11 10:03:41 +00:00
|
|
|
int r_width, r_height;
|
2010-09-29 20:09:11 +00:00
|
|
|
float *r_zbuffer;
|
|
|
|
unsigned *r_picbuffer;
|
2003-03-18 19:48:24 +00:00
|
|
|
|
2010-09-29 20:09:11 +00:00
|
|
|
vec5_t rightside, leftside, rightstep, leftstep;
|
2003-03-18 19:48:24 +00:00
|
|
|
|
2010-09-29 20:09:11 +00:00
|
|
|
face_t *r_face;
|
2003-03-18 19:48:24 +00:00
|
|
|
|
2010-09-29 20:09:11 +00:00
|
|
|
BOOL r_drawflat;
|
2003-03-18 19:48:24 +00:00
|
|
|
|
2010-09-29 20:09:11 +00:00
|
|
|
pixel32_t r_flatcolor;
|
2003-03-18 19:48:24 +00:00
|
|
|
|
2010-09-29 20:09:11 +00:00
|
|
|
int sy[20];
|
2003-03-18 19:48:24 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
====================
|
|
|
|
REN_ClearBuffers
|
|
|
|
====================
|
|
|
|
*/
|
2010-09-11 10:03:41 +00:00
|
|
|
void
|
|
|
|
REN_ClearBuffers (void)
|
2003-03-18 19:48:24 +00:00
|
|
|
{
|
2010-09-29 20:09:11 +00:00
|
|
|
int size;
|
2010-09-11 10:03:41 +00:00
|
|
|
|
2011-04-16 09:58:05 +00:00
|
|
|
size = r_width * r_height;
|
2003-03-18 19:48:24 +00:00
|
|
|
|
2011-04-16 09:58:05 +00:00
|
|
|
memset (r_zbuffer, 0, size * sizeof (float));
|
|
|
|
memset (r_picbuffer, 0, size * sizeof (unsigned));
|
2003-03-18 19:48:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
====================
|
|
|
|
REN_SetTexture
|
|
|
|
====================
|
|
|
|
*/
|
2010-09-11 10:03:41 +00:00
|
|
|
void
|
|
|
|
REN_SetTexture (face_t * face)
|
2003-03-18 19:48:24 +00:00
|
|
|
{
|
2010-09-11 10:03:41 +00:00
|
|
|
int i;
|
|
|
|
int t_heightshift;
|
2010-09-29 20:09:11 +00:00
|
|
|
qtexture_t *q;
|
2003-03-18 19:48:24 +00:00
|
|
|
|
|
|
|
if (!face->qtexture)
|
2010-09-29 20:09:11 +00:00
|
|
|
face->qtexture = TEX_ForName (face->texture.texture); // try to load
|
2003-03-18 19:48:24 +00:00
|
|
|
q = face->qtexture;
|
2010-09-11 10:03:41 +00:00
|
|
|
|
2003-03-18 19:48:24 +00:00
|
|
|
t_width = q->width;
|
|
|
|
t_height = q->height;
|
|
|
|
t_data = q->data;
|
|
|
|
r_flatcolor = q->flatcolor;
|
|
|
|
|
|
|
|
r_flatcolor.chan[0] *= r_face->light;
|
|
|
|
r_flatcolor.chan[1] *= r_face->light;
|
|
|
|
r_flatcolor.chan[2] *= r_face->light;
|
2010-09-11 10:03:41 +00:00
|
|
|
|
|
|
|
t_widthadd = t_width * 1024;
|
|
|
|
t_heightadd = t_height * 1024;
|
|
|
|
|
|
|
|
t_widthmask = t_width - 1;
|
|
|
|
t_heightmask = t_height - 1;
|
|
|
|
|
2003-03-18 19:48:24 +00:00
|
|
|
t_widthshift = 0;
|
|
|
|
i = t_width;
|
2010-09-11 10:03:41 +00:00
|
|
|
while (i >= 2) {
|
2003-03-18 19:48:24 +00:00
|
|
|
t_widthshift++;
|
2010-09-11 10:03:41 +00:00
|
|
|
i >>= 1;
|
2003-03-18 19:48:24 +00:00
|
|
|
}
|
2010-09-11 10:03:41 +00:00
|
|
|
|
2003-03-18 19:48:24 +00:00
|
|
|
t_heightshift = 0;
|
|
|
|
i = t_width;
|
2010-09-11 10:03:41 +00:00
|
|
|
while (i >= 2) {
|
2003-03-18 19:48:24 +00:00
|
|
|
t_heightshift++;
|
2010-09-11 10:03:41 +00:00
|
|
|
i >>= 1;
|
2003-03-18 19:48:24 +00:00
|
|
|
}
|
2010-09-11 10:03:41 +00:00
|
|
|
|
|
|
|
if ((1 << t_widthshift) != t_width || (1 << t_heightshift) != t_height)
|
2010-09-29 20:09:11 +00:00
|
|
|
t_widthshift = t_heightshift = 0; // non power of two
|
2003-03-18 19:48:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
==================
|
|
|
|
REN_DrawSpan
|
|
|
|
==================
|
|
|
|
*/
|
2010-09-11 10:03:41 +00:00
|
|
|
void
|
|
|
|
REN_DrawSpan (int y)
|
2003-03-18 19:48:24 +00:00
|
|
|
{
|
2010-09-11 10:03:41 +00:00
|
|
|
int x, count;
|
|
|
|
int ofs;
|
|
|
|
int tx, ty;
|
|
|
|
int x1, x2;
|
|
|
|
float ufrac, vfrac, zfrac, lightfrac, ustep, vstep, zstep;
|
2010-09-29 20:09:11 +00:00
|
|
|
pixel32_t *in, *out;
|
2010-09-11 10:03:41 +00:00
|
|
|
float scale;
|
|
|
|
|
|
|
|
if (y < 0 || y >= r_height)
|
2003-03-18 19:48:24 +00:00
|
|
|
return;
|
2010-09-11 10:03:41 +00:00
|
|
|
|
2003-03-18 19:48:24 +00:00
|
|
|
x1 = (leftside[0]);
|
|
|
|
x2 = (rightside[0]);
|
|
|
|
|
|
|
|
count = x2 - x1;
|
|
|
|
if (count < 0)
|
|
|
|
return;
|
2010-09-11 10:03:41 +00:00
|
|
|
|
2003-03-18 19:48:24 +00:00
|
|
|
zfrac = leftside[2];
|
|
|
|
ufrac = leftside[3];
|
|
|
|
vfrac = leftside[4];
|
|
|
|
lightfrac = r_face->light;
|
2010-09-11 10:03:41 +00:00
|
|
|
|
2003-03-18 19:48:24 +00:00
|
|
|
if (!count)
|
|
|
|
scale = 1;
|
|
|
|
else
|
2010-09-11 10:03:41 +00:00
|
|
|
scale = 1.0 / count;
|
|
|
|
|
|
|
|
zstep = (rightside[2] - zfrac) * scale;
|
|
|
|
ustep = (rightside[3] - ufrac) * scale;
|
|
|
|
vstep = (rightside[4] - vfrac) * scale;
|
|
|
|
|
|
|
|
if (x1 < 0) {
|
|
|
|
ufrac -= x1 * ustep;
|
|
|
|
vfrac -= x1 * vstep;
|
|
|
|
zfrac -= x1 * zstep;
|
2003-03-18 19:48:24 +00:00
|
|
|
x1 = 0;
|
|
|
|
}
|
2010-09-11 10:03:41 +00:00
|
|
|
|
2003-03-18 19:48:24 +00:00
|
|
|
if (x2 > r_width)
|
|
|
|
x2 = r_width;
|
2010-09-11 10:03:41 +00:00
|
|
|
ofs = y * r_width + x1;
|
|
|
|
|
2010-10-11 12:11:33 +00:00
|
|
|
// this should be specialized for 1.0 / 0.5 / 0.75 light levels
|
2010-09-11 10:03:41 +00:00
|
|
|
for (x = x1; x < x2; x++) {
|
|
|
|
if (r_zbuffer[ofs] <= zfrac) {
|
|
|
|
scale = 1 / zfrac;
|
2003-03-18 19:48:24 +00:00
|
|
|
|
|
|
|
r_zbuffer[ofs] = zfrac;
|
|
|
|
|
2010-09-11 10:03:41 +00:00
|
|
|
if (t_widthshift) {
|
|
|
|
tx = (int) ((ufrac * scale)) & t_widthmask;
|
|
|
|
ty = (int) ((vfrac * scale)) & t_heightmask;
|
2010-09-29 20:09:11 +00:00
|
|
|
in = (pixel32_t *) &t_data[(ty << t_widthshift) + tx];
|
2010-09-11 10:03:41 +00:00
|
|
|
} else {
|
|
|
|
tx = (int) ((ufrac * scale) + t_widthadd) % t_width;
|
|
|
|
ty = (int) ((vfrac * scale) + t_heightadd) % t_height;
|
2010-09-29 20:09:11 +00:00
|
|
|
in = (pixel32_t *) &t_data[ty * t_width + tx];
|
2003-03-18 19:48:24 +00:00
|
|
|
}
|
2010-09-11 10:03:41 +00:00
|
|
|
|
2010-09-29 20:09:11 +00:00
|
|
|
out = (pixel32_t *) &r_picbuffer[ofs];
|
2003-03-18 19:48:24 +00:00
|
|
|
#ifdef NOLIGHT
|
|
|
|
*out = *in;
|
|
|
|
#else
|
2010-09-11 10:03:41 +00:00
|
|
|
out->chan[0] = in->chan[0] * lightfrac;
|
|
|
|
out->chan[1] = in->chan[1] * lightfrac;
|
|
|
|
out->chan[2] = in->chan[2] * lightfrac;
|
2003-03-18 19:48:24 +00:00
|
|
|
out->chan[3] = 0xff;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
ufrac += ustep;
|
|
|
|
vfrac += vstep;
|
|
|
|
zfrac += zstep;
|
|
|
|
ofs++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
==================
|
|
|
|
REN_DrawFlatSpan
|
|
|
|
==================
|
|
|
|
*/
|
2010-09-11 10:03:41 +00:00
|
|
|
void
|
|
|
|
REN_DrawFlatSpan (int y)
|
2003-03-18 19:48:24 +00:00
|
|
|
{
|
2010-09-29 20:09:11 +00:00
|
|
|
int x, count;
|
|
|
|
int ofs;
|
|
|
|
int x1, x2;
|
|
|
|
float zfrac, zstep;
|
|
|
|
int *out;
|
2010-09-11 10:03:41 +00:00
|
|
|
|
|
|
|
if (y < 0 || y >= r_height)
|
2003-03-18 19:48:24 +00:00
|
|
|
return;
|
2010-09-11 10:03:41 +00:00
|
|
|
|
2003-03-18 19:48:24 +00:00
|
|
|
x1 = (leftside[0]);
|
|
|
|
x2 = (rightside[0]);
|
|
|
|
|
|
|
|
count = x2 - x1;
|
|
|
|
if (count < 0)
|
|
|
|
return;
|
2010-09-11 10:03:41 +00:00
|
|
|
|
2003-03-18 19:48:24 +00:00
|
|
|
zfrac = leftside[2];
|
2010-09-11 10:03:41 +00:00
|
|
|
|
|
|
|
zstep = (rightside[2] - zfrac) / count;
|
|
|
|
|
|
|
|
if (x1 < 0) {
|
|
|
|
zfrac -= x1 * zstep;
|
2003-03-18 19:48:24 +00:00
|
|
|
x1 = 0;
|
|
|
|
}
|
2010-09-11 10:03:41 +00:00
|
|
|
|
2003-03-18 19:48:24 +00:00
|
|
|
if (x2 > r_width)
|
|
|
|
x2 = r_width;
|
|
|
|
|
2010-09-11 10:03:41 +00:00
|
|
|
ofs = y * r_width + x1;
|
|
|
|
|
2010-10-11 12:11:33 +00:00
|
|
|
// this should be specialized for 1.0 / 0.5 / 0.75 light levels
|
2010-09-11 10:03:41 +00:00
|
|
|
for (x = x1; x < x2; x++) {
|
|
|
|
if (r_zbuffer[ofs] <= zfrac) {
|
2003-03-18 19:48:24 +00:00
|
|
|
r_zbuffer[ofs] = zfrac;
|
2010-09-11 10:03:41 +00:00
|
|
|
out = (int *) &r_picbuffer[ofs];
|
2003-03-18 19:48:24 +00:00
|
|
|
*out = r_flatcolor.p;
|
|
|
|
}
|
|
|
|
zfrac += zstep;
|
|
|
|
ofs++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
=====================
|
|
|
|
REN_RasterizeFace
|
|
|
|
|
|
|
|
=====================
|
|
|
|
*/
|
2010-09-11 10:03:41 +00:00
|
|
|
void
|
|
|
|
REN_RasterizeFace (winding_t * w)
|
2003-03-18 19:48:24 +00:00
|
|
|
{
|
2010-09-29 20:09:11 +00:00
|
|
|
int y;
|
|
|
|
int i;
|
|
|
|
int top, bot;
|
|
|
|
int leftv, rightv;
|
|
|
|
int count;
|
|
|
|
int numvertex;
|
2003-03-18 19:48:24 +00:00
|
|
|
|
2010-10-11 12:11:33 +00:00
|
|
|
// find top vertex
|
2003-03-18 19:48:24 +00:00
|
|
|
numvertex = w->numpoints;
|
|
|
|
top = 0x7fffffff;
|
|
|
|
bot = 0x80000000;
|
|
|
|
leftv = 0;
|
2010-09-11 10:03:41 +00:00
|
|
|
|
|
|
|
for (i = 0; i < numvertex; i++) {
|
2003-03-18 19:48:24 +00:00
|
|
|
w->points[i][3] *= w->points[i][2];
|
|
|
|
w->points[i][4] *= w->points[i][2];
|
|
|
|
|
2010-09-11 10:03:41 +00:00
|
|
|
sy[i] = (int) w->points[i][1];
|
|
|
|
|
|
|
|
if (sy[i] < top) {
|
2003-03-18 19:48:24 +00:00
|
|
|
top = sy[i];
|
|
|
|
leftv = i;
|
|
|
|
}
|
|
|
|
if (sy[i] > bot)
|
|
|
|
bot = sy[i];
|
|
|
|
}
|
2010-09-11 10:03:41 +00:00
|
|
|
rightv = leftv;
|
|
|
|
|
2003-03-18 19:48:24 +00:00
|
|
|
if (top < 0 || bot > r_height || top > bot)
|
2010-09-29 20:09:11 +00:00
|
|
|
return; // shouldn't have to have this...
|
2010-09-11 10:03:41 +00:00
|
|
|
|
2010-10-11 12:11:33 +00:00
|
|
|
// render a trapezoid
|
2003-03-18 19:48:24 +00:00
|
|
|
y = top;
|
2010-09-11 10:03:41 +00:00
|
|
|
|
|
|
|
while (y < bot) {
|
|
|
|
if (y >= sy[leftv]) {
|
|
|
|
do {
|
|
|
|
for (i = 0; i < 5; i++)
|
2003-03-18 19:48:24 +00:00
|
|
|
leftside[i] = w->points[leftv][i];
|
2010-09-29 20:09:11 +00:00
|
|
|
|
2003-03-18 19:48:24 +00:00
|
|
|
leftv--;
|
|
|
|
if (leftv == -1)
|
2010-09-11 10:03:41 +00:00
|
|
|
leftv = numvertex - 1;
|
2003-03-18 19:48:24 +00:00
|
|
|
} while (sy[leftv] <= y);
|
2010-09-11 10:03:41 +00:00
|
|
|
count = sy[leftv] - y;
|
|
|
|
for (i = 0; i < 5; i++)
|
|
|
|
leftstep[i] = (w->points[leftv][i] - leftside[i]) / count;
|
2003-03-18 19:48:24 +00:00
|
|
|
}
|
2010-09-11 10:03:41 +00:00
|
|
|
if (y >= sy[rightv]) {
|
|
|
|
do {
|
|
|
|
for (i = 0; i < 5; i++)
|
2003-03-18 19:48:24 +00:00
|
|
|
rightside[i] = w->points[rightv][i];
|
2010-09-29 20:09:11 +00:00
|
|
|
|
2003-03-18 19:48:24 +00:00
|
|
|
rightv++;
|
|
|
|
if (rightv == numvertex)
|
|
|
|
rightv = 0;
|
|
|
|
} while (sy[rightv] <= y);
|
2010-09-11 10:03:41 +00:00
|
|
|
count = sy[rightv] - y;
|
|
|
|
for (i = 0; i < 5; i++)
|
|
|
|
rightstep[i] = (w->points[rightv][i] - rightside[i]) / count;
|
2003-03-18 19:48:24 +00:00
|
|
|
}
|
2010-09-11 10:03:41 +00:00
|
|
|
|
2003-03-18 19:48:24 +00:00
|
|
|
if (r_drawflat)
|
|
|
|
REN_DrawFlatSpan (y);
|
|
|
|
else
|
|
|
|
REN_DrawSpan (y);
|
2010-09-11 10:03:41 +00:00
|
|
|
|
|
|
|
for (i = 0; i < 5; i++) {
|
2003-03-18 19:48:24 +00:00
|
|
|
leftside[i] += leftstep[i];
|
|
|
|
rightside[i] += rightstep[i];
|
|
|
|
}
|
2010-09-11 10:03:41 +00:00
|
|
|
|
2003-03-18 19:48:24 +00:00
|
|
|
y++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-29 20:09:11 +00:00
|
|
|
// =============================================================================
|
2003-03-18 19:48:24 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
==================
|
|
|
|
REN_DrawSpanLinear
|
|
|
|
==================
|
|
|
|
*/
|
2010-09-11 10:03:41 +00:00
|
|
|
void
|
|
|
|
REN_DrawSpanLinear (int y)
|
2003-03-18 19:48:24 +00:00
|
|
|
{
|
2010-09-11 10:03:41 +00:00
|
|
|
int x, count;
|
|
|
|
int ofs;
|
|
|
|
int tx, ty;
|
|
|
|
int x1, x2;
|
|
|
|
float ufrac, vfrac, zfrac, ustep, vstep, zstep;
|
2010-09-29 20:09:11 +00:00
|
|
|
pixel32_t *in, *out;
|
2010-09-11 10:03:41 +00:00
|
|
|
float scale;
|
|
|
|
|
|
|
|
if (y < 0 || y >= r_height)
|
2003-03-18 19:48:24 +00:00
|
|
|
return;
|
2010-09-11 10:03:41 +00:00
|
|
|
|
2003-03-18 19:48:24 +00:00
|
|
|
x1 = (leftside[0]);
|
|
|
|
x2 = (rightside[0]);
|
|
|
|
|
|
|
|
count = x2 - x1;
|
|
|
|
if (count < 0)
|
|
|
|
return;
|
2010-09-11 10:03:41 +00:00
|
|
|
|
2003-03-18 19:48:24 +00:00
|
|
|
zfrac = leftside[2];
|
|
|
|
ufrac = leftside[3];
|
|
|
|
vfrac = leftside[4];
|
2010-09-11 10:03:41 +00:00
|
|
|
|
2003-03-18 19:48:24 +00:00
|
|
|
if (!count)
|
|
|
|
scale = 1;
|
|
|
|
else
|
2010-09-11 10:03:41 +00:00
|
|
|
scale = 1.0 / count;
|
|
|
|
|
|
|
|
zstep = (rightside[2] - zfrac) * scale;
|
|
|
|
ustep = (rightside[3] - ufrac) * scale;
|
|
|
|
vstep = (rightside[4] - vfrac) * scale;
|
|
|
|
|
|
|
|
if (x1 < 0) {
|
|
|
|
ufrac -= x1 * ustep;
|
|
|
|
vfrac -= x1 * vstep;
|
|
|
|
zfrac -= x1 * zstep;
|
2003-03-18 19:48:24 +00:00
|
|
|
x1 = 0;
|
|
|
|
}
|
2010-09-11 10:03:41 +00:00
|
|
|
|
2003-03-18 19:48:24 +00:00
|
|
|
if (x2 > r_width)
|
|
|
|
x2 = r_width;
|
|
|
|
|
2010-09-11 10:03:41 +00:00
|
|
|
ofs = y * r_width + x1;
|
|
|
|
|
|
|
|
for (x = x1; x < x2; x++) {
|
|
|
|
if (r_zbuffer[ofs] <= zfrac) {
|
2003-03-18 19:48:24 +00:00
|
|
|
r_zbuffer[ofs] = zfrac;
|
|
|
|
|
2010-09-11 10:03:41 +00:00
|
|
|
if (t_widthshift) {
|
|
|
|
tx = (int) ufrac & t_widthmask;
|
|
|
|
ty = (int) vfrac & t_heightmask;
|
2010-09-29 20:09:11 +00:00
|
|
|
in = (pixel32_t *) &t_data[(ty << t_widthshift) + tx];
|
2010-09-11 10:03:41 +00:00
|
|
|
} else {
|
|
|
|
tx = (int) (ufrac + t_widthadd) % t_width;
|
|
|
|
ty = (int) (vfrac + t_heightadd) % t_height;
|
2010-09-29 20:09:11 +00:00
|
|
|
in = (pixel32_t *) &t_data[ty * t_width + tx];
|
2003-03-18 19:48:24 +00:00
|
|
|
}
|
2010-09-11 10:03:41 +00:00
|
|
|
|
2010-09-29 20:09:11 +00:00
|
|
|
out = (pixel32_t *) &r_picbuffer[ofs];
|
2003-03-18 19:48:24 +00:00
|
|
|
*out = *in;
|
|
|
|
}
|
|
|
|
ufrac += ustep;
|
|
|
|
vfrac += vstep;
|
|
|
|
zfrac += zstep;
|
|
|
|
ofs++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
=====================
|
|
|
|
REN_RasterizeFaceLinear
|
|
|
|
|
|
|
|
=====================
|
|
|
|
*/
|
2010-09-11 10:03:41 +00:00
|
|
|
void
|
|
|
|
REN_RasterizeFaceLinear (winding_t * w)
|
2003-03-18 19:48:24 +00:00
|
|
|
{
|
2010-09-29 20:09:11 +00:00
|
|
|
int y;
|
|
|
|
int i;
|
|
|
|
int top, bot;
|
|
|
|
int leftv, rightv;
|
|
|
|
int count;
|
|
|
|
int numvertex;
|
2010-09-11 10:03:41 +00:00
|
|
|
|
2010-10-11 12:11:33 +00:00
|
|
|
// find top vertex
|
2003-03-18 19:48:24 +00:00
|
|
|
numvertex = w->numpoints;
|
|
|
|
top = 0x7fffffff;
|
|
|
|
bot = 0x80000000;
|
|
|
|
|
|
|
|
leftv = 0;
|
2010-09-11 10:03:41 +00:00
|
|
|
for (i = 0; i < numvertex; i++) {
|
|
|
|
sy[i] = (int) w->points[i][1];
|
|
|
|
|
|
|
|
if (sy[i] < top) {
|
2003-03-18 19:48:24 +00:00
|
|
|
top = sy[i];
|
|
|
|
leftv = i;
|
|
|
|
}
|
|
|
|
if (sy[i] > bot)
|
|
|
|
bot = sy[i];
|
|
|
|
}
|
2010-09-11 10:03:41 +00:00
|
|
|
rightv = leftv;
|
|
|
|
|
2003-03-18 19:48:24 +00:00
|
|
|
if (top < 0 || bot > r_height || top > bot)
|
2010-09-29 20:09:11 +00:00
|
|
|
return; // shouldn't have to have this...
|
2010-09-11 10:03:41 +00:00
|
|
|
|
2010-10-11 12:11:33 +00:00
|
|
|
// render a trapezoid
|
2003-03-18 19:48:24 +00:00
|
|
|
y = top;
|
2010-09-11 10:03:41 +00:00
|
|
|
|
|
|
|
while (y < bot) {
|
|
|
|
if (y >= sy[leftv]) {
|
|
|
|
do {
|
|
|
|
for (i = 0; i < 5; i++)
|
2003-03-18 19:48:24 +00:00
|
|
|
leftside[i] = w->points[leftv][i];
|
2010-09-29 20:09:11 +00:00
|
|
|
|
2003-03-18 19:48:24 +00:00
|
|
|
leftv--;
|
|
|
|
if (leftv == -1)
|
2010-09-11 10:03:41 +00:00
|
|
|
leftv = numvertex - 1;
|
2003-03-18 19:48:24 +00:00
|
|
|
} while (sy[leftv] <= y);
|
2010-09-11 10:03:41 +00:00
|
|
|
count = sy[leftv] - y;
|
|
|
|
for (i = 0; i < 5; i++)
|
|
|
|
leftstep[i] = (w->points[leftv][i] - leftside[i]) / count;
|
2003-03-18 19:48:24 +00:00
|
|
|
}
|
2010-09-11 10:03:41 +00:00
|
|
|
if (y >= sy[rightv]) {
|
|
|
|
do {
|
|
|
|
for (i = 0; i < 5; i++)
|
2003-03-18 19:48:24 +00:00
|
|
|
rightside[i] = w->points[rightv][i];
|
2010-09-29 20:09:11 +00:00
|
|
|
|
2003-03-18 19:48:24 +00:00
|
|
|
rightv++;
|
|
|
|
if (rightv == numvertex)
|
|
|
|
rightv = 0;
|
|
|
|
} while (sy[rightv] <= y);
|
2010-09-11 10:03:41 +00:00
|
|
|
count = sy[rightv] - y;
|
|
|
|
for (i = 0; i < 5; i++)
|
|
|
|
rightstep[i] = (w->points[rightv][i] - rightside[i]) / count;
|
2003-03-18 19:48:24 +00:00
|
|
|
}
|
2010-09-11 10:03:41 +00:00
|
|
|
|
2003-03-18 19:48:24 +00:00
|
|
|
REN_DrawSpanLinear (y);
|
2010-09-11 10:03:41 +00:00
|
|
|
|
|
|
|
for (i = 0; i < 5; i++) {
|
2003-03-18 19:48:24 +00:00
|
|
|
leftside[i] += leftstep[i];
|
|
|
|
rightside[i] += rightstep[i];
|
|
|
|
}
|
2010-09-11 10:03:41 +00:00
|
|
|
|
2003-03-18 19:48:24 +00:00
|
|
|
y++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-29 20:09:11 +00:00
|
|
|
// ============================================================================
|
2003-03-18 19:48:24 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
==================
|
|
|
|
REN_BeginCamera
|
|
|
|
===================
|
|
|
|
*/
|
2010-09-11 10:03:41 +00:00
|
|
|
float r_width_2, r_height_3;
|
|
|
|
plane_t rfrustum[5];
|
2003-03-18 19:48:24 +00:00
|
|
|
|
2010-09-11 10:03:41 +00:00
|
|
|
void
|
|
|
|
REN_BeginCamera (void)
|
2003-03-18 19:48:24 +00:00
|
|
|
{
|
2010-09-11 10:03:41 +00:00
|
|
|
r_width_2 = (float) r_width / 2;
|
|
|
|
r_height_3 = (float) r_height / 3;
|
|
|
|
|
2010-09-29 20:09:11 +00:00
|
|
|
// clip to right side
|
2010-09-09 11:46:38 +00:00
|
|
|
rfrustum[0].normal[0] = -1;
|
|
|
|
rfrustum[0].normal[1] = 0;
|
|
|
|
rfrustum[0].normal[2] = 1;
|
|
|
|
rfrustum[0].dist = 0;
|
2003-03-18 19:48:24 +00:00
|
|
|
|
2010-09-29 20:09:11 +00:00
|
|
|
// clip to left side
|
2010-09-09 11:46:38 +00:00
|
|
|
rfrustum[1].normal[0] = 1;
|
|
|
|
rfrustum[1].normal[1] = 0;
|
|
|
|
rfrustum[1].normal[2] = 1;
|
|
|
|
rfrustum[1].dist = 0;
|
2003-03-18 19:48:24 +00:00
|
|
|
|
2010-09-29 20:09:11 +00:00
|
|
|
// clip to top side
|
2010-09-09 11:46:38 +00:00
|
|
|
rfrustum[2].normal[0] = 0;
|
|
|
|
rfrustum[2].normal[1] = -1;
|
|
|
|
rfrustum[2].normal[2] = r_height_3 / r_width_2;
|
|
|
|
rfrustum[2].dist = 0;
|
2003-03-18 19:48:24 +00:00
|
|
|
|
2010-09-29 20:09:11 +00:00
|
|
|
// clip to bottom side
|
2010-09-09 11:46:38 +00:00
|
|
|
rfrustum[3].normal[0] = 0;
|
|
|
|
rfrustum[3].normal[1] = 1;
|
2010-09-11 10:03:41 +00:00
|
|
|
rfrustum[3].normal[2] = 2 * r_height_3 / r_width_2;
|
2010-09-09 11:46:38 +00:00
|
|
|
rfrustum[3].dist = 0;
|
2003-03-18 19:48:24 +00:00
|
|
|
|
2010-09-29 20:09:11 +00:00
|
|
|
// near Z
|
2010-09-09 11:46:38 +00:00
|
|
|
rfrustum[4].normal[0] = 0;
|
|
|
|
rfrustum[4].normal[1] = 0;
|
|
|
|
rfrustum[4].normal[2] = 1;
|
|
|
|
rfrustum[4].dist = 1;
|
2003-03-18 19:48:24 +00:00
|
|
|
}
|
|
|
|
|
2010-09-11 10:03:41 +00:00
|
|
|
void
|
|
|
|
REN_BeginXY (void)
|
2003-03-18 19:48:24 +00:00
|
|
|
{
|
2010-09-09 11:46:38 +00:00
|
|
|
rfrustum[0].normal[0] = 1;
|
|
|
|
rfrustum[0].normal[1] = 0;
|
|
|
|
rfrustum[0].normal[2] = 0;
|
|
|
|
rfrustum[0].dist = 0;
|
2010-09-11 10:03:41 +00:00
|
|
|
|
2010-09-09 11:46:38 +00:00
|
|
|
rfrustum[1].normal[0] = -1;
|
|
|
|
rfrustum[1].normal[1] = 0;
|
|
|
|
rfrustum[1].normal[2] = 0;
|
|
|
|
rfrustum[1].dist = -r_width;
|
|
|
|
|
|
|
|
rfrustum[2].normal[0] = 0;
|
|
|
|
rfrustum[2].normal[1] = 1;
|
|
|
|
rfrustum[2].normal[2] = 0;
|
|
|
|
rfrustum[2].dist = 0;
|
|
|
|
|
|
|
|
rfrustum[3].normal[0] = 0;
|
|
|
|
rfrustum[3].normal[1] = -1;
|
|
|
|
rfrustum[3].normal[2] = 0;
|
|
|
|
rfrustum[3].dist = -r_height;
|
2003-03-18 19:48:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
=====================
|
|
|
|
REN_DrawCameraFace
|
|
|
|
=====================
|
|
|
|
*/
|
2010-09-11 10:03:41 +00:00
|
|
|
void
|
|
|
|
REN_DrawCameraFace (face_t * idpol)
|
2003-03-18 19:48:24 +00:00
|
|
|
{
|
2010-09-11 10:03:41 +00:00
|
|
|
int i;
|
|
|
|
float scale;
|
|
|
|
int numvertex;
|
2010-09-29 20:09:11 +00:00
|
|
|
winding_t *w, *in;
|
2010-09-11 10:03:41 +00:00
|
|
|
vec3_t temp;
|
|
|
|
|
2003-03-18 19:48:24 +00:00
|
|
|
if (!idpol->w)
|
2010-09-29 20:09:11 +00:00
|
|
|
return; // overconstrained plane
|
2010-09-11 10:03:41 +00:00
|
|
|
|
2003-03-18 19:48:24 +00:00
|
|
|
r_face = idpol;
|
|
|
|
|
2010-10-11 12:11:33 +00:00
|
|
|
// back face cull
|
2003-03-18 19:48:24 +00:00
|
|
|
if (DotProduct (r_origin, idpol->plane.normal) <= idpol->plane.dist)
|
|
|
|
return;
|
|
|
|
|
2010-10-11 12:11:33 +00:00
|
|
|
// transform in 3D (FIXME: clip first, then transform)
|
2003-03-18 19:48:24 +00:00
|
|
|
in = idpol->w;
|
|
|
|
numvertex = in->numpoints;
|
|
|
|
|
|
|
|
w = NewWinding (numvertex);
|
|
|
|
w->numpoints = numvertex;
|
2010-09-11 10:03:41 +00:00
|
|
|
for (i = 0; i < numvertex; i++) {
|
2003-03-18 19:48:24 +00:00
|
|
|
VectorSubtract (in->points[i], r_origin, temp);
|
2010-09-11 10:03:41 +00:00
|
|
|
|
|
|
|
w->points[i][0] = DotProduct (temp, r_matrix[0]);
|
|
|
|
w->points[i][1] = DotProduct (temp, r_matrix[1]);
|
|
|
|
w->points[i][2] = DotProduct (temp, r_matrix[2]);
|
2003-03-18 19:48:24 +00:00
|
|
|
|
|
|
|
w->points[i][3] = in->points[i][3];
|
|
|
|
w->points[i][4] = in->points[i][4];
|
|
|
|
}
|
2010-09-11 10:03:41 +00:00
|
|
|
|
2010-10-11 12:11:33 +00:00
|
|
|
// 3D clip
|
2010-09-11 10:03:41 +00:00
|
|
|
for (i = 0; i < 4; i++) {
|
2010-09-09 11:46:38 +00:00
|
|
|
w = ClipWinding (w, &rfrustum[i]);
|
2003-03-18 19:48:24 +00:00
|
|
|
if (!w)
|
|
|
|
return;
|
|
|
|
}
|
2010-09-11 10:03:41 +00:00
|
|
|
|
2010-10-11 12:11:33 +00:00
|
|
|
// project to 2D
|
2010-09-11 10:03:41 +00:00
|
|
|
for (i = 0; i < w->numpoints; i++) {
|
2003-03-18 19:48:24 +00:00
|
|
|
scale = r_width_2 / w->points[i][2];
|
2010-09-11 10:03:41 +00:00
|
|
|
w->points[i][0] = r_width_2 + scale * w->points[i][0];
|
|
|
|
w->points[i][1] = r_height_3 - scale * w->points[i][1];
|
2003-03-18 19:48:24 +00:00
|
|
|
w->points[i][2] = scale;
|
|
|
|
}
|
2010-09-11 10:03:41 +00:00
|
|
|
|
2010-10-11 12:11:33 +00:00
|
|
|
// draw it
|
2003-03-18 19:48:24 +00:00
|
|
|
REN_SetTexture (idpol);
|
2010-09-11 10:03:41 +00:00
|
|
|
|
2003-03-18 19:48:24 +00:00
|
|
|
REN_RasterizeFace (w);
|
|
|
|
free (w);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
=====================
|
|
|
|
REN_DrawXYFace
|
|
|
|
=====================
|
|
|
|
*/
|
2010-09-11 10:03:41 +00:00
|
|
|
void
|
|
|
|
REN_DrawXYFace (face_t * idpol)
|
2003-03-18 19:48:24 +00:00
|
|
|
{
|
2010-09-11 10:03:41 +00:00
|
|
|
int i, j, numvertex;
|
2010-09-29 20:09:11 +00:00
|
|
|
winding_t *w, *in;
|
|
|
|
float *dest, *source;
|
2010-09-11 10:03:41 +00:00
|
|
|
float temp;
|
|
|
|
|
2003-03-18 19:48:24 +00:00
|
|
|
if (!idpol->w)
|
2010-09-29 20:09:11 +00:00
|
|
|
return; // overconstrained plane
|
2010-09-11 10:03:41 +00:00
|
|
|
|
2010-09-29 20:09:11 +00:00
|
|
|
w = idpol->w;
|
2003-03-18 19:48:24 +00:00
|
|
|
r_face = idpol;
|
2010-09-11 10:03:41 +00:00
|
|
|
|
2010-10-11 12:11:33 +00:00
|
|
|
// back (and side) face cull
|
2003-03-18 19:48:24 +00:00
|
|
|
if (DotProduct (idpol->plane.normal, xy_viewnormal) > -VECTOR_EPSILON)
|
|
|
|
return;
|
|
|
|
|
2010-10-11 12:11:33 +00:00
|
|
|
// transform
|
2003-03-18 19:48:24 +00:00
|
|
|
in = idpol->w;
|
|
|
|
numvertex = in->numpoints;
|
|
|
|
|
|
|
|
w = NewWinding (numvertex);
|
|
|
|
w->numpoints = numvertex;
|
|
|
|
|
2010-09-11 10:03:41 +00:00
|
|
|
for (i = 0; i < numvertex; i++) {
|
|
|
|
// using Z as a scale for the 2D projection
|
|
|
|
w->points[i][0] = (in->points[i][0] - r_origin[0]) * r_origin[2];
|
|
|
|
w->points[i][1] =
|
2010-09-29 20:09:11 +00:00
|
|
|
r_height - (in->points[i][1] - r_origin[1]) * r_origin[2];
|
2003-03-18 19:48:24 +00:00
|
|
|
w->points[i][2] = in->points[i][2] + 3000;
|
|
|
|
w->points[i][3] = in->points[i][3];
|
|
|
|
w->points[i][4] = in->points[i][4];
|
|
|
|
}
|
2010-09-11 10:03:41 +00:00
|
|
|
|
2010-10-11 12:11:33 +00:00
|
|
|
// clip
|
2010-09-11 10:03:41 +00:00
|
|
|
for (i = 0; i < 4; i++) {
|
2010-09-09 11:46:38 +00:00
|
|
|
w = ClipWinding (w, &rfrustum[i]);
|
2003-03-18 19:48:24 +00:00
|
|
|
if (!w)
|
|
|
|
return;
|
|
|
|
}
|
2010-09-11 10:03:41 +00:00
|
|
|
|
2010-10-11 12:11:33 +00:00
|
|
|
// project to 2D
|
2010-09-11 10:03:41 +00:00
|
|
|
for (i = 0; i < w->numpoints; i++) {
|
2003-03-18 19:48:24 +00:00
|
|
|
dest = w->points[i];
|
|
|
|
if (dest[0] < 0)
|
|
|
|
dest[0] = 0;
|
|
|
|
if (dest[0] > r_width)
|
|
|
|
dest[0] = r_width;
|
|
|
|
if (dest[1] < 0)
|
|
|
|
dest[1] = 0;
|
|
|
|
if (dest[1] > r_height)
|
|
|
|
dest[1] = r_height;
|
|
|
|
if (xy_viewnormal[2] > 0)
|
2010-09-11 10:03:41 +00:00
|
|
|
dest[2] = 4096 - dest[2];
|
2003-03-18 19:48:24 +00:00
|
|
|
}
|
|
|
|
|
2010-09-29 20:09:11 +00:00
|
|
|
if (xy_viewnormal[2] > 0) { // flip order when upside down
|
2010-09-11 10:03:41 +00:00
|
|
|
for (i = 0; i < w->numpoints / 2; i++) {
|
2003-03-18 19:48:24 +00:00
|
|
|
dest = w->points[i];
|
2010-09-11 10:03:41 +00:00
|
|
|
source = w->points[w->numpoints - 1 - i];
|
|
|
|
for (j = 0; j < 5; j++) {
|
2003-03-18 19:48:24 +00:00
|
|
|
temp = dest[j];
|
|
|
|
dest[j] = source[j];
|
|
|
|
source[j] = temp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-09-11 10:03:41 +00:00
|
|
|
|
2003-03-18 19:48:24 +00:00
|
|
|
REN_SetTexture (idpol);
|
2010-09-11 10:03:41 +00:00
|
|
|
|
2010-10-11 12:11:33 +00:00
|
|
|
// draw it
|
2003-03-18 19:48:24 +00:00
|
|
|
REN_RasterizeFaceLinear (w);
|
|
|
|
free (w);
|
|
|
|
}
|