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