1 // Created on: 2011-10-20
2 // Created by: Sergey ZERCHANINOV
3 // Copyright (c) 2011-2013 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
20 #include <OpenGl_GraphicDriver.hxx>
21 #include <OpenGl_Context.hxx>
22 #include <OpenGl_Flipper.hxx>
23 #include <OpenGl_GraduatedTrihedron.hxx>
24 #include <OpenGl_Group.hxx>
25 #include <OpenGl_CView.hxx>
26 #include <OpenGl_View.hxx>
27 #include <OpenGl_StencilTest.hxx>
28 #include <OpenGl_Text.hxx>
29 #include <OpenGl_Trihedron.hxx>
30 #include <OpenGl_Workspace.hxx>
32 #include <OSD_Environment.hxx>
33 #include <Standard_NotImplemented.hxx>
35 #if (!defined(_WIN32) && (!defined(__APPLE__) || defined(MACOSX_USE_GLX)))
36 #include <X11/Xlib.h> // XOpenDisplay()
39 IMPLEMENT_STANDARD_HANDLE(OpenGl_GraphicDriver,Graphic3d_GraphicDriver)
40 IMPLEMENT_STANDARD_RTTIEXT(OpenGl_GraphicDriver,Graphic3d_GraphicDriver)
44 static const Handle(OpenGl_Context) TheNullGlCtx;
47 // Pour eviter de "mangler" MetaGraphicDriverFactory, le nom de la
48 // fonction qui cree un Graphic3d_GraphicDriver.
49 // En effet, ce nom est recherche par la methode DlSymb de la
50 // classe OSD_SharedLibrary dans la methode SetGraphicDriver de la
51 // classe Graphic3d_GraphicDevice
53 #if defined(_MSC_VER) // disable MS VC++ warning on C-style function returning C++ object
55 #pragma warning(disable:4190)
57 Standard_EXPORT Handle(Graphic3d_GraphicDriver) MetaGraphicDriverFactory (const Standard_CString theShrName)
59 Handle(OpenGl_GraphicDriver) aDriver = new OpenGl_GraphicDriver (theShrName);
67 // =======================================================================
68 // function : OpenGl_GraphicDriver
70 // =======================================================================
71 OpenGl_GraphicDriver::OpenGl_GraphicDriver (const Handle(Aspect_DisplayConnection)& theDisplayConnection)
72 : Graphic3d_GraphicDriver ("TKOpenGl"),
73 myCaps (new OpenGl_Caps()),
74 myMapOfView (1, NCollection_BaseAllocator::CommonBaseAllocator()),
75 myMapOfWS (1, NCollection_BaseAllocator::CommonBaseAllocator()),
76 myMapOfStructure (1, NCollection_BaseAllocator::CommonBaseAllocator()),
77 myUserDrawCallback (NULL),
78 myTempText (new OpenGl_Text())
80 Begin (theDisplayConnection);
83 // =======================================================================
84 // function : OpenGl_GraphicDriver
86 // =======================================================================
87 OpenGl_GraphicDriver::OpenGl_GraphicDriver (const Standard_CString theShrName)
88 : Graphic3d_GraphicDriver (theShrName),
89 myCaps (new OpenGl_Caps()),
90 myMapOfView (1, NCollection_BaseAllocator::CommonBaseAllocator()),
91 myMapOfWS (1, NCollection_BaseAllocator::CommonBaseAllocator()),
92 myMapOfStructure (1, NCollection_BaseAllocator::CommonBaseAllocator()),
93 myUserDrawCallback (NULL),
94 myTempText (new OpenGl_Text())
99 // =======================================================================
102 // =======================================================================
103 Standard_Boolean OpenGl_GraphicDriver::Begin (const Handle(Aspect_DisplayConnection)& theDisplayConnection)
105 myDisplayConnection = theDisplayConnection;
106 if (myDisplayConnection.IsNull())
108 #if (!defined(_WIN32) && (!defined(__APPLE__) || defined(MACOSX_USE_GLX)))
109 //Aspect_GraphicDeviceDefinitionError::Raise ("OpenGl_GraphicDriver: cannot connect to X server!");
110 return Standard_False;
112 return Standard_True;
116 #if (!defined(_WIN32) && (!defined(__APPLE__) || defined(MACOSX_USE_GLX)))
117 Display* aDisplay = myDisplayConnection->GetDisplay();
118 Bool toSync = ::getenv ("CSF_GraphicSync") != NULL
119 || ::getenv ("CALL_SYNCHRO_X") != NULL;
120 XSynchronize (aDisplay, toSync);
122 // does the server know about OpenGL & GLX?
124 if (!XQueryExtension (aDisplay, "GLX", &aDummy, &aDummy, &aDummy))
127 std::cerr << "This system doesn't appear to support OpenGL\n";
131 return Standard_True;
134 // =======================================================================
137 // =======================================================================
138 void OpenGl_GraphicDriver::End()
141 ///myDisplayConnection.Nullify();
144 // =======================================================================
145 // function : InquireLightLimit
147 // =======================================================================
148 Standard_Integer OpenGl_GraphicDriver::InquireLightLimit()
150 return OpenGLMaxLights;
153 // =======================================================================
154 // function : InquireViewLimit
156 // =======================================================================
157 Standard_Integer OpenGl_GraphicDriver::InquireViewLimit()
162 // =======================================================================
163 // function : InquirePlaneLimit
165 // =======================================================================
166 Standard_Integer OpenGl_GraphicDriver::InquirePlaneLimit()
168 // NOTE the 2 first planes are reserved for ZClipping
169 const Handle(OpenGl_Context)& aCtx = GetSharedContext();
170 return aCtx.IsNull() ? 0 : Max (aCtx->MaxClipPlanes() - 2, 0);
173 // =======================================================================
174 // function : UserDrawCallback
176 // =======================================================================
177 OpenGl_GraphicDriver::OpenGl_UserDrawCallback_t& OpenGl_GraphicDriver::UserDrawCallback()
179 return myUserDrawCallback;
182 // =======================================================================
183 // function : DefaultTextHeight
185 // =======================================================================
186 Standard_ShortReal OpenGl_GraphicDriver::DefaultTextHeight() const
191 // =======================================================================
192 // function : EnableVBO
194 // =======================================================================
195 void OpenGl_GraphicDriver::EnableVBO (const Standard_Boolean theToTurnOn)
197 myCaps->vboDisable = !theToTurnOn;
200 // =======================================================================
201 // function : GetSharedContext
203 // =======================================================================
204 const Handle(OpenGl_Context)& OpenGl_GraphicDriver::GetSharedContext() const
206 if (myMapOfWS.IsEmpty())
211 NCollection_DataMap<Standard_Integer, Handle(OpenGl_Workspace)>::Iterator anIter (myMapOfWS);
212 return anIter.Value()->GetGlContext();
215 // =======================================================================
216 // function : MemoryInfo
218 // =======================================================================
219 Standard_Boolean OpenGl_GraphicDriver::MemoryInfo (Standard_Size& theFreeBytes,
220 TCollection_AsciiString& theInfo) const
222 // this is extra work (for OpenGl_Context initialization)...
223 OpenGl_Context aGlCtx;
226 return Standard_False;
228 theFreeBytes = aGlCtx.AvailableMemory();
229 theInfo = aGlCtx.MemoryInfo();
230 return !theInfo.IsEmpty();
233 // =======================================================================
234 // function : SetImmediateModeDrawToFront
236 // =======================================================================
237 Standard_Boolean OpenGl_GraphicDriver::SetImmediateModeDrawToFront (const Graphic3d_CView& theCView,
238 const Standard_Boolean theDrawToFrontBuffer)
240 if (theCView.ViewId == -1)
242 return Standard_False;
245 const OpenGl_CView* aCView = (const OpenGl_CView* )theCView.ptrView;
248 return aCView->WS->SetImmediateModeDrawToFront (theDrawToFrontBuffer);
250 return Standard_False;
253 // =======================================================================
254 // function : DisplayImmediateStructure
256 // =======================================================================
257 void OpenGl_GraphicDriver::DisplayImmediateStructure (const Graphic3d_CView& theCView,
258 const Graphic3d_CStructure& theCStructure)
260 OpenGl_CView* aCView = (OpenGl_CView* )theCView.ptrView;
261 OpenGl_Structure* aStructure = (OpenGl_Structure* )&theCStructure;
267 aCView->View->DisplayImmediateStructure (aStructure);
270 // =======================================================================
271 // function : EraseImmediateStructure
273 // =======================================================================
274 void OpenGl_GraphicDriver::EraseImmediateStructure (const Graphic3d_CView& theCView,
275 const Graphic3d_CStructure& theCStructure)
277 OpenGl_CView* aCView = (OpenGl_CView* )theCView.ptrView;
278 OpenGl_Structure* aStructure = (OpenGl_Structure* )&theCStructure;
284 aCView->View->EraseImmediateStructure (aStructure);
288 // =======================================================================
291 // =======================================================================
292 Standard_Boolean OpenGl_GraphicDriver::Print (const Graphic3d_CView& theCView,
293 const Aspect_CLayer2d& theCUnderLayer,
294 const Aspect_CLayer2d& theCOverLayer,
295 const Aspect_Handle thePrintDC,
296 const Standard_Boolean theToShowBackground,
297 const Standard_CString theFilename,
298 const Aspect_PrintAlgo thePrintAlgorithm,
299 const Standard_Real theScaleFactor) const
301 const OpenGl_CView* aCView = (const OpenGl_CView* )theCView.ptrView;
303 || !myPrintContext.IsNull())
305 return Standard_False;
308 Standard_Boolean isPrinted = Standard_False;
309 myPrintContext = new OpenGl_PrinterContext();
311 isPrinted = aCView->WS->Print (myPrintContext,
321 Standard_NotImplemented::Raise ("OpenGl_GraphicDriver::Print is implemented only on Windows");
323 myPrintContext.Nullify();
327 // =======================================================================
328 // function : ZBufferTriedronSetup
330 // =======================================================================
331 void OpenGl_GraphicDriver::ZBufferTriedronSetup (const Quantity_NameOfColor theXColor,
332 const Quantity_NameOfColor theYColor,
333 const Quantity_NameOfColor theZColor,
334 const Standard_Real theSizeRatio,
335 const Standard_Real theAxisDiametr,
336 const Standard_Integer theNbFacettes)
338 OpenGl_Trihedron::Setup (theXColor, theYColor, theZColor, theSizeRatio, theAxisDiametr, theNbFacettes);
341 // =======================================================================
342 // function : TriedronDisplay
344 // =======================================================================
345 void OpenGl_GraphicDriver::TriedronDisplay (const Graphic3d_CView& theCView,
346 const Aspect_TypeOfTriedronPosition thePosition,
347 const Quantity_NameOfColor theColor,
348 const Standard_Real theScale,
349 const Standard_Boolean theAsWireframe)
351 const OpenGl_CView* aCView = (const OpenGl_CView* )theCView.ptrView;
354 aCView->View->TriedronDisplay (aCView->WS->GetGlContext(), thePosition, theColor, theScale, theAsWireframe);
358 // =======================================================================
359 // function : TriedronErase
361 // =======================================================================
362 void OpenGl_GraphicDriver::TriedronErase (const Graphic3d_CView& theCView)
364 const OpenGl_CView* aCView = (const OpenGl_CView* )theCView.ptrView;
367 aCView->View->TriedronErase (aCView->WS->GetGlContext());
371 // =======================================================================
372 // function : TriedronEcho
374 // =======================================================================
375 void OpenGl_GraphicDriver::TriedronEcho (const Graphic3d_CView& ,
376 const Aspect_TypeOfTriedronEcho )
381 // =======================================================================
382 // function : Environment
384 // =======================================================================
385 void OpenGl_GraphicDriver::Environment (const Graphic3d_CView& theCView)
387 const OpenGl_CView* aCView = (const OpenGl_CView* )theCView.ptrView;
393 aCView->View->SetTextureEnv (aCView->WS->GetGlContext(), theCView.Context.TextureEnv);
394 aCView->View->SetSurfaceDetail ((Visual3d_TypeOfSurfaceDetail)theCView.Context.SurfaceDetail);
397 // =======================================================================
398 // function : BackgroundImage
400 // =======================================================================
401 void OpenGl_GraphicDriver::BackgroundImage (const Standard_CString theFileName,
402 const Graphic3d_CView& theCView,
403 const Aspect_FillMethod theFillStyle)
405 const OpenGl_CView* aCView = (const OpenGl_CView* )theCView.ptrView;
408 aCView->View->CreateBackgroundTexture (theFileName, theFillStyle);
412 // =======================================================================
413 // function : SetBgImageStyle
415 // =======================================================================
416 void OpenGl_GraphicDriver::SetBgImageStyle (const Graphic3d_CView& theCView,
417 const Aspect_FillMethod theFillStyle)
419 const OpenGl_CView* aCView = (const OpenGl_CView* )theCView.ptrView;
422 aCView->View->SetBackgroundTextureStyle (theFillStyle);
426 // =======================================================================
427 // function : SetBgGradientStyle
429 // =======================================================================
430 void OpenGl_GraphicDriver::SetBgGradientStyle (const Graphic3d_CView& theCView,
431 const Aspect_GradientFillMethod theFillType)
433 const OpenGl_CView* aCView = (const OpenGl_CView* )theCView.ptrView;
436 aCView->View->SetBackgroundGradientType (theFillType);
440 // =======================================================================
441 // function : GraduatedTrihedronDisplay
443 // =======================================================================
444 void OpenGl_GraphicDriver::GraduatedTrihedronDisplay (const Graphic3d_CView& theCView,
445 const Graphic3d_CGraduatedTrihedron& theCubic)
447 const OpenGl_CView* aCView = (const OpenGl_CView* )theCView.ptrView;
450 aCView->View->GraduatedTrihedronDisplay (aCView->WS->GetGlContext(), theCubic);
454 // =======================================================================
455 // function : GraduatedTrihedronErase
457 // =======================================================================
458 void OpenGl_GraphicDriver::GraduatedTrihedronErase (const Graphic3d_CView& theCView)
460 const OpenGl_CView* aCView = (const OpenGl_CView* )theCView.ptrView;
463 aCView->View->GraduatedTrihedronErase (aCView->WS->GetGlContext());
467 // =======================================================================
468 // function : GraduatedTrihedronMinMaxValues
470 // =======================================================================
471 void OpenGl_GraphicDriver::GraduatedTrihedronMinMaxValues (const Standard_ShortReal theMinX,
472 const Standard_ShortReal theMinY,
473 const Standard_ShortReal theMinZ,
474 const Standard_ShortReal theMaxX,
475 const Standard_ShortReal theMaxY,
476 const Standard_ShortReal theMaxZ)
478 OpenGl_GraduatedTrihedron::SetMinMax (theMinX, theMinY, theMinZ, theMaxX, theMaxY, theMaxZ);