Skip to content

Commit

Permalink
Cleaning up shader code and improving code consistency
Browse files Browse the repository at this point in the history
  • Loading branch information
adriengivry committed Mar 20, 2024
1 parent 79e51d7 commit 393751a
Show file tree
Hide file tree
Showing 10 changed files with 151 additions and 158 deletions.
14 changes: 6 additions & 8 deletions Resources/Engine/Shaders/Common/Physics.ovfxh
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
bool PointInAABB(vec3 p_Point, vec3 p_AabbCenter, vec3 p_AabbHalfSize)
bool IsPointInAABB(vec3 point, vec3 aabbCenter, vec3 aabbHalfSize)
{
return
(
p_Point.x > p_AabbCenter.x - p_AabbHalfSize.x && p_Point.x < p_AabbCenter.x + p_AabbHalfSize.x &&
p_Point.y > p_AabbCenter.y - p_AabbHalfSize.y && p_Point.y < p_AabbCenter.y + p_AabbHalfSize.y &&
p_Point.z > p_AabbCenter.z - p_AabbHalfSize.z && p_Point.z < p_AabbCenter.z + p_AabbHalfSize.z
);
point.x > aabbCenter.x - aabbHalfSize.x && point.x < aabbCenter.x + aabbHalfSize.x &&
point.y > aabbCenter.y - aabbHalfSize.y && point.y < aabbCenter.y + aabbHalfSize.y &&
point.z > aabbCenter.z - aabbHalfSize.z && point.z < aabbCenter.z + aabbHalfSize.z;
}

bool PointInSphere(vec3 p_Point, vec3 p_SphereCenter, float p_SphereRadius)
bool IsPointInSphere(vec3 point, vec3 sphereCenter, float sphereRadius)
{
return distance(p_Point, p_SphereCenter) <= p_SphereRadius;
return distance(point, sphereCenter) <= sphereRadius;
}
29 changes: 14 additions & 15 deletions Resources/Engine/Shaders/Common/Utils.ovfxh
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
vec2 TRANSFORM_TEX_COORDS(vec2 texCoords, vec2 tiling, vec2 offset)
vec3 UnPack(float target)
{
return vec3 (
float((uint(target) >> 24) & 0xff) * 0.003921568627451,
float((uint(target) >> 16) & 0xff) * 0.003921568627451,
float((uint(target) >> 8) & 0xff) * 0.003921568627451
);
}

vec2 TileAndOffsetTexCoords(vec2 texCoords, vec2 tiling, vec2 offset)
{
return vec2(mod(texCoords.x * tiling.x, 1), mod(texCoords.y * tiling.y, 1)) + offset;
}

vec2 APPLY_PARALLAX_MAPPING(vec2 texCoords, sampler2D heightMap, vec3 tangentViewPos, vec3 tangentFragPos, float heightScale)
vec2 ApplyParallaxMapping(vec2 texCoords, sampler2D heightMap, vec3 tangentViewPos, vec3 tangentFragPos, float heightScale)
{
if (heightScale > 0)
{
Expand All @@ -15,12 +24,12 @@ vec2 APPLY_PARALLAX_MAPPING(vec2 texCoords, sampler2D heightMap, vec3 tangentVie
return texCoords;
}

bool IS_MASKED(sampler2D maskMap, vec2 texCoords)
bool IsMasked(sampler2D maskMap, vec2 texCoords)
{
return texture(maskMap, texCoords).r == 0.0;
}

mat3 COMPUTE_TBN(mat4 model, vec3 normal, vec3 tangent, vec3 bitangent)
mat3 ConstructTBN(mat4 model, vec3 normal, vec3 tangent, vec3 bitangent)
{
return mat3(
normalize(vec3(model * vec4(tangent, 0.0))),
Expand All @@ -29,17 +38,7 @@ mat3 COMPUTE_TBN(mat4 model, vec3 normal, vec3 tangent, vec3 bitangent)
);
}

vec3 UnPack(float p_Target)
{
return vec3
(
float((uint(p_Target) >> 24) & 0xff) * 0.003921568627451,
float((uint(p_Target) >> 16) & 0xff) * 0.003921568627451,
float((uint(p_Target) >> 8) & 0xff) * 0.003921568627451
);
}

vec3 COMPUTE_NORMAL(bool enableNormalMapping, vec2 texCoords, vec3 normal, sampler2D normalMap, mat3 TBN)
vec3 ComputeNormal(bool enableNormalMapping, vec2 texCoords, vec3 normal, sampler2D normalMap, mat3 TBN)
{
if (enableNormalMapping)
{
Expand Down
4 changes: 2 additions & 2 deletions Resources/Engine/Shaders/Lambert.ovfx
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ out vec4 FRAGMENT_COLOR;

void main()
{
vec2 texCoords = TRANSFORM_TEX_COORDS(fs_in.TexCoords, u_TextureTiling, u_TextureOffset);
vec2 texCoords = TileAndOffsetTexCoords(fs_in.TexCoords, u_TextureTiling, u_TextureOffset);

const vec3 kLightPosition = vec3(-9000.0, 10000.0, 11000.0);
const vec3 kLightDiffuse = vec3(1.0);
const vec3 kLightAmbient = vec3(0.3);
const vec3 lambert = Lambert(fs_in.FragPos, fs_in.Normal, kLightPosition, kLightDiffuse, kLightAmbient);
const vec3 lambert = ComputeLambertLighting(fs_in.FragPos, fs_in.Normal, kLightPosition, kLightDiffuse, kLightAmbient);

FRAGMENT_COLOR = texture(u_DiffuseMap, texCoords) * u_Diffuse * vec4(lambert, 1.0);
}
104 changes: 48 additions & 56 deletions Resources/Engine/Shaders/Lighting/BlinnPhong.ovfxh
Original file line number Diff line number Diff line change
@@ -1,106 +1,98 @@
#include ":Shaders/Common/Physics.ovfxh"
#include ":Shaders/Common/Utils.ovfxh"
#include ":Shaders/Lighting/Shared.ovfxh"
#include ":Shaders/Common/Buffers/LightsSSBO.ovfxh"

vec3 BlinnPhong(vec3 p_LightDir, vec3 p_LightColor, float p_Luminosity, vec4 diffuseTexel, vec4 specularTexel, vec3 normal, vec3 viewDir, float shininess)
vec3 BlinnPhong(vec3 lightDir, vec3 lightColor, float luminosity, vec4 diffuseTexel, vec4 specularTexel, vec3 normal, vec3 viewDir, float shininess)
{
const vec3 halfwayDir = normalize(p_LightDir + viewDir);
const float diffuseCoefficient = max(dot(normal, p_LightDir), 0.0);
const vec3 halfwayDir = normalize(lightDir + viewDir);
const float diffuseCoefficient = max(dot(normal, lightDir), 0.0);
const float specularCoefficient = pow(max(dot(normal, halfwayDir), 0.0), shininess * 2.0);

return p_LightColor * diffuseTexel.rgb * diffuseCoefficient * p_Luminosity + ((p_Luminosity > 0.0) ? (p_LightColor * specularTexel.rgb * specularCoefficient * p_Luminosity) : vec3(0.0));
return lightColor * diffuseTexel.rgb * diffuseCoefficient * luminosity + ((luminosity > 0.0) ? (lightColor * specularTexel.rgb * specularCoefficient * luminosity) : vec3(0.0));
}

float LuminosityFromAttenuation(mat4 p_Light, vec3 fragPos)
{
const vec3 lightPosition = p_Light[0].rgb;
const float constant = p_Light[0][3];
const float linear = p_Light[1][3];
const float quadratic = p_Light[2][3];

const float distanceToLight = length(lightPosition - fragPos);
const float attenuation = (constant + linear * distanceToLight + quadratic * (distanceToLight * distanceToLight));
return 1.0 / attenuation;
}

vec3 CalcPointLight(mat4 p_Light, vec3 fragPos, vec4 diffuseTexel, vec4 specularTexel, vec3 normal, vec3 viewDir, float shininess)
vec3 ComputePointLight(mat4 light, vec3 fragPos, vec4 diffuseTexel, vec4 specularTexel, vec3 normal, vec3 viewDir, float shininess)
{
/* Extract light information from light mat4 */
const vec3 lightPosition = p_Light[0].rgb;
const vec3 lightColor = UnPack(p_Light[2][0]);
const float intensity = p_Light[3][3];
const vec3 lightPosition = light[0].rgb;
const vec3 lightColor = UnPack(light[2][0]);
const float intensity = light[3][3];

const vec3 lightDirection = normalize(lightPosition - fragPos);
const float luminosity = LuminosityFromAttenuation(p_Light, fragPos);
const vec3 lightDirection = normalize(lightPosition - fragPos);
const float luminosity = LuminosityFromAttenuation(light, fragPos);

return BlinnPhong(lightDirection, lightColor, intensity * luminosity, diffuseTexel, specularTexel, normal, viewDir, shininess);
}

vec3 CalcDirectionalLight(mat4 light, vec4 diffuseTexel, vec4 specularTexel, vec3 normal, vec3 viewDir, float shininess)
vec3 ComputeDirectionalLight(mat4 light, vec4 diffuseTexel, vec4 specularTexel, vec3 normal, vec3 viewDir, float shininess)
{
return BlinnPhong(-light[1].rgb, UnPack(light[2][0]), light[3][3], diffuseTexel, specularTexel, normal, viewDir, shininess);
}

vec3 CalcSpotLight(mat4 p_Light, vec3 fragPos, vec4 diffuseTexel, vec4 specularTexel, vec3 normal, vec3 viewDir, float shininess)
vec3 ComputeSpotLight(mat4 light, vec3 fragPos, vec4 diffuseTexel, vec4 specularTexel, vec3 normal, vec3 viewDir, float shininess)
{
/* Extract light information from light mat4 */
const vec3 lightPosition = p_Light[0].rgb;
const vec3 lightForward = p_Light[1].rgb;
const vec3 lightColor = UnPack(p_Light[2][0]);
const float intensity = p_Light[3][3];
const float cutOff = cos(radians(p_Light[3][1]));
const float outerCutOff = cos(radians(p_Light[3][1] + p_Light[3][2]));
const vec3 lightPosition = light[0].rgb;
const vec3 lightForward = light[1].rgb;
const vec3 lightColor = UnPack(light[2][0]);
const float intensity = light[3][3];
const float cutOff = cos(radians(light[3][1]));
const float outerCutOff = cos(radians(light[3][1] + light[3][2]));

const vec3 lightDirection = normalize(lightPosition - fragPos);
const float luminosity = LuminosityFromAttenuation(p_Light, fragPos);
const vec3 lightDirection = normalize(lightPosition - fragPos);
const float luminosity = LuminosityFromAttenuation(light, fragPos);

/* Calculate the spot intensity */
const float theta = dot(lightDirection, normalize(-lightForward));
const float epsilon = cutOff - outerCutOff;
const float spotIntensity = clamp((theta - outerCutOff) / epsilon, 0.0, 1.0);
const float theta = dot(lightDirection, normalize(-lightForward));
const float epsilon = cutOff - outerCutOff;
const float spotIntensity = clamp((theta - outerCutOff) / epsilon, 0.0, 1.0);

return BlinnPhong(lightDirection, lightColor, intensity * spotIntensity * luminosity, diffuseTexel, specularTexel, normal, viewDir, shininess);
}

vec3 CalcAmbientBoxLight(mat4 p_Light, vec3 fragPos, vec4 diffuseTexel)
vec3 ComputeAmbientBoxLight(mat4 light, vec3 fragPos, vec4 diffuseTexel)
{
const vec3 lightPosition = p_Light[0].rgb;
const vec3 lightColor = UnPack(p_Light[2][0]);
const float intensity = p_Light[3][3];
const vec3 size = vec3(p_Light[0][3], p_Light[1][3], p_Light[2][3]);
const vec3 lightPosition = light[0].rgb;
const vec3 lightColor = UnPack(light[2][0]);
const float intensity = light[3][3];
const vec3 size = vec3(light[0][3], light[1][3], light[2][3]);

return PointInAABB(fragPos, lightPosition, size) ? diffuseTexel.rgb * lightColor * intensity : vec3(0.0);
return IsPointInAABB(fragPos, lightPosition, size) ? diffuseTexel.rgb * lightColor * intensity : vec3(0.0);
}

vec3 CalcAmbientSphereLight(mat4 p_Light, vec3 fragPos, vec4 diffuseTexel)
vec3 ComputeAmbientSphereLight(mat4 light, vec3 fragPos, vec4 diffuseTexel)
{
const vec3 lightPosition = p_Light[0].rgb;
const vec3 lightColor = UnPack(p_Light[2][0]);
const float intensity = p_Light[3][3];
const float radius = p_Light[0][3];
const vec3 lightPosition = light[0].rgb;
const vec3 lightColor = UnPack(light[2][0]);
const float intensity = light[3][3];
const float radius = light[0][3];

return PointInSphere(fragPos, lightPosition, radius) ? diffuseTexel.rgb * lightColor * intensity : vec3(0.0);
return IsPointInSphere(fragPos, lightPosition, radius) ? diffuseTexel.rgb * lightColor * intensity : vec3(0.0);
}

vec4 BLINN_PHONG(vec2 texCoords, vec3 normal, vec3 viewPos, vec3 fragPos, vec4 diffuse, vec3 specular, sampler2D diffuseMap, sampler2D specularMap, float shininess)
vec4 ComputeBlinnPhongLighting(vec2 texCoords, vec3 normal, vec3 viewPos, vec3 fragPos, vec4 diffuse, vec3 specular, sampler2D diffuseMap, sampler2D specularMap, float shininess)
{
vec3 viewDir = normalize(viewPos - fragPos);
vec4 diffuseTexel = texture(diffuseMap, texCoords) * diffuse;
vec4 specularTexel = texture(specularMap, texCoords) * vec4(specular, 1.0);

vec3 lightSum = vec3(0.0);
vec3 lightAccumulation = vec3(0.0);

for (int i = 0; i < ssbo_Lights.length(); ++i)
{
switch(int(ssbo_Lights[i][3][0]))
const mat4 light = ssbo_Lights[i];
const int lightType = int(light[3][0]);

switch(lightType)
{
case 0: lightSum += CalcPointLight(ssbo_Lights[i], fragPos, diffuseTexel, specularTexel, normal, viewDir, shininess); break;
case 1: lightSum += CalcDirectionalLight(ssbo_Lights[i], diffuseTexel, specularTexel, normal, viewDir, shininess); break;
case 2: lightSum += CalcSpotLight(ssbo_Lights[i], fragPos, diffuseTexel, specularTexel, normal, viewDir, shininess); break;
case 3: lightSum += CalcAmbientBoxLight(ssbo_Lights[i], fragPos, diffuseTexel); break;
case 4: lightSum += CalcAmbientSphereLight(ssbo_Lights[i], fragPos, diffuseTexel); break;
case 0: lightAccumulation += ComputePointLight(light, fragPos, diffuseTexel, specularTexel, normal, viewDir, shininess); break;
case 1: lightAccumulation += ComputeDirectionalLight(light, diffuseTexel, specularTexel, normal, viewDir, shininess); break;
case 2: lightAccumulation += ComputeSpotLight(light, fragPos, diffuseTexel, specularTexel, normal, viewDir, shininess); break;
case 3: lightAccumulation += ComputeAmbientBoxLight(light, fragPos, diffuseTexel); break;
case 4: lightAccumulation += ComputeAmbientSphereLight(light, fragPos, diffuseTexel); break;
}
}

return vec4(lightSum, diffuseTexel.a);
return vec4(lightAccumulation, diffuseTexel.a);
}
4 changes: 2 additions & 2 deletions Resources/Engine/Shaders/Lighting/Lambert.ovfxh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
vec3 Lambert(vec3 p_fragPos, vec3 p_normal, vec3 lightPos, vec3 lightDiffuse, vec3 lightAmbient)
vec3 ComputeLambertLighting(vec3 fragPos, vec3 normal, vec3 lightPos, vec3 lightDiffuse, vec3 lightAmbient)
{
const float diffuse = max(dot(p_normal, normalize(lightPos - p_fragPos)), 0.0);
const float diffuse = max(dot(normal, normalize(lightPos - fragPos)), 0.0);
return clamp(lightDiffuse * diffuse + lightAmbient, 0.0, 1.0);
}
Loading

0 comments on commit 393751a

Please sign in to comment.