Rearrange the light struc description in the shader side, needs the c++ to match now

This commit is contained in:
samcake 2016-09-28 18:32:35 -07:00
parent 21b7e9dfe8
commit b09151f2a2
13 changed files with 166 additions and 164 deletions

View file

@ -26,8 +26,46 @@ typedef glm::vec3 Vec3;
typedef glm::vec4 Vec4; typedef glm::vec4 Vec4;
typedef glm::quat Quat; typedef glm::quat Quat;
class Light { class Light {
public: public:
struct LightVolume {
vec3 position;
float radius;
vec3 direction;
float spotCos;
bool isPoint() { return bool(spotCos < 0.f); }
bool isSpot() { return bool(spotCos >= 0.f); }
vec3 getPosition() { return position; }
float getRadius() { return radius; }
float getRadiusSquare() { return radius * radius; }
vec3 getDirection() { return direction; }
float getSpotAngleCos() { return spotCos; }
};
struct LightIrradiance {
vec3 color;
float intensity;
float falloffRadius;
float falloffSpot;
float spare1;
float spare2;
vec3 getColor() { return color; }
float getIntensity() { return intensity; }
vec3 getIrradiance() { return color * intensity; }
float getFalloffRadius() { return falloffRadius; }
float getFalloffRadiusSquare() { return falloffRadius * falloffRadius; }
float getFalloffSpot() { return falloffSpot; }
};
enum Type { enum Type {
SUN = 0, SUN = 0,
POINT, POINT,

View file

@ -11,129 +11,74 @@
<@if not MODEL_LIGHT_SLH@> <@if not MODEL_LIGHT_SLH@>
<@def MODEL_LIGHT_SLH@> <@def MODEL_LIGHT_SLH@>
struct SphericalHarmonics { <@include model/SphericalHarmonics.shared.slh@>
vec4 L00; <@include model/LightVolume.shared.slh@>
vec4 L1m1; <@include model/LightIrradiance.shared.slh@>
vec4 L10;
vec4 L11;
vec4 L2m2;
vec4 L2m1;
vec4 L20;
vec4 L21;
vec4 L22;
};
vec4 evalSphericalLight(SphericalHarmonics sh, vec3 direction ) { // NOw lets define Light
vec3 dir = direction.xyz;
const float C1 = 0.429043;
const float C2 = 0.511664;
const float C3 = 0.743125;
const float C4 = 0.886227;
const float C5 = 0.247708;
vec4 value = C1 * sh.L22 * (dir.x * dir.x - dir.y * dir.y) +
C3 * sh.L20 * dir.z * dir.z +
C4 * sh.L00 - C5 * sh.L20 +
2.0 * C1 * ( sh.L2m2 * dir.x * dir.y +
sh.L21 * dir.x * dir.z +
sh.L2m1 * dir.y * dir.z ) +
2.0 * C2 * ( sh.L11 * dir.x +
sh.L1m1 * dir.y +
sh.L10 * dir.z ) ;
return value;
}
struct Light { struct Light {
vec4 _position; LightVolume volume;
vec4 _direction; LightIrradiance irradiance;
vec4 _color;
vec4 _attenuation;
vec4 _spot;
// vec4 _shadow;
vec4 _control;
vec4 _volumeGeometry;
SphericalHarmonics _ambientSphere;
}; };
const int LIGHT_SPOT = 2;
bool light_isSpot(Light l) {
return bool(l._control.y >= 2.f);
}
vec3 getLightPosition(Light l) { return l._position.xyz; }
vec3 getLightDirection(Light l) { return l._direction.xyz; } // direction is -Z axis
vec3 getLightColor(Light l) { return l._color.rgb; }
float getLightIntensity(Light l) { return l._color.w; }
float getLightAmbientIntensity(Light l) { return l._direction.w; }
float getLightSpotAngleCos(Light l) {
return l._spot.x;
}
vec2 getLightSpotOutsideNormal2(Light l) {
return vec2(-l._spot.y, l._spot.x);
}
float evalLightSpotAttenuation(Light l, float cosA) {
return pow(cosA, l._spot.w);
}
float getLightRadius(Light l) {
return l._attenuation.x;
}
float getLightSquareRadius(Light l) {
return getLightRadius(l) * getLightRadius(l);
}
float getLightCutoffRadius(Light l) {
return l._attenuation.z;
}
float getLightCutoffSquareRadius(Light l) {
return getLightCutoffRadius(l) * getLightCutoffRadius(l);
}
float getLightShowContour(Light l) {
return l._control.w;
}
vec4 getLightVolumeGeometry(Light l) {
return l._volumeGeometry;
}
// Light is the light source its self, d is the light's distance calculated as length(unnormalized light vector). // Light is the light source its self, d is the light's distance calculated as length(unnormalized light vector).
float evalLightAttenuation(Light l, float d) { float evalLightAttenuation(Light l, float d) {
float radius = getLightRadius(l); float radius = lightIrradiance_getFalloffRadius(l.irradiance);
float cutoff = lightVolume_getRadius(l.volume);
float denom = d / radius + 1.0; float denom = d / radius + 1.0;
float attenuation = 1.0 / (denom * denom); float attenuation = 1.0 / (denom * denom);
float cutoff = getLightCutoffRadius(l);
// "Fade" the edges of light sources to make things look a bit more attractive. // "Fade" the edges of light sources to make things look a bit more attractive.
// Note: this tends to look a bit odd at lower exponents. // Note: this tends to look a bit odd at lower exponents.
attenuation *= min(1, max(0, -(d - cutoff))); attenuation *= min(1, max(0, -(d - cutoff)));
return attenuation; return attenuation;
} }
SphericalHarmonics getLightAmbientSphere(Light l) {
float evalLightSpotAttenuation(Light l, float cosA) {
return pow(cosA, lightIrradiance_getFalloffSpot(l.irradiance));
}
bool light_isSpot(Light l) { return lightVolume_isSpot(l.volume); }
vec3 getLightPosition(Light l) { return lightVolume_getPosition(l.volume); }
vec3 getLightDirection(Light l) { return lightVolume_getDirection(l.volume); }
vec3 getLightColor(Light l) { return lightIrradiance_getColor(l.irradiance); }
float getLightIntensity(Light l) { return lightIrradiance_getIntensity(l.irradiance); }
vec3 getLightIrradiance(Light l) { return lightIrradiance_getIrradiance(l.irradiance); }
struct LightAmbient {
vec4 _ambient;
SphericalHarmonics _ambientSphere;
};
SphericalHarmonics getLightAmbientSphere(LightAmbient l) {
return l._ambientSphere; return l._ambientSphere;
} }
bool getLightHasAmbientMap(Light l) {
return l._control.x > 0; float getLightAmbientIntensity(LightAmbient l) { return l._ambient.w; }
bool getLightHasAmbientMap(LightAmbient l) {
return l._ambient.x > 0;
// return l._control.x > 0;
} }
float getLightAmbientMapNumMips(Light l) { float getLightAmbientMapNumMips(LightAmbient l) {
return l._control.x; return l._ambient.x;
} }
<@func declareLightBuffer(N)@> <@func declareLightBuffer(N)@>
@ -146,6 +91,7 @@ uniform lightBuffer {
Light getLight(int index) { Light getLight(int index) {
return lightArray[index]; return lightArray[index];
} }
<@else@> <@else@>
uniform lightBuffer { uniform lightBuffer {
Light light; Light light;
@ -161,39 +107,32 @@ Light getLight() {
bool clipFragToLightVolumePoint(Light light, vec3 fragPos, out vec4 fragLightVecLen2) {
fragLightVecLen2.xyz = getLightPosition(light) - fragPos.xyz;
fragLightVecLen2.w = dot(fragLightVecLen2.xyz, fragLightVecLen2.xyz);
// Kill if too far from the light center
if (fragLightVecLen2.w > getLightCutoffSquareRadius(light)) { <@func declareLightAmbientBuffer(N)@>
return false;
} <@if N@>
return true;
uniform lightAmbientBuffer {
LightAmbient lightAmbientArray[<$N$>];
};
LightAmbient getLightAmbient(int index) {
return lightAmbientArray[index];
} }
bool clipFragToLightVolumeSpot(Light light, vec3 fragPos, out vec4 fragLightVecLen2, out vec4 fragLightDirLen, out float cosSpotAngle) { <@else@>
fragLightVecLen2.xyz = getLightPosition(light) - fragPos.xyz; uniform lightAmbientBuffer {
fragLightVecLen2.w = dot(fragLightVecLen2.xyz, fragLightVecLen2.xyz); LightAmbient lightAmbient;
};
// Kill if too far from the light center LightAmbient getLightAmbient() {
if (fragLightVecLen2.w > getLightCutoffSquareRadius(light)) { return lightAmbient;
return false;
}
// Allright we re valid in the volume
fragLightDirLen.w = length(fragLightVecLen2.xyz);
fragLightDirLen.xyz = fragLightVecLen2.xyz / fragLightDirLen.w;
// Kill if not in the spot light (ah ah !)
cosSpotAngle = max(-dot(fragLightDirLen.xyz, getLightDirection(light)), 0.0);
if (cosSpotAngle < getLightSpotAngleCos(light)) {
return false;
}
return true;
} }
<@endif@>
<@endfunc@>
<@endif@> <@endif@>

View file

@ -15,6 +15,7 @@
<@include LightingModel.slh@> <@include LightingModel.slh@>
<$declareLightBuffer()$> <$declareLightBuffer()$>
<$declareLightAmbientBuffer()$>
<@include LightAmbient.slh@> <@include LightAmbient.slh@>
<@include LightDirectional.slh@> <@include LightDirectional.slh@>
@ -29,7 +30,11 @@
// Get light // Get light
Light light = getLight(); Light light = getLight();
LightAmbient lightAmbient = getLightAmbient();
vec3 lightDirection = getLightDirection(light);
vec3 lightIrradiance = getLightIrradiance(light);
vec3 color = vec3(0.0); vec3 color = vec3(0.0);
<@endfunc@> <@endfunc@>
@ -38,7 +43,7 @@
<@func declareEvalAmbientGlobalColor()@> <@func declareEvalAmbientGlobalColor()@>
vec3 evalAmbientGlobalColor(mat4 invViewMat, float shadowAttenuation, float obscurance, vec3 position, vec3 normal, vec3 albedo, vec3 fresnel, float metallic, float roughness) { vec3 evalAmbientGlobalColor(mat4 invViewMat, float shadowAttenuation, float obscurance, vec3 position, vec3 normal, vec3 albedo, vec3 fresnel, float metallic, float roughness) {
<$prepareGlobalLight()$> <$prepareGlobalLight()$>
color += albedo * getLightColor(light) * obscurance * getLightAmbientIntensity(light); color += albedo * getLightColor(light) * obscurance * getLightAmbientIntensity(lightAmbient);
return color; return color;
} }
<@endfunc@> <@endfunc@>
@ -63,7 +68,7 @@ vec3 albedo, vec3 fresnel, float metallic, float roughness
// Ambient // Ambient
vec3 ambientDiffuse; vec3 ambientDiffuse;
vec3 ambientSpecular; vec3 ambientSpecular;
evalLightingAmbient(ambientDiffuse, ambientSpecular, light, fragEyeDir, fragNormal, roughness, metallic, fresnel, albedo, obscurance evalLightingAmbient(ambientDiffuse, ambientSpecular, lightAmbient, fragEyeDir, fragNormal, roughness, metallic, fresnel, albedo, obscurance
<@if supportScattering@> <@if supportScattering@>
,scattering, midNormalCurvature, lowNormalCurvature ,scattering, midNormalCurvature, lowNormalCurvature
<@endif@> ); <@endif@> );
@ -74,7 +79,7 @@ vec3 albedo, vec3 fresnel, float metallic, float roughness
// Directional // Directional
vec3 directionalDiffuse; vec3 directionalDiffuse;
vec3 directionalSpecular; vec3 directionalSpecular;
evalLightingDirectional(directionalDiffuse, directionalSpecular, light, fragEyeDir, fragNormal, roughness, metallic, fresnel, albedo, shadowAttenuation evalLightingDirectional(directionalDiffuse, directionalSpecular, lightDirection, lightIrradiance, fragEyeDir, fragNormal, roughness, metallic, fresnel, albedo, shadowAttenuation
<@if supportScattering@> <@if supportScattering@>
,scattering, midNormalCurvature, lowNormalCurvature ,scattering, midNormalCurvature, lowNormalCurvature
<@endif@> ); <@endif@> );
@ -107,7 +112,7 @@ vec3 evalSkyboxGlobalColor(mat4 invViewMat, float shadowAttenuation, float obscu
// Ambient // Ambient
vec3 ambientDiffuse; vec3 ambientDiffuse;
vec3 ambientSpecular; vec3 ambientSpecular;
evalLightingAmbient(ambientDiffuse, ambientSpecular, light, fragEyeDir, fragNormal, roughness, metallic, fresnel, albedo, obscurance evalLightingAmbient(ambientDiffuse, ambientSpecular, lightAmbient, fragEyeDir, fragNormal, roughness, metallic, fresnel, albedo, obscurance
<@if supportScattering@> <@if supportScattering@>
,scattering, midNormalCurvature, lowNormalCurvature ,scattering, midNormalCurvature, lowNormalCurvature
<@endif@> <@endif@>
@ -119,7 +124,7 @@ vec3 evalSkyboxGlobalColor(mat4 invViewMat, float shadowAttenuation, float obscu
// Directional // Directional
vec3 directionalDiffuse; vec3 directionalDiffuse;
vec3 directionalSpecular; vec3 directionalSpecular;
evalLightingDirectional(directionalDiffuse, directionalSpecular, light, fragEyeDir, fragNormal, roughness, metallic, fresnel, albedo, shadowAttenuation evalLightingDirectional(directionalDiffuse, directionalSpecular, lightDirection, lightIrradiance, fragEyeDir, fragNormal, roughness, metallic, fresnel, albedo, shadowAttenuation
<@if supportScattering@> <@if supportScattering@>
,scattering, midNormalCurvature, lowNormalCurvature ,scattering, midNormalCurvature, lowNormalCurvature
<@endif@> <@endif@>
@ -135,6 +140,7 @@ vec3 evalSkyboxGlobalColor(mat4 invViewMat, float shadowAttenuation, float obscu
<@func declareEvalLightmappedColor()@> <@func declareEvalLightmappedColor()@>
vec3 evalLightmappedColor(mat4 invViewMat, float shadowAttenuation, float obscurance, vec3 normal, vec3 albedo, vec3 lightmap) { vec3 evalLightmappedColor(mat4 invViewMat, float shadowAttenuation, float obscurance, vec3 normal, vec3 albedo, vec3 lightmap) {
Light light = getLight(); Light light = getLight();
LightAmbient ambient = getLightAmbient();
// Catch normals perpendicular to the projection plane, hence the magic number for the threshold // Catch normals perpendicular to the projection plane, hence the magic number for the threshold
// It should be just 0, but we have inaccuracy so we overshoot // It should be just 0, but we have inaccuracy so we overshoot
@ -150,7 +156,7 @@ vec3 evalLightmappedColor(mat4 invViewMat, float shadowAttenuation, float obscur
vec3 diffuseLight = lightAttenuation * lightmap; vec3 diffuseLight = lightAttenuation * lightmap;
// Ambient light is the lightmap when in shadow // Ambient light is the lightmap when in shadow
vec3 ambientLight = (1 - lightAttenuation) * lightmap * getLightAmbientIntensity(light); vec3 ambientLight = (1 - lightAttenuation) * lightmap * getLightAmbientIntensity(ambient);
return isLightmapEnabled() * obscurance * albedo * (diffuseLight + ambientLight); return isLightmapEnabled() * obscurance * albedo * (diffuseLight + ambientLight);
} }
@ -172,7 +178,7 @@ vec3 evalGlobalLightingAlphaBlended(mat4 invViewMat, float shadowAttenuation, fl
// Ambient // Ambient
vec3 ambientDiffuse; vec3 ambientDiffuse;
vec3 ambientSpecular; vec3 ambientSpecular;
evalLightingAmbient(ambientDiffuse, ambientSpecular, light, fragEyeDir, fragNormal, roughness, metallic, fresnel, albedo, obscurance); evalLightingAmbient(ambientDiffuse, ambientSpecular, lightAmbient, fragEyeDir, fragNormal, roughness, metallic, fresnel, albedo, obscurance);
color += ambientDiffuse; color += ambientDiffuse;
color += ambientSpecular / opacity; color += ambientSpecular / opacity;
@ -180,7 +186,7 @@ vec3 evalGlobalLightingAlphaBlended(mat4 invViewMat, float shadowAttenuation, fl
// Directional // Directional
vec3 directionalDiffuse; vec3 directionalDiffuse;
vec3 directionalSpecular; vec3 directionalSpecular;
evalLightingDirectional(directionalDiffuse, directionalSpecular, light, fragEyeDir, fragNormal, roughness, metallic, fresnel, albedo, shadowAttenuation); evalLightingDirectional(directionalDiffuse, directionalSpecular, lightDirection, lightIrradiance, fragEyeDir, fragNormal, roughness, metallic, fresnel, albedo, shadowAttenuation);
color += directionalDiffuse; color += directionalDiffuse;
color += directionalSpecular / opacity; color += directionalSpecular / opacity;

View file

@ -30,16 +30,16 @@ vec3 fresnelSchlickAmbient(vec3 fresnelColor, vec3 lightDir, vec3 halfDir, float
<$declareSkyboxMap()$> <$declareSkyboxMap()$>
<@endif@> <@endif@>
vec3 evalAmbientSpecularIrradiance(Light light, vec3 fragEyeDir, vec3 fragNormal, float roughness, vec3 fresnel) { vec3 evalAmbientSpecularIrradiance(LightAmbient ambient, vec3 fragEyeDir, vec3 fragNormal, float roughness, vec3 fresnel) {
vec3 direction = -reflect(fragEyeDir, fragNormal); vec3 direction = -reflect(fragEyeDir, fragNormal);
vec3 ambientFresnel = fresnelSchlickAmbient(fresnel, fragEyeDir, fragNormal, 1 - roughness); vec3 ambientFresnel = fresnelSchlickAmbient(fresnel, fragEyeDir, fragNormal, 1 - roughness);
vec3 specularLight; vec3 specularLight;
<@if supportIfAmbientMapElseAmbientSphere@> <@if supportIfAmbientMapElseAmbientSphere@>
if (getLightHasAmbientMap(light)) if (getLightHasAmbientMap(ambient))
<@endif@> <@endif@>
<@if supportAmbientMap@> <@if supportAmbientMap@>
{ {
float levels = getLightAmbientMapNumMips(light); float levels = getLightAmbientMapNumMips(ambient);
float lod = min(floor((roughness)* levels), levels); float lod = min(floor((roughness)* levels), levels);
specularLight = evalSkyboxLight(direction, lod).xyz; specularLight = evalSkyboxLight(direction, lod).xyz;
} }
@ -49,7 +49,7 @@ vec3 evalAmbientSpecularIrradiance(Light light, vec3 fragEyeDir, vec3 fragNormal
<@endif@> <@endif@>
<@if supportAmbientSphere@> <@if supportAmbientSphere@>
{ {
specularLight = evalSphericalLight(getLightAmbientSphere(light), direction).xyz; specularLight = sphericalHarmonics_evalSphericalLight(getLightAmbientSphere(ambient), direction).xyz;
} }
<@endif@> <@endif@>
@ -67,7 +67,7 @@ float curvatureAO(in float k) {
} }
<@endif@> <@endif@>
void evalLightingAmbient(out vec3 diffuse, out vec3 specular, Light light, vec3 eyeDir, vec3 normal, void evalLightingAmbient(out vec3 diffuse, out vec3 specular, LightAmbient ambient, vec3 eyeDir, vec3 normal,
float roughness, float metallic, vec3 fresnel, vec3 albedo, float obscurance float roughness, float metallic, vec3 fresnel, vec3 albedo, float obscurance
<@if supportScattering@> <@if supportScattering@>
, float scattering, vec4 midNormalCurvature, vec4 lowNormalCurvature , float scattering, vec4 midNormalCurvature, vec4 lowNormalCurvature
@ -76,10 +76,10 @@ void evalLightingAmbient(out vec3 diffuse, out vec3 specular, Light light, vec3
// Diffuse from ambient // Diffuse from ambient
diffuse = (1 - metallic) * evalSphericalLight(getLightAmbientSphere(light), normal).xyz; diffuse = (1 - metallic) * sphericalHarmonics_evalSphericalLight(getLightAmbientSphere(ambient), normal).xyz;
// Specular highlight from ambient // Specular highlight from ambient
specular = evalAmbientSpecularIrradiance(light, eyeDir, normal, roughness, fresnel) * obscurance * getLightAmbientIntensity(light); specular = evalAmbientSpecularIrradiance(ambient, eyeDir, normal, roughness, fresnel) * obscurance * getLightAmbientIntensity(ambient);
<@if supportScattering@> <@if supportScattering@>
@ -92,7 +92,7 @@ void evalLightingAmbient(out vec3 diffuse, out vec3 specular, Light light, vec3
if (scattering * isScatteringEnabled() > 0.0) { if (scattering * isScatteringEnabled() > 0.0) {
// Diffuse from ambient // Diffuse from ambient
diffuse = evalSphericalLight(getLightAmbientSphere(light), lowNormalCurvature.xyz).xyz; diffuse = sphericalHarmonics_evalSphericalLight(getLightAmbientSphere(ambient), lowNormalCurvature.xyz).xyz;
specular = vec3(0.0); specular = vec3(0.0);
} }
@ -102,7 +102,7 @@ void evalLightingAmbient(out vec3 diffuse, out vec3 specular, Light light, vec3
obscurance = 1.0; obscurance = 1.0;
} }
float lightEnergy = obscurance * getLightAmbientIntensity(light); float lightEnergy = obscurance * getLightAmbientIntensity(ambient);
if (isAlbedoEnabled() > 0.0) { if (isAlbedoEnabled() > 0.0) {
diffuse *= albedo; diffuse *= albedo;

View file

@ -11,7 +11,7 @@
<@func declareLightingDirectional(supportScattering)@> <@func declareLightingDirectional(supportScattering)@>
void evalLightingDirectional(out vec3 diffuse, out vec3 specular, Light light, void evalLightingDirectional(out vec3 diffuse, out vec3 specular, vec3 lightDir, vec3 lightIrradiance,
vec3 eyeDir, vec3 normal, float roughness, vec3 eyeDir, vec3 normal, float roughness,
float metallic, vec3 fresnel, vec3 albedo, float shadow float metallic, vec3 fresnel, vec3 albedo, float shadow
<@if supportScattering@> <@if supportScattering@>
@ -20,9 +20,9 @@ void evalLightingDirectional(out vec3 diffuse, out vec3 specular, Light light,
) { ) {
// Attenuation // Attenuation
vec3 lightEnergy = shadow * getLightColor(light) * getLightIntensity(light); vec3 lightEnergy = shadow * lightIrradiance;
evalFragShading(diffuse, specular, normal, -getLightDirection(light), eyeDir, metallic, fresnel, roughness, albedo evalFragShading(diffuse, specular, normal, -lightDir, eyeDir, metallic, fresnel, roughness, albedo
<@if supportScattering@> <@if supportScattering@>
,scattering, midNormalCurvature, lowNormalCurvature ,scattering, midNormalCurvature, lowNormalCurvature
<@endif@> <@endif@>

View file

@ -25,7 +25,7 @@ void evalLightingPoint(out vec3 diffuse, out vec3 specular, Light light,
// Eval attenuation // Eval attenuation
float radialAttenuation = evalLightAttenuation(light, fragLightDistance); float radialAttenuation = evalLightAttenuation(light, fragLightDistance);
vec3 lightEnergy = radialAttenuation * shadow * getLightColor(light) * getLightIntensity(light); vec3 lightEnergy = radialAttenuation * shadow * getLightIrradiance(light);
// Eval shading // Eval shading
evalFragShading(diffuse, specular, normal, fragLightDir, fragEyeDir, metallic, fresnel, roughness, albedo evalFragShading(diffuse, specular, normal, fragLightDir, fragEyeDir, metallic, fresnel, roughness, albedo
@ -39,7 +39,7 @@ void evalLightingPoint(out vec3 diffuse, out vec3 specular, Light light,
if (isShowLightContour() > 0.0) { if (isShowLightContour() > 0.0) {
// Show edge // Show edge
float edge = abs(2.0 * ((getLightCutoffRadius(light) - fragLightDistance) / (0.1)) - 1.0); float edge = abs(2.0 * ((lightVolume_getRadius(light.volume) - fragLightDistance) / (0.1)) - 1.0);
if (edge < 1) { if (edge < 1) {
float edgeCoord = exp2(-8.0*edge*edge); float edgeCoord = exp2(-8.0*edge*edge);
diffuse = vec3(edgeCoord * edgeCoord * getLightColor(light)); diffuse = vec3(edgeCoord * edgeCoord * getLightColor(light));

View file

@ -26,7 +26,7 @@ void evalLightingSpot(out vec3 diffuse, out vec3 specular, Light light,
// Eval attenuation // Eval attenuation
float radialAttenuation = evalLightAttenuation(light, fragLightDistance); float radialAttenuation = evalLightAttenuation(light, fragLightDistance);
float angularAttenuation = evalLightSpotAttenuation(light, cosSpotAngle); float angularAttenuation = evalLightSpotAttenuation(light, cosSpotAngle);
vec3 lightEnergy = angularAttenuation * radialAttenuation * shadow * getLightColor(light) * getLightIntensity(light); vec3 lightEnergy = angularAttenuation * radialAttenuation * shadow *getLightIrradiance(light);
// Eval shading // Eval shading
evalFragShading(diffuse, specular, normal, fragLightDir, fragEyeDir, metallic, fresnel, roughness, albedo evalFragShading(diffuse, specular, normal, fragLightDir, fragEyeDir, metallic, fresnel, roughness, albedo
@ -40,8 +40,8 @@ void evalLightingSpot(out vec3 diffuse, out vec3 specular, Light light,
if (isShowLightContour() > 0.0) { if (isShowLightContour() > 0.0) {
// Show edges // Show edges
float edgeDistR = (getLightCutoffRadius(light) - fragLightDistance); float edgeDistR = (lightVolume_getRadius(light.volume) - fragLightDistance);
float edgeDistS = dot(fragLightDistance * vec2(cosSpotAngle, sqrt(1.0 - cosSpotAngle * cosSpotAngle)), -getLightSpotOutsideNormal2(light)); float edgeDistS = dot(fragLightDistance * vec2(cosSpotAngle, sqrt(1.0 - cosSpotAngle * cosSpotAngle)), -lightVolume_getSpotOutsideNormal2(light.volume));
float edgeDist = min(edgeDistR, edgeDistS); float edgeDist = min(edgeDistR, edgeDistS);
float edge = abs(2.0 * (edgeDist / (0.1)) - 1.0); float edge = abs(2.0 * (edgeDist / (0.1)) - 1.0);
if (edge < 1) { if (edge < 1) {

View file

@ -34,7 +34,7 @@ void main(void) {
Light light = getLight(instanceID); Light light = getLight(instanceID);
vec4 sphereVertex = inPosition; vec4 sphereVertex = inPosition;
vec3 lightOrigin = getLightPosition(light); vec3 lightOrigin = getLightPosition(light);
vec4 sphereParam = getLightVolumeGeometry(light); vec4 sphereParam = vec4(1.0); // = getLightVolumeGeometry(light);
sphereVertex.xyz *= sphereParam.w; sphereVertex.xyz *= sphereParam.w;

View file

@ -31,7 +31,7 @@ void main(void) {
int instanceID = lightIndex[gl_InstanceID]; int instanceID = lightIndex[gl_InstanceID];
Light light = getLight(instanceID); Light light = getLight(instanceID);
vec3 lightPos = getLightPosition(light); vec3 lightPos = getLightPosition(light);
vec4 coneParam = getLightVolumeGeometry(light); vec4 coneParam = vec4(1.0); // = getLightVolumeGeometry(light);
if(coneVertex.z >= 0.0) { if(coneVertex.z >= 0.0) {
// Evaluate the true position of the spot volume // Evaluate the true position of the spot volume

View file

@ -94,7 +94,14 @@ void main(void) {
vec4 fragEyeVector = invViewMat * vec4(-frag.position.xyz, 0.0); vec4 fragEyeVector = invViewMat * vec4(-frag.position.xyz, 0.0);
vec3 fragEyeDir = normalize(fragEyeVector.xyz); vec3 fragEyeDir = normalize(fragEyeVector.xyz);
// int numLights = lightIndex[0];
/* int theLightIndex = clusterGrid_getClusterLightId(0, cluster);
if (theLightIndex == 65535) {
discard;
}
Light light = getLight(theLightIndex);
int numLights = lightIndex[0];*/
for (int i = 0; i < numLights; i++) { for (int i = 0; i < numLights; i++) {
// Need the light now // Need the light now
int theLightIndex = clusterGrid_getClusterLightId(i, cluster); int theLightIndex = clusterGrid_getClusterLightId(i, cluster);
@ -105,11 +112,11 @@ void main(void) {
vec4 fragLightDirLen; vec4 fragLightDirLen;
float cosSpotAngle; float cosSpotAngle;
if (isSpot) { if (isSpot) {
if (!clipFragToLightVolumeSpot(light, fragPos.xyz, fragLightVecLen2, fragLightDirLen, cosSpotAngle)) { if (!lightVolume_clipFragToLightVolumeSpot(light.volume, fragPos.xyz, fragLightVecLen2, fragLightDirLen, cosSpotAngle)) {
continue; continue;
} }
} else { } else {
if (!clipFragToLightVolumePoint(light, fragPos.xyz, fragLightVecLen2)) { if (!lightVolume_clipFragToLightVolumePoint(light.volume, fragPos.xyz, fragLightVecLen2)) {
continue; continue;
} }
} }

View file

@ -14,6 +14,7 @@
<@include model/Light.slh@> <@include model/Light.slh@>
<$declareLightBuffer()$> <$declareLightBuffer()$>
<$declareLightAmbientBuffer()$>
<@include LightingModel.slh@> <@include LightingModel.slh@>
@ -27,6 +28,11 @@ vec4 evalGlobalColor(float shadowAttenuation, vec3 position, vec3 normal, vec3 a
// Need the light now // Need the light now
Light light = getLight(); Light light = getLight();
vec3 lightDirection = getLightDirection(light);
vec3 lightIrradiance = getLightIrradiance(light);
LightAmbient ambient = getLightAmbient();
TransformCamera cam = getTransformCamera(); TransformCamera cam = getTransformCamera();
vec3 fragNormal; vec3 fragNormal;
<$transformEyeToWorldDir(cam, normal, fragNormal)$> <$transformEyeToWorldDir(cam, normal, fragNormal)$>
@ -34,12 +40,12 @@ vec4 evalGlobalColor(float shadowAttenuation, vec3 position, vec3 normal, vec3 a
vec3 fragEyeDir; vec3 fragEyeDir;
<$transformEyeToWorldDir(cam, fragEyeVectorView, fragEyeDir)$> <$transformEyeToWorldDir(cam, fragEyeVectorView, fragEyeDir)$>
vec3 color = opacity * albedo * getLightColor(light) * getLightAmbientIntensity(light); vec3 color = opacity * albedo * getLightColor(light) * getLightAmbientIntensity(ambient);
// Directional // Directional
vec3 directionalDiffuse; vec3 directionalDiffuse;
vec3 directionalSpecular; vec3 directionalSpecular;
evalLightingDirectional(directionalDiffuse, directionalSpecular, light, fragEyeDir, fragNormal, roughness, metallic, fresnel, albedo, shadowAttenuation); evalLightingDirectional(directionalDiffuse, directionalSpecular, lightDirection, lightIrradiance, fragEyeDir, fragNormal, roughness, metallic, fresnel, albedo, shadowAttenuation);
color += directionalDiffuse * isDiffuseEnabled() * isDirectionalEnabled(); color += directionalDiffuse * isDiffuseEnabled() * isDirectionalEnabled();
color += directionalSpecular * isSpecularEnabled() * isDirectionalEnabled(); color += directionalSpecular * isSpecularEnabled() * isDirectionalEnabled();

View file

@ -14,6 +14,7 @@
<@include model/Light.slh@> <@include model/Light.slh@>
<$declareLightBuffer()$> <$declareLightBuffer()$>
<$declareLightAmbientBuffer()$>
<@include LightingModel.slh@> <@include LightingModel.slh@>
@ -27,6 +28,11 @@ vec4 evalGlobalColor(float shadowAttenuation, vec3 position, vec3 normal, vec3 a
// Need the light now // Need the light now
Light light = getLight(); Light light = getLight();
vec3 lightDirection = getLightDirection(light);
vec3 lightIrradiance = getLightIrradiance(light);
LightAmbient ambient = getLightAmbient();
TransformCamera cam = getTransformCamera(); TransformCamera cam = getTransformCamera();
vec3 fragNormal; vec3 fragNormal;
<$transformEyeToWorldDir(cam, normal, fragNormal)$> <$transformEyeToWorldDir(cam, normal, fragNormal)$>
@ -34,12 +40,12 @@ vec4 evalGlobalColor(float shadowAttenuation, vec3 position, vec3 normal, vec3 a
vec3 fragEyeDir; vec3 fragEyeDir;
<$transformEyeToWorldDir(cam, fragEyeVectorView, fragEyeDir)$> <$transformEyeToWorldDir(cam, fragEyeVectorView, fragEyeDir)$>
vec3 color = opacity * albedo * getLightColor(light) * getLightAmbientIntensity(light); vec3 color = opacity * albedo * getLightColor(light) * getLightAmbientIntensity(ambient);
// Directional // Directional
vec3 directionalDiffuse; vec3 directionalDiffuse;
vec3 directionalSpecular; vec3 directionalSpecular;
evalLightingDirectional(directionalDiffuse, directionalSpecular, light, fragEyeDir, fragNormal, roughness, metallic, fresnel, albedo, shadowAttenuation); evalLightingDirectional(directionalDiffuse, directionalSpecular, lightDirection, lightIrradiance, fragEyeDir, fragNormal, roughness, metallic, fresnel, albedo, shadowAttenuation);
color += directionalDiffuse; color += directionalDiffuse;
color += directionalSpecular / opacity; color += directionalSpecular / opacity;

View file

@ -58,7 +58,7 @@ void main(void) {
// Clip againgst the light volume and Make the Light vector going from fragment to light center in world space // Clip againgst the light volume and Make the Light vector going from fragment to light center in world space
vec4 fragLightVecLen2; vec4 fragLightVecLen2;
if (!clipFragToLightVolumePoint(light, fragPos.xyz, fragLightVecLen2)) { if (!lightVolume_clipFragToLightVolumePoint(light.volume, fragPos.xyz, fragLightVecLen2)) {
discard; discard;
} }