From 97ff495a9ca29f78a195c22892d97cabeb1957f6 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Wed, 27 Jun 2007 10:37:30 +0000 Subject: [PATCH] From Rajce Nickolov, improvements to PixelBufferWin32 and GraphicsWindowWin32 --- src/osgViewer/GraphicsWindowWin32.cpp | 30 ++++++++++- src/osgViewer/PixelBufferWin32.cpp | 78 +++++++++++++++------------ 2 files changed, 72 insertions(+), 36 deletions(-) diff --git a/src/osgViewer/GraphicsWindowWin32.cpp b/src/osgViewer/GraphicsWindowWin32.cpp index fd10a8e67..10e0bc145 100644 --- a/src/osgViewer/GraphicsWindowWin32.cpp +++ b/src/osgViewer/GraphicsWindowWin32.cpp @@ -1535,13 +1535,41 @@ bool GraphicsWindowWin32::realizeImplementation() { init(); if (!_initialized) return false; + } + { if (_traits.valid() && _traits->sharedContext) { GraphicsWindowWin32* sharedContextWin32 = dynamic_cast(_traits->sharedContext); if (sharedContextWin32) { - if (!makeCurrent()) return false; + struct RestoreContext + { + RestoreContext() + { + _hdc = wglGetCurrentDC(); + _hglrc = wglGetCurrentContext(); + } + ~RestoreContext() + { + if (_hdc) + { + wglMakeCurrent(_hdc,_hglrc); + } + } + protected: + HDC _hdc; + HGLRC _hglrc; + } restoreContext; + + _realized = true; + bool result = makeCurrent(); + _realized = false; + + if (!result) + { + return false; + } if (!wglShareLists(sharedContextWin32->getWGLContext(), getWGLContext())) { reportErrorForScreen("GraphicsWindowWin32::realizeImplementation() - Unable to share OpenGL context", _traits->screenNum, ::GetLastError()); diff --git a/src/osgViewer/PixelBufferWin32.cpp b/src/osgViewer/PixelBufferWin32.cpp index 11a934e50..7e9bca59b 100644 --- a/src/osgViewer/PixelBufferWin32.cpp +++ b/src/osgViewer/PixelBufferWin32.cpp @@ -16,12 +16,12 @@ #include #include +#include #include #include #include #include -#include #ifndef WGL_ARB_pbuffer #define WGL_ARB_pbuffer 1 @@ -144,7 +144,6 @@ DECLARE_HANDLE(HPBUFFERARB); #define WGL_TEXTURE_RECTANGLE_NV 0x20A2 #endif - #ifndef WGL_SAMPLE_BUFFERS_ARB #define WGL_SAMPLE_BUFFERS_ARB 0x2041 #endif @@ -439,7 +438,6 @@ PixelBufferWin32::PixelBufferWin32( osg::GraphicsContext::Traits* traits ): setState( new osg::State ); getState()->setGraphicsContext(this); - if (_traits.valid() && _traits->sharedContext && !_traits->target) { getState()->setContextID( _traits->sharedContext->getState()->getContextID() ); @@ -481,6 +479,25 @@ void PixelBufferWin32::init() if (!_traits) return; if (!_traits->pbuffer) return; + struct RestoreContext + { + RestoreContext() + { + _hdc = wglGetCurrentDC(); + _hglrc = wglGetCurrentContext(); + } + ~RestoreContext() + { + if (_hdc) + { + wglMakeCurrent(_hdc,_hglrc); + } + } + protected: + HDC _hdc; + HGLRC _hglrc; + } restoreContext; + WGLExtensions* wgle = WGLExtensions::instance(); if (!wgle || !wgle->isValid()) @@ -540,23 +557,37 @@ void PixelBufferWin32::init() if (_traits->target != 0) { - // TODO: Cube Maps + // TODO: Cube Maps if (_traits->target == GL_TEXTURE_RECTANGLE) { - bAttribList.push_back(WGL_TEXTURE_TARGET_ARB); - bAttribList.push_back(WGL_TEXTURE_RECTANGLE_NV); + bAttribList.push_back(WGL_TEXTURE_TARGET_ARB); + bAttribList.push_back(WGL_TEXTURE_RECTANGLE_NV); + + if (_traits->alpha) + fAttribList.push_back(WGL_BIND_TO_TEXTURE_RECTANGLE_RGBA_NV); + else + fAttribList.push_back(WGL_BIND_TO_TEXTURE_RECTANGLE_RGB_NV); + fAttribList.push_back(true); + } else { - bAttribList.push_back(WGL_TEXTURE_TARGET_ARB); - bAttribList.push_back(WGL_TEXTURE_2D_ARB); + bAttribList.push_back(WGL_TEXTURE_TARGET_ARB); + bAttribList.push_back(WGL_TEXTURE_2D_ARB); + + if (_traits->alpha) + fAttribList.push_back(WGL_BIND_TO_TEXTURE_RGBA_ARB); + else + fAttribList.push_back(WGL_BIND_TO_TEXTURE_RGB_ARB); + fAttribList.push_back(true); + } - fAttribList.push_back(WGL_BIND_TO_TEXTURE_RGBA_ARB); - fAttribList.push_back(true); - bAttribList.push_back(WGL_TEXTURE_FORMAT_ARB); - bAttribList.push_back(WGL_TEXTURE_RGBA_ARB); + if (_traits->alpha) + bAttribList.push_back(WGL_TEXTURE_RGBA_ARB); + else + bAttribList.push_back(WGL_TEXTURE_RGB_ARB); if (_traits->mipMapGeneration) { @@ -573,9 +604,6 @@ void PixelBufferWin32::init() int format; osg::ref_ptr tempWin; - HDC ohdc = 0; - HGLRC ohglrc = 0; - if (_traits->sharedContext && !_traits->target) { GraphicsWindowWin32* graphicsWindowWin32 = dynamic_cast(_traits->sharedContext); @@ -598,9 +626,6 @@ void PixelBufferWin32::init() } else { - ohdc = wglGetCurrentDC(); - ohglrc = wglGetCurrentContext(); - tempWin = new TemporaryWindow; hdc = tempWin->getDC(); tempWin->makeCurrent(); @@ -621,10 +646,6 @@ void PixelBufferWin32::init() { //doInternalError("wglCreatePbufferARB() failed"); osg::notify(osg::NOTICE) << "PixelBufferWin32::init(), Error: wglCreatePbufferARB failed" << std::endl; - if (ohdc) - { - wglMakeCurrent(ohdc,ohglrc); - } return ; } @@ -633,10 +654,6 @@ void PixelBufferWin32::init() { //doInternalError("wglGetPbufferDCARB() failed"); osg::notify(osg::NOTICE) << "PixelBufferWin32::init(), Error: wglGetPbufferDCARB failed" << std::endl; - if (ohdc) - { - wglMakeCurrent(ohdc,ohglrc); - } return; } if (_traits->sharedContext && !_traits->target) @@ -650,10 +667,6 @@ void PixelBufferWin32::init() { //doInternalError("wglCreateContext() failed"); osg::notify(osg::NOTICE) << "PixelBufferWin32::init(), Error: wglCreateContext failed" << std::endl; - if (ohdc) - { - wglMakeCurrent(ohdc,ohglrc); - } return; } } @@ -666,11 +679,6 @@ void PixelBufferWin32::init() _initialized = true; _valid = true; - if (ohdc) - { - wglMakeCurrent(ohdc,ohglrc); - } - return; }