Last active
February 9, 2018 15:40
-
-
Save tgfrerer/ea253bbfe5d4b89618a6902f9c6ec0ff to your computer and use it in GitHub Desktop.
simplified method to draw camera frustum in oF
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void ofCamera::drawFrustum(const ofRectangle & viewport) const { | |
ofPushMatrix(); | |
// In World Space, the camera frustum is the "capped pyramid" which | |
// contains everything which can be seen from a camera. | |
// | |
// In Clip Space, the frustum is defined as a box with dimensions: -1, -1, -1 to +1, +1, +1. | |
// | |
// Much simpler. | |
// | |
// We therefore want to draw our frustum as if in Clip Space. | |
// For this, we must find a matrix which will transform Clip Space | |
// back to World Space. | |
// Note: globalTransformMatrix == inverse(modelViewMatrix) | |
glm::mat4 clipSpaceToWorld = getGlobalTransformMatrix() * glm::inverse(getProjectionMatrix(viewport)); | |
// Move into Clip Space - this matrix will transform anything we | |
// draw from Clip Space to World Space | |
ofMultMatrix(clipSpaceToWorld); | |
// Anything we draw now is transformed from Clip Space back to World Space | |
ofPushStyle(); | |
ofNoFill(); | |
ofDrawBox(0, 0, 0, 2.f, 2.f, 2.f); // In Clip Space, the frustum is standardised to be a box of dimensions -1,1 in each axis | |
ofPopStyle(); | |
ofPopMatrix(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment