diff --git a/src/OpenGl/OpenGl_ShaderManager.cxx b/src/OpenGl/OpenGl_ShaderManager.cxx index c9b9aad8c6..173156f026 100644 --- a/src/OpenGl/OpenGl_ShaderManager.cxx +++ b/src/OpenGl/OpenGl_ShaderManager.cxx @@ -30,12 +30,18 @@ #include "../Shaders/Shaders_DirectionalLightShadow_glsl.pxx" #include "../Shaders/Shaders_PBRDistribution_glsl.pxx" +#include "../Shaders/Shaders_PBRDirectionalLight_glsl.pxx" #include "../Shaders/Shaders_PBRGeometry_glsl.pxx" #include "../Shaders/Shaders_PBRFresnel_glsl.pxx" #include "../Shaders/Shaders_PBRCookTorrance_glsl.pxx" #include "../Shaders/Shaders_PBRIllumination_glsl.pxx" +#include "../Shaders/Shaders_PBRPointLight_glsl.pxx" +#include "../Shaders/Shaders_PBRSpotLight_glsl.pxx" #include "../Shaders/Shaders_PBREnvBaking_fs.pxx" #include "../Shaders/Shaders_PBREnvBaking_vs.pxx" +#include "../Shaders/Shaders_PhongDirectionalLight_glsl.pxx" +#include "../Shaders/Shaders_PhongPointLight_glsl.pxx" +#include "../Shaders/Shaders_PhongSpotLight_glsl.pxx" #include "../Shaders/Shaders_PointLightAttenuation_glsl.pxx" #include "../Shaders/Shaders_TangentSpaceNormal_glsl.pxx" @@ -104,192 +110,7 @@ const char THE_FUNC_PBR_lightDef[] = EOL"vec3 Emission;" //!< Light intensity emitted by material EOL"float IOR;"; //!< Material's index of refraction -//! Function computes contribution of isotropic point light source -const char THE_FUNC_pointLight[] = - EOL"void pointLight (in int theId," - EOL" in vec3 theNormal," - EOL" in vec3 theView," - EOL" in vec3 thePoint," - EOL" in bool theIsFront)" - EOL"{" - EOL" vec3 aLight = vec3 (occWorldViewMatrix * vec4 (occLight_Position (theId), 1.0)) - thePoint;" - EOL - EOL" float aDist = length (aLight);" - EOL" float aRange = occLight_Range (theId);" - EOL" float anAtten = occPointLightAttenuation (aDist, aRange, occLight_LinearAttenuation (theId), occLight_ConstAttenuation (theId));" - EOL" if (anAtten <= 0.0) return;" - EOL" aLight /= aDist;" - EOL - EOL" vec3 aHalf = normalize (aLight + theView);" - EOL - EOL" vec3 aFaceSideNormal = theIsFront ? theNormal : -theNormal;" - EOL" float aNdotL = max (0.0, dot (aFaceSideNormal, aLight));" - EOL" float aNdotH = max (0.0, dot (aFaceSideNormal, aHalf ));" - EOL - EOL" float aSpecl = 0.0;" - EOL" if (aNdotL > 0.0)" - EOL" {" - EOL" aSpecl = pow (aNdotH, theIsFront ? occFrontMaterial_Shininess() : occBackMaterial_Shininess());" - EOL" }" - EOL - EOL" Diffuse += occLight_Diffuse (theId) * aNdotL * anAtten;" - EOL" Specular += occLight_Specular(theId) * aSpecl * anAtten;" - EOL"}"; - -//! Function computes contribution of isotropic point light source -const char THE_FUNC_PBR_pointLight[] = - EOL"void pointLight (in int theId," - EOL" in vec3 theNormal," - EOL" in vec3 theView," - EOL" in vec3 thePoint," - EOL" in bool theIsFront)" - EOL"{" - EOL" vec3 aLight = occLight_Position (theId) - thePoint;" - EOL - EOL" float aDist = length (aLight);" - EOL" float aRange = occLight_Range (theId);" - EOL" float anAtten = occPointLightAttenuation (aDist, aRange);" - EOL" if (anAtten <= 0.0) return;" - EOL" aLight /= aDist;" - EOL - EOL" theNormal = theIsFront ? theNormal : -theNormal;" - EOL" DirectLighting += occPBRIllumination (theView, aLight, theNormal," - EOL" BaseColor, Metallic, Roughness, IOR," - EOL" occLight_Specular (theId)," - EOL" occLight_Intensity(theId) * anAtten);" - EOL"}"; - -//! Function computes contribution of spotlight source -const char THE_FUNC_spotLight[] = - EOL"void spotLight (in int theId," - EOL" in vec3 theNormal," - EOL" in vec3 theView," - EOL" in vec3 thePoint," - EOL" in bool theIsFront)" - EOL"{" - EOL" vec3 aLight = vec3 (occWorldViewMatrix * vec4 (occLight_Position (theId), 1.0)) - thePoint;" - EOL - EOL" float aDist = length (aLight);" - EOL" float aRange = occLight_Range (theId);" - EOL" float anAtten = occPointLightAttenuation (aDist, aRange, occLight_LinearAttenuation (theId), occLight_ConstAttenuation (theId));" - EOL" if (anAtten <= 0.0) return;" - EOL" aLight /= aDist;" - EOL - EOL" vec3 aSpotDir = vec3 (occWorldViewMatrix * vec4 (occLight_SpotDirection (theId), 0.0));" - EOL" aSpotDir = normalize (aSpotDir);" - // light cone - EOL" float aCosA = dot (aSpotDir, -aLight);" - EOL" if (aCosA >= 1.0 || aCosA < cos (occLight_SpotCutOff (theId)))" - EOL" {" - EOL" return;" - EOL" }" - EOL - EOL" float anExponent = occLight_SpotExponent (theId);" - EOL" if (anExponent > 0.0)" - EOL" {" - EOL" anAtten *= pow (aCosA, anExponent * 128.0);" - EOL" }" - EOL - EOL" vec3 aHalf = normalize (aLight + theView);" - EOL - EOL" vec3 aFaceSideNormal = theIsFront ? theNormal : -theNormal;" - EOL" float aNdotL = max (0.0, dot (aFaceSideNormal, aLight));" - EOL" float aNdotH = max (0.0, dot (aFaceSideNormal, aHalf ));" - EOL - EOL" float aSpecl = 0.0;" - EOL" if (aNdotL > 0.0)" - EOL" {" - EOL" aSpecl = pow (aNdotH, theIsFront ? occFrontMaterial_Shininess() : occBackMaterial_Shininess());" - EOL" }" - EOL - EOL" Diffuse += occLight_Diffuse (theId) * aNdotL * anAtten;" - EOL" Specular += occLight_Specular(theId) * aSpecl * anAtten;" - EOL"}"; - -//! Function computes contribution of spotlight source - const char THE_FUNC_PBR_spotLight[] = - EOL"void spotLight (in int theId," - EOL" in vec3 theNormal," - EOL" in vec3 theView," - EOL" in vec3 thePoint," - EOL" in bool theIsFront)" - EOL"{" - EOL" vec3 aLight = occLight_Position (theId) - thePoint;" - EOL - EOL" float aDist = length (aLight);" - EOL" float aRange = occLight_Range (theId);" - EOL" float anAtten = occPointLightAttenuation (aDist, aRange);" - EOL" if (anAtten <= 0.0) return;" - EOL" aLight /= aDist;" - EOL - EOL" vec3 aSpotDir = occLight_SpotDirection (theId);" - // light cone - EOL" float aCosA = dot (aSpotDir, -aLight);" - EOL" float aRelativeAngle = 2.0 * acos(aCosA) / occLight_SpotCutOff(theId);" - EOL" if (aCosA >= 1.0 || aRelativeAngle > 1.0)" - EOL" {" - EOL" return;" - EOL" }" - EOL" float anExponent = occLight_SpotExponent (theId);" - EOL" if ((1.0 - aRelativeAngle) <= anExponent)" - EOL" {" - EOL" float anAngularAttenuationOffset = cos(0.5 * occLight_SpotCutOff(theId));" - EOL" float anAngularAttenuationScale = 1.0 / max(0.001, cos(0.5 * occLight_SpotCutOff(theId) * (1.0 - anExponent)) - anAngularAttenuationOffset);" - EOL" anAngularAttenuationOffset *= -anAngularAttenuationScale;" - EOL" float anAngularAttenuantion = clamp(aCosA * anAngularAttenuationScale + anAngularAttenuationOffset, 0.0, 1.0);" - EOL" anAtten *= anAngularAttenuantion * anAngularAttenuantion;" - EOL" }" - EOL" theNormal = theIsFront ? theNormal : -theNormal;" - EOL" DirectLighting += occPBRIllumination (theView, aLight, theNormal," - EOL" BaseColor, Metallic, Roughness, IOR," - EOL" occLight_Specular(theId)," - EOL" occLight_Intensity(theId) * anAtten);" - EOL"}"; - -//! Function computes contribution of directional light source -const char THE_FUNC_directionalLight[] = - EOL"void directionalLight (in int theId," - EOL" in vec3 theNormal," - EOL" in vec3 theView," - EOL" in bool theIsFront," - EOL" in float theShadow)" - EOL"{" - EOL" vec3 aLight = vec3 (occWorldViewMatrix * vec4 (occLight_Position (theId), 0.0));" - EOL - EOL" vec3 aHalf = normalize (aLight + theView);" - EOL - EOL" vec3 aFaceSideNormal = theIsFront ? theNormal : -theNormal;" - EOL" float aNdotL = max (0.0, dot (aFaceSideNormal, aLight));" - EOL" float aNdotH = max (0.0, dot (aFaceSideNormal, aHalf ));" - EOL - EOL" float aSpecl = 0.0;" - EOL" if (aNdotL > 0.0)" - EOL" {" - EOL" aSpecl = pow (aNdotH, theIsFront ? occFrontMaterial_Shininess() : occBackMaterial_Shininess());" - EOL" }" - EOL - EOL" Diffuse += occLight_Diffuse (theId) * aNdotL * theShadow;" - EOL" Specular += occLight_Specular (theId) * aSpecl * theShadow;" - EOL"}"; - -//! Function computes contribution of directional light source -const char THE_FUNC_PBR_directionalLight[] = - EOL"void directionalLight (in int theId," - EOL" in vec3 theNormal," - EOL" in vec3 theView," - EOL" in bool theIsFront," - EOL" in float theShadow)" - EOL"{" - EOL" vec3 aLight = occLight_Position (theId);" - EOL - EOL" theNormal = theIsFront ? theNormal : -theNormal;" - EOL" DirectLighting += occPBRIllumination (theView, aLight, theNormal," - EOL" BaseColor, Metallic, Roughness, IOR," - EOL" occLight_Specular (theId)," - EOL" occLight_Intensity(theId)) * theShadow;" - EOL"}"; - -//! The same as THE_FUNC_directionalLight but for the light with zero index +//! The same as Shaders_PhongDirectionalLight_glsl but for the light with zero index //! (avoids limitations on some mobile devices). const char THE_FUNC_directionalLightFirst[] = EOL"void directionalLightFirst (in vec3 theNormal," @@ -2305,8 +2126,8 @@ TCollection_AsciiString OpenGl_ShaderManager::stdComputeLighting (Standard_Integ if (aLightIter.Value()->Type() == Graphic3d_TOLS_DIRECTIONAL && aLightIter.Value()->ToCastShadows()) { - aLightsLoop = aLightsLoop + EOL" directionalLight (" + anIndex + ", theNormal, theView, theIsFront," - EOL" occDirectionalLightShadow (" + anIndex + ", theNormal));"; + aLightsLoop = aLightsLoop + EOL" occDirectionalLight (" + anIndex + ", theNormal, theView, theIsFront," + EOL" occDirectionalLightShadow (" + anIndex + ", theNormal));"; ++anIndex; } } @@ -2327,19 +2148,19 @@ TCollection_AsciiString OpenGl_ShaderManager::stdComputeLighting (Standard_Integ { break; } - aLightsLoop = aLightsLoop + EOL" directionalLight (" + anIndex + ", theNormal, theView, theIsFront, 1.0);"; + aLightsLoop = aLightsLoop + EOL" occDirectionalLight (" + anIndex + ", theNormal, theView, theIsFront, 1.0);"; ++anIndex; break; } case Graphic3d_TOLS_POSITIONAL: { - aLightsLoop = aLightsLoop + EOL" pointLight (" + anIndex + ", theNormal, theView, aPoint, theIsFront);"; + aLightsLoop = aLightsLoop + EOL" occPointLight (" + anIndex + ", theNormal, theView, aPoint, theIsFront);"; ++anIndex; break; } case Graphic3d_TOLS_SPOT: { - aLightsLoop = aLightsLoop + EOL" spotLight (" + anIndex + ", theNormal, theView, aPoint, theIsFront);"; + aLightsLoop = aLightsLoop + EOL" occSpotLight (" + anIndex + ", theNormal, theView, aPoint, theIsFront);"; ++anIndex; break; } @@ -2360,7 +2181,7 @@ TCollection_AsciiString OpenGl_ShaderManager::stdComputeLighting (Standard_Integ aLightsLoop += EOL" if (aType == OccLightType_Direct)" EOL" {" - EOL" directionalLight (anIndex, theNormal, theView, theIsFront, 1.0);" + EOL" occDirectionalLight (anIndex, theNormal, theView, theIsFront, 1.0);" EOL" }"; } if (aLights->NbEnabledLightsOfType (Graphic3d_TOLS_POSITIONAL) > 0) @@ -2373,7 +2194,7 @@ TCollection_AsciiString OpenGl_ShaderManager::stdComputeLighting (Standard_Integ aLightsLoop += EOL" if (aType == OccLightType_Point)" EOL" {" - EOL" pointLight (anIndex, theNormal, theView, aPoint, theIsFront);" + EOL" occPointLight (anIndex, theNormal, theView, aPoint, theIsFront);" EOL" }"; } if (aLights->NbEnabledLightsOfType (Graphic3d_TOLS_SPOT) > 0) @@ -2386,7 +2207,7 @@ TCollection_AsciiString OpenGl_ShaderManager::stdComputeLighting (Standard_Integ aLightsLoop += EOL" if (aType == OccLightType_Spot)" EOL" {" - EOL" spotLight (anIndex, theNormal, theView, aPoint, theIsFront);" + EOL" occSpotLight (anIndex, theNormal, theView, aPoint, theIsFront);" EOL" }"; } aLightsLoop += EOL" }"; @@ -2416,15 +2237,15 @@ TCollection_AsciiString OpenGl_ShaderManager::stdComputeLighting (Standard_Integ { aLightsFunc += Shaders_DirectionalLightShadow_glsl; } - aLightsFunc += theIsPBR ? THE_FUNC_PBR_directionalLight : THE_FUNC_directionalLight; + aLightsFunc += theIsPBR ? Shaders_PBRDirectionalLight_glsl : Shaders_PhongDirectionalLight_glsl; } if (aLights->NbEnabledLightsOfType (Graphic3d_TOLS_POSITIONAL) > 0) { - aLightsFunc += theIsPBR ? THE_FUNC_PBR_pointLight : THE_FUNC_pointLight; + aLightsFunc += theIsPBR ? Shaders_PBRPointLight_glsl : Shaders_PhongPointLight_glsl; } if (aLights->NbEnabledLightsOfType (Graphic3d_TOLS_SPOT) > 0) { - aLightsFunc += theIsPBR ? THE_FUNC_PBR_spotLight : THE_FUNC_spotLight; + aLightsFunc += theIsPBR ? Shaders_PBRSpotLight_glsl : Shaders_PhongSpotLight_glsl; } } diff --git a/src/Shaders/FILES b/src/Shaders/FILES index ada8209873..e4efc735ae 100644 --- a/src/Shaders/FILES +++ b/src/Shaders/FILES @@ -2,14 +2,20 @@ srcinc:::Declarations.glsl srcinc:::DeclarationsImpl.glsl srcinc:::DirectionalLightShadow.glsl srcinc:::PBRCookTorrance.glsl +srcinc:::PBRDirectionalLight.glsl srcinc:::PBRDistribution.glsl srcinc:::PBREnvBaking.fs srcinc:::PBREnvBaking.vs srcinc:::PBRFresnel.glsl srcinc:::PBRGeometry.glsl srcinc:::PBRIllumination.glsl +srcinc:::PBRPointLight.glsl +srcinc:::PBRSpotLight.glsl srcinc:::PhongShading.fs srcinc:::PhongShading.vs +srcinc:::PhongDirectionalLight.glsl +srcinc:::PhongPointLight.glsl +srcinc:::PhongSpotLight.glsl srcinc:::PointLightAttenuation.glsl srcinc:::Display.fs srcinc:::RaytraceBase.fs @@ -23,12 +29,18 @@ Shaders_DeclarationsImpl_glsl.pxx Shaders_DirectionalLightShadow_glsl.pxx Shaders_Display_fs.pxx Shaders_PBRCookTorrance_glsl.pxx +Shaders_PBRDirectionalLight_glsl.pxx Shaders_PBRDistribution_glsl.pxx Shaders_PBREnvBaking_fs.pxx Shaders_PBREnvBaking_vs.pxx Shaders_PBRFresnel_glsl.pxx Shaders_PBRGeometry_glsl.pxx Shaders_PBRIllumination_glsl.pxx +Shaders_PBRPointLight_glsl.pxx +Shaders_PBRSpotLight_glsl.pxx +Shaders_PhongDirectionalLight_glsl.pxx +Shaders_PhongPointLight_glsl.pxx +Shaders_PhongSpotLight_glsl.pxx Shaders_PointLightAttenuation_glsl.pxx Shaders_RaytraceBase_fs.pxx Shaders_RaytraceRender_fs.pxx diff --git a/src/Shaders/PBRDirectionalLight.glsl b/src/Shaders/PBRDirectionalLight.glsl new file mode 100644 index 0000000000..770e996a42 --- /dev/null +++ b/src/Shaders/PBRDirectionalLight.glsl @@ -0,0 +1,20 @@ +//! Function computes contribution of directional light source +//! into global variable DirectLighting (PBR shading). +//! @param theId light source index +//! @param theNormal surface normal +//! @param theView view direction +//! @param theIsFront front/back face flag +//! @param theShadow shadow attenuation +void occDirectionalLight (in int theId, + in vec3 theNormal, + in vec3 theView, + in bool theIsFront, + in float theShadow) +{ + vec3 aLight = occLight_Position (theId); + theNormal = theIsFront ? theNormal : -theNormal; + DirectLighting += occPBRIllumination (theView, aLight, theNormal, + BaseColor, Metallic, Roughness, IOR, + occLight_Specular (theId), + occLight_Intensity(theId)) * theShadow; +} diff --git a/src/Shaders/PBRPointLight.glsl b/src/Shaders/PBRPointLight.glsl new file mode 100644 index 0000000000..d6c0fca936 --- /dev/null +++ b/src/Shaders/PBRPointLight.glsl @@ -0,0 +1,27 @@ +//! Function computes contribution of isotropic point light source +//! into global variable DirectLighting (PBR shading). +//! @param theId light source index +//! @param theNormal surface normal +//! @param theView view direction +//! @param thePoint 3D position (world space) +//! @param theIsFront front/back face flag +void occPointLight (in int theId, + in vec3 theNormal, + in vec3 theView, + in vec3 thePoint, + in bool theIsFront) +{ + vec3 aLight = occLight_Position (theId) - thePoint; + + float aDist = length (aLight); + float aRange = occLight_Range (theId); + float anAtten = occPointLightAttenuation (aDist, aRange); + if (anAtten <= 0.0) return; + aLight /= aDist; + + theNormal = theIsFront ? theNormal : -theNormal; + DirectLighting += occPBRIllumination (theView, aLight, theNormal, + BaseColor, Metallic, Roughness, IOR, + occLight_Specular (theId), + occLight_Intensity(theId) * anAtten); +} diff --git a/src/Shaders/PBRSpotLight.glsl b/src/Shaders/PBRSpotLight.glsl new file mode 100644 index 0000000000..4692f7be02 --- /dev/null +++ b/src/Shaders/PBRSpotLight.glsl @@ -0,0 +1,44 @@ +//! Function computes contribution of spotlight source +//! into global variable DirectLighting (PBR shading). +//! @param theId light source index +//! @param theNormal surface normal +//! @param theView view direction +//! @param thePoint 3D position (world space) +//! @param theIsFront front/back face flag +void occSpotLight (in int theId, + in vec3 theNormal, + in vec3 theView, + in vec3 thePoint, + in bool theIsFront) +{ + vec3 aLight = occLight_Position (theId) - thePoint; + + float aDist = length (aLight); + float aRange = occLight_Range (theId); + float anAtten = occPointLightAttenuation (aDist, aRange); + if (anAtten <= 0.0) return; + aLight /= aDist; + + vec3 aSpotDir = occLight_SpotDirection (theId); + // light cone + float aCosA = dot (aSpotDir, -aLight); + float aRelativeAngle = 2.0 * acos(aCosA) / occLight_SpotCutOff(theId); + if (aCosA >= 1.0 || aRelativeAngle > 1.0) + { + return; + } + float anExponent = occLight_SpotExponent (theId); + if ((1.0 - aRelativeAngle) <= anExponent) + { + float anAngularAttenuationOffset = cos(0.5 * occLight_SpotCutOff(theId)); + float anAngularAttenuationScale = 1.0 / max(0.001, cos(0.5 * occLight_SpotCutOff(theId) * (1.0 - anExponent)) - anAngularAttenuationOffset); + anAngularAttenuationOffset *= -anAngularAttenuationScale; + float anAngularAttenuantion = clamp(aCosA * anAngularAttenuationScale + anAngularAttenuationOffset, 0.0, 1.0); + anAtten *= anAngularAttenuantion * anAngularAttenuantion; + } + theNormal = theIsFront ? theNormal : -theNormal; + DirectLighting += occPBRIllumination (theView, aLight, theNormal, + BaseColor, Metallic, Roughness, IOR, + occLight_Specular(theId), + occLight_Intensity(theId) * anAtten); +} diff --git a/src/Shaders/PhongDirectionalLight.glsl b/src/Shaders/PhongDirectionalLight.glsl new file mode 100644 index 0000000000..6b9d3112f5 --- /dev/null +++ b/src/Shaders/PhongDirectionalLight.glsl @@ -0,0 +1,29 @@ +//! Function computes contribution of directional light source +//! into global variables Diffuse and Specular (Phong shading). +//! @param theId light source index +//! @param theNormal surface normal +//! @param theView view direction +//! @param theIsFront front/back face flag +//! @param theShadow shadow attenuation +void occDirectionalLight (in int theId, + in vec3 theNormal, + in vec3 theView, + in bool theIsFront, + in float theShadow) +{ + vec3 aLight = vec3 (occWorldViewMatrix * vec4 (occLight_Position (theId), 0.0)); + vec3 aHalf = normalize (aLight + theView); + + vec3 aFaceSideNormal = theIsFront ? theNormal : -theNormal; + float aNdotL = max (0.0, dot (aFaceSideNormal, aLight)); + float aNdotH = max (0.0, dot (aFaceSideNormal, aHalf )); + + float aSpecl = 0.0; + if (aNdotL > 0.0) + { + aSpecl = pow (aNdotH, theIsFront ? occFrontMaterial_Shininess() : occBackMaterial_Shininess()); + } + + Diffuse += occLight_Diffuse (theId) * aNdotL * theShadow; + Specular += occLight_Specular (theId) * aSpecl * theShadow; +} diff --git a/src/Shaders/PhongPointLight.glsl b/src/Shaders/PhongPointLight.glsl new file mode 100644 index 0000000000..d0e67b976d --- /dev/null +++ b/src/Shaders/PhongPointLight.glsl @@ -0,0 +1,36 @@ +//! Function computes contribution of isotropic point light source +//! into global variables Diffuse and Specular (Phong shading). +//! @param theId light source index +//! @param theNormal surface normal +//! @param theView view direction +//! @param thePoint 3D position (view space) +//! @param theIsFront front/back face flag +void occPointLight (in int theId, + in vec3 theNormal, + in vec3 theView, + in vec3 thePoint, + in bool theIsFront) +{ + vec3 aLight = vec3 (occWorldViewMatrix * vec4 (occLight_Position (theId), 1.0)) - thePoint; + + float aDist = length (aLight); + float aRange = occLight_Range (theId); + float anAtten = occPointLightAttenuation (aDist, aRange, occLight_LinearAttenuation (theId), occLight_ConstAttenuation (theId)); + if (anAtten <= 0.0) return; + aLight /= aDist; + + vec3 aHalf = normalize (aLight + theView); + + vec3 aFaceSideNormal = theIsFront ? theNormal : -theNormal; + float aNdotL = max (0.0, dot (aFaceSideNormal, aLight)); + float aNdotH = max (0.0, dot (aFaceSideNormal, aHalf )); + + float aSpecl = 0.0; + if (aNdotL > 0.0) + { + aSpecl = pow (aNdotH, theIsFront ? occFrontMaterial_Shininess() : occBackMaterial_Shininess()); + } + + Diffuse += occLight_Diffuse (theId) * aNdotL * anAtten; + Specular += occLight_Specular(theId) * aSpecl * anAtten; +} diff --git a/src/Shaders/PhongSpotLight.glsl b/src/Shaders/PhongSpotLight.glsl new file mode 100644 index 0000000000..5139fa6c1c --- /dev/null +++ b/src/Shaders/PhongSpotLight.glsl @@ -0,0 +1,51 @@ +//! Function computes contribution of spotlight source +//! into global variables Diffuse and Specular (Phong shading). +//! @param theId light source index +//! @param theNormal surface normal +//! @param theView view direction +//! @param thePoint 3D position (view space) +//! @param theIsFront front/back face flag +void occSpotLight (in int theId, + in vec3 theNormal, + in vec3 theView, + in vec3 thePoint, + in bool theIsFront) +{ + vec3 aLight = vec3 (occWorldViewMatrix * vec4 (occLight_Position (theId), 1.0)) - thePoint; + + float aDist = length (aLight); + float aRange = occLight_Range (theId); + float anAtten = occPointLightAttenuation (aDist, aRange, occLight_LinearAttenuation (theId), occLight_ConstAttenuation (theId)); + if (anAtten <= 0.0) return; + aLight /= aDist; + + vec3 aSpotDir = vec3 (occWorldViewMatrix * vec4 (occLight_SpotDirection (theId), 0.0)); + aSpotDir = normalize (aSpotDir); + // light cone + float aCosA = dot (aSpotDir, -aLight); + if (aCosA >= 1.0 || aCosA < cos (occLight_SpotCutOff (theId))) + { + return; + } + + float anExponent = occLight_SpotExponent (theId); + if (anExponent > 0.0) + { + anAtten *= pow (aCosA, anExponent * 128.0); + } + + vec3 aHalf = normalize (aLight + theView); + + vec3 aFaceSideNormal = theIsFront ? theNormal : -theNormal; + float aNdotL = max (0.0, dot (aFaceSideNormal, aLight)); + float aNdotH = max (0.0, dot (aFaceSideNormal, aHalf )); + + float aSpecl = 0.0; + if (aNdotL > 0.0) + { + aSpecl = pow (aNdotH, theIsFront ? occFrontMaterial_Shininess() : occBackMaterial_Shininess()); + } + + Diffuse += occLight_Diffuse (theId) * aNdotL * anAtten; + Specular += occLight_Specular(theId) * aSpecl * anAtten; +} diff --git a/src/Shaders/Shaders_PBRDirectionalLight_glsl.pxx b/src/Shaders/Shaders_PBRDirectionalLight_glsl.pxx new file mode 100644 index 0000000000..1f7ae942ed --- /dev/null +++ b/src/Shaders/Shaders_PBRDirectionalLight_glsl.pxx @@ -0,0 +1,23 @@ +// This file has been automatically generated from resource file src/Shaders/PBRDirectionalLight.glsl + +static const char Shaders_PBRDirectionalLight_glsl[] = + "//! Function computes contribution of directional light source\n" + "//! into global variable DirectLighting (PBR shading).\n" + "//! @param theId light source index\n" + "//! @param theNormal surface normal\n" + "//! @param theView view direction\n" + "//! @param theIsFront front/back face flag\n" + "//! @param theShadow shadow attenuation\n" + "void occDirectionalLight (in int theId,\n" + " in vec3 theNormal,\n" + " in vec3 theView,\n" + " in bool theIsFront,\n" + " in float theShadow)\n" + "{\n" + " vec3 aLight = occLight_Position (theId);\n" + " theNormal = theIsFront ? theNormal : -theNormal;\n" + " DirectLighting += occPBRIllumination (theView, aLight, theNormal,\n" + " BaseColor, Metallic, Roughness, IOR,\n" + " occLight_Specular (theId),\n" + " occLight_Intensity(theId)) * theShadow;\n" + "}\n"; diff --git a/src/Shaders/Shaders_PBRPointLight_glsl.pxx b/src/Shaders/Shaders_PBRPointLight_glsl.pxx new file mode 100644 index 0000000000..d08f5908e8 --- /dev/null +++ b/src/Shaders/Shaders_PBRPointLight_glsl.pxx @@ -0,0 +1,30 @@ +// This file has been automatically generated from resource file src/Shaders/PBRPointLight.glsl + +static const char Shaders_PBRPointLight_glsl[] = + "//! Function computes contribution of isotropic point light source\n" + "//! into global variable DirectLighting (PBR shading).\n" + "//! @param theId light source index\n" + "//! @param theNormal surface normal\n" + "//! @param theView view direction\n" + "//! @param thePoint 3D position (world space)\n" + "//! @param theIsFront front/back face flag\n" + "void occPointLight (in int theId,\n" + " in vec3 theNormal,\n" + " in vec3 theView,\n" + " in vec3 thePoint,\n" + " in bool theIsFront)\n" + "{\n" + " vec3 aLight = occLight_Position (theId) - thePoint;\n" + "\n" + " float aDist = length (aLight);\n" + " float aRange = occLight_Range (theId);\n" + " float anAtten = occPointLightAttenuation (aDist, aRange);\n" + " if (anAtten <= 0.0) return;\n" + " aLight /= aDist;\n" + "\n" + " theNormal = theIsFront ? theNormal : -theNormal;\n" + " DirectLighting += occPBRIllumination (theView, aLight, theNormal,\n" + " BaseColor, Metallic, Roughness, IOR,\n" + " occLight_Specular (theId),\n" + " occLight_Intensity(theId) * anAtten);\n" + "}\n"; diff --git a/src/Shaders/Shaders_PBRSpotLight_glsl.pxx b/src/Shaders/Shaders_PBRSpotLight_glsl.pxx new file mode 100644 index 0000000000..92734288a1 --- /dev/null +++ b/src/Shaders/Shaders_PBRSpotLight_glsl.pxx @@ -0,0 +1,47 @@ +// This file has been automatically generated from resource file src/Shaders/PBRSpotLight.glsl + +static const char Shaders_PBRSpotLight_glsl[] = + "//! Function computes contribution of spotlight source\n" + "//! into global variable DirectLighting (PBR shading).\n" + "//! @param theId light source index\n" + "//! @param theNormal surface normal\n" + "//! @param theView view direction\n" + "//! @param thePoint 3D position (world space)\n" + "//! @param theIsFront front/back face flag\n" + "void occSpotLight (in int theId,\n" + " in vec3 theNormal,\n" + " in vec3 theView,\n" + " in vec3 thePoint,\n" + " in bool theIsFront)\n" + "{\n" + " vec3 aLight = occLight_Position (theId) - thePoint;\n" + "\n" + " float aDist = length (aLight);\n" + " float aRange = occLight_Range (theId);\n" + " float anAtten = occPointLightAttenuation (aDist, aRange);\n" + " if (anAtten <= 0.0) return;\n" + " aLight /= aDist;\n" + "\n" + " vec3 aSpotDir = occLight_SpotDirection (theId);\n" + " // light cone\n" + " float aCosA = dot (aSpotDir, -aLight);\n" + " float aRelativeAngle = 2.0 * acos(aCosA) / occLight_SpotCutOff(theId);\n" + " if (aCosA >= 1.0 || aRelativeAngle > 1.0)\n" + " {\n" + " return;\n" + " }\n" + " float anExponent = occLight_SpotExponent (theId);\n" + " if ((1.0 - aRelativeAngle) <= anExponent)\n" + " {\n" + " float anAngularAttenuationOffset = cos(0.5 * occLight_SpotCutOff(theId));\n" + " float anAngularAttenuationScale = 1.0 / max(0.001, cos(0.5 * occLight_SpotCutOff(theId) * (1.0 - anExponent)) - anAngularAttenuationOffset);\n" + " anAngularAttenuationOffset *= -anAngularAttenuationScale;\n" + " float anAngularAttenuantion = clamp(aCosA * anAngularAttenuationScale + anAngularAttenuationOffset, 0.0, 1.0);\n" + " anAtten *= anAngularAttenuantion * anAngularAttenuantion;\n" + " }\n" + " theNormal = theIsFront ? theNormal : -theNormal;\n" + " DirectLighting += occPBRIllumination (theView, aLight, theNormal,\n" + " BaseColor, Metallic, Roughness, IOR,\n" + " occLight_Specular(theId),\n" + " occLight_Intensity(theId) * anAtten);\n" + "}\n"; diff --git a/src/Shaders/Shaders_PhongDirectionalLight_glsl.pxx b/src/Shaders/Shaders_PhongDirectionalLight_glsl.pxx new file mode 100644 index 0000000000..477d79ee4a --- /dev/null +++ b/src/Shaders/Shaders_PhongDirectionalLight_glsl.pxx @@ -0,0 +1,32 @@ +// This file has been automatically generated from resource file src/Shaders/PhongDirectionalLight.glsl + +static const char Shaders_PhongDirectionalLight_glsl[] = + "//! Function computes contribution of directional light source\n" + "//! into global variables Diffuse and Specular (Phong shading).\n" + "//! @param theId light source index\n" + "//! @param theNormal surface normal\n" + "//! @param theView view direction\n" + "//! @param theIsFront front/back face flag\n" + "//! @param theShadow shadow attenuation\n" + "void occDirectionalLight (in int theId,\n" + " in vec3 theNormal,\n" + " in vec3 theView,\n" + " in bool theIsFront,\n" + " in float theShadow)\n" + "{\n" + " vec3 aLight = vec3 (occWorldViewMatrix * vec4 (occLight_Position (theId), 0.0));\n" + " vec3 aHalf = normalize (aLight + theView);\n" + "\n" + " vec3 aFaceSideNormal = theIsFront ? theNormal : -theNormal;\n" + " float aNdotL = max (0.0, dot (aFaceSideNormal, aLight));\n" + " float aNdotH = max (0.0, dot (aFaceSideNormal, aHalf ));\n" + "\n" + " float aSpecl = 0.0;\n" + " if (aNdotL > 0.0)\n" + " {\n" + " aSpecl = pow (aNdotH, theIsFront ? occFrontMaterial_Shininess() : occBackMaterial_Shininess());\n" + " }\n" + "\n" + " Diffuse += occLight_Diffuse (theId) * aNdotL * theShadow;\n" + " Specular += occLight_Specular (theId) * aSpecl * theShadow;\n" + "}\n"; diff --git a/src/Shaders/Shaders_PhongPointLight_glsl.pxx b/src/Shaders/Shaders_PhongPointLight_glsl.pxx new file mode 100644 index 0000000000..8ad8b83d32 --- /dev/null +++ b/src/Shaders/Shaders_PhongPointLight_glsl.pxx @@ -0,0 +1,39 @@ +// This file has been automatically generated from resource file src/Shaders/PhongPointLight.glsl + +static const char Shaders_PhongPointLight_glsl[] = + "//! Function computes contribution of isotropic point light source\n" + "//! into global variables Diffuse and Specular (Phong shading).\n" + "//! @param theId light source index\n" + "//! @param theNormal surface normal\n" + "//! @param theView view direction\n" + "//! @param thePoint 3D position (view space)\n" + "//! @param theIsFront front/back face flag\n" + "void occPointLight (in int theId,\n" + " in vec3 theNormal,\n" + " in vec3 theView,\n" + " in vec3 thePoint,\n" + " in bool theIsFront)\n" + "{\n" + " vec3 aLight = vec3 (occWorldViewMatrix * vec4 (occLight_Position (theId), 1.0)) - thePoint;\n" + "\n" + " float aDist = length (aLight);\n" + " float aRange = occLight_Range (theId);\n" + " float anAtten = occPointLightAttenuation (aDist, aRange, occLight_LinearAttenuation (theId), occLight_ConstAttenuation (theId));\n" + " if (anAtten <= 0.0) return;\n" + " aLight /= aDist;\n" + "\n" + " vec3 aHalf = normalize (aLight + theView);\n" + "\n" + " vec3 aFaceSideNormal = theIsFront ? theNormal : -theNormal;\n" + " float aNdotL = max (0.0, dot (aFaceSideNormal, aLight));\n" + " float aNdotH = max (0.0, dot (aFaceSideNormal, aHalf ));\n" + "\n" + " float aSpecl = 0.0;\n" + " if (aNdotL > 0.0)\n" + " {\n" + " aSpecl = pow (aNdotH, theIsFront ? occFrontMaterial_Shininess() : occBackMaterial_Shininess());\n" + " }\n" + "\n" + " Diffuse += occLight_Diffuse (theId) * aNdotL * anAtten;\n" + " Specular += occLight_Specular(theId) * aSpecl * anAtten;\n" + "}\n"; diff --git a/src/Shaders/Shaders_PhongSpotLight_glsl.pxx b/src/Shaders/Shaders_PhongSpotLight_glsl.pxx new file mode 100644 index 0000000000..30b88ca48a --- /dev/null +++ b/src/Shaders/Shaders_PhongSpotLight_glsl.pxx @@ -0,0 +1,54 @@ +// This file has been automatically generated from resource file src/Shaders/PhongSpotLight.glsl + +static const char Shaders_PhongSpotLight_glsl[] = + "//! Function computes contribution of spotlight source\n" + "//! into global variables Diffuse and Specular (Phong shading).\n" + "//! @param theId light source index\n" + "//! @param theNormal surface normal\n" + "//! @param theView view direction\n" + "//! @param thePoint 3D position (view space)\n" + "//! @param theIsFront front/back face flag\n" + "void occSpotLight (in int theId,\n" + " in vec3 theNormal,\n" + " in vec3 theView,\n" + " in vec3 thePoint,\n" + " in bool theIsFront)\n" + "{\n" + " vec3 aLight = vec3 (occWorldViewMatrix * vec4 (occLight_Position (theId), 1.0)) - thePoint;\n" + "\n" + " float aDist = length (aLight);\n" + " float aRange = occLight_Range (theId);\n" + " float anAtten = occPointLightAttenuation (aDist, aRange, occLight_LinearAttenuation (theId), occLight_ConstAttenuation (theId));\n" + " if (anAtten <= 0.0) return;\n" + " aLight /= aDist;\n" + "\n" + " vec3 aSpotDir = vec3 (occWorldViewMatrix * vec4 (occLight_SpotDirection (theId), 0.0));\n" + " aSpotDir = normalize (aSpotDir);\n" + " // light cone\n" + " float aCosA = dot (aSpotDir, -aLight);\n" + " if (aCosA >= 1.0 || aCosA < cos (occLight_SpotCutOff (theId)))\n" + " {\n" + " return;\n" + " }\n" + "\n" + " float anExponent = occLight_SpotExponent (theId);\n" + " if (anExponent > 0.0)\n" + " {\n" + " anAtten *= pow (aCosA, anExponent * 128.0);\n" + " }\n" + "\n" + " vec3 aHalf = normalize (aLight + theView);\n" + "\n" + " vec3 aFaceSideNormal = theIsFront ? theNormal : -theNormal;\n" + " float aNdotL = max (0.0, dot (aFaceSideNormal, aLight));\n" + " float aNdotH = max (0.0, dot (aFaceSideNormal, aHalf ));\n" + "\n" + " float aSpecl = 0.0;\n" + " if (aNdotL > 0.0)\n" + " {\n" + " aSpecl = pow (aNdotH, theIsFront ? occFrontMaterial_Shininess() : occBackMaterial_Shininess());\n" + " }\n" + "\n" + " Diffuse += occLight_Diffuse (theId) * aNdotL * anAtten;\n" + " Specular += occLight_Specular(theId) * aSpecl * anAtten;\n" + "}\n";