Sign in to follow this  

Sphere projection

This topic is 4339 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

Hello there, I'm coding a deferred shading algorythm, for thoses of you not familliar with the subject its simply a way to lighten a scene in image space. Now my problem is not directly related to deferred shading but rater to finding the dimensions of screen aligned quad that would perfectly fit the projection of a sphere in 3D. The sphere is centered on a light position in 3D and its radius is equal to the range of the attenuation of the light. What I tried is to transform the position of the light in view space then, in the same space, create a screen aligned quad using the apropriate radius. But after projection, the quad is not large enough (this works only in ortho projection). It would be easier to understand with a picture, but I dont know how to add one here...

Share this post


Link to post
Share on other sites
I found a solution...

That was what I did first :

ocvec3 & viewPosition = ovec3 ().transformCoord (lightPosition, matView);
ovec3 corners[4];
corners[0].transformCoord (viewPosition + ocvec3 (lightFalloff, lightFalloff, 0), matProjection);
corners[1].transformCoord (viewPosition + ocvec3 (-lightFalloff, lightFalloff, 0), matProjection);
corners[2].transformCoord (viewPosition + ocvec3 (lightFalloff, -lightFalloff, 0), matProjection);
corners[3].transformCoord (viewPosition + ocvec3 (-lightFalloff, -lightFalloff, 0), matProjection);
for (odword i = 0; i < 4; ++i)
{
if (corners[i].x < tlCorner.x) tlCorner.x = corners[i].x;
if (corners[i].x > brCorner.x) brCorner.x = corners[i].x;
if (corners[i].y > tlCorner.y) tlCorner.y = corners[i].y;
if (corners[i].y < brCorner.y) brCorner.y = corners[i].y;
}

This is what I do now :

ocvec3 & viewPosition = ovec3 ().transformCoord (lightPosition, matView);
ovec3 corners[8];
corners[0].transformCoord (viewPosition + ocvec3 (lightFalloff, lightFalloff, lightFalloff), matProjection);
corners[1].transformCoord (viewPosition + ocvec3 (-lightFalloff, lightFalloff, lightFalloff), matProjection);
corners[2].transformCoord (viewPosition + ocvec3 (lightFalloff, -lightFalloff, lightFalloff), matProjection);
corners[3].transformCoord (viewPosition + ocvec3 (-lightFalloff, -lightFalloff, lightFalloff), matProjection);
corners[4].transformCoord (viewPosition + ocvec3 (lightFalloff, lightFalloff, -lightFalloff), matProjection);
corners[5].transformCoord (viewPosition + ocvec3 (-lightFalloff, lightFalloff, -lightFalloff), matProjection);
corners[6].transformCoord (viewPosition + ocvec3 (lightFalloff, -lightFalloff, -lightFalloff), matProjection);
corners[7].transformCoord (viewPosition + ocvec3 (-lightFalloff, -lightFalloff, -lightFalloff), matProjection);
for (odword i = 0; i < 4; ++i)
{
ocvec2 & center = (corners[i] + corners[i + 4]) * 0.5f;
if (center.x < tlCorner.x) tlCorner.x = center.x;
if (center.x > brCorner.x) brCorner.x = center.x;
if (center.y > tlCorner.y) tlCorner.y = center.y;
if (center.y < brCorner.y) brCorner.y = center.y;
}

A make a full box around the sphere in view space, project it, and find the "center" between the "near" and "far" plane of that box in projected space... It seems to work.. but is there a better way to do that ?

Share this post


Link to post
Share on other sites

This topic is 4339 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this