0024228: TKOpenGL - destroy GL context at view close
[occt.git] / src / OpenGl / OpenGl_Display.cxx
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
20 #include <OpenGl_GlCore11.hxx>
21
22 #include <OpenGl_Display.hxx>
23 #include <OpenGl_Context.hxx>
24 #include <OpenGl_Light.hxx>
25
26 #include <OSD_Environment.hxx>
27 #include <TCollection_AsciiString.hxx>
28 #include <Aspect_GraphicDeviceDefinitionError.hxx>
29
30 #if (!defined(_WIN32) && !defined(__WIN32__) && (!defined(__APPLE__) || defined(MACOSX_USE_GLX)))
31   #include <X11/Xlib.h> // XOpenDisplay()
32 #endif
33
34 IMPLEMENT_STANDARD_HANDLE(OpenGl_Display,MMgt_TShared)
35 IMPLEMENT_STANDARD_RTTIEXT(OpenGl_Display,MMgt_TShared)
36
37 namespace
38 {
39   #if (defined(_WIN32) || defined(__WIN32__)) || (defined(__APPLE__) && !defined(MACOSX_USE_GLX))
40     static char* TheDummyDisplay = "DISPLAY";
41   #endif
42
43   static const OpenGl_Facilities myDefaultFacilities = { 1, 1, 1, 1, OpenGLMaxLights, 10000 };
44 };
45
46 /*----------------------------------------------------------------------*/
47 #if (defined(_WIN32) || defined(__WIN32__)) || (defined(__APPLE__) && !defined(MACOSX_USE_GLX))
48 OpenGl_Display::OpenGl_Display (const Handle(Aspect_DisplayConnection)& )
49 #else
50 OpenGl_Display::OpenGl_Display (const Handle(Aspect_DisplayConnection)& theDisplayConnection)
51 #endif
52 : myDisplay(NULL),
53   myFacilities(myDefaultFacilities),
54   myDBuffer(Standard_True),
55   myDither(Standard_True),
56   myBackDither(Standard_False),
57   myWalkthrough(Standard_False),
58   mySymPerspective(Standard_False),
59   myOffsetFactor(1.F),
60   myOffsetUnits(0.F),
61   myAntiAliasingMode(3),
62   myLinestyleBase(0),
63   myPatternBase(0)
64 {
65 #if (defined(_WIN32) || defined(__WIN32__)) || (defined(__APPLE__) && !defined(MACOSX_USE_GLX))
66   myDisplay = TheDummyDisplay;
67 #else
68   myDisplay = theDisplayConnection->GetDisplay();
69 #endif
70
71   Init();
72 }
73
74 /*----------------------------------------------------------------------*/
75
76 OpenGl_Display::~OpenGl_Display ()
77 {
78   ReleaseAttributes (NULL);
79   myDisplay = NULL;
80 }
81
82 void OpenGl_Display::ReleaseAttributes (const OpenGl_Context* theGlCtx)
83 {
84   // Delete line styles
85   if (myLinestyleBase != 0)
86   {
87     if (theGlCtx->IsValid())
88     {
89       glDeleteLists ((GLuint )myLinestyleBase, 5);
90     }
91     myLinestyleBase = 0;
92   }
93   // Delete surface patterns
94   if (myPatternBase != 0)
95   {
96     if (theGlCtx->IsValid())
97     {
98       glDeleteLists ((GLuint )myPatternBase, TEL_HS_USER_DEF_START);
99     }
100     myPatternBase = 0;
101   }
102 }
103
104 /*----------------------------------------------------------------------*/
105
106 void OpenGl_Display::Init()
107 {
108   if (myDisplay != NULL)
109   {
110   #if (!defined(_WIN32) && !defined(__WIN32__) && (!defined(__APPLE__) || defined(MACOSX_USE_GLX)))
111     XSynchronize ((Display* )myDisplay, (getenv("CALL_SYNCHRO_X") != NULL) ? 1 : 0);
112
113     if (getenv("CSF_GraphicSync") != NULL)
114       XSynchronize ((Display* )myDisplay, 1);
115
116     // does the server know about OpenGL & GLX?
117     int aDummy;
118     if (!XQueryExtension ((Display* )myDisplay, "GLX", &aDummy, &aDummy, &aDummy))
119     {
120     #ifdef DEBUG
121       std::cerr << "This system doesn't appear to support OpenGL\n";
122     #endif
123     }
124   #endif
125   }
126   else
127   {
128     TCollection_AsciiString msg("OpenGl_Display::Init");
129   #if (!defined(_WIN32) && !defined(__WIN32__) && (!defined(__APPLE__) || defined(MACOSX_USE_GLX)))
130     msg += " : Cannot connect to X server ";
131     msg += XDisplayName ((char*) NULL);
132   #endif
133     Aspect_GraphicDeviceDefinitionError::Raise(msg.ToCString());
134   }
135
136   if (getenv("CALL_OPENGL_NO_DBF") != NULL)
137     myDBuffer = Standard_False;
138
139   if (getenv("CALL_OPENGL_NO_DITHER") != NULL)
140     myDither = Standard_False;
141
142   if (getenv("CALL_OPENGL_NO_BACKDITHER") != NULL)
143     myBackDither = Standard_False;
144
145   if (getenv("CSF_WALKTHROUGH") != NULL)
146     myWalkthrough = Standard_True;
147
148   /* OCC18942: Test if symmetric perspective projection should be turned on */
149   if (getenv("CSF_SYM_PERSPECTIVE") != NULL)
150     mySymPerspective = Standard_True;
151
152   const char* pvalue = getenv("CALL_OPENGL_POLYGON_OFFSET");
153   if (pvalue)
154   {
155     float v1, v2;
156     const int n = sscanf(pvalue, "%f %f", &v1, &v2);
157     if (n > 0) myOffsetFactor = v1;
158     if (n > 1) myOffsetUnits  = v2;
159   }
160
161   pvalue = getenv("CALL_OPENGL_ANTIALIASING_MODE");
162   if (pvalue)
163   {
164     int v;
165     if ( sscanf(pvalue,"%d",&v) > 0 ) myAntiAliasingMode = v;
166   }
167 }