0024739: TKOpenGl - port ray-tracing from OpenCL to GLSL for better integration and...
[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// =======================================================================
679ecdee 175// function : DisplayImmediateStructure
1981cb22 176// purpose :
177// =======================================================================
679ecdee 178void OpenGl_GraphicDriver::DisplayImmediateStructure (const Graphic3d_CView& theCView,
179 const Graphic3d_CStructure& theCStructure)
1981cb22 180{
679ecdee 181 OpenGl_CView* aCView = (OpenGl_CView* )theCView.ptrView;
182 OpenGl_Structure* aStructure = (OpenGl_Structure* )&theCStructure;
183 if (aCView == NULL)
1981cb22 184 {
679ecdee 185 return;
1981cb22 186 }
187
679ecdee 188 aCView->View->DisplayImmediateStructure (aStructure);
1981cb22 189}
190
191// =======================================================================
679ecdee 192// function : EraseImmediateStructure
1981cb22 193// purpose :
194// =======================================================================
679ecdee 195void OpenGl_GraphicDriver::EraseImmediateStructure (const Graphic3d_CView& theCView,
196 const Graphic3d_CStructure& theCStructure)
1981cb22 197{
679ecdee 198 OpenGl_CView* aCView = (OpenGl_CView* )theCView.ptrView;
63bcc448 199 OpenGl_Structure* aStructure = (OpenGl_Structure* )&theCStructure;
679ecdee 200 if (aCView == NULL)
1981cb22 201 {
679ecdee 202 return;
1981cb22 203 }
1981cb22 204
679ecdee 205 aCView->View->EraseImmediateStructure (aStructure);
1981cb22 206}
a174a3c5 207
679ecdee 208
a174a3c5 209// =======================================================================
210// function : Print
211// purpose :
212// =======================================================================
213Standard_Boolean OpenGl_GraphicDriver::Print (const Graphic3d_CView& theCView,
214 const Aspect_CLayer2d& theCUnderLayer,
215 const Aspect_CLayer2d& theCOverLayer,
216 const Aspect_Handle thePrintDC,
217 const Standard_Boolean theToShowBackground,
218 const Standard_CString theFilename,
219 const Aspect_PrintAlgo thePrintAlgorithm,
220 const Standard_Real theScaleFactor) const
221{
222 const OpenGl_CView* aCView = (const OpenGl_CView* )theCView.ptrView;
223 if (aCView == NULL
224 || !myPrintContext.IsNull())
225 {
226 return Standard_False;
227 }
228
229 Standard_Boolean isPrinted = Standard_False;
230 myPrintContext = new OpenGl_PrinterContext();
231#ifdef _WIN32
232 isPrinted = aCView->WS->Print (myPrintContext,
233 theCView,
234 theCUnderLayer,
235 theCOverLayer,
236 thePrintDC,
237 theToShowBackground,
238 theFilename,
239 thePrintAlgorithm,
240 theScaleFactor);
241#else
242 Standard_NotImplemented::Raise ("OpenGl_GraphicDriver::Print is implemented only on Windows");
243#endif
244 myPrintContext.Nullify();
245 return isPrinted;
246}
247
a174a3c5 248// =======================================================================
249// function : ZBufferTriedronSetup
250// purpose :
251// =======================================================================
252void OpenGl_GraphicDriver::ZBufferTriedronSetup (const Quantity_NameOfColor theXColor,
253 const Quantity_NameOfColor theYColor,
254 const Quantity_NameOfColor theZColor,
255 const Standard_Real theSizeRatio,
256 const Standard_Real theAxisDiametr,
257 const Standard_Integer theNbFacettes)
258{
259 OpenGl_Trihedron::Setup (theXColor, theYColor, theZColor, theSizeRatio, theAxisDiametr, theNbFacettes);
260}
261
262// =======================================================================
263// function : TriedronDisplay
264// purpose :
265// =======================================================================
266void OpenGl_GraphicDriver::TriedronDisplay (const Graphic3d_CView& theCView,
267 const Aspect_TypeOfTriedronPosition thePosition,
268 const Quantity_NameOfColor theColor,
269 const Standard_Real theScale,
270 const Standard_Boolean theAsWireframe)
271{
272 const OpenGl_CView* aCView = (const OpenGl_CView* )theCView.ptrView;
273 if (aCView != NULL)
274 {
275 aCView->View->TriedronDisplay (aCView->WS->GetGlContext(), thePosition, theColor, theScale, theAsWireframe);
276 }
277}
278
279// =======================================================================
280// function : TriedronErase
281// purpose :
282// =======================================================================
283void OpenGl_GraphicDriver::TriedronErase (const Graphic3d_CView& theCView)
284{
285 const OpenGl_CView* aCView = (const OpenGl_CView* )theCView.ptrView;
286 if (aCView != NULL)
287 {
288 aCView->View->TriedronErase (aCView->WS->GetGlContext());
289 }
290}
291
292// =======================================================================
293// function : TriedronEcho
294// purpose :
295// =======================================================================
296void OpenGl_GraphicDriver::TriedronEcho (const Graphic3d_CView& ,
297 const Aspect_TypeOfTriedronEcho )
298{
299 // do nothing
300}
301
302// =======================================================================
303// function : Environment
304// purpose :
305// =======================================================================
306void OpenGl_GraphicDriver::Environment (const Graphic3d_CView& theCView)
307{
308 const OpenGl_CView* aCView = (const OpenGl_CView* )theCView.ptrView;
309 if (aCView == NULL)
310 {
311 return;
312 }
313
314 aCView->View->SetTextureEnv (aCView->WS->GetGlContext(), theCView.Context.TextureEnv);
315 aCView->View->SetSurfaceDetail ((Visual3d_TypeOfSurfaceDetail)theCView.Context.SurfaceDetail);
316}
317
318// =======================================================================
319// function : BackgroundImage
320// purpose :
321// =======================================================================
322void OpenGl_GraphicDriver::BackgroundImage (const Standard_CString theFileName,
323 const Graphic3d_CView& theCView,
324 const Aspect_FillMethod theFillStyle)
325{
326 const OpenGl_CView* aCView = (const OpenGl_CView* )theCView.ptrView;
327 if (aCView != NULL)
328 {
329 aCView->View->CreateBackgroundTexture (theFileName, theFillStyle);
330 }
331}
332
333// =======================================================================
334// function : SetBgImageStyle
335// purpose :
336// =======================================================================
337void OpenGl_GraphicDriver::SetBgImageStyle (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->SetBackgroundTextureStyle (theFillStyle);
344 }
345}
346
347// =======================================================================
348// function : SetBgGradientStyle
349// purpose :
350// =======================================================================
351void OpenGl_GraphicDriver::SetBgGradientStyle (const Graphic3d_CView& theCView,
352 const Aspect_GradientFillMethod theFillType)
353{
354 const OpenGl_CView* aCView = (const OpenGl_CView* )theCView.ptrView;
355 if (aCView != NULL)
356 {
357 aCView->View->SetBackgroundGradientType (theFillType);
358 }
359}
360
361// =======================================================================
362// function : GraduatedTrihedronDisplay
363// purpose :
364// =======================================================================
365void OpenGl_GraphicDriver::GraduatedTrihedronDisplay (const Graphic3d_CView& theCView,
366 const Graphic3d_CGraduatedTrihedron& theCubic)
367{
368 const OpenGl_CView* aCView = (const OpenGl_CView* )theCView.ptrView;
369 if (aCView != NULL)
370 {
371 aCView->View->GraduatedTrihedronDisplay (aCView->WS->GetGlContext(), theCubic);
372 }
373}
374
375// =======================================================================
376// function : GraduatedTrihedronErase
377// purpose :
378// =======================================================================
379void OpenGl_GraphicDriver::GraduatedTrihedronErase (const Graphic3d_CView& theCView)
380{
381 const OpenGl_CView* aCView = (const OpenGl_CView* )theCView.ptrView;
382 if (aCView != NULL)
383 {
384 aCView->View->GraduatedTrihedronErase (aCView->WS->GetGlContext());
385 }
386}
387
388// =======================================================================
389// function : GraduatedTrihedronMinMaxValues
390// purpose :
391// =======================================================================
392void OpenGl_GraphicDriver::GraduatedTrihedronMinMaxValues (const Standard_ShortReal theMinX,
393 const Standard_ShortReal theMinY,
394 const Standard_ShortReal theMinZ,
395 const Standard_ShortReal theMaxX,
396 const Standard_ShortReal theMaxY,
397 const Standard_ShortReal theMaxZ)
398{
399 OpenGl_GraduatedTrihedron::SetMinMax (theMinX, theMinY, theMinZ, theMaxX, theMaxY, theMaxZ);
400}