Set up the normalmatrix in the driver and feed it to the shaders, instead of calculating it in the shaders.

--HG--
branch : gsoc2013-dfighter
hg/feature/gsoc2013-dfighter
dfighter1985 11 years ago
parent 9c0e1c2449
commit 8f2e75415c

@ -119,6 +119,8 @@ struct CProgramIndex
ModelViewProjectionTranspose, ModelViewProjectionTranspose,
ModelViewProjectionInverseTranspose, ModelViewProjectionInverseTranspose,
NormalMatrix,
Fog, Fog,
FogStart, FogStart,

@ -284,21 +284,7 @@ namespace NL3D
void CGLSLShaderGenerator::addNormalMatrix() void CGLSLShaderGenerator::addNormalMatrix()
{ {
ss << "mat3 normalMatrix;" << std::endl; ss << "uniform 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;
} }
void CGLSLShaderGenerator::addAlphaTreshold() void CGLSLShaderGenerator::addAlphaTreshold()
@ -573,8 +559,6 @@ namespace NL3D
addLightOutsVS(); addLightOutsVS();
ss << std::endl; ss << std::endl;
addNormalFromMVFunction();
addLightsFunctionVS(); addLightsFunctionVS();
ss << std::endl; ss << std::endl;
} }
@ -582,9 +566,6 @@ namespace NL3D
ss << "void main( void )" << std::endl; ss << "void main( void )" << std::endl;
ss << "{" << std::endl; ss << "{" << std::endl;
if( desc->lightingEnabled() )
ss << "calcNMFromMV();" << std::endl;
ss << "gl_Position = modelViewProjection * " << "v" << attribNames[ 0 ] << ";" << std::endl; ss << "gl_Position = modelViewProjection * " << "v" << attribNames[ 0 ] << ";" << std::endl;
if( desc->fogEnabled() || desc->hasPointLight() ) if( desc->fogEnabled() || desc->hasPointLight() )
@ -632,8 +613,6 @@ namespace NL3D
addLightOutsVS(); addLightOutsVS();
ss << std::endl; ss << std::endl;
addNormalFromMVFunction();
addLightsFunctionVS(); addLightsFunctionVS();
ss << std::endl; ss << std::endl;
} }
@ -649,9 +628,6 @@ namespace NL3D
ss << "{" << std::endl; ss << "{" << std::endl;
ss << "vec4 eyePosition = modelView * v" << attribNames[ 0 ] << ";" << std::endl; ss << "vec4 eyePosition = modelView * v" << attribNames[ 0 ] << ";" << std::endl;
if( desc->lightingEnabled() )
ss << "calcNMFromMV();" << std::endl;
if( desc->hasPointLight() ) if( desc->hasPointLight() )
ss << "ecPos4 = eyePosition;" << std::endl; ss << "ecPos4 = eyePosition;" << std::endl;

@ -59,9 +59,6 @@ namespace NL3D
/// Adds the normal matrix declaration to the program /// Adds the normal matrix declaration to the program
void addNormalMatrix(); 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 ////////////////// //////////////////////////// Alpha Threshold //////////////////
/// Adds the alpha threshold uniform to the program /// Adds the alpha threshold uniform to the program

@ -1287,6 +1287,7 @@ private:
void setUniform3f(TProgram program, uint index, const NLMISC::CVector& v); 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::CVector& v, float f3);
void setUniform4f(TProgram program, uint index, const NLMISC::CRGBAF& rgba); 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 NLMISC::CMatrix& m);
void setUniform4x4f(TProgram program, uint index, const float *src ); void setUniform4x4f(TProgram program, uint index, const float *src );
void setUniform4fv(TProgram program, uint index, size_t num, const float *src); void setUniform4fv(TProgram program, uint index, size_t num, const float *src);

@ -402,6 +402,12 @@ namespace NL3D
nglProgramUniform4f( id, index, rgba.R, rgba.G, rgba.B, rgba.A ); 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 ) void CDriverGL3::setUniform4x4f( TProgram program, uint index, const CMatrix &m )
{ {
uint32 id = getProgramId( program ); uint32 id = getProgramId( program );
@ -778,12 +784,25 @@ namespace NL3D
setUniform4x4f( program, mvIndex, _ModelViewMatrix ); setUniform4x4f( program, mvIndex, _ModelViewMatrix );
} }
/* int nmIdx = p->getUniformIndex( CProgramIndex::NormalMatrix );
int nmIdx = currentProgram->getUniformIndex( IProgram::NormalMatrix );
if( nmIdx != -1 ) 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 ); int fogStartIdx = p->getUniformIndex( CProgramIndex::FogStart );
if( fogStartIdx != -1 ) if( fogStartIdx != -1 )

@ -89,6 +89,8 @@ const char *CProgramIndex::Names[NUM_UNIFORMS] =
"modelViewProjectionTranspose", "modelViewProjectionTranspose",
"modelViewProjectionInverseTranspose", "modelViewProjectionInverseTranspose",
"normalMatrix",
"fog", "fog",
"fogStart", "fogStart",

Loading…
Cancel
Save