-
Notifications
You must be signed in to change notification settings - Fork 230
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cleaning up shader code and improving code consistency
- Loading branch information
1 parent
79e51d7
commit 393751a
Showing
10 changed files
with
151 additions
and
158 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
Oops, something went wrong.