From cbbae0c9bfcf968d62d7c5854e8f30ea757a308e Mon Sep 17 00:00:00 2001 From: Alibek Omarov Date: Mon, 6 Apr 2026 08:25:41 +0500 Subject: [PATCH] ref: gl: simplify backface culling checks in frustum class --- ref/gl/gl_cull.c | 40 ++++++++++++++-------------------------- ref/gl/gl_frustum.c | 17 ++++++----------- 2 files changed, 20 insertions(+), 37 deletions(-) diff --git a/ref/gl/gl_cull.c b/ref/gl/gl_cull.c index 27f394a4..a897db8a 100644 --- a/ref/gl/gl_cull.c +++ b/ref/gl/gl_cull.c @@ -67,10 +67,10 @@ int R_CullSurface( const msurface_t *surf, const gl_frustum_t *frustum, uint cli { cl_entity_t *e = RI.currententity; - if( !e || !surf || !surf->texinfo || !surf->texinfo->texture ) + if( unlikely( !e || !surf || !surf->texinfo || !surf->texinfo->texture )) return CULL_OTHER; - if( r_nocull.value ) + if( unlikely( r_nocull.value )) return CULL_VISIBLE; // world surfaces can be culled by vis frame too @@ -78,9 +78,10 @@ int R_CullSurface( const msurface_t *surf, const gl_frustum_t *frustum, uint cli return CULL_VISFRAME; // only static ents can be culled by frustum - if( !R_StaticEntity( e )) frustum = NULL; + if( !R_StaticEntity( e )) + frustum = NULL; - if( !VectorIsNull( surf->plane->normal )) + if( glState.faceCull != GL_NONE && !VectorIsNull( surf->plane->normal )) { float dist; @@ -95,35 +96,22 @@ int R_CullSurface( const msurface_t *surf, const gl_frustum_t *frustum, uint cli } else dist = PlaneDiff( tr.modelorg, surf->plane ); + if( FBitSet( surf->flags, SURF_PLANEBACK )) + dist = -dist; + if( glState.faceCull == GL_FRONT ) { - if( FBitSet( surf->flags, SURF_PLANEBACK )) - { - if( dist >= -BACKFACE_EPSILON ) - return CULL_BACKSIDE; // wrong side - } - else - { - if( dist <= BACKFACE_EPSILON ) - return CULL_BACKSIDE; // wrong side - } + if( dist <= BACKFACE_EPSILON ) + return CULL_BACKSIDE; } - else if( glState.faceCull == GL_BACK ) + else // if( glState.faceCull == GL_BACK ) { - if( FBitSet( surf->flags, SURF_PLANEBACK )) - { - if( dist <= BACKFACE_EPSILON ) - return CULL_BACKSIDE; // wrong side - } - else - { - if( dist >= -BACKFACE_EPSILON ) - return CULL_BACKSIDE; // wrong side - } + if( dist >= -BACKFACE_EPSILON ) + return CULL_BACKSIDE; } } - if( frustum && GL_FrustumCullBox( frustum, surf->info->mins, surf->info->maxs, clipflags )) + if( frustum && clipflags && GL_FrustumCullBox( frustum, surf->info->mins, surf->info->maxs, clipflags )) return CULL_FRUSTUM; return CULL_VISIBLE; diff --git a/ref/gl/gl_frustum.c b/ref/gl/gl_frustum.c index 0e4b2103..06e54af6 100644 --- a/ref/gl/gl_frustum.c +++ b/ref/gl/gl_frustum.c @@ -99,15 +99,14 @@ void GL_FrustumInitOrtho( gl_frustum_t *out, float xLeft, float xRight, float yT // cull methods qboolean GL_FrustumCullBox( const gl_frustum_t *out, const vec3_t mins, const vec3_t maxs, int userClipFlags ) { - int iClipFlags; + int iClipFlags = userClipFlags != 0 ? userClipFlags : out->clipFlags; int i, bit; - if( r_nocull.value ) + if( unlikely( r_nocull.value )) return false; - if( userClipFlags != 0 ) - iClipFlags = userClipFlags; - else iClipFlags = out->clipFlags; + if( !iClipFlags ) + return false; for( i = FRUSTUM_PLANES, bit = 1; i > 0; i--, bit <<= 1 ) { @@ -160,16 +159,12 @@ qboolean GL_FrustumCullBox( const gl_frustum_t *out, const vec3_t mins, const ve qboolean GL_FrustumCullSphere( const gl_frustum_t *out, const vec3_t center, float radius, int userClipFlags ) { - int iClipFlags; + int iClipFlags = userClipFlags != 0 ? userClipFlags : out->clipFlags; int i, bit; - if( r_nocull.value ) + if( unlikely( r_nocull.value )) return false; - if( userClipFlags != 0 ) - iClipFlags = userClipFlags; - else iClipFlags = out->clipFlags; - for( i = FRUSTUM_PLANES, bit = 1; i > 0; i--, bit <<= 1 ) { const mplane_t *p = &out->planes[FRUSTUM_PLANES - i];