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