Skip to content

Instantly share code, notes, and snippets.

@greggman
Created April 10, 2014 17:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save greggman/10405738 to your computer and use it in GitHub Desktop.
Save greggman/10405738 to your computer and use it in GitHub Desktop.
Set a frustum to a sub rect of a larger view
void SetFrustumToSubRectOfLargerView(
float fullWidth, // width of large 'virtual' monitor that other views will be sub rect of
float fullHeight, // height of large 'virtual' monitor that other views will be sub rect of
float subRectX, // left edge of sub rect for this view
float subRectY, // top edge of sub rect for this view
float subRectWidth, // width of sub rect for this view
float subRectHeight, // height of sub rect fo this view
float fovInRadians, // field of view in radians for fullwidth x fullheight view.
float zNear, // z near clipping plane
float zFar) // z far clipping plane
{
float aspect = fullWidth / fullHeight;
float top = tanf(fovInRadians * 0.5f) * zNear;
float bottom = -top;
float left = aspect * bottom;
float right = aspect * top;
float width = fabs(right - left);
float height = fabs(top - bottom);
gluFrustum(
left + subRectX * width / fullWidth,
left + (subRectX + subRectWidth) * width / fullWidth,
top - (subRectY + subRectHeight) * height / fullHeight,
top - subRectY * height / fullHeight,
zNear,
zFar
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment