delete extFBO;
}
+// =======================================================================
+// function : IsCurrent
+// purpose :
+// =======================================================================
+Standard_Boolean OpenGl_Context::IsCurrent() const
+{
+#if (defined(_WIN32) || defined(__WIN32__))
+ if (myWindowDC == NULL || myGContext == NULL)
+ {
+ return Standard_False;
+ }
+ return (( (HDC )myWindowDC == wglGetCurrentDC())
+ && ((HGLRC )myGContext == wglGetCurrentContext()));
+#else
+ if (myDisplay == NULL || myWindow == 0 || myGContext == 0)
+ {
+ return Standard_False;
+ }
+
+ return ( ((Display* )myDisplay == glXGetCurrentDisplay())
+ && ((GLXContext )myGContext == glXGetCurrentContext())
+ && ((GLXDrawable )myWindow == glXGetCurrentDrawable()));
+#endif
+}
+
// =======================================================================
// function : MakeCurrent
// purpose :
Standard_Boolean OpenGl_Context::MakeCurrent()
{
#if (defined(_WIN32) || defined(__WIN32__))
- if (myWindowDC == NULL || myGContext == NULL ||
- !wglMakeCurrent ((HDC )myWindowDC, (HGLRC )myGContext))
+ if (myWindowDC == NULL || myGContext == NULL)
{
- //GLenum anErrCode = glGetError();
- //const GLubyte* anErrorString = gluErrorString (anErrCode);
- //std::cerr << "wglMakeCurrent() failed: " << anErrCode << " " << anErrorString << "\n";
+ Standard_ProgramError_Raise_if (myIsInitialized, "OpenGl_Context::Init() should be called before!");
+ return Standard_False;
+ }
+
+ // technically it should be safe to activate already bound GL context
+ // however some drivers (Intel etc.) may FAIL doing this for unknown reason
+ if (IsCurrent())
+ {
+ return Standard_True;
+ }
+ else if (wglMakeCurrent ((HDC )myWindowDC, (HGLRC )myGContext) != TRUE)
+ {
+ // notice that glGetError() couldn't be used here!
+ wchar_t* aMsgBuff = NULL;
+ DWORD anErrorCode = GetLastError();
+ FormatMessageW (FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
+ NULL, anErrorCode, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (wchar_t* )&aMsgBuff, 0, NULL);
+ if (aMsgBuff != NULL)
+ {
+ std::wcerr << L"OpenGL interface: wglMakeCurrent() failed. " << aMsgBuff << L" (" << int(anErrorCode) << L")\n";
+ LocalFree (aMsgBuff);
+ }
+ else
+ {
+ std::wcerr << L"OpenGL interface: wglMakeCurrent() failed with #" << int(anErrorCode) << L" error code\n";
+ }
return Standard_False;
}
#else
- if (myDisplay == NULL || myWindow == 0 || myGContext == 0 ||
- !glXMakeCurrent ((Display* )myDisplay, (GLXDrawable )myWindow, (GLXContext )myGContext))
+ if (myDisplay == NULL || myWindow == 0 || myGContext == 0)
+ {
+ Standard_ProgramError_Raise_if (myIsInitialized, "OpenGl_Context::Init() should be called before!");
+ return Standard_False;
+ }
+
+ if (!glXMakeCurrent ((Display* )myDisplay, (GLXDrawable )myWindow, (GLXContext )myGContext))
{
// if there is no current context it might be impossible to use glGetError() correctly
//std::cerr << "glXMakeCurrent() failed!\n";
#else
myDisplay = theDisplay;
#endif
- if (myGContext == NULL)
+ if (myGContext == NULL || !MakeCurrent())
{
return Standard_False;
}
/************************************************************************/
#include <OpenGl_GraphicDriver.hxx>
+#include <OpenGl_Context.hxx>
+#include <OpenGl_CView.hxx>
#include <OSD_Localizer.hxx>
#ifdef HAVE_CONFIG_H
const Standard_Address /*theProgressObject*/)
{
#ifdef HAVE_GL2PS
+ // gl2psBeginPage() will call OpenGL functions
+ // so we should activate correct GL context before redraw scene call
+ const OpenGl_CView* aCView = (const OpenGl_CView* )theView.ptrView;
+ if (aCView == NULL || !aCView->WS->GetGlContext()->MakeCurrent())
+ {
+ return Standard_False;
+ }
+
Standard_Integer aFormat = -1;
Standard_Integer aSortType = Graphic3d_ST_BSP_Tree;
switch (theFormat)
myWindow = aParent;
#endif
+#if (defined(_WIN32) || defined(__WIN32__))
+ myGlContext->Init (myWindow, myWindowDC, myGContext);
+#else
+ myGlContext->Init (myWindow, myDisplay->GetDisplay(), myGContext);
+#endif
Init();
- myGlContext->Init();
}
// =======================================================================
// =======================================================================
Standard_Boolean OpenGl_Window::Activate()
{
- DISPLAY* aDisp = (DISPLAY* )myDisplay->GetDisplay();
- if (aDisp == NULL)
- return Standard_False;
-
-#if (defined(_WIN32) || defined(__WIN32__))
- if (!wglMakeCurrent (myWindowDC, myGContext))
- {
- //GLenum errorcode = glGetError();
- //const GLubyte *errorstring = gluErrorString(errorcode);
- //printf("wglMakeCurrent failed: %d %s\n", errorcode, errorstring);
- return Standard_False;
- }
-#else
- if (!glXMakeCurrent (aDisp, myWindow, myGContext))
- {
- // if there is no current context it might be impossible to use glGetError correctly
- //printf("glXMakeCurrent failed!\n");
- return Standard_False;
- }
-#endif
-
- return Standard_True;
+ return myGlContext->MakeCurrent();
}
// =======================================================================