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 } };
41 #if (defined(_WIN32) || defined(__WIN32__))
42 static int find_pixel_format (HDC theDevCtx,
43 PIXELFORMATDESCRIPTOR& thePixelFrmt,
44 const Standard_Boolean theIsDoubleBuff)
46 PIXELFORMATDESCRIPTOR aPixelFrmtTmp;
47 memset (&aPixelFrmtTmp, 0, sizeof (PIXELFORMATDESCRIPTOR));
48 aPixelFrmtTmp.nSize = sizeof (PIXELFORMATDESCRIPTOR);
49 aPixelFrmtTmp.nVersion = 1;
50 aPixelFrmtTmp.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | (theIsDoubleBuff ? PFD_DOUBLEBUFFER : PFD_SUPPORT_GDI);
51 aPixelFrmtTmp.iPixelType = PFD_TYPE_RGBA;
52 aPixelFrmtTmp.iLayerType = PFD_MAIN_PLANE;
54 const int BUFF_BITS_STENCIL[] = { 8, 1 };
55 const int BUFF_BITS_COLOR[] = { 32, 24 };
56 const int BUFF_BITS_DEPTH[] = { 32, 24, 16 };
58 int aGoodBits[] = { 0, 0, 0 };
59 int aPixelFrmtIdLast = 0;
60 int aPixelFrmtIdGood = 0;
61 Standard_Size aStencilIter = 0, aColorIter = 0, aDepthIter = 0;
62 for (aStencilIter = 0; aStencilIter < sizeof(BUFF_BITS_STENCIL) / sizeof(int); ++aStencilIter)
64 aPixelFrmtTmp.cStencilBits = BUFF_BITS_STENCIL[aStencilIter];
65 for (aDepthIter = 0; aDepthIter < sizeof(BUFF_BITS_DEPTH) / sizeof(int); ++aDepthIter)
67 aPixelFrmtTmp.cDepthBits = BUFF_BITS_DEPTH[aDepthIter];
69 for (aColorIter = 0; aColorIter < sizeof(BUFF_BITS_COLOR) / sizeof(int); ++aColorIter)
71 aPixelFrmtTmp.cColorBits = BUFF_BITS_COLOR[aColorIter];
72 aPixelFrmtIdLast = ChoosePixelFormat (theDevCtx, &aPixelFrmtTmp);
73 if (aPixelFrmtIdLast == 0)
78 thePixelFrmt.cDepthBits = 0;
79 thePixelFrmt.cColorBits = 0;
80 thePixelFrmt.cStencilBits = 0;
81 DescribePixelFormat (theDevCtx, aPixelFrmtIdLast, sizeof(PIXELFORMATDESCRIPTOR), &thePixelFrmt);
82 if (thePixelFrmt.cColorBits >= BUFF_BITS_COLOR[aColorIter]
83 && thePixelFrmt.cDepthBits >= BUFF_BITS_DEPTH[aDepthIter]
84 && thePixelFrmt.cStencilBits >= BUFF_BITS_STENCIL[aStencilIter])
88 if (thePixelFrmt.cColorBits > aGoodBits[0])
90 aGoodBits[0] = thePixelFrmt.cColorBits;
91 aGoodBits[1] = thePixelFrmt.cDepthBits;
92 aGoodBits[2] = thePixelFrmt.cStencilBits;
93 aPixelFrmtIdGood = aPixelFrmtIdLast;
95 else if (thePixelFrmt.cColorBits == aGoodBits[0])
97 if (thePixelFrmt.cDepthBits > aGoodBits[1])
99 aGoodBits[1] = thePixelFrmt.cDepthBits;
100 aGoodBits[2] = thePixelFrmt.cStencilBits;
101 aPixelFrmtIdGood = aPixelFrmtIdLast;
103 else if (thePixelFrmt.cDepthBits == aGoodBits[1])
105 if(thePixelFrmt.cStencilBits > aGoodBits[2])
107 aGoodBits[2] = thePixelFrmt.cStencilBits;
108 aPixelFrmtIdGood = aPixelFrmtIdLast;
113 if (aColorIter < sizeof(BUFF_BITS_COLOR) / sizeof(int))
118 if (aDepthIter < sizeof(BUFF_BITS_DEPTH) / sizeof(int))
124 return (aPixelFrmtIdLast == 0) ? aPixelFrmtIdGood : aPixelFrmtIdLast;
127 static Bool WaitForNotify (Display* theDisp, XEvent* theEv, char* theArg)
129 return (theEv->type == MapNotify) && (theEv->xmap.window == (Window )theArg);
135 // =======================================================================
136 // function : OpenGl_Window
138 // =======================================================================
139 OpenGl_Window::OpenGl_Window (const Handle(OpenGl_Display)& theDisplay,
140 const CALL_DEF_WINDOW& theCWindow,
141 Aspect_RenderingContext theGContext,
142 const Handle(OpenGl_Context)& theShareCtx)
143 : myDisplay (theDisplay),
144 myGlContext (new OpenGl_Context()),
145 myOwnGContext (theGContext == 0),
146 #if (defined(_WIN32) || defined(__WIN32__))
147 mySysPalInUse (FALSE),
149 myWidth ((Standard_Integer )theCWindow.dx),
150 myHeight ((Standard_Integer )theCWindow.dy),
151 myBgColor (THE_DEFAULT_BG_COLOR),
152 myDither (theDisplay->Dither()),
153 myBackDither (theDisplay->BackDither())
155 myBgColor.rgb[0] = theCWindow.Background.r;
156 myBgColor.rgb[1] = theCWindow.Background.g;
157 myBgColor.rgb[2] = theCWindow.Background.b;
159 #if (defined(_WIN32) || defined(__WIN32__))
160 HWND aWindow = (HWND )theCWindow.XWindow;
161 HDC aWindowDC = GetDC (aWindow);
162 HGLRC aGContext = (HGLRC )theGContext;
164 PIXELFORMATDESCRIPTOR aPixelFrmt;
165 const int aPixelFrmtId = find_pixel_format (aWindowDC, aPixelFrmt, myDisplay->DBuffer());
166 if (aPixelFrmtId == 0)
168 ReleaseDC (aWindow, aWindowDC);
170 TCollection_AsciiString aMsg ("OpenGl_Window::CreateWindow: ChoosePixelFormat failed. Error code: ");
171 aMsg += (int )GetLastError();
172 Aspect_GraphicDeviceDefinitionError::Raise (aMsg.ToCString());
176 if (aPixelFrmt.dwFlags & PFD_NEED_PALETTE)
178 WINDOW_DATA* aWndData = (WINDOW_DATA* )GetWindowLongPtr (aWindow, GWLP_USERDATA);
180 mySysPalInUse = (aPixelFrmt.dwFlags & PFD_NEED_SYSTEM_PALETTE) ? TRUE : FALSE;
181 InterfaceGraphic_RealizePalette (aWindowDC, aWndData->hPal, FALSE, mySysPalInUse);
186 myDither = (aPixelFrmt.cColorBits <= 8);
191 myBackDither = (aPixelFrmt.cColorBits <= 8);
194 if (!SetPixelFormat (aWindowDC, aPixelFrmtId, &aPixelFrmt))
196 ReleaseDC (aWindow, aWindowDC);
198 TCollection_AsciiString aMsg("OpenGl_Window::CreateWindow: SetPixelFormat failed. Error code: ");
199 aMsg += (int )GetLastError();
200 Aspect_GraphicDeviceDefinitionError::Raise (aMsg.ToCString());
204 if (aGContext == NULL)
206 aGContext = wglCreateContext (aWindowDC);
207 if (aGContext == NULL)
209 ReleaseDC (aWindow, aWindowDC);
211 TCollection_AsciiString aMsg ("OpenGl_Window::CreateWindow: wglCreateContext failed. Error code: ");
212 aMsg += (int )GetLastError();
213 Aspect_GraphicDeviceDefinitionError::Raise (aMsg.ToCString());
218 // all GL context within one OpenGl_GraphicDriver should be shared!
219 if (!theShareCtx.IsNull() && wglShareLists ((HGLRC )theShareCtx->myGContext, aGContext) != TRUE)
221 TCollection_AsciiString aMsg ("OpenGl_Window::CreateWindow: wglShareLists failed. Error code: ");
222 aMsg += (int )GetLastError();
223 Aspect_GraphicDeviceDefinitionError::Raise (aMsg.ToCString());
227 myGlContext->Init ((Aspect_Handle )aWindow, (Aspect_Handle )aWindowDC, (Aspect_RenderingContext )aGContext);
229 WINDOW aParent = (WINDOW )theCWindow.XWindow;
231 DISPLAY* aDisp = (DISPLAY* )myDisplay->GetDisplay();
232 GLXContext aGContext = (GLXContext )theGContext;
234 XWindowAttributes wattr;
235 XGetWindowAttributes (aDisp, aParent, &wattr);
236 const int scr = DefaultScreen (aDisp);
238 XVisualInfo* aVis = NULL;
240 unsigned long aVisInfoMask = VisualIDMask | VisualScreenMask;
241 XVisualInfo aVisInfo;
242 aVisInfo.visualid = wattr.visual->visualid;
243 aVisInfo.screen = scr;
245 aVis = XGetVisualInfo (aDisp, aVisInfoMask, &aVisInfo, &aNbItems);
252 Aspect_GraphicDeviceDefinitionError::Raise ("OpenGl_Window::CreateWindow: XGetVisualInfo failed.");
260 #if defined(__linux) || defined(Linux) || defined(__APPLE__)
263 // check Visual for OpenGl context's parameters compability
264 int isGl = 0, isDoubleBuffer = 0, isRGBA = 0, aDepthSize = 0, aStencilSize = 0;
266 if (glXGetConfig (aDisp, aVis, GLX_USE_GL, &isGl) != 0)
269 if (glXGetConfig (aDisp, aVis, GLX_RGBA, &isRGBA) != 0)
272 if (glXGetConfig (aDisp, aVis, GLX_DOUBLEBUFFER, &isDoubleBuffer) != 0)
275 if (glXGetConfig (aDisp, aVis, GLX_DEPTH_SIZE, &aDepthSize) != 0)
278 if (glXGetConfig (aDisp, aVis, GLX_STENCIL_SIZE, &aStencilSize) != 0)
281 if (!isGl || !aDepthSize || !isRGBA || (isDoubleBuffer ? 1 : 0) != (myDisplay->DBuffer()? 1 : 0))
293 anAttribs[anIter++] = GLX_RGBA;
295 anAttribs[anIter++] = GLX_DEPTH_SIZE;
296 anAttribs[anIter++] = 1;
298 anAttribs[anIter++] = GLX_STENCIL_SIZE;
299 anAttribs[anIter++] = 1;
301 anAttribs[anIter++] = GLX_RED_SIZE;
302 anAttribs[anIter++] = (wattr.depth <= 8) ? 0 : 1;
304 anAttribs[anIter++] = GLX_GREEN_SIZE;
305 anAttribs[anIter++] = (wattr.depth <= 8) ? 0 : 1;
307 anAttribs[anIter++] = GLX_BLUE_SIZE;
308 anAttribs[anIter++] = (wattr.depth <= 8) ? 0 : 1;
310 if (myDisplay->DBuffer())
311 anAttribs[anIter++] = GLX_DOUBLEBUFFER;
313 anAttribs[anIter++] = None;
315 aVis = glXChooseVisual (aDisp, scr, anAttribs);
318 Aspect_GraphicDeviceDefinitionError::Raise ("OpenGl_Window::CreateWindow: glXChooseVisual failed.");
323 if (!theShareCtx.IsNull())
325 // ctx est une copie du previous
326 aGContext = glXCreateContext (aDisp, aVis, (GLXContext )theShareCtx->myGContext, GL_TRUE);
330 aGContext = glXCreateContext (aDisp, aVis, NULL, GL_TRUE);
335 Aspect_GraphicDeviceDefinitionError::Raise ("OpenGl_Window::CreateWindow: glXCreateContext failed.");
339 Colormap cmap = XCreateColormap (aDisp, aParent, aVis->visual, AllocNone);
342 color.red = (unsigned short) (myBgColor.rgb[0] * 0xFFFF);
343 color.green = (unsigned short) (myBgColor.rgb[1] * 0xFFFF);
344 color.blue = (unsigned short) (myBgColor.rgb[2] * 0xFFFF);
345 color.flags = DoRed | DoGreen | DoBlue;
346 XAllocColor (aDisp, cmap, &color);
348 XSetWindowAttributes cwa;
350 cwa.event_mask = StructureNotifyMask;
351 cwa.border_pixel = color.pixel;
352 cwa.background_pixel = color.pixel;
354 if (aVis->visualid == wattr.visual->visualid)
360 unsigned long mask = CWBackPixel | CWColormap | CWBorderPixel | CWEventMask;
361 aWindow = XCreateWindow (aDisp, aParent, 0, 0, myWidth, myHeight, 0/*bw*/, aVis->depth, InputOutput, aVis->visual, mask, &cwa);
364 XSetWindowBackground (aDisp, aWindow, cwa.background_pixel);
365 XClearWindow (aDisp, aWindow);
367 if (aWindow != aParent)
370 XMapWindow (aDisp, aWindow);
371 XIfEvent (aDisp, &anEvent, WaitForNotify, (char* )aWindow);
376 * Le BackDitherProp est utilise pour le clear du background
377 * Pour eviter une difference de couleurs avec la couleur choisie
378 * par l'application (XWindow) il faut desactiver le dithering
379 * au dessus de 8 plans.
381 * Pour le DitherProp:
382 * On cherchera a activer le Dithering que si le Visual a au moins
383 * 8 plans pour le GLX_RED_SIZE. Le test est plus sur car on peut
384 * avoir une profondeur superieure a 12 mais avoir besoin du dithering.
385 * (Carte Impact avec GLX_RED_SIZE a 5 par exemple)
389 glXGetConfig (aDisp, aVis, GLX_RED_SIZE, &aValue);
392 myDither = (aValue < 8);
395 myBackDither = (aVis->depth <= 8);
397 XFree ((char* )aVis);
399 myGlContext->Init ((Aspect_Drawable )aWindow, (Aspect_Display )myDisplay->GetDisplay(), (Aspect_RenderingContext )aGContext);
401 myGlContext->Share (theShareCtx);
406 // =======================================================================
407 // function : ~OpenGl_Window
409 // =======================================================================
410 OpenGl_Window::~OpenGl_Window()
412 #if (defined(_WIN32) || defined(__WIN32__))
413 HWND aWindow = (HWND )myGlContext->myWindow;
414 HDC aWindowDC = (HDC )myGlContext->myWindowDC;
415 HGLRC aGContext = (HGLRC )myGlContext->myGContext;
416 myGlContext.Nullify();
420 if (wglGetCurrentContext() != NULL)
422 wglDeleteContext (aGContext);
424 ReleaseDC (aWindow, aWindowDC);
427 GLXDrawable aWindow = (GLXDrawable )myGlContext->myWindow;
428 Display* aDisplay = (Display* )myGlContext->myDisplay;
429 GLXContext aGContext = (GLXContext )myGlContext->myGContext;
430 myGlContext.Nullify();
432 if (aDisplay != NULL && myOwnGContext)
434 // FSXXX sync necessary if non-direct rendering
436 glXDestroyContext (aDisplay, aGContext);
443 // =======================================================================
444 // function : Activate
446 // =======================================================================
447 Standard_Boolean OpenGl_Window::Activate()
449 return myGlContext->MakeCurrent();
452 #if !defined(__APPLE__) || defined(MACOSX_USE_GLX)
454 // =======================================================================
456 // purpose : call_subr_resize
457 // =======================================================================
458 void OpenGl_Window::Resize (const CALL_DEF_WINDOW& theCWindow)
460 DISPLAY* aDisp = (DISPLAY* )myDisplay->GetDisplay();
464 // If the size is not changed - do nothing
465 if ((myWidth == theCWindow.dx) && (myHeight == theCWindow.dy))
468 myWidth = (Standard_Integer )theCWindow.dx;
469 myHeight = (Standard_Integer )theCWindow.dy;
471 #if (!defined(_WIN32) && !defined(__WIN32__))
472 XResizeWindow (aDisp, myGlContext->myWindow, (unsigned int )myWidth, (unsigned int )myHeight);
473 XSync (aDisp, False);
481 // =======================================================================
482 // function : ReadDepths
483 // purpose : TelReadDepths
484 // =======================================================================
485 void OpenGl_Window::ReadDepths (const Standard_Integer theX, const Standard_Integer theY,
486 const Standard_Integer theWidth, const Standard_Integer theHeight,
489 if (theDepths == NULL || !Activate())
492 glMatrixMode (GL_PROJECTION);
494 gluOrtho2D (0.0, (GLdouble )myWidth, 0.0, (GLdouble )myHeight);
495 glMatrixMode (GL_MODELVIEW);
498 glRasterPos2i (theX, theY);
500 glReadPixels (theX, theY, theWidth, theHeight, GL_DEPTH_COMPONENT, GL_FLOAT, theDepths);
504 // =======================================================================
505 // function : SetBackgroundColor
506 // purpose : call_subr_set_background
507 // =======================================================================
508 void OpenGl_Window::SetBackgroundColor (const Standard_ShortReal theR,
509 const Standard_ShortReal theG,
510 const Standard_ShortReal theB)
512 myBgColor.rgb[0] = theR;
513 myBgColor.rgb[1] = theG;
514 myBgColor.rgb[2] = theB;
517 #if !defined(__APPLE__) || defined(MACOSX_USE_GLX)
519 // =======================================================================
522 // =======================================================================
523 void OpenGl_Window::Init()
528 #if (defined(_WIN32) || defined(__WIN32__))
530 GetClientRect ((HWND )myGlContext->myWindow, &cr);
531 myWidth = cr.right - cr.left;
532 myHeight = cr.bottom - cr.top;
536 unsigned int aDummyU;
537 unsigned int aNewWidth = 0;
538 unsigned int aNewHeight = 0;
539 DISPLAY* aDisp = (DISPLAY* )myDisplay->GetDisplay();
540 XGetGeometry (aDisp, myGlContext->myWindow, &aRootWin, &aDummy, &aDummy, &aNewWidth, &aNewHeight, &aDummyU, &aDummyU);
542 myHeight = aNewHeight;
545 glMatrixMode (GL_MODELVIEW);
546 glViewport (0, 0, myWidth, myHeight);
548 glDisable (GL_SCISSOR_TEST);
549 glDrawBuffer (GL_BACK);
554 // =======================================================================
555 // function : EnablePolygonOffset
556 // purpose : call_subr_enable_polygon_offset
557 // =======================================================================
558 void OpenGl_Window::EnablePolygonOffset() const
560 Standard_ShortReal aFactor, aUnits;
561 myDisplay->PolygonOffset (aFactor, aUnits);
562 glPolygonOffset (aFactor, aUnits);
563 glEnable (GL_POLYGON_OFFSET_FILL);
566 // =======================================================================
567 // function : DisablePolygonOffset
568 // purpose : call_subr_disable_polygon_offset
569 // =======================================================================
570 void OpenGl_Window::DisablePolygonOffset() const
572 glDisable (GL_POLYGON_OFFSET_FILL);
575 // =======================================================================
576 // function : EnableFeatures
578 // =======================================================================
579 void OpenGl_Window::EnableFeatures() const
581 /*glPixelTransferi (GL_MAP_COLOR, GL_TRUE);*/
584 glEnable (GL_DITHER);
586 glDisable (GL_DITHER);
589 // =======================================================================
590 // function : DisableFeatures
592 // =======================================================================
593 void OpenGl_Window::DisableFeatures() const
595 glDisable (GL_DITHER);
596 glPixelTransferi (GL_MAP_COLOR, GL_FALSE);
599 * Disable stuff that's likely to slow down glDrawPixels.
600 * (Omit as much of this as possible, when you know in advance
601 * that the OpenGL state will already be set correctly.)
603 glDisable(GL_ALPHA_TEST);
605 glDisable(GL_DEPTH_TEST);
607 glDisable(GL_LIGHTING);
609 glDisable(GL_LOGIC_OP);
610 glDisable(GL_STENCIL_TEST);
611 glDisable(GL_TEXTURE_1D);
612 glDisable(GL_TEXTURE_2D);
613 glPixelTransferi(GL_MAP_COLOR, GL_FALSE);
614 glPixelTransferi(GL_RED_SCALE, 1);
615 glPixelTransferi(GL_RED_BIAS, 0);
616 glPixelTransferi(GL_GREEN_SCALE, 1);
617 glPixelTransferi(GL_GREEN_BIAS, 0);
618 glPixelTransferi(GL_BLUE_SCALE, 1);
619 glPixelTransferi(GL_BLUE_BIAS, 0);
620 glPixelTransferi(GL_ALPHA_SCALE, 1);
621 glPixelTransferi(GL_ALPHA_BIAS, 0);
624 * Disable extensions that could slow down glDrawPixels.
625 * (Actually, you should check for the presence of the proper
626 * extension before making these calls. I've omitted that
627 * code for simplicity.)
630 #ifdef GL_EXT_convolution
631 glDisable(GL_CONVOLUTION_1D_EXT);
632 glDisable(GL_CONVOLUTION_2D_EXT);
633 glDisable(GL_SEPARABLE_2D_EXT);
636 #ifdef GL_EXT_histogram
637 glDisable(GL_HISTOGRAM_EXT);
638 glDisable(GL_MINMAX_EXT);
641 #ifdef GL_EXT_texture3D
642 glDisable(GL_TEXTURE_3D_EXT);
646 // =======================================================================
647 // function : MakeFrontBufCurrent
648 // purpose : TelMakeFrontBufCurrent
649 // =======================================================================
650 void OpenGl_Window::MakeFrontBufCurrent() const
652 glDrawBuffer (GL_FRONT);
655 // =======================================================================
656 // function : MakeBackBufCurrent
657 // purpose : TelMakeBackBufCurrent
658 // =======================================================================
659 void OpenGl_Window::MakeBackBufCurrent() const
661 glDrawBuffer (GL_BACK);
664 // =======================================================================
665 // function : MakeFrontAndBackBufCurrent
666 // purpose : TelMakeFrontAndBackBufCurrent
667 // =======================================================================
668 void OpenGl_Window::MakeFrontAndBackBufCurrent() const
670 glDrawBuffer (GL_FRONT_AND_BACK);
673 // =======================================================================
674 // function : GetGContext
676 // =======================================================================
677 GLCONTEXT OpenGl_Window::GetGContext() const
679 return (GLCONTEXT )myGlContext->myGContext;