From 9c69e1bf84a673fe43b6a475bbb53cdc0d741d2b Mon Sep 17 00:00:00 2001 From: dfighter1985 Date: Sun, 8 Dec 2013 03:34:58 +0100 Subject: [PATCH] Material colors ( ambient, diffuse, specular ) should now be used when there's no texture available. --HG-- branch : gsoc2013-dfighter --- .../opengl3/driver_glsl_shader_generator.cpp | 49 ++++++++++++++++--- .../opengl3/driver_glsl_shader_generator.h | 6 +++ .../driver/opengl3/driver_opengl_program.cpp | 10 +++- .../opengl3/driver_opengl_shader_desc.h | 8 +++ 4 files changed, 66 insertions(+), 7 deletions(-) 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 f4e402688..f5efd73e0 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 @@ -183,6 +183,13 @@ namespace NL3D } ss << std::endl; + if( !desc->useTextures() ) + { + addAmbient(); + addDiffuse(); + addSpecular(); + } + switch( material->getShader() ) { case CMaterial::Normal: @@ -264,11 +271,21 @@ namespace NL3D ps.assign( ss.str() ); } + void CGLSLShaderGenerator::addAmbient() + { + ss << "uniform vec4 ambientColor;" << std::endl; + } + void CGLSLShaderGenerator::addDiffuse() { ss << "uniform vec4 diffuseColor;" << std::endl; } + void CGLSLShaderGenerator::addSpecular() + { + ss << "uniform vec4 specularColor;" << std::endl; + } + void CGLSLShaderGenerator::addColor() { ss << "uniform vec4 materialColor;" << std::endl; @@ -435,9 +452,20 @@ namespace NL3D ss << "vec3 normal3 = vnormal.xyz / vnormal.w;" << std::endl; ss << "normal3 = normalMatrix * normal3;" << std::endl; ss << "normal3 = normalize( normal3 );" << std::endl; - ss << "vec4 lc = getIntensity" << num << "( normal3, lightDir ) * light" << num << "ColDiff + "; - ss << "getSpecIntensity" << num << "( normal3, lightDir ) * light" << num << "ColSpec + "; - ss << "light" << num << "ColAmb;" << std::endl; + + if( desc->useTextures() || ( material->getShader() == CMaterial::LightMap ) ) + { + ss << "vec4 lc = getIntensity" << num << "( normal3, lightDir ) * light" << num << "ColDiff + "; + ss << "getSpecIntensity" << num << "( normal3, lightDir ) * light" << num << "ColSpec + "; + ss << "light" << num << "ColAmb;" << std::endl; + } + else + { + ss << "vec4 lc = getIntensity" << num << "( normal3, lightDir ) * light" << num << "ColDiff * diffuseColor + "; + ss << "getSpecIntensity" << num << "( normal3, lightDir ) * light" << num << "ColSpec * specularColor + "; + ss << "light" << num << "ColAmb * ambientColor;" << std::endl; + } + ss << "return lc;" << std::endl; ss << "}" << std::endl; ss << std::endl; @@ -480,9 +508,18 @@ namespace NL3D ss << "normal3 = normalMatrix * normal3;" << std::endl; ss << "normal3 = normalize( normal3 );" << std::endl; - ss << "vec4 lc = getIntensity" << num << "( normal3, lightDirection ) * light" << num << "ColDiff + "; - ss << "getSpecIntensity" << num << "( normal3, lightDirection ) * light" << num << "ColSpec + "; - ss << "light" << num << "ColAmb;" << std::endl; + if( desc->useTextures() || ( material->getShader() == CMaterial::LightMap ) ) + { + ss << "vec4 lc = getIntensity" << num << "( normal3, lightDirection ) * light" << num << "ColDiff + "; + ss << "getSpecIntensity" << num << "( normal3, lightDirection ) * light" << num << "ColSpec + "; + ss << "light" << num << "ColAmb;" << std::endl; + } + else + { + ss << "vec4 lc = getIntensity" << num << "( normal3, lightDirection ) * light" << num << "ColDiff * diffuseColor+ "; + ss << "getSpecIntensity" << num << "( normal3, lightDirection ) * light" << num << "ColSpec * specularColor + "; + ss << "light" << num << "ColAmb * ambientColor;" << std::endl; + } ss << "lc = lc / attenuation;" << std::endl; ss << "return lc;" << 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 5fc2f43a4..7aa824f4f 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 @@ -47,9 +47,15 @@ namespace NL3D void setShaderDesc( CShaderDesc *d ){ desc = d; } private: + /// Adds ambient color constant uniform declaration to the program + void addAmbient(); + /// Adds diffuse constant uniform declaration to the program void addDiffuse(); + /// Adds specular color constant uniform declaration to the program + void addSpecular(); + /// Adds Color constant uniform declaration to the program void addColor(); 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 e0570b735..976eaf204 100644 --- a/code/nel/src/3d/driver/opengl3/driver_opengl_program.cpp +++ b/code/nel/src/3d/driver/opengl3/driver_opengl_program.cpp @@ -494,6 +494,8 @@ namespace NL3D if( mat.getShader() == CMaterial::Normal ) { + bool useTextures = false; + int maxTextures = std::min( int( SHADER_MAX_TEXTURES ), int( IDRV_MAT_MAXTEXTURES ) ); for( int i = 0; i < maxTextures; i++ ) { @@ -505,10 +507,11 @@ namespace NL3D if( desc.hasVBFlags( vertexFlags[ TexCoord0 + i ] ) ) { desc.setUseTexStage( i, true ); + useTextures = true; } } - if( !desc.getUseTexStage( 1 ) ) + if( useTextures && !desc.getUseTexStage( 1 ) ) { for( int i = 1; i < maxTextures; i++ ) { @@ -519,6 +522,11 @@ namespace NL3D } } } + else + if( !useTextures ) + { + desc.setNoTextures( true ); + } } if( mat.getAlphaTest() ) 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 42735e240..a116776cf 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 @@ -95,6 +95,7 @@ namespace NL3D for( int i = 0; i < SHADER_MAX_TEXTURES; i++ ) useTextureStage[ i ] = false; useFirstTextureCoordSet = false; + noTextures = true; features = None; shaderType = Normal; @@ -110,6 +111,9 @@ namespace NL3D bool operator==( const CShaderDesc &o ) const { + if( noTextures != o.noTextures ) + return false; + if( shaderType != o.shaderType ) return false; @@ -169,6 +173,9 @@ namespace NL3D void setUseTexStage( uint8 stage, bool b ){ useTextureStage[ stage ] = b; } bool getUseTexStage( uint8 stage ) const{ return useTextureStage[ stage ]; } + void setNoTextures( bool b ){ noTextures = b; } + bool useTextures() const{ return !noTextures; } + void setVBFlags( uint32 flags ){ vbFlags = flags; } bool hasVBFlags( uint32 flags ) const{ if( ( vbFlags & flags ) != 0 ) @@ -254,6 +261,7 @@ namespace NL3D uint32 texEnvMode[ SHADER_MAX_TEXTURES ]; bool useTextureStage[ SHADER_MAX_TEXTURES ]; bool useFirstTextureCoordSet; + bool noTextures; uint32 vbFlags; uint32 shaderType; uint32 nlightmaps;