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__) && (!defined(__APPLE__) || defined(MACOSX_USE_GLX)))
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__)) || (defined(__APPLE__) && !defined(MACOSX_USE_GLX))
43 static char* TheDummyDisplay = "DISPLAY";
46 static const OpenGl_Facilities myDefaultFacilities = { 1, 1, 1, 1, OpenGLMaxLights, 10000 };
49 /*----------------------------------------------------------------------*/
51 OpenGl_Display::OpenGl_Display (const Handle(Aspect_DisplayConnection)& theDisplayConnection)
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),
66 #if (defined(_WIN32) || defined(__WIN32__)) || (defined(__APPLE__) && !defined(MACOSX_USE_GLX))
67 myDisplay = TheDummyDisplay;
69 myDisplay = theDisplayConnection->GetDisplay();
75 /*----------------------------------------------------------------------*/
77 OpenGl_Display::~OpenGl_Display ()
82 glDeleteLists((GLuint)myLinestyleBase,5);
85 // Delete surface patterns
88 glDeleteLists((GLuint)myPatternBase,TEL_HS_USER_DEF_START);
94 glDeleteLists((GLuint)myMarkerBase,60);
97 // Delete user markers
98 OpenGl_MapOfUserMarker::Iterator itm(myMapOfUM);
99 for (; itm.More(); itm.Next())
101 const OPENGL_MARKER_DATA &aData = itm.Value();
104 delete[] aData.Array;
106 else if (aData.ListId != 0)
108 glDeleteLists ( aData.ListId, 1 );
114 /*----------------------------------------------------------------------*/
116 Handle(OpenGl_Window) OpenGl_Display::GetWindow (const Aspect_Drawable AParent) const
118 Handle(OpenGl_Window) aWindow;
119 if ( myMapOfWindows.IsBound( AParent ) )
121 aWindow = myMapOfWindows.Find( AParent );
126 /*----------------------------------------------------------------------*/
128 void OpenGl_Display::SetWindow (const Aspect_Drawable AParent, const Handle(OpenGl_Window) &AWindow)
130 if ( !myMapOfWindows.IsBound( AParent ) )
132 myMapOfWindows.Bind( AParent, AWindow );
136 /*----------------------------------------------------------------------*/
138 //GenerateMarkerBitmap
139 void OpenGl_Display::AddUserMarker (const Standard_Integer AIndex,
140 const Standard_Integer AMarkWidth,
141 const Standard_Integer AMarkHeight,
142 const Handle(TColStd_HArray1OfByte)& ATexture)
144 if (!myMapOfUM.IsBound(AIndex))
146 const OPENGL_MARKER_DATA anEmptyData = { 0, 0, 0, NULL };
147 myMapOfUM.Bind(AIndex,anEmptyData);
150 OPENGL_MARKER_DATA &aData = myMapOfUM.ChangeFind(AIndex);
154 delete[] aData.Array;
158 unsigned char *anArray = new unsigned char[ATexture->Length()];
160 const int aByteWidth = AMarkWidth / 8;
161 int i, anIndex = ATexture->Upper() - ATexture->Lower() - aByteWidth + 1;
162 for ( ; anIndex >= 0; anIndex -= aByteWidth )
163 for ( i = 0; i < aByteWidth; i++ )
164 anArray[ATexture->Upper() - ATexture->Lower() - aByteWidth + 1 - anIndex + i ] = ATexture->Value( anIndex + i + 1 );
166 aData.Width = AMarkWidth;
167 aData.Height = AMarkHeight;
168 aData.Array = anArray;
171 /*----------------------------------------------------------------------*/
173 void OpenGl_Display::UpdateUserMarkers ()
175 OpenGl_MapOfUserMarker::Iterator itm(myMapOfUM);
176 for (; itm.More(); itm.Next())
178 OPENGL_MARKER_DATA &aData = itm.ChangeValue();
181 if (aData.ListId == 0)
182 aData.ListId = glGenLists(1);
184 glNewList( (GLuint)aData.ListId, GL_COMPILE );
186 GLint w = ( GLsizei ) aData.Width;
187 GLint h = ( GLsizei ) aData.Height;
189 0.5F * ( float )aData.Width, 0.5F * ( float )aData.Height,
190 ( float )30.0, ( float )30.0,
191 ( GLubyte* )aData.Array );
195 delete[] aData.Array;
201 /*----------------------------------------------------------------------*/
203 Standard_Integer OpenGl_Display::GetUserMarkerListIndex (const Standard_Integer AIndex) const
205 if (myMapOfUM.IsBound(AIndex))
207 const OPENGL_MARKER_DATA &aData = myMapOfUM.Find(AIndex);
214 /*----------------------------------------------------------------------*/
216 void OpenGl_Display::Init()
218 if (myDisplay != NULL)
220 #if (!defined(_WIN32) && !defined(__WIN32__) && (!defined(__APPLE__) || defined(MACOSX_USE_GLX)))
221 XSynchronize ((Display* )myDisplay, (getenv("CALL_SYNCHRO_X") != NULL) ? 1 : 0);
223 if (getenv("CSF_GraphicSync") != NULL)
224 XSynchronize ((Display* )myDisplay, 1);
226 // does the server know about OpenGL & GLX?
228 if (!XQueryExtension ((Display* )myDisplay, "GLX", &aDummy, &aDummy, &aDummy))
231 std::cerr << "This system doesn't appear to support OpenGL\n";
238 TCollection_AsciiString msg("OpenGl_Display::Init");
239 #if (!defined(_WIN32) && !defined(__WIN32__) && (!defined(__APPLE__) || defined(MACOSX_USE_GLX)))
240 msg += " : Cannot connect to X server ";
241 msg += XDisplayName ((char*) NULL);
243 Aspect_GraphicDeviceDefinitionError::Raise(msg.ToCString());
246 if (getenv("CALL_OPENGL_NO_DBF") != NULL)
247 myDBuffer = Standard_False;
249 if (getenv("CALL_OPENGL_NO_DITHER") != NULL)
250 myDither = Standard_False;
252 if (getenv("CALL_OPENGL_NO_BACKDITHER") != NULL)
253 myBackDither = Standard_False;
255 if (getenv("CSF_WALKTHROUGH") != NULL)
256 myWalkthrough = Standard_True;
258 /* OCC18942: Test if symmetric perspective projection should be turned on */
259 if (getenv("CSF_SYM_PERSPECTIVE") != NULL)
260 mySymPerspective = Standard_True;
262 const char* pvalue = getenv("CALL_OPENGL_POLYGON_OFFSET");
266 const int n = sscanf(pvalue, "%f %f", &v1, &v2);
267 if (n > 0) myOffsetFactor = v1;
268 if (n > 1) myOffsetUnits = v2;
271 pvalue = getenv("CALL_OPENGL_ANTIALIASING_MODE");
275 if ( sscanf(pvalue,"%d",&v) > 0 ) myAntiAliasingMode = v;