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 <OpenGl_GlCore11.hxx>
23 #include <OpenGl_Display.hxx>
25 #include <OSD_Environment.hxx>
26 #include <TCollection_AsciiString.hxx>
27 #include <Aspect_GraphicDeviceDefinitionError.hxx>
29 #include <OpenGl_Light.hxx>
31 #if (!defined(_WIN32) && !defined(__WIN32__))
32 #include <X11/Xlib.h> // XOpenDisplay()
35 IMPLEMENT_STANDARD_HANDLE(OpenGl_Display,MMgt_TShared)
36 IMPLEMENT_STANDARD_RTTIEXT(OpenGl_Display,MMgt_TShared)
38 Handle(OpenGl_Display) openglDisplay;
42 #if (defined(_WIN32) || defined(__WIN32__))
43 static char* TheDummyDisplay = "DISPLAY";
46 static const OpenGl_Facilities myDefaultFacilities = { 1, 1, 1, 1, OpenGLMaxLights, 10000 };
49 /*----------------------------------------------------------------------*/
51 OpenGl_Display::OpenGl_Display (const Standard_CString theDisplay)
53 myFacilities(myDefaultFacilities),
54 myDBuffer(Standard_True),
55 myDither(Standard_True),
56 myBackDither(Standard_False),
57 myWalkthrough(Standard_False),
58 mySymPerspective(Standard_False),
61 myAntiAliasingMode(3),
68 #if (defined(_WIN32) || defined(__WIN32__))
69 myDisplay = TheDummyDisplay;
71 if (theDisplay != NULL && *theDisplay != '\0')
73 OSD_Environment aDispEnv ("DISPLAY", theDisplay);
77 // Specifies the hardware display name, which determines the
78 // display and communications domain to be used.
79 // On a POSIX system, if the display_name is NULL, it defaults
80 // to the value of the DISPLAY environment variable.
81 myDisplay = XOpenDisplay (NULL);
87 /*----------------------------------------------------------------------*/
89 OpenGl_Display::OpenGl_Display (const Aspect_Display theDisplay)
91 myFacilities(myDefaultFacilities),
92 myDBuffer(Standard_True),
93 myDither(Standard_True),
94 myBackDither(Standard_False),
95 myWalkthrough(Standard_False),
96 mySymPerspective(Standard_False),
99 myAntiAliasingMode(3),
106 #if (defined(_WIN32) || defined(__WIN32__))
107 myDisplay = TheDummyDisplay;
109 myDisplay = theDisplay;
115 /*----------------------------------------------------------------------*/
117 OpenGl_Display::~OpenGl_Display ()
119 // Delete line styles
122 glDeleteLists((GLuint)myLinestyleBase,5);
125 // Delete surface patterns
128 glDeleteLists((GLuint)myPatternBase,TEL_HS_USER_DEF_START);
134 glDeleteLists((GLuint)myMarkerBase,60);
137 // Delete user markers
138 OpenGl_MapOfUserMarker::Iterator itm(myMapOfUM);
139 for (; itm.More(); itm.Next())
141 const OPENGL_MARKER_DATA &aData = itm.Value();
144 delete[] aData.Array;
146 else if (aData.ListId != 0)
148 glDeleteLists ( aData.ListId, 1 );
154 /*----------------------------------------------------------------------*/
156 Handle(OpenGl_Window) OpenGl_Display::GetWindow (const Aspect_Drawable AParent) const
158 Handle(OpenGl_Window) aWindow;
159 if ( myMapOfWindows.IsBound( AParent ) )
161 aWindow = myMapOfWindows.Find( AParent );
166 /*----------------------------------------------------------------------*/
168 void OpenGl_Display::SetWindow (const Aspect_Drawable AParent, const Handle(OpenGl_Window) &AWindow)
170 if ( !myMapOfWindows.IsBound( AParent ) )
172 myMapOfWindows.Bind( AParent, AWindow );
176 /*----------------------------------------------------------------------*/
178 //GenerateMarkerBitmap
179 void OpenGl_Display::AddUserMarker (const Standard_Integer AIndex,
180 const Standard_Integer AMarkWidth,
181 const Standard_Integer AMarkHeight,
182 const Handle(TColStd_HArray1OfByte)& ATexture)
184 if (!myMapOfUM.IsBound(AIndex))
186 const OPENGL_MARKER_DATA anEmptyData = { 0, 0, 0, NULL };
187 myMapOfUM.Bind(AIndex,anEmptyData);
190 OPENGL_MARKER_DATA &aData = myMapOfUM.ChangeFind(AIndex);
194 delete[] aData.Array;
198 unsigned char *anArray = new unsigned char[ATexture->Length()];
200 const int aByteWidth = AMarkWidth / 8;
201 int i, anIndex = ATexture->Upper() - ATexture->Lower() - aByteWidth + 1;
202 for ( ; anIndex >= 0; anIndex -= aByteWidth )
203 for ( i = 0; i < aByteWidth; i++ )
204 anArray[ATexture->Upper() - ATexture->Lower() - aByteWidth + 1 - anIndex + i ] = ATexture->Value( anIndex + i + 1 );
206 aData.Width = AMarkWidth;
207 aData.Height = AMarkHeight;
208 aData.Array = anArray;
211 /*----------------------------------------------------------------------*/
213 void OpenGl_Display::UpdateUserMarkers ()
215 OpenGl_MapOfUserMarker::Iterator itm(myMapOfUM);
216 for (; itm.More(); itm.Next())
218 OPENGL_MARKER_DATA &aData = itm.ChangeValue();
221 if (aData.ListId == 0)
222 aData.ListId = glGenLists(1);
224 glNewList( (GLuint)aData.ListId, GL_COMPILE );
226 GLint w = ( GLsizei ) aData.Width;
227 GLint h = ( GLsizei ) aData.Height;
229 0.5F * ( float )aData.Width, 0.5F * ( float )aData.Height,
230 ( float )30.0, ( float )30.0,
231 ( GLubyte* )aData.Array );
235 delete[] aData.Array;
241 /*----------------------------------------------------------------------*/
243 Standard_Integer OpenGl_Display::GetUserMarkerListIndex (const Standard_Integer AIndex) const
245 if (myMapOfUM.IsBound(AIndex))
247 const OPENGL_MARKER_DATA &aData = myMapOfUM.Find(AIndex);
254 /*----------------------------------------------------------------------*/
256 void OpenGl_Display::Init()
258 if (myDisplay != NULL)
260 #if (!defined(_WIN32) && !defined(__WIN32__))
261 XSynchronize ((Display* )myDisplay, (getenv("CALL_SYNCHRO_X") != NULL) ? 1 : 0);
263 if (getenv("CSF_GraphicSync") != NULL)
264 XSynchronize ((Display* )myDisplay, 1);
266 // does the server know about OpenGL & GLX?
268 if (!XQueryExtension ((Display* )myDisplay, "GLX", &aDummy, &aDummy, &aDummy))
271 std::cerr << "This system doesn't appear to support OpenGL\n";
278 TCollection_AsciiString msg("OpenGl_Display::Init");
279 #if (!defined(_WIN32) && !defined(__WIN32__))
280 msg += " : Cannot connect to X server ";
281 msg += XDisplayName ((char*) NULL);
283 Aspect_GraphicDeviceDefinitionError::Raise(msg.ToCString());
286 if (getenv("CALL_OPENGL_NO_DBF") != NULL)
287 myDBuffer = Standard_False;
289 if (getenv("CALL_OPENGL_NO_DITHER") != NULL)
290 myDither = Standard_False;
292 if (getenv("CALL_OPENGL_NO_BACKDITHER") != NULL)
293 myBackDither = Standard_False;
295 if (getenv("CSF_WALKTHROUGH") != NULL)
296 myWalkthrough = Standard_True;
298 /* OCC18942: Test if symmetric perspective projection should be turned on */
299 if (getenv("CSF_SYM_PERSPECTIVE") != NULL)
300 mySymPerspective = Standard_True;
302 const char* pvalue = getenv("CALL_OPENGL_POLYGON_OFFSET");
306 const int n = sscanf(pvalue, "%f %f", &v1, &v2);
307 if (n > 0) myOffsetFactor = v1;
308 if (n > 1) myOffsetUnits = v2;
311 pvalue = getenv("CALL_OPENGL_ANTIALIASING_MODE");
315 if ( sscanf(pvalue,"%d",&v) > 0 ) myAntiAliasingMode = v;