1 // Created on: 2011-09-20
2 // Created by: Sergey ZERCHANINOV
3 // Copyright (c) 2011-2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 #include <OpenGl_GlCore12.hxx>
18 #include <InterfaceGraphic.hxx>
20 #include <OpenGl_Window.hxx>
22 #include <OpenGl_Context.hxx>
23 #include <OpenGl_Display.hxx>
25 #include <Aspect_GraphicDeviceDefinitionError.hxx>
26 #include <TCollection_AsciiString.hxx>
27 #include <TCollection_ExtendedString.hxx>
29 IMPLEMENT_STANDARD_HANDLE(OpenGl_Window,MMgt_TShared)
30 IMPLEMENT_STANDARD_RTTIEXT(OpenGl_Window,MMgt_TShared)
32 #if !defined(__APPLE__) || defined(MACOSX_USE_GLX)
36 static const TEL_COLOUR THE_DEFAULT_BG_COLOR = { { 0.F, 0.F, 0.F, 1.F } };
40 // WGL_ARB_pixel_format
41 #ifndef WGL_NUMBER_PIXEL_FORMATS_ARB
42 #define WGL_NUMBER_PIXEL_FORMATS_ARB 0x2000
43 #define WGL_DRAW_TO_WINDOW_ARB 0x2001
44 #define WGL_DRAW_TO_BITMAP_ARB 0x2002
45 #define WGL_ACCELERATION_ARB 0x2003
46 #define WGL_NEED_PALETTE_ARB 0x2004
47 #define WGL_NEED_SYSTEM_PALETTE_ARB 0x2005
48 #define WGL_SWAP_LAYER_BUFFERS_ARB 0x2006
49 #define WGL_SWAP_METHOD_ARB 0x2007
50 #define WGL_NUMBER_OVERLAYS_ARB 0x2008
51 #define WGL_NUMBER_UNDERLAYS_ARB 0x2009
52 #define WGL_TRANSPARENT_ARB 0x200A
53 #define WGL_TRANSPARENT_RED_VALUE_ARB 0x2037
54 #define WGL_TRANSPARENT_GREEN_VALUE_ARB 0x2038
55 #define WGL_TRANSPARENT_BLUE_VALUE_ARB 0x2039
56 #define WGL_TRANSPARENT_ALPHA_VALUE_ARB 0x203A
57 #define WGL_TRANSPARENT_INDEX_VALUE_ARB 0x203B
58 #define WGL_SHARE_DEPTH_ARB 0x200C
59 #define WGL_SHARE_STENCIL_ARB 0x200D
60 #define WGL_SHARE_ACCUM_ARB 0x200E
61 #define WGL_SUPPORT_GDI_ARB 0x200F
62 #define WGL_SUPPORT_OPENGL_ARB 0x2010
63 #define WGL_DOUBLE_BUFFER_ARB 0x2011
64 #define WGL_STEREO_ARB 0x2012
65 #define WGL_PIXEL_TYPE_ARB 0x2013
66 #define WGL_COLOR_BITS_ARB 0x2014
67 #define WGL_RED_BITS_ARB 0x2015
68 #define WGL_RED_SHIFT_ARB 0x2016
69 #define WGL_GREEN_BITS_ARB 0x2017
70 #define WGL_GREEN_SHIFT_ARB 0x2018
71 #define WGL_BLUE_BITS_ARB 0x2019
72 #define WGL_BLUE_SHIFT_ARB 0x201A
73 #define WGL_ALPHA_BITS_ARB 0x201B
74 #define WGL_ALPHA_SHIFT_ARB 0x201C
75 #define WGL_ACCUM_BITS_ARB 0x201D
76 #define WGL_ACCUM_RED_BITS_ARB 0x201E
77 #define WGL_ACCUM_GREEN_BITS_ARB 0x201F
78 #define WGL_ACCUM_BLUE_BITS_ARB 0x2020
79 #define WGL_ACCUM_ALPHA_BITS_ARB 0x2021
80 #define WGL_DEPTH_BITS_ARB 0x2022
81 #define WGL_STENCIL_BITS_ARB 0x2023
82 #define WGL_AUX_BUFFERS_ARB 0x2024
84 #define WGL_NO_ACCELERATION_ARB 0x2025
85 #define WGL_GENERIC_ACCELERATION_ARB 0x2026
86 #define WGL_FULL_ACCELERATION_ARB 0x2027
88 #define WGL_SWAP_EXCHANGE_ARB 0x2028
89 #define WGL_SWAP_COPY_ARB 0x2029
90 #define WGL_SWAP_UNDEFINED_ARB 0x202A
92 #define WGL_TYPE_RGBA_ARB 0x202B
93 #define WGL_TYPE_COLORINDEX_ARB 0x202C
94 #endif // WGL_NUMBER_PIXEL_FORMATS_ARB
96 // WGL_ARB_create_context_profile
97 #ifndef WGL_CONTEXT_MAJOR_VERSION_ARB
98 #define WGL_CONTEXT_MAJOR_VERSION_ARB 0x2091
99 #define WGL_CONTEXT_MINOR_VERSION_ARB 0x2092
100 #define WGL_CONTEXT_LAYER_PLANE_ARB 0x2093
101 #define WGL_CONTEXT_FLAGS_ARB 0x2094
102 #define WGL_CONTEXT_PROFILE_MASK_ARB 0x9126
104 // WGL_CONTEXT_FLAGS bits
105 #define WGL_CONTEXT_DEBUG_BIT_ARB 0x0001
106 #define WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB 0x0002
108 // WGL_CONTEXT_PROFILE_MASK_ARB bits
109 #define WGL_CONTEXT_CORE_PROFILE_BIT_ARB 0x00000001
110 #define WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB 0x00000002
111 #endif // WGL_CONTEXT_MAJOR_VERSION_ARB
113 static LRESULT CALLBACK wndProcDummy (HWND theWin, UINT theMsg, WPARAM theParamW, LPARAM theParamL)
115 return DefWindowProcW (theWin, theMsg, theParamW, theParamL);
118 static Bool WaitForNotify (Display* theDisp, XEvent* theEv, char* theArg)
120 return (theEv->type == MapNotify) && (theEv->xmap.window == (Window )theArg);
126 // =======================================================================
127 // function : OpenGl_Window
129 // =======================================================================
130 OpenGl_Window::OpenGl_Window (const Handle(OpenGl_Display)& theDisplay,
131 const CALL_DEF_WINDOW& theCWindow,
132 Aspect_RenderingContext theGContext,
133 const Handle(OpenGl_Caps)& theCaps,
134 const Handle(OpenGl_Context)& theShareCtx)
135 : myDisplay (theDisplay),
136 myGlContext (new OpenGl_Context (theCaps)),
137 myOwnGContext (theGContext == 0),
139 mySysPalInUse (FALSE),
141 myWidth ((Standard_Integer )theCWindow.dx),
142 myHeight ((Standard_Integer )theCWindow.dy),
143 myBgColor (THE_DEFAULT_BG_COLOR),
144 myDither (theDisplay->Dither()),
145 myBackDither (theDisplay->BackDither())
147 myBgColor.rgb[0] = theCWindow.Background.r;
148 myBgColor.rgb[1] = theCWindow.Background.g;
149 myBgColor.rgb[2] = theCWindow.Background.b;
152 HWND aWindow = (HWND )theCWindow.XWindow;
153 HDC aWindowDC = GetDC (aWindow);
154 HGLRC aGContext = (HGLRC )theGContext;
156 PIXELFORMATDESCRIPTOR aPixelFrmt;
157 memset (&aPixelFrmt, 0, sizeof(aPixelFrmt));
158 aPixelFrmt.nSize = sizeof(PIXELFORMATDESCRIPTOR);
159 aPixelFrmt.nVersion = 1;
160 aPixelFrmt.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
161 aPixelFrmt.iPixelType = PFD_TYPE_RGBA;
162 aPixelFrmt.cColorBits = 24;
163 aPixelFrmt.cDepthBits = 24;
164 aPixelFrmt.cStencilBits = 8;
165 aPixelFrmt.iLayerType = PFD_MAIN_PLANE;
166 if (theCaps->contextStereo)
168 aPixelFrmt.dwFlags |= PFD_STEREO;
171 int aPixelFrmtId = ChoosePixelFormat (aWindowDC, &aPixelFrmt);
173 // in case of failure try without stereo if any
174 if (aPixelFrmtId == 0 && theCaps->contextStereo)
176 TCollection_ExtendedString aMsg ("OpenGl_Window::CreateWindow: "
177 "ChoosePixelFormat is unable to find stereo supported pixel format. "
178 "Choosing similar non stereo format.");
179 myGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB,
180 GL_DEBUG_TYPE_OTHER_ARB,
181 0, GL_DEBUG_SEVERITY_HIGH_ARB, aMsg);
183 aPixelFrmt.dwFlags &= ~PFD_STEREO;
184 aPixelFrmtId = ChoosePixelFormat (aWindowDC, &aPixelFrmt);
187 if (aPixelFrmtId == 0)
189 ReleaseDC (aWindow, aWindowDC);
191 TCollection_AsciiString aMsg ("OpenGl_Window::CreateWindow: ChoosePixelFormat failed. Error code: ");
192 aMsg += (int )GetLastError();
193 Aspect_GraphicDeviceDefinitionError::Raise (aMsg.ToCString());
197 DescribePixelFormat (aWindowDC, aPixelFrmtId, sizeof(aPixelFrmt), &aPixelFrmt);
198 if (aPixelFrmt.dwFlags & PFD_NEED_PALETTE)
200 WINDOW_DATA* aWndData = (WINDOW_DATA* )GetWindowLongPtr (aWindow, GWLP_USERDATA);
202 mySysPalInUse = (aPixelFrmt.dwFlags & PFD_NEED_SYSTEM_PALETTE) ? TRUE : FALSE;
203 InterfaceGraphic_RealizePalette (aWindowDC, aWndData->hPal, FALSE, mySysPalInUse);
208 myDither = (aPixelFrmt.cColorBits <= 8);
213 myBackDither = (aPixelFrmt.cColorBits <= 8);
216 HGLRC aSlaveCtx = !theShareCtx.IsNull() ? (HGLRC )theShareCtx->myGContext : NULL;
217 if (aGContext == NULL)
219 // create temporary context to retrieve advanced context creation procedures
220 HMODULE aModule = GetModuleHandleW(NULL);
221 WNDCLASSW aClass; memset (&aClass, 0, sizeof(aClass));
222 aClass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
223 aClass.lpfnWndProc = wndProcDummy;
224 aClass.hInstance = aModule;
225 aClass.lpszClassName = L"OpenGl_WindowTmp";
227 HDC aDevCtxTmp = NULL;
228 HGLRC aRendCtxTmp = NULL;
229 if ((!theCaps->contextDebug && !theCaps->contextNoAccel)
230 || RegisterClassW (&aClass) == 0)
232 aClass.lpszClassName = NULL;
234 if (aClass.lpszClassName != NULL)
236 DWORD anExStyle = WS_EX_TOOLWINDOW | WS_EX_WINDOWEDGE;
237 #if (_WIN32_WINNT >= 0x0500)
238 anExStyle |= WS_EX_NOACTIVATE;
240 aWinTmp = CreateWindowExW(anExStyle,
241 aClass.lpszClassName, L"OpenGl_WindowTmp",
242 WS_POPUP | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_DISABLED,
244 NULL, NULL, aModule, NULL);
248 aDevCtxTmp = GetDC (aWinTmp);
249 SetPixelFormat (aDevCtxTmp, aPixelFrmtId, &aPixelFrmt);
250 aRendCtxTmp = wglCreateContext (aDevCtxTmp);
253 typedef BOOL (WINAPI *wglChoosePixelFormatARB_t)(HDC theDevCtx,
254 const int* theIntAttribs,
255 const float* theFloatAttribs,
256 unsigned int theMaxFormats,
258 unsigned int* theNumFormatsOut);
259 typedef HGLRC (WINAPI *wglCreateContextAttribsARB_t)(HDC theDevCtx,
260 HGLRC theShareContext,
261 const int* theAttribs);
262 wglChoosePixelFormatARB_t aChoosePixProc = NULL;
263 wglCreateContextAttribsARB_t aCreateCtxProc = NULL;
264 if (aRendCtxTmp != NULL)
266 wglMakeCurrent (aDevCtxTmp, aRendCtxTmp);
268 typedef const char* (WINAPI *wglGetExtensionsStringARB_t)(HDC theDeviceContext);
269 wglGetExtensionsStringARB_t aGetExtensions = (wglGetExtensionsStringARB_t )wglGetProcAddress ("wglGetExtensionsStringARB");
270 const char* aWglExts = (aGetExtensions != NULL) ? aGetExtensions (wglGetCurrentDC()) : NULL;
271 if (OpenGl_Context::CheckExtension (aWglExts, "WGL_ARB_pixel_format"))
273 aChoosePixProc = (wglChoosePixelFormatARB_t )wglGetProcAddress ("wglChoosePixelFormatARB");
275 if (OpenGl_Context::CheckExtension (aWglExts, "WGL_ARB_create_context_profile"))
277 aCreateCtxProc = (wglCreateContextAttribsARB_t )wglGetProcAddress ("wglCreateContextAttribsARB");
281 // choose extended pixel format
282 if (aChoosePixProc != NULL)
284 const int aPixAttribs[] =
286 WGL_DRAW_TO_WINDOW_ARB, GL_TRUE,
287 WGL_SUPPORT_OPENGL_ARB, GL_TRUE,
288 WGL_DOUBLE_BUFFER_ARB, GL_TRUE,
289 WGL_STEREO_ARB, theCaps->contextStereo ? GL_TRUE : GL_FALSE,
290 WGL_PIXEL_TYPE_ARB, WGL_TYPE_RGBA_ARB,
291 //WGL_SAMPLE_BUFFERS_ARB, 1,
292 //WGL_SAMPLES_ARB, 8,
293 WGL_COLOR_BITS_ARB, 24,
294 WGL_DEPTH_BITS_ARB, 24,
295 WGL_STENCIL_BITS_ARB, 8,
296 WGL_ACCELERATION_ARB, theCaps->contextNoAccel ? WGL_NO_ACCELERATION_ARB : WGL_FULL_ACCELERATION_ARB,
299 unsigned int aFrmtsNb = 0;
300 aChoosePixProc (aWindowDC, aPixAttribs, NULL, 1, &aPixelFrmtId, &aFrmtsNb);
303 // setup pixel format - may be set only once per window
304 if (!SetPixelFormat (aWindowDC, aPixelFrmtId, &aPixelFrmt))
306 ReleaseDC (aWindow, aWindowDC);
308 TCollection_AsciiString aMsg("OpenGl_Window::CreateWindow: SetPixelFormat failed. Error code: ");
309 aMsg += (int )GetLastError();
310 Aspect_GraphicDeviceDefinitionError::Raise (aMsg.ToCString());
314 // create GL context with extra options
315 if (aCreateCtxProc != NULL)
317 // Beware! NVIDIA drivers reject context creation when WGL_CONTEXT_PROFILE_MASK_ARB are specified
318 // but not WGL_CONTEXT_MAJOR_VERSION_ARB/WGL_CONTEXT_MINOR_VERSION_ARB.
321 //WGL_CONTEXT_MAJOR_VERSION_ARB, 3,
322 //WGL_CONTEXT_MINOR_VERSION_ARB, 2,
323 //WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB, //WGL_CONTEXT_CORE_PROFILE_BIT_ARB,
324 WGL_CONTEXT_FLAGS_ARB, theCaps->contextDebug ? WGL_CONTEXT_DEBUG_BIT_ARB : 0,
328 aGContext = aCreateCtxProc (aWindowDC, aSlaveCtx, aCtxAttribs);
329 if (aGContext != NULL)
335 if (aRendCtxTmp != NULL)
337 wglDeleteContext (aRendCtxTmp);
339 if (aDevCtxTmp != NULL)
341 ReleaseDC (aWinTmp, aDevCtxTmp);
345 DestroyWindow (aWinTmp);
347 if (aClass.lpszClassName != NULL)
349 UnregisterClassW (aClass.lpszClassName, aModule);
352 if (aGContext == NULL)
354 // create context using obsolete functionality
355 aGContext = wglCreateContext (aWindowDC);
357 if (aGContext == NULL)
359 ReleaseDC (aWindow, aWindowDC);
361 TCollection_AsciiString aMsg ("OpenGl_Window::CreateWindow: wglCreateContext failed. Error code: ");
362 aMsg += (int )GetLastError();
363 Aspect_GraphicDeviceDefinitionError::Raise (aMsg.ToCString());
368 // all GL context within one OpenGl_GraphicDriver should be shared!
369 if (aSlaveCtx != NULL && wglShareLists (aSlaveCtx, aGContext) != TRUE)
371 TCollection_AsciiString aMsg ("OpenGl_Window::CreateWindow: wglShareLists failed. Error code: ");
372 aMsg += (int )GetLastError();
373 Aspect_GraphicDeviceDefinitionError::Raise (aMsg.ToCString());
377 myGlContext->Init ((Aspect_Handle )aWindow, (Aspect_Handle )aWindowDC, (Aspect_RenderingContext )aGContext);
379 WINDOW aParent = (WINDOW )theCWindow.XWindow;
381 DISPLAY* aDisp = (DISPLAY* )myDisplay->GetDisplay();
382 GLXContext aGContext = (GLXContext )theGContext;
384 XWindowAttributes wattr;
385 XGetWindowAttributes (aDisp, aParent, &wattr);
386 const int scr = DefaultScreen (aDisp);
388 XVisualInfo* aVis = NULL;
390 unsigned long aVisInfoMask = VisualIDMask | VisualScreenMask;
391 XVisualInfo aVisInfo;
392 aVisInfo.visualid = wattr.visual->visualid;
393 aVisInfo.screen = scr;
395 aVis = XGetVisualInfo (aDisp, aVisInfoMask, &aVisInfo, &aNbItems);
402 Aspect_GraphicDeviceDefinitionError::Raise ("OpenGl_Window::CreateWindow: XGetVisualInfo failed.");
410 #if defined(__linux) || defined(Linux) || defined(__APPLE__)
413 // check Visual for OpenGl context's parameters compability
414 int isGl = 0, isDoubleBuffer = 0, isRGBA = 0, isStereo = 0;
415 int aDepthSize = 0, aStencilSize = 0;
417 if (glXGetConfig (aDisp, aVis, GLX_USE_GL, &isGl) != 0)
420 if (glXGetConfig (aDisp, aVis, GLX_RGBA, &isRGBA) != 0)
423 if (glXGetConfig (aDisp, aVis, GLX_DOUBLEBUFFER, &isDoubleBuffer) != 0)
426 if (glXGetConfig (aDisp, aVis, GLX_STEREO, &isStereo) != 0)
429 if (glXGetConfig (aDisp, aVis, GLX_DEPTH_SIZE, &aDepthSize) != 0)
432 if (glXGetConfig (aDisp, aVis, GLX_STENCIL_SIZE, &aStencilSize) != 0)
435 if (!isGl || !aDepthSize || !isRGBA || !aStencilSize ||
436 (isDoubleBuffer ? 1 : 0) != (myDisplay->DBuffer() ? 1 : 0) ||
437 (isStereo ? 1 : 0) != (theCaps->contextStereo ? 1 : 0))
449 anAttribs[anIter++] = GLX_RGBA;
451 anAttribs[anIter++] = GLX_DEPTH_SIZE;
452 anAttribs[anIter++] = 1;
454 anAttribs[anIter++] = GLX_STENCIL_SIZE;
455 anAttribs[anIter++] = 1;
457 anAttribs[anIter++] = GLX_RED_SIZE;
458 anAttribs[anIter++] = (wattr.depth <= 8) ? 0 : 1;
460 anAttribs[anIter++] = GLX_GREEN_SIZE;
461 anAttribs[anIter++] = (wattr.depth <= 8) ? 0 : 1;
463 anAttribs[anIter++] = GLX_BLUE_SIZE;
464 anAttribs[anIter++] = (wattr.depth <= 8) ? 0 : 1;
466 if (myDisplay->DBuffer())
467 anAttribs[anIter++] = GLX_DOUBLEBUFFER;
469 // warning: this flag may be set to None, so it need to be last in anAttribs
470 Standard_Integer aStereoFlagPos = anIter;
471 if (theCaps->contextStereo)
472 anAttribs[anIter++] = GLX_STEREO;
474 anAttribs[anIter++] = None;
476 aVis = glXChooseVisual (aDisp, scr, anAttribs);
478 // in case of failure try without stereo if any
479 if (aVis == NULL && theCaps->contextStereo)
481 TCollection_ExtendedString aMsg ("OpenGl_Window::CreateWindow: "
482 "glXChooseVisual is unable to find stereo supported pixel format. "
483 "Choosing similar non stereo format.");
484 myGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB,
485 GL_DEBUG_TYPE_OTHER_ARB,
486 0, GL_DEBUG_SEVERITY_HIGH_ARB, aMsg);
488 anAttribs[aStereoFlagPos] = None;
489 aVis = glXChooseVisual (aDisp, scr, anAttribs);
494 Aspect_GraphicDeviceDefinitionError::Raise ("OpenGl_Window::CreateWindow: glXChooseVisual failed.");
499 if (!theShareCtx.IsNull())
501 // ctx est une copie du previous
502 aGContext = glXCreateContext (aDisp, aVis, (GLXContext )theShareCtx->myGContext, GL_TRUE);
506 aGContext = glXCreateContext (aDisp, aVis, NULL, GL_TRUE);
511 Aspect_GraphicDeviceDefinitionError::Raise ("OpenGl_Window::CreateWindow: glXCreateContext failed.");
515 Colormap cmap = XCreateColormap (aDisp, aParent, aVis->visual, AllocNone);
518 color.red = (unsigned short) (myBgColor.rgb[0] * 0xFFFF);
519 color.green = (unsigned short) (myBgColor.rgb[1] * 0xFFFF);
520 color.blue = (unsigned short) (myBgColor.rgb[2] * 0xFFFF);
521 color.flags = DoRed | DoGreen | DoBlue;
522 XAllocColor (aDisp, cmap, &color);
524 XSetWindowAttributes cwa;
526 cwa.event_mask = StructureNotifyMask;
527 cwa.border_pixel = color.pixel;
528 cwa.background_pixel = color.pixel;
530 if (aVis->visualid == wattr.visual->visualid)
536 unsigned long mask = CWBackPixel | CWColormap | CWBorderPixel | CWEventMask;
537 aWindow = XCreateWindow (aDisp, aParent, 0, 0, myWidth, myHeight, 0/*bw*/, aVis->depth, InputOutput, aVis->visual, mask, &cwa);
540 XSetWindowBackground (aDisp, aWindow, cwa.background_pixel);
541 XClearWindow (aDisp, aWindow);
543 if (aWindow != aParent)
546 XMapWindow (aDisp, aWindow);
547 XIfEvent (aDisp, &anEvent, WaitForNotify, (char* )aWindow);
552 * Le BackDitherProp est utilise pour le clear du background
553 * Pour eviter une difference de couleurs avec la couleur choisie
554 * par l'application (XWindow) il faut desactiver le dithering
555 * au dessus de 8 plans.
557 * Pour le DitherProp:
558 * On cherchera a activer le Dithering que si le Visual a au moins
559 * 8 plans pour le GLX_RED_SIZE. Le test est plus sur car on peut
560 * avoir une profondeur superieure a 12 mais avoir besoin du dithering.
561 * (Carte Impact avec GLX_RED_SIZE a 5 par exemple)
565 glXGetConfig (aDisp, aVis, GLX_RED_SIZE, &aValue);
568 myDither = (aValue < 8);
571 myBackDither = (aVis->depth <= 8);
573 XFree ((char* )aVis);
575 myGlContext->Init ((Aspect_Drawable )aWindow, (Aspect_Display )myDisplay->GetDisplay(), (Aspect_RenderingContext )aGContext);
577 myGlContext->Share (theShareCtx);
582 // =======================================================================
583 // function : ~OpenGl_Window
585 // =======================================================================
586 OpenGl_Window::~OpenGl_Window()
590 myGlContext.Nullify();
594 // release "GL" context if it is owned by window
596 HWND aWindow = (HWND )myGlContext->myWindow;
597 HDC aWindowDC = (HDC )myGlContext->myWindowDC;
598 HGLRC aWindowGContext = (HGLRC )myGlContext->myGContext;
599 HGLRC aThreadGlContext = wglGetCurrentContext();
600 myGlContext.Nullify();
602 if (aThreadGlContext != NULL)
604 // Mesa implementation can fail to reset this thread's context if wglDeleteContext()
605 // called without this step. This might lead to crash when using newly created
606 // context if wglMakeCurrent() is not forced right after the wglCreateContext().
607 if (aThreadGlContext == aWindowGContext)
609 wglMakeCurrent (NULL, NULL);
612 wglDeleteContext (aWindowGContext);
614 ReleaseDC (aWindow, aWindowDC);
616 Display* aDisplay = (Display* )myGlContext->myDisplay;
617 GLXContext aGContext = (GLXContext )myGlContext->myGContext;
618 myGlContext.Nullify();
620 if (aDisplay != NULL)
622 // FSXXX sync necessary if non-direct rendering
624 glXDestroyContext (aDisplay, aGContext);
631 // =======================================================================
632 // function : Activate
634 // =======================================================================
635 Standard_Boolean OpenGl_Window::Activate()
637 return myGlContext->MakeCurrent();
640 #if !defined(__APPLE__) || defined(MACOSX_USE_GLX)
642 // =======================================================================
644 // purpose : call_subr_resize
645 // =======================================================================
646 void OpenGl_Window::Resize (const CALL_DEF_WINDOW& theCWindow)
648 DISPLAY* aDisp = (DISPLAY* )myDisplay->GetDisplay();
652 // If the size is not changed - do nothing
653 if ((myWidth == theCWindow.dx) && (myHeight == theCWindow.dy))
656 myWidth = (Standard_Integer )theCWindow.dx;
657 myHeight = (Standard_Integer )theCWindow.dy;
660 XResizeWindow (aDisp, myGlContext->myWindow, (unsigned int )myWidth, (unsigned int )myHeight);
661 XSync (aDisp, False);
669 // =======================================================================
670 // function : ReadDepths
671 // purpose : TelReadDepths
672 // =======================================================================
673 void OpenGl_Window::ReadDepths (const Standard_Integer theX, const Standard_Integer theY,
674 const Standard_Integer theWidth, const Standard_Integer theHeight,
677 if (theDepths == NULL || !Activate())
680 glMatrixMode (GL_PROJECTION);
682 gluOrtho2D (0.0, (GLdouble )myWidth, 0.0, (GLdouble )myHeight);
683 glMatrixMode (GL_MODELVIEW);
686 glRasterPos2i (theX, theY);
688 glReadPixels (theX, theY, theWidth, theHeight, GL_DEPTH_COMPONENT, GL_FLOAT, theDepths);
692 // =======================================================================
693 // function : SetBackgroundColor
694 // purpose : call_subr_set_background
695 // =======================================================================
696 void OpenGl_Window::SetBackgroundColor (const Standard_ShortReal theR,
697 const Standard_ShortReal theG,
698 const Standard_ShortReal theB)
700 myBgColor.rgb[0] = theR;
701 myBgColor.rgb[1] = theG;
702 myBgColor.rgb[2] = theB;
705 #if !defined(__APPLE__) || defined(MACOSX_USE_GLX)
707 // =======================================================================
710 // =======================================================================
711 void OpenGl_Window::Init()
718 GetClientRect ((HWND )myGlContext->myWindow, &cr);
719 myWidth = cr.right - cr.left;
720 myHeight = cr.bottom - cr.top;
724 unsigned int aDummyU;
725 unsigned int aNewWidth = 0;
726 unsigned int aNewHeight = 0;
727 DISPLAY* aDisp = (DISPLAY* )myDisplay->GetDisplay();
728 XGetGeometry (aDisp, myGlContext->myWindow, &aRootWin, &aDummy, &aDummy, &aNewWidth, &aNewHeight, &aDummyU, &aDummyU);
730 myHeight = aNewHeight;
733 glMatrixMode (GL_MODELVIEW);
734 glViewport (0, 0, myWidth, myHeight);
736 glDisable (GL_SCISSOR_TEST);
737 glDrawBuffer (GL_BACK);
742 // =======================================================================
743 // function : EnablePolygonOffset
744 // purpose : call_subr_enable_polygon_offset
745 // =======================================================================
746 void OpenGl_Window::EnablePolygonOffset() const
748 Standard_ShortReal aFactor, aUnits;
749 myDisplay->PolygonOffset (aFactor, aUnits);
750 glPolygonOffset (aFactor, aUnits);
751 glEnable (GL_POLYGON_OFFSET_FILL);
754 // =======================================================================
755 // function : DisablePolygonOffset
756 // purpose : call_subr_disable_polygon_offset
757 // =======================================================================
758 void OpenGl_Window::DisablePolygonOffset() const
760 glDisable (GL_POLYGON_OFFSET_FILL);
763 // =======================================================================
764 // function : EnableFeatures
766 // =======================================================================
767 void OpenGl_Window::EnableFeatures() const
769 /*glPixelTransferi (GL_MAP_COLOR, GL_TRUE);*/
772 glEnable (GL_DITHER);
774 glDisable (GL_DITHER);
777 // =======================================================================
778 // function : DisableFeatures
780 // =======================================================================
781 void OpenGl_Window::DisableFeatures() const
783 glDisable (GL_DITHER);
784 glPixelTransferi (GL_MAP_COLOR, GL_FALSE);
787 * Disable stuff that's likely to slow down glDrawPixels.
788 * (Omit as much of this as possible, when you know in advance
789 * that the OpenGL state will already be set correctly.)
791 glDisable(GL_ALPHA_TEST);
793 glDisable(GL_DEPTH_TEST);
795 glDisable(GL_LIGHTING);
797 glDisable(GL_LOGIC_OP);
798 glDisable(GL_STENCIL_TEST);
799 glDisable(GL_TEXTURE_1D);
800 glDisable(GL_TEXTURE_2D);
801 glPixelTransferi(GL_MAP_COLOR, GL_FALSE);
802 glPixelTransferi(GL_RED_SCALE, 1);
803 glPixelTransferi(GL_RED_BIAS, 0);
804 glPixelTransferi(GL_GREEN_SCALE, 1);
805 glPixelTransferi(GL_GREEN_BIAS, 0);
806 glPixelTransferi(GL_BLUE_SCALE, 1);
807 glPixelTransferi(GL_BLUE_BIAS, 0);
808 glPixelTransferi(GL_ALPHA_SCALE, 1);
809 glPixelTransferi(GL_ALPHA_BIAS, 0);
812 * Disable extensions that could slow down glDrawPixels.
813 * (Actually, you should check for the presence of the proper
814 * extension before making these calls. I've omitted that
815 * code for simplicity.)
818 if ((myGlContext->myGlVerMajor >= 1) && (myGlContext->myGlVerMinor >= 2))
820 #ifdef GL_EXT_convolution
821 if (myGlContext->CheckExtension ("GL_CONVOLUTION_1D_EXT"))
822 glDisable(GL_CONVOLUTION_1D_EXT);
824 if (myGlContext->CheckExtension ("GL_CONVOLUTION_2D_EXT"))
825 glDisable(GL_CONVOLUTION_2D_EXT);
827 if (myGlContext->CheckExtension ("GL_SEPARABLE_2D_EXT"))
828 glDisable(GL_SEPARABLE_2D_EXT);
831 #ifdef GL_EXT_histogram
832 if (myGlContext->CheckExtension ("GL_SEPARABLE_2D_EXT"))
833 glDisable(GL_HISTOGRAM_EXT);
835 if (myGlContext->CheckExtension ("GL_MINMAX_EXT"))
836 glDisable(GL_MINMAX_EXT);
839 #ifdef GL_EXT_texture3D
840 if (myGlContext->CheckExtension ("GL_TEXTURE_3D_EXT"))
841 glDisable(GL_TEXTURE_3D_EXT);
846 // =======================================================================
847 // function : MakeFrontBufCurrent
848 // purpose : TelMakeFrontBufCurrent
849 // =======================================================================
850 void OpenGl_Window::MakeFrontBufCurrent() const
852 glDrawBuffer (GL_FRONT);
855 // =======================================================================
856 // function : MakeBackBufCurrent
857 // purpose : TelMakeBackBufCurrent
858 // =======================================================================
859 void OpenGl_Window::MakeBackBufCurrent() const
861 glDrawBuffer (GL_BACK);
864 // =======================================================================
865 // function : MakeFrontAndBackBufCurrent
866 // purpose : TelMakeFrontAndBackBufCurrent
867 // =======================================================================
868 void OpenGl_Window::MakeFrontAndBackBufCurrent() const
870 glDrawBuffer (GL_FRONT_AND_BACK);
873 // =======================================================================
874 // function : GetGContext
876 // =======================================================================
877 GLCONTEXT OpenGl_Window::GetGContext() const
879 return (GLCONTEXT )myGlContext->myGContext;