0024198: Remove unused stuff in WNT package
[occt.git] / src / OpenGl / OpenGl_Window.cxx
CommitLineData
b311480e 1// Created on: 2011-09-20
2// Created by: Sergey ZERCHANINOV
3// Copyright (c) 2011-2012 OPEN CASCADE SAS
4//
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.
9//
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.
12//
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.
19
cfd5fa01 20#include <OpenGl_GlCore11.hxx>
21
2166f0fa
SK
22#include <InterfaceGraphic.hxx>
23
24#include <OpenGl_Window.hxx>
25
26#include <OpenGl_Context.hxx>
27#include <OpenGl_Display.hxx>
2166f0fa
SK
28
29#include <Aspect_GraphicDeviceDefinitionError.hxx>
30#include <TCollection_AsciiString.hxx>
31
32IMPLEMENT_STANDARD_HANDLE(OpenGl_Window,MMgt_TShared)
33IMPLEMENT_STANDARD_RTTIEXT(OpenGl_Window,MMgt_TShared)
34
4fe56619 35#if !defined(__APPLE__) || defined(MACOSX_USE_GLX)
36
2166f0fa
SK
37namespace
38{
39 static const TEL_COLOUR THE_DEFAULT_BG_COLOR = { { 0.F, 0.F, 0.F, 1.F } };
40
58655684 41#if defined(_WIN32)
42
43 // WGL_ARB_create_context_profile
44#ifndef WGL_CONTEXT_MAJOR_VERSION_ARB
45 #define WGL_CONTEXT_MAJOR_VERSION_ARB 0x2091
46 #define WGL_CONTEXT_MINOR_VERSION_ARB 0x2092
47 #define WGL_CONTEXT_LAYER_PLANE_ARB 0x2093
48 #define WGL_CONTEXT_FLAGS_ARB 0x2094
49 #define WGL_CONTEXT_PROFILE_MASK_ARB 0x9126
50
51 // WGL_CONTEXT_FLAGS bits
52 #define WGL_CONTEXT_DEBUG_BIT_ARB 0x0001
53 #define WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB 0x0002
54
55 // WGL_CONTEXT_PROFILE_MASK_ARB bits
56 #define WGL_CONTEXT_CORE_PROFILE_BIT_ARB 0x00000001
57 #define WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB 0x00000002
58#endif // WGL_CONTEXT_MAJOR_VERSION_ARB
59
60 static LRESULT CALLBACK wndProcDummy (HWND theWin, UINT theMsg, WPARAM theParamW, LPARAM theParamL)
61 {
62 return DefWindowProcW (theWin, theMsg, theParamW, theParamL);
63 }
64
3d8969b1 65 static int find_pixel_format (HDC theDevCtx,
66 PIXELFORMATDESCRIPTOR& thePixelFrmt,
67 const Standard_Boolean theIsDoubleBuff)
2166f0fa 68 {
3d8969b1 69 PIXELFORMATDESCRIPTOR aPixelFrmtTmp;
70 memset (&aPixelFrmtTmp, 0, sizeof (PIXELFORMATDESCRIPTOR));
71 aPixelFrmtTmp.nSize = sizeof (PIXELFORMATDESCRIPTOR);
72 aPixelFrmtTmp.nVersion = 1;
73 aPixelFrmtTmp.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | (theIsDoubleBuff ? PFD_DOUBLEBUFFER : PFD_SUPPORT_GDI);
74 aPixelFrmtTmp.iPixelType = PFD_TYPE_RGBA;
75 aPixelFrmtTmp.iLayerType = PFD_MAIN_PLANE;
76
77 const int BUFF_BITS_STENCIL[] = { 8, 1 };
78 const int BUFF_BITS_COLOR[] = { 32, 24 };
79 const int BUFF_BITS_DEPTH[] = { 32, 24, 16 };
80
81 int aGoodBits[] = { 0, 0, 0 };
82 int aPixelFrmtIdLast = 0;
83 int aPixelFrmtIdGood = 0;
84 Standard_Size aStencilIter = 0, aColorIter = 0, aDepthIter = 0;
85 for (aStencilIter = 0; aStencilIter < sizeof(BUFF_BITS_STENCIL) / sizeof(int); ++aStencilIter)
2166f0fa 86 {
8263fcd3 87 aPixelFrmtTmp.cStencilBits = (BYTE)(BUFF_BITS_STENCIL[aStencilIter]);
3d8969b1 88 for (aDepthIter = 0; aDepthIter < sizeof(BUFF_BITS_DEPTH) / sizeof(int); ++aDepthIter)
2166f0fa 89 {
8263fcd3 90 aPixelFrmtTmp.cDepthBits = (BYTE)(BUFF_BITS_DEPTH[aDepthIter]);
3d8969b1 91 aPixelFrmtIdGood = 0;
92 for (aColorIter = 0; aColorIter < sizeof(BUFF_BITS_COLOR) / sizeof(int); ++aColorIter)
2166f0fa 93 {
8263fcd3 94 aPixelFrmtTmp.cColorBits = (BYTE)(BUFF_BITS_COLOR[aColorIter]);
3d8969b1 95 aPixelFrmtIdLast = ChoosePixelFormat (theDevCtx, &aPixelFrmtTmp);
96 if (aPixelFrmtIdLast == 0)
97 {
98 continue;
99 }
100
101 thePixelFrmt.cDepthBits = 0;
102 thePixelFrmt.cColorBits = 0;
103 thePixelFrmt.cStencilBits = 0;
104 DescribePixelFormat (theDevCtx, aPixelFrmtIdLast, sizeof(PIXELFORMATDESCRIPTOR), &thePixelFrmt);
105 if (thePixelFrmt.cColorBits >= BUFF_BITS_COLOR[aColorIter]
106 && thePixelFrmt.cDepthBits >= BUFF_BITS_DEPTH[aDepthIter]
107 && thePixelFrmt.cStencilBits >= BUFF_BITS_STENCIL[aStencilIter])
108 {
2166f0fa 109 break;
3d8969b1 110 }
111 if (thePixelFrmt.cColorBits > aGoodBits[0])
112 {
113 aGoodBits[0] = thePixelFrmt.cColorBits;
114 aGoodBits[1] = thePixelFrmt.cDepthBits;
115 aGoodBits[2] = thePixelFrmt.cStencilBits;
116 aPixelFrmtIdGood = aPixelFrmtIdLast;
117 }
118 else if (thePixelFrmt.cColorBits == aGoodBits[0])
119 {
120 if (thePixelFrmt.cDepthBits > aGoodBits[1])
121 {
122 aGoodBits[1] = thePixelFrmt.cDepthBits;
123 aGoodBits[2] = thePixelFrmt.cStencilBits;
124 aPixelFrmtIdGood = aPixelFrmtIdLast;
125 }
126 else if (thePixelFrmt.cDepthBits == aGoodBits[1])
127 {
128 if(thePixelFrmt.cStencilBits > aGoodBits[2])
129 {
130 aGoodBits[2] = thePixelFrmt.cStencilBits;
131 aPixelFrmtIdGood = aPixelFrmtIdLast;
132 }
133 }
134 }
135 }
136 if (aColorIter < sizeof(BUFF_BITS_COLOR) / sizeof(int))
137 {
138 break;
2166f0fa
SK
139 }
140 }
3d8969b1 141 if (aDepthIter < sizeof(BUFF_BITS_DEPTH) / sizeof(int))
142 {
2166f0fa 143 break;
3d8969b1 144 }
2166f0fa
SK
145 }
146
3d8969b1 147 return (aPixelFrmtIdLast == 0) ? aPixelFrmtIdGood : aPixelFrmtIdLast;
2166f0fa
SK
148 }
149#else
150 static Bool WaitForNotify (Display* theDisp, XEvent* theEv, char* theArg)
151 {
152 return (theEv->type == MapNotify) && (theEv->xmap.window == (Window )theArg);
153 }
154#endif
155
156};
157
158// =======================================================================
159// function : OpenGl_Window
160// purpose :
161// =======================================================================
162OpenGl_Window::OpenGl_Window (const Handle(OpenGl_Display)& theDisplay,
163 const CALL_DEF_WINDOW& theCWindow,
5e27df78 164 Aspect_RenderingContext theGContext,
58655684 165 const Handle(OpenGl_Caps)& theCaps,
5e27df78 166 const Handle(OpenGl_Context)& theShareCtx)
2166f0fa 167: myDisplay (theDisplay),
58655684 168 myGlContext (new OpenGl_Context (theCaps)),
2166f0fa 169 myOwnGContext (theGContext == 0),
58655684 170#if defined(_WIN32)
2166f0fa
SK
171 mySysPalInUse (FALSE),
172#endif
173 myWidth ((Standard_Integer )theCWindow.dx),
174 myHeight ((Standard_Integer )theCWindow.dy),
175 myBgColor (THE_DEFAULT_BG_COLOR),
176 myDither (theDisplay->Dither()),
177 myBackDither (theDisplay->BackDither())
178{
179 myBgColor.rgb[0] = theCWindow.Background.r;
180 myBgColor.rgb[1] = theCWindow.Background.g;
181 myBgColor.rgb[2] = theCWindow.Background.b;
182
58655684 183#if defined(_WIN32)
5e27df78 184 HWND aWindow = (HWND )theCWindow.XWindow;
185 HDC aWindowDC = GetDC (aWindow);
186 HGLRC aGContext = (HGLRC )theGContext;
187
3d8969b1 188 PIXELFORMATDESCRIPTOR aPixelFrmt;
189 const int aPixelFrmtId = find_pixel_format (aWindowDC, aPixelFrmt, myDisplay->DBuffer());
190 if (aPixelFrmtId == 0)
5e27df78 191 {
192 ReleaseDC (aWindow, aWindowDC);
193
194 TCollection_AsciiString aMsg ("OpenGl_Window::CreateWindow: ChoosePixelFormat failed. Error code: ");
195 aMsg += (int )GetLastError();
196 Aspect_GraphicDeviceDefinitionError::Raise (aMsg.ToCString());
197 return;
198 }
199
3d8969b1 200 if (aPixelFrmt.dwFlags & PFD_NEED_PALETTE)
5e27df78 201 {
3d8969b1 202 WINDOW_DATA* aWndData = (WINDOW_DATA* )GetWindowLongPtr (aWindow, GWLP_USERDATA);
5e27df78 203
3d8969b1 204 mySysPalInUse = (aPixelFrmt.dwFlags & PFD_NEED_SYSTEM_PALETTE) ? TRUE : FALSE;
205 InterfaceGraphic_RealizePalette (aWindowDC, aWndData->hPal, FALSE, mySysPalInUse);
5e27df78 206 }
207
208 if (myDither)
3d8969b1 209 {
210 myDither = (aPixelFrmt.cColorBits <= 8);
211 }
5e27df78 212
213 if (myBackDither)
3d8969b1 214 {
215 myBackDither = (aPixelFrmt.cColorBits <= 8);
216 }
5e27df78 217
3d8969b1 218 if (!SetPixelFormat (aWindowDC, aPixelFrmtId, &aPixelFrmt))
5e27df78 219 {
220 ReleaseDC (aWindow, aWindowDC);
221
222 TCollection_AsciiString aMsg("OpenGl_Window::CreateWindow: SetPixelFormat failed. Error code: ");
223 aMsg += (int )GetLastError();
224 Aspect_GraphicDeviceDefinitionError::Raise (aMsg.ToCString());
225 return;
226 }
227
58655684 228 HGLRC aSlaveCtx = !theShareCtx.IsNull() ? (HGLRC )theShareCtx->myGContext : NULL;
5e27df78 229 if (aGContext == NULL)
230 {
58655684 231 // create temporary context to retrieve advanced context creation procedures
232 HMODULE aModule = GetModuleHandleW(NULL);
233 WNDCLASSW aClass; memset (&aClass, 0, sizeof(aClass));
234 aClass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
235 aClass.lpfnWndProc = wndProcDummy;
236 aClass.hInstance = aModule;
237 aClass.lpszClassName = L"OpenGl_WindowTmp";
238 HWND aWinTmp = NULL;
239 HDC aDevCtxTmp = NULL;
240 HGLRC aRendCtxTmp = NULL;
241 if (!theCaps->contextDebug
242 || RegisterClassW (&aClass) == 0)
243 {
244 aClass.lpszClassName = NULL;
245 }
246 if (aClass.lpszClassName != NULL)
247 {
248 aWinTmp = CreateWindowExW(WS_EX_TOOLWINDOW | WS_EX_WINDOWEDGE | WS_EX_NOACTIVATE,
249 aClass.lpszClassName, L"OpenGl_WindowTmp",
250 WS_POPUP | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_DISABLED,
251 2, 2, 4, 4,
252 NULL, NULL, aModule, NULL);
253 }
254 if (aWinTmp != NULL)
255 {
256 aDevCtxTmp = GetDC (aWinTmp);
257 SetPixelFormat (aDevCtxTmp, aPixelFrmtId, &aPixelFrmt);
258 aRendCtxTmp = wglCreateContext (aDevCtxTmp);
259 }
260 if (aRendCtxTmp != NULL)
261 {
262 wglMakeCurrent (aDevCtxTmp, aRendCtxTmp);
263
264 typedef const char* (WINAPI *wglGetExtensionsStringARB_t)(HDC theDeviceContext);
265 typedef HGLRC (WINAPI *wglCreateContextAttribsARB_t)(HDC theDevCtx,
266 HGLRC theShareContext,
267 const int* theAttribs);
268 wglGetExtensionsStringARB_t aGetExtensions = (wglGetExtensionsStringARB_t )wglGetProcAddress ("wglGetExtensionsStringARB");
269 wglCreateContextAttribsARB_t aCreateCtxProc = (wglCreateContextAttribsARB_t )wglGetProcAddress ("wglCreateContextAttribsARB");
270 const char* aWglExts = aGetExtensions (wglGetCurrentDC());
271 if (aCreateCtxProc != NULL
272 && OpenGl_Context::CheckExtension (aWglExts, "WGL_ARB_create_context_profile"))
273 {
274 // Beware! NVIDIA drivers reject context creation when WGL_CONTEXT_PROFILE_MASK_ARB are specified
275 // but not WGL_CONTEXT_MAJOR_VERSION_ARB/WGL_CONTEXT_MINOR_VERSION_ARB.
276 int aCtxAttribs[] =
277 {
278 //WGL_CONTEXT_MAJOR_VERSION_ARB, 3,
279 //WGL_CONTEXT_MINOR_VERSION_ARB, 2,
280 //WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB, //WGL_CONTEXT_CORE_PROFILE_BIT_ARB,
281 WGL_CONTEXT_FLAGS_ARB, theCaps->contextDebug ? WGL_CONTEXT_DEBUG_BIT_ARB : 0,
282 0, 0
283 };
284
285 aGContext = aCreateCtxProc (aWindowDC, aSlaveCtx, aCtxAttribs);
286 if (aGContext != NULL)
287 {
288 aSlaveCtx = NULL;
289 }
290 }
291 }
292
293 if (aRendCtxTmp != NULL)
294 {
295 wglDeleteContext (aRendCtxTmp);
296 }
297 if (aDevCtxTmp != NULL)
298 {
299 ReleaseDC (aWinTmp, aDevCtxTmp);
300 }
301 if (aWinTmp != NULL)
302 {
303 DestroyWindow (aWinTmp);
304 }
305 if (aClass.lpszClassName != NULL)
306 {
307 UnregisterClassW (aClass.lpszClassName, aModule);
308 }
309
310 if (aGContext == NULL)
311 {
312 aGContext = wglCreateContext (aWindowDC);
313 }
5e27df78 314 if (aGContext == NULL)
315 {
316 ReleaseDC (aWindow, aWindowDC);
317
318 TCollection_AsciiString aMsg ("OpenGl_Window::CreateWindow: wglCreateContext failed. Error code: ");
319 aMsg += (int )GetLastError();
320 Aspect_GraphicDeviceDefinitionError::Raise (aMsg.ToCString());
321 return;
322 }
323 }
324
325 // all GL context within one OpenGl_GraphicDriver should be shared!
58655684 326 if (aSlaveCtx != NULL && wglShareLists (aSlaveCtx, aGContext) != TRUE)
5e27df78 327 {
328 TCollection_AsciiString aMsg ("OpenGl_Window::CreateWindow: wglShareLists failed. Error code: ");
329 aMsg += (int )GetLastError();
330 Aspect_GraphicDeviceDefinitionError::Raise (aMsg.ToCString());
331 return;
332 }
333
334 myGlContext->Init ((Aspect_Handle )aWindow, (Aspect_Handle )aWindowDC, (Aspect_RenderingContext )aGContext);
335#else
2166f0fa 336 WINDOW aParent = (WINDOW )theCWindow.XWindow;
5e27df78 337 WINDOW aWindow = 0;
2166f0fa 338 DISPLAY* aDisp = (DISPLAY* )myDisplay->GetDisplay();
5e27df78 339 GLXContext aGContext = (GLXContext )theGContext;
2166f0fa 340
2166f0fa
SK
341 XWindowAttributes wattr;
342 XGetWindowAttributes (aDisp, aParent, &wattr);
343 const int scr = DefaultScreen (aDisp);
344
345 XVisualInfo* aVis = NULL;
346 {
347 unsigned long aVisInfoMask = VisualIDMask | VisualScreenMask;
348 XVisualInfo aVisInfo;
349 aVisInfo.visualid = wattr.visual->visualid;
350 aVisInfo.screen = scr;
351 int aNbItems;
352 aVis = XGetVisualInfo (aDisp, aVisInfoMask, &aVisInfo, &aNbItems);
353 }
354
2166f0fa
SK
355 if (!myOwnGContext)
356 {
357 if (aVis != NULL)
358 {
359 Aspect_GraphicDeviceDefinitionError::Raise ("OpenGl_Window::CreateWindow: XGetVisualInfo failed.");
360 return;
361 }
362
5e27df78 363 aWindow = aParent;
2166f0fa
SK
364 }
365 else
366 {
cdc56cc8 367 #if defined(__linux) || defined(Linux) || defined(__APPLE__)
2166f0fa
SK
368 if (aVis != NULL)
369 {
370 // check Visual for OpenGl context's parameters compability
3d8969b1 371 int isGl = 0, isDoubleBuffer = 0, isRGBA = 0, aDepthSize = 0, aStencilSize = 0;
2166f0fa
SK
372
373 if (glXGetConfig (aDisp, aVis, GLX_USE_GL, &isGl) != 0)
374 isGl = 0;
375
376 if (glXGetConfig (aDisp, aVis, GLX_RGBA, &isRGBA) != 0)
377 isRGBA = 0;
378
379 if (glXGetConfig (aDisp, aVis, GLX_DOUBLEBUFFER, &isDoubleBuffer) != 0)
380 isDoubleBuffer = 0;
381
382 if (glXGetConfig (aDisp, aVis, GLX_DEPTH_SIZE, &aDepthSize) != 0)
383 aDepthSize = 0;
384
3d8969b1 385 if (glXGetConfig (aDisp, aVis, GLX_STENCIL_SIZE, &aStencilSize) != 0)
386 aStencilSize = 0;
387
2166f0fa
SK
388 if (!isGl || !aDepthSize || !isRGBA || (isDoubleBuffer ? 1 : 0) != (myDisplay->DBuffer()? 1 : 0))
389 {
390 XFree (aVis);
391 aVis = NULL;
392 }
393 }
394 #endif
395
396 if (aVis == NULL)
397 {
398 int anIter = 0;
3d8969b1 399 int anAttribs[13];
2166f0fa
SK
400 anAttribs[anIter++] = GLX_RGBA;
401
402 anAttribs[anIter++] = GLX_DEPTH_SIZE;
403 anAttribs[anIter++] = 1;
404
3d8969b1 405 anAttribs[anIter++] = GLX_STENCIL_SIZE;
406 anAttribs[anIter++] = 1;
407
2166f0fa
SK
408 anAttribs[anIter++] = GLX_RED_SIZE;
409 anAttribs[anIter++] = (wattr.depth <= 8) ? 0 : 1;
410
411 anAttribs[anIter++] = GLX_GREEN_SIZE;
412 anAttribs[anIter++] = (wattr.depth <= 8) ? 0 : 1;
413
414 anAttribs[anIter++] = GLX_BLUE_SIZE;
415 anAttribs[anIter++] = (wattr.depth <= 8) ? 0 : 1;
416
417 if (myDisplay->DBuffer())
418 anAttribs[anIter++] = GLX_DOUBLEBUFFER;
419
420 anAttribs[anIter++] = None;
421
422 aVis = glXChooseVisual (aDisp, scr, anAttribs);
423 if (aVis == NULL)
424 {
425 Aspect_GraphicDeviceDefinitionError::Raise ("OpenGl_Window::CreateWindow: glXChooseVisual failed.");
426 return;
427 }
428 }
429
5e27df78 430 if (!theShareCtx.IsNull())
2166f0fa 431 {
5e27df78 432 // ctx est une copie du previous
433 aGContext = glXCreateContext (aDisp, aVis, (GLXContext )theShareCtx->myGContext, GL_TRUE);
2166f0fa
SK
434 }
435 else
436 {
5e27df78 437 aGContext = glXCreateContext (aDisp, aVis, NULL, GL_TRUE);
2166f0fa
SK
438 }
439
5e27df78 440 if (!aGContext)
2166f0fa
SK
441 {
442 Aspect_GraphicDeviceDefinitionError::Raise ("OpenGl_Window::CreateWindow: glXCreateContext failed.");
443 return;
444 }
445
2166f0fa
SK
446 Colormap cmap = XCreateColormap (aDisp, aParent, aVis->visual, AllocNone);
447
448 XColor color;
449 color.red = (unsigned short) (myBgColor.rgb[0] * 0xFFFF);
450 color.green = (unsigned short) (myBgColor.rgb[1] * 0xFFFF);
451 color.blue = (unsigned short) (myBgColor.rgb[2] * 0xFFFF);
452 color.flags = DoRed | DoGreen | DoBlue;
453 XAllocColor (aDisp, cmap, &color);
454
455 XSetWindowAttributes cwa;
456 cwa.colormap = cmap;
457 cwa.event_mask = StructureNotifyMask;
458 cwa.border_pixel = color.pixel;
459 cwa.background_pixel = color.pixel;
460
461 if (aVis->visualid == wattr.visual->visualid)
462 {
5e27df78 463 aWindow = aParent;
2166f0fa
SK
464 }
465 else
466 {
467 unsigned long mask = CWBackPixel | CWColormap | CWBorderPixel | CWEventMask;
5e27df78 468 aWindow = XCreateWindow (aDisp, aParent, 0, 0, myWidth, myHeight, 0/*bw*/, aVis->depth, InputOutput, aVis->visual, mask, &cwa);
2166f0fa
SK
469 }
470
5e27df78 471 XSetWindowBackground (aDisp, aWindow, cwa.background_pixel);
472 XClearWindow (aDisp, aWindow);
2166f0fa 473
5e27df78 474 if (aWindow != aParent)
2166f0fa
SK
475 {
476 XEvent anEvent;
5e27df78 477 XMapWindow (aDisp, aWindow);
478 XIfEvent (aDisp, &anEvent, WaitForNotify, (char* )aWindow);
2166f0fa 479 }
2166f0fa
SK
480 }
481
482 /*
483 * Le BackDitherProp est utilise pour le clear du background
484 * Pour eviter une difference de couleurs avec la couleur choisie
485 * par l'application (XWindow) il faut desactiver le dithering
486 * au dessus de 8 plans.
487 *
488 * Pour le DitherProp:
489 * On cherchera a activer le Dithering que si le Visual a au moins
490 * 8 plans pour le GLX_RED_SIZE. Le test est plus sur car on peut
491 * avoir une profondeur superieure a 12 mais avoir besoin du dithering.
492 * (Carte Impact avec GLX_RED_SIZE a 5 par exemple)
493 */
494
5e27df78 495 int aValue;
496 glXGetConfig (aDisp, aVis, GLX_RED_SIZE, &aValue);
2166f0fa
SK
497
498 if (myDither)
5e27df78 499 myDither = (aValue < 8);
2166f0fa
SK
500
501 if (myBackDither)
502 myBackDither = (aVis->depth <= 8);
503
504 XFree ((char* )aVis);
505
5e27df78 506 myGlContext->Init ((Aspect_Drawable )aWindow, (Aspect_Display )myDisplay->GetDisplay(), (Aspect_RenderingContext )aGContext);
2166f0fa 507#endif
5e27df78 508 myGlContext->Share (theShareCtx);
2166f0fa
SK
509
510 Init();
2166f0fa
SK
511}
512
513// =======================================================================
514// function : ~OpenGl_Window
515// purpose :
516// =======================================================================
517OpenGl_Window::~OpenGl_Window()
518{
58655684 519#if defined(_WIN32)
5e27df78 520 HWND aWindow = (HWND )myGlContext->myWindow;
521 HDC aWindowDC = (HDC )myGlContext->myWindowDC;
522 HGLRC aGContext = (HGLRC )myGlContext->myGContext;
523 myGlContext.Nullify();
2166f0fa 524
5e27df78 525 if (myOwnGContext)
2166f0fa 526 {
5e27df78 527 if (wglGetCurrentContext() != NULL)
2166f0fa 528 {
5e27df78 529 wglDeleteContext (aGContext);
2166f0fa 530 }
5e27df78 531 ReleaseDC (aWindow, aWindowDC);
2166f0fa 532 }
5e27df78 533#else
5e27df78 534 Display* aDisplay = (Display* )myGlContext->myDisplay;
535 GLXContext aGContext = (GLXContext )myGlContext->myGContext;
536 myGlContext.Nullify();
537
538 if (aDisplay != NULL && myOwnGContext)
2166f0fa 539 {
5e27df78 540 // FSXXX sync necessary if non-direct rendering
541 glXWaitGL();
542 glXDestroyContext (aDisplay, aGContext);
2166f0fa
SK
543 }
544#endif
545}
546
4fe56619 547#endif // !__APPLE__
548
2166f0fa
SK
549// =======================================================================
550// function : Activate
551// purpose :
552// =======================================================================
553Standard_Boolean OpenGl_Window::Activate()
554{
86fa64d9 555 return myGlContext->MakeCurrent();
2166f0fa
SK
556}
557
4fe56619 558#if !defined(__APPLE__) || defined(MACOSX_USE_GLX)
559
2166f0fa
SK
560// =======================================================================
561// function : Resize
562// purpose : call_subr_resize
563// =======================================================================
564void OpenGl_Window::Resize (const CALL_DEF_WINDOW& theCWindow)
565{
566 DISPLAY* aDisp = (DISPLAY* )myDisplay->GetDisplay();
567 if (aDisp == NULL)
568 return;
569
570 // If the size is not changed - do nothing
571 if ((myWidth == theCWindow.dx) && (myHeight == theCWindow.dy))
572 return;
573
574 myWidth = (Standard_Integer )theCWindow.dx;
575 myHeight = (Standard_Integer )theCWindow.dy;
576
58655684 577#if !defined(_WIN32)
5e27df78 578 XResizeWindow (aDisp, myGlContext->myWindow, (unsigned int )myWidth, (unsigned int )myHeight);
2166f0fa
SK
579 XSync (aDisp, False);
580#endif
581
582 Init();
583}
584
4fe56619 585#endif // !__APPLE__
586
2166f0fa
SK
587// =======================================================================
588// function : ReadDepths
589// purpose : TelReadDepths
590// =======================================================================
591void OpenGl_Window::ReadDepths (const Standard_Integer theX, const Standard_Integer theY,
592 const Standard_Integer theWidth, const Standard_Integer theHeight,
593 float* theDepths)
594{
595 if (theDepths == NULL || !Activate())
596 return;
597
598 glMatrixMode (GL_PROJECTION);
599 glLoadIdentity();
600 gluOrtho2D (0.0, (GLdouble )myWidth, 0.0, (GLdouble )myHeight);
601 glMatrixMode (GL_MODELVIEW);
602 glLoadIdentity();
603
604 glRasterPos2i (theX, theY);
605 DisableFeatures();
606 glReadPixels (theX, theY, theWidth, theHeight, GL_DEPTH_COMPONENT, GL_FLOAT, theDepths);
607 EnableFeatures();
608}
609
610// =======================================================================
611// function : SetBackgroundColor
612// purpose : call_subr_set_background
613// =======================================================================
614void OpenGl_Window::SetBackgroundColor (const Standard_ShortReal theR,
615 const Standard_ShortReal theG,
616 const Standard_ShortReal theB)
617{
618 myBgColor.rgb[0] = theR;
619 myBgColor.rgb[1] = theG;
620 myBgColor.rgb[2] = theB;
621}
622
4fe56619 623#if !defined(__APPLE__) || defined(MACOSX_USE_GLX)
624
2166f0fa
SK
625// =======================================================================
626// function : Init
627// purpose :
628// =======================================================================
629void OpenGl_Window::Init()
630{
631 if (!Activate())
632 return;
633
58655684 634#if defined(_WIN32)
2166f0fa 635 RECT cr;
5e27df78 636 GetClientRect ((HWND )myGlContext->myWindow, &cr);
2166f0fa
SK
637 myWidth = cr.right - cr.left;
638 myHeight = cr.bottom - cr.top;
639#else
640 Window aRootWin;
641 int aDummy;
642 unsigned int aDummyU;
643 unsigned int aNewWidth = 0;
644 unsigned int aNewHeight = 0;
645 DISPLAY* aDisp = (DISPLAY* )myDisplay->GetDisplay();
5e27df78 646 XGetGeometry (aDisp, myGlContext->myWindow, &aRootWin, &aDummy, &aDummy, &aNewWidth, &aNewHeight, &aDummyU, &aDummyU);
2166f0fa
SK
647 myWidth = aNewWidth;
648 myHeight = aNewHeight;
649#endif
650
651 glMatrixMode (GL_MODELVIEW);
652 glViewport (0, 0, myWidth, myHeight);
653
654 glDisable (GL_SCISSOR_TEST);
655 glDrawBuffer (GL_BACK);
656}
657
4fe56619 658#endif // !__APPLE__
659
2166f0fa
SK
660// =======================================================================
661// function : EnablePolygonOffset
662// purpose : call_subr_enable_polygon_offset
663// =======================================================================
664void OpenGl_Window::EnablePolygonOffset() const
665{
666 Standard_ShortReal aFactor, aUnits;
667 myDisplay->PolygonOffset (aFactor, aUnits);
668 glPolygonOffset (aFactor, aUnits);
669 glEnable (GL_POLYGON_OFFSET_FILL);
670}
671
672// =======================================================================
673// function : DisablePolygonOffset
674// purpose : call_subr_disable_polygon_offset
675// =======================================================================
676void OpenGl_Window::DisablePolygonOffset() const
677{
678 glDisable (GL_POLYGON_OFFSET_FILL);
679}
680
681// =======================================================================
682// function : EnableFeatures
683// purpose :
684// =======================================================================
685void OpenGl_Window::EnableFeatures() const
686{
687 /*glPixelTransferi (GL_MAP_COLOR, GL_TRUE);*/
688
689 if (myDither)
690 glEnable (GL_DITHER);
691 else
692 glDisable (GL_DITHER);
693}
694
695// =======================================================================
696// function : DisableFeatures
697// purpose :
698// =======================================================================
699void OpenGl_Window::DisableFeatures() const
700{
701 glDisable (GL_DITHER);
702 glPixelTransferi (GL_MAP_COLOR, GL_FALSE);
703
704 /*
705 * Disable stuff that's likely to slow down glDrawPixels.
706 * (Omit as much of this as possible, when you know in advance
707 * that the OpenGL state will already be set correctly.)
708 */
709 glDisable(GL_ALPHA_TEST);
710 glDisable(GL_BLEND);
711 glDisable(GL_DEPTH_TEST);
712 glDisable(GL_FOG);
713 glDisable(GL_LIGHTING);
714
715 glDisable(GL_LOGIC_OP);
716 glDisable(GL_STENCIL_TEST);
717 glDisable(GL_TEXTURE_1D);
718 glDisable(GL_TEXTURE_2D);
719 glPixelTransferi(GL_MAP_COLOR, GL_FALSE);
720 glPixelTransferi(GL_RED_SCALE, 1);
721 glPixelTransferi(GL_RED_BIAS, 0);
722 glPixelTransferi(GL_GREEN_SCALE, 1);
723 glPixelTransferi(GL_GREEN_BIAS, 0);
724 glPixelTransferi(GL_BLUE_SCALE, 1);
725 glPixelTransferi(GL_BLUE_BIAS, 0);
726 glPixelTransferi(GL_ALPHA_SCALE, 1);
727 glPixelTransferi(GL_ALPHA_BIAS, 0);
728
729 /*
730 * Disable extensions that could slow down glDrawPixels.
731 * (Actually, you should check for the presence of the proper
732 * extension before making these calls. I've omitted that
733 * code for simplicity.)
734 */
735
736#ifdef GL_EXT_convolution
737 glDisable(GL_CONVOLUTION_1D_EXT);
738 glDisable(GL_CONVOLUTION_2D_EXT);
739 glDisable(GL_SEPARABLE_2D_EXT);
740#endif
741
742#ifdef GL_EXT_histogram
743 glDisable(GL_HISTOGRAM_EXT);
744 glDisable(GL_MINMAX_EXT);
745#endif
746
747#ifdef GL_EXT_texture3D
748 glDisable(GL_TEXTURE_3D_EXT);
749#endif
750}
751
752// =======================================================================
753// function : MakeFrontBufCurrent
754// purpose : TelMakeFrontBufCurrent
755// =======================================================================
756void OpenGl_Window::MakeFrontBufCurrent() const
757{
758 glDrawBuffer (GL_FRONT);
759}
760
761// =======================================================================
762// function : MakeBackBufCurrent
763// purpose : TelMakeBackBufCurrent
764// =======================================================================
765void OpenGl_Window::MakeBackBufCurrent() const
766{
767 glDrawBuffer (GL_BACK);
768}
769
770// =======================================================================
771// function : MakeFrontAndBackBufCurrent
772// purpose : TelMakeFrontAndBackBufCurrent
773// =======================================================================
774void OpenGl_Window::MakeFrontAndBackBufCurrent() const
775{
776 glDrawBuffer (GL_FRONT_AND_BACK);
777}
5e27df78 778
779// =======================================================================
780// function : GetGContext
781// purpose :
782// =======================================================================
783GLCONTEXT OpenGl_Window::GetGContext() const
784{
785 return (GLCONTEXT )myGlContext->myGContext;
786}