0024704: Visualization - inherit OpenGl_Structure from Graphic3d_CStructure
[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
65993a95 62// =======================================================================
63// function : OpenGl_GraphicDriver
64// purpose :
65// =======================================================================
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
f0430952 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// =======================================================================
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
5e27df78 121// =======================================================================
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
f0430952 136// =======================================================================
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
e276548b 174// =======================================================================
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
1981cb22 202// =======================================================================
203// function : BeginAddMode
204// purpose :
205// =======================================================================
206Standard_Boolean OpenGl_GraphicDriver::BeginAddMode (const Graphic3d_CView& theCView)
207{
208 if (theCView.ViewId == -1)
209 {
210 return Standard_False;
211 }
212
213 const OpenGl_CView* aCView = (const OpenGl_CView* )theCView.ptrView;
214 if (aCView != NULL && aCView->WS->BeginAddMode())
215 {
216 myImmediateWS = aCView->WS;
217 return Standard_True;
218 }
219
220 return Standard_False;
221}
222
223// =======================================================================
224// function : EndAddMode
225// purpose :
226// =======================================================================
227void OpenGl_GraphicDriver::EndAddMode()
228{
229 if (!myImmediateWS.IsNull())
230 {
231 myImmediateWS->EndAddMode();
232 myImmediateWS.Nullify();
233 }
234}
235
236// =======================================================================
237// function : BeginImmediatMode
238// purpose :
239// =======================================================================
240Standard_Boolean OpenGl_GraphicDriver::BeginImmediatMode (const Graphic3d_CView& theCView,
35e08fe8 241 const Aspect_CLayer2d& /*theCUnderLayer*/,
242 const Aspect_CLayer2d& /*theCOverLayer*/,
1981cb22 243 const Standard_Boolean theDoubleBuffer,
244 const Standard_Boolean theRetainMode)
245{
246 if (theCView.ViewId == -1)
247 {
248 return Standard_False;
249 }
250
251 const OpenGl_CView* aCView = (const OpenGl_CView* )theCView.ptrView;
252 if (aCView != NULL && aCView->WS->BeginImmediatMode (theCView, theDoubleBuffer, theRetainMode))
253 {
254 myImmediateWS = aCView->WS;
255 return Standard_True;
256 }
257
258 return Standard_False;
259}
260
261// =======================================================================
262// function : ClearImmediatMode
263// purpose :
264// =======================================================================
265void OpenGl_GraphicDriver::ClearImmediatMode (const Graphic3d_CView& theCView,
266 const Standard_Boolean theToFlush)
267{
268 const OpenGl_CView* aCView = (const OpenGl_CView* )theCView.ptrView;
269 if (aCView != NULL)
270 {
271 aCView->WS->ClearImmediatMode (theCView, theToFlush);
272 }
273}
274
275// =======================================================================
276// function : DrawStructure
277// purpose :
278// =======================================================================
279void OpenGl_GraphicDriver::DrawStructure (const Graphic3d_CStructure& theCStructure)
280{
63bcc448 281 OpenGl_Structure* aStructure = (OpenGl_Structure* )&theCStructure;
1981cb22 282 if (!myImmediateWS.IsNull())
283 {
284 myImmediateWS->DrawStructure (aStructure);
285 }
286}
287
288// =======================================================================
289// function : EndImmediatMode
290// purpose :
291// =======================================================================
292void OpenGl_GraphicDriver::EndImmediatMode (const Standard_Integer )
293{
294 if (!myImmediateWS.IsNull())
295 {
296 myImmediateWS->EndImmediatMode();
297 myImmediateWS.Nullify();
298 }
299}
a174a3c5 300
301// =======================================================================
302// function : Print
303// purpose :
304// =======================================================================
305Standard_Boolean OpenGl_GraphicDriver::Print (const Graphic3d_CView& theCView,
306 const Aspect_CLayer2d& theCUnderLayer,
307 const Aspect_CLayer2d& theCOverLayer,
308 const Aspect_Handle thePrintDC,
309 const Standard_Boolean theToShowBackground,
310 const Standard_CString theFilename,
311 const Aspect_PrintAlgo thePrintAlgorithm,
312 const Standard_Real theScaleFactor) const
313{
314 const OpenGl_CView* aCView = (const OpenGl_CView* )theCView.ptrView;
315 if (aCView == NULL
316 || !myPrintContext.IsNull())
317 {
318 return Standard_False;
319 }
320
321 Standard_Boolean isPrinted = Standard_False;
322 myPrintContext = new OpenGl_PrinterContext();
323#ifdef _WIN32
324 isPrinted = aCView->WS->Print (myPrintContext,
325 theCView,
326 theCUnderLayer,
327 theCOverLayer,
328 thePrintDC,
329 theToShowBackground,
330 theFilename,
331 thePrintAlgorithm,
332 theScaleFactor);
333#else
334 Standard_NotImplemented::Raise ("OpenGl_GraphicDriver::Print is implemented only on Windows");
335#endif
336 myPrintContext.Nullify();
337 return isPrinted;
338}
339
a6eb515f 340void OpenGl_GraphicDriver::SetStencilTestOptions (const Graphic3d_CGroup& theCGroup,
341 const Standard_Boolean theIsEnabled)
342{
343 OpenGl_StencilTest* aStencilTest = new OpenGl_StencilTest();
344 aStencilTest->SetOptions (theIsEnabled);
5322131b 345 ((OpenGl_Group* )theCGroup.ptrGroup)->AddElement (aStencilTest);
a6eb515f 346}
347
a174a3c5 348// =======================================================================
349// function : Text
350// purpose :
351// =======================================================================
352void OpenGl_GraphicDriver::Text (const Graphic3d_CGroup& theCGroup,
353 const TCollection_ExtendedString& theText,
354 const Graphic3d_Vertex& thePoint,
355 const Standard_Real theHeight,
356 const Quantity_PlaneAngle /*theAngle*/,
357 const Graphic3d_TextPath /*theTp*/,
358 const Graphic3d_HorizontalTextAlignment theHta,
359 const Graphic3d_VerticalTextAlignment theVta,
360 const Standard_Boolean /*theToEvalMinMax*/)
361{
362 if (theCGroup.ptrGroup == NULL)
363 {
364 return;
365 }
366
367 OpenGl_TextParam aParams;
368 aParams.Height = int ((theHeight < 2.0) ? DefaultTextHeight() : theHeight);
369 aParams.HAlign = theHta;
370 aParams.VAlign = theVta;
371 const OpenGl_Vec3 aPoint (thePoint.X(), thePoint.Y(), thePoint.Z());
372 OpenGl_Text* aText = new OpenGl_Text (theText, aPoint, aParams);
5322131b 373 ((OpenGl_Group* )theCGroup.ptrGroup)->AddElement (aText);
a174a3c5 374}
375
376// =======================================================================
377// function : Text
378// purpose : Wrapper CString -> TCollection_ExtendedString
379// =======================================================================
380void OpenGl_GraphicDriver::Text (const Graphic3d_CGroup& theCGroup,
381 const Standard_CString theText,
382 const Graphic3d_Vertex& thePoint,
383 const Standard_Real theHeight,
384 const Quantity_PlaneAngle theAngle,
385 const Graphic3d_TextPath theTp,
386 const Graphic3d_HorizontalTextAlignment theHta,
387 const Graphic3d_VerticalTextAlignment theVta,
388 const Standard_Boolean theToEvalMinMax)
389{
390 OpenGl_GraphicDriver::Text (theCGroup, TCollection_ExtendedString (theText),
391 thePoint, theHeight, theAngle, theTp, theHta, theVta, theToEvalMinMax);
392}
393
394// =======================================================================
395// function : Text
396// purpose : Wrapper CString -> TCollection_ExtendedString
397// =======================================================================
398void OpenGl_GraphicDriver::Text (const Graphic3d_CGroup& theCGroup,
399 const Standard_CString theText,
400 const Graphic3d_Vertex& thePoint,
401 const Standard_Real theHeight,
402 const Standard_Boolean theToEvalMinMax)
403{
404 OpenGl_GraphicDriver::Text (theCGroup, TCollection_ExtendedString (theText), thePoint, theHeight, theToEvalMinMax);
405}
406
407// =======================================================================
408// function : Text
409// purpose : Wrapper with default values
410// =======================================================================
411void OpenGl_GraphicDriver::Text (const Graphic3d_CGroup& theCGroup,
412 const TCollection_ExtendedString& theText,
413 const Graphic3d_Vertex& thePoint,
414 const Standard_Real theHeight,
415 const Standard_Boolean theToEvalMinMax)
416{
417 OpenGl_GraphicDriver::Text (theCGroup,
418 theText, thePoint, theHeight, 0.0,
419 Graphic3d_TP_RIGHT, Graphic3d_HTA_LEFT, Graphic3d_VTA_BOTTOM,
420 theToEvalMinMax);
421}
422
423// =======================================================================
424// function : ZBufferTriedronSetup
425// purpose :
426// =======================================================================
427void OpenGl_GraphicDriver::ZBufferTriedronSetup (const Quantity_NameOfColor theXColor,
428 const Quantity_NameOfColor theYColor,
429 const Quantity_NameOfColor theZColor,
430 const Standard_Real theSizeRatio,
431 const Standard_Real theAxisDiametr,
432 const Standard_Integer theNbFacettes)
433{
434 OpenGl_Trihedron::Setup (theXColor, theYColor, theZColor, theSizeRatio, theAxisDiametr, theNbFacettes);
435}
436
437// =======================================================================
438// function : TriedronDisplay
439// purpose :
440// =======================================================================
441void OpenGl_GraphicDriver::TriedronDisplay (const Graphic3d_CView& theCView,
442 const Aspect_TypeOfTriedronPosition thePosition,
443 const Quantity_NameOfColor theColor,
444 const Standard_Real theScale,
445 const Standard_Boolean theAsWireframe)
446{
447 const OpenGl_CView* aCView = (const OpenGl_CView* )theCView.ptrView;
448 if (aCView != NULL)
449 {
450 aCView->View->TriedronDisplay (aCView->WS->GetGlContext(), thePosition, theColor, theScale, theAsWireframe);
451 }
452}
453
454// =======================================================================
455// function : TriedronErase
456// purpose :
457// =======================================================================
458void OpenGl_GraphicDriver::TriedronErase (const Graphic3d_CView& theCView)
459{
460 const OpenGl_CView* aCView = (const OpenGl_CView* )theCView.ptrView;
461 if (aCView != NULL)
462 {
463 aCView->View->TriedronErase (aCView->WS->GetGlContext());
464 }
465}
466
467// =======================================================================
468// function : TriedronEcho
469// purpose :
470// =======================================================================
471void OpenGl_GraphicDriver::TriedronEcho (const Graphic3d_CView& ,
472 const Aspect_TypeOfTriedronEcho )
473{
474 // do nothing
475}
476
477// =======================================================================
478// function : Environment
479// purpose :
480// =======================================================================
481void OpenGl_GraphicDriver::Environment (const Graphic3d_CView& theCView)
482{
483 const OpenGl_CView* aCView = (const OpenGl_CView* )theCView.ptrView;
484 if (aCView == NULL)
485 {
486 return;
487 }
488
489 aCView->View->SetTextureEnv (aCView->WS->GetGlContext(), theCView.Context.TextureEnv);
490 aCView->View->SetSurfaceDetail ((Visual3d_TypeOfSurfaceDetail)theCView.Context.SurfaceDetail);
491}
492
493// =======================================================================
494// function : BackgroundImage
495// purpose :
496// =======================================================================
497void OpenGl_GraphicDriver::BackgroundImage (const Standard_CString theFileName,
498 const Graphic3d_CView& theCView,
499 const Aspect_FillMethod theFillStyle)
500{
501 const OpenGl_CView* aCView = (const OpenGl_CView* )theCView.ptrView;
502 if (aCView != NULL)
503 {
504 aCView->View->CreateBackgroundTexture (theFileName, theFillStyle);
505 }
506}
507
508// =======================================================================
509// function : SetBgImageStyle
510// purpose :
511// =======================================================================
512void OpenGl_GraphicDriver::SetBgImageStyle (const Graphic3d_CView& theCView,
513 const Aspect_FillMethod theFillStyle)
514{
515 const OpenGl_CView* aCView = (const OpenGl_CView* )theCView.ptrView;
516 if (aCView != NULL)
517 {
518 aCView->View->SetBackgroundTextureStyle (theFillStyle);
519 }
520}
521
522// =======================================================================
523// function : SetBgGradientStyle
524// purpose :
525// =======================================================================
526void OpenGl_GraphicDriver::SetBgGradientStyle (const Graphic3d_CView& theCView,
527 const Aspect_GradientFillMethod theFillType)
528{
529 const OpenGl_CView* aCView = (const OpenGl_CView* )theCView.ptrView;
530 if (aCView != NULL)
531 {
532 aCView->View->SetBackgroundGradientType (theFillType);
533 }
534}
535
536// =======================================================================
537// function : GraduatedTrihedronDisplay
538// purpose :
539// =======================================================================
540void OpenGl_GraphicDriver::GraduatedTrihedronDisplay (const Graphic3d_CView& theCView,
541 const Graphic3d_CGraduatedTrihedron& theCubic)
542{
543 const OpenGl_CView* aCView = (const OpenGl_CView* )theCView.ptrView;
544 if (aCView != NULL)
545 {
546 aCView->View->GraduatedTrihedronDisplay (aCView->WS->GetGlContext(), theCubic);
547 }
548}
549
550// =======================================================================
551// function : GraduatedTrihedronErase
552// purpose :
553// =======================================================================
554void OpenGl_GraphicDriver::GraduatedTrihedronErase (const Graphic3d_CView& theCView)
555{
556 const OpenGl_CView* aCView = (const OpenGl_CView* )theCView.ptrView;
557 if (aCView != NULL)
558 {
559 aCView->View->GraduatedTrihedronErase (aCView->WS->GetGlContext());
560 }
561}
562
563// =======================================================================
564// function : GraduatedTrihedronMinMaxValues
565// purpose :
566// =======================================================================
567void OpenGl_GraphicDriver::GraduatedTrihedronMinMaxValues (const Standard_ShortReal theMinX,
568 const Standard_ShortReal theMinY,
569 const Standard_ShortReal theMinZ,
570 const Standard_ShortReal theMaxX,
571 const Standard_ShortReal theMaxY,
572 const Standard_ShortReal theMaxZ)
573{
574 OpenGl_GraduatedTrihedron::SetMinMax (theMinX, theMinY, theMinZ, theMaxX, theMaxY, theMaxZ);
575}
938d4544 576
577// =======================================================================
578// function : SetFlippingOptions
579// purpose : Enable or disable flipping option for the given group
580// =======================================================================
581void OpenGl_GraphicDriver::SetFlippingOptions (const Graphic3d_CGroup& theCGroup,
582 const Standard_Boolean theIsEnabled,
583 const gp_Ax2& theRefPlane)
584{
585 OpenGl_Flipper* aFlipper = new OpenGl_Flipper (theRefPlane);
586 aFlipper->SetOptions (theIsEnabled);
5322131b 587 ((OpenGl_Group* )theCGroup.ptrGroup)->AddElement (aFlipper);
938d4544 588}