diff options
Diffstat (limited to 'src/gl_world.c')
-rw-r--r-- | src/gl_world.c | 363 |
1 files changed, 189 insertions, 174 deletions
diff --git a/src/gl_world.c b/src/gl_world.c index 492d7a3..d006139 100644 --- a/src/gl_world.c +++ b/src/gl_world.c @@ -8,7 +8,7 @@ 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. +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. @@ -20,7 +20,8 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "gl_local.h" -static qboolean GL_LightPoint( vec3_t origin, vec3_t color ) { +static qboolean GL_LightPoint(vec3_t origin, vec3_t color) +{ bsp_t *bsp = gl_static.world.cache; mface_t *surf; int s, t, i; @@ -33,48 +34,48 @@ static qboolean GL_LightPoint( vec3_t origin, vec3_t color ) { lightstyle_t *style; vec3_t point; - if( !bsp || !bsp->lightmap ) { + if (!bsp || !bsp->lightmap) { return qfalse; } point[0] = origin[0]; point[1] = origin[1]; point[2] = origin[2] - 8192; - - surf = BSP_LightPoint( bsp->nodes, origin, point, &s, &t ); - if( !surf ) { + + surf = BSP_LightPoint(bsp->nodes, origin, point, &s, &t); + if (!surf) { return qfalse; } fracu = s & 15; fracv = t & 15; - w1 = ( 16 - fracu ) * ( 16 - fracv ); - w2 = fracu * ( 16 - fracv ); + w1 = (16 - fracu) * (16 - fracv); + w2 = fracu * (16 - fracv); w3 = fracu * fracv; - w4 = ( 16 - fracu ) * fracv; + w4 = (16 - fracu) * fracv; s >>= 4; t >>= 4; - smax = S_MAX( surf ); - tmax = T_MAX( surf ); + smax = S_MAX(surf); + tmax = T_MAX(surf); size = smax * tmax * 3; - VectorClear( color ); + VectorClear(color); lightmap = surf->lightmap; - for( i = 0; i < surf->numstyles; i++ ) { - b1 = &lightmap[3 * ( ( t + 0 ) * smax + ( s + 0 ) )]; - b2 = &lightmap[3 * ( ( t + 0 ) * smax + ( s + 1 ) )]; - b3 = &lightmap[3 * ( ( t + 1 ) * smax + ( s + 1 ) )]; - b4 = &lightmap[3 * ( ( t + 1 ) * smax + ( s + 0 ) )]; + for (i = 0; i < surf->numstyles; i++) { + b1 = &lightmap[3 * ((t + 0) * smax + (s + 0))]; + b2 = &lightmap[3 * ((t + 0) * smax + (s + 1))]; + b3 = &lightmap[3 * ((t + 1) * smax + (s + 1))]; + b4 = &lightmap[3 * ((t + 1) * smax + (s + 0))]; - temp[0] = ( w1 * b1[0] + w2 * b2[0] + w3 * b3[0] + w4 * b4[0] ) >> 8; - temp[1] = ( w1 * b1[1] + w2 * b2[1] + w3 * b3[1] + w4 * b4[1] ) >> 8; - temp[2] = ( w1 * b1[2] + w2 * b2[2] + w3 * b3[2] + w4 * b4[2] ) >> 8; + temp[0] = (w1 * b1[0] + w2 * b2[0] + w3 * b3[0] + w4 * b4[0]) >> 8; + temp[1] = (w1 * b1[1] + w2 * b2[1] + w3 * b3[1] + w4 * b4[1]) >> 8; + temp[2] = (w1 * b1[2] + w2 * b2[2] + w3 * b3[2] + w4 * b4[2]) >> 8; - style = LIGHT_STYLE( surf, i ); + style = LIGHT_STYLE(surf, i); color[0] += temp[0] * style->rgb[0]; color[1] += temp[1] * style->rgb[1]; @@ -83,129 +84,136 @@ static qboolean GL_LightPoint( vec3_t origin, vec3_t color ) { lightmap += size; } - GL_AdjustColor( color ); + GL_AdjustColor(color); return qtrue; } #if USE_DLIGHTS -static void GL_MarkLights_r( mnode_t *node, dlight_t *light, int lightbit ) { +static void GL_MarkLights_r(mnode_t *node, dlight_t *light, int lightbit) +{ vec_t dot; int count; mface_t *face; - - while( node->plane ) { - dot = PlaneDiffFast( light->transformed, node->plane ); - if( dot > light->intensity - DLIGHT_CUTOFF ) { + + while (node->plane) { + dot = PlaneDiffFast(light->transformed, node->plane); + if (dot > light->intensity - DLIGHT_CUTOFF) { node = node->children[0]; continue; } - if( dot < -light->intensity + DLIGHT_CUTOFF ) { + if (dot < -light->intensity + DLIGHT_CUTOFF) { node = node->children[1]; continue; } face = node->firstface; count = node->numfaces; - while( count-- ) { - if( !( face->drawflags & SURF_NOLM_MASK ) ) { - if( face->dlightframe != glr.dlightframe ) { + while (count--) { + if (!(face->drawflags & SURF_NOLM_MASK)) { + if (face->dlightframe != glr.dlightframe) { face->dlightframe = glr.dlightframe; face->dlightbits = 0; } - + face->dlightbits |= lightbit; } face++; } - GL_MarkLights_r( node->children[0], light, lightbit ); + GL_MarkLights_r(node->children[0], light, lightbit); node = node->children[1]; } } -static void GL_MarkLights( void ) { +static void GL_MarkLights(void) +{ int i; dlight_t *light; - for( i = 0, light = glr.fd.dlights; i < glr.fd.num_dlights; i++, light++ ) { - VectorCopy( light->origin, light->transformed ); - GL_MarkLights_r( gl_static.world.cache->nodes, light, 1 << i ); + for (i = 0, light = glr.fd.dlights; i < glr.fd.num_dlights; i++, light++) { + VectorCopy(light->origin, light->transformed); + GL_MarkLights_r(gl_static.world.cache->nodes, light, 1 << i); } } -static void GL_TransformLights( mmodel_t *model ) { +static void GL_TransformLights(mmodel_t *model) +{ int i; dlight_t *light; vec3_t temp; - if( !model->headnode ) { + if (!model->headnode) { return; } - - for( i = 0, light = glr.fd.dlights; i < glr.fd.num_dlights; i++, light++ ) { - VectorSubtract( light->origin, glr.ent->origin, temp ); - light->transformed[0] = DotProduct( temp, glr.entaxis[0] ); - light->transformed[1] = DotProduct( temp, glr.entaxis[1] ); - light->transformed[2] = DotProduct( temp, glr.entaxis[2] ); - GL_MarkLights_r( model->headnode, light, 1 << i ); + + for (i = 0, light = glr.fd.dlights; i < glr.fd.num_dlights; i++, light++) { + VectorSubtract(light->origin, glr.ent->origin, temp); + light->transformed[0] = DotProduct(temp, glr.entaxis[0]); + light->transformed[1] = DotProduct(temp, glr.entaxis[1]); + light->transformed[2] = DotProduct(temp, glr.entaxis[2]); + GL_MarkLights_r(model->headnode, light, 1 << i); } } -static void GL_AddLights( vec3_t origin, vec3_t color ) { +static void GL_AddLights(vec3_t origin, vec3_t color) +{ dlight_t *light; vec3_t dir; vec_t f; int i; - for( i = 0, light = glr.fd.dlights; i < glr.fd.num_dlights; i++, light++ ) { - VectorSubtract( light->origin, origin, dir ); - f = light->intensity - VectorLength( dir ); - if( f > 0 ) { - f *= (1.0f/255); - VectorMA( color, f, light->color, color ); + for (i = 0, light = glr.fd.dlights; i < glr.fd.num_dlights; i++, light++) { + VectorSubtract(light->origin, origin, dir); + f = light->intensity - VectorLength(dir); + if (f > 0) { + f *= (1.0f / 255); + VectorMA(color, f, light->color, color); } } } #endif -void _R_LightPoint( vec3_t origin, vec3_t color ) { - if( gl_fullbright->integer ) { - VectorSet( color, 1, 1, 1 ); +void _R_LightPoint(vec3_t origin, vec3_t color) +{ + if (gl_fullbright->integer) { + VectorSet(color, 1, 1, 1); return; } // get lighting from world - if( !GL_LightPoint( origin, color ) ) { - VectorSet( color, 1, 1, 1 ); + if (!GL_LightPoint(origin, color)) { + VectorSet(color, 1, 1, 1); } #if USE_DLIGHTS - if( gl_dynamic->integer == 1 ) { + if (gl_dynamic->integer == 1) { // add dynamic lights - GL_AddLights( origin, color ); + GL_AddLights(origin, color); } #endif - if( gl_doublelight_entities->integer ) { + if (gl_doublelight_entities->integer) { // apply modulate twice to mimic original ref_gl behavior - VectorScale( color, gl_static.entity_modulate, color ); + VectorScale(color, gl_static.entity_modulate, color); } } -void R_LightPoint( vec3_t origin, vec3_t color ) { +void R_LightPoint(vec3_t origin, vec3_t color) +{ int i; - _R_LightPoint( origin, color ); + _R_LightPoint(origin, color); - for( i = 0; i < 3; i++ ) { - clamp( color[i], 0, 1 ); + for (i = 0; i < 3; i++) { + clamp(color[i], 0, 1); } } -static void GL_MarkLeaves( void ) { +static void GL_MarkLeaves(void) +{ static int lastNodesVisible; byte vis1[VIS_MAX_BYTES]; byte vis2[VIS_MAX_BYTES]; @@ -217,24 +225,24 @@ static void GL_MarkLeaves( void ) { int i; bsp_t *bsp = gl_static.world.cache; - leaf = BSP_PointLeaf( bsp->nodes, glr.fd.vieworg ); + leaf = BSP_PointLeaf(bsp->nodes, glr.fd.vieworg); cluster1 = cluster2 = leaf->cluster; - VectorCopy( glr.fd.vieworg, tmp ); - if( !leaf->contents ) { - tmp[2] -= 16; + VectorCopy(glr.fd.vieworg, tmp); + if (!leaf->contents) { + tmp[2] -= 16; } else { - tmp[2] += 16; - } - leaf = BSP_PointLeaf( bsp->nodes, tmp ); - if( !( leaf->contents & CONTENTS_SOLID ) ) { + tmp[2] += 16; + } + leaf = BSP_PointLeaf(bsp->nodes, tmp); + if (!(leaf->contents & CONTENTS_SOLID)) { cluster2 = leaf->cluster; } - - if( cluster1 == glr.viewcluster1 && cluster2 == glr.viewcluster2 ) { + + if (cluster1 == glr.viewcluster1 && cluster2 == glr.viewcluster2) { goto finish; } - - if( gl_lockpvs->integer ) { + + if (gl_lockpvs->integer) { goto finish; } @@ -242,50 +250,50 @@ static void GL_MarkLeaves( void ) { glr.viewcluster1 = cluster1; glr.viewcluster2 = cluster2; - if( !bsp->vis || gl_novis->integer || cluster1 == -1 ) { + if (!bsp->vis || gl_novis->integer || cluster1 == -1) { // mark everything visible - for( i = 0; i < bsp->numnodes; i++ ) { + for (i = 0; i < bsp->numnodes; i++) { bsp->nodes[i].visframe = glr.visframe; } - for( i = 0; i < bsp->numleafs; i++ ) { + for (i = 0; i < bsp->numleafs; i++) { bsp->leafs[i].visframe = glr.visframe; } lastNodesVisible = bsp->numnodes; goto finish; } - BSP_ClusterVis( bsp, vis1, cluster1, DVIS_PVS ); - if( cluster1 != cluster2 ) { - BSP_ClusterVis( bsp, vis2, cluster2, DVIS_PVS ); - longs = VIS_FAST_LONGS( bsp ); - src1 = ( uint_fast32_t * )vis1; - src2 = ( uint_fast32_t * )vis2; - while( longs-- ) { + BSP_ClusterVis(bsp, vis1, cluster1, DVIS_PVS); + if (cluster1 != cluster2) { + BSP_ClusterVis(bsp, vis2, cluster2, DVIS_PVS); + longs = VIS_FAST_LONGS(bsp); + src1 = (uint_fast32_t *)vis1; + src2 = (uint_fast32_t *)vis2; + while (longs--) { *src1++ |= *src2++; } } lastNodesVisible = 0; - for( i = 0, leaf = bsp->leafs; i < bsp->numleafs; i++, leaf++ ) { + for (i = 0, leaf = bsp->leafs; i < bsp->numleafs; i++, leaf++) { cluster1 = leaf->cluster; - if( cluster1 == -1 ) { + if (cluster1 == -1) { continue; } - if( Q_IsBitSet( vis1, cluster1 ) ) { - node = ( mnode_t * )leaf; + if (Q_IsBitSet(vis1, cluster1)) { + node = (mnode_t *)leaf; // mark parent nodes visible do { - if( node->visframe == glr.visframe ) { + if (node->visframe == glr.visframe) { break; } node->visframe = glr.visframe; node = node->parent; lastNodesVisible++; - } while( node ); + } while (node); } } - + finish: c.nodesVisible = lastNodesVisible; @@ -293,11 +301,12 @@ finish: #define BACKFACE_EPSILON 0.001f -#define BSP_CullFace( face, dot ) \ - ( ( (dot) < -BACKFACE_EPSILON && !( (face)->drawflags & DSURF_PLANEBACK ) ) || \ - ( (dot) > BACKFACE_EPSILON && ( (face)->drawflags & DSURF_PLANEBACK ) ) ) +#define BSP_CullFace(face, dot) \ + (((dot) < -BACKFACE_EPSILON && !((face)->drawflags & DSURF_PLANEBACK)) || \ + ((dot) > BACKFACE_EPSILON && ((face)->drawflags & DSURF_PLANEBACK))) -void GL_DrawBspModel( mmodel_t *model ) { +void GL_DrawBspModel(mmodel_t *model) +{ mface_t *face; int count, mask = 0; vec3_t bounds[2]; @@ -306,36 +315,36 @@ void GL_DrawBspModel( mmodel_t *model ) { entity_t *ent = glr.ent; glCullResult_t cull; - if( glr.entrotated ) { - cull = GL_CullSphere( ent->origin, model->radius ); - if( cull == CULL_OUT ) { + if (glr.entrotated) { + cull = GL_CullSphere(ent->origin, model->radius); + if (cull == CULL_OUT) { c.spheresCulled++; return; } - if( cull == CULL_CLIP ) { - VectorCopy( model->mins, bounds[0] ); - VectorCopy( model->maxs, bounds[1] ); - cull = GL_CullLocalBox( ent->origin, bounds ); - if( cull == CULL_OUT ) { + if (cull == CULL_CLIP) { + VectorCopy(model->mins, bounds[0]); + VectorCopy(model->maxs, bounds[1]); + cull = GL_CullLocalBox(ent->origin, bounds); + if (cull == CULL_OUT) { c.rotatedBoxesCulled++; return; } } - VectorSubtract( glr.fd.vieworg, ent->origin, temp ); - transformed[0] = DotProduct( temp, glr.entaxis[0] ); - transformed[1] = DotProduct( temp, glr.entaxis[1] ); - transformed[2] = DotProduct( temp, glr.entaxis[2] ); + VectorSubtract(glr.fd.vieworg, ent->origin, temp); + transformed[0] = DotProduct(temp, glr.entaxis[0]); + transformed[1] = DotProduct(temp, glr.entaxis[1]); + transformed[2] = DotProduct(temp, glr.entaxis[2]); } else { - VectorAdd( model->mins, ent->origin, bounds[0] ); - VectorAdd( model->maxs, ent->origin, bounds[1] ); - cull = GL_CullBox( bounds ); - if( cull == CULL_OUT ) { + VectorAdd(model->mins, ent->origin, bounds[0]); + VectorAdd(model->maxs, ent->origin, bounds[1]); + cull = GL_CullBox(bounds); + if (cull == CULL_OUT) { c.boxesCulled++; return; } - VectorSubtract( glr.fd.vieworg, ent->origin, transformed ); - if( VectorEmpty( ent->origin ) && model->drawframe != glr.drawframe ) { - mask = SURF_TRANS33|SURF_TRANS66; + VectorSubtract(glr.fd.vieworg, ent->origin, transformed); + if (VectorEmpty(ent->origin) && model->drawframe != glr.drawframe) { + mask = SURF_TRANS33 | SURF_TRANS66; } } @@ -345,42 +354,42 @@ void GL_DrawBspModel( mmodel_t *model ) { #if USE_DLIGHTS glr.dlightframe++; - if( gl_dynamic->integer == 1 ) { - GL_TransformLights( model ); + if (gl_dynamic->integer == 1) { + GL_TransformLights(model); } #endif - if( gl_dynamic->integer ) { + if (gl_dynamic->integer) { GL_BeginLights(); } qglPushMatrix(); - qglTranslatef( ent->origin[0], ent->origin[1], ent->origin[2] ); - if( glr.entrotated ) { - qglRotatef( ent->angles[YAW], 0, 0, 1 ); - qglRotatef( ent->angles[PITCH], 0, 1, 0 ); - qglRotatef( ent->angles[ROLL], 1, 0, 0 ); + qglTranslatef(ent->origin[0], ent->origin[1], ent->origin[2]); + if (glr.entrotated) { + qglRotatef(ent->angles[YAW], 0, 0, 1); + qglRotatef(ent->angles[PITCH], 0, 1, 0); + qglRotatef(ent->angles[ROLL], 1, 0, 0); } // draw visible faces // FIXME: go by headnode instead? face = model->firstface; count = model->numfaces; - while( count-- ) { - dot = PlaneDiffFast( transformed, face->plane ); - if( BSP_CullFace( face, dot ) ) { + while (count--) { + dot = PlaneDiffFast(transformed, face->plane); + if (BSP_CullFace(face, dot)) { c.facesCulled++; - } else if( face->drawflags & mask ) { + } else if (face->drawflags & mask) { // FIXME: alpha faces are not supported // on rotated or translated inline models - GL_AddAlphaFace( face ); + GL_AddAlphaFace(face); } else { - GL_AddSolidFace( face ); + GL_AddSolidFace(face); } face++; } - if( gl_dynamic->integer ) { + if (gl_dynamic->integer) { GL_EndLights(); } @@ -392,23 +401,24 @@ void GL_DrawBspModel( mmodel_t *model ) { #define NODE_CLIPPED 0 #define NODE_UNCLIPPED 15 -static inline qboolean GL_ClipNode( mnode_t *node, int *clipflags ) { +static inline qboolean GL_ClipNode(mnode_t *node, int *clipflags) +{ int flags = *clipflags; int i, bits, mask; - - if( flags == NODE_UNCLIPPED ) { + + if (flags == NODE_UNCLIPPED) { return qtrue; } - for( i = 0, mask = 1; i < 4; i++, mask <<= 1 ) { - if( flags & mask ) { + for (i = 0, mask = 1; i < 4; i++, mask <<= 1) { + if (flags & mask) { continue; } - bits = BoxOnPlaneSide( node->mins, node->maxs, - &glr.frustumPlanes[i] ); - if( bits == BOX_BEHIND ) { + bits = BoxOnPlaneSide(node->mins, node->maxs, + &glr.frustumPlanes[i]); + if (bits == BOX_BEHIND) { return qfalse; } - if( bits == BOX_INFRONT ) { + if (bits == BOX_INFRONT) { flags |= mask; } } @@ -418,101 +428,106 @@ static inline qboolean GL_ClipNode( mnode_t *node, int *clipflags ) { return qtrue; } -static inline void GL_DrawLeaf( mleaf_t *leaf ) { +static inline void GL_DrawLeaf(mleaf_t *leaf) +{ mface_t **face, **last; - if( leaf->contents == CONTENTS_SOLID ) { + if (leaf->contents == CONTENTS_SOLID) { return; // solid leaf } - if( glr.fd.areabits && !Q_IsBitSet( glr.fd.areabits, leaf->area ) ) { + if (glr.fd.areabits && !Q_IsBitSet(glr.fd.areabits, leaf->area)) { return; // door blocks sight } last = leaf->firstleafface + leaf->numleaffaces; - for( face = leaf->firstleafface; face < last; face++ ) { + for (face = leaf->firstleafface; face < last; face++) { (*face)->drawframe = glr.drawframe; } c.leavesDrawn++; } -static inline void GL_AddGenericFace( mface_t *face ) { - if( face->drawflags & SURF_SKY ) { - R_AddSkySurface( face ); +static inline void GL_AddGenericFace(mface_t *face) +{ + if (face->drawflags & SURF_SKY) { + R_AddSkySurface(face); return; } - if( face->drawflags & (SURF_TRANS33|SURF_TRANS66) ) { - GL_AddAlphaFace( face ); + if (face->drawflags & (SURF_TRANS33 | SURF_TRANS66)) { + GL_AddAlphaFace(face); return; } - GL_AddSolidFace( face ); + GL_AddSolidFace(face); } -static inline void GL_DrawNode( mnode_t *node, vec_t dot ) { +static inline void GL_DrawNode(mnode_t *node, vec_t dot) +{ mface_t *face, *last = node->firstface + node->numfaces; - for( face = node->firstface; face < last; face++ ) { - if( face->drawframe != glr.drawframe ) { + for (face = node->firstface; face < last; face++) { + if (face->drawframe != glr.drawframe) { continue; } - if( BSP_CullFace( face, dot ) ) { + if (BSP_CullFace(face, dot)) { c.facesCulled++; } else { - GL_AddGenericFace( face ); + GL_AddGenericFace(face); } } c.nodesDrawn++; } -static void GL_WorldNode_r( mnode_t *node, int clipflags ) { +static void GL_WorldNode_r(mnode_t *node, int clipflags) +{ int side; vec_t dot; - while( node->visframe == glr.visframe ) { - if( !GL_ClipNode( node, &clipflags ) ) { + while (node->visframe == glr.visframe) { + if (!GL_ClipNode(node, &clipflags)) { c.nodesCulled++; break; } - if( !node->plane ) { - GL_DrawLeaf( ( mleaf_t * )node ); + if (!node->plane) { + GL_DrawLeaf((mleaf_t *)node); break; } - dot = PlaneDiffFast( glr.fd.vieworg, node->plane ); + dot = PlaneDiffFast(glr.fd.vieworg, node->plane); side = dot < 0; - GL_WorldNode_r( node->children[side], clipflags ); + GL_WorldNode_r(node->children[side], clipflags); - GL_DrawNode( node, dot ); + GL_DrawNode(node, dot); - node = node->children[ side ^ 1 ]; + node = node->children[side ^ 1]; } } -void GL_DrawWorld( void ) { +void GL_DrawWorld(void) +{ GL_MarkLeaves(); #if USE_DLIGHTS glr.dlightframe++; - if( gl_dynamic->integer == 1 ) { + if (gl_dynamic->integer == 1) { GL_MarkLights(); } #endif - + R_ClearSkyBox(); - if( gl_dynamic->integer ) { + if (gl_dynamic->integer) { GL_BeginLights(); } - GL_WorldNode_r( gl_static.world.cache->nodes, - gl_cull_nodes->integer ? NODE_CLIPPED : NODE_UNCLIPPED ); + GL_WorldNode_r(gl_static.world.cache->nodes, + gl_cull_nodes->integer ? NODE_CLIPPED : NODE_UNCLIPPED); - if( gl_dynamic->integer ) { + if (gl_dynamic->integer) { GL_EndLights(); } |