2001-02-19 21:15:25 +00:00
|
|
|
/*
|
2001-08-25 02:47:11 +00:00
|
|
|
sw_redge.c
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-05-15 04:50:53 +00:00
|
|
|
(description)
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
Copyright (C) 1996-1997 Id Software, Inc.
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License
|
|
|
|
as published by the Free Software Foundation; either version 2
|
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
|
|
|
|
See the GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to:
|
|
|
|
|
|
|
|
Free Software Foundation, Inc.
|
|
|
|
59 Temple Place - Suite 330
|
|
|
|
Boston, MA 02111-1307, USA
|
|
|
|
|
|
|
|
*/
|
2001-09-28 06:26:31 +00:00
|
|
|
static const char rcsid[] =
|
|
|
|
"$Id$";
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2001-08-23 00:39:22 +00:00
|
|
|
#include "QF/render.h"
|
2001-05-16 17:20:42 +00:00
|
|
|
#include "QF/sound.h"
|
|
|
|
|
|
|
|
#include "d_ifacea.h"
|
2001-02-19 21:15:25 +00:00
|
|
|
#include "r_local.h"
|
|
|
|
|
2001-08-23 00:39:22 +00:00
|
|
|
/*
|
|
|
|
FIXME
|
|
|
|
the complex cases add new polys on most lines, so dont optimize for
|
|
|
|
keeping them the same have multiple free span lists to try to get better
|
|
|
|
coherence ? low depth complexity-- 1 to 3 or so this breaks spans at every
|
|
|
|
edge, even hidden ones (bad)
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-08-28 22:46:02 +00:00
|
|
|
have a sentinal at both ends?
|
2001-08-23 00:39:22 +00:00
|
|
|
*/
|
2001-05-16 17:20:42 +00:00
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
edge_t *auxedges;
|
|
|
|
edge_t *r_edges, *edge_p, *edge_max;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
surf_t *surfaces, *surface_p, *surf_max;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-08-28 22:46:02 +00:00
|
|
|
/*
|
|
|
|
surfaces are generated in back to front order by the bsp, so if a surf
|
|
|
|
pointer is greater than another one, it should be drawn in front
|
|
|
|
surfaces[1] is the background, and is used as the active surface stack
|
|
|
|
*/
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
edge_t *newedges[MAXHEIGHT];
|
|
|
|
edge_t *removeedges[MAXHEIGHT];
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
espan_t *span_p, *max_span_p;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
int r_currentkey;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
extern int screenwidth;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
int current_iv;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
int edge_head_u_shift20, edge_tail_u_shift20;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
static void (*pdrawfunc) (void);
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
edge_t edge_head;
|
|
|
|
edge_t edge_tail;
|
|
|
|
edge_t edge_aftertail;
|
|
|
|
edge_t edge_sentinel;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
float fv;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-05-15 21:13:07 +00:00
|
|
|
void R_GenerateSpans (void);
|
|
|
|
void R_GenerateSpansBackward (void);
|
|
|
|
void R_LeadingEdge (edge_t *edge);
|
|
|
|
void R_LeadingEdgeBackwards (edge_t *edge);
|
|
|
|
void R_TrailingEdge (surf_t *surf, edge_t *edge);
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
void
|
|
|
|
R_DrawCulledPolys (void)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2001-02-26 06:48:02 +00:00
|
|
|
surf_t *s;
|
|
|
|
msurface_t *pface;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-05-18 17:50:19 +00:00
|
|
|
currententity = &r_worldentity;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
if (r_worldpolysbacktofront) {
|
|
|
|
for (s = surface_p - 1; s > &surfaces[1]; s--) {
|
2001-02-19 21:15:25 +00:00
|
|
|
if (!s->spans)
|
|
|
|
continue;
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
if (!(s->flags & SURF_DRAWBACKGROUND)) {
|
|
|
|
pface = (msurface_t *) s->data;
|
2001-02-19 21:15:25 +00:00
|
|
|
R_RenderPoly (pface, 15);
|
|
|
|
}
|
|
|
|
}
|
2001-02-26 06:48:02 +00:00
|
|
|
} else {
|
|
|
|
for (s = &surfaces[1]; s < surface_p; s++) {
|
2001-02-19 21:15:25 +00:00
|
|
|
if (!s->spans)
|
|
|
|
continue;
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
if (!(s->flags & SURF_DRAWBACKGROUND)) {
|
|
|
|
pface = (msurface_t *) s->data;
|
2001-02-19 21:15:25 +00:00
|
|
|
R_RenderPoly (pface, 15);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
void
|
|
|
|
R_BeginEdgeFrame (void)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2001-02-26 06:48:02 +00:00
|
|
|
int v;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
edge_p = r_edges;
|
|
|
|
edge_max = &r_edges[r_numallocatededges];
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
surface_p = &surfaces[2]; // background is surface 1,
|
2001-05-16 17:20:42 +00:00
|
|
|
// surface 0 is a dummy
|
2001-02-26 06:48:02 +00:00
|
|
|
surfaces[1].spans = NULL; // no background spans yet
|
2001-02-19 21:15:25 +00:00
|
|
|
surfaces[1].flags = SURF_DRAWBACKGROUND;
|
|
|
|
|
2001-05-15 21:13:07 +00:00
|
|
|
// put the background behind everything in the world
|
2001-05-16 06:01:34 +00:00
|
|
|
pdrawfunc = R_GenerateSpans;
|
|
|
|
surfaces[1].key = 0x7FFFFFFF;
|
|
|
|
r_currentkey = 0;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-08-23 00:39:22 +00:00
|
|
|
// FIXME: set with memset
|
2001-02-26 06:48:02 +00:00
|
|
|
for (v = r_refdef.vrect.y; v < r_refdef.vrectbottom; v++) {
|
2001-02-19 21:15:25 +00:00
|
|
|
newedges[v] = removeedges[v] = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-06-11 20:59:48 +00:00
|
|
|
#ifdef PIC
|
2001-08-23 00:39:22 +00:00
|
|
|
# undef USE_INTEL_ASM //XXX asm pic hack
|
2001-06-11 20:59:48 +00:00
|
|
|
#endif
|
2001-05-25 14:57:31 +00:00
|
|
|
|
2001-05-15 04:50:53 +00:00
|
|
|
#ifndef USE_INTEL_ASM
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
/*
|
2001-05-15 04:50:53 +00:00
|
|
|
R_InsertNewEdges
|
|
|
|
|
|
|
|
Adds the edges in the linked list edgestoadd, adding them to the edges
|
|
|
|
in the linked list edgelist. edgestoadd is assumed to be sorted on u,
|
|
|
|
and non-empty (this is actually newedges[v]). edgelist is assumed to
|
|
|
|
be sorted on u, with a sentinel at the end (actually, this is the
|
|
|
|
active edge table starting at edge_head.next).
|
2001-02-19 21:15:25 +00:00
|
|
|
*/
|
2001-02-26 06:48:02 +00:00
|
|
|
void
|
|
|
|
R_InsertNewEdges (edge_t *edgestoadd, edge_t *edgelist)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2001-02-26 06:48:02 +00:00
|
|
|
edge_t *next_edge;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
do {
|
2001-02-19 21:15:25 +00:00
|
|
|
next_edge = edgestoadd->next;
|
2001-08-28 22:46:02 +00:00
|
|
|
edgesearch:
|
2001-02-19 21:15:25 +00:00
|
|
|
if (edgelist->u >= edgestoadd->u)
|
|
|
|
goto addedge;
|
2001-02-26 06:48:02 +00:00
|
|
|
edgelist = edgelist->next;
|
2001-02-19 21:15:25 +00:00
|
|
|
if (edgelist->u >= edgestoadd->u)
|
|
|
|
goto addedge;
|
2001-02-26 06:48:02 +00:00
|
|
|
edgelist = edgelist->next;
|
2001-02-19 21:15:25 +00:00
|
|
|
if (edgelist->u >= edgestoadd->u)
|
|
|
|
goto addedge;
|
2001-02-26 06:48:02 +00:00
|
|
|
edgelist = edgelist->next;
|
2001-02-19 21:15:25 +00:00
|
|
|
if (edgelist->u >= edgestoadd->u)
|
|
|
|
goto addedge;
|
2001-02-26 06:48:02 +00:00
|
|
|
edgelist = edgelist->next;
|
2001-02-19 21:15:25 +00:00
|
|
|
goto edgesearch;
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
// insert edgestoadd before edgelist
|
2001-08-28 22:46:02 +00:00
|
|
|
addedge:
|
2001-02-19 21:15:25 +00:00
|
|
|
edgestoadd->next = edgelist;
|
|
|
|
edgestoadd->prev = edgelist->prev;
|
|
|
|
edgelist->prev->next = edgestoadd;
|
|
|
|
edgelist->prev = edgestoadd;
|
|
|
|
} while ((edgestoadd = next_edge) != NULL);
|
|
|
|
}
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
void
|
|
|
|
R_RemoveEdges (edge_t *pedge)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
do {
|
2001-02-19 21:15:25 +00:00
|
|
|
pedge->next->prev = pedge->prev;
|
|
|
|
pedge->prev->next = pedge->next;
|
|
|
|
} while ((pedge = pedge->nextremove) != NULL);
|
|
|
|
}
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
void
|
|
|
|
R_StepActiveU (edge_t *pedge)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2001-02-26 06:48:02 +00:00
|
|
|
edge_t *pnext_edge, *pwedge;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
while (1) {
|
|
|
|
nextedge:
|
2001-02-19 21:15:25 +00:00
|
|
|
pedge->u += pedge->u_step;
|
|
|
|
if (pedge->u < pedge->prev->u)
|
|
|
|
goto pushback;
|
|
|
|
pedge = pedge->next;
|
2001-02-26 06:48:02 +00:00
|
|
|
|
2001-02-19 21:15:25 +00:00
|
|
|
pedge->u += pedge->u_step;
|
|
|
|
if (pedge->u < pedge->prev->u)
|
|
|
|
goto pushback;
|
|
|
|
pedge = pedge->next;
|
2001-02-26 06:48:02 +00:00
|
|
|
|
2001-02-19 21:15:25 +00:00
|
|
|
pedge->u += pedge->u_step;
|
|
|
|
if (pedge->u < pedge->prev->u)
|
|
|
|
goto pushback;
|
|
|
|
pedge = pedge->next;
|
2001-02-26 06:48:02 +00:00
|
|
|
|
2001-02-19 21:15:25 +00:00
|
|
|
pedge->u += pedge->u_step;
|
|
|
|
if (pedge->u < pedge->prev->u)
|
|
|
|
goto pushback;
|
|
|
|
pedge = pedge->next;
|
2001-02-26 06:48:02 +00:00
|
|
|
|
|
|
|
goto nextedge;
|
|
|
|
|
|
|
|
pushback:
|
2001-02-19 21:15:25 +00:00
|
|
|
if (pedge == &edge_aftertail)
|
|
|
|
return;
|
2001-02-26 06:48:02 +00:00
|
|
|
|
|
|
|
// push it back to keep it sorted
|
2001-02-19 21:15:25 +00:00
|
|
|
pnext_edge = pedge->next;
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
// pull the edge out of the edge list
|
2001-02-19 21:15:25 +00:00
|
|
|
pedge->next->prev = pedge->prev;
|
|
|
|
pedge->prev->next = pedge->next;
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
// find out where the edge goes in the edge list
|
2001-02-19 21:15:25 +00:00
|
|
|
pwedge = pedge->prev->prev;
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
while (pwedge->u > pedge->u) {
|
2001-02-19 21:15:25 +00:00
|
|
|
pwedge = pwedge->prev;
|
|
|
|
}
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
// put the edge back into the edge list
|
2001-02-19 21:15:25 +00:00
|
|
|
pedge->next = pwedge->next;
|
|
|
|
pedge->prev = pwedge;
|
|
|
|
pedge->next->prev = pedge;
|
|
|
|
pwedge->next = pedge;
|
|
|
|
|
|
|
|
pedge = pnext_edge;
|
|
|
|
if (pedge == &edge_tail)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-05-15 04:50:53 +00:00
|
|
|
#endif // !USE_INTEL_ASM
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
void
|
2001-05-15 04:50:53 +00:00
|
|
|
R_CleanupSpan (void)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2001-02-26 06:48:02 +00:00
|
|
|
surf_t *surf;
|
|
|
|
int iu;
|
|
|
|
espan_t *span;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-05-15 21:13:07 +00:00
|
|
|
// now that we've reached the right edge of the screen, we're done with any
|
|
|
|
// unfinished surfaces, so emit a span for whatever's on top
|
2001-02-19 21:15:25 +00:00
|
|
|
surf = surfaces[1].next;
|
|
|
|
iu = edge_tail_u_shift20;
|
2001-02-26 06:48:02 +00:00
|
|
|
if (iu > surf->last_u) {
|
2001-02-19 21:15:25 +00:00
|
|
|
span = span_p++;
|
|
|
|
span->u = surf->last_u;
|
|
|
|
span->count = iu - span->u;
|
|
|
|
span->v = current_iv;
|
|
|
|
span->pnext = surf->spans;
|
|
|
|
surf->spans = span;
|
|
|
|
}
|
2001-05-15 21:13:07 +00:00
|
|
|
// reset spanstate for all surfaces in the surface stack
|
2001-02-26 06:48:02 +00:00
|
|
|
do {
|
2001-02-19 21:15:25 +00:00
|
|
|
surf->spanstate = 0;
|
|
|
|
surf = surf->next;
|
|
|
|
} while (surf != &surfaces[1]);
|
|
|
|
}
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
void
|
|
|
|
R_LeadingEdgeBackwards (edge_t *edge)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2001-02-26 06:48:02 +00:00
|
|
|
espan_t *span;
|
|
|
|
surf_t *surf, *surf2;
|
|
|
|
int iu;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-05-15 21:13:07 +00:00
|
|
|
// it's adding a new surface in, so find the correct place
|
2001-02-19 21:15:25 +00:00
|
|
|
surf = &surfaces[edge->surfs[1]];
|
|
|
|
|
2001-08-23 00:39:22 +00:00
|
|
|
// don't start a span if this is an inverted span, with the end edge
|
|
|
|
// preceding the start edge (that is, we've already seen the end edge)
|
2001-02-26 06:48:02 +00:00
|
|
|
if (++surf->spanstate == 1) {
|
2001-02-19 21:15:25 +00:00
|
|
|
surf2 = surfaces[1].next;
|
|
|
|
|
|
|
|
if (surf->key > surf2->key)
|
|
|
|
goto newtop;
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
// if it's two surfaces on the same plane, the one that's already
|
|
|
|
// active is in front, so keep going unless it's a bmodel
|
|
|
|
if (surf->insubmodel && (surf->key == surf2->key)) {
|
|
|
|
// must be two bmodels in the same leaf; don't care, because
|
2001-05-15 21:13:07 +00:00
|
|
|
// they'll never be farthest anyway
|
2001-02-19 21:15:25 +00:00
|
|
|
goto newtop;
|
|
|
|
}
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
continue_search:
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
do {
|
2001-02-19 21:15:25 +00:00
|
|
|
surf2 = surf2->next;
|
|
|
|
} while (surf->key < surf2->key);
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
if (surf->key == surf2->key) {
|
|
|
|
// if it's two surfaces on the same plane, the one that's already
|
|
|
|
// active is in front, so keep going unless it's a bmodel
|
2001-02-19 21:15:25 +00:00
|
|
|
if (!surf->insubmodel)
|
|
|
|
goto continue_search;
|
|
|
|
|
2001-05-16 17:20:42 +00:00
|
|
|
// must be two bmodels in the same leaf; don't care which is
|
|
|
|
// really in front, because they'll never be farthest anyway
|
2001-02-19 21:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
goto gotposition;
|
|
|
|
|
2001-08-28 22:46:02 +00:00
|
|
|
newtop:
|
2001-02-26 06:48:02 +00:00
|
|
|
// emit a span (obscures current top)
|
2001-02-19 21:15:25 +00:00
|
|
|
iu = edge->u >> 20;
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
if (iu > surf2->last_u) {
|
2001-02-19 21:15:25 +00:00
|
|
|
span = span_p++;
|
|
|
|
span->u = surf2->last_u;
|
|
|
|
span->count = iu - span->u;
|
|
|
|
span->v = current_iv;
|
|
|
|
span->pnext = surf2->spans;
|
|
|
|
surf2->spans = span;
|
|
|
|
}
|
|
|
|
// set last_u on the new span
|
|
|
|
surf->last_u = iu;
|
2001-02-26 06:48:02 +00:00
|
|
|
|
2001-08-28 22:46:02 +00:00
|
|
|
gotposition:
|
2001-02-26 06:48:02 +00:00
|
|
|
// insert before surf2
|
2001-02-19 21:15:25 +00:00
|
|
|
surf->next = surf2;
|
|
|
|
surf->prev = surf2->prev;
|
|
|
|
surf2->prev->next = surf;
|
|
|
|
surf2->prev = surf;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
void
|
|
|
|
R_TrailingEdge (surf_t *surf, edge_t *edge)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2001-02-26 06:48:02 +00:00
|
|
|
espan_t *span;
|
|
|
|
int iu;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-05-15 21:13:07 +00:00
|
|
|
// don't generate a span if this is an inverted span, with the end edge
|
|
|
|
// preceding the start edge (that is, we haven't seen the start edge yet)
|
2001-02-26 06:48:02 +00:00
|
|
|
if (--surf->spanstate == 0) {
|
2001-02-19 21:15:25 +00:00
|
|
|
if (surf->insubmodel)
|
|
|
|
r_bmodelactive--;
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
if (surf == surfaces[1].next) {
|
|
|
|
// emit a span (current top going away)
|
2001-02-19 21:15:25 +00:00
|
|
|
iu = edge->u >> 20;
|
2001-02-26 06:48:02 +00:00
|
|
|
if (iu > surf->last_u) {
|
2001-02-19 21:15:25 +00:00
|
|
|
span = span_p++;
|
|
|
|
span->u = surf->last_u;
|
|
|
|
span->count = iu - span->u;
|
|
|
|
span->v = current_iv;
|
|
|
|
span->pnext = surf->spans;
|
|
|
|
surf->spans = span;
|
|
|
|
}
|
2001-02-26 06:48:02 +00:00
|
|
|
// set last_u on the surface below
|
2001-02-19 21:15:25 +00:00
|
|
|
surf->next->last_u = iu;
|
|
|
|
}
|
|
|
|
|
|
|
|
surf->prev->next = surf->next;
|
|
|
|
surf->next->prev = surf->prev;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-05-15 04:50:53 +00:00
|
|
|
#ifndef USE_INTEL_ASM
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
void
|
|
|
|
R_LeadingEdge (edge_t *edge)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2001-02-26 06:48:02 +00:00
|
|
|
espan_t *span;
|
|
|
|
surf_t *surf, *surf2;
|
|
|
|
int iu;
|
|
|
|
double fu, newzi, testzi, newzitop, newzibottom;
|
|
|
|
|
|
|
|
if (edge->surfs[1]) {
|
|
|
|
// it's adding a new surface in, so find the correct place
|
2001-02-19 21:15:25 +00:00
|
|
|
surf = &surfaces[edge->surfs[1]];
|
|
|
|
|
2001-08-23 00:39:22 +00:00
|
|
|
// don't start a span if this is an inverted span, with the end edge
|
|
|
|
// preceding the start edge (that is, we've already seen the end edge)
|
2001-02-26 06:48:02 +00:00
|
|
|
if (++surf->spanstate == 1) {
|
2001-02-19 21:15:25 +00:00
|
|
|
if (surf->insubmodel)
|
|
|
|
r_bmodelactive++;
|
|
|
|
|
|
|
|
surf2 = surfaces[1].next;
|
|
|
|
|
|
|
|
if (surf->key < surf2->key)
|
|
|
|
goto newtop;
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
// if it's two surfaces on the same plane, the one that's already
|
|
|
|
// active is in front, so keep going unless it's a bmodel
|
|
|
|
if (surf->insubmodel && (surf->key == surf2->key)) {
|
|
|
|
// must be two bmodels in the same leaf; sort on 1/z
|
|
|
|
fu = (float) (edge->u - 0xFFFFF) * (1.0 / 0x100000);
|
|
|
|
newzi = surf->d_ziorigin + fv * surf->d_zistepv +
|
|
|
|
fu * surf->d_zistepu;
|
2001-02-19 21:15:25 +00:00
|
|
|
newzibottom = newzi * 0.99;
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
testzi = surf2->d_ziorigin + fv * surf2->d_zistepv +
|
|
|
|
fu * surf2->d_zistepu;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
if (newzibottom >= testzi) {
|
2001-02-19 21:15:25 +00:00
|
|
|
goto newtop;
|
|
|
|
}
|
|
|
|
|
|
|
|
newzitop = newzi * 1.01;
|
2001-02-26 06:48:02 +00:00
|
|
|
if (newzitop >= testzi) {
|
|
|
|
if (surf->d_zistepu >= surf2->d_zistepu) {
|
2001-02-19 21:15:25 +00:00
|
|
|
goto newtop;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
continue_search:
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
do {
|
2001-02-19 21:15:25 +00:00
|
|
|
surf2 = surf2->next;
|
|
|
|
} while (surf->key > surf2->key);
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
if (surf->key == surf2->key) {
|
2001-08-28 22:46:02 +00:00
|
|
|
// if it's two surfaces on the same plane, the already active
|
|
|
|
// one is in front, so keep going unless it's a bmodel
|
2001-02-19 21:15:25 +00:00
|
|
|
if (!surf->insubmodel)
|
|
|
|
goto continue_search;
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
// must be two bmodels in the same leaf; sort on 1/z
|
|
|
|
fu = (float) (edge->u - 0xFFFFF) * (1.0 / 0x100000);
|
|
|
|
newzi = surf->d_ziorigin + fv * surf->d_zistepv +
|
|
|
|
fu * surf->d_zistepu;
|
2001-02-19 21:15:25 +00:00
|
|
|
newzibottom = newzi * 0.99;
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
testzi = surf2->d_ziorigin + fv * surf2->d_zistepv +
|
|
|
|
fu * surf2->d_zistepu;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
if (newzibottom >= testzi) {
|
2001-02-19 21:15:25 +00:00
|
|
|
goto gotposition;
|
|
|
|
}
|
|
|
|
|
|
|
|
newzitop = newzi * 1.01;
|
2001-02-26 06:48:02 +00:00
|
|
|
if (newzitop >= testzi) {
|
|
|
|
if (surf->d_zistepu >= surf2->d_zistepu) {
|
2001-02-19 21:15:25 +00:00
|
|
|
goto gotposition;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
goto continue_search;
|
|
|
|
}
|
|
|
|
|
|
|
|
goto gotposition;
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
newtop:
|
|
|
|
// emit a span (obscures current top)
|
2001-02-19 21:15:25 +00:00
|
|
|
iu = edge->u >> 20;
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
if (iu > surf2->last_u) {
|
2001-02-19 21:15:25 +00:00
|
|
|
span = span_p++;
|
|
|
|
span->u = surf2->last_u;
|
|
|
|
span->count = iu - span->u;
|
|
|
|
span->v = current_iv;
|
|
|
|
span->pnext = surf2->spans;
|
|
|
|
surf2->spans = span;
|
|
|
|
}
|
|
|
|
// set last_u on the new span
|
|
|
|
surf->last_u = iu;
|
2001-02-26 06:48:02 +00:00
|
|
|
|
|
|
|
gotposition:
|
|
|
|
// insert before surf2
|
2001-02-19 21:15:25 +00:00
|
|
|
surf->next = surf2;
|
|
|
|
surf->prev = surf2->prev;
|
|
|
|
surf2->prev->next = surf;
|
|
|
|
surf2->prev = surf;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
void
|
|
|
|
R_GenerateSpans (void)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2001-02-26 06:48:02 +00:00
|
|
|
edge_t *edge;
|
|
|
|
surf_t *surf;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
r_bmodelactive = 0;
|
|
|
|
|
2001-05-15 21:13:07 +00:00
|
|
|
// clear active surfaces to just the background surface
|
2001-02-19 21:15:25 +00:00
|
|
|
surfaces[1].next = surfaces[1].prev = &surfaces[1];
|
|
|
|
surfaces[1].last_u = edge_head_u_shift20;
|
|
|
|
|
2001-05-15 21:13:07 +00:00
|
|
|
// generate spans
|
2001-02-26 06:48:02 +00:00
|
|
|
for (edge = edge_head.next; edge != &edge_tail; edge = edge->next) {
|
|
|
|
if (edge->surfs[0]) {
|
2001-05-15 21:13:07 +00:00
|
|
|
// it has a left surface, so a surface is going away for this span
|
2001-02-19 21:15:25 +00:00
|
|
|
surf = &surfaces[edge->surfs[0]];
|
|
|
|
|
|
|
|
R_TrailingEdge (surf, edge);
|
|
|
|
|
|
|
|
if (!edge->surfs[1])
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
R_LeadingEdge (edge);
|
|
|
|
}
|
|
|
|
|
|
|
|
R_CleanupSpan ();
|
|
|
|
}
|
|
|
|
|
2001-05-15 04:50:53 +00:00
|
|
|
#endif // !USE_INTEL_ASM
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
void
|
|
|
|
R_GenerateSpansBackward (void)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2001-02-26 06:48:02 +00:00
|
|
|
edge_t *edge;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
r_bmodelactive = 0;
|
|
|
|
|
2001-05-15 21:13:07 +00:00
|
|
|
// clear active surfaces to just the background surface
|
2001-02-19 21:15:25 +00:00
|
|
|
surfaces[1].next = surfaces[1].prev = &surfaces[1];
|
|
|
|
surfaces[1].last_u = edge_head_u_shift20;
|
|
|
|
|
2001-05-15 21:13:07 +00:00
|
|
|
// generate spans
|
2001-02-26 06:48:02 +00:00
|
|
|
for (edge = edge_head.next; edge != &edge_tail; edge = edge->next) {
|
2001-02-19 21:15:25 +00:00
|
|
|
if (edge->surfs[0])
|
|
|
|
R_TrailingEdge (&surfaces[edge->surfs[0]], edge);
|
|
|
|
|
|
|
|
if (edge->surfs[1])
|
|
|
|
R_LeadingEdgeBackwards (edge);
|
|
|
|
}
|
|
|
|
|
|
|
|
R_CleanupSpan ();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2001-05-15 04:50:53 +00:00
|
|
|
R_ScanEdges
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-05-15 04:50:53 +00:00
|
|
|
Input:
|
|
|
|
newedges[] array
|
|
|
|
this has links to edges, which have links to surfaces
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-05-15 04:50:53 +00:00
|
|
|
Output:
|
|
|
|
Each surface has a linked list of its visible spans
|
2001-02-19 21:15:25 +00:00
|
|
|
*/
|
2001-02-26 06:48:02 +00:00
|
|
|
void
|
|
|
|
R_ScanEdges (void)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2001-02-26 06:48:02 +00:00
|
|
|
int iv, bottom;
|
|
|
|
byte basespans[MAXSPANS * sizeof (espan_t) + CACHE_SIZE];
|
|
|
|
espan_t *basespan_p;
|
|
|
|
surf_t *s;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
basespan_p = (espan_t *)
|
2001-02-26 06:48:02 +00:00
|
|
|
((long) (basespans + CACHE_SIZE - 1) & ~(CACHE_SIZE - 1));
|
2001-02-19 21:15:25 +00:00
|
|
|
max_span_p = &basespan_p[MAXSPANS - r_refdef.vrect.width];
|
|
|
|
|
|
|
|
span_p = basespan_p;
|
|
|
|
|
2001-05-15 21:13:07 +00:00
|
|
|
// clear active edges to just the background edges around the whole screen
|
2001-08-28 22:46:02 +00:00
|
|
|
// FIXME: most of this only needs to be set up once
|
2001-02-19 21:15:25 +00:00
|
|
|
edge_head.u = r_refdef.vrect.x << 20;
|
|
|
|
edge_head_u_shift20 = edge_head.u >> 20;
|
|
|
|
edge_head.u_step = 0;
|
|
|
|
edge_head.prev = NULL;
|
|
|
|
edge_head.next = &edge_tail;
|
|
|
|
edge_head.surfs[0] = 0;
|
|
|
|
edge_head.surfs[1] = 1;
|
2001-02-26 06:48:02 +00:00
|
|
|
|
2001-02-19 21:15:25 +00:00
|
|
|
edge_tail.u = (r_refdef.vrectright << 20) + 0xFFFFF;
|
|
|
|
edge_tail_u_shift20 = edge_tail.u >> 20;
|
|
|
|
edge_tail.u_step = 0;
|
|
|
|
edge_tail.prev = &edge_head;
|
|
|
|
edge_tail.next = &edge_aftertail;
|
|
|
|
edge_tail.surfs[0] = 1;
|
|
|
|
edge_tail.surfs[1] = 0;
|
2001-02-26 06:48:02 +00:00
|
|
|
|
|
|
|
edge_aftertail.u = -1; // force a move
|
2001-02-19 21:15:25 +00:00
|
|
|
edge_aftertail.u_step = 0;
|
|
|
|
edge_aftertail.next = &edge_sentinel;
|
|
|
|
edge_aftertail.prev = &edge_tail;
|
|
|
|
|
2001-08-28 22:46:02 +00:00
|
|
|
// FIXME: do we need this now that we clamp x in r_draw.c?
|
2001-02-19 21:15:25 +00:00
|
|
|
edge_sentinel.u = 2000 << 24; // make sure nothing sorts past this
|
|
|
|
edge_sentinel.prev = &edge_aftertail;
|
|
|
|
|
2001-05-15 21:13:07 +00:00
|
|
|
// process all scan lines
|
2001-02-19 21:15:25 +00:00
|
|
|
bottom = r_refdef.vrectbottom - 1;
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
for (iv = r_refdef.vrect.y; iv < bottom; iv++) {
|
2001-02-19 21:15:25 +00:00
|
|
|
current_iv = iv;
|
2001-02-26 06:48:02 +00:00
|
|
|
fv = (float) iv;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
// mark that the head (background start) span is pre-included
|
2001-02-19 21:15:25 +00:00
|
|
|
surfaces[1].spanstate = 1;
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
if (newedges[iv]) {
|
2001-02-19 21:15:25 +00:00
|
|
|
R_InsertNewEdges (newedges[iv], edge_head.next);
|
|
|
|
}
|
|
|
|
|
|
|
|
(*pdrawfunc) ();
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
// flush the span list if we can't be sure we have enough spans left
|
2001-05-15 21:13:07 +00:00
|
|
|
// for the next scan
|
2001-05-16 17:20:42 +00:00
|
|
|
if (span_p > max_span_p) {
|
2001-02-19 21:15:25 +00:00
|
|
|
VID_UnlockBuffer ();
|
2001-05-16 17:20:42 +00:00
|
|
|
S_ExtraUpdate (); // don't let sound get messed up if going slow
|
2001-02-19 21:15:25 +00:00
|
|
|
VID_LockBuffer ();
|
2001-02-26 06:48:02 +00:00
|
|
|
|
2001-05-15 04:50:53 +00:00
|
|
|
if (r_drawculledpolys)
|
2001-02-19 21:15:25 +00:00
|
|
|
R_DrawCulledPolys ();
|
2001-05-15 04:50:53 +00:00
|
|
|
else
|
2001-02-19 21:15:25 +00:00
|
|
|
D_DrawSurfaces ();
|
|
|
|
|
2001-02-26 06:48:02 +00:00
|
|
|
// clear the surface span pointers
|
|
|
|
for (s = &surfaces[1]; s < surface_p; s++)
|
2001-02-19 21:15:25 +00:00
|
|
|
s->spans = NULL;
|
|
|
|
|
|
|
|
span_p = basespan_p;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (removeedges[iv])
|
|
|
|
R_RemoveEdges (removeedges[iv]);
|
|
|
|
|
|
|
|
if (edge_head.next != &edge_tail)
|
|
|
|
R_StepActiveU (edge_head.next);
|
|
|
|
}
|
|
|
|
|
2001-05-15 21:13:07 +00:00
|
|
|
// do the last scan (no need to step or sort or remove on the last scan)
|
2001-02-19 21:15:25 +00:00
|
|
|
current_iv = iv;
|
2001-02-26 06:48:02 +00:00
|
|
|
fv = (float) iv;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-05-15 21:13:07 +00:00
|
|
|
// mark that the head (background start) span is pre-included
|
2001-02-19 21:15:25 +00:00
|
|
|
surfaces[1].spanstate = 1;
|
|
|
|
|
|
|
|
if (newedges[iv])
|
|
|
|
R_InsertNewEdges (newedges[iv], edge_head.next);
|
|
|
|
|
|
|
|
(*pdrawfunc) ();
|
|
|
|
|
2001-05-15 21:13:07 +00:00
|
|
|
// draw whatever's left in the span list
|
2001-02-19 21:15:25 +00:00
|
|
|
if (r_drawculledpolys)
|
|
|
|
R_DrawCulledPolys ();
|
|
|
|
else
|
|
|
|
D_DrawSurfaces ();
|
|
|
|
}
|