diff --git a/src/Camera.cpp b/src/Camera.cpp index f729afb1f..309e29854 100644 --- a/src/Camera.cpp +++ b/src/Camera.cpp @@ -20,6 +20,21 @@ using namespace std; namespace Magnum { +GLbitfield Camera::clearMask = GL_COLOR_BUFFER_BIT; + +void Camera::setFeature(Feature feature, bool enabled) { + /* Enable or disable the feature */ + enabled ? glEnable(static_cast(feature)) : glDisable(static_cast(feature)); + + /* Update clear mask, if needed */ + GLbitfield clearMaskChange; + if(feature == Feature::DepthTest) clearMaskChange = GL_DEPTH_BUFFER_BIT; + else if(feature == Feature::StencilTest) clearMaskChange = GL_STENCIL_BUFFER_BIT; + else return; + + enabled ? clearMask |= clearMaskChange : clearMask &= ~clearMaskChange; +} + Camera::Camera(Object* parent): Object(parent), _aspectRatioPolicy(AspectRatioPolicy::Extend) {} void Camera::setOrthographic(GLfloat size, GLfloat near, GLfloat far) { @@ -109,8 +124,7 @@ void Camera::draw() { Scene* s = scene(); CORRADE_ASSERT(s, "Camera: cannot draw without camera attached to scene", ) - /** @todo Clear only set features */ - glClear(GL_COLOR_BUFFER_BIT|GL_STENCIL_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); + clear(); /* Recursively draw child objects */ drawChildren(s, cameraMatrix()); diff --git a/src/Camera.h b/src/Camera.h index 4ba36abf5..da76f008d 100644 --- a/src/Camera.h +++ b/src/Camera.h @@ -67,9 +67,7 @@ class MAGNUM_EXPORT Camera: public Object { }; /** @brief Set feature */ - inline static void setFeature(Feature feature, bool enabled) { - enabled ? glEnable(static_cast(feature)) : glDisable(static_cast(feature)); - } + static void setFeature(Feature feature, bool enabled); /** * @brief Constructor @@ -151,14 +149,24 @@ class MAGNUM_EXPORT Camera: public Object { /** * @brief Draw the scene * - * Clears the color, depth and stencil buffer and draws the scene - * using drawChildren(). + * Calls clear() and draws the scene using drawChildren(). */ virtual void draw(); using Object::draw; /* Don't hide Object's draw() */ protected: + /** + * @brief Clear framebuffer + * + * Clears color buffer, depth and stencil buffer in currently active + * framebuffer. If depth or stencil test is not enabled, it doesn't + * clear these buffers. + * + * @see setFeature() + */ + inline static void clear() { glClear(clearMask); } + /** * Recalculates camera matrix. */ @@ -172,6 +180,8 @@ class MAGNUM_EXPORT Camera: public Object { void drawChildren(Object* object, const Matrix4& transformationMatrix); private: + static GLbitfield clearMask; + Matrix4 rawProjectionMatrix; Matrix4 _projectionMatrix; Matrix4 _cameraMatrix;