Graphics Reference
In-Depth Information
// Clamp
the
rectangle to
the
screen extents
rectTopCS
= Clamp(
rectTopCS,
-1.0f, 1.0f );
rectBottomCS
= Clamp(
rectBottomCS,
-l.0f, l.0f );
rectLeftCS
= Clamp(
rectLeftCS,
-l.0f, l.ef );
rectRightCS
=
Clamp(
rectRightCS,
-l.0f, 1.0f );
// Now we convert to screen coordinates by applying the
// viewport transform
float
rectTopSS
=
rectTopCS
* 0.5f + 0.5f;
float
rectBottomSS
=
rectBottomCS
* 0.5f + 0.5f;
float
rectLeftSS
=
rectLeftCS
* 0.5f + 0.5f;
float
rectRightSS
=
rectRightCS
* 0.5f + 0.5f;
rectTopSS
= 1.0f -
rectTopSS;
rectBottomSS
= l.0f -
rectBottomSS;
rectTopSS
*=
m_uVPHeight;
rectBottomSS
*=
m_uVPHeight;
rectLeftSS
*=
m_uVPWidth;
rectRightSS
*=
m_uVPWidth;
// Final step is to convert to integers and fill out the
// D3D11_RECT structure
D3D11_RECT
rect;
rect.left
=
static_cast<LONG>(
rectLeftSS
);
rect.
right
=
static_cast<LON6>(
rectRightSS
);
rect.top
=
static_cast<LONG>(
rectTopSS
);
rect.bottom
=
static_cast<LONG>(
rectBottomSS
);
// Clamp to the viewport size
rect.left
= max(
rect.left,
0 );
rect.
top
=
max(
rect.top,
0 );
rect.right
= min(
rect.
right,
static_cast<LONG>(
m_uVPWidth
) );
rect.bottom
=
min(
rect.bottom,
static_cast<LONG>(
m_uVPHeight
) );
return
rect;
}
Listing 11.14. C++ code for fitting scissor rectangle to a point light.
For a spot light, we can use the same process of fitting a bounding sphere and de-
termining the screen-space extents. The code in Listing 11.14 will work for a spot light
without any modifications, since the resulting bounding sphere will completely encompass
the conical area of effect, regardless of the light's orientation and angular attenuation pa-
rameters. If necessary, the angular attenuation factors can be used to fit a bounding cone to
the light, to which a tighter bounding sphere can be fit.
An alternative to this approach is to approximate the light's bounding cone using a
mesh of vertices, and then calculate the bounding box, based on the vertices' projected
positions in screen space. This requires more calculations at runtime, but it can potentially
result in a tighter fit than using a bounding sphere.
Search WWH ::
Custom Search