diff --git a/code/nel/include/nel/3d/i_program_object.h b/code/nel/include/nel/3d/i_program_object.h index caf610865..5646ef21f 100644 --- a/code/nel/include/nel/3d/i_program_object.h +++ b/code/nel/include/nel/3d/i_program_object.h @@ -31,6 +31,7 @@ namespace NL3D { MVPMatrix, MVMatrix, + NormalMatrix, TexMatrix0, TexMatrix1, TexMatrix2, @@ -74,6 +75,14 @@ namespace NL3D Light5ColAmb, Light6ColAmb, Light7ColAmb, + Light0ColSpec, + Light1ColSpec, + Light2ColSpec, + Light3ColSpec, + Light4ColSpec, + Light5ColSpec, + Light6ColSpec, + Light7ColSpec, 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 e78ec9bac..2d577855d 100644 --- a/code/nel/src/3d/driver/OpenGL3/driver_glsl_program.cpp +++ b/code/nel/src/3d/driver/OpenGL3/driver_glsl_program.cpp @@ -177,6 +177,7 @@ namespace NL3D { "mvpMatrix", "mvMatrix", + "normalMatrix", "texMatrix0", "texMatrix1", "texMatrix2", @@ -219,7 +220,15 @@ namespace NL3D "light4ColAmb", "light5ColAmb", "light6ColAmb", - "light7ColAmb" + "light7ColAmb", + "light0ColSpec", + "light1ColSpec", + "light2ColSpec", + "light3ColSpec", + "light4ColSpec", + "light5ColSpec", + "light6ColSpec", + "light7ColSpec" }; 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 89ec89c58..213ec3250 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 @@ -275,6 +275,25 @@ namespace NL3D ss << "uniform vec4 constant3;" << std::endl; } + void CGLSLShaderGenerator::addNormalMatrix() + { + ss << "mat3 normalMatrix;" << std::endl; + } + + void CGLSLShaderGenerator::addNormalFromMVFunction() + { + ss << "// Calculates the normal matrix from the modelview matrix" << std::endl; + ss << "void calcNMFromMV()" << std::endl; + ss << "{" << std::endl; + ss << "normalMatrix[ 0 ] = mvMatrix[ 0 ].xyz;" << std::endl; + ss << "normalMatrix[ 1 ] = mvMatrix[ 1 ].xyz;" << std::endl; + ss << "normalMatrix[ 2 ] = mvMatrix[ 2 ].xyz;" << std::endl; + ss << "normalMatrix = inverse( normalMatrix );" << std::endl; + ss << "normalMatrix = transpose( normalMatrix );" << std::endl; + ss << "}" << std::endl; + ss << std::endl; + } + void CGLSLShaderGenerator::addAlphaTreshold() { ss << "uniform float alphaTreshold;" << std::endl; @@ -343,6 +362,7 @@ namespace NL3D case CShaderDesc::Directional: ss << "uniform vec3 light" << i << "Dir;" << std::endl; + ss << "" << std::endl; break; } } @@ -361,6 +381,7 @@ namespace NL3D case CShaderDesc::Directional: ss << "uniform vec4 light" << i << "ColDiff;" << std::endl; ss << "uniform vec4 light" << i << "ColAmb;" << std::endl; + ss << "uniform vec4 light" << i << "ColSpec;" << std::endl; break; } } @@ -379,6 +400,7 @@ namespace NL3D case CShaderDesc::Directional: ss << "smooth out float intensity" << i << ";" << std::endl; + ss << "smooth out float specIntensity" << i << ";" << std::endl; break; } } @@ -396,6 +418,7 @@ namespace NL3D case CShaderDesc::Directional: ss << "smooth in float intensity" << i << ";" << std::endl; + ss << "smooth in float specIntensity" << i << ";" << std::endl; break; } } @@ -405,19 +428,25 @@ namespace NL3D { ss << "float getIntensity" << num << "( void )" << std::endl; ss << "{" << std::endl; - ss << "mat3 nmat;" << std::endl; - ss << "nmat[ 0 ] = mvMatrix[ 0 ].xyz;" << std::endl; - ss << "nmat[ 1 ] = mvMatrix[ 1 ].xyz;" << std::endl; - ss << "nmat[ 2 ] = mvMatrix[ 2 ].xyz;" << std::endl; - ss << "nmat = inverse( nmat );" << std::endl; - ss << "nmat = transpose( nmat );" << std::endl; ss << "vec3 normal3 = vnormal.xyz / vnormal.w;" << std::endl; - ss << "normal3 = nmat * normal3;" << std::endl; + ss << "normal3 = normalMatrix * normal3;" << std::endl; ss << "float angle = dot( normalize( light" << num << "Dir ), normal3 );" << std::endl; ss << "angle = max( 0.0, angle );" << std::endl; ss << "return angle;" << std::endl; ss << "}" << std::endl; ss << std::endl; + + ss << "float getSpecIntensity" << num << "( void )" << std::endl; + ss << "{" << std::endl; + ss << "vec3 normal3 = vnormal.xyz / vnormal.w;" << std::endl; + ss << "normal3 = normalMatrix * normal3;" << std::endl; + ss << "vec3 reflection = reflect( normalize( -light" << num << "Dir ), normal3 );" << std::endl; + ss << "float angle = dot( normal3, reflection );" << std::endl; + ss << "angle = max( 0.0, angle );" << std::endl; + ss << "float si = pow( angle, 128.0 );" << std::endl; + ss << "return si;" << std::endl; + ss << "}" << std::endl; + ss << std::endl; } void CGLSLShaderGenerator::addLightsFunctionVS() @@ -445,7 +474,11 @@ namespace NL3D for( int i = 0; i < SHADER_MAX_LIGHTS; i++ ) { if( desc->getLight( i ) != CShaderDesc::Nolight ) - ss << "col = col * ( intensity" << i << " * light" << i << "ColDiff + light" << i << "ColAmb );" << std::endl; + { + ss << "col = col * ( intensity" << i << " * light" << i << "ColDiff"; + ss << " + specIntensity" << i << "* light" << i << "ColSpec"; + ss << " + light" << i << "ColAmb );" << std::endl; + } } ss << "return col;" << std::endl; @@ -464,6 +497,7 @@ namespace NL3D case CShaderDesc::Directional: ss << "intensity" << i << " = getIntensity" << i << "();" << std::endl; + ss << "specIntensity" << i << " = getSpecIntensity" << i << "();" << std::endl; break; } } @@ -488,16 +522,22 @@ namespace NL3D if( desc->lightingEnabled() ) { + addNormalMatrix(); addLightUniformsVS(); addLightOutsVS(); ss << std::endl; + addNormalFromMVFunction(); + addLightsFunctionVS(); ss << std::endl; } ss << "void main( void )" << std::endl; ss << "{" << std::endl; + + if( desc->lightingEnabled() ) + ss << "calcNMFromMV();" << std::endl; if( desc->lightingEnabled() ) addLightsVS(); 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 60aaba81e..11683a4e6 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 @@ -44,6 +44,8 @@ namespace NL3D void addDiffuse(); void addColor(); void addConstants(); + void addNormalMatrix(); + void addNormalFromMVFunction(); void addAlphaTreshold(); void addAlphaTest(); 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 f8574b17b..a57cac824 100644 --- a/code/nel/src/3d/driver/OpenGL3/driver_opengl_program.cpp +++ b/code/nel/src/3d/driver/OpenGL3/driver_opengl_program.cpp @@ -318,6 +318,13 @@ namespace NL3D setUniformMatrix4fv( mvIndex, 1, false, _ModelViewMatrix.get() ); } + /* + int nmIdx = currentProgram->getUniformIndex( IProgramObject::NormalMatrix ); + if( nmIdx != -1 ) + { + } + */ + int fogStartIdx = currentProgram->getUniformIndex( IProgramObject::FogStart ); if( fogStartIdx != -1 ) { @@ -394,13 +401,19 @@ namespace NL3D int ldc = currentProgram->getUniformIndex( IProgramObject::EUniform( IProgramObject::Light0ColDiff + i ) ); if( ldc != -1 ) { - setUniform4f( ldc, 1.0f, 1.0f, 1.0f, 1.0f ); + setUniform4f( ldc, 1.0f, 0.0f, 0.0f, 1.0f ); + } + + int lsc = currentProgram->getUniformIndex( IProgramObject::EUniform( IProgramObject::Light0ColSpec + i ) ); + if( lsc != -1 ) + { + setUniform4f( lsc, 0.0f, 1.0f, 0.0f, 1.0f ); } int lac = currentProgram->getUniformIndex( IProgramObject::EUniform( IProgramObject::Light0ColAmb + i ) ); if( lac != -1 ) { - setUniform4f( lac, 0.1f, 0.1f, 0.1f, 1.0f ); + setUniform4f( lac, 0.0f, 0.0f, 0.3f, 1.0f ); } }