Changed light model

Former-commit-id: bdf192bd83dbb5ae3ce8c46d6414e5b4fbc0047a
This commit is contained in:
Lynix 2013-08-28 02:52:09 +02:00
parent 83dc00dc60
commit f69d2f13e5
7 changed files with 198 additions and 207 deletions

View File

@ -163,17 +163,9 @@ int main()
// Nous choisissons une lumière directionnelle représentant la nébuleuse de notre skybox
NzLight nebulaLight(nzLightType_Directional);
// Il nous faut ensuite configurer la lumière, pour commencer, les couleurs.
// La couleur ambiante est celle qui sera appliquée à toutes les faces, éclairées ou non, dans le rayon de la lumière
// Comme nous avons une lumière infinie, ceci est la couleur appliquée de base à toutes les faces de la scène
nebulaLight.SetAmbientColor(NzColor(30, 30, 30));
// Ensuite vient la couleur diffuse, celle-ci étant la couleur appliquée lorsque la lumière éclaire une face
nebulaLight.SetDiffuseColor(NzColor(255, 182, 90));
// Ensuite, la lumière spéculaire, appliquée aux faces éclairées faisant face à la lumière
nebulaLight.SetSpecularColor(NzColor::Orange);
// Il nous faut ensuite configurer la lumière
// Pour commencer, sa couleur, la nébuleuse étant d'une couleur jaune, j'ai choisi ces valeurs
nebulaLight.SetColor(NzColor(255, 182, 90));
// Nous appliquons ensuite une rotation de sorte que la lumière dans la même direction que la nébuleuse
nebulaLight.SetRotation(NzEulerAnglesf(0.f, 102.f, 0.f));

View File

@ -25,26 +25,26 @@ class NAZARA_API NzLight : public NzSceneNode
void Enable(const NzShaderProgram* program, unsigned int lightUnit) const;
NzColor GetAmbientColor() const;
float GetAmbientFactor() const;
float GetAttenuation() const;
const NzBoundingVolumef& GetBoundingVolume() const;
NzColor GetDiffuseColor() const;
NzColor GetColor() const;
float GetDiffuseFactor() const;
float GetInnerAngle() const;
nzLightType GetLightType() const;
float GetOuterAngle() const;
float GetRadius() const;
nzSceneNodeType GetSceneNodeType() const;
NzColor GetSpecularColor() const;
bool IsDrawable() const;
void SetAmbientColor(const NzColor& ambient);
void SetAmbientFactor(float factor);
void SetAttenuation(float attenuation);
void SetDiffuseColor(const NzColor& diffuse);
void SetColor(const NzColor& color);
void SetDiffuseFactor(float factor);
void SetInnerAngle(float innerAngle);
void SetOuterAngle(float outerAngle);
void SetRadius(float radius);
void SetSpecularColor(const NzColor& specular);
NzLight& operator=(const NzLight& light);
@ -59,11 +59,11 @@ class NAZARA_API NzLight : public NzSceneNode
nzLightType m_type;
mutable NzBoundingVolumef m_boundingVolume;
NzColor m_ambientColor;
NzColor m_diffuseColor;
NzColor m_specularColor;
NzColor m_color;
mutable bool m_boundingVolumeUpdated;
float m_ambientFactor;
float m_attenuation;
float m_diffuseFactor;
float m_innerAngle;
float m_outerAngle;
float m_radius;

View File

@ -17,11 +17,11 @@
NzLight::NzLight(nzLightType type) :
m_type(type),
m_ambientColor((type == nzLightType_Directional) ? NzColor(50, 50, 50) : NzColor::Black),
m_diffuseColor(NzColor::White),
m_specularColor(NzColor::White),
m_color(NzColor::White),
m_boundingVolumeUpdated(false),
m_ambientFactor((type == nzLightType_Directional) ? 0.2f : 0.f),
m_attenuation(0.9f),
m_diffuseFactor(1.f),
m_innerAngle(15.f),
m_outerAngle(45.f),
m_radius(5.f)
@ -45,9 +45,8 @@ void NzLight::Enable(const NzShaderProgram* program, unsigned int lightUnit) con
struct Light
{
int type;
vec4 ambient;
vec4 diffuse;
vec4 specular;
vec4 color;
vec2 factors;
vec4 parameters1;
vec4 parameters2;
@ -69,9 +68,8 @@ void NzLight::Enable(const NzShaderProgram* program, unsigned int lightUnit) con
///TODO: Optimiser
int typeLocation = program->GetUniformLocation("Lights[0].type");
int ambientLocation = program->GetUniformLocation("Lights[0].ambient");
int diffuseLocation = program->GetUniformLocation("Lights[0].diffuse");
int specularLocation = program->GetUniformLocation("Lights[0].specular");
int colorLocation = program->GetUniformLocation("Lights[0].color");
int factorsLocation = program->GetUniformLocation("Lights[0].factors");
int parameters1Location = program->GetUniformLocation("Lights[0].parameters1");
int parameters2Location = program->GetUniformLocation("Lights[0].parameters2");
int parameters3Location = program->GetUniformLocation("Lights[0].parameters3");
@ -83,18 +81,16 @@ void NzLight::Enable(const NzShaderProgram* program, unsigned int lightUnit) con
// On applique cet offset
typeLocation += offset;
ambientLocation += offset;
diffuseLocation += offset;
specularLocation += offset;
colorLocation += offset;
factorsLocation += offset;
parameters1Location += offset;
parameters2Location += offset;
parameters3Location += offset;
}
program->SendInteger(typeLocation, m_type);
program->SendColor(ambientLocation, m_ambientColor);
program->SendColor(diffuseLocation, m_diffuseColor);
program->SendColor(specularLocation, m_specularColor);
program->SendColor(colorLocation, m_color);
program->SendVector(factorsLocation, NzVector2f(m_ambientFactor, m_diffuseFactor));
if (!m_derivedUpdated)
UpdateDerived();
@ -118,6 +114,16 @@ void NzLight::Enable(const NzShaderProgram* program, unsigned int lightUnit) con
}
}
float NzLight::GetAmbientFactor() const
{
return m_ambientFactor;
}
float NzLight::GetAttenuation() const
{
return m_attenuation;
}
const NzBoundingVolumef& NzLight::GetBoundingVolume() const
{
if (!m_boundingVolumeUpdated)
@ -126,19 +132,14 @@ const NzBoundingVolumef& NzLight::GetBoundingVolume() const
return m_boundingVolume;
}
NzColor NzLight::GetAmbientColor() const
NzColor NzLight::GetColor() const
{
return m_ambientColor;
return m_color;
}
float NzLight::GetAttenuation() const
float NzLight::GetDiffuseFactor() const
{
return m_attenuation;
}
NzColor NzLight::GetDiffuseColor() const
{
return m_diffuseColor;
return m_diffuseFactor;
}
float NzLight::GetInnerAngle() const
@ -166,19 +167,14 @@ nzSceneNodeType NzLight::GetSceneNodeType() const
return nzSceneNodeType_Light;
}
NzColor NzLight::GetSpecularColor() const
{
return m_specularColor;
}
bool NzLight::IsDrawable() const
{
return true;
}
void NzLight::SetAmbientColor(const NzColor& ambient)
void NzLight::SetAmbientFactor(float factor)
{
m_ambientColor = ambient;
m_ambientFactor = factor;
}
void NzLight::SetAttenuation(float attenuation)
@ -186,9 +182,14 @@ void NzLight::SetAttenuation(float attenuation)
m_attenuation = attenuation;
}
void NzLight::SetDiffuseColor(const NzColor& diffuse)
void NzLight::SetColor(const NzColor& color)
{
m_diffuseColor = diffuse;
m_color = color;
}
void NzLight::SetDiffuseFactor(float factor)
{
m_diffuseFactor = factor;
}
void NzLight::SetInnerAngle(float innerAngle)
@ -212,11 +213,6 @@ void NzLight::SetRadius(float radius)
m_boundingVolumeUpdated = false;
}
void NzLight::SetSpecularColor(const NzColor& specular)
{
m_specularColor = specular;
}
NzLight& NzLight::operator=(const NzLight& light)
{
std::memcpy(this, &light, sizeof(NzLight));

View File

@ -16,9 +16,8 @@ varying vec3 vWorldPos;
struct Light
{
int type;
vec4 ambient;
vec4 diffuse;
vec4 specular;
vec4 color;
vec2 factors;
vec4 parameters1;
vec4 parameters2;
@ -44,29 +43,27 @@ uniform vec4 SceneAmbient;
/********************Fonctions********************/
void main()
{
vec4 fragmentColor = MaterialDiffuse;
vec4 diffuseColor = MaterialDiffuse;
#if DIFFUSE_MAPPING
fragmentColor *= texture2D(MaterialDiffuseMap, vTexCoord);
diffuseColor *= texture(MaterialDiffuseMap, vTexCoord);
#endif
#if ALPHA_MAPPING
fragmentColor.a *= texture2D(MaterialAlphaMap, vTexCoord).r;
diffuseColor.a *= texture(MaterialAlphaMap, vTexCoord).r;
#endif
#if ALPHA_TEST
if (fragmentColor.a < MaterialAlphaThreshold)
if (diffuseColor.a < MaterialAlphaThreshold)
discard;
#endif
#if LIGHTING
vec3 lightColor = vec3(0.0);
#if SPECULAR_MAPPING
vec3 specularColor = vec3(0.0);
#endif
vec3 lightAmbient = vec3(0.0);
vec3 lightDiffuse = vec3(0.0);
vec3 lightSpecular = vec3(0.0);
#if NORMAL_MAPPING
vec3 normal = normalize(vLightToWorld * (2.0 * vec3(texture2D(MaterialNormalMap, vTexCoord)) - 1.0));
vec3 normal = normalize(vLightToWorld * (2.0 * vec3(texture(MaterialNormalMap, vTexCoord)) - 1.0));
#else
vec3 normal = normalize(vNormal);
#endif
@ -79,76 +76,74 @@ void main()
{
if (Lights[i].type == LIGHT_DIRECTIONAL)
{
vec3 lightDir = normalize(-Lights[i].parameters1.xyz);
vec3 lightDir = -Lights[i].parameters1.xyz;
// Ambient
lightColor += Lights[i].ambient.rgb * (MaterialAmbient.rgb + SceneAmbient.rgb);
lightAmbient += Lights[i].color.rgb * Lights[i].factors.x;
// Diffuse
float lambert = max(dot(normal, lightDir), 0.0);
lightColor += lambert * Lights[i].diffuse.rgb * MaterialDiffuse.rgb;
lightDiffuse += lambert * Lights[i].color.rgb * Lights[i].factors.y;
// Specular
vec3 reflection = reflect(-lightDir, normal);
float specular = pow(max(dot(reflection, eyeVec), 0.0), MaterialShininess);
float specularFactor = max(dot(reflection, eyeVec), 0.0);
specularFactor = pow(specularFactor, MaterialShininess);
#if SPECULAR_MAPPING
specularColor += specular * Lights[i].specular.rgb * MaterialSpecular.rgb;
#else
lightColor += specular * Lights[i].specular.rgb * MaterialSpecular.rgb;
#endif
lightSpecular += specularFactor * Lights[i].color.rgb;
}
else if (Lights[i].type == LIGHT_POINT)
{
vec3 lightDir = Lights[i].parameters1.xyz - vWorldPos;
float att = max(Lights[i].parameters1.w - Lights[i].parameters2.x*length(lightDir), 0.0);
lightDir = normalize(lightDir);
float lightDirLength = length(lightDir);
lightDir /= lightDirLength; // Normalisation
float att = max(Lights[i].parameters1.w - Lights[i].parameters2.x*lightDirLength, 0.0);
// Ambient
lightColor += att * Lights[i].ambient.rgb * (MaterialAmbient.rgb + SceneAmbient.rgb);
lightAmbient += att * Lights[i].color.rgb * Lights[i].factors.x;
// Diffuse
float lambert = max(dot(normal, lightDir), 0.0);
lightColor += att * lambert * Lights[i].diffuse.rgb * MaterialDiffuse.rgb;
lightDiffuse += att * lambert * Lights[i].color.rgb * Lights[i].factors.y;
// Specular
vec3 reflection = reflect(-lightDir, normal);
float specular = pow(max(dot(reflection, eyeVec), 0.0), MaterialShininess);
float specularFactor = max(dot(reflection, eyeVec), 0.0);
specularFactor = pow(specularFactor, MaterialShininess);
#if SPECULAR_MAPPING
specularColor += att * specular * Lights[i].specular.rgb * MaterialSpecular.rgb;
#else
lightColor += att * specular * Lights[i].specular.rgb * MaterialSpecular.rgb;
#endif
lightSpecular += att * specularFactor * Lights[i].color.rgb;
}
else if (Lights[i].type == LIGHT_SPOT)
{
vec3 lightDir = Lights[i].parameters1.xyz - vWorldPos;
float att = max(Lights[i].parameters1.w - Lights[i].parameters2.x*length(lightDir), 0.0);
lightDir = normalize(lightDir);
float lightDirLength = length(lightDir);
lightDir /= lightDirLength; // Normalisation
float att = max(Lights[i].parameters1.w - Lights[i].parameters2.x*lightDirLength, 0.0);
// Ambient
lightColor += att * Lights[i].ambient.rgb * (MaterialAmbient.rgb + SceneAmbient.rgb);
lightAmbient += att * Lights[i].color.rgb * Lights[i].factors.x;
// Modification de l'atténuation
// Diffuse
float lambert = max(dot(normal, lightDir), 0.0);
// Modification de l'atténuation pour gérer le spot
float curAngle = dot(Lights[i].parameters2.xyz, -lightDir);
float outerAngle = Lights[i].parameters3.y;
float innerMinusOuterAngle = Lights[i].parameters3.x - outerAngle;
float lambert = max(dot(normal, lightDir), 0.0);
att *= max((curAngle - outerAngle) / innerMinusOuterAngle, 0.0);
// Diffuse
lightColor += att * lambert * Lights[i].diffuse.rgb * MaterialDiffuse.rgb;
lightDiffuse += att * lambert * Lights[i].color.rgb * Lights[i].factors.y;
// Specular
vec3 reflection = reflect(-lightDir, normal);
float specular = pow(max(dot(reflection, eyeVec), 0.0), MaterialShininess);
float specularFactor = max(dot(reflection, eyeVec), 0.0);
specularFactor = pow(specularFactor, MaterialShininess);
#if SPECULAR_MAPPING
specularColor += att * specular * Lights[i].specular.rgb * MaterialSpecular.rgb;
#else
lightColor += att * specular * Lights[i].specular.rgb * MaterialSpecular.rgb;
#endif
lightSpecular += att * specularFactor * Lights[i].color.rgb;
}
}
}
@ -158,68 +153,75 @@ void main()
{
if (Lights[i].type == LIGHT_DIRECTIONAL)
{
vec3 lightDir = normalize(-Lights[i].parameters1.xyz);
vec3 lightDir = -Lights[i].parameters1.xyz;
// Ambient
lightColor += Lights[i].ambient.rgb * (MaterialAmbient.rgb + SceneAmbient.rgb);
lightAmbient += Lights[i].color.rgb * Lights[i].factors.x;
// Diffuse
float lambert = max(dot(normal, lightDir), 0.0);
lightColor += lambert * Lights[i].diffuse.rgb * MaterialDiffuse.rgb;
lightDiffuse += lambert * Lights[i].color.rgb * Lights[i].factors.y;
}
else if (Lights[i].type == LIGHT_POINT)
{
vec3 lightDir = Lights[i].parameters1.xyz - vWorldPos;
float att = max(Lights[i].parameters1.w - Lights[i].parameters2.x*length(lightDir), 0.0);
lightDir = normalize(lightDir);
float lightDirLength = length(lightDir);
lightDir /= lightDirLength; // Normalisation
float att = max(Lights[i].parameters1.w - Lights[i].parameters2.x*lightDirLength, 0.0);
// Ambient
lightColor += att * Lights[i].ambient.rgb * (MaterialAmbient.rgb + SceneAmbient.rgb);
lightAmbient += att * Lights[i].color.rgb * Lights[i].factors.x;
// Diffuse
float lambert = max(dot(normal, lightDir), 0.0);
lightColor += att * lambert * Lights[i].diffuse.rgb * MaterialDiffuse.rgb;
lightDiffuse += att * lambert * Lights[i].color.rgb * Lights[i].factors.y;
}
else if (Lights[i].type == LIGHT_SPOT)
{
vec3 lightDir = Lights[i].parameters1.xyz - vWorldPos;
float att = max(Lights[i].parameters1.w - Lights[i].parameters2.x*length(lightDir), 0.0);
lightDir = normalize(lightDir);
float lightDirLength = length(lightDir);
lightDir /= lightDirLength; // Normalisation
float att = max(Lights[i].parameters1.w - Lights[i].parameters2.x*lightDirLength, 0.0);
// Ambient
lightColor += att * Lights[i].ambient.rgb * (MaterialAmbient.rgb + SceneAmbient.rgb);
lightAmbient += att * Lights[i].color.rgb * Lights[i].factors.x;
// Modification de l'atténuation
// Diffuse
float lambert = max(dot(normal, lightDir), 0.0);
// Modification de l'atténuation pour gérer le spot
float curAngle = dot(Lights[i].parameters2.xyz, -lightDir);
float outerAngle = Lights[i].parameters3.y;
float innerMinusOuterAngle = Lights[i].parameters3.x - outerAngle;
float lambert = max(dot(normal, lightDir), 0.0);
att *= max((curAngle - outerAngle) / innerMinusOuterAngle, 0.0);
// Diffuse
lightColor += att * lambert * Lights[i].diffuse.rgb * MaterialDiffuse.rgb;
lightDiffuse += att * lambert * Lights[i].color.rgb * Lights[i].factors.y;
}
}
}
fragmentColor *= vec4(lightColor, 1.0);
lightAmbient = (lightAmbient + SceneAmbient.rgb)*MaterialAmbient.rgb;
lightSpecular *= MaterialSpecular.rgb;
#if SPECULAR_MAPPING
fragmentColor *= vec4(specularColor * texture2D(MaterialSpecularMap, vTexCoord).rgb, 1.0); // Utiliser l'alpha de MaterialSpecular n'aurait aucun sens
lightSpecular *= texture(MaterialSpecularMap, vTexCoord).rgb; // Utiliser l'alpha de MaterialSpecular n'aurait aucun sens
#endif
vec3 lightColor = (lightAmbient + lightDiffuse + lightSpecular);
vec4 fragmentColor = vec4(lightColor, 1.0) * diffuseColor;
#if EMISSIVE_MAPPING
#if SPECULAR_MAPPING
float lightIntensity = dot(lightColor + specularColor, vec3(0.3, 0.59, 0.11));
#else
float lightIntensity = dot(lightColor, vec3(0.3, 0.59, 0.11));
#endif // SPECULAR_MAPPING
vec3 emissionColor = MaterialDiffuse.rgb * texture2D(MaterialEmissiveMap, vTexCoord).rgb;
gl_FragColor = vec4(mix(fragmentColor.rgb, emissionColor, clamp(1.0 - 3.0*lightIntensity, 0.0, 1.0)), fragmentColor.a);
vec3 emissionColor = MaterialDiffuse.rgb * texture(MaterialEmissiveMap, vTexCoord).rgb;
RenderTarget0 = vec4(mix(fragmentColor.rgb, emissionColor, clamp(1.0 - 3.0*lightIntensity, 0.0, 1.0)), fragmentColor.a);
#else
gl_FragColor = fragmentColor;
RenderTarget0 = fragmentColor;
#endif // EMISSIVE_MAPPING
#else
gl_FragColor = fragmentColor;
RenderTarget0 = diffuseColor;
#endif // LIGHTING
}

File diff suppressed because one or more lines are too long

View File

@ -17,9 +17,8 @@ out vec4 RenderTarget2;
struct Light
{
int type;
vec4 ambient;
vec4 diffuse;
vec4 specular;
vec4 color;
vec2 factors;
vec4 parameters1;
vec4 parameters2;
@ -45,18 +44,18 @@ uniform vec4 SceneAmbient;
/********************Fonctions********************/
void main()
{
vec4 fragmentColor = MaterialDiffuse;
vec4 diffuseColor = MaterialDiffuse;
#if DIFFUSE_MAPPING
fragmentColor *= texture(MaterialDiffuseMap, vTexCoord);
diffuseColor *= texture(MaterialDiffuseMap, vTexCoord);
#endif
#if FLAG_DEFERRED
#if ALPHA_TEST
#if ALPHA_MAPPING // Inutile de faire de l'alpha-mapping sans alpha-test en Deferred (l'alpha n'est pas sauvegardé)
fragmentColor.a *= texture(MaterialAlphaMap, vTexCoord).r;
diffuseColor.a *= texture(MaterialAlphaMap, vTexCoord).r;
#endif
if (fragmentColor.a < MaterialAlphaThreshold)
if (diffuseColor.a < MaterialAlphaThreshold)
discard;
#endif // ALPHA_TEST
@ -67,39 +66,37 @@ void main()
vec3 normal = normalize(vNormal);
#endif // NORMAL_MAPPING
vec3 specular = MaterialSpecular.rgb;
vec3 specularColor = MaterialSpecular.rgb;
#if SPECULAR_MAPPING
specular *= texture(MaterialSpecularMap, vTexCoord).rgb;
specularColor *= texture(MaterialSpecularMap, vTexCoord).rgb;
#endif
/*
Texture0: Diffuse Color + Flags
Texture1: Normal map + Empty
Texture2: Specular value + Shininess
Texture2: Specular color + Shininess
Texture3: Depth texture
*/
RenderTarget0 = vec4(fragmentColor.rgb, 1.0);
RenderTarget1 = vec4(normal, 0.0);
RenderTarget2 = vec4(specular, MaterialShininess);
RenderTarget0 = vec4(diffuseColor.rgb, 1.0);
RenderTarget1 = vec4(normal*0.5 + 0.5, 0.0);
RenderTarget2 = vec4(specularColor, MaterialShininess);
#else // LIGHTING
RenderTarget0 = vec4(fragmentColor.rgb, 0.0);
RenderTarget0 = vec4(diffuseColor.rgb, 0.0);
#endif
#else // FLAG_DEFERRED
#if ALPHA_MAPPING
fragmentColor.a *= texture(MaterialAlphaMap, vTexCoord).r;
diffuseColor.a *= texture(MaterialAlphaMap, vTexCoord).r;
#endif
#if ALPHA_TEST
if (fragmentColor.a < MaterialAlphaThreshold)
if (diffuseColor.a < MaterialAlphaThreshold)
discard;
#endif
#if LIGHTING
vec3 lightColor = vec3(0.0);
#if SPECULAR_MAPPING
vec3 specularColor = vec3(0.0);
#endif
vec3 lightAmbient = vec3(0.0);
vec3 lightDiffuse = vec3(0.0);
vec3 lightSpecular = vec3(0.0);
#if NORMAL_MAPPING
vec3 normal = normalize(vLightToWorld * (2.0 * vec3(texture(MaterialNormalMap, vTexCoord)) - 1.0));
@ -117,80 +114,78 @@ void main()
{
case LIGHT_DIRECTIONAL:
{
vec3 lightDir = normalize(-Lights[i].parameters1.xyz);
vec3 lightDir = -Lights[i].parameters1.xyz;
// Ambient
lightColor += Lights[i].ambient.rgb * (MaterialAmbient.rgb + SceneAmbient.rgb);
lightAmbient += Lights[i].color.rgb * Lights[i].factors.x;
// Diffuse
float lambert = max(dot(normal, lightDir), 0.0);
lightColor += lambert * Lights[i].diffuse.rgb * MaterialDiffuse.rgb;
lightDiffuse += lambert * Lights[i].color.rgb * Lights[i].factors.y;
// Specular
vec3 reflection = reflect(-lightDir, normal);
float specular = pow(max(dot(reflection, eyeVec), 0.0), MaterialShininess);
float specularFactor = max(dot(reflection, eyeVec), 0.0);
specularFactor = pow(specularFactor, MaterialShininess);
#if SPECULAR_MAPPING
specularColor += specular * Lights[i].specular.rgb * MaterialSpecular.rgb;
#else
lightColor += specular * Lights[i].specular.rgb * MaterialSpecular.rgb;
#endif
lightSpecular += specularFactor * Lights[i].color.rgb;
break;
}
case LIGHT_POINT:
{
vec3 lightDir = Lights[i].parameters1.xyz - vWorldPos;
float att = max(Lights[i].parameters1.w - Lights[i].parameters2.x*length(lightDir), 0.0);
lightDir = normalize(lightDir);
float lightDirLength = length(lightDir);
lightDir /= lightDirLength; // Normalisation
float att = max(Lights[i].parameters1.w - Lights[i].parameters2.x*lightDirLength, 0.0);
// Ambient
lightColor += att * Lights[i].ambient.rgb * (MaterialAmbient.rgb + SceneAmbient.rgb);
lightAmbient += att * Lights[i].color.rgb * Lights[i].factors.x;
// Diffuse
float lambert = max(dot(normal, lightDir), 0.0);
lightColor += att * lambert * Lights[i].diffuse.rgb * MaterialDiffuse.rgb;
lightDiffuse += att * lambert * Lights[i].color.rgb * Lights[i].factors.y;
// Specular
vec3 reflection = reflect(-lightDir, normal);
float specular = pow(max(dot(reflection, eyeVec), 0.0), MaterialShininess);
float specularFactor = max(dot(reflection, eyeVec), 0.0);
specularFactor = pow(specularFactor, MaterialShininess);
#if SPECULAR_MAPPING
specularColor += att * specular * Lights[i].specular.rgb * MaterialSpecular.rgb;
#else
lightColor += att * specular * Lights[i].specular.rgb * MaterialSpecular.rgb;
#endif
lightSpecular += att * specularFactor * Lights[i].color.rgb;
break;
}
case LIGHT_SPOT:
{
vec3 lightDir = Lights[i].parameters1.xyz - vWorldPos;
float att = max(Lights[i].parameters1.w - Lights[i].parameters2.x*length(lightDir), 0.0);
lightDir = normalize(lightDir);
float lightDirLength = length(lightDir);
lightDir /= lightDirLength; // Normalisation
float att = max(Lights[i].parameters1.w - Lights[i].parameters2.x*lightDirLength, 0.0);
// Ambient
lightColor += att * Lights[i].ambient.rgb * (MaterialAmbient.rgb + SceneAmbient.rgb);
lightAmbient += att * Lights[i].color.rgb * Lights[i].factors.x;
// Modification de l'atténuation
// Diffuse
float lambert = max(dot(normal, lightDir), 0.0);
// Modification de l'atténuation pour gérer le spot
float curAngle = dot(Lights[i].parameters2.xyz, -lightDir);
float outerAngle = Lights[i].parameters3.y;
float innerMinusOuterAngle = Lights[i].parameters3.x - outerAngle;
float lambert = max(dot(normal, lightDir), 0.0);
att *= max((curAngle - outerAngle) / innerMinusOuterAngle, 0.0);
// Diffuse
lightColor += att * lambert * Lights[i].diffuse.rgb * MaterialDiffuse.rgb;
lightDiffuse += att * lambert * Lights[i].color.rgb * Lights[i].factors.y;
// Specular
vec3 reflection = reflect(-lightDir, normal);
float specular = pow(max(dot(reflection, eyeVec), 0.0), MaterialShininess);
float specularFactor = max(dot(reflection, eyeVec), 0.0);
specularFactor = pow(specularFactor, MaterialShininess);
#if SPECULAR_MAPPING
specularColor += att * specular * Lights[i].specular.rgb * MaterialSpecular.rgb;
#else
lightColor += att * specular * Lights[i].specular.rgb * MaterialSpecular.rgb;
#endif
lightSpecular += att * specularFactor * Lights[i].color.rgb;
break;
}
@ -208,50 +203,56 @@ void main()
case LIGHT_DIRECTIONAL:
{
vec3 lightDir = normalize(-Lights[i].parameters1.xyz);
// Ambient
lightColor += Lights[i].ambient.rgb * (MaterialAmbient.rgb + SceneAmbient.rgb);
lightAmbient += Lights[i].color.rgb * Lights[i].factors.x;
// Diffuse
float lambert = max(dot(normal, lightDir), 0.0);
lightColor += lambert * Lights[i].diffuse.rgb * MaterialDiffuse.rgb;
lightDiffuse += lambert * Lights[i].color.rgb * Lights[i].factors.y;
break;
}
case LIGHT_POINT:
{
vec3 lightDir = Lights[i].parameters1.xyz - vWorldPos;
float att = max(Lights[i].parameters1.w - Lights[i].parameters2.x*length(lightDir), 0.0);
lightDir = normalize(lightDir);
float lightDirLength = length(lightDir);
lightDir /= lightDirLength; // Normalisation
float att = max(Lights[i].parameters1.w - Lights[i].parameters2.x*lightDirLength, 0.0);
// Ambient
lightColor += att * Lights[i].ambient.rgb * (MaterialAmbient.rgb + SceneAmbient.rgb);
lightAmbient += att * Lights[i].color.rgb * Lights[i].factors.x;
// Diffuse
float lambert = max(dot(normal, lightDir), 0.0);
lightColor += att * lambert * Lights[i].diffuse.rgb * MaterialDiffuse.rgb;
lightDiffuse += att * lambert * Lights[i].color.rgb * Lights[i].factors.y;
break;
}
case LIGHT_SPOT:
{
vec3 lightDir = Lights[i].parameters1.xyz - vWorldPos;
float att = max(Lights[i].parameters1.w - Lights[i].parameters2.x*length(lightDir), 0.0);
lightDir = normalize(lightDir);
float lightDirLength = length(lightDir);
lightDir /= lightDirLength; // Normalisation
float att = max(Lights[i].parameters1.w - Lights[i].parameters2.x*lightDirLength, 0.0);
// Ambient
lightColor += att * Lights[i].ambient.rgb * (MaterialAmbient.rgb + SceneAmbient.rgb);
lightAmbient += att * Lights[i].color.rgb * Lights[i].factors.x;
// Modification de l'atténuation
// Diffuse
float lambert = max(dot(normal, lightDir), 0.0);
// Modification de l'atténuation pour gérer le spot
float curAngle = dot(Lights[i].parameters2.xyz, -lightDir);
float outerAngle = Lights[i].parameters3.y;
float innerMinusOuterAngle = Lights[i].parameters3.x - outerAngle;
float lambert = max(dot(normal, lightDir), 0.0);
att *= max((curAngle - outerAngle) / innerMinusOuterAngle, 0.0);
// Diffuse
lightColor += att * lambert * Lights[i].diffuse.rgb * MaterialDiffuse.rgb;
break;
lightDiffuse += att * lambert * Lights[i].color.rgb * Lights[i].factors.y;
}
default:
@ -260,17 +261,17 @@ void main()
}
}
fragmentColor *= vec4(lightColor, 1.0);
lightAmbient = (lightAmbient + SceneAmbient.rgb)*MaterialAmbient.rgb;
lightSpecular *= MaterialSpecular.rgb;
#if SPECULAR_MAPPING
fragmentColor *= vec4(specularColor * texture(MaterialSpecularMap, vTexCoord).rgb, 1.0); // Utiliser l'alpha de MaterialSpecular n'aurait aucun sens
lightSpecular *= texture(MaterialSpecularMap, vTexCoord).rgb; // Utiliser l'alpha de MaterialSpecular n'aurait aucun sens
#endif
vec3 lightColor = (lightAmbient + lightDiffuse + lightSpecular);
vec4 fragmentColor = vec4(lightColor, 1.0) * diffuseColor;
#if EMISSIVE_MAPPING
#if SPECULAR_MAPPING
float lightIntensity = dot(lightColor + specularColor, vec3(0.3, 0.59, 0.11));
#else
float lightIntensity = dot(lightColor, vec3(0.3, 0.59, 0.11));
#endif // SPECULAR_MAPPING
vec3 emissionColor = MaterialDiffuse.rgb * texture(MaterialEmissiveMap, vTexCoord).rgb;
RenderTarget0 = vec4(mix(fragmentColor.rgb, emissionColor, clamp(1.0 - 3.0*lightIntensity, 0.0, 1.0)), fragmentColor.a);
@ -278,7 +279,7 @@ void main()
RenderTarget0 = fragmentColor;
#endif // EMISSIVE_MAPPING
#else
RenderTarget0 = fragmentColor;
RenderTarget0 = diffuseColor;
#endif // LIGHTING
#endif // FLAG_DEFERRED
}

File diff suppressed because one or more lines are too long