Improve quad's fit of pointlight extend

This commit is contained in:
Vincent Lejeune 2014-04-07 21:04:03 +02:00
parent 8aa5031fc0
commit 0b318b9c7e

View File

@ -16,23 +16,24 @@ const float zNear = 1.;
void main(void) void main(void)
{ {
// Beyond that value, light is too attenuated // Beyond that value, light is too attenuated
float r = 40 * Energy; float r = 50 * Energy;
center = Position; center = Position;
energy = Energy; energy = Energy;
vec4 Center = ViewMatrix * vec4(Position, 1.); vec4 Center = ViewMatrix * vec4(Position, 1.);
vec4 ProjectedCornerPosition = ProjectionMatrix * (Center + r * vec4(Corner, 0., 0.));
float adjustedDepth = ProjectedCornerPosition.z;
if (Center.z > zNear) // Light is in front of the cam if (Center.z > zNear) // Light is in front of the cam
{ {
vec3 UnitCenter = normalize(-Center.xyz); adjustedDepth = max(Center.z - r, zNear);
float clampedR = min(r, Center.z - 1.);
float cosTheta = dot(UnitCenter, vec3(0., 0., -1));
float d = clampedR / cosTheta;
Center.xyz += d * UnitCenter;
} }
else if (Center.z + r > zNear) // Light is behind the cam but in range else if (Center.z + r > zNear) // Light is behind the cam but in range
{ {
Center.z = zNear; adjustedDepth = zNear;
// TODO: Change r so that we make the screen aligned quad fits light range.
} }
ProjectedCornerPosition /= ProjectedCornerPosition.w;
ProjectedCornerPosition.zw = (ProjectionMatrix * vec4(0., 0., adjustedDepth, 1.)).zw;
ProjectedCornerPosition.xy *= ProjectedCornerPosition.w;
col = Color; col = Color;
gl_Position = ProjectionMatrix * (Center + r * vec4(Corner, 0., 0.)); gl_Position = ProjectedCornerPosition;
} }