0023814: Drop plugin interface for Graphic3d_GraphicDriver instantiation
[occt.git] / src / OpenGl / OpenGl_GraphicDriver.cxx
CommitLineData
b311480e 1// Created on: 2011-10-20
2// Created by: Sergey ZERCHANINOV
1981cb22 3// Copyright (c) 2011-2013 OPEN CASCADE SAS
b311480e 4//
973c2be1 5// This file is part of Open CASCADE Technology software library.
b311480e 6//
d5f74e42 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
973c2be1 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.
b311480e 12//
973c2be1 13// Alternatively, this file may be used under the terms of Open CASCADE
14// commercial license or contractual agreement.
b311480e 15
e276548b 16#ifdef HAVE_CONFIG_H
17 #include <config.h>
18#endif
19
2166f0fa 20#include <OpenGl_GraphicDriver.hxx>
f0430952 21#include <OpenGl_Context.hxx>
938d4544 22#include <OpenGl_Flipper.hxx>
a174a3c5 23#include <OpenGl_GraduatedTrihedron.hxx>
24#include <OpenGl_Group.hxx>
1981cb22 25#include <OpenGl_CView.hxx>
2166f0fa 26#include <OpenGl_View.hxx>
a6eb515f 27#include <OpenGl_StencilTest.hxx>
a174a3c5 28#include <OpenGl_Text.hxx>
29#include <OpenGl_Trihedron.hxx>
2166f0fa 30#include <OpenGl_Workspace.hxx>
7fd59977 31
73192b37 32#include <OSD_Environment.hxx>
a174a3c5 33#include <Standard_NotImplemented.hxx>
34
73192b37 35#if (!defined(_WIN32) && (!defined(__APPLE__) || defined(MACOSX_USE_GLX)))
36 #include <X11/Xlib.h> // XOpenDisplay()
37#endif
38
2166f0fa
SK
39IMPLEMENT_STANDARD_HANDLE(OpenGl_GraphicDriver,Graphic3d_GraphicDriver)
40IMPLEMENT_STANDARD_RTTIEXT(OpenGl_GraphicDriver,Graphic3d_GraphicDriver)
7fd59977 41
2166f0fa
SK
42namespace
43{
5e27df78 44 static const Handle(OpenGl_Context) TheNullGlCtx;
7fd59977 45}
46
65993a95 47// =======================================================================
48// function : OpenGl_GraphicDriver
49// purpose :
50// =======================================================================
fe9fc669 51OpenGl_GraphicDriver::OpenGl_GraphicDriver (const Handle(Aspect_DisplayConnection)& theDisp)
52: Graphic3d_GraphicDriver (theDisp),
65993a95 53 myCaps (new OpenGl_Caps()),
54 myMapOfView (1, NCollection_BaseAllocator::CommonBaseAllocator()),
55 myMapOfWS (1, NCollection_BaseAllocator::CommonBaseAllocator()),
56 myMapOfStructure (1, NCollection_BaseAllocator::CommonBaseAllocator()),
57 myUserDrawCallback (NULL),
58 myTempText (new OpenGl_Text())
59{
fe9fc669 60#if (!defined(_WIN32) && (!defined(__APPLE__) || defined(MACOSX_USE_GLX)))
73192b37 61 if (myDisplayConnection.IsNull())
62 {
73192b37 63 //Aspect_GraphicDeviceDefinitionError::Raise ("OpenGl_GraphicDriver: cannot connect to X server!");
fe9fc669 64 return;
73192b37 65 }
66
73192b37 67 Display* aDisplay = myDisplayConnection->GetDisplay();
68 Bool toSync = ::getenv ("CSF_GraphicSync") != NULL
69 || ::getenv ("CALL_SYNCHRO_X") != NULL;
70 XSynchronize (aDisplay, toSync);
71
72 // does the server know about OpenGL & GLX?
73 int aDummy;
74 if (!XQueryExtension (aDisplay, "GLX", &aDummy, &aDummy, &aDummy))
75 {
76 #ifdef DEBUG
77 std::cerr << "This system doesn't appear to support OpenGL\n";
78 #endif
79 }
80#endif
73192b37 81}
82
83// =======================================================================
84// function : InquireLightLimit
85// purpose :
86// =======================================================================
87Standard_Integer OpenGl_GraphicDriver::InquireLightLimit()
88{
89 return OpenGLMaxLights;
90}
91
92// =======================================================================
93// function : InquireViewLimit
94// purpose :
95// =======================================================================
96Standard_Integer OpenGl_GraphicDriver::InquireViewLimit()
97{
98 return 10000;
99}
100
101// =======================================================================
102// function : InquirePlaneLimit
103// purpose :
104// =======================================================================
105Standard_Integer OpenGl_GraphicDriver::InquirePlaneLimit()
106{
107 // NOTE the 2 first planes are reserved for ZClipping
108 const Handle(OpenGl_Context)& aCtx = GetSharedContext();
109 return aCtx.IsNull() ? 0 : Max (aCtx->MaxClipPlanes() - 2, 0);
110}
111
f0430952 112// =======================================================================
5e27df78 113// function : UserDrawCallback
f0430952 114// purpose :
115// =======================================================================
5e27df78 116OpenGl_GraphicDriver::OpenGl_UserDrawCallback_t& OpenGl_GraphicDriver::UserDrawCallback()
2166f0fa 117{
5e27df78 118 return myUserDrawCallback;
2166f0fa
SK
119}
120
f0430952 121// =======================================================================
5e27df78 122// function : DefaultTextHeight
f0430952 123// purpose :
124// =======================================================================
5e27df78 125Standard_ShortReal OpenGl_GraphicDriver::DefaultTextHeight() const
2166f0fa 126{
5e27df78 127 return 16.;
2166f0fa
SK
128}
129
f0430952 130// =======================================================================
131// function : EnableVBO
132// purpose :
133// =======================================================================
2166f0fa 134void OpenGl_GraphicDriver::EnableVBO (const Standard_Boolean theToTurnOn)
7fd59977 135{
58655684 136 myCaps->vboDisable = !theToTurnOn;
7fd59977 137}
f0430952 138
5e27df78 139// =======================================================================
140// function : GetSharedContext
141// purpose :
142// =======================================================================
143const Handle(OpenGl_Context)& OpenGl_GraphicDriver::GetSharedContext() const
144{
145 if (myMapOfWS.IsEmpty())
146 {
147 return TheNullGlCtx;
148 }
149
150 NCollection_DataMap<Standard_Integer, Handle(OpenGl_Workspace)>::Iterator anIter (myMapOfWS);
151 return anIter.Value()->GetGlContext();
152}
153
f0430952 154// =======================================================================
155// function : MemoryInfo
156// purpose :
157// =======================================================================
158Standard_Boolean OpenGl_GraphicDriver::MemoryInfo (Standard_Size& theFreeBytes,
159 TCollection_AsciiString& theInfo) const
160{
161 // this is extra work (for OpenGl_Context initialization)...
162 OpenGl_Context aGlCtx;
163 if (!aGlCtx.Init())
164 {
165 return Standard_False;
166 }
167 theFreeBytes = aGlCtx.AvailableMemory();
168 theInfo = aGlCtx.MemoryInfo();
169 return !theInfo.IsEmpty();
170}
1981cb22 171
172// =======================================================================
173// function : SetImmediateModeDrawToFront
174// purpose :
175// =======================================================================
176Standard_Boolean OpenGl_GraphicDriver::SetImmediateModeDrawToFront (const Graphic3d_CView& theCView,
177 const Standard_Boolean theDrawToFrontBuffer)
178{
179 if (theCView.ViewId == -1)
180 {
181 return Standard_False;
182 }
183
184 const OpenGl_CView* aCView = (const OpenGl_CView* )theCView.ptrView;
185 if (aCView != NULL)
186 {
187 return aCView->WS->SetImmediateModeDrawToFront (theDrawToFrontBuffer);
188 }
189 return Standard_False;
190}
191
192// =======================================================================
679ecdee 193// function : DisplayImmediateStructure
1981cb22 194// purpose :
195// =======================================================================
679ecdee 196void OpenGl_GraphicDriver::DisplayImmediateStructure (const Graphic3d_CView& theCView,
197 const Graphic3d_CStructure& theCStructure)
1981cb22 198{
679ecdee 199 OpenGl_CView* aCView = (OpenGl_CView* )theCView.ptrView;
200 OpenGl_Structure* aStructure = (OpenGl_Structure* )&theCStructure;
201 if (aCView == NULL)
1981cb22 202 {
679ecdee 203 return;
1981cb22 204 }
205
679ecdee 206 aCView->View->DisplayImmediateStructure (aStructure);
1981cb22 207}
208
209// =======================================================================
679ecdee 210// function : EraseImmediateStructure
1981cb22 211// purpose :
212// =======================================================================
679ecdee 213void OpenGl_GraphicDriver::EraseImmediateStructure (const Graphic3d_CView& theCView,
214 const Graphic3d_CStructure& theCStructure)
1981cb22 215{
679ecdee 216 OpenGl_CView* aCView = (OpenGl_CView* )theCView.ptrView;
63bcc448 217 OpenGl_Structure* aStructure = (OpenGl_Structure* )&theCStructure;
679ecdee 218 if (aCView == NULL)
1981cb22 219 {
679ecdee 220 return;
1981cb22 221 }
1981cb22 222
679ecdee 223 aCView->View->EraseImmediateStructure (aStructure);
1981cb22 224}
a174a3c5 225
679ecdee 226
a174a3c5 227// =======================================================================
228// function : Print
229// purpose :
230// =======================================================================
231Standard_Boolean OpenGl_GraphicDriver::Print (const Graphic3d_CView& theCView,
232 const Aspect_CLayer2d& theCUnderLayer,
233 const Aspect_CLayer2d& theCOverLayer,
234 const Aspect_Handle thePrintDC,
235 const Standard_Boolean theToShowBackground,
236 const Standard_CString theFilename,
237 const Aspect_PrintAlgo thePrintAlgorithm,
238 const Standard_Real theScaleFactor) const
239{
240 const OpenGl_CView* aCView = (const OpenGl_CView* )theCView.ptrView;
241 if (aCView == NULL
242 || !myPrintContext.IsNull())
243 {
244 return Standard_False;
245 }
246
247 Standard_Boolean isPrinted = Standard_False;
248 myPrintContext = new OpenGl_PrinterContext();
249#ifdef _WIN32
250 isPrinted = aCView->WS->Print (myPrintContext,
251 theCView,
252 theCUnderLayer,
253 theCOverLayer,
254 thePrintDC,
255 theToShowBackground,
256 theFilename,
257 thePrintAlgorithm,
258 theScaleFactor);
259#else
260 Standard_NotImplemented::Raise ("OpenGl_GraphicDriver::Print is implemented only on Windows");
261#endif
262 myPrintContext.Nullify();
263 return isPrinted;
264}
265
a174a3c5 266// =======================================================================
267// function : ZBufferTriedronSetup
268// purpose :
269// =======================================================================
270void OpenGl_GraphicDriver::ZBufferTriedronSetup (const Quantity_NameOfColor theXColor,
271 const Quantity_NameOfColor theYColor,
272 const Quantity_NameOfColor theZColor,
273 const Standard_Real theSizeRatio,
274 const Standard_Real theAxisDiametr,
275 const Standard_Integer theNbFacettes)
276{
277 OpenGl_Trihedron::Setup (theXColor, theYColor, theZColor, theSizeRatio, theAxisDiametr, theNbFacettes);
278}
279
280// =======================================================================
281// function : TriedronDisplay
282// purpose :
283// =======================================================================
284void OpenGl_GraphicDriver::TriedronDisplay (const Graphic3d_CView& theCView,
285 const Aspect_TypeOfTriedronPosition thePosition,
286 const Quantity_NameOfColor theColor,
287 const Standard_Real theScale,
288 const Standard_Boolean theAsWireframe)
289{
290 const OpenGl_CView* aCView = (const OpenGl_CView* )theCView.ptrView;
291 if (aCView != NULL)
292 {
293 aCView->View->TriedronDisplay (aCView->WS->GetGlContext(), thePosition, theColor, theScale, theAsWireframe);
294 }
295}
296
297// =======================================================================
298// function : TriedronErase
299// purpose :
300// =======================================================================
301void OpenGl_GraphicDriver::TriedronErase (const Graphic3d_CView& theCView)
302{
303 const OpenGl_CView* aCView = (const OpenGl_CView* )theCView.ptrView;
304 if (aCView != NULL)
305 {
306 aCView->View->TriedronErase (aCView->WS->GetGlContext());
307 }
308}
309
310// =======================================================================
311// function : TriedronEcho
312// purpose :
313// =======================================================================
314void OpenGl_GraphicDriver::TriedronEcho (const Graphic3d_CView& ,
315 const Aspect_TypeOfTriedronEcho )
316{
317 // do nothing
318}
319
320// =======================================================================
321// function : Environment
322// purpose :
323// =======================================================================
324void OpenGl_GraphicDriver::Environment (const Graphic3d_CView& theCView)
325{
326 const OpenGl_CView* aCView = (const OpenGl_CView* )theCView.ptrView;
327 if (aCView == NULL)
328 {
329 return;
330 }
331
332 aCView->View->SetTextureEnv (aCView->WS->GetGlContext(), theCView.Context.TextureEnv);
333 aCView->View->SetSurfaceDetail ((Visual3d_TypeOfSurfaceDetail)theCView.Context.SurfaceDetail);
334}
335
336// =======================================================================
337// function : BackgroundImage
338// purpose :
339// =======================================================================
340void OpenGl_GraphicDriver::BackgroundImage (const Standard_CString theFileName,
341 const Graphic3d_CView& theCView,
342 const Aspect_FillMethod theFillStyle)
343{
344 const OpenGl_CView* aCView = (const OpenGl_CView* )theCView.ptrView;
345 if (aCView != NULL)
346 {
347 aCView->View->CreateBackgroundTexture (theFileName, theFillStyle);
348 }
349}
350
351// =======================================================================
352// function : SetBgImageStyle
353// purpose :
354// =======================================================================
355void OpenGl_GraphicDriver::SetBgImageStyle (const Graphic3d_CView& theCView,
356 const Aspect_FillMethod theFillStyle)
357{
358 const OpenGl_CView* aCView = (const OpenGl_CView* )theCView.ptrView;
359 if (aCView != NULL)
360 {
361 aCView->View->SetBackgroundTextureStyle (theFillStyle);
362 }
363}
364
365// =======================================================================
366// function : SetBgGradientStyle
367// purpose :
368// =======================================================================
369void OpenGl_GraphicDriver::SetBgGradientStyle (const Graphic3d_CView& theCView,
370 const Aspect_GradientFillMethod theFillType)
371{
372 const OpenGl_CView* aCView = (const OpenGl_CView* )theCView.ptrView;
373 if (aCView != NULL)
374 {
375 aCView->View->SetBackgroundGradientType (theFillType);
376 }
377}
378
379// =======================================================================
380// function : GraduatedTrihedronDisplay
381// purpose :
382// =======================================================================
383void OpenGl_GraphicDriver::GraduatedTrihedronDisplay (const Graphic3d_CView& theCView,
384 const Graphic3d_CGraduatedTrihedron& theCubic)
385{
386 const OpenGl_CView* aCView = (const OpenGl_CView* )theCView.ptrView;
387 if (aCView != NULL)
388 {
389 aCView->View->GraduatedTrihedronDisplay (aCView->WS->GetGlContext(), theCubic);
390 }
391}
392
393// =======================================================================
394// function : GraduatedTrihedronErase
395// purpose :
396// =======================================================================
397void OpenGl_GraphicDriver::GraduatedTrihedronErase (const Graphic3d_CView& theCView)
398{
399 const OpenGl_CView* aCView = (const OpenGl_CView* )theCView.ptrView;
400 if (aCView != NULL)
401 {
402 aCView->View->GraduatedTrihedronErase (aCView->WS->GetGlContext());
403 }
404}
405
406// =======================================================================
407// function : GraduatedTrihedronMinMaxValues
408// purpose :
409// =======================================================================
410void OpenGl_GraphicDriver::GraduatedTrihedronMinMaxValues (const Standard_ShortReal theMinX,
411 const Standard_ShortReal theMinY,
412 const Standard_ShortReal theMinZ,
413 const Standard_ShortReal theMaxX,
414 const Standard_ShortReal theMaxY,
415 const Standard_ShortReal theMaxZ)
416{
417 OpenGl_GraduatedTrihedron::SetMinMax (theMinX, theMinY, theMinZ, theMaxX, theMaxY, theMaxZ);
418}