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.
21 #include <InterfaceGraphic.hxx>
23 #include <OpenGl_Window.hxx>
25 #include <OpenGl_Context.hxx>
26 #include <OpenGl_Display.hxx>
28 #include <Aspect_GraphicDeviceDefinitionError.hxx>
29 #include <TCollection_AsciiString.hxx>
31 #include <GL/glu.h> // gluOrtho2D()
33 IMPLEMENT_STANDARD_HANDLE(OpenGl_Window,MMgt_TShared)
34 IMPLEMENT_STANDARD_RTTIEXT(OpenGl_Window,MMgt_TShared)
38 static const TEL_COLOUR THE_DEFAULT_BG_COLOR = { { 0.F, 0.F, 0.F, 1.F } };
40 #if (defined(_WIN32) || defined(__WIN32__))
41 static int find_pixel_format (HDC hDC, PIXELFORMATDESCRIPTOR* pfd, const Standard_Boolean dbuff)
43 PIXELFORMATDESCRIPTOR pfd0;
44 memset (&pfd0, 0, sizeof (PIXELFORMATDESCRIPTOR));
45 pfd0.nSize = sizeof (PIXELFORMATDESCRIPTOR);
47 pfd0.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | (dbuff ? PFD_DOUBLEBUFFER : PFD_SUPPORT_GDI);
48 pfd0.iPixelType = PFD_TYPE_RGBA;
49 pfd0.iLayerType = PFD_MAIN_PLANE;
53 const int cBits[] = { 32, 24 };
54 const int dBits[] = { 32, 24, 16 };
57 for (i = 0; i < sizeof(dBits) / sizeof(int); i++)
59 pfd0.cDepthBits = dBits[i];
61 for (j = 0; j < sizeof(cBits) / sizeof(int); j++)
63 pfd0.cColorBits = cBits[j];
64 iPixelFormat = ChoosePixelFormat (hDC, &pfd0);
69 DescribePixelFormat (hDC, iPixelFormat, sizeof (PIXELFORMATDESCRIPTOR), pfd);
70 if (pfd->cColorBits >= cBits[j] && pfd->cDepthBits >= dBits[i])
76 if (j < sizeof(cBits) / sizeof(int))
80 if (iPixelFormat == 0)
86 static Bool WaitForNotify (Display* theDisp, XEvent* theEv, char* theArg)
88 return (theEv->type == MapNotify) && (theEv->xmap.window == (Window )theArg);
94 // =======================================================================
95 // function : OpenGl_Window
97 // =======================================================================
98 OpenGl_Window::OpenGl_Window (const Handle(OpenGl_Display)& theDisplay,
99 const CALL_DEF_WINDOW& theCWindow,
100 Aspect_RenderingContext theGContext,
101 const Handle(OpenGl_Context)& theShareCtx)
102 : myDisplay (theDisplay),
103 myGlContext (new OpenGl_Context()),
104 myOwnGContext (theGContext == 0),
105 #if (defined(_WIN32) || defined(__WIN32__))
106 mySysPalInUse (FALSE),
108 myWidth ((Standard_Integer )theCWindow.dx),
109 myHeight ((Standard_Integer )theCWindow.dy),
110 myBgColor (THE_DEFAULT_BG_COLOR),
111 myDither (theDisplay->Dither()),
112 myBackDither (theDisplay->BackDither())
114 myBgColor.rgb[0] = theCWindow.Background.r;
115 myBgColor.rgb[1] = theCWindow.Background.g;
116 myBgColor.rgb[2] = theCWindow.Background.b;
118 #if (defined(_WIN32) || defined(__WIN32__))
119 HWND aWindow = (HWND )theCWindow.XWindow;
120 HDC aWindowDC = GetDC (aWindow);
121 HGLRC aGContext = (HGLRC )theGContext;
123 PIXELFORMATDESCRIPTOR pfd;
124 int iPixelFormat = find_pixel_format (aWindowDC, &pfd, myDisplay->DBuffer());
125 if (iPixelFormat == 0)
127 ReleaseDC (aWindow, aWindowDC);
129 TCollection_AsciiString aMsg ("OpenGl_Window::CreateWindow: ChoosePixelFormat failed. Error code: ");
130 aMsg += (int )GetLastError();
131 Aspect_GraphicDeviceDefinitionError::Raise (aMsg.ToCString());
135 if (pfd.dwFlags & PFD_NEED_PALETTE)
137 WINDOW_DATA* wd = (WINDOW_DATA* )GetWindowLongPtr (aWindow, GWLP_USERDATA);
139 mySysPalInUse = (pfd.dwFlags & PFD_NEED_SYSTEM_PALETTE) ? TRUE : FALSE;
140 InterfaceGraphic_RealizePalette (aWindowDC, wd->hPal, FALSE, mySysPalInUse);
144 myDither = (pfd.cColorBits <= 8);
147 myBackDither = (pfd.cColorBits <= 8);
149 if (!SetPixelFormat (aWindowDC, iPixelFormat, &pfd))
151 ReleaseDC (aWindow, aWindowDC);
153 TCollection_AsciiString aMsg("OpenGl_Window::CreateWindow: SetPixelFormat failed. Error code: ");
154 aMsg += (int )GetLastError();
155 Aspect_GraphicDeviceDefinitionError::Raise (aMsg.ToCString());
159 if (aGContext == NULL)
161 aGContext = wglCreateContext (aWindowDC);
162 if (aGContext == NULL)
164 ReleaseDC (aWindow, aWindowDC);
166 TCollection_AsciiString aMsg ("OpenGl_Window::CreateWindow: wglCreateContext failed. Error code: ");
167 aMsg += (int )GetLastError();
168 Aspect_GraphicDeviceDefinitionError::Raise (aMsg.ToCString());
173 // all GL context within one OpenGl_GraphicDriver should be shared!
174 if (!theShareCtx.IsNull() && wglShareLists ((HGLRC )theShareCtx->myGContext, aGContext) != TRUE)
176 TCollection_AsciiString aMsg ("OpenGl_Window::CreateWindow: wglShareLists failed. Error code: ");
177 aMsg += (int )GetLastError();
178 Aspect_GraphicDeviceDefinitionError::Raise (aMsg.ToCString());
182 myGlContext->Init ((Aspect_Handle )aWindow, (Aspect_Handle )aWindowDC, (Aspect_RenderingContext )aGContext);
184 WINDOW aParent = (WINDOW )theCWindow.XWindow;
186 DISPLAY* aDisp = (DISPLAY* )myDisplay->GetDisplay();
187 GLXContext aGContext = (GLXContext )theGContext;
189 XWindowAttributes wattr;
190 XGetWindowAttributes (aDisp, aParent, &wattr);
191 const int scr = DefaultScreen (aDisp);
193 XVisualInfo* aVis = NULL;
195 unsigned long aVisInfoMask = VisualIDMask | VisualScreenMask;
196 XVisualInfo aVisInfo;
197 aVisInfo.visualid = wattr.visual->visualid;
198 aVisInfo.screen = scr;
200 aVis = XGetVisualInfo (aDisp, aVisInfoMask, &aVisInfo, &aNbItems);
207 Aspect_GraphicDeviceDefinitionError::Raise ("OpenGl_Window::CreateWindow: XGetVisualInfo failed.");
215 #if defined(__linux) || defined(Linux) || defined(__APPLE__)
218 // check Visual for OpenGl context's parameters compability
219 int isGl = 0, isDoubleBuffer = 0, isRGBA = 0, aDepthSize = 0;
221 if (glXGetConfig (aDisp, aVis, GLX_USE_GL, &isGl) != 0)
224 if (glXGetConfig (aDisp, aVis, GLX_RGBA, &isRGBA) != 0)
227 if (glXGetConfig (aDisp, aVis, GLX_DOUBLEBUFFER, &isDoubleBuffer) != 0)
230 if (glXGetConfig (aDisp, aVis, GLX_DEPTH_SIZE, &aDepthSize) != 0)
233 if (!isGl || !aDepthSize || !isRGBA || (isDoubleBuffer ? 1 : 0) != (myDisplay->DBuffer()? 1 : 0))
245 anAttribs[anIter++] = GLX_RGBA;
247 anAttribs[anIter++] = GLX_DEPTH_SIZE;
248 anAttribs[anIter++] = 1;
250 anAttribs[anIter++] = GLX_RED_SIZE;
251 anAttribs[anIter++] = (wattr.depth <= 8) ? 0 : 1;
253 anAttribs[anIter++] = GLX_GREEN_SIZE;
254 anAttribs[anIter++] = (wattr.depth <= 8) ? 0 : 1;
256 anAttribs[anIter++] = GLX_BLUE_SIZE;
257 anAttribs[anIter++] = (wattr.depth <= 8) ? 0 : 1;
259 if (myDisplay->DBuffer())
260 anAttribs[anIter++] = GLX_DOUBLEBUFFER;
262 anAttribs[anIter++] = None;
264 aVis = glXChooseVisual (aDisp, scr, anAttribs);
267 Aspect_GraphicDeviceDefinitionError::Raise ("OpenGl_Window::CreateWindow: glXChooseVisual failed.");
272 if (!theShareCtx.IsNull())
274 // ctx est une copie du previous
275 aGContext = glXCreateContext (aDisp, aVis, (GLXContext )theShareCtx->myGContext, GL_TRUE);
279 aGContext = glXCreateContext (aDisp, aVis, NULL, GL_TRUE);
284 Aspect_GraphicDeviceDefinitionError::Raise ("OpenGl_Window::CreateWindow: glXCreateContext failed.");
288 Colormap cmap = XCreateColormap (aDisp, aParent, aVis->visual, AllocNone);
291 color.red = (unsigned short) (myBgColor.rgb[0] * 0xFFFF);
292 color.green = (unsigned short) (myBgColor.rgb[1] * 0xFFFF);
293 color.blue = (unsigned short) (myBgColor.rgb[2] * 0xFFFF);
294 color.flags = DoRed | DoGreen | DoBlue;
295 XAllocColor (aDisp, cmap, &color);
297 XSetWindowAttributes cwa;
299 cwa.event_mask = StructureNotifyMask;
300 cwa.border_pixel = color.pixel;
301 cwa.background_pixel = color.pixel;
303 if (aVis->visualid == wattr.visual->visualid)
309 unsigned long mask = CWBackPixel | CWColormap | CWBorderPixel | CWEventMask;
310 aWindow = XCreateWindow (aDisp, aParent, 0, 0, myWidth, myHeight, 0/*bw*/, aVis->depth, InputOutput, aVis->visual, mask, &cwa);
313 XSetWindowBackground (aDisp, aWindow, cwa.background_pixel);
314 XClearWindow (aDisp, aWindow);
316 if (aWindow != aParent)
319 XMapWindow (aDisp, aWindow);
320 XIfEvent (aDisp, &anEvent, WaitForNotify, (char* )aWindow);
325 * Le BackDitherProp est utilise pour le clear du background
326 * Pour eviter une difference de couleurs avec la couleur choisie
327 * par l'application (XWindow) il faut desactiver le dithering
328 * au dessus de 8 plans.
330 * Pour le DitherProp:
331 * On cherchera a activer le Dithering que si le Visual a au moins
332 * 8 plans pour le GLX_RED_SIZE. Le test est plus sur car on peut
333 * avoir une profondeur superieure a 12 mais avoir besoin du dithering.
334 * (Carte Impact avec GLX_RED_SIZE a 5 par exemple)
338 glXGetConfig (aDisp, aVis, GLX_RED_SIZE, &aValue);
341 myDither = (aValue < 8);
344 myBackDither = (aVis->depth <= 8);
346 XFree ((char* )aVis);
348 myGlContext->Init ((Aspect_Drawable )aWindow, (Aspect_Display )myDisplay->GetDisplay(), (Aspect_RenderingContext )aGContext);
350 myGlContext->Share (theShareCtx);
355 // =======================================================================
356 // function : ~OpenGl_Window
358 // =======================================================================
359 OpenGl_Window::~OpenGl_Window()
361 #if (defined(_WIN32) || defined(__WIN32__))
362 HWND aWindow = (HWND )myGlContext->myWindow;
363 HDC aWindowDC = (HDC )myGlContext->myWindowDC;
364 HGLRC aGContext = (HGLRC )myGlContext->myGContext;
365 myGlContext.Nullify();
369 if (wglGetCurrentContext() != NULL)
371 wglDeleteContext (aGContext);
373 ReleaseDC (aWindow, aWindowDC);
376 GLXDrawable aWindow = (GLXDrawable )myGlContext->myWindow;
377 Display* aDisplay = (Display* )myGlContext->myDisplay;
378 GLXContext aGContext = (GLXContext )myGlContext->myGContext;
379 myGlContext.Nullify();
381 if (aDisplay != NULL && myOwnGContext)
383 // FSXXX sync necessary if non-direct rendering
385 glXDestroyContext (aDisplay, aGContext);
390 // =======================================================================
391 // function : Activate
393 // =======================================================================
394 Standard_Boolean OpenGl_Window::Activate()
396 return myGlContext->MakeCurrent();
399 // =======================================================================
401 // purpose : call_subr_resize
402 // =======================================================================
403 void OpenGl_Window::Resize (const CALL_DEF_WINDOW& theCWindow)
405 DISPLAY* aDisp = (DISPLAY* )myDisplay->GetDisplay();
409 // If the size is not changed - do nothing
410 if ((myWidth == theCWindow.dx) && (myHeight == theCWindow.dy))
413 myWidth = (Standard_Integer )theCWindow.dx;
414 myHeight = (Standard_Integer )theCWindow.dy;
416 #if (!defined(_WIN32) && !defined(__WIN32__))
417 XResizeWindow (aDisp, myGlContext->myWindow, (unsigned int )myWidth, (unsigned int )myHeight);
418 XSync (aDisp, False);
424 // =======================================================================
425 // function : ReadDepths
426 // purpose : TelReadDepths
427 // =======================================================================
428 void OpenGl_Window::ReadDepths (const Standard_Integer theX, const Standard_Integer theY,
429 const Standard_Integer theWidth, const Standard_Integer theHeight,
432 if (theDepths == NULL || !Activate())
435 glMatrixMode (GL_PROJECTION);
437 gluOrtho2D (0.0, (GLdouble )myWidth, 0.0, (GLdouble )myHeight);
438 glMatrixMode (GL_MODELVIEW);
441 glRasterPos2i (theX, theY);
443 glReadPixels (theX, theY, theWidth, theHeight, GL_DEPTH_COMPONENT, GL_FLOAT, theDepths);
447 // =======================================================================
448 // function : SetBackgroundColor
449 // purpose : call_subr_set_background
450 // =======================================================================
451 void OpenGl_Window::SetBackgroundColor (const Standard_ShortReal theR,
452 const Standard_ShortReal theG,
453 const Standard_ShortReal theB)
455 myBgColor.rgb[0] = theR;
456 myBgColor.rgb[1] = theG;
457 myBgColor.rgb[2] = theB;
460 // =======================================================================
463 // =======================================================================
464 void OpenGl_Window::Init()
469 #if (defined(_WIN32) || defined(__WIN32__))
471 GetClientRect ((HWND )myGlContext->myWindow, &cr);
472 myWidth = cr.right - cr.left;
473 myHeight = cr.bottom - cr.top;
477 unsigned int aDummyU;
478 unsigned int aNewWidth = 0;
479 unsigned int aNewHeight = 0;
480 DISPLAY* aDisp = (DISPLAY* )myDisplay->GetDisplay();
481 XGetGeometry (aDisp, myGlContext->myWindow, &aRootWin, &aDummy, &aDummy, &aNewWidth, &aNewHeight, &aDummyU, &aDummyU);
483 myHeight = aNewHeight;
486 glMatrixMode (GL_MODELVIEW);
487 glViewport (0, 0, myWidth, myHeight);
489 glDisable (GL_SCISSOR_TEST);
490 glDrawBuffer (GL_BACK);
493 // =======================================================================
494 // function : EnablePolygonOffset
495 // purpose : call_subr_enable_polygon_offset
496 // =======================================================================
497 void OpenGl_Window::EnablePolygonOffset() const
499 Standard_ShortReal aFactor, aUnits;
500 myDisplay->PolygonOffset (aFactor, aUnits);
501 glPolygonOffset (aFactor, aUnits);
502 glEnable (GL_POLYGON_OFFSET_FILL);
505 // =======================================================================
506 // function : DisablePolygonOffset
507 // purpose : call_subr_disable_polygon_offset
508 // =======================================================================
509 void OpenGl_Window::DisablePolygonOffset() const
511 glDisable (GL_POLYGON_OFFSET_FILL);
514 // =======================================================================
515 // function : EnableFeatures
517 // =======================================================================
518 void OpenGl_Window::EnableFeatures() const
520 /*glPixelTransferi (GL_MAP_COLOR, GL_TRUE);*/
523 glEnable (GL_DITHER);
525 glDisable (GL_DITHER);
528 // =======================================================================
529 // function : DisableFeatures
531 // =======================================================================
532 void OpenGl_Window::DisableFeatures() const
534 glDisable (GL_DITHER);
535 glPixelTransferi (GL_MAP_COLOR, GL_FALSE);
538 * Disable stuff that's likely to slow down glDrawPixels.
539 * (Omit as much of this as possible, when you know in advance
540 * that the OpenGL state will already be set correctly.)
542 glDisable(GL_ALPHA_TEST);
544 glDisable(GL_DEPTH_TEST);
546 glDisable(GL_LIGHTING);
548 glDisable(GL_LOGIC_OP);
549 glDisable(GL_STENCIL_TEST);
550 glDisable(GL_TEXTURE_1D);
551 glDisable(GL_TEXTURE_2D);
552 glPixelTransferi(GL_MAP_COLOR, GL_FALSE);
553 glPixelTransferi(GL_RED_SCALE, 1);
554 glPixelTransferi(GL_RED_BIAS, 0);
555 glPixelTransferi(GL_GREEN_SCALE, 1);
556 glPixelTransferi(GL_GREEN_BIAS, 0);
557 glPixelTransferi(GL_BLUE_SCALE, 1);
558 glPixelTransferi(GL_BLUE_BIAS, 0);
559 glPixelTransferi(GL_ALPHA_SCALE, 1);
560 glPixelTransferi(GL_ALPHA_BIAS, 0);
563 * Disable extensions that could slow down glDrawPixels.
564 * (Actually, you should check for the presence of the proper
565 * extension before making these calls. I've omitted that
566 * code for simplicity.)
569 #ifdef GL_EXT_convolution
570 glDisable(GL_CONVOLUTION_1D_EXT);
571 glDisable(GL_CONVOLUTION_2D_EXT);
572 glDisable(GL_SEPARABLE_2D_EXT);
575 #ifdef GL_EXT_histogram
576 glDisable(GL_HISTOGRAM_EXT);
577 glDisable(GL_MINMAX_EXT);
580 #ifdef GL_EXT_texture3D
581 glDisable(GL_TEXTURE_3D_EXT);
585 // =======================================================================
586 // function : MakeFrontBufCurrent
587 // purpose : TelMakeFrontBufCurrent
588 // =======================================================================
589 void OpenGl_Window::MakeFrontBufCurrent() const
591 glDrawBuffer (GL_FRONT);
594 // =======================================================================
595 // function : MakeBackBufCurrent
596 // purpose : TelMakeBackBufCurrent
597 // =======================================================================
598 void OpenGl_Window::MakeBackBufCurrent() const
600 glDrawBuffer (GL_BACK);
603 // =======================================================================
604 // function : MakeFrontAndBackBufCurrent
605 // purpose : TelMakeFrontAndBackBufCurrent
606 // =======================================================================
607 void OpenGl_Window::MakeFrontAndBackBufCurrent() const
609 glDrawBuffer (GL_FRONT_AND_BACK);
612 // =======================================================================
613 // function : GetGContext
615 // =======================================================================
616 GLCONTEXT OpenGl_Window::GetGContext() const
618 return (GLCONTEXT )myGlContext->myGContext;