📄 tr_main.c
字号:
==========================================================================================
DRAWSURF SORTING
==========================================================================================
*/
/*
=================
qsort replacement
=================
*/
#define SWAP_DRAW_SURF(a,b) temp=((int *)a)[0];((int *)a)[0]=((int *)b)[0];((int *)b)[0]=temp; temp=((int *)a)[1];((int *)a)[1]=((int *)b)[1];((int *)b)[1]=temp;
/* this parameter defines the cutoff between using quick sort and
insertion sort for arrays; arrays with lengths shorter or equal to the
below value use insertion sort */
#define CUTOFF 8 /* testing shows that this is good value */
static void shortsort( drawSurf_t *lo, drawSurf_t *hi ) {
drawSurf_t *p, *max;
int temp;
while (hi > lo) {
max = lo;
for (p = lo + 1; p <= hi; p++ ) {
if ( p->sort > max->sort ) {
max = p;
}
}
SWAP_DRAW_SURF(max, hi);
hi--;
}
}
/* sort the array between lo and hi (inclusive)
FIXME: this was lifted and modified from the microsoft lib source...
*/
void qsortFast (
void *base,
unsigned num,
unsigned width
)
{
char *lo, *hi; /* ends of sub-array currently sorting */
char *mid; /* points to middle of subarray */
char *loguy, *higuy; /* traveling pointers for partition step */
unsigned size; /* size of the sub-array */
char *lostk[30], *histk[30];
int stkptr; /* stack for saving sub-array to be processed */
int temp;
if ( sizeof(drawSurf_t) != 8 ) {
ri.Error( ERR_DROP, "change SWAP_DRAW_SURF macro" );
}
/* Note: the number of stack entries required is no more than
1 + log2(size), so 30 is sufficient for any array */
if (num < 2 || width == 0)
return; /* nothing to do */
stkptr = 0; /* initialize stack */
lo = base;
hi = (char *)base + width * (num-1); /* initialize limits */
/* this entry point is for pseudo-recursion calling: setting
lo and hi and jumping to here is like recursion, but stkptr is
prserved, locals aren't, so we preserve stuff on the stack */
recurse:
size = (hi - lo) / width + 1; /* number of el's to sort */
/* below a certain size, it is faster to use a O(n^2) sorting method */
if (size <= CUTOFF) {
shortsort((drawSurf_t *)lo, (drawSurf_t *)hi);
}
else {
/* First we pick a partititioning element. The efficiency of the
algorithm demands that we find one that is approximately the
median of the values, but also that we select one fast. Using
the first one produces bad performace if the array is already
sorted, so we use the middle one, which would require a very
wierdly arranged array for worst case performance. Testing shows
that a median-of-three algorithm does not, in general, increase
performance. */
mid = lo + (size / 2) * width; /* find middle element */
SWAP_DRAW_SURF(mid, lo); /* swap it to beginning of array */
/* We now wish to partition the array into three pieces, one
consisiting of elements <= partition element, one of elements
equal to the parition element, and one of element >= to it. This
is done below; comments indicate conditions established at every
step. */
loguy = lo;
higuy = hi + width;
/* Note that higuy decreases and loguy increases on every iteration,
so loop must terminate. */
for (;;) {
/* lo <= loguy < hi, lo < higuy <= hi + 1,
A[i] <= A[lo] for lo <= i <= loguy,
A[i] >= A[lo] for higuy <= i <= hi */
do {
loguy += width;
} while (loguy <= hi &&
( ((drawSurf_t *)loguy)->sort <= ((drawSurf_t *)lo)->sort ) );
/* lo < loguy <= hi+1, A[i] <= A[lo] for lo <= i < loguy,
either loguy > hi or A[loguy] > A[lo] */
do {
higuy -= width;
} while (higuy > lo &&
( ((drawSurf_t *)higuy)->sort >= ((drawSurf_t *)lo)->sort ) );
/* lo-1 <= higuy <= hi, A[i] >= A[lo] for higuy < i <= hi,
either higuy <= lo or A[higuy] < A[lo] */
if (higuy < loguy)
break;
/* if loguy > hi or higuy <= lo, then we would have exited, so
A[loguy] > A[lo], A[higuy] < A[lo],
loguy < hi, highy > lo */
SWAP_DRAW_SURF(loguy, higuy);
/* A[loguy] < A[lo], A[higuy] > A[lo]; so condition at top
of loop is re-established */
}
/* A[i] >= A[lo] for higuy < i <= hi,
A[i] <= A[lo] for lo <= i < loguy,
higuy < loguy, lo <= higuy <= hi
implying:
A[i] >= A[lo] for loguy <= i <= hi,
A[i] <= A[lo] for lo <= i <= higuy,
A[i] = A[lo] for higuy < i < loguy */
SWAP_DRAW_SURF(lo, higuy); /* put partition element in place */
/* OK, now we have the following:
A[i] >= A[higuy] for loguy <= i <= hi,
A[i] <= A[higuy] for lo <= i < higuy
A[i] = A[lo] for higuy <= i < loguy */
/* We've finished the partition, now we want to sort the subarrays
[lo, higuy-1] and [loguy, hi].
We do the smaller one first to minimize stack usage.
We only sort arrays of length 2 or more.*/
if ( higuy - 1 - lo >= hi - loguy ) {
if (lo + width < higuy) {
lostk[stkptr] = lo;
histk[stkptr] = higuy - width;
++stkptr;
} /* save big recursion for later */
if (loguy < hi) {
lo = loguy;
goto recurse; /* do small recursion */
}
}
else {
if (loguy < hi) {
lostk[stkptr] = loguy;
histk[stkptr] = hi;
++stkptr; /* save big recursion for later */
}
if (lo + width < higuy) {
hi = higuy - width;
goto recurse; /* do small recursion */
}
}
}
/* We have sorted the array, except for any pending sorts on the stack.
Check if there are any, and do them. */
--stkptr;
if (stkptr >= 0) {
lo = lostk[stkptr];
hi = histk[stkptr];
goto recurse; /* pop subarray from stack */
}
else
return; /* all subarrays done */
}
//==========================================================================================
/*
=================
R_AddDrawSurf
=================
*/
void R_AddDrawSurf( surfaceType_t *surface, shader_t *shader,
int fogIndex, int dlightMap ) {
int index;
// instead of checking for overflow, we just mask the index
// so it wraps around
index = tr.refdef.numDrawSurfs & DRAWSURF_MASK;
// the sort data is packed into a single 32 bit value so it can be
// compared quickly during the qsorting process
tr.refdef.drawSurfs[index].sort = (shader->sortedIndex << QSORT_SHADERNUM_SHIFT)
| tr.shiftedEntityNum | ( fogIndex << QSORT_FOGNUM_SHIFT ) | (int)dlightMap;
tr.refdef.drawSurfs[index].surface = surface;
tr.refdef.numDrawSurfs++;
}
/*
=================
R_DecomposeSort
=================
*/
void R_DecomposeSort( unsigned sort, int *entityNum, shader_t **shader,
int *fogNum, int *dlightMap ) {
*fogNum = ( sort >> QSORT_FOGNUM_SHIFT ) & 31;
*shader = tr.sortedShaders[ ( sort >> QSORT_SHADERNUM_SHIFT ) & (MAX_SHADERS-1) ];
*entityNum = ( sort >> QSORT_ENTITYNUM_SHIFT ) & 1023;
*dlightMap = sort & 3;
}
/*
=================
R_SortDrawSurfs
=================
*/
void R_SortDrawSurfs( drawSurf_t *drawSurfs, int numDrawSurfs ) {
shader_t *shader;
int fogNum;
int entityNum;
int dlighted;
int i;
// it is possible for some views to not have any surfaces
if ( numDrawSurfs < 1 ) {
// we still need to add it for hyperspace cases
R_AddDrawSurfCmd( drawSurfs, numDrawSurfs );
return;
}
// if we overflowed MAX_DRAWSURFS, the drawsurfs
// wrapped around in the buffer and we will be missing
// the first surfaces, not the last ones
if ( numDrawSurfs > MAX_DRAWSURFS ) {
numDrawSurfs = MAX_DRAWSURFS;
}
// sort the drawsurfs by sort type, then orientation, then shader
qsortFast (drawSurfs, numDrawSurfs, sizeof(drawSurf_t) );
// check for any pass through drawing, which
// may cause another view to be rendered first
for ( i = 0 ; i < numDrawSurfs ; i++ ) {
R_DecomposeSort( (drawSurfs+i)->sort, &entityNum, &shader, &fogNum, &dlighted );
if ( shader->sort > SS_PORTAL ) {
break;
}
// no shader should ever have this sort type
if ( shader->sort == SS_BAD ) {
ri.Error (ERR_DROP, "Shader '%s'with sort == SS_BAD", shader->name );
}
// if the mirror was completely clipped away, we may need to check another surface
if ( R_MirrorViewBySurface( (drawSurfs+i), entityNum) ) {
// this is a debug option to see exactly what is being mirrored
if ( r_portalOnly->integer ) {
return;
}
break; // only one mirror view at a time
}
}
R_AddDrawSurfCmd( drawSurfs, numDrawSurfs );
}
/*
=============
R_AddEntitySurfaces
=============
*/
void R_AddEntitySurfaces (void) {
trRefEntity_t *ent;
shader_t *shader;
if ( !r_drawentities->integer ) {
return;
}
for ( tr.currentEntityNum = 0;
tr.currentEntityNum < tr.refdef.num_entities;
tr.currentEntityNum++ ) {
ent = tr.currentEntity = &tr.refdef.entities[tr.currentEntityNum];
ent->needDlights = qfalse;
// preshift the value we are going to OR into the drawsurf sort
tr.shiftedEntityNum = tr.currentEntityNum << QSORT_ENTITYNUM_SHIFT;
//
// the weapon model must be handled special --
// we don't want the hacked weapon position showing in
// mirrors, because the true body position will already be drawn
//
if ( (ent->e.renderfx & RF_FIRST_PERSON) && tr.viewParms.isPortal) {
continue;
}
// simple generated models, like sprites and beams, are not culled
switch ( ent->e.reType ) {
case RT_PORTALSURFACE:
break; // don't draw anything
case RT_SPRITE:
case RT_BEAM:
case RT_LIGHTNING:
case RT_RAIL_CORE:
case RT_RAIL_RINGS:
// self blood sprites, talk balloons, etc should not be drawn in the primary
// view. We can't just do this check for all entities, because md3
// entities may still want to cast shadows from them
if ( (ent->e.renderfx & RF_THIRD_PERSON) && !tr.viewParms.isPortal) {
continue;
}
shader = R_GetShaderByHandle( ent->e.customShader );
R_AddDrawSurf( &entitySurface, shader, R_SpriteFogNum( ent ), 0 );
break;
case RT_MODEL:
// we must set up parts of tr.or for model culling
R_RotateForEntity( ent, &tr.viewParms, &tr.or );
tr.currentModel = R_GetModelByHandle( ent->e.hModel );
if (!tr.currentModel) {
R_AddDrawSurf( &entitySurface, tr.defaultShader, 0, 0 );
} else {
switch ( tr.currentModel->type ) {
case MOD_MESH:
R_AddMD3Surfaces( ent );
break;
case MOD_MD4:
R_AddAnimSurfaces( ent );
break;
case MOD_BRUSH:
R_AddBrushModelSurfaces( ent );
break;
case MOD_BAD: // null model axis
if ( (ent->e.renderfx & RF_THIRD_PERSON) && !tr.viewParms.isPortal) {
break;
}
shader = R_GetShaderByHandle( ent->e.customShader );
R_AddDrawSurf( &entitySurface, tr.defaultShader, 0, 0 );
break;
default:
ri.Error( ERR_DROP, "R_AddEntitySurfaces: Bad modeltype" );
break;
}
}
break;
default:
ri.Error( ERR_DROP, "R_AddEntitySurfaces: Bad reType" );
}
}
}
/*
====================
R_GenerateDrawSurfs
====================
*/
void R_GenerateDrawSurfs( void ) {
R_AddWorldSurfaces ();
R_AddPolygonSurfaces();
// set the projection matrix with the minimum zfar
// now that we have the world bounded
// this needs to be done before entities are
// added, because they use the projection
// matrix for lod calculation
R_SetupProjection ();
R_AddEntitySurfaces ();
}
/*
================
R_DebugPolygon
================
*/
void R_DebugPolygon( int color, int numPoints, float *points ) {
int i;
GL_State( GLS_DEPTHMASK_TRUE | GLS_SRCBLEND_ONE | GLS_DSTBLEND_ONE );
// draw solid shade
qglColor3f( color&1, (color>>1)&1, (color>>2)&1 );
qglBegin( GL_POLYGON );
for ( i = 0 ; i < numPoints ; i++ ) {
qglVertex3fv( points + i * 3 );
}
qglEnd();
// draw wireframe outline
GL_State( GLS_POLYMODE_LINE | GLS_DEPTHMASK_TRUE | GLS_SRCBLEND_ONE | GLS_DSTBLEND_ONE );
qglDepthRange( 0, 0 );
qglColor3f( 1, 1, 1 );
qglBegin( GL_POLYGON );
for ( i = 0 ; i < numPoints ; i++ ) {
qglVertex3fv( points + i * 3 );
}
qglEnd();
qglDepthRange( 0, 1 );
}
/*
====================
R_DebugGraphics
Visualization aid for movement clipping debugging
====================
*/
void R_DebugGraphics( void ) {
if ( !r_debugSurface->integer ) {
return;
}
// the render thread can't make callbacks to the main thread
R_SyncRenderThread();
GL_Bind( tr.whiteImage);
GL_Cull( CT_FRONT_SIDED );
ri.CM_DrawDebugSurface( R_DebugPolygon );
}
/*
================
R_RenderView
A view may be either the actual camera view,
or a mirror / remote location
================
*/
void R_RenderView (viewParms_t *parms) {
int firstDrawSurf;
if ( parms->viewportWidth <= 0 || parms->viewportHeight <= 0 ) {
return;
}
tr.viewCount++;
tr.viewParms = *parms;
tr.viewParms.frameSceneNum = tr.frameSceneNum;
tr.viewParms.frameCount = tr.frameCount;
firstDrawSurf = tr.refdef.numDrawSurfs;
tr.viewCount++;
// set viewParms.world
R_RotateForViewer ();
R_SetupFrustum ();
R_GenerateDrawSurfs();
R_SortDrawSurfs( tr.refdef.drawSurfs + firstDrawSurf, tr.refdef.numDrawSurfs - firstDrawSurf );
// draw main system development information (surface outlines, etc)
R_DebugGraphics();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -