From 8f2e75415c11345e0fbf522fba025c544e9c3227 Mon Sep 17 00:00:00 2001 From: dfighter1985 Date: Tue, 12 Nov 2013 23:56:40 +0100 Subject: [PATCH] Set up the normalmatrix in the driver and feed it to the shaders, instead of calculating it in the shaders. --HG-- branch : gsoc2013-dfighter --- code/nel/include/nel/3d/program.h | 2 ++ .../opengl3/driver_glsl_shader_generator.cpp | 26 +------------------ .../opengl3/driver_glsl_shader_generator.h | 3 --- .../nel/src/3d/driver/opengl3/driver_opengl.h | 1 + .../driver/opengl3/driver_opengl_program.cpp | 25 +++++++++++++++--- code/nel/src/3d/program.cpp | 4 ++- 6 files changed, 29 insertions(+), 32 deletions(-) diff --git a/code/nel/include/nel/3d/program.h b/code/nel/include/nel/3d/program.h index bbc4ab7db..c0e058a32 100644 --- a/code/nel/include/nel/3d/program.h +++ b/code/nel/include/nel/3d/program.h @@ -119,6 +119,8 @@ struct CProgramIndex ModelViewProjectionTranspose, ModelViewProjectionInverseTranspose, + NormalMatrix, + Fog, FogStart, 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 f1965d91c..d24f82245 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 @@ -284,21 +284,7 @@ namespace NL3D 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 ] = modelView[ 0 ].xyz;" << std::endl; - ss << "normalMatrix[ 1 ] = modelView[ 1 ].xyz;" << std::endl; - ss << "normalMatrix[ 2 ] = modelView[ 2 ].xyz;" << std::endl; - ss << "normalMatrix = inverse( normalMatrix );" << std::endl; - ss << "normalMatrix = transpose( normalMatrix );" << std::endl; - ss << "}" << std::endl; - ss << std::endl; + ss << "uniform mat3 normalMatrix;" << std::endl; } void CGLSLShaderGenerator::addAlphaTreshold() @@ -573,8 +559,6 @@ namespace NL3D addLightOutsVS(); ss << std::endl; - addNormalFromMVFunction(); - addLightsFunctionVS(); ss << std::endl; } @@ -582,9 +566,6 @@ namespace NL3D ss << "void main( void )" << std::endl; ss << "{" << std::endl; - if( desc->lightingEnabled() ) - ss << "calcNMFromMV();" << std::endl; - ss << "gl_Position = modelViewProjection * " << "v" << attribNames[ 0 ] << ";" << std::endl; if( desc->fogEnabled() || desc->hasPointLight() ) @@ -632,8 +613,6 @@ namespace NL3D addLightOutsVS(); ss << std::endl; - addNormalFromMVFunction(); - addLightsFunctionVS(); ss << std::endl; } @@ -649,9 +628,6 @@ namespace NL3D ss << "{" << std::endl; ss << "vec4 eyePosition = modelView * v" << attribNames[ 0 ] << ";" << std::endl; - if( desc->lightingEnabled() ) - ss << "calcNMFromMV();" << std::endl; - if( desc->hasPointLight() ) ss << "ecPos4 = eyePosition;" << 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 a187dd063..caab848d2 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 @@ -59,9 +59,6 @@ namespace NL3D /// Adds the normal matrix declaration to the program void addNormalMatrix(); - /// Adds the normal matrix calculating function to the program ( calculated from the inverse transpose of the upper left 3x3 part ) - void addNormalFromMVFunction(); - //////////////////////////// Alpha Threshold ////////////////// /// Adds the alpha threshold uniform to the program diff --git a/code/nel/src/3d/driver/opengl3/driver_opengl.h b/code/nel/src/3d/driver/opengl3/driver_opengl.h index a6b04274c..76bfb3abf 100644 --- a/code/nel/src/3d/driver/opengl3/driver_opengl.h +++ b/code/nel/src/3d/driver/opengl3/driver_opengl.h @@ -1287,6 +1287,7 @@ private: void setUniform3f(TProgram program, uint index, const NLMISC::CVector& v); void setUniform4f(TProgram program, uint index, const NLMISC::CVector& v, float f3); void setUniform4f(TProgram program, uint index, const NLMISC::CRGBAF& rgba); + void setUniform3x3f(TProgram program, uint index, const float *src ); void setUniform4x4f(TProgram program, uint index, const NLMISC::CMatrix& m); void setUniform4x4f(TProgram program, uint index, const float *src ); void setUniform4fv(TProgram program, uint index, size_t num, const float *src); 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 b4be7cd0d..b1972bc3e 100644 --- a/code/nel/src/3d/driver/opengl3/driver_opengl_program.cpp +++ b/code/nel/src/3d/driver/opengl3/driver_opengl_program.cpp @@ -402,6 +402,12 @@ namespace NL3D nglProgramUniform4f( id, index, rgba.R, rgba.G, rgba.B, rgba.A ); } + void CDriverGL3::setUniform3x3f( TProgram program, uint index, const float *src ) + { + uint32 id = getProgramId( program ); + nglProgramUniformMatrix3fv( id, index, 1, false, src ); + } + void CDriverGL3::setUniform4x4f( TProgram program, uint index, const CMatrix &m ) { uint32 id = getProgramId( program ); @@ -778,12 +784,25 @@ namespace NL3D setUniform4x4f( program, mvIndex, _ModelViewMatrix ); } - /* - int nmIdx = currentProgram->getUniformIndex( IProgram::NormalMatrix ); + int nmIdx = p->getUniformIndex( CProgramIndex::NormalMatrix ); if( nmIdx != -1 ) { + // normal matrix is the inverse-transpose of the rotation part of the modelview matrix + // Inverse-transpose of the rotation matrix, is itself + const float *mv = _ModelViewMatrix.get(); + float nm[ 3 * 3 ]; + nm[ 0 ] = mv[ 0 ]; + nm[ 1 ] = mv[ 1 ]; + nm[ 2 ] = mv[ 2 ]; + nm[ 3 ] = mv[ 4 ]; + nm[ 4 ] = mv[ 5 ]; + nm[ 5 ] = mv[ 6 ]; + nm[ 6 ] = mv[ 8 ]; + nm[ 7 ] = mv[ 9 ]; + nm[ 8 ] = mv[ 10 ]; + + setUniform3x3f( program, nmIdx, nm ); } - */ int fogStartIdx = p->getUniformIndex( CProgramIndex::FogStart ); if( fogStartIdx != -1 ) diff --git a/code/nel/src/3d/program.cpp b/code/nel/src/3d/program.cpp index f29e4ab5d..99fb762e2 100644 --- a/code/nel/src/3d/program.cpp +++ b/code/nel/src/3d/program.cpp @@ -87,7 +87,9 @@ const char *CProgramIndex::Names[NUM_UNIFORMS] = "modelViewProjection", "modelViewProjectionInverse", "modelViewProjectionTranspose", - "modelViewProjectionInverseTranspose", + "modelViewProjectionInverseTranspose", + + "normalMatrix", "fog",