Jump to content
  • Advertisement
Sign in to follow this  
Mihumihu

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

This topic is 797 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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);
	}
Edited by JoeJ

Share this post


Link to post
Share on other sites
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). Edited by JoeJ

Share this post


Link to post
Share on other sites

>> 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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!