Added specular term to the directional lights.

--HG--
branch : gsoc2013-dfighter
hg/feature/gsoc2013-dfighter
dfighter1985 11 years ago
parent 354d2b8c65
commit 28c4b5b157

@ -31,6 +31,7 @@ namespace NL3D
{ {
MVPMatrix, MVPMatrix,
MVMatrix, MVMatrix,
NormalMatrix,
TexMatrix0, TexMatrix0,
TexMatrix1, TexMatrix1,
TexMatrix2, TexMatrix2,
@ -74,6 +75,14 @@ namespace NL3D
Light5ColAmb, Light5ColAmb,
Light6ColAmb, Light6ColAmb,
Light7ColAmb, Light7ColAmb,
Light0ColSpec,
Light1ColSpec,
Light2ColSpec,
Light3ColSpec,
Light4ColSpec,
Light5ColSpec,
Light6ColSpec,
Light7ColSpec,
NUM_UNIFORMS NUM_UNIFORMS
}; };

@ -177,6 +177,7 @@ namespace NL3D
{ {
"mvpMatrix", "mvpMatrix",
"mvMatrix", "mvMatrix",
"normalMatrix",
"texMatrix0", "texMatrix0",
"texMatrix1", "texMatrix1",
"texMatrix2", "texMatrix2",
@ -219,7 +220,15 @@ namespace NL3D
"light4ColAmb", "light4ColAmb",
"light5ColAmb", "light5ColAmb",
"light6ColAmb", "light6ColAmb",
"light7ColAmb" "light7ColAmb",
"light0ColSpec",
"light1ColSpec",
"light2ColSpec",
"light3ColSpec",
"light4ColSpec",
"light5ColSpec",
"light6ColSpec",
"light7ColSpec"
}; };
void CGLSLProgram::cacheUniformIndices() void CGLSLProgram::cacheUniformIndices()

@ -275,6 +275,25 @@ namespace NL3D
ss << "uniform vec4 constant3;" << std::endl; 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() void CGLSLShaderGenerator::addAlphaTreshold()
{ {
ss << "uniform float alphaTreshold;" << std::endl; ss << "uniform float alphaTreshold;" << std::endl;
@ -343,6 +362,7 @@ namespace NL3D
case CShaderDesc::Directional: case CShaderDesc::Directional:
ss << "uniform vec3 light" << i << "Dir;" << std::endl; ss << "uniform vec3 light" << i << "Dir;" << std::endl;
ss << "" << std::endl;
break; break;
} }
} }
@ -361,6 +381,7 @@ namespace NL3D
case CShaderDesc::Directional: case CShaderDesc::Directional:
ss << "uniform vec4 light" << i << "ColDiff;" << std::endl; ss << "uniform vec4 light" << i << "ColDiff;" << std::endl;
ss << "uniform vec4 light" << i << "ColAmb;" << std::endl; ss << "uniform vec4 light" << i << "ColAmb;" << std::endl;
ss << "uniform vec4 light" << i << "ColSpec;" << std::endl;
break; break;
} }
} }
@ -379,6 +400,7 @@ namespace NL3D
case CShaderDesc::Directional: case CShaderDesc::Directional:
ss << "smooth out float intensity" << i << ";" << std::endl; ss << "smooth out float intensity" << i << ";" << std::endl;
ss << "smooth out float specIntensity" << i << ";" << std::endl;
break; break;
} }
} }
@ -396,6 +418,7 @@ namespace NL3D
case CShaderDesc::Directional: case CShaderDesc::Directional:
ss << "smooth in float intensity" << i << ";" << std::endl; ss << "smooth in float intensity" << i << ";" << std::endl;
ss << "smooth in float specIntensity" << i << ";" << std::endl;
break; break;
} }
} }
@ -405,19 +428,25 @@ namespace NL3D
{ {
ss << "float getIntensity" << num << "( void )" << std::endl; ss << "float getIntensity" << num << "( void )" << std::endl;
ss << "{" << 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 << "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 << "float angle = dot( normalize( light" << num << "Dir ), normal3 );" << std::endl;
ss << "angle = max( 0.0, angle );" << std::endl; ss << "angle = max( 0.0, angle );" << std::endl;
ss << "return angle;" << std::endl; ss << "return angle;" << std::endl;
ss << "}" << std::endl; ss << "}" << 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() void CGLSLShaderGenerator::addLightsFunctionVS()
@ -445,7 +474,11 @@ namespace NL3D
for( int i = 0; i < SHADER_MAX_LIGHTS; i++ ) for( int i = 0; i < SHADER_MAX_LIGHTS; i++ )
{ {
if( desc->getLight( i ) != CShaderDesc::Nolight ) 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; ss << "return col;" << std::endl;
@ -464,6 +497,7 @@ namespace NL3D
case CShaderDesc::Directional: case CShaderDesc::Directional:
ss << "intensity" << i << " = getIntensity" << i << "();" << std::endl; ss << "intensity" << i << " = getIntensity" << i << "();" << std::endl;
ss << "specIntensity" << i << " = getSpecIntensity" << i << "();" << std::endl;
break; break;
} }
} }
@ -488,10 +522,13 @@ namespace NL3D
if( desc->lightingEnabled() ) if( desc->lightingEnabled() )
{ {
addNormalMatrix();
addLightUniformsVS(); addLightUniformsVS();
addLightOutsVS(); addLightOutsVS();
ss << std::endl; ss << std::endl;
addNormalFromMVFunction();
addLightsFunctionVS(); addLightsFunctionVS();
ss << std::endl; ss << std::endl;
} }
@ -499,6 +536,9 @@ 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;
if( desc->lightingEnabled() ) if( desc->lightingEnabled() )
addLightsVS(); addLightsVS();

@ -44,6 +44,8 @@ namespace NL3D
void addDiffuse(); void addDiffuse();
void addColor(); void addColor();
void addConstants(); void addConstants();
void addNormalMatrix();
void addNormalFromMVFunction();
void addAlphaTreshold(); void addAlphaTreshold();
void addAlphaTest(); void addAlphaTest();

@ -318,6 +318,13 @@ namespace NL3D
setUniformMatrix4fv( mvIndex, 1, false, _ModelViewMatrix.get() ); setUniformMatrix4fv( mvIndex, 1, false, _ModelViewMatrix.get() );
} }
/*
int nmIdx = currentProgram->getUniformIndex( IProgramObject::NormalMatrix );
if( nmIdx != -1 )
{
}
*/
int fogStartIdx = currentProgram->getUniformIndex( IProgramObject::FogStart ); int fogStartIdx = currentProgram->getUniformIndex( IProgramObject::FogStart );
if( fogStartIdx != -1 ) if( fogStartIdx != -1 )
{ {
@ -394,13 +401,19 @@ namespace NL3D
int ldc = currentProgram->getUniformIndex( IProgramObject::EUniform( IProgramObject::Light0ColDiff + i ) ); int ldc = currentProgram->getUniformIndex( IProgramObject::EUniform( IProgramObject::Light0ColDiff + i ) );
if( ldc != -1 ) 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 ) ); int lac = currentProgram->getUniformIndex( IProgramObject::EUniform( IProgramObject::Light0ColAmb + i ) );
if( lac != -1 ) if( lac != -1 )
{ {
setUniform4f( lac, 0.1f, 0.1f, 0.1f, 1.0f ); setUniform4f( lac, 0.0f, 0.0f, 0.3f, 1.0f );
} }
} }

Loading…
Cancel
Save