From 9fcace0e73d256aa3607b732fb9fe421d9e32822 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Fri, 5 Jan 2007 15:24:06 +0000 Subject: [PATCH] Added GraphicsContext support for clearing the graphis context. The clearing is off by default (ClearMask is 0), but can be switched on when ClearMask is non zero. GraphicsContext::clear() is called after each swap buffers --- examples/osghangglide/osghangglide.cpp | 5 +++++ include/osg/GraphicsContext | 28 +++++++++++++++++++++++--- src/osg/GraphicsContext.cpp | 17 +++++++++++++++- src/osg/GraphicsThread.cpp | 1 + 4 files changed, 47 insertions(+), 4 deletions(-) diff --git a/examples/osghangglide/osghangglide.cpp b/examples/osghangglide/osghangglide.cpp index e10f5c1ae..d557b9cd4 100644 --- a/examples/osghangglide/osghangglide.cpp +++ b/examples/osghangglide/osghangglide.cpp @@ -161,6 +161,11 @@ int main( int argc, char **argv ) osg::notify(osg::NOTICE)<<" GraphicsWindow has been created successfully."<getEventQueue()->getCurrentEventState()->setWindowRectangle(0, 0, width, height ); + + // need to ensure that the window is cleared make sure that the complete window is set the correct colour + // rather than just the parts of the window that are under the camera's viewports + gw->setClearColor(osg::Vec4f(0.2f,0.2f,0.6f,1.0f)); + gw->setClearMask(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); } else { diff --git a/include/osg/GraphicsContext b/include/osg/GraphicsContext index 8d5525649..2f4e3cd59 100644 --- a/include/osg/GraphicsContext +++ b/include/osg/GraphicsContext @@ -169,6 +169,9 @@ class OSG_EXPORT GraphicsContext : public Referenced /** Get the traits of the GraphicsContext.*/ inline const Traits* getTraits() const { return _traits.get(); } + /** Return whether a valid and usable GraphicsContext has been created.*/ + virtual bool valid() const { return false; } + /** Set the State object which tracks the current OpenGL state for this graphics context.*/ inline void setState(State* state) { _state = state; } @@ -178,10 +181,25 @@ class OSG_EXPORT GraphicsContext : public Referenced /** Get the const State object which tracks the current OpenGL state for this graphics context.*/ inline const State* getState() const { return _state.get(); } - - /** Return whether a valid and usable GraphicsContext has been created.*/ - virtual bool valid() const { return false; } + + /** Sets the clear color. */ + inline void setClearColor(const Vec4& color) { _clearColor = color; } + + /** Returns the clear color. */ + inline const Vec4& getClearColor() const { return _clearColor; } + + /** Set the clear mask used in glClear(..). + * Defaults to GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT. */ + inline void setClearMask(GLbitfield mask) { _clearMask = mask; } + + /** Get the clear mask.*/ + inline GLbitfield getClearMask() const { return _clearMask; } + + /** Do an OpenGL clear of the full graphics context/window. + * Note, must only be called from a thread with this context current.*/ + virtual void clear(); + /** Realise the GraphicsContext.*/ bool realize(); @@ -288,6 +306,10 @@ class OSG_EXPORT GraphicsContext : public Referenced ref_ptr _traits; ref_ptr _state; + + Vec4 _clearColor; + GLbitfield _clearMask; + OpenThreads::Mutex _mutex; OpenThreads::Thread* _threadOfLastMakeCurrent; diff --git a/src/osg/GraphicsContext.cpp b/src/osg/GraphicsContext.cpp index e1207a099..70e585914 100644 --- a/src/osg/GraphicsContext.cpp +++ b/src/osg/GraphicsContext.cpp @@ -126,6 +126,8 @@ void GraphicsContext::decrementContextIDUsageCount(unsigned int contextID) GraphicsContext::GraphicsContext(): + _clearColor(osg::Vec4(0.0f,0.0f,0.0f,1.0f)), + _clearMask(0), _threadOfLastMakeCurrent(0) { setThreadSafeRefUnref(true); @@ -137,7 +139,18 @@ GraphicsContext::~GraphicsContext() close(false); } -/** Realise the GraphicsContext.*/ +void GraphicsContext::clear() +{ + if (_clearMask==0 || !_traits) return; + + glViewport(0, 0, _traits->width, _traits->height); + glScissor(0, 0, _traits->width, _traits->height); + + glClearColor( _clearColor[0], _clearColor[1], _clearColor[2], _clearColor[3]); + + glClear( _clearMask ); +} + bool GraphicsContext::realize() { if (realizeImplementation()) @@ -212,6 +225,7 @@ void GraphicsContext::swapBuffers() if (isCurrent()) { swapBuffersImplementation(); + clear(); } else if (_graphicsThread.valid() && _threadOfLastMakeCurrent == _graphicsThread.get()) @@ -222,6 +236,7 @@ void GraphicsContext::swapBuffers() { makeCurrent(); swapBuffersImplementation(); + clear(); releaseContext(); } } diff --git a/src/osg/GraphicsThread.cpp b/src/osg/GraphicsThread.cpp index c65972a58..7632f66ef 100644 --- a/src/osg/GraphicsThread.cpp +++ b/src/osg/GraphicsThread.cpp @@ -379,6 +379,7 @@ void SwapBuffersOperation::operator () (GraphicsContext* context) if (context) { context->swapBuffersImplementation(); + context->clear(); } }