0024752: Visualization - inherit OpenGl_Group from Graphic3d_Group
[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
a174a3c5 32#include <Standard_NotImplemented.hxx>
33
2166f0fa
SK
34IMPLEMENT_STANDARD_HANDLE(OpenGl_GraphicDriver,Graphic3d_GraphicDriver)
35IMPLEMENT_STANDARD_RTTIEXT(OpenGl_GraphicDriver,Graphic3d_GraphicDriver)
7fd59977 36
2166f0fa
SK
37namespace
38{
5e27df78 39 static const Handle(OpenGl_Context) TheNullGlCtx;
2166f0fa 40};
7fd59977 41
42// Pour eviter de "mangler" MetaGraphicDriverFactory, le nom de la
43// fonction qui cree un Graphic3d_GraphicDriver.
44// En effet, ce nom est recherche par la methode DlSymb de la
45// classe OSD_SharedLibrary dans la methode SetGraphicDriver de la
46// classe Graphic3d_GraphicDevice
47extern "C" {
5e27df78 48#if defined(_MSC_VER) // disable MS VC++ warning on C-style function returning C++ object
2166f0fa
SK
49 #pragma warning(push)
50 #pragma warning(disable:4190)
7fd59977 51#endif
5e27df78 52 Standard_EXPORT Handle(Graphic3d_GraphicDriver) MetaGraphicDriverFactory (const Standard_CString theShrName)
2166f0fa 53 {
5e27df78 54 Handle(OpenGl_GraphicDriver) aDriver = new OpenGl_GraphicDriver (theShrName);
55 return aDriver;
2166f0fa 56 }
5e27df78 57#if defined(_MSC_VER)
2166f0fa 58 #pragma warning(pop)
7fd59977 59#endif
60}
61
f0430952 62// =======================================================================
63// function : OpenGl_GraphicDriver
64// purpose :
65// =======================================================================
65993a95 66OpenGl_GraphicDriver::OpenGl_GraphicDriver (const Handle(Aspect_DisplayConnection)& theDisplayConnection)
67: Graphic3d_GraphicDriver ("TKOpenGl"),
68 myCaps (new OpenGl_Caps()),
69 myMapOfView (1, NCollection_BaseAllocator::CommonBaseAllocator()),
70 myMapOfWS (1, NCollection_BaseAllocator::CommonBaseAllocator()),
71 myMapOfStructure (1, NCollection_BaseAllocator::CommonBaseAllocator()),
72 myUserDrawCallback (NULL),
73 myTempText (new OpenGl_Text())
74{
75 Begin (theDisplayConnection);
76}
77
78// =======================================================================
79// function : OpenGl_GraphicDriver
80// purpose :
81// =======================================================================
2166f0fa 82OpenGl_GraphicDriver::OpenGl_GraphicDriver (const Standard_CString theShrName)
5e27df78 83: Graphic3d_GraphicDriver (theShrName),
58655684 84 myCaps (new OpenGl_Caps()),
5e27df78 85 myMapOfView (1, NCollection_BaseAllocator::CommonBaseAllocator()),
86 myMapOfWS (1, NCollection_BaseAllocator::CommonBaseAllocator()),
87 myMapOfStructure (1, NCollection_BaseAllocator::CommonBaseAllocator()),
a174a3c5 88 myUserDrawCallback (NULL),
89 myTempText (new OpenGl_Text())
7fd59977 90{
2166f0fa 91 //
7fd59977 92}
93
f0430952 94// =======================================================================
5e27df78 95// function : UserDrawCallback
f0430952 96// purpose :
97// =======================================================================
5e27df78 98OpenGl_GraphicDriver::OpenGl_UserDrawCallback_t& OpenGl_GraphicDriver::UserDrawCallback()
2166f0fa 99{
5e27df78 100 return myUserDrawCallback;
2166f0fa
SK
101}
102
f0430952 103// =======================================================================
5e27df78 104// function : DefaultTextHeight
f0430952 105// purpose :
106// =======================================================================
5e27df78 107Standard_ShortReal OpenGl_GraphicDriver::DefaultTextHeight() const
2166f0fa 108{
5e27df78 109 return 16.;
2166f0fa
SK
110}
111
f0430952 112// =======================================================================
f0430952 113// function : EnableVBO
114// purpose :
115// =======================================================================
2166f0fa 116void OpenGl_GraphicDriver::EnableVBO (const Standard_Boolean theToTurnOn)
7fd59977 117{
58655684 118 myCaps->vboDisable = !theToTurnOn;
7fd59977 119}
f0430952 120
121// =======================================================================
5e27df78 122// function : GetSharedContext
123// purpose :
124// =======================================================================
125const Handle(OpenGl_Context)& OpenGl_GraphicDriver::GetSharedContext() const
126{
127 if (myMapOfWS.IsEmpty())
128 {
129 return TheNullGlCtx;
130 }
131
132 NCollection_DataMap<Standard_Integer, Handle(OpenGl_Workspace)>::Iterator anIter (myMapOfWS);
133 return anIter.Value()->GetGlContext();
134}
135
136// =======================================================================
f0430952 137// function : MemoryInfo
138// purpose :
139// =======================================================================
140Standard_Boolean OpenGl_GraphicDriver::MemoryInfo (Standard_Size& theFreeBytes,
141 TCollection_AsciiString& theInfo) const
142{
143 // this is extra work (for OpenGl_Context initialization)...
144 OpenGl_Context aGlCtx;
145 if (!aGlCtx.Init())
146 {
147 return Standard_False;
148 }
149 theFreeBytes = aGlCtx.AvailableMemory();
150 theInfo = aGlCtx.MemoryInfo();
151 return !theInfo.IsEmpty();
152}
1981cb22 153
154// =======================================================================
155// function : SetImmediateModeDrawToFront
156// purpose :
157// =======================================================================
158Standard_Boolean OpenGl_GraphicDriver::SetImmediateModeDrawToFront (const Graphic3d_CView& theCView,
159 const Standard_Boolean theDrawToFrontBuffer)
160{
161 if (theCView.ViewId == -1)
162 {
163 return Standard_False;
164 }
165
166 const OpenGl_CView* aCView = (const OpenGl_CView* )theCView.ptrView;
167 if (aCView != NULL)
168 {
169 return aCView->WS->SetImmediateModeDrawToFront (theDrawToFrontBuffer);
170 }
171 return Standard_False;
172}
173
174// =======================================================================
e276548b 175// function : GetOpenClDeviceInfo
176// purpose : Returns information about device used for computations
177// =======================================================================
178#ifndef HAVE_OPENCL
179
180Standard_Boolean OpenGl_GraphicDriver::GetOpenClDeviceInfo (const Graphic3d_CView&,
181 NCollection_DataMap<TCollection_AsciiString, TCollection_AsciiString>&)
182{
7ae4a307 183 return Standard_False;
e276548b 184}
185
186#else
187
188Standard_Boolean OpenGl_GraphicDriver::GetOpenClDeviceInfo (const Graphic3d_CView& theCView,
189 NCollection_DataMap<TCollection_AsciiString, TCollection_AsciiString>& theInfo)
190{
191
192 if (theCView.ViewId == -1 || theCView.ptrView == NULL)
193 {
194 return Standard_False;
195 }
5322131b 196
e276548b 197 return reinterpret_cast<const OpenGl_CView*> (theCView.ptrView)->WS->GetOpenClDeviceInfo (theInfo);
198}
199
200#endif
201
202// =======================================================================
679ecdee 203// function : DisplayImmediateStructure
1981cb22 204// purpose :
205// =======================================================================
679ecdee 206void OpenGl_GraphicDriver::DisplayImmediateStructure (const Graphic3d_CView& theCView,
207 const Graphic3d_CStructure& theCStructure)
1981cb22 208{
679ecdee 209 OpenGl_CView* aCView = (OpenGl_CView* )theCView.ptrView;
210 OpenGl_Structure* aStructure = (OpenGl_Structure* )&theCStructure;
211 if (aCView == NULL)
1981cb22 212 {
679ecdee 213 return;
1981cb22 214 }
215
679ecdee 216 aCView->View->DisplayImmediateStructure (aStructure);
1981cb22 217}
218
219// =======================================================================
679ecdee 220// function : EraseImmediateStructure
1981cb22 221// purpose :
222// =======================================================================
679ecdee 223void OpenGl_GraphicDriver::EraseImmediateStructure (const Graphic3d_CView& theCView,
224 const Graphic3d_CStructure& theCStructure)
1981cb22 225{
679ecdee 226 OpenGl_CView* aCView = (OpenGl_CView* )theCView.ptrView;
63bcc448 227 OpenGl_Structure* aStructure = (OpenGl_Structure* )&theCStructure;
679ecdee 228 if (aCView == NULL)
1981cb22 229 {
679ecdee 230 return;
1981cb22 231 }
1981cb22 232
679ecdee 233 aCView->View->EraseImmediateStructure (aStructure);
1981cb22 234}
a174a3c5 235
679ecdee 236
a174a3c5 237// =======================================================================
238// function : Print
239// purpose :
240// =======================================================================
241Standard_Boolean OpenGl_GraphicDriver::Print (const Graphic3d_CView& theCView,
242 const Aspect_CLayer2d& theCUnderLayer,
243 const Aspect_CLayer2d& theCOverLayer,
244 const Aspect_Handle thePrintDC,
245 const Standard_Boolean theToShowBackground,
246 const Standard_CString theFilename,
247 const Aspect_PrintAlgo thePrintAlgorithm,
248 const Standard_Real theScaleFactor) const
249{
250 const OpenGl_CView* aCView = (const OpenGl_CView* )theCView.ptrView;
251 if (aCView == NULL
252 || !myPrintContext.IsNull())
253 {
254 return Standard_False;
255 }
256
257 Standard_Boolean isPrinted = Standard_False;
258 myPrintContext = new OpenGl_PrinterContext();
259#ifdef _WIN32
260 isPrinted = aCView->WS->Print (myPrintContext,
261 theCView,
262 theCUnderLayer,
263 theCOverLayer,
264 thePrintDC,
265 theToShowBackground,
266 theFilename,
267 thePrintAlgorithm,
268 theScaleFactor);
269#else
270 Standard_NotImplemented::Raise ("OpenGl_GraphicDriver::Print is implemented only on Windows");
271#endif
272 myPrintContext.Nullify();
273 return isPrinted;
274}
275
a174a3c5 276// =======================================================================
277// function : ZBufferTriedronSetup
278// purpose :
279// =======================================================================
280void OpenGl_GraphicDriver::ZBufferTriedronSetup (const Quantity_NameOfColor theXColor,
281 const Quantity_NameOfColor theYColor,
282 const Quantity_NameOfColor theZColor,
283 const Standard_Real theSizeRatio,
284 const Standard_Real theAxisDiametr,
285 const Standard_Integer theNbFacettes)
286{
287 OpenGl_Trihedron::Setup (theXColor, theYColor, theZColor, theSizeRatio, theAxisDiametr, theNbFacettes);
288}
289
290// =======================================================================
291// function : TriedronDisplay
292// purpose :
293// =======================================================================
294void OpenGl_GraphicDriver::TriedronDisplay (const Graphic3d_CView& theCView,
295 const Aspect_TypeOfTriedronPosition thePosition,
296 const Quantity_NameOfColor theColor,
297 const Standard_Real theScale,
298 const Standard_Boolean theAsWireframe)
299{
300 const OpenGl_CView* aCView = (const OpenGl_CView* )theCView.ptrView;
301 if (aCView != NULL)
302 {
303 aCView->View->TriedronDisplay (aCView->WS->GetGlContext(), thePosition, theColor, theScale, theAsWireframe);
304 }
305}
306
307// =======================================================================
308// function : TriedronErase
309// purpose :
310// =======================================================================
311void OpenGl_GraphicDriver::TriedronErase (const Graphic3d_CView& theCView)
312{
313 const OpenGl_CView* aCView = (const OpenGl_CView* )theCView.ptrView;
314 if (aCView != NULL)
315 {
316 aCView->View->TriedronErase (aCView->WS->GetGlContext());
317 }
318}
319
320// =======================================================================
321// function : TriedronEcho
322// purpose :
323// =======================================================================
324void OpenGl_GraphicDriver::TriedronEcho (const Graphic3d_CView& ,
325 const Aspect_TypeOfTriedronEcho )
326{
327 // do nothing
328}
329
330// =======================================================================
331// function : Environment
332// purpose :
333// =======================================================================
334void OpenGl_GraphicDriver::Environment (const Graphic3d_CView& theCView)
335{
336 const OpenGl_CView* aCView = (const OpenGl_CView* )theCView.ptrView;
337 if (aCView == NULL)
338 {
339 return;
340 }
341
342 aCView->View->SetTextureEnv (aCView->WS->GetGlContext(), theCView.Context.TextureEnv);
343 aCView->View->SetSurfaceDetail ((Visual3d_TypeOfSurfaceDetail)theCView.Context.SurfaceDetail);
344}
345
346// =======================================================================
347// function : BackgroundImage
348// purpose :
349// =======================================================================
350void OpenGl_GraphicDriver::BackgroundImage (const Standard_CString theFileName,
351 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->CreateBackgroundTexture (theFileName, theFillStyle);
358 }
359}
360
361// =======================================================================
362// function : SetBgImageStyle
363// purpose :
364// =======================================================================
365void OpenGl_GraphicDriver::SetBgImageStyle (const Graphic3d_CView& theCView,
366 const Aspect_FillMethod theFillStyle)
367{
368 const OpenGl_CView* aCView = (const OpenGl_CView* )theCView.ptrView;
369 if (aCView != NULL)
370 {
371 aCView->View->SetBackgroundTextureStyle (theFillStyle);
372 }
373}
374
375// =======================================================================
376// function : SetBgGradientStyle
377// purpose :
378// =======================================================================
379void OpenGl_GraphicDriver::SetBgGradientStyle (const Graphic3d_CView& theCView,
380 const Aspect_GradientFillMethod theFillType)
381{
382 const OpenGl_CView* aCView = (const OpenGl_CView* )theCView.ptrView;
383 if (aCView != NULL)
384 {
385 aCView->View->SetBackgroundGradientType (theFillType);
386 }
387}
388
389// =======================================================================
390// function : GraduatedTrihedronDisplay
391// purpose :
392// =======================================================================
393void OpenGl_GraphicDriver::GraduatedTrihedronDisplay (const Graphic3d_CView& theCView,
394 const Graphic3d_CGraduatedTrihedron& theCubic)
395{
396 const OpenGl_CView* aCView = (const OpenGl_CView* )theCView.ptrView;
397 if (aCView != NULL)
398 {
399 aCView->View->GraduatedTrihedronDisplay (aCView->WS->GetGlContext(), theCubic);
400 }
401}
402
403// =======================================================================
404// function : GraduatedTrihedronErase
405// purpose :
406// =======================================================================
407void OpenGl_GraphicDriver::GraduatedTrihedronErase (const Graphic3d_CView& theCView)
408{
409 const OpenGl_CView* aCView = (const OpenGl_CView* )theCView.ptrView;
410 if (aCView != NULL)
411 {
412 aCView->View->GraduatedTrihedronErase (aCView->WS->GetGlContext());
413 }
414}
415
416// =======================================================================
417// function : GraduatedTrihedronMinMaxValues
418// purpose :
419// =======================================================================
420void OpenGl_GraphicDriver::GraduatedTrihedronMinMaxValues (const Standard_ShortReal theMinX,
421 const Standard_ShortReal theMinY,
422 const Standard_ShortReal theMinZ,
423 const Standard_ShortReal theMaxX,
424 const Standard_ShortReal theMaxY,
425 const Standard_ShortReal theMaxZ)
426{
427 OpenGl_GraduatedTrihedron::SetMinMax (theMinX, theMinY, theMinZ, theMaxX, theMaxY, theMaxZ);
428}