From 06d7bd9c7475e05bba99ba11814bb99b301df1f2 Mon Sep 17 00:00:00 2001 From: dfighter1985 Date: Fri, 13 Sep 2013 01:29:01 +0200 Subject: [PATCH] Added alpha test to generated shaders. --HG-- branch : gsoc2013-dfighter --- code/nel/include/nel/3d/i_program_object.h | 1 + .../3d/driver/OpenGL3/driver_glsl_program.cpp | 3 +- .../OpenGL3/driver_glsl_shader_generator.cpp | 32 +++++++++++++++++-- .../OpenGL3/driver_glsl_shader_generator.h | 2 ++ .../driver/OpenGL3/driver_opengl_program.cpp | 3 ++ .../OpenGL3/driver_opengl_shader_desc.h | 20 ++++++++++++ 6 files changed, 57 insertions(+), 4 deletions(-) diff --git a/code/nel/include/nel/3d/i_program_object.h b/code/nel/include/nel/3d/i_program_object.h index 61a1a7ddb..5b0c1dc71 100644 --- a/code/nel/include/nel/3d/i_program_object.h +++ b/code/nel/include/nel/3d/i_program_object.h @@ -45,6 +45,7 @@ namespace NL3D Sampler1, Sampler2, Sampler3, + AlphaTreshold, NUM_UNIFORMS }; diff --git a/code/nel/src/3d/driver/OpenGL3/driver_glsl_program.cpp b/code/nel/src/3d/driver/OpenGL3/driver_glsl_program.cpp index 6cf4379f6..bf0b1e6d2 100644 --- a/code/nel/src/3d/driver/OpenGL3/driver_glsl_program.cpp +++ b/code/nel/src/3d/driver/OpenGL3/driver_glsl_program.cpp @@ -190,7 +190,8 @@ namespace NL3D "sampler0", "sampler1", "sampler2", - "sampler3" + "sampler3", + "alphaTreshold" }; void CGLSLProgram::cacheUniformIndices() diff --git a/code/nel/src/3d/driver/OpenGL3/driver_glsl_shader_generator.cpp b/code/nel/src/3d/driver/OpenGL3/driver_glsl_shader_generator.cpp index 97a19d110..f87e69725 100644 --- a/code/nel/src/3d/driver/OpenGL3/driver_glsl_shader_generator.cpp +++ b/code/nel/src/3d/driver/OpenGL3/driver_glsl_shader_generator.cpp @@ -274,6 +274,17 @@ namespace NL3D ss << "uniform vec4 constant3;" << std::endl; } + void CGLSLShaderGenerator::addAlphaTreshold() + { + ss << "uniform float alphaTreshold;" << std::endl; + } + + void CGLSLShaderGenerator::addAlphaTest() + { + if( material->getAlphaTest() ) + ss << "if( fragColor.a <= ( alphaTreshold - 0.0001 ) ) discard;" << std::endl; + } + void CGLSLShaderGenerator::generateNormalVS() { ss << "void main( void )" << std::endl; @@ -464,6 +475,7 @@ namespace NL3D } addColor(); addConstants(); + addAlphaTreshold(); ss << std::endl; @@ -504,10 +516,10 @@ namespace NL3D if( vertexColor ) ss << "texel = color * texel;" << std::endl; - // Alpha test - //ss << "if( texel.a <= 0.5 ) discard;" << std::endl; - ss << "fragColor = texel;" << std::endl; + + addAlphaTest(); + ss << "}" << std::endl; } @@ -872,6 +884,8 @@ namespace NL3D addDiffuse(); + addAlphaTreshold(); + ss << std::endl; ss << "void main( void )" << std::endl; @@ -910,6 +924,7 @@ namespace NL3D } ss << "fragColor = texel;" << std::endl; + addAlphaTest(); ss << "}" << std::endl; ss << std::endl; } @@ -920,6 +935,7 @@ namespace NL3D ss << "uniform sampler2D sampler0;" << std::endl; ss << "uniform samplerCube sampler1;" << std::endl; addDiffuse(); + addAlphaTreshold(); ss << "void main( void )" << std::endl; ss << "{" << std::endl; @@ -931,6 +947,7 @@ namespace NL3D ss << "texel.rgb = texel1.rgb * texel.a + texel.rgb;" << std::endl; ss << "texel.a = texel1.a;" << std::endl; ss << "fragColor = texel;" << std::endl; + addAlphaTest(); ss << "}" << std::endl; } @@ -951,6 +968,8 @@ namespace NL3D addConstants(); + addAlphaTreshold(); + ss << std::endl; ss << "void main( void )" << std::endl; @@ -981,6 +1000,7 @@ namespace NL3D } ss << "fragColor = texel;" << std::endl; + addAlphaTest(); ss << "}" << std::endl; } @@ -1010,6 +1030,8 @@ namespace NL3D if( diffuse ) ss << "uniform sampler2D sampler3;" << std::endl; + addAlphaTreshold(); + ss << std::endl; ss << "void main( void )" << std::endl; @@ -1037,6 +1059,8 @@ namespace NL3D else ss << "fragColor = texel2" << std::endl; + addAlphaTest(); + ss << "}" << std::endl; ss << std::endl; @@ -1047,6 +1071,7 @@ namespace NL3D ss << "uniform sampler2D sampler0;" << std::endl; ss << "uniform sampler2D sampler1;" << std::endl; addDiffuse(); + addAlphaTreshold(); ss << std::endl; ss << "void main( void )" << std::endl; @@ -1056,6 +1081,7 @@ namespace NL3D ss << "vec4 tex = mix( tex0, tex1, diffuse.a );" << std::endl; ss << "tex.a = 0;" << std::endl; ss << "fragColor = tex;" << std::endl; + addAlphaTest(); ss << "}" << std::endl; ss << std::endl; diff --git a/code/nel/src/3d/driver/OpenGL3/driver_glsl_shader_generator.h b/code/nel/src/3d/driver/OpenGL3/driver_glsl_shader_generator.h index 30e6f581b..f4ad2893b 100644 --- a/code/nel/src/3d/driver/OpenGL3/driver_glsl_shader_generator.h +++ b/code/nel/src/3d/driver/OpenGL3/driver_glsl_shader_generator.h @@ -42,6 +42,8 @@ namespace NL3D void addDiffuse(); void addColor(); void addConstants(); + void addAlphaTreshold(); + void addAlphaTest(); void generateNormalVS(); void generateSpecularVS(); diff --git a/code/nel/src/3d/driver/OpenGL3/driver_opengl_program.cpp b/code/nel/src/3d/driver/OpenGL3/driver_opengl_program.cpp index 8910b0f04..791fe8149 100644 --- a/code/nel/src/3d/driver/OpenGL3/driver_opengl_program.cpp +++ b/code/nel/src/3d/driver/OpenGL3/driver_opengl_program.cpp @@ -169,6 +169,9 @@ namespace NL3D } } + desc.setAlphaTest( mat.getAlphaTest() ); + desc.setAlphaTestThreshold( mat.getAlphaTestThreshold() ); + p = shaderCache.findShader( desc ); if( p != NULL ) diff --git a/code/nel/src/3d/driver/OpenGL3/driver_opengl_shader_desc.h b/code/nel/src/3d/driver/OpenGL3/driver_opengl_shader_desc.h index 4f5a6a594..7dfe4419b 100644 --- a/code/nel/src/3d/driver/OpenGL3/driver_opengl_shader_desc.h +++ b/code/nel/src/3d/driver/OpenGL3/driver_opengl_shader_desc.h @@ -35,6 +35,8 @@ namespace NL3D program = NULL; vbFlags = 0; nlightmaps = 0; + alphaTest = false; + alphaTestTreshold = 0.5f; } ~CShaderDesc(){ @@ -51,6 +53,18 @@ namespace NL3D if( nlightmaps != o.nlightmaps ) return false; + if( alphaTest != o.alphaTest ) + return false; + + if( alphaTest ) + { + if( alphaTestTreshold > o.alphaTestTreshold + 0.0001f ) + return false; + + if( alphaTestTreshold < o.alphaTestTreshold - 0.0001f ) + return false; + } + for( int i = 0; i < MAX_TEXTURES; i++ ) if( texEnvMode[ i ] != o.texEnvMode[ i ] ) return false; @@ -63,6 +77,9 @@ namespace NL3D void setShaderType( uint32 type ){ shaderType = type; } void setProgram( IProgramObject *p ){ program = p; } void setNLightMaps( uint32 n ){ nlightmaps = n; } + void setAlphaTest( bool b ){ alphaTest = b; } + void setAlphaTestThreshold( float t ){ alphaTestTreshold = t; } + IProgramObject* getProgram() const{ return program; } private: @@ -70,6 +87,9 @@ namespace NL3D uint32 vbFlags; uint32 shaderType; uint32 nlightmaps; + bool alphaTest; + float alphaTestTreshold; + IProgramObject *program; }; }