Skip to content

Commit

Permalink
#5566: Add alphatest support to the z-fill program (WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
codereader committed Apr 9, 2021
1 parent 3a59cb0 commit 613068d
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 26 deletions.
40 changes: 19 additions & 21 deletions install/gl/zfill_alpha_fp.glsl
@@ -1,27 +1,25 @@
/// ============================================================================
/*
Copyright (C) 2004 Robert Beckebans <trebor_7@users.sourceforge.net>
Please see the file "CONTRIBUTORS" for a list of contributors

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
uniform sampler2D u_diffuse;
uniform float u_alpha_test;

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.
varying vec2 var_tex_diffuse;

See the GNU Lesser General Public License for more details.
void main()
{
if (u_alpha_test < 0)
{
gl_FragColor.a = 1.0;
gl_FragColor.rgb = vec3(1.0, 1.0, 0.0);
}
else
{
vec4 tex = texture2D(u_diffuse, var_tex_diffuse);

You should have received a copy of the GNU Lesser General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
/// ============================================================================
if (tex.a <= u_alpha_test)
{
discard;
}

void main()
{
gl_FragColor.a = 1.0;
gl_FragColor.rgb = vec3(1.0, 1.0, 0.0);
gl_FragColor = tex;
}
}
4 changes: 3 additions & 1 deletion install/gl/zfill_vp.glsl
Expand Up @@ -22,13 +22,15 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

attribute vec4 attr_TexCoord0;

varying vec2 var_tex_diffuse;

void main()
{
// transform vertex position into homogenous clip-space
gl_Position = ftransform();

// transform texcoords
gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0;
var_tex_diffuse = (gl_TextureMatrix[0] * attr_TexCoord0).st;

// assign color
gl_FrontColor = gl_Color;
Expand Down
26 changes: 25 additions & 1 deletion radiantcore/rendersystem/backend/OpenGLShader.cpp
Expand Up @@ -312,6 +312,7 @@ void OpenGLShader::appendInteractionLayer(const DBSTriplet& triplet)
alphaTest = triplet.diffuse->getAlphaTest();
}

#if 0
// Append a depthfill shader pass if requested (not applicable for
// alpha-test materials)
if (triplet.needDepthFill && alphaTest <= 0.0)
Expand All @@ -329,15 +330,38 @@ void OpenGLShader::appendInteractionLayer(const DBSTriplet& triplet)

zPass.glProgram = _renderSystem.getGLProgramFactory().getBuiltInProgram("depthFill");
}
else
#else
if (triplet.needDepthFill && triplet.diffuse)
{
// Create depth-buffer fill pass with alpha test
OpenGLState& zPass = appendDefaultPass();
zPass.setRenderFlag(RENDER_MASKCOLOUR);
zPass.setRenderFlag(RENDER_FILL);
zPass.setRenderFlag(RENDER_CULLFACE);
zPass.setRenderFlag(RENDER_DEPTHTEST);
zPass.setRenderFlag(RENDER_DEPTHWRITE);
zPass.setRenderFlag(RENDER_PROGRAM);
zPass.setRenderFlag(RENDER_ALPHATEST);
zPass.setRenderFlag(RENDER_TEXTURE_2D);
zPass.setRenderFlag(RENDER_BUMP);
zPass.alphaThreshold = static_cast<GLfloat>(alphaTest);

zPass.setSortPosition(OpenGLState::SORT_ZFILL);
zPass.stage0 = triplet.diffuse;
zPass.texture0 = getTextureOrInteractionDefault(triplet.diffuse)->getGLTexNum();

zPass.glProgram = _renderSystem.getGLProgramFactory().getBuiltInProgram("depthFillAlpha");
}
#endif
// Add the DBS pass
OpenGLState& dbsPass = appendDefaultPass();

// Populate the textures and remember the stage reference
setGLTexturesFromTriplet(dbsPass, triplet);

// Set render flags
dbsPass.setRenderFlag(RENDER_BLEND);
//dbsPass.setRenderFlag(RENDER_BLEND);
dbsPass.setRenderFlag(RENDER_FILL);
dbsPass.setRenderFlag(RENDER_TEXTURE_2D);
dbsPass.setRenderFlag(RENDER_CULLFACE);
Expand Down
16 changes: 16 additions & 0 deletions radiantcore/rendersystem/backend/OpenGLShaderPass.cpp
Expand Up @@ -11,6 +11,8 @@
#include "debugging/render.h"
#include "debugging/gl.h"

#include "glprogram/GLSLDepthFillAlphaProgram.h"

namespace render
{

Expand Down Expand Up @@ -303,6 +305,20 @@ void OpenGLShaderPass::applyState(OpenGLState& current,
{
current.glProgram->enable();
}

// Check if we need to set the alpha test value
if ((requiredState & (RENDER_ALPHATEST | RENDER_FILL)) == (RENDER_ALPHATEST | RENDER_FILL))
{
auto zFillAlphaProgram = dynamic_cast<GLSLDepthFillAlphaProgram*>(current.glProgram);

if (zFillAlphaProgram != nullptr)
{
zFillAlphaProgram->applyAlphaTest(_glState.alphaThreshold);

setTextureState(current.texture0, _glState.texture0, GL_TEXTURE0, GL_TEXTURE_2D);
setupTextureMatrix(GL_TEXTURE0, _glState.stage0);
}
}
}

// State changes. Only perform these if changingBitsMask > 0, since if there are
Expand Down
4 changes: 1 addition & 3 deletions radiantcore/rendersystem/backend/glprogram/GLSLBumpProgram.h
Expand Up @@ -9,6 +9,7 @@ namespace render
class GLSLBumpProgram :
public GLSLProgramBase
{
private:
// The value all lights should be scaled by, obtained from the game description
float _lightScale;

Expand All @@ -20,9 +21,6 @@ class GLSLBumpProgram :
int _locAmbientLight;
int _locInvertVCol;

// Program object identifier
GLuint _programObj;

public:

/* GLProgram implementation */
Expand Down
Expand Up @@ -23,6 +23,49 @@ void GLSLDepthFillAlphaProgram::create()
DEPTHFILL_ALPHA_VP_FILENAME, DEPTHFILL_ALPHA_FP_FILENAME
);

glBindAttribLocation(_programObj, ATTR_TEXCOORD, "attr_TexCoord0");

glLinkProgram(_programObj);

debug::assertNoGlErrors();

_locAlphaTest = glGetUniformLocation(_programObj, "u_alpha_test");

glUseProgram(_programObj);
debug::assertNoGlErrors();

GLint samplerLoc = glGetUniformLocation(_programObj, "u_diffuse");
glUniform1i(samplerLoc, 0);

debug::assertNoGlErrors();
}

void GLSLDepthFillAlphaProgram::enable()
{
GLSLProgramBase::enable();

glEnableVertexAttribArrayARB(ATTR_TEXCOORD);
}

void GLSLDepthFillAlphaProgram::disable()
{
GLSLProgramBase::disable();

glDisableVertexAttribArrayARB(ATTR_TEXCOORD);
}

void GLSLDepthFillAlphaProgram::applyAlphaTest(float alphaTest)
{
glUniform1f(_locAlphaTest, alphaTest);

debug::assertNoGlErrors();

glActiveTexture(GL_TEXTURE0);
glClientActiveTexture(GL_TEXTURE0);

glMatrixMode(GL_TEXTURE);
glLoadIdentity();

debug::assertNoGlErrors();
}

Expand Down
Expand Up @@ -8,8 +8,15 @@ namespace render
class GLSLDepthFillAlphaProgram :
public GLSLProgramBase
{
private:
GLint _locAlphaTest;

public:
void create() override;
void enable() override;
void disable() override;

void applyAlphaTest(float alphaTest);
};

}

0 comments on commit 613068d

Please sign in to comment.