Sphere projection

Started by
0 comments, last by Shnoutz 18 years, 2 months ago
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...
Advertisement
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.x < tlCorner.x) tlCorner.x = corners.x;
if (corners.x > brCorner.x) brCorner.x = corners.x;
if (corners.y > tlCorner.y) tlCorner.y = corners.y;
if (corners.y < brCorner.y) brCorner.y = corners.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 + corners) * 0.5f;<br> if (center.x &lt; tlCorner.x) tlCorner.x = center.x;<br> if (center.x &gt; brCorner.x) brCorner.x = center.x;<br> if (center.y &gt; tlCorner.y) tlCorner.y = center.y;<br> if (center.y &lt; brCorner.y) brCorner.y = center.y;<br>}<br><br>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 ?

This topic is closed to new replies.

Advertisement