0024512: clang++ compiler complains about extra semicolon
[occt.git] / src / OpenGl / OpenGl_Window.cxx
CommitLineData
b311480e 1// Created on: 2011-09-20
2// Created by: Sergey ZERCHANINOV
973c2be1 3// Copyright (c) 2011-2014 OPEN CASCADE SAS
b311480e 4//
973c2be1 5// This file is part of Open CASCADE Technology software library.
b311480e 6//
973c2be1 7// This library is free software; you can redistribute it and / or modify it
8// under the terms of the GNU Lesser General Public 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.
b311480e 12//
973c2be1 13// Alternatively, this file may be used under the terms of Open CASCADE
14// commercial license or contractual agreement.
b311480e 15
cfd5fa01 16#include <OpenGl_GlCore11.hxx>
17
2166f0fa
SK
18#include <InterfaceGraphic.hxx>
19
20#include <OpenGl_Window.hxx>
21
22#include <OpenGl_Context.hxx>
23#include <OpenGl_Display.hxx>
2166f0fa
SK
24
25#include <Aspect_GraphicDeviceDefinitionError.hxx>
26#include <TCollection_AsciiString.hxx>
27
28IMPLEMENT_STANDARD_HANDLE(OpenGl_Window,MMgt_TShared)
29IMPLEMENT_STANDARD_RTTIEXT(OpenGl_Window,MMgt_TShared)
30
4fe56619 31#if !defined(__APPLE__) || defined(MACOSX_USE_GLX)
32
2166f0fa
SK
33namespace
34{
35 static const TEL_COLOUR THE_DEFAULT_BG_COLOR = { { 0.F, 0.F, 0.F, 1.F } };
36
58655684 37#if defined(_WIN32)
38
abe46077 39 // WGL_ARB_pixel_format
40#ifndef WGL_NUMBER_PIXEL_FORMATS_ARB
41 #define WGL_NUMBER_PIXEL_FORMATS_ARB 0x2000
42 #define WGL_DRAW_TO_WINDOW_ARB 0x2001
43 #define WGL_DRAW_TO_BITMAP_ARB 0x2002
44 #define WGL_ACCELERATION_ARB 0x2003
45 #define WGL_NEED_PALETTE_ARB 0x2004
46 #define WGL_NEED_SYSTEM_PALETTE_ARB 0x2005
47 #define WGL_SWAP_LAYER_BUFFERS_ARB 0x2006
48 #define WGL_SWAP_METHOD_ARB 0x2007
49 #define WGL_NUMBER_OVERLAYS_ARB 0x2008
50 #define WGL_NUMBER_UNDERLAYS_ARB 0x2009
51 #define WGL_TRANSPARENT_ARB 0x200A
52 #define WGL_TRANSPARENT_RED_VALUE_ARB 0x2037
53 #define WGL_TRANSPARENT_GREEN_VALUE_ARB 0x2038
54 #define WGL_TRANSPARENT_BLUE_VALUE_ARB 0x2039
55 #define WGL_TRANSPARENT_ALPHA_VALUE_ARB 0x203A
56 #define WGL_TRANSPARENT_INDEX_VALUE_ARB 0x203B
57 #define WGL_SHARE_DEPTH_ARB 0x200C
58 #define WGL_SHARE_STENCIL_ARB 0x200D
59 #define WGL_SHARE_ACCUM_ARB 0x200E
60 #define WGL_SUPPORT_GDI_ARB 0x200F
61 #define WGL_SUPPORT_OPENGL_ARB 0x2010
62 #define WGL_DOUBLE_BUFFER_ARB 0x2011
63 #define WGL_STEREO_ARB 0x2012
64 #define WGL_PIXEL_TYPE_ARB 0x2013
65 #define WGL_COLOR_BITS_ARB 0x2014
66 #define WGL_RED_BITS_ARB 0x2015
67 #define WGL_RED_SHIFT_ARB 0x2016
68 #define WGL_GREEN_BITS_ARB 0x2017
69 #define WGL_GREEN_SHIFT_ARB 0x2018
70 #define WGL_BLUE_BITS_ARB 0x2019
71 #define WGL_BLUE_SHIFT_ARB 0x201A
72 #define WGL_ALPHA_BITS_ARB 0x201B
73 #define WGL_ALPHA_SHIFT_ARB 0x201C
74 #define WGL_ACCUM_BITS_ARB 0x201D
75 #define WGL_ACCUM_RED_BITS_ARB 0x201E
76 #define WGL_ACCUM_GREEN_BITS_ARB 0x201F
77 #define WGL_ACCUM_BLUE_BITS_ARB 0x2020
78 #define WGL_ACCUM_ALPHA_BITS_ARB 0x2021
79 #define WGL_DEPTH_BITS_ARB 0x2022
80 #define WGL_STENCIL_BITS_ARB 0x2023
81 #define WGL_AUX_BUFFERS_ARB 0x2024
82
83 #define WGL_NO_ACCELERATION_ARB 0x2025
84 #define WGL_GENERIC_ACCELERATION_ARB 0x2026
85 #define WGL_FULL_ACCELERATION_ARB 0x2027
86
87 #define WGL_SWAP_EXCHANGE_ARB 0x2028
88 #define WGL_SWAP_COPY_ARB 0x2029
89 #define WGL_SWAP_UNDEFINED_ARB 0x202A
90
91 #define WGL_TYPE_RGBA_ARB 0x202B
92 #define WGL_TYPE_COLORINDEX_ARB 0x202C
93#endif // WGL_NUMBER_PIXEL_FORMATS_ARB
94
58655684 95 // WGL_ARB_create_context_profile
96#ifndef WGL_CONTEXT_MAJOR_VERSION_ARB
97 #define WGL_CONTEXT_MAJOR_VERSION_ARB 0x2091
98 #define WGL_CONTEXT_MINOR_VERSION_ARB 0x2092
99 #define WGL_CONTEXT_LAYER_PLANE_ARB 0x2093
100 #define WGL_CONTEXT_FLAGS_ARB 0x2094
101 #define WGL_CONTEXT_PROFILE_MASK_ARB 0x9126
102
103 // WGL_CONTEXT_FLAGS bits
104 #define WGL_CONTEXT_DEBUG_BIT_ARB 0x0001
105 #define WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB 0x0002
106
107 // WGL_CONTEXT_PROFILE_MASK_ARB bits
108 #define WGL_CONTEXT_CORE_PROFILE_BIT_ARB 0x00000001
109 #define WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB 0x00000002
110#endif // WGL_CONTEXT_MAJOR_VERSION_ARB
111
112 static LRESULT CALLBACK wndProcDummy (HWND theWin, UINT theMsg, WPARAM theParamW, LPARAM theParamL)
113 {
114 return DefWindowProcW (theWin, theMsg, theParamW, theParamL);
115 }
2166f0fa
SK
116#else
117 static Bool WaitForNotify (Display* theDisp, XEvent* theEv, char* theArg)
118 {
119 return (theEv->type == MapNotify) && (theEv->xmap.window == (Window )theArg);
120 }
121#endif
122
5640d653 123}
2166f0fa
SK
124
125// =======================================================================
126// function : OpenGl_Window
127// purpose :
128// =======================================================================
129OpenGl_Window::OpenGl_Window (const Handle(OpenGl_Display)& theDisplay,
130 const CALL_DEF_WINDOW& theCWindow,
5e27df78 131 Aspect_RenderingContext theGContext,
58655684 132 const Handle(OpenGl_Caps)& theCaps,
5e27df78 133 const Handle(OpenGl_Context)& theShareCtx)
2166f0fa 134: myDisplay (theDisplay),
58655684 135 myGlContext (new OpenGl_Context (theCaps)),
2166f0fa 136 myOwnGContext (theGContext == 0),
58655684 137#if defined(_WIN32)
2166f0fa
SK
138 mySysPalInUse (FALSE),
139#endif
140 myWidth ((Standard_Integer )theCWindow.dx),
141 myHeight ((Standard_Integer )theCWindow.dy),
142 myBgColor (THE_DEFAULT_BG_COLOR),
143 myDither (theDisplay->Dither()),
144 myBackDither (theDisplay->BackDither())
145{
146 myBgColor.rgb[0] = theCWindow.Background.r;
147 myBgColor.rgb[1] = theCWindow.Background.g;
148 myBgColor.rgb[2] = theCWindow.Background.b;
149
58655684 150#if defined(_WIN32)
5e27df78 151 HWND aWindow = (HWND )theCWindow.XWindow;
152 HDC aWindowDC = GetDC (aWindow);
153 HGLRC aGContext = (HGLRC )theGContext;
154
3d8969b1 155 PIXELFORMATDESCRIPTOR aPixelFrmt;
abe46077 156 memset (&aPixelFrmt, 0, sizeof(aPixelFrmt));
157 aPixelFrmt.nSize = sizeof(PIXELFORMATDESCRIPTOR);
158 aPixelFrmt.nVersion = 1;
159 aPixelFrmt.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
160 aPixelFrmt.iPixelType = PFD_TYPE_RGBA;
161 aPixelFrmt.cColorBits = 24;
162 aPixelFrmt.cDepthBits = 24;
163 aPixelFrmt.cStencilBits = 8;
164 aPixelFrmt.iLayerType = PFD_MAIN_PLANE;
165 if (theCaps->contextStereo)
166 {
167 aPixelFrmt.dwFlags |= PFD_STEREO;
168 }
169
170 int aPixelFrmtId = ChoosePixelFormat (aWindowDC, &aPixelFrmt);
3d8969b1 171 if (aPixelFrmtId == 0)
5e27df78 172 {
173 ReleaseDC (aWindow, aWindowDC);
174
175 TCollection_AsciiString aMsg ("OpenGl_Window::CreateWindow: ChoosePixelFormat failed. Error code: ");
176 aMsg += (int )GetLastError();
177 Aspect_GraphicDeviceDefinitionError::Raise (aMsg.ToCString());
178 return;
179 }
180
abe46077 181 DescribePixelFormat (aWindowDC, aPixelFrmtId, sizeof(aPixelFrmt), &aPixelFrmt);
3d8969b1 182 if (aPixelFrmt.dwFlags & PFD_NEED_PALETTE)
5e27df78 183 {
3d8969b1 184 WINDOW_DATA* aWndData = (WINDOW_DATA* )GetWindowLongPtr (aWindow, GWLP_USERDATA);
5e27df78 185
3d8969b1 186 mySysPalInUse = (aPixelFrmt.dwFlags & PFD_NEED_SYSTEM_PALETTE) ? TRUE : FALSE;
187 InterfaceGraphic_RealizePalette (aWindowDC, aWndData->hPal, FALSE, mySysPalInUse);
5e27df78 188 }
189
190 if (myDither)
3d8969b1 191 {
192 myDither = (aPixelFrmt.cColorBits <= 8);
193 }
5e27df78 194
195 if (myBackDither)
3d8969b1 196 {
197 myBackDither = (aPixelFrmt.cColorBits <= 8);
198 }
5e27df78 199
58655684 200 HGLRC aSlaveCtx = !theShareCtx.IsNull() ? (HGLRC )theShareCtx->myGContext : NULL;
5e27df78 201 if (aGContext == NULL)
202 {
58655684 203 // create temporary context to retrieve advanced context creation procedures
204 HMODULE aModule = GetModuleHandleW(NULL);
205 WNDCLASSW aClass; memset (&aClass, 0, sizeof(aClass));
206 aClass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
207 aClass.lpfnWndProc = wndProcDummy;
208 aClass.hInstance = aModule;
209 aClass.lpszClassName = L"OpenGl_WindowTmp";
210 HWND aWinTmp = NULL;
211 HDC aDevCtxTmp = NULL;
212 HGLRC aRendCtxTmp = NULL;
abe46077 213 if ((!theCaps->contextDebug && !theCaps->contextNoAccel)
58655684 214 || RegisterClassW (&aClass) == 0)
215 {
216 aClass.lpszClassName = NULL;
217 }
218 if (aClass.lpszClassName != NULL)
219 {
229c0b6a 220 DWORD anExStyle = WS_EX_TOOLWINDOW | WS_EX_WINDOWEDGE;
221 #if (_WIN32_WINNT >= 0x0500)
222 anExStyle |= WS_EX_NOACTIVATE;
223 #endif
224 aWinTmp = CreateWindowExW(anExStyle,
58655684 225 aClass.lpszClassName, L"OpenGl_WindowTmp",
226 WS_POPUP | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_DISABLED,
227 2, 2, 4, 4,
228 NULL, NULL, aModule, NULL);
229 }
230 if (aWinTmp != NULL)
231 {
232 aDevCtxTmp = GetDC (aWinTmp);
233 SetPixelFormat (aDevCtxTmp, aPixelFrmtId, &aPixelFrmt);
234 aRendCtxTmp = wglCreateContext (aDevCtxTmp);
235 }
abe46077 236
237 typedef BOOL (WINAPI *wglChoosePixelFormatARB_t)(HDC theDevCtx,
238 const int* theIntAttribs,
239 const float* theFloatAttribs,
240 unsigned int theMaxFormats,
241 int* theFormatsOut,
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;
58655684 248 if (aRendCtxTmp != NULL)
249 {
250 wglMakeCurrent (aDevCtxTmp, aRendCtxTmp);
251
252 typedef const char* (WINAPI *wglGetExtensionsStringARB_t)(HDC theDeviceContext);
abe46077 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"))
256 {
257 aChoosePixProc = (wglChoosePixelFormatARB_t )wglGetProcAddress ("wglChoosePixelFormatARB");
258 }
259 if (OpenGl_Context::CheckExtension (aWglExts, "WGL_ARB_create_context_profile"))
260 {
261 aCreateCtxProc = (wglCreateContextAttribsARB_t )wglGetProcAddress ("wglCreateContextAttribsARB");
262 }
263 }
264
265 // choose extended pixel format
266 if (aChoosePixProc != NULL)
267 {
268 const int aPixAttribs[] =
269 {
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,
281 0, 0,
282 };
283 unsigned int aFrmtsNb = 0;
284 aChoosePixProc (aWindowDC, aPixAttribs, NULL, 1, &aPixelFrmtId, &aFrmtsNb);
285 }
286
287 // setup pixel format - may be set only once per window
288 if (!SetPixelFormat (aWindowDC, aPixelFrmtId, &aPixelFrmt))
289 {
290 ReleaseDC (aWindow, aWindowDC);
291
292 TCollection_AsciiString aMsg("OpenGl_Window::CreateWindow: SetPixelFormat failed. Error code: ");
293 aMsg += (int )GetLastError();
294 Aspect_GraphicDeviceDefinitionError::Raise (aMsg.ToCString());
295 return;
296 }
297
298 // create GL context with extra options
299 if (aCreateCtxProc != NULL)
300 {
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.
303 int aCtxAttribs[] =
304 {
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,
309 0, 0
310 };
311
312 aGContext = aCreateCtxProc (aWindowDC, aSlaveCtx, aCtxAttribs);
313 if (aGContext != NULL)
58655684 314 {
abe46077 315 aSlaveCtx = NULL;
58655684 316 }
317 }
318
319 if (aRendCtxTmp != NULL)
320 {
321 wglDeleteContext (aRendCtxTmp);
322 }
323 if (aDevCtxTmp != NULL)
324 {
325 ReleaseDC (aWinTmp, aDevCtxTmp);
326 }
327 if (aWinTmp != NULL)
328 {
329 DestroyWindow (aWinTmp);
330 }
331 if (aClass.lpszClassName != NULL)
332 {
333 UnregisterClassW (aClass.lpszClassName, aModule);
334 }
335
336 if (aGContext == NULL)
337 {
abe46077 338 // create context using obsolete functionality
58655684 339 aGContext = wglCreateContext (aWindowDC);
340 }
5e27df78 341 if (aGContext == NULL)
342 {
343 ReleaseDC (aWindow, aWindowDC);
344
345 TCollection_AsciiString aMsg ("OpenGl_Window::CreateWindow: wglCreateContext failed. Error code: ");
346 aMsg += (int )GetLastError();
347 Aspect_GraphicDeviceDefinitionError::Raise (aMsg.ToCString());
348 return;
349 }
350 }
351
352 // all GL context within one OpenGl_GraphicDriver should be shared!
58655684 353 if (aSlaveCtx != NULL && wglShareLists (aSlaveCtx, aGContext) != TRUE)
5e27df78 354 {
355 TCollection_AsciiString aMsg ("OpenGl_Window::CreateWindow: wglShareLists failed. Error code: ");
356 aMsg += (int )GetLastError();
357 Aspect_GraphicDeviceDefinitionError::Raise (aMsg.ToCString());
358 return;
359 }
360
361 myGlContext->Init ((Aspect_Handle )aWindow, (Aspect_Handle )aWindowDC, (Aspect_RenderingContext )aGContext);
362#else
2166f0fa 363 WINDOW aParent = (WINDOW )theCWindow.XWindow;
5e27df78 364 WINDOW aWindow = 0;
2166f0fa 365 DISPLAY* aDisp = (DISPLAY* )myDisplay->GetDisplay();
5e27df78 366 GLXContext aGContext = (GLXContext )theGContext;
2166f0fa 367
2166f0fa
SK
368 XWindowAttributes wattr;
369 XGetWindowAttributes (aDisp, aParent, &wattr);
370 const int scr = DefaultScreen (aDisp);
371
372 XVisualInfo* aVis = NULL;
373 {
374 unsigned long aVisInfoMask = VisualIDMask | VisualScreenMask;
375 XVisualInfo aVisInfo;
376 aVisInfo.visualid = wattr.visual->visualid;
377 aVisInfo.screen = scr;
378 int aNbItems;
379 aVis = XGetVisualInfo (aDisp, aVisInfoMask, &aVisInfo, &aNbItems);
380 }
381
2166f0fa
SK
382 if (!myOwnGContext)
383 {
384 if (aVis != NULL)
385 {
386 Aspect_GraphicDeviceDefinitionError::Raise ("OpenGl_Window::CreateWindow: XGetVisualInfo failed.");
387 return;
388 }
389
5e27df78 390 aWindow = aParent;
2166f0fa
SK
391 }
392 else
393 {
cdc56cc8 394 #if defined(__linux) || defined(Linux) || defined(__APPLE__)
2166f0fa
SK
395 if (aVis != NULL)
396 {
397 // check Visual for OpenGl context's parameters compability
3d8969b1 398 int isGl = 0, isDoubleBuffer = 0, isRGBA = 0, aDepthSize = 0, aStencilSize = 0;
2166f0fa
SK
399
400 if (glXGetConfig (aDisp, aVis, GLX_USE_GL, &isGl) != 0)
401 isGl = 0;
402
403 if (glXGetConfig (aDisp, aVis, GLX_RGBA, &isRGBA) != 0)
404 isRGBA = 0;
405
406 if (glXGetConfig (aDisp, aVis, GLX_DOUBLEBUFFER, &isDoubleBuffer) != 0)
407 isDoubleBuffer = 0;
408
409 if (glXGetConfig (aDisp, aVis, GLX_DEPTH_SIZE, &aDepthSize) != 0)
410 aDepthSize = 0;
411
3d8969b1 412 if (glXGetConfig (aDisp, aVis, GLX_STENCIL_SIZE, &aStencilSize) != 0)
413 aStencilSize = 0;
414
a6eb515f 415 if (!isGl || !aDepthSize || !aStencilSize || !isRGBA || (isDoubleBuffer ? 1 : 0) != (myDisplay->DBuffer()? 1 : 0))
2166f0fa
SK
416 {
417 XFree (aVis);
418 aVis = NULL;
419 }
420 }
421 #endif
422
423 if (aVis == NULL)
424 {
425 int anIter = 0;
3d8969b1 426 int anAttribs[13];
2166f0fa
SK
427 anAttribs[anIter++] = GLX_RGBA;
428
429 anAttribs[anIter++] = GLX_DEPTH_SIZE;
430 anAttribs[anIter++] = 1;
431
3d8969b1 432 anAttribs[anIter++] = GLX_STENCIL_SIZE;
433 anAttribs[anIter++] = 1;
434
2166f0fa
SK
435 anAttribs[anIter++] = GLX_RED_SIZE;
436 anAttribs[anIter++] = (wattr.depth <= 8) ? 0 : 1;
437
438 anAttribs[anIter++] = GLX_GREEN_SIZE;
439 anAttribs[anIter++] = (wattr.depth <= 8) ? 0 : 1;
440
441 anAttribs[anIter++] = GLX_BLUE_SIZE;
442 anAttribs[anIter++] = (wattr.depth <= 8) ? 0 : 1;
443
444 if (myDisplay->DBuffer())
445 anAttribs[anIter++] = GLX_DOUBLEBUFFER;
446
447 anAttribs[anIter++] = None;
448
449 aVis = glXChooseVisual (aDisp, scr, anAttribs);
450 if (aVis == NULL)
451 {
452 Aspect_GraphicDeviceDefinitionError::Raise ("OpenGl_Window::CreateWindow: glXChooseVisual failed.");
453 return;
454 }
455 }
456
5e27df78 457 if (!theShareCtx.IsNull())
2166f0fa 458 {
5e27df78 459 // ctx est une copie du previous
460 aGContext = glXCreateContext (aDisp, aVis, (GLXContext )theShareCtx->myGContext, GL_TRUE);
2166f0fa
SK
461 }
462 else
463 {
5e27df78 464 aGContext = glXCreateContext (aDisp, aVis, NULL, GL_TRUE);
2166f0fa
SK
465 }
466
5e27df78 467 if (!aGContext)
2166f0fa
SK
468 {
469 Aspect_GraphicDeviceDefinitionError::Raise ("OpenGl_Window::CreateWindow: glXCreateContext failed.");
470 return;
471 }
472
2166f0fa
SK
473 Colormap cmap = XCreateColormap (aDisp, aParent, aVis->visual, AllocNone);
474
475 XColor color;
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);
481
482 XSetWindowAttributes cwa;
483 cwa.colormap = cmap;
484 cwa.event_mask = StructureNotifyMask;
485 cwa.border_pixel = color.pixel;
486 cwa.background_pixel = color.pixel;
487
488 if (aVis->visualid == wattr.visual->visualid)
489 {
5e27df78 490 aWindow = aParent;
2166f0fa
SK
491 }
492 else
493 {
494 unsigned long mask = CWBackPixel | CWColormap | CWBorderPixel | CWEventMask;
5e27df78 495 aWindow = XCreateWindow (aDisp, aParent, 0, 0, myWidth, myHeight, 0/*bw*/, aVis->depth, InputOutput, aVis->visual, mask, &cwa);
2166f0fa
SK
496 }
497
5e27df78 498 XSetWindowBackground (aDisp, aWindow, cwa.background_pixel);
499 XClearWindow (aDisp, aWindow);
2166f0fa 500
5e27df78 501 if (aWindow != aParent)
2166f0fa
SK
502 {
503 XEvent anEvent;
5e27df78 504 XMapWindow (aDisp, aWindow);
505 XIfEvent (aDisp, &anEvent, WaitForNotify, (char* )aWindow);
2166f0fa 506 }
2166f0fa
SK
507 }
508
509 /*
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.
514 *
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)
520 */
521
5e27df78 522 int aValue;
523 glXGetConfig (aDisp, aVis, GLX_RED_SIZE, &aValue);
2166f0fa
SK
524
525 if (myDither)
5e27df78 526 myDither = (aValue < 8);
2166f0fa
SK
527
528 if (myBackDither)
529 myBackDither = (aVis->depth <= 8);
530
531 XFree ((char* )aVis);
532
5e27df78 533 myGlContext->Init ((Aspect_Drawable )aWindow, (Aspect_Display )myDisplay->GetDisplay(), (Aspect_RenderingContext )aGContext);
2166f0fa 534#endif
5e27df78 535 myGlContext->Share (theShareCtx);
2166f0fa
SK
536
537 Init();
2166f0fa
SK
538}
539
540// =======================================================================
541// function : ~OpenGl_Window
542// purpose :
543// =======================================================================
544OpenGl_Window::~OpenGl_Window()
545{
fd4a6963 546 if (!myOwnGContext)
547 {
548 myGlContext.Nullify();
549 return;
550 }
551
552 // release "GL" context if it is owned by window
58655684 553#if defined(_WIN32)
fd4a6963 554 HWND aWindow = (HWND )myGlContext->myWindow;
555 HDC aWindowDC = (HDC )myGlContext->myWindowDC;
556 HGLRC aWindowGContext = (HGLRC )myGlContext->myGContext;
557 HGLRC aThreadGlContext = wglGetCurrentContext();
5e27df78 558 myGlContext.Nullify();
2166f0fa 559
fd4a6963 560 if (aThreadGlContext != NULL)
2166f0fa 561 {
fd4a6963 562 // Mesa implementation can fail to reset this thread's context if wglDeleteContext()
563 // called without this step. This might lead to crash when using newly created
564 // context if wglMakeCurrent() is not forced right after the wglCreateContext().
565 if (aThreadGlContext == aWindowGContext)
2166f0fa 566 {
fd4a6963 567 wglMakeCurrent (NULL, NULL);
2166f0fa 568 }
fd4a6963 569
570 wglDeleteContext (aWindowGContext);
2166f0fa 571 }
fd4a6963 572 ReleaseDC (aWindow, aWindowDC);
5e27df78 573#else
5e27df78 574 Display* aDisplay = (Display* )myGlContext->myDisplay;
575 GLXContext aGContext = (GLXContext )myGlContext->myGContext;
576 myGlContext.Nullify();
577
fd4a6963 578 if (aDisplay != NULL)
2166f0fa 579 {
5e27df78 580 // FSXXX sync necessary if non-direct rendering
581 glXWaitGL();
582 glXDestroyContext (aDisplay, aGContext);
2166f0fa
SK
583 }
584#endif
585}
586
4fe56619 587#endif // !__APPLE__
588
2166f0fa
SK
589// =======================================================================
590// function : Activate
591// purpose :
592// =======================================================================
593Standard_Boolean OpenGl_Window::Activate()
594{
86fa64d9 595 return myGlContext->MakeCurrent();
2166f0fa
SK
596}
597
4fe56619 598#if !defined(__APPLE__) || defined(MACOSX_USE_GLX)
599
2166f0fa
SK
600// =======================================================================
601// function : Resize
602// purpose : call_subr_resize
603// =======================================================================
604void OpenGl_Window::Resize (const CALL_DEF_WINDOW& theCWindow)
605{
606 DISPLAY* aDisp = (DISPLAY* )myDisplay->GetDisplay();
607 if (aDisp == NULL)
608 return;
609
610 // If the size is not changed - do nothing
611 if ((myWidth == theCWindow.dx) && (myHeight == theCWindow.dy))
612 return;
613
614 myWidth = (Standard_Integer )theCWindow.dx;
615 myHeight = (Standard_Integer )theCWindow.dy;
616
58655684 617#if !defined(_WIN32)
5e27df78 618 XResizeWindow (aDisp, myGlContext->myWindow, (unsigned int )myWidth, (unsigned int )myHeight);
2166f0fa
SK
619 XSync (aDisp, False);
620#endif
621
622 Init();
623}
624
4fe56619 625#endif // !__APPLE__
626
2166f0fa
SK
627// =======================================================================
628// function : ReadDepths
629// purpose : TelReadDepths
630// =======================================================================
631void OpenGl_Window::ReadDepths (const Standard_Integer theX, const Standard_Integer theY,
632 const Standard_Integer theWidth, const Standard_Integer theHeight,
633 float* theDepths)
634{
635 if (theDepths == NULL || !Activate())
636 return;
637
638 glMatrixMode (GL_PROJECTION);
639 glLoadIdentity();
640 gluOrtho2D (0.0, (GLdouble )myWidth, 0.0, (GLdouble )myHeight);
641 glMatrixMode (GL_MODELVIEW);
642 glLoadIdentity();
643
644 glRasterPos2i (theX, theY);
645 DisableFeatures();
646 glReadPixels (theX, theY, theWidth, theHeight, GL_DEPTH_COMPONENT, GL_FLOAT, theDepths);
647 EnableFeatures();
648}
649
650// =======================================================================
651// function : SetBackgroundColor
652// purpose : call_subr_set_background
653// =======================================================================
654void OpenGl_Window::SetBackgroundColor (const Standard_ShortReal theR,
655 const Standard_ShortReal theG,
656 const Standard_ShortReal theB)
657{
658 myBgColor.rgb[0] = theR;
659 myBgColor.rgb[1] = theG;
660 myBgColor.rgb[2] = theB;
661}
662
4fe56619 663#if !defined(__APPLE__) || defined(MACOSX_USE_GLX)
664
2166f0fa
SK
665// =======================================================================
666// function : Init
667// purpose :
668// =======================================================================
669void OpenGl_Window::Init()
670{
671 if (!Activate())
672 return;
673
58655684 674#if defined(_WIN32)
2166f0fa 675 RECT cr;
5e27df78 676 GetClientRect ((HWND )myGlContext->myWindow, &cr);
2166f0fa
SK
677 myWidth = cr.right - cr.left;
678 myHeight = cr.bottom - cr.top;
679#else
680 Window aRootWin;
681 int aDummy;
682 unsigned int aDummyU;
683 unsigned int aNewWidth = 0;
684 unsigned int aNewHeight = 0;
685 DISPLAY* aDisp = (DISPLAY* )myDisplay->GetDisplay();
5e27df78 686 XGetGeometry (aDisp, myGlContext->myWindow, &aRootWin, &aDummy, &aDummy, &aNewWidth, &aNewHeight, &aDummyU, &aDummyU);
2166f0fa
SK
687 myWidth = aNewWidth;
688 myHeight = aNewHeight;
689#endif
690
691 glMatrixMode (GL_MODELVIEW);
692 glViewport (0, 0, myWidth, myHeight);
693
694 glDisable (GL_SCISSOR_TEST);
695 glDrawBuffer (GL_BACK);
696}
697
4fe56619 698#endif // !__APPLE__
699
2166f0fa
SK
700// =======================================================================
701// function : EnablePolygonOffset
702// purpose : call_subr_enable_polygon_offset
703// =======================================================================
704void OpenGl_Window::EnablePolygonOffset() const
705{
706 Standard_ShortReal aFactor, aUnits;
707 myDisplay->PolygonOffset (aFactor, aUnits);
708 glPolygonOffset (aFactor, aUnits);
709 glEnable (GL_POLYGON_OFFSET_FILL);
710}
711
712// =======================================================================
713// function : DisablePolygonOffset
714// purpose : call_subr_disable_polygon_offset
715// =======================================================================
716void OpenGl_Window::DisablePolygonOffset() const
717{
718 glDisable (GL_POLYGON_OFFSET_FILL);
719}
720
721// =======================================================================
722// function : EnableFeatures
723// purpose :
724// =======================================================================
725void OpenGl_Window::EnableFeatures() const
726{
727 /*glPixelTransferi (GL_MAP_COLOR, GL_TRUE);*/
728
729 if (myDither)
730 glEnable (GL_DITHER);
731 else
732 glDisable (GL_DITHER);
733}
734
735// =======================================================================
736// function : DisableFeatures
737// purpose :
738// =======================================================================
739void OpenGl_Window::DisableFeatures() const
740{
741 glDisable (GL_DITHER);
742 glPixelTransferi (GL_MAP_COLOR, GL_FALSE);
743
744 /*
745 * Disable stuff that's likely to slow down glDrawPixels.
746 * (Omit as much of this as possible, when you know in advance
747 * that the OpenGL state will already be set correctly.)
748 */
749 glDisable(GL_ALPHA_TEST);
750 glDisable(GL_BLEND);
751 glDisable(GL_DEPTH_TEST);
752 glDisable(GL_FOG);
753 glDisable(GL_LIGHTING);
754
755 glDisable(GL_LOGIC_OP);
756 glDisable(GL_STENCIL_TEST);
757 glDisable(GL_TEXTURE_1D);
758 glDisable(GL_TEXTURE_2D);
759 glPixelTransferi(GL_MAP_COLOR, GL_FALSE);
760 glPixelTransferi(GL_RED_SCALE, 1);
761 glPixelTransferi(GL_RED_BIAS, 0);
762 glPixelTransferi(GL_GREEN_SCALE, 1);
763 glPixelTransferi(GL_GREEN_BIAS, 0);
764 glPixelTransferi(GL_BLUE_SCALE, 1);
765 glPixelTransferi(GL_BLUE_BIAS, 0);
766 glPixelTransferi(GL_ALPHA_SCALE, 1);
767 glPixelTransferi(GL_ALPHA_BIAS, 0);
768
769 /*
770 * Disable extensions that could slow down glDrawPixels.
771 * (Actually, you should check for the presence of the proper
772 * extension before making these calls. I've omitted that
773 * code for simplicity.)
774 */
775
776#ifdef GL_EXT_convolution
777 glDisable(GL_CONVOLUTION_1D_EXT);
778 glDisable(GL_CONVOLUTION_2D_EXT);
779 glDisable(GL_SEPARABLE_2D_EXT);
780#endif
781
782#ifdef GL_EXT_histogram
783 glDisable(GL_HISTOGRAM_EXT);
784 glDisable(GL_MINMAX_EXT);
785#endif
786
787#ifdef GL_EXT_texture3D
788 glDisable(GL_TEXTURE_3D_EXT);
789#endif
790}
791
792// =======================================================================
793// function : MakeFrontBufCurrent
794// purpose : TelMakeFrontBufCurrent
795// =======================================================================
796void OpenGl_Window::MakeFrontBufCurrent() const
797{
798 glDrawBuffer (GL_FRONT);
799}
800
801// =======================================================================
802// function : MakeBackBufCurrent
803// purpose : TelMakeBackBufCurrent
804// =======================================================================
805void OpenGl_Window::MakeBackBufCurrent() const
806{
807 glDrawBuffer (GL_BACK);
808}
809
810// =======================================================================
811// function : MakeFrontAndBackBufCurrent
812// purpose : TelMakeFrontAndBackBufCurrent
813// =======================================================================
814void OpenGl_Window::MakeFrontAndBackBufCurrent() const
815{
816 glDrawBuffer (GL_FRONT_AND_BACK);
817}
5e27df78 818
819// =======================================================================
820// function : GetGContext
821// purpose :
822// =======================================================================
823GLCONTEXT OpenGl_Window::GetGContext() const
824{
825 return (GLCONTEXT )myGlContext->myGContext;
826}