Skip to content

Commit

Permalink
- replaced deprecated alpha testing with shader code.
Browse files Browse the repository at this point in the history
  • Loading branch information
Evghenii committed Aug 27, 2024
1 parent e1cd9e7 commit 08fc74a
Show file tree
Hide file tree
Showing 16 changed files with 49 additions and 99 deletions.
4 changes: 1 addition & 3 deletions src/gl/renderer/gl_renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ void FGLRenderer::DrawTexture(FTexture *img, DCanvas::DrawParms &parms)

glColor4f(r, g, b, FIXED2FLOAT(parms.alpha));

gl_RenderState.EnableAlphaTest(false);
gl_RenderState.AlphaFunc(GL_GEQUAL, 0.f);
gl_RenderState.Apply();
glBegin(GL_TRIANGLE_STRIP);
glTexCoord2f(u1, v1);
Expand Down Expand Up @@ -442,8 +442,6 @@ void FGLRenderer::DrawTexture(FTexture *img, DCanvas::DrawParms &parms)
glEnd();
}

gl_RenderState.EnableAlphaTest(true);

glScissor(0, 0, screen->GetWidth(), screen->GetHeight());
glDisable(GL_SCISSOR_TEST);
gl_RenderState.SetTextureMode(TM_MODULATE);
Expand Down
16 changes: 2 additions & 14 deletions src/gl/renderer/gl_renderstate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,6 @@ void FRenderState::Reset()
mSrcBlend = GL_SRC_ALPHA;
mDstBlend = GL_ONE_MINUS_SRC_ALPHA;
glSrcBlend = glDstBlend = -1;
glAlphaFunc = -1;
mAlphaFunc = GL_GEQUAL;
mAlphaThreshold = 0.5f;
mBlendEquation = GL_FUNC_ADD;
glBlendEquation = -1;
Expand Down Expand Up @@ -209,6 +207,8 @@ bool FRenderState::ApplyShader()
}
}

glUniform1f(activeShader->alpha_threshold, mAlphaThreshold);

if (fogset != activeShader->currentfogenabled)
{
glUniform1i(activeShader->fogenabled_index, (activeShader->currentfogenabled = fogset));
Expand Down Expand Up @@ -280,18 +280,6 @@ void FRenderState::Apply(bool forcenoshader)
glDstBlend = mDstBlend;
glBlendFunc(mSrcBlend, mDstBlend);
}
if (mAlphaFunc != glAlphaFunc || mAlphaThreshold != glAlphaThreshold)
{
glAlphaFunc = mAlphaFunc;
glAlphaThreshold = mAlphaThreshold;
::glBlendFunc(mAlphaFunc, mAlphaThreshold);
}
if (mAlphaTest != glAlphaTest)
{
glAlphaTest = mAlphaTest;
if (mAlphaTest) glEnable(GL_ALPHA_TEST);
else glDisable(GL_ALPHA_TEST);
}
if (mBlendEquation != glBlendEquation)
{
glBlendEquation = mBlendEquation;
Expand Down
28 changes: 2 additions & 26 deletions src/gl/renderer/gl_renderstate.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,7 @@ class FRenderState
int mNumLights[3];
float *mLightData;
int mSrcBlend, mDstBlend;
int mAlphaFunc;
float mAlphaThreshold;
bool mAlphaTest;
int mBlendEquation;
bool m2D;

Expand All @@ -113,9 +111,7 @@ class FRenderState
float mWarpTime;

int glSrcBlend, glDstBlend;
int glAlphaFunc;
float glAlphaThreshold;
bool glAlphaTest;
int glBlendEquation;

bool ffTextureEnabled;
Expand Down Expand Up @@ -236,28 +232,8 @@ class FRenderState

void AlphaFunc(int func, float thresh)
{
if (!gl_direct_state_change)
{
mAlphaFunc = func;
mAlphaThreshold = thresh;
}
else
{
::glAlphaFunc(func, thresh);
}
}

void EnableAlphaTest(bool on)
{
if (!gl_direct_state_change)
{
mAlphaTest = on;
}
else
{
if (on) glEnable(GL_ALPHA_TEST);
else glDisable(GL_ALPHA_TEST);
}
if (func == GL_GREATER) mAlphaThreshold = thresh;
else mAlphaThreshold = thresh - 0.001f;
}

void BlendEquation(int eq)
Expand Down
2 changes: 1 addition & 1 deletion src/gl/scene/gl_decal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ void GLWall::DrawDecal(DBaseDecal *decal)
gl_SetRenderStyle(decal->RenderStyle, false, false);

// If srcalpha is one it looks better with a higher alpha threshold
if (decal->RenderStyle.SrcAlpha == STYLEALPHA_One) gl_RenderState.AlphaFunc(GL_GEQUAL, gl_mask_threshold);
if (decal->RenderStyle.SrcAlpha == STYLEALPHA_One) gl_RenderState.AlphaFunc(GL_GEQUAL, gl_mask_sprite_threshold);
else gl_RenderState.AlphaFunc(GL_GREATER, 0.f);

gl_RenderState.Apply();
Expand Down
2 changes: 1 addition & 1 deletion src/gl/scene/gl_flats.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ void GLFlat::Draw(int pass)
if (renderstyle==STYLE_Add) gl_RenderState.BlendFunc(GL_SRC_ALPHA, GL_ONE);
gl_SetColor(lightlevel, rel, &Colormap, alpha);
gl_SetFog(lightlevel, rel, &Colormap, false);
gl_RenderState.AlphaFunc(GL_GEQUAL,gl_mask_threshold*(alpha));
gl_RenderState.AlphaFunc(GL_GEQUAL, gl_mask_threshold);
if (!gltexture)
{
gl_RenderState.EnableTexture(false);
Expand Down
4 changes: 2 additions & 2 deletions src/gl/scene/gl_portal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -934,7 +934,7 @@ int GLMirrorPortal::ClipPoint(fixed_t x, fixed_t y)
// are 2 problems with it:
//
// 1. Setting this up completely negates any performance gains.
// 2. It doesn't work with a 360° field of view (as when you are looking up.)
// 2. It doesn't work with a 360 field of view (as when you are looking up.)
//
//
// So the brute force mechanism is just as good.
Expand Down Expand Up @@ -985,7 +985,7 @@ void GLHorizonPortal::DrawContents()

gltexture->Bind(origin->colormap.colormap);

gl_RenderState.EnableAlphaTest(false);
gl_RenderState.AlphaFunc(GL_GEQUAL, 0.f);
gl_RenderState.BlendFunc(GL_ONE,GL_ZERO);
gl_RenderState.Apply();

Expand Down
23 changes: 9 additions & 14 deletions src/gl/scene/gl_scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ void FGLRenderer::RenderScene(int recursion)
glDepthFunc(GL_LESS);


gl_RenderState.EnableAlphaTest(false);
gl_RenderState.AlphaFunc(GL_GEQUAL, 0.f);

glDisable(GL_POLYGON_OFFSET_FILL); // just in case

Expand Down Expand Up @@ -422,16 +422,14 @@ void FGLRenderer::RenderScene(int recursion)
gl_drawinfo->drawlists[GLDL_LIGHTFOG].Draw(pass);


gl_RenderState.EnableAlphaTest(true);

// Part 2: masked geometry. This is set up so that only pixels with alpha>0.5 will show
if (!gl_texture)
{
gl_RenderState.EnableTexture(true);
gl_RenderState.SetTextureMode(TM_MASK);
}
if (pass == GLPASS_BASE) pass = GLPASS_BASE_MASKED;
gl_RenderState.AlphaFunc(GL_GEQUAL,gl_mask_threshold);
gl_RenderState.AlphaFunc(GL_GEQUAL, gl_mask_threshold);
gl_RenderState.EnableBrightmap(true);
gl_drawinfo->drawlists[GLDL_MASKED].Sort();
gl_drawinfo->drawlists[GLDL_MASKED].Draw(pass);
Expand Down Expand Up @@ -490,10 +488,10 @@ void FGLRenderer::RenderScene(int recursion)
glDepthFunc(GL_LEQUAL);
if (gl_texture)
{
gl_RenderState.EnableAlphaTest(false);
gl_RenderState.AlphaFunc(GL_GEQUAL, 0.f);
gl_drawinfo->drawlists[GLDL_LIGHT].Sort();
gl_drawinfo->drawlists[GLDL_LIGHT].Draw(GLPASS_TEXTURE);
gl_RenderState.EnableAlphaTest(true);
gl_RenderState.AlphaFunc(GL_GEQUAL, gl_mask_threshold);
gl_drawinfo->drawlists[GLDL_LIGHTBRIGHT].Sort();
gl_drawinfo->drawlists[GLDL_LIGHTBRIGHT].Draw(GLPASS_TEXTURE);
gl_drawinfo->drawlists[GLDL_LIGHTMASKED].Sort();
Expand Down Expand Up @@ -550,10 +548,9 @@ void FGLRenderer::RenderScene(int recursion)
{
glDepthMask(false); // don't write to Z-buffer!
gl_RenderState.EnableFog(true);
gl_RenderState.EnableAlphaTest(false);
gl_RenderState.AlphaFunc(GL_GEQUAL, 0.f);
gl_RenderState.BlendFunc(GL_ONE,GL_ZERO);
gl_drawinfo->DrawUnhandledMissingTextures();
gl_RenderState.EnableAlphaTest(true);
}
glDepthMask(true);

Expand All @@ -579,8 +576,7 @@ void FGLRenderer::RenderTranslucent()
gl_RenderState.SetCameraPos(FIXED2FLOAT(viewx), FIXED2FLOAT(viewy), FIXED2FLOAT(viewz));

// final pass: translucent stuff
gl_RenderState.EnableAlphaTest(true);
gl_RenderState.AlphaFunc(GL_GEQUAL,gl_mask_sprite_threshold);
gl_RenderState.AlphaFunc(GL_GEQUAL, gl_mask_sprite_threshold);
gl_RenderState.BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

gl_RenderState.EnableBrightmap(true);
Expand All @@ -590,7 +586,7 @@ void FGLRenderer::RenderTranslucent()

glDepthMask(true);

gl_RenderState.AlphaFunc(GL_GEQUAL,0.5f);
gl_RenderState.AlphaFunc(GL_GEQUAL, 0.5f);
RenderAll.Unclock();
}

Expand Down Expand Up @@ -718,7 +714,7 @@ void FGLRenderer::DrawBlend(sector_t * viewsector)
// black multiplicative blends are ignored
if (extra_red || extra_green || extra_blue)
{
gl_RenderState.EnableAlphaTest(false);
gl_RenderState.AlphaFunc(GL_GEQUAL, 0.f);
gl_RenderState.EnableTexture(false);
gl_RenderState.BlendFunc(GL_DST_COLOR,GL_ZERO);
glColor4f(extra_red, extra_green, extra_blue, 1.0f);
Expand Down Expand Up @@ -756,7 +752,7 @@ void FGLRenderer::DrawBlend(sector_t * viewsector)
if (blend[3]>0.0f)
{
gl_RenderState.BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
gl_RenderState.EnableAlphaTest(false);
gl_RenderState.AlphaFunc(GL_GEQUAL, 0.f);
gl_RenderState.EnableTexture(false);
glColor4fv(blend);
gl_RenderState.Apply(true);
Expand Down Expand Up @@ -809,7 +805,6 @@ void FGLRenderer::EndDrawScene(sector_t * viewsector)
gl_RenderState.BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glColor3f(1.0f,1.0f,1.0f);
gl_RenderState.EnableTexture(true);
gl_RenderState.EnableAlphaTest(true);
glDisable(GL_SCISSOR_TEST);
}

Expand Down
8 changes: 3 additions & 5 deletions src/gl/scene/gl_skydome.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@

//-----------------------------------------------------------------------------
//
// Shamelessly lifted from Doomsday (written by Jaakko Keränen)
// Shamelessly lifted from Doomsday (written by Jaakko Ker�nen)
// also shamelessly lifted from ZDoomGL! ;)
//
//-----------------------------------------------------------------------------
Expand Down Expand Up @@ -567,7 +567,7 @@ void GLSkyPortal::DrawContents()
}

gl_RenderState.EnableFog(false);
gl_RenderState.EnableAlphaTest(false);
gl_RenderState.AlphaFunc(GL_GEQUAL, 0.f);
gl_RenderState.BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

glMatrixMode(GL_MODELVIEW);
Expand All @@ -588,7 +588,6 @@ void GLSkyPortal::DrawContents()
else R=G=B=1.f;

RenderBox(origin->skytexno1, origin->texture[0], origin->x_offset[0], CM_Index, origin->sky2);
gl_RenderState.EnableAlphaTest(true);
}
else
{
Expand All @@ -601,8 +600,7 @@ void GLSkyPortal::DrawContents()
gl_RenderState.SetTextureMode(TM_MODULATE);
}

gl_RenderState.EnableAlphaTest(true);
gl_RenderState.AlphaFunc(GL_GEQUAL,0.05f);
gl_RenderState.AlphaFunc(GL_GEQUAL, 0.05f);

if (origin->doublesky && origin->texture[1])
{
Expand Down
20 changes: 5 additions & 15 deletions src/gl/scene/gl_sprite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,11 @@ void GLSprite::Draw(int pass)

if (hw_styleflags == STYLEHW_NoAlphaTest)
{
gl_RenderState.EnableAlphaTest(false);
gl_RenderState.AlphaFunc(GL_GEQUAL, 0.f);
}
else
{
gl_RenderState.AlphaFunc(GL_GEQUAL,trans*gl_mask_sprite_threshold);
gl_RenderState.AlphaFunc(GL_GEQUAL, gl_mask_sprite_threshold);
}

if (RenderStyle.BlendOp == STYLEOP_Shadow)
Expand All @@ -180,7 +180,7 @@ void GLSprite::Draw(int pass)
minalpha*=factor;
}

gl_RenderState.AlphaFunc(GL_GEQUAL,minalpha*gl_mask_sprite_threshold);
gl_RenderState.AlphaFunc(GL_GEQUAL, gl_mask_sprite_threshold);
glColor4f(0.2f,0.2f,0.2f,fuzzalpha);
additivefog = true;
}
Expand Down Expand Up @@ -263,7 +263,7 @@ void GLSprite::Draw(int pass)
const bool drawWithXYBillboard = ( (particle && gl_billboard_particles) || (!(actor && actor->renderflags & RF_FORCEYBILLBOARD)
//&& GLRenderer->mViewActor != NULL
&& (gl_billboard_mode == 1 || (actor && actor->renderflags & RF_FORCEXYBILLBOARD ))) );
gl_RenderState.Apply();
gl_RenderState.Apply(hw_styleflags == STYLEHW_NoAlphaTest);

Vector v1;
Vector v2;
Expand Down Expand Up @@ -322,7 +322,7 @@ void GLSprite::Draw(int pass)
gl_RenderState.SetFixedColormap(CM_FOGLAYER);
gl_RenderState.BlendEquation(GL_FUNC_ADD);
gl_RenderState.BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
gl_RenderState.Apply();
gl_RenderState.Apply(hw_styleflags == STYLEHW_NoAlphaTest);

glBegin(GL_TRIANGLE_STRIP);
if (gltexture)
Expand Down Expand Up @@ -405,16 +405,6 @@ void GLSprite::Draw(int pass)
gl_RenderState.BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
gl_RenderState.BlendEquation(GL_FUNC_ADD);
gl_RenderState.SetTextureMode(TM_MODULATE);

// [BB] Restore the alpha test after drawing a smooth particle.
if (hw_styleflags == STYLEHW_NoAlphaTest)
{
gl_RenderState.EnableAlphaTest(true);
}
else
{
gl_RenderState.AlphaFunc(GL_GEQUAL,gl_mask_sprite_threshold);
}
}

// End of gl_sprite_brightfog hack: restore FadeColor to normalcy
Expand Down
10 changes: 4 additions & 6 deletions src/gl/scene/gl_walls_draw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -304,9 +304,8 @@ void GLWall::RenderFogBoundary()
int rel = rellight + getExtraLight();
gl_SetFog(lightlevel, rel, &Colormap, false);
gl_RenderState.SetEffect(EFF_FOGBOUNDARY);
gl_RenderState.EnableAlphaTest(false);
gl_RenderState.AlphaFunc(GL_GEQUAL, 0.f);
RenderWall(0, NULL);
gl_RenderState.EnableAlphaTest(true);
gl_RenderState.SetEffect(EFF_NONE);
}
else
Expand Down Expand Up @@ -382,7 +381,7 @@ void GLWall::RenderMirrorSurface()

// Restore the defaults for the translucent pass
gl_RenderState.BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
gl_RenderState.AlphaFunc(GL_GEQUAL,0.5f*gl_mask_sprite_threshold);
gl_RenderState.AlphaFunc(GL_GEQUAL, gl_mask_sprite_threshold);
glDepthFunc(GL_LESS);

// This is drawn in the translucent pass which is done after the decal pass
Expand Down Expand Up @@ -416,8 +415,8 @@ void GLWall::RenderTranslucentWall()
// and until that changes I won't fix this code for the new blending modes!
bool isadditive = RenderStyle == STYLE_Add;

if (!transparent) gl_RenderState.AlphaFunc(GL_GEQUAL,gl_mask_threshold*fabs(alpha));
else gl_RenderState.EnableAlphaTest(false);
if (!transparent) gl_RenderState.AlphaFunc(GL_GEQUAL, gl_mask_threshold);
else gl_RenderState.AlphaFunc(GL_GEQUAL, 0.f);
if (isadditive) gl_RenderState.BlendFunc(GL_SRC_ALPHA,GL_ONE);

int extra;
Expand All @@ -442,7 +441,6 @@ void GLWall::RenderTranslucentWall()

// restore default settings
if (isadditive) gl_RenderState.BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
if (transparent) gl_RenderState.EnableAlphaTest(true);

if (!gltexture)
{
Expand Down
7 changes: 2 additions & 5 deletions src/gl/scene/gl_weapon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ void FGLRenderer::DrawPSprite (player_t * player,pspdef_t *psp,fixed_t sx, fixed

if (tex->GetTransparent() || OverrideShader != 0)
{
gl_RenderState.EnableAlphaTest(false);
gl_RenderState.AlphaFunc(GL_GEQUAL, 0.f);
}
gl_RenderState.Apply();
glBegin(GL_TRIANGLE_STRIP);
Expand All @@ -160,10 +160,7 @@ void FGLRenderer::DrawPSprite (player_t * player,pspdef_t *psp,fixed_t sx, fixed
glTexCoord2f(fU2, fV1); glVertex2f(x2,y1);
glTexCoord2f(fU2, fV2); glVertex2f(x2,y2);
glEnd();
if (tex->GetTransparent() || OverrideShader != 0)
{
gl_RenderState.EnableAlphaTest(true);
}
gl_RenderState.AlphaFunc(GL_GEQUAL, 0.5f);
}

//==========================================================================
Expand Down
Loading

0 comments on commit 08fc74a

Please sign in to comment.