How to calculate lighting by point light with size of the lightsource like Sun?

Started by
3 comments, last by Norman Barrows 7 years, 11 months ago
I calculate lightWeighting with that:
lightDirection = normalize(pointLightsPosition - vPosition.xyz);
normal = normalize(vTransformedNormal);
eyeDirection = normalize(-vPosition.xyz);
reflectionDirection = reflect(-lightDirection, normal);
specularLightWeighting = pow(max(dot(reflectionDirection, eyeDirection), 0.0), pointLightsParamsf[0]);
diffuseLightWeighting = max(dot(normal, lightDirection), 0.0);
lightWeighting = ambient + diffuse * diffuseLightWeighting + specular * specularLightWeighting;
It is point light source in pointLightsPosition, how to calculate lightWeighting if light source has a size like radius(Sun, lamp etc.)?
Thanks.
Advertisement

You split it into smaller point light sources? I like analytical solutions to integrals, but somehow that is not the direction the development has taken (quality / render time).

You can use the form factor of a disc pointing towards sample position:
qVec3 diff = ePos - rPos; //  emitter (disc) - receiving sample position
float dR = dot(rDir, diff); // rDir = receiver normal
if (dR > FP_EPSILON)
{
	float dE = -dot(eDir, diff); // eDir = disc normal
	if (dE > FP_EPSILON)
	{
		float sqD = dot(diff, diff);
		float formFactor = (dR * dE) / (sqD * (PI * sqD + discArea)) * discArea;

		qVec3 light = eLuminosity * eCol;
		sampleReceivedLight += formFactor * light;
	}
}
If that works for you, you could try to optimize for a sphere.
The code snippet below should help to understand the math.
It projects a sphere to the hemisphere and then to the normal plane of the sample (We want to know the area if the projected shape).
The disc is approximated then by a simple dot product (assuming orthonormal projection and ignoring perspective).
A disc intersecting the normal plane is also approximated only with a dot product -> some error when disc is large and close to sample.



inline void ProjectDisc (
		qVec3 &ray, float &hemiSphereProjectedAreaByPI,
		const qVec3 &relPos, const qVec3 &discDir, const qMat3 &local) // todo: remove matrix and give localRay instead
	{
		// Note: Disc direction is assumed to point towards the sample position!

		float sqDist = relPos.SqL() + FP_TINY;	
		ray = local.Unrotate(relPos);
		hemiSphereProjectedAreaByPI = -(ray[2] * discDir.Dot(relPos)) / 
			(sqDist * (PI * sqDist + discDir[3])) * discDir[3]; // discDir[3] = disc area

		ray /= sqrt(sqDist);

		//		unoptimized math:
		//
		//			float unitRad = radius / sqrt(radius*radius + dist*dist); // sin(solidAngle); // radius / Hypotenuse
		//			float unitArea = unitRad*unitRad*PI;
		//			planeArea = unitArea * ray[2];
		//			planeArea *= fabs (discDir.Dot(ray));
		//			planeAreaByPI = planeArea / PI;
		//
		//			float solidAngle = atan (radius * ooDist);
		//			RenderCone (qVec3(0,0,0), (qVec3&)ray, solidAngle, 0,0.5,1, 1,0,0);
		//			RenderCircle (sin(solidAngle), (qVec3&)(ray * cos(solidAngle)), (qVec3&)ray, 0,0.5,1);
	}
Just found another snippet for just a sphere:
float unitRad = rad / dist; // disk radius of sphere projected to hemisphere // == sin(solidAngle);
if (unitRad > 1.0f) unitRad = 1.0f;
float cosSolidAngle = sqrt(1.0f - unitRad*unitRad);
float solidAngle = asin (unitRad);
float unitArea = unitRad*unitRad*PI; // area on hemisphere projection
float planeArea = unitArea * ray[2]; // area on sample plane projection // ray[2] = sampleNormal.Dot(ray)
Those values are accurate as long as the sphere does not intersect the sample normal plane.
(For sun that case should go totally unnoticed)

Hope there's no bug (i've kept this in comments too because the math is confusing).

>> how to calculate lightWeighting if light source has a size like radius(Sun,

for something like a space flight sim, you'd probably want to use directional lights for nearby stars, with lots of ambient, and very little diffuse.

Norm Barrows

Rockland Software Productions

"Building PC games since 1989"

rocklandsoftware.net

PLAY CAVEMAN NOW!

http://rocklandsoftware.net/beta.php

This topic is closed to new replies.

Advertisement