1 // Created on: 2011-09-20
2 // Created by: Sergey ZERCHANINOV
3 // Copyright (c) 2011-2012 OPEN CASCADE SAS
5 // The content of this file is subject to the Open CASCADE Technology Public
6 // License Version 6.5 (the "License"). You may not use the content of this file
7 // except in compliance with the License. Please obtain a copy of the License
8 // at http://www.opencascade.org and read it completely before using this file.
10 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
11 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
13 // The Original Code and all software distributed under the License is
14 // distributed on an "AS IS" basis, without warranty of any kind, and the
15 // Initial Developer hereby disclaims all such warranties, including without
16 // limitation, any warranties of merchantability, fitness for a particular
17 // purpose or non-infringement. Please see the License for the specific terms
18 // and conditions governing the rights and limitations under the License.
20 #include <OpenGl_GlCore11.hxx>
22 #include <InterfaceGraphic.hxx>
24 #include <OpenGl_Window.hxx>
26 #include <OpenGl_Context.hxx>
27 #include <OpenGl_Display.hxx>
29 #include <Aspect_GraphicDeviceDefinitionError.hxx>
30 #include <TCollection_AsciiString.hxx>
32 IMPLEMENT_STANDARD_HANDLE(OpenGl_Window,MMgt_TShared)
33 IMPLEMENT_STANDARD_RTTIEXT(OpenGl_Window,MMgt_TShared)
35 #if !defined(__APPLE__) || defined(MACOSX_USE_GLX)
39 static const TEL_COLOUR THE_DEFAULT_BG_COLOR = { { 0.F, 0.F, 0.F, 1.F } };
43 // WGL_ARB_pixel_format
44 #ifndef WGL_NUMBER_PIXEL_FORMATS_ARB
45 #define WGL_NUMBER_PIXEL_FORMATS_ARB 0x2000
46 #define WGL_DRAW_TO_WINDOW_ARB 0x2001
47 #define WGL_DRAW_TO_BITMAP_ARB 0x2002
48 #define WGL_ACCELERATION_ARB 0x2003
49 #define WGL_NEED_PALETTE_ARB 0x2004
50 #define WGL_NEED_SYSTEM_PALETTE_ARB 0x2005
51 #define WGL_SWAP_LAYER_BUFFERS_ARB 0x2006
52 #define WGL_SWAP_METHOD_ARB 0x2007
53 #define WGL_NUMBER_OVERLAYS_ARB 0x2008
54 #define WGL_NUMBER_UNDERLAYS_ARB 0x2009
55 #define WGL_TRANSPARENT_ARB 0x200A
56 #define WGL_TRANSPARENT_RED_VALUE_ARB 0x2037
57 #define WGL_TRANSPARENT_GREEN_VALUE_ARB 0x2038
58 #define WGL_TRANSPARENT_BLUE_VALUE_ARB 0x2039
59 #define WGL_TRANSPARENT_ALPHA_VALUE_ARB 0x203A
60 #define WGL_TRANSPARENT_INDEX_VALUE_ARB 0x203B
61 #define WGL_SHARE_DEPTH_ARB 0x200C
62 #define WGL_SHARE_STENCIL_ARB 0x200D
63 #define WGL_SHARE_ACCUM_ARB 0x200E
64 #define WGL_SUPPORT_GDI_ARB 0x200F
65 #define WGL_SUPPORT_OPENGL_ARB 0x2010
66 #define WGL_DOUBLE_BUFFER_ARB 0x2011
67 #define WGL_STEREO_ARB 0x2012
68 #define WGL_PIXEL_TYPE_ARB 0x2013
69 #define WGL_COLOR_BITS_ARB 0x2014
70 #define WGL_RED_BITS_ARB 0x2015
71 #define WGL_RED_SHIFT_ARB 0x2016
72 #define WGL_GREEN_BITS_ARB 0x2017
73 #define WGL_GREEN_SHIFT_ARB 0x2018
74 #define WGL_BLUE_BITS_ARB 0x2019
75 #define WGL_BLUE_SHIFT_ARB 0x201A
76 #define WGL_ALPHA_BITS_ARB 0x201B
77 #define WGL_ALPHA_SHIFT_ARB 0x201C
78 #define WGL_ACCUM_BITS_ARB 0x201D
79 #define WGL_ACCUM_RED_BITS_ARB 0x201E
80 #define WGL_ACCUM_GREEN_BITS_ARB 0x201F
81 #define WGL_ACCUM_BLUE_BITS_ARB 0x2020
82 #define WGL_ACCUM_ALPHA_BITS_ARB 0x2021
83 #define WGL_DEPTH_BITS_ARB 0x2022
84 #define WGL_STENCIL_BITS_ARB 0x2023
85 #define WGL_AUX_BUFFERS_ARB 0x2024
87 #define WGL_NO_ACCELERATION_ARB 0x2025
88 #define WGL_GENERIC_ACCELERATION_ARB 0x2026
89 #define WGL_FULL_ACCELERATION_ARB 0x2027
91 #define WGL_SWAP_EXCHANGE_ARB 0x2028
92 #define WGL_SWAP_COPY_ARB 0x2029
93 #define WGL_SWAP_UNDEFINED_ARB 0x202A
95 #define WGL_TYPE_RGBA_ARB 0x202B
96 #define WGL_TYPE_COLORINDEX_ARB 0x202C
97 #endif // WGL_NUMBER_PIXEL_FORMATS_ARB
99 // WGL_ARB_create_context_profile
100 #ifndef WGL_CONTEXT_MAJOR_VERSION_ARB
101 #define WGL_CONTEXT_MAJOR_VERSION_ARB 0x2091
102 #define WGL_CONTEXT_MINOR_VERSION_ARB 0x2092
103 #define WGL_CONTEXT_LAYER_PLANE_ARB 0x2093
104 #define WGL_CONTEXT_FLAGS_ARB 0x2094
105 #define WGL_CONTEXT_PROFILE_MASK_ARB 0x9126
107 // WGL_CONTEXT_FLAGS bits
108 #define WGL_CONTEXT_DEBUG_BIT_ARB 0x0001
109 #define WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB 0x0002
111 // WGL_CONTEXT_PROFILE_MASK_ARB bits
112 #define WGL_CONTEXT_CORE_PROFILE_BIT_ARB 0x00000001
113 #define WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB 0x00000002
114 #endif // WGL_CONTEXT_MAJOR_VERSION_ARB
116 static LRESULT CALLBACK wndProcDummy (HWND theWin, UINT theMsg, WPARAM theParamW, LPARAM theParamL)
118 return DefWindowProcW (theWin, theMsg, theParamW, theParamL);
121 static Bool WaitForNotify (Display* theDisp, XEvent* theEv, char* theArg)
123 return (theEv->type == MapNotify) && (theEv->xmap.window == (Window )theArg);
129 // =======================================================================
130 // function : OpenGl_Window
132 // =======================================================================
133 OpenGl_Window::OpenGl_Window (const Handle(OpenGl_Display)& theDisplay,
134 const CALL_DEF_WINDOW& theCWindow,
135 Aspect_RenderingContext theGContext,
136 const Handle(OpenGl_Caps)& theCaps,
137 const Handle(OpenGl_Context)& theShareCtx)
138 : myDisplay (theDisplay),
139 myGlContext (new OpenGl_Context (theCaps)),
140 myOwnGContext (theGContext == 0),
142 mySysPalInUse (FALSE),
144 myWidth ((Standard_Integer )theCWindow.dx),
145 myHeight ((Standard_Integer )theCWindow.dy),
146 myBgColor (THE_DEFAULT_BG_COLOR),
147 myDither (theDisplay->Dither()),
148 myBackDither (theDisplay->BackDither())
150 myBgColor.rgb[0] = theCWindow.Background.r;
151 myBgColor.rgb[1] = theCWindow.Background.g;
152 myBgColor.rgb[2] = theCWindow.Background.b;
155 HWND aWindow = (HWND )theCWindow.XWindow;
156 HDC aWindowDC = GetDC (aWindow);
157 HGLRC aGContext = (HGLRC )theGContext;
159 PIXELFORMATDESCRIPTOR aPixelFrmt;
160 memset (&aPixelFrmt, 0, sizeof(aPixelFrmt));
161 aPixelFrmt.nSize = sizeof(PIXELFORMATDESCRIPTOR);
162 aPixelFrmt.nVersion = 1;
163 aPixelFrmt.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
164 aPixelFrmt.iPixelType = PFD_TYPE_RGBA;
165 aPixelFrmt.cColorBits = 24;
166 aPixelFrmt.cDepthBits = 24;
167 aPixelFrmt.cStencilBits = 8;
168 aPixelFrmt.iLayerType = PFD_MAIN_PLANE;
169 if (theCaps->contextStereo)
171 aPixelFrmt.dwFlags |= PFD_STEREO;
174 int aPixelFrmtId = ChoosePixelFormat (aWindowDC, &aPixelFrmt);
175 if (aPixelFrmtId == 0)
177 ReleaseDC (aWindow, aWindowDC);
179 TCollection_AsciiString aMsg ("OpenGl_Window::CreateWindow: ChoosePixelFormat failed. Error code: ");
180 aMsg += (int )GetLastError();
181 Aspect_GraphicDeviceDefinitionError::Raise (aMsg.ToCString());
185 DescribePixelFormat (aWindowDC, aPixelFrmtId, sizeof(aPixelFrmt), &aPixelFrmt);
186 if (aPixelFrmt.dwFlags & PFD_NEED_PALETTE)
188 WINDOW_DATA* aWndData = (WINDOW_DATA* )GetWindowLongPtr (aWindow, GWLP_USERDATA);
190 mySysPalInUse = (aPixelFrmt.dwFlags & PFD_NEED_SYSTEM_PALETTE) ? TRUE : FALSE;
191 InterfaceGraphic_RealizePalette (aWindowDC, aWndData->hPal, FALSE, mySysPalInUse);
196 myDither = (aPixelFrmt.cColorBits <= 8);
201 myBackDither = (aPixelFrmt.cColorBits <= 8);
204 HGLRC aSlaveCtx = !theShareCtx.IsNull() ? (HGLRC )theShareCtx->myGContext : NULL;
205 if (aGContext == NULL)
207 // create temporary context to retrieve advanced context creation procedures
208 HMODULE aModule = GetModuleHandleW(NULL);
209 WNDCLASSW aClass; memset (&aClass, 0, sizeof(aClass));
210 aClass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
211 aClass.lpfnWndProc = wndProcDummy;
212 aClass.hInstance = aModule;
213 aClass.lpszClassName = L"OpenGl_WindowTmp";
215 HDC aDevCtxTmp = NULL;
216 HGLRC aRendCtxTmp = NULL;
217 if ((!theCaps->contextDebug && !theCaps->contextNoAccel)
218 || RegisterClassW (&aClass) == 0)
220 aClass.lpszClassName = NULL;
222 if (aClass.lpszClassName != NULL)
224 aWinTmp = CreateWindowExW(WS_EX_TOOLWINDOW | WS_EX_WINDOWEDGE | WS_EX_NOACTIVATE,
225 aClass.lpszClassName, L"OpenGl_WindowTmp",
226 WS_POPUP | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_DISABLED,
228 NULL, NULL, aModule, NULL);
232 aDevCtxTmp = GetDC (aWinTmp);
233 SetPixelFormat (aDevCtxTmp, aPixelFrmtId, &aPixelFrmt);
234 aRendCtxTmp = wglCreateContext (aDevCtxTmp);
237 typedef BOOL (WINAPI *wglChoosePixelFormatARB_t)(HDC theDevCtx,
238 const int* theIntAttribs,
239 const float* theFloatAttribs,
240 unsigned int theMaxFormats,
242 unsigned int* theNumFormatsOut);
243 typedef HGLRC (WINAPI *wglCreateContextAttribsARB_t)(HDC theDevCtx,
244 HGLRC theShareContext,
245 const int* theAttribs);
246 wglChoosePixelFormatARB_t aChoosePixProc = NULL;
247 wglCreateContextAttribsARB_t aCreateCtxProc = NULL;
248 if (aRendCtxTmp != NULL)
250 wglMakeCurrent (aDevCtxTmp, aRendCtxTmp);
252 typedef const char* (WINAPI *wglGetExtensionsStringARB_t)(HDC theDeviceContext);
253 wglGetExtensionsStringARB_t aGetExtensions = (wglGetExtensionsStringARB_t )wglGetProcAddress ("wglGetExtensionsStringARB");
254 const char* aWglExts = (aGetExtensions != NULL) ? aGetExtensions (wglGetCurrentDC()) : NULL;
255 if (OpenGl_Context::CheckExtension (aWglExts, "WGL_ARB_pixel_format"))
257 aChoosePixProc = (wglChoosePixelFormatARB_t )wglGetProcAddress ("wglChoosePixelFormatARB");
259 if (OpenGl_Context::CheckExtension (aWglExts, "WGL_ARB_create_context_profile"))
261 aCreateCtxProc = (wglCreateContextAttribsARB_t )wglGetProcAddress ("wglCreateContextAttribsARB");
265 // choose extended pixel format
266 if (aChoosePixProc != NULL)
268 const int aPixAttribs[] =
270 WGL_DRAW_TO_WINDOW_ARB, GL_TRUE,
271 WGL_SUPPORT_OPENGL_ARB, GL_TRUE,
272 WGL_DOUBLE_BUFFER_ARB, GL_TRUE,
273 WGL_STEREO_ARB, theCaps->contextStereo ? GL_TRUE : GL_FALSE,
274 WGL_PIXEL_TYPE_ARB, WGL_TYPE_RGBA_ARB,
275 //WGL_SAMPLE_BUFFERS_ARB, 1,
276 //WGL_SAMPLES_ARB, 8,
277 WGL_COLOR_BITS_ARB, 24,
278 WGL_DEPTH_BITS_ARB, 24,
279 WGL_STENCIL_BITS_ARB, 8,
280 WGL_ACCELERATION_ARB, theCaps->contextNoAccel ? WGL_NO_ACCELERATION_ARB : WGL_FULL_ACCELERATION_ARB,
283 unsigned int aFrmtsNb = 0;
284 aChoosePixProc (aWindowDC, aPixAttribs, NULL, 1, &aPixelFrmtId, &aFrmtsNb);
287 // setup pixel format - may be set only once per window
288 if (!SetPixelFormat (aWindowDC, aPixelFrmtId, &aPixelFrmt))
290 ReleaseDC (aWindow, aWindowDC);
292 TCollection_AsciiString aMsg("OpenGl_Window::CreateWindow: SetPixelFormat failed. Error code: ");
293 aMsg += (int )GetLastError();
294 Aspect_GraphicDeviceDefinitionError::Raise (aMsg.ToCString());
298 // create GL context with extra options
299 if (aCreateCtxProc != NULL)
301 // Beware! NVIDIA drivers reject context creation when WGL_CONTEXT_PROFILE_MASK_ARB are specified
302 // but not WGL_CONTEXT_MAJOR_VERSION_ARB/WGL_CONTEXT_MINOR_VERSION_ARB.
305 //WGL_CONTEXT_MAJOR_VERSION_ARB, 3,
306 //WGL_CONTEXT_MINOR_VERSION_ARB, 2,
307 //WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB, //WGL_CONTEXT_CORE_PROFILE_BIT_ARB,
308 WGL_CONTEXT_FLAGS_ARB, theCaps->contextDebug ? WGL_CONTEXT_DEBUG_BIT_ARB : 0,
312 aGContext = aCreateCtxProc (aWindowDC, aSlaveCtx, aCtxAttribs);
313 if (aGContext != NULL)
319 if (aRendCtxTmp != NULL)
321 wglDeleteContext (aRendCtxTmp);
323 if (aDevCtxTmp != NULL)
325 ReleaseDC (aWinTmp, aDevCtxTmp);
329 DestroyWindow (aWinTmp);
331 if (aClass.lpszClassName != NULL)
333 UnregisterClassW (aClass.lpszClassName, aModule);
336 if (aGContext == NULL)
338 // create context using obsolete functionality
339 aGContext = wglCreateContext (aWindowDC);
341 if (aGContext == NULL)
343 ReleaseDC (aWindow, aWindowDC);
345 TCollection_AsciiString aMsg ("OpenGl_Window::CreateWindow: wglCreateContext failed. Error code: ");
346 aMsg += (int )GetLastError();
347 Aspect_GraphicDeviceDefinitionError::Raise (aMsg.ToCString());
352 // all GL context within one OpenGl_GraphicDriver should be shared!
353 if (aSlaveCtx != NULL && wglShareLists (aSlaveCtx, aGContext) != TRUE)
355 TCollection_AsciiString aMsg ("OpenGl_Window::CreateWindow: wglShareLists failed. Error code: ");
356 aMsg += (int )GetLastError();
357 Aspect_GraphicDeviceDefinitionError::Raise (aMsg.ToCString());
361 myGlContext->Init ((Aspect_Handle )aWindow, (Aspect_Handle )aWindowDC, (Aspect_RenderingContext )aGContext);
363 WINDOW aParent = (WINDOW )theCWindow.XWindow;
365 DISPLAY* aDisp = (DISPLAY* )myDisplay->GetDisplay();
366 GLXContext aGContext = (GLXContext )theGContext;
368 XWindowAttributes wattr;
369 XGetWindowAttributes (aDisp, aParent, &wattr);
370 const int scr = DefaultScreen (aDisp);
372 XVisualInfo* aVis = NULL;
374 unsigned long aVisInfoMask = VisualIDMask | VisualScreenMask;
375 XVisualInfo aVisInfo;
376 aVisInfo.visualid = wattr.visual->visualid;
377 aVisInfo.screen = scr;
379 aVis = XGetVisualInfo (aDisp, aVisInfoMask, &aVisInfo, &aNbItems);
386 Aspect_GraphicDeviceDefinitionError::Raise ("OpenGl_Window::CreateWindow: XGetVisualInfo failed.");
394 #if defined(__linux) || defined(Linux) || defined(__APPLE__)
397 // check Visual for OpenGl context's parameters compability
398 int isGl = 0, isDoubleBuffer = 0, isRGBA = 0, aDepthSize = 0, aStencilSize = 0;
400 if (glXGetConfig (aDisp, aVis, GLX_USE_GL, &isGl) != 0)
403 if (glXGetConfig (aDisp, aVis, GLX_RGBA, &isRGBA) != 0)
406 if (glXGetConfig (aDisp, aVis, GLX_DOUBLEBUFFER, &isDoubleBuffer) != 0)
409 if (glXGetConfig (aDisp, aVis, GLX_DEPTH_SIZE, &aDepthSize) != 0)
412 if (glXGetConfig (aDisp, aVis, GLX_STENCIL_SIZE, &aStencilSize) != 0)
415 if (!isGl || !aDepthSize || !isRGBA || (isDoubleBuffer ? 1 : 0) != (myDisplay->DBuffer()? 1 : 0))
427 anAttribs[anIter++] = GLX_RGBA;
429 anAttribs[anIter++] = GLX_DEPTH_SIZE;
430 anAttribs[anIter++] = 1;
432 anAttribs[anIter++] = GLX_STENCIL_SIZE;
433 anAttribs[anIter++] = 1;
435 anAttribs[anIter++] = GLX_RED_SIZE;
436 anAttribs[anIter++] = (wattr.depth <= 8) ? 0 : 1;
438 anAttribs[anIter++] = GLX_GREEN_SIZE;
439 anAttribs[anIter++] = (wattr.depth <= 8) ? 0 : 1;
441 anAttribs[anIter++] = GLX_BLUE_SIZE;
442 anAttribs[anIter++] = (wattr.depth <= 8) ? 0 : 1;
444 if (myDisplay->DBuffer())
445 anAttribs[anIter++] = GLX_DOUBLEBUFFER;
447 anAttribs[anIter++] = None;
449 aVis = glXChooseVisual (aDisp, scr, anAttribs);
452 Aspect_GraphicDeviceDefinitionError::Raise ("OpenGl_Window::CreateWindow: glXChooseVisual failed.");
457 if (!theShareCtx.IsNull())
459 // ctx est une copie du previous
460 aGContext = glXCreateContext (aDisp, aVis, (GLXContext )theShareCtx->myGContext, GL_TRUE);
464 aGContext = glXCreateContext (aDisp, aVis, NULL, GL_TRUE);
469 Aspect_GraphicDeviceDefinitionError::Raise ("OpenGl_Window::CreateWindow: glXCreateContext failed.");
473 Colormap cmap = XCreateColormap (aDisp, aParent, aVis->visual, AllocNone);
476 color.red = (unsigned short) (myBgColor.rgb[0] * 0xFFFF);
477 color.green = (unsigned short) (myBgColor.rgb[1] * 0xFFFF);
478 color.blue = (unsigned short) (myBgColor.rgb[2] * 0xFFFF);
479 color.flags = DoRed | DoGreen | DoBlue;
480 XAllocColor (aDisp, cmap, &color);
482 XSetWindowAttributes cwa;
484 cwa.event_mask = StructureNotifyMask;
485 cwa.border_pixel = color.pixel;
486 cwa.background_pixel = color.pixel;
488 if (aVis->visualid == wattr.visual->visualid)
494 unsigned long mask = CWBackPixel | CWColormap | CWBorderPixel | CWEventMask;
495 aWindow = XCreateWindow (aDisp, aParent, 0, 0, myWidth, myHeight, 0/*bw*/, aVis->depth, InputOutput, aVis->visual, mask, &cwa);
498 XSetWindowBackground (aDisp, aWindow, cwa.background_pixel);
499 XClearWindow (aDisp, aWindow);
501 if (aWindow != aParent)
504 XMapWindow (aDisp, aWindow);
505 XIfEvent (aDisp, &anEvent, WaitForNotify, (char* )aWindow);
510 * Le BackDitherProp est utilise pour le clear du background
511 * Pour eviter une difference de couleurs avec la couleur choisie
512 * par l'application (XWindow) il faut desactiver le dithering
513 * au dessus de 8 plans.
515 * Pour le DitherProp:
516 * On cherchera a activer le Dithering que si le Visual a au moins
517 * 8 plans pour le GLX_RED_SIZE. Le test est plus sur car on peut
518 * avoir une profondeur superieure a 12 mais avoir besoin du dithering.
519 * (Carte Impact avec GLX_RED_SIZE a 5 par exemple)
523 glXGetConfig (aDisp, aVis, GLX_RED_SIZE, &aValue);
526 myDither = (aValue < 8);
529 myBackDither = (aVis->depth <= 8);
531 XFree ((char* )aVis);
533 myGlContext->Init ((Aspect_Drawable )aWindow, (Aspect_Display )myDisplay->GetDisplay(), (Aspect_RenderingContext )aGContext);
535 myGlContext->Share (theShareCtx);
540 // =======================================================================
541 // function : ~OpenGl_Window
543 // =======================================================================
544 OpenGl_Window::~OpenGl_Window()
547 HWND aWindow = (HWND )myGlContext->myWindow;
548 HDC aWindowDC = (HDC )myGlContext->myWindowDC;
549 HGLRC aGContext = (HGLRC )myGlContext->myGContext;
550 myGlContext.Nullify();
554 if (wglGetCurrentContext() != NULL)
556 wglDeleteContext (aGContext);
558 ReleaseDC (aWindow, aWindowDC);
561 Display* aDisplay = (Display* )myGlContext->myDisplay;
562 GLXContext aGContext = (GLXContext )myGlContext->myGContext;
563 myGlContext.Nullify();
565 if (aDisplay != NULL && myOwnGContext)
567 // FSXXX sync necessary if non-direct rendering
569 glXDestroyContext (aDisplay, aGContext);
576 // =======================================================================
577 // function : Activate
579 // =======================================================================
580 Standard_Boolean OpenGl_Window::Activate()
582 return myGlContext->MakeCurrent();
585 #if !defined(__APPLE__) || defined(MACOSX_USE_GLX)
587 // =======================================================================
589 // purpose : call_subr_resize
590 // =======================================================================
591 void OpenGl_Window::Resize (const CALL_DEF_WINDOW& theCWindow)
593 DISPLAY* aDisp = (DISPLAY* )myDisplay->GetDisplay();
597 // If the size is not changed - do nothing
598 if ((myWidth == theCWindow.dx) && (myHeight == theCWindow.dy))
601 myWidth = (Standard_Integer )theCWindow.dx;
602 myHeight = (Standard_Integer )theCWindow.dy;
605 XResizeWindow (aDisp, myGlContext->myWindow, (unsigned int )myWidth, (unsigned int )myHeight);
606 XSync (aDisp, False);
614 // =======================================================================
615 // function : ReadDepths
616 // purpose : TelReadDepths
617 // =======================================================================
618 void OpenGl_Window::ReadDepths (const Standard_Integer theX, const Standard_Integer theY,
619 const Standard_Integer theWidth, const Standard_Integer theHeight,
622 if (theDepths == NULL || !Activate())
625 glMatrixMode (GL_PROJECTION);
627 gluOrtho2D (0.0, (GLdouble )myWidth, 0.0, (GLdouble )myHeight);
628 glMatrixMode (GL_MODELVIEW);
631 glRasterPos2i (theX, theY);
633 glReadPixels (theX, theY, theWidth, theHeight, GL_DEPTH_COMPONENT, GL_FLOAT, theDepths);
637 // =======================================================================
638 // function : SetBackgroundColor
639 // purpose : call_subr_set_background
640 // =======================================================================
641 void OpenGl_Window::SetBackgroundColor (const Standard_ShortReal theR,
642 const Standard_ShortReal theG,
643 const Standard_ShortReal theB)
645 myBgColor.rgb[0] = theR;
646 myBgColor.rgb[1] = theG;
647 myBgColor.rgb[2] = theB;
650 #if !defined(__APPLE__) || defined(MACOSX_USE_GLX)
652 // =======================================================================
655 // =======================================================================
656 void OpenGl_Window::Init()
663 GetClientRect ((HWND )myGlContext->myWindow, &cr);
664 myWidth = cr.right - cr.left;
665 myHeight = cr.bottom - cr.top;
669 unsigned int aDummyU;
670 unsigned int aNewWidth = 0;
671 unsigned int aNewHeight = 0;
672 DISPLAY* aDisp = (DISPLAY* )myDisplay->GetDisplay();
673 XGetGeometry (aDisp, myGlContext->myWindow, &aRootWin, &aDummy, &aDummy, &aNewWidth, &aNewHeight, &aDummyU, &aDummyU);
675 myHeight = aNewHeight;
678 glMatrixMode (GL_MODELVIEW);
679 glViewport (0, 0, myWidth, myHeight);
681 glDisable (GL_SCISSOR_TEST);
682 glDrawBuffer (GL_BACK);
687 // =======================================================================
688 // function : EnablePolygonOffset
689 // purpose : call_subr_enable_polygon_offset
690 // =======================================================================
691 void OpenGl_Window::EnablePolygonOffset() const
693 Standard_ShortReal aFactor, aUnits;
694 myDisplay->PolygonOffset (aFactor, aUnits);
695 glPolygonOffset (aFactor, aUnits);
696 glEnable (GL_POLYGON_OFFSET_FILL);
699 // =======================================================================
700 // function : DisablePolygonOffset
701 // purpose : call_subr_disable_polygon_offset
702 // =======================================================================
703 void OpenGl_Window::DisablePolygonOffset() const
705 glDisable (GL_POLYGON_OFFSET_FILL);
708 // =======================================================================
709 // function : EnableFeatures
711 // =======================================================================
712 void OpenGl_Window::EnableFeatures() const
714 /*glPixelTransferi (GL_MAP_COLOR, GL_TRUE);*/
717 glEnable (GL_DITHER);
719 glDisable (GL_DITHER);
722 // =======================================================================
723 // function : DisableFeatures
725 // =======================================================================
726 void OpenGl_Window::DisableFeatures() const
728 glDisable (GL_DITHER);
729 glPixelTransferi (GL_MAP_COLOR, GL_FALSE);
732 * Disable stuff that's likely to slow down glDrawPixels.
733 * (Omit as much of this as possible, when you know in advance
734 * that the OpenGL state will already be set correctly.)
736 glDisable(GL_ALPHA_TEST);
738 glDisable(GL_DEPTH_TEST);
740 glDisable(GL_LIGHTING);
742 glDisable(GL_LOGIC_OP);
743 glDisable(GL_STENCIL_TEST);
744 glDisable(GL_TEXTURE_1D);
745 glDisable(GL_TEXTURE_2D);
746 glPixelTransferi(GL_MAP_COLOR, GL_FALSE);
747 glPixelTransferi(GL_RED_SCALE, 1);
748 glPixelTransferi(GL_RED_BIAS, 0);
749 glPixelTransferi(GL_GREEN_SCALE, 1);
750 glPixelTransferi(GL_GREEN_BIAS, 0);
751 glPixelTransferi(GL_BLUE_SCALE, 1);
752 glPixelTransferi(GL_BLUE_BIAS, 0);
753 glPixelTransferi(GL_ALPHA_SCALE, 1);
754 glPixelTransferi(GL_ALPHA_BIAS, 0);
757 * Disable extensions that could slow down glDrawPixels.
758 * (Actually, you should check for the presence of the proper
759 * extension before making these calls. I've omitted that
760 * code for simplicity.)
763 #ifdef GL_EXT_convolution
764 glDisable(GL_CONVOLUTION_1D_EXT);
765 glDisable(GL_CONVOLUTION_2D_EXT);
766 glDisable(GL_SEPARABLE_2D_EXT);
769 #ifdef GL_EXT_histogram
770 glDisable(GL_HISTOGRAM_EXT);
771 glDisable(GL_MINMAX_EXT);
774 #ifdef GL_EXT_texture3D
775 glDisable(GL_TEXTURE_3D_EXT);
779 // =======================================================================
780 // function : MakeFrontBufCurrent
781 // purpose : TelMakeFrontBufCurrent
782 // =======================================================================
783 void OpenGl_Window::MakeFrontBufCurrent() const
785 glDrawBuffer (GL_FRONT);
788 // =======================================================================
789 // function : MakeBackBufCurrent
790 // purpose : TelMakeBackBufCurrent
791 // =======================================================================
792 void OpenGl_Window::MakeBackBufCurrent() const
794 glDrawBuffer (GL_BACK);
797 // =======================================================================
798 // function : MakeFrontAndBackBufCurrent
799 // purpose : TelMakeFrontAndBackBufCurrent
800 // =======================================================================
801 void OpenGl_Window::MakeFrontAndBackBufCurrent() const
803 glDrawBuffer (GL_FRONT_AND_BACK);
806 // =======================================================================
807 // function : GetGContext
809 // =======================================================================
810 GLCONTEXT OpenGl_Window::GetGContext() const
812 return (GLCONTEXT )myGlContext->myGContext;