0030668: Visualization - revise adding ZLayer API
[occt.git] / src / OpenGl / OpenGl_View.cxx
CommitLineData
b311480e 1// Created on: 2011-09-20
2// Created by: Sergey ZERCHANINOV
973c2be1 3// Copyright (c) 2011-2014 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
c357e426 16#include <OpenGl_View.hxx>
5f8b738e 17
c357e426 18#include <Aspect_RenderingContext.hxx>
19#include <Aspect_Window.hxx>
20#include <Graphic3d_AspectFillArea3d.hxx>
21#include <Graphic3d_Texture2Dmanual.hxx>
22#include <Graphic3d_TextureEnv.hxx>
23#include <Graphic3d_Mat4d.hxx>
24#include <NCollection_Mat4.hxx>
bf75be98 25#include <OpenGl_Context.hxx>
c357e426 26#include <OpenGl_FrameBuffer.hxx>
30f0ad28 27#include <OpenGl_GlCore11.hxx>
28#include <OpenGl_GraduatedTrihedron.hxx>
392ac980 29#include <OpenGl_GraphicDriver.hxx>
30f0ad28 30#include <OpenGl_ShaderManager.hxx>
bf75be98 31#include <OpenGl_Texture.hxx>
c357e426 32#include <OpenGl_Window.hxx>
30f0ad28 33#include <OpenGl_Workspace.hxx>
f5b72419 34#include <OSD_Parallel.hxx>
c357e426 35#include <Standard_CLocaleSentry.hxx>
2166f0fa 36
f5b72419 37#include "../Graphic3d/Graphic3d_Structure.pxx"
38
92efcf78 39IMPLEMENT_STANDARD_RTTIEXT(OpenGl_View,Graphic3d_CView)
40
c357e426 41// =======================================================================
42// function : Constructor
43// purpose :
44// =======================================================================
45OpenGl_View::OpenGl_View (const Handle(Graphic3d_StructureManager)& theMgr,
46 const Handle(OpenGl_GraphicDriver)& theDriver,
47 const Handle(OpenGl_Caps)& theCaps,
c357e426 48 OpenGl_StateCounter* theCounter)
49: Graphic3d_CView (theMgr),
50 myDriver (theDriver.operator->()),
51 myCaps (theCaps),
c357e426 52 myWasRedrawnGL (Standard_False),
c357e426 53 myBackfacing (Graphic3d_TOBM_AUTOMATIC),
c357e426 54 myToShowGradTrihedron (false),
f5b72419 55 myZLayers (Structure_MAX_PRIORITY - Structure_MIN_PRIORITY + 1),
c357e426 56 myStateCounter (theCounter),
992ed6b3 57 myCurrLightSourceState (theCounter->Increment()),
58 myLightsRevision (0),
0b0320e7 59 myLastLightSourceState (0, 0),
3c4b62a4 60 myFboColorFormat (GL_RGBA8),
61 myFboDepthFormat (GL_DEPTH24_STENCIL8),
62 myToFlipOutput (Standard_False),
c357e426 63 myFrameCounter (0),
64 myHasFboBlit (Standard_True),
a1073ae2 65 myToDisableOIT (Standard_False),
66 myToDisableOITMSAA (Standard_False),
7ccf8676 67 myToDisableMSAA (Standard_False),
c357e426 68 myTransientDrawToFront (Standard_True),
69 myBackBufferRestored (Standard_False),
70 myIsImmediateDrawn (Standard_False),
bf5f0ca2 71 myTextureParams (new OpenGl_Aspects()),
0b0320e7 72 myBgGradientArray (new OpenGl_BackgroundArray (Graphic3d_TOB_GRADIENT)),
91c60b57 73 myBgTextureArray (new OpenGl_BackgroundArray (Graphic3d_TOB_TEXTURE)),
74 // ray-tracing fields initialization
c357e426 75 myRaytraceInitStatus (OpenGl_RT_NONE),
76 myIsRaytraceDataValid (Standard_False),
91c60b57 77 myIsRaytraceWarnTextures (Standard_False),
f5b72419 78 myRaytraceBVHBuilder (new BVH_BinnedBuilder<Standard_ShortReal, 3, BVH_Constants_NbBinsBest> (BVH_Constants_LeafNodeSizeAverage,
79 BVH_Constants_MaxTreeDepth,
80 Standard_False,
81 OSD_Parallel::NbLogicalProcessors() + 1)),
82 myRaytraceSceneRadius (0.0f),
83 myRaytraceSceneEpsilon (1.0e-6f),
91c60b57 84 myToUpdateEnvironmentMap (Standard_False),
b27ab03d 85 myRaytraceLayerListState (0),
86 myPrevCameraApertureRadius(0.f),
87 myPrevCameraFocalPlaneDist(0.f)
2166f0fa 88{
c357e426 89 myWorkspace = new OpenGl_Workspace (this, NULL);
2166f0fa 90
992ed6b3 91 Handle(Graphic3d_CLight) aLight = new Graphic3d_CLight (Graphic3d_TOLS_AMBIENT);
92 aLight->SetHeadlight (false);
93 aLight->SetColor (Quantity_NOC_WHITE);
94 myNoShadingLight = new Graphic3d_LightSet();
95 myNoShadingLight->Add (aLight);
016e5959 96
a1073ae2 97 myMainSceneFbos[0] = new OpenGl_FrameBuffer();
98 myMainSceneFbos[1] = new OpenGl_FrameBuffer();
99 myMainSceneFbosOit[0] = new OpenGl_FrameBuffer();
100 myMainSceneFbosOit[1] = new OpenGl_FrameBuffer();
101 myImmediateSceneFbos[0] = new OpenGl_FrameBuffer();
102 myImmediateSceneFbos[1] = new OpenGl_FrameBuffer();
103 myImmediateSceneFbosOit[0] = new OpenGl_FrameBuffer();
104 myImmediateSceneFbosOit[1] = new OpenGl_FrameBuffer();
105 myOpenGlFBO = new OpenGl_FrameBuffer();
106 myOpenGlFBO2 = new OpenGl_FrameBuffer();
107 myRaytraceFBO1[0] = new OpenGl_FrameBuffer();
108 myRaytraceFBO1[1] = new OpenGl_FrameBuffer();
109 myRaytraceFBO2[0] = new OpenGl_FrameBuffer();
110 myRaytraceFBO2[1] = new OpenGl_FrameBuffer();
c357e426 111}
112
113// =======================================================================
114// function : Destructor
115// purpose :
116// =======================================================================
0b0320e7 117OpenGl_View::~OpenGl_View()
2166f0fa 118{
bf75be98 119 ReleaseGlResources (NULL); // ensure ReleaseGlResources() was called within valid context
0b0320e7 120 OpenGl_Element::Destroy (NULL, myBgGradientArray);
121 OpenGl_Element::Destroy (NULL, myBgTextureArray);
122 OpenGl_Element::Destroy (NULL, myTextureParams);
bf75be98 123}
124
c357e426 125// =======================================================================
126// function : ReleaseGlResources
127// purpose :
128// =======================================================================
bf75be98 129void OpenGl_View::ReleaseGlResources (const Handle(OpenGl_Context)& theCtx)
130{
ad67e367 131 myGraduatedTrihedron.Release (theCtx.get());
132 myFrameStatsPrs.Release (theCtx.get());
30f0ad28 133
bf75be98 134 if (!myTextureEnv.IsNull())
135 {
ad67e367 136 if (!theCtx.IsNull())
cc8cbabe 137 {
ad67e367 138 for (OpenGl_TextureSet::Iterator aTextureIter (myTextureEnv); aTextureIter.More(); aTextureIter.Next())
139 {
140 theCtx->DelayedRelease (aTextureIter.ChangeValue());
141 aTextureIter.ChangeValue().Nullify();
142 }
cc8cbabe 143 }
bf75be98 144 myTextureEnv.Nullify();
145 }
0b0320e7 146
147 if (myTextureParams != NULL)
148 {
ad67e367 149 myTextureParams->Release (theCtx.get());
0b0320e7 150 }
151 if (myBgGradientArray != NULL)
152 {
ad67e367 153 myBgGradientArray->Release (theCtx.get());
0b0320e7 154 }
155 if (myBgTextureArray != NULL)
bf75be98 156 {
ad67e367 157 myBgTextureArray->Release (theCtx.get());
bf75be98 158 }
91c60b57 159
ad67e367 160 myMainSceneFbos[0] ->Release (theCtx.get());
161 myMainSceneFbos[1] ->Release (theCtx.get());
162 myMainSceneFbosOit[0] ->Release (theCtx.get());
163 myMainSceneFbosOit[1] ->Release (theCtx.get());
164 myImmediateSceneFbos[0] ->Release (theCtx.get());
165 myImmediateSceneFbos[1] ->Release (theCtx.get());
166 myImmediateSceneFbosOit[0]->Release (theCtx.get());
167 myImmediateSceneFbosOit[1]->Release (theCtx.get());
168 myOpenGlFBO ->Release (theCtx.get());
169 myOpenGlFBO2 ->Release (theCtx.get());
170 myFullScreenQuad .Release (theCtx.get());
171 myFullScreenQuadFlip .Release (theCtx.get());
c357e426 172
91c60b57 173 releaseRaytraceResources (theCtx);
bf75be98 174}
2166f0fa 175
c357e426 176// =======================================================================
177// function : Remove
178// purpose :
179// =======================================================================
180void OpenGl_View::Remove()
bf75be98 181{
c357e426 182 if (IsRemoved())
bf75be98 183 {
c357e426 184 return;
bf75be98 185 }
186
c357e426 187 myDriver->RemoveView (this);
188 myWindow.Nullify();
189
190 Graphic3d_CView::Remove();
191}
192
7c3ef2f7 193// =======================================================================
194// function : SetLocalOrigin
195// purpose :
196// =======================================================================
197void OpenGl_View::SetLocalOrigin (const gp_XYZ& theOrigin)
198{
199 myLocalOrigin = theOrigin;
200 const Handle(OpenGl_Context)& aCtx = myWorkspace->GetGlContext();
201 if (!aCtx.IsNull())
202 {
203 aCtx->ShaderManager()->SetLocalOrigin (theOrigin);
204 }
205}
206
c357e426 207// =======================================================================
208// function : SetTextureEnv
209// purpose :
210// =======================================================================
211void OpenGl_View::SetTextureEnv (const Handle(Graphic3d_TextureEnv)& theTextureEnv)
212{
213 Handle(OpenGl_Context) aCtx = myWorkspace->GetGlContext();
214 if (!aCtx.IsNull() && !myTextureEnv.IsNull())
215 {
cc8cbabe 216 for (OpenGl_TextureSet::Iterator aTextureIter (myTextureEnv); aTextureIter.More(); aTextureIter.Next())
217 {
218 aCtx->DelayedRelease (aTextureIter.ChangeValue());
219 aTextureIter.ChangeValue().Nullify();
220 }
c357e426 221 }
222
223 myToUpdateEnvironmentMap = Standard_True;
224 myTextureEnvData = theTextureEnv;
225 myTextureEnv.Nullify();
226 initTextureEnv (aCtx);
227}
228
229// =======================================================================
230// function : initTextureEnv
231// purpose :
232// =======================================================================
233void OpenGl_View::initTextureEnv (const Handle(OpenGl_Context)& theContext)
234{
235 if (myTextureEnvData.IsNull()
236 || theContext.IsNull()
237 || !theContext->MakeCurrent())
bf75be98 238 {
239 return;
240 }
241
cc8cbabe 242 myTextureEnv = new OpenGl_TextureSet (1);
243 Handle(OpenGl_Texture)& aTextureEnv = myTextureEnv->ChangeFirst();
244 aTextureEnv = new OpenGl_Texture (myTextureEnvData->GetId(), myTextureEnvData->GetParams());
c357e426 245 Handle(Image_PixMap) anImage = myTextureEnvData->GetImage();
e276548b 246 if (!anImage.IsNull())
c357e426 247 {
cc8cbabe 248 aTextureEnv->Init (theContext, *anImage.operator->(), myTextureEnvData->Type());
c357e426 249 }
250}
e276548b 251
c357e426 252// =======================================================================
253// function : SetImmediateModeDrawToFront
254// purpose :
255// =======================================================================
256Standard_Boolean OpenGl_View::SetImmediateModeDrawToFront (const Standard_Boolean theDrawToFrontBuffer)
257{
258 const Standard_Boolean aPrevMode = myTransientDrawToFront;
259 myTransientDrawToFront = theDrawToFrontBuffer;
260 return aPrevMode;
e276548b 261}
262
c357e426 263// =======================================================================
264// function : SetWindow
265// purpose :
266// =======================================================================
267void OpenGl_View::SetWindow (const Handle(Aspect_Window)& theWindow,
a521d90d 268 const Aspect_RenderingContext theContext)
e276548b 269{
c357e426 270 myWindow = myDriver->CreateRenderWindow (theWindow, theContext);
271 Standard_ASSERT_RAISE (!myWindow.IsNull(),
272 "OpenGl_View::SetWindow, "
273 "Failed to create OpenGl window.");
e276548b 274
c357e426 275 myWorkspace = new OpenGl_Workspace (this, myWindow);
276 myWorldViewProjState.Reset();
91c60b57 277 myToUpdateEnvironmentMap = Standard_True;
c357e426 278 myHasFboBlit = Standard_True;
279 Invalidate();
280
281 // Environment texture resource does not support lazy initialization.
282 initTextureEnv (myWorkspace->GetGlContext());
283}
284
285// =======================================================================
286// function : Resized
287// purpose :
288// =======================================================================
289void OpenGl_View::Resized()
290{
291 if (myWindow.IsNull())
292 return;
293
294 myWindow->Resize();
295}
296
c357e426 297// =======================================================================
298// function : SetMinMaxValuesCallback
299// purpose :
300// =======================================================================
301static void SetMinMaxValuesCallback (Graphic3d_CView* theView)
302{
303 OpenGl_View* aView = dynamic_cast<OpenGl_View*>(theView);
304 if (aView == NULL)
305 return;
306
307 Bnd_Box aBox = theView->MinMaxValues();
308 if (!aBox.IsVoid())
2166f0fa 309 {
c357e426 310 gp_Pnt aMin = aBox.CornerMin();
311 gp_Pnt aMax = aBox.CornerMax();
312
313 Graphic3d_Vec3 aMinVec ((Standard_ShortReal )aMin.X(), (Standard_ShortReal )aMin.Y(), (Standard_ShortReal )aMin.Z());
314 Graphic3d_Vec3 aMaxVec ((Standard_ShortReal )aMax.X(), (Standard_ShortReal )aMax.Y(), (Standard_ShortReal )aMax.Z());
315 aView->GraduatedTrihedronMinMaxValues (aMinVec, aMaxVec);
2166f0fa
SK
316 }
317}
318
c357e426 319// =======================================================================
320// function : GraduatedTrihedronDisplay
321// purpose :
322// =======================================================================
323void OpenGl_View::GraduatedTrihedronDisplay (const Graphic3d_GraduatedTrihedron& theTrihedronData)
324{
325 myGTrihedronData = theTrihedronData;
326 myGTrihedronData.PtrView = this;
327 myGTrihedronData.CubicAxesCallback = SetMinMaxValuesCallback;
328 myGraduatedTrihedron.SetValues (myGTrihedronData);
329 myToShowGradTrihedron = true;
330}
2166f0fa 331
c357e426 332// =======================================================================
333// function : GraduatedTrihedronErase
334// purpose :
335// =======================================================================
336void OpenGl_View::GraduatedTrihedronErase()
2166f0fa 337{
c357e426 338 myGTrihedronData.PtrView = NULL;
339 myGraduatedTrihedron.Release (myWorkspace->GetGlContext().operator->());
340 myToShowGradTrihedron = false;
2166f0fa
SK
341}
342
c357e426 343// =======================================================================
344// function : GraduatedTrihedronMinMaxValues
345// purpose :
346// =======================================================================
347void OpenGl_View::GraduatedTrihedronMinMaxValues (const Graphic3d_Vec3 theMin, const Graphic3d_Vec3 theMax)
348{
349 myGraduatedTrihedron.SetMinMax (theMin, theMax);
350}
2166f0fa 351
c357e426 352// =======================================================================
353// function : BufferDump
354// purpose :
355// =======================================================================
356Standard_Boolean OpenGl_View::BufferDump (Image_PixMap& theImage, const Graphic3d_BufferType& theBufferType)
357{
38d90bb3 358 if (theBufferType != Graphic3d_BT_RGB_RayTraceHdrLeft)
359 {
360 return myWorkspace->BufferDump(myFBO, theImage, theBufferType);
361 }
362
363 if (!myRaytraceParameters.AdaptiveScreenSampling)
364 {
365 return myWorkspace->BufferDump(myAccumFrames % 2 ? myRaytraceFBO2[0] : myRaytraceFBO1[0], theImage, theBufferType);
366 }
367
368#if defined(GL_ES_VERSION_2_0)
369 return false;
370#else
371 if (theImage.Format() != Image_Format_RGBF)
372 {
373 return false;
374 }
375
376 const GLuint aW = myRaytraceOutputTexture[0]->SizeX();
377 const GLuint aH = myRaytraceOutputTexture[0]->SizeY();
378 if (aW / 3 != theImage.SizeX() || aH / 2 != theImage.SizeY())
379 {
380 return false;
381 }
382
383 std::vector<GLfloat> aValues;
384 try
385 {
386 aValues.resize (aW * aH);
387 }
388 catch (const std::bad_alloc&)
389 {
390 return false;
391 }
392
393 glBindTexture (GL_TEXTURE_RECTANGLE, myRaytraceOutputTexture[0]->TextureId());
394 glGetTexImage (GL_TEXTURE_RECTANGLE, 0, OpenGl_TextureFormat::Create<GLfloat, 1>().Format(), GL_FLOAT, &aValues[0]);
395 glBindTexture (GL_TEXTURE_RECTANGLE, 0);
396 for (unsigned int aRow = 0; aRow < aH; aRow += 2)
397 {
398 for (unsigned int aCol = 0; aCol < aW; aCol += 3)
399 {
400 float* anImageValue = theImage.ChangeValue<float[3]> ((aH - aRow) / 2 - 1, aCol / 3);
401 float aInvNbSamples = 1.f / aValues[aRow * aW + aCol + aW];
402 anImageValue[0] = aValues[aRow * aW + aCol] * aInvNbSamples;
403 anImageValue[1] = aValues[aRow * aW + aCol + 1] * aInvNbSamples;
404 anImageValue[2] = aValues[aRow * aW + aCol + 1 + aW] * aInvNbSamples;
405 }
406 }
407
408 return true;
409#endif
c357e426 410}
411
c357e426 412// =======================================================================
413// function : GradientBackground
414// purpose :
415// =======================================================================
416Aspect_GradientBackground OpenGl_View::GradientBackground() const
417{
418 Quantity_Color aColor1, aColor2;
419 aColor1.SetValues (myBgGradientArray->GradientColor (0).r(),
420 myBgGradientArray->GradientColor (0).g(),
421 myBgGradientArray->GradientColor (0).b(), Quantity_TOC_RGB);
422 aColor2.SetValues (myBgGradientArray->GradientColor (1).r(),
423 myBgGradientArray->GradientColor (1).g(),
424 myBgGradientArray->GradientColor (1).b(), Quantity_TOC_RGB);
425 return Aspect_GradientBackground (aColor1, aColor2, myBgGradientArray->GradientFillMethod());
426}
2166f0fa 427
c357e426 428// =======================================================================
429// function : SetGradientBackground
430// purpose :
431// =======================================================================
432void OpenGl_View::SetGradientBackground (const Aspect_GradientBackground& theBackground)
2166f0fa 433{
c357e426 434 Quantity_Color aColor1, aColor2;
435 theBackground.Colors (aColor1, aColor2);
436 myBgGradientArray->SetGradientParameters (aColor1, aColor2, theBackground.BgGradientFillMethod());
437}
438
439// =======================================================================
440// function : SetBackgroundImage
441// purpose :
442// =======================================================================
443void OpenGl_View::SetBackgroundImage (const TCollection_AsciiString& theFilePath)
444{
445 // Prepare aspect for texture storage
446 myBackgroundImagePath = theFilePath;
447 Handle(Graphic3d_AspectFillArea3d) anAspect = new Graphic3d_AspectFillArea3d();
448 Handle(Graphic3d_Texture2Dmanual) aTextureMap = new Graphic3d_Texture2Dmanual (TCollection_AsciiString (theFilePath));
449 aTextureMap->EnableRepeat();
450 aTextureMap->DisableModulate();
451 aTextureMap->GetParams()->SetGenMode (Graphic3d_TOTM_MANUAL,
452 Graphic3d_Vec4 (0.0f, 0.0f, 0.0f, 0.0f),
453 Graphic3d_Vec4 (0.0f, 0.0f, 0.0f, 0.0f));
454 anAspect->SetTextureMap (aTextureMap);
455 anAspect->SetInteriorStyle (Aspect_IS_SOLID);
b6472664 456 anAspect->SetSuppressBackFaces (false);
c357e426 457 // Enable texture mapping
458 if (aTextureMap->IsDone())
2166f0fa 459 {
c357e426 460 anAspect->SetTextureMapOn();
2166f0fa
SK
461 }
462 else
463 {
c357e426 464 anAspect->SetTextureMapOff();
465 return;
466 }
2166f0fa 467
c357e426 468 // Set texture parameters
469 myTextureParams->SetAspect (anAspect);
470}
2166f0fa 471
c357e426 472// =======================================================================
473// function : BackgroundImageStyle
474// purpose :
475// =======================================================================
476Aspect_FillMethod OpenGl_View::BackgroundImageStyle() const
477{
478 return myBgTextureArray->TextureFillMethod();
2166f0fa
SK
479}
480
c357e426 481// =======================================================================
482// function : SetBackgroundImageStyle
483// purpose :
484// =======================================================================
485void OpenGl_View::SetBackgroundImageStyle (const Aspect_FillMethod theFillStyle)
486{
487 myBgTextureArray->SetTextureFillMethod (theFillStyle);
488}
2166f0fa 489
c357e426 490//=======================================================================
1c728f2d 491//function : InsertLayerBefore
c357e426 492//purpose :
493//=======================================================================
1c728f2d 494void OpenGl_View::InsertLayerBefore (const Graphic3d_ZLayerId theLayerId,
495 const Graphic3d_ZLayerSettings& theSettings,
496 const Graphic3d_ZLayerId theLayerAfter)
2166f0fa 497{
1c728f2d 498 myZLayers.InsertLayerBefore (theLayerId, theSettings, theLayerAfter);
499}
500
501//=======================================================================
502//function : InsertLayerAfter
503//purpose :
504//=======================================================================
505void OpenGl_View::InsertLayerAfter (const Graphic3d_ZLayerId theLayerId,
506 const Graphic3d_ZLayerSettings& theSettings,
507 const Graphic3d_ZLayerId theLayerBefore)
508{
509 myZLayers.InsertLayerAfter (theLayerId, theSettings, theLayerBefore);
2166f0fa
SK
510}
511
c357e426 512//=======================================================================
513//function : RemoveZLayer
514//purpose :
515//=======================================================================
516void OpenGl_View::RemoveZLayer (const Graphic3d_ZLayerId theLayerId)
517{
518 myZLayers.RemoveLayer (theLayerId);
519}
2166f0fa 520
c357e426 521//=======================================================================
522//function : SetZLayerSettings
523//purpose :
524//=======================================================================
525void OpenGl_View::SetZLayerSettings (const Graphic3d_ZLayerId theLayerId,
526 const Graphic3d_ZLayerSettings& theSettings)
2166f0fa 527{
c357e426 528 myZLayers.SetLayerSettings (theLayerId, theSettings);
2166f0fa
SK
529}
530
50d06d8f 531//=======================================================================
532//function : ZLayerMax
533//purpose :
534//=======================================================================
535Standard_Integer OpenGl_View::ZLayerMax() const
536{
537 Standard_Integer aLayerMax = Graphic3d_ZLayerId_Default;
1c728f2d 538 for (NCollection_List<Handle(Graphic3d_Layer)>::Iterator aLayerIter (myZLayers.Layers()); aLayerIter.More(); aLayerIter.Next())
50d06d8f 539 {
1c728f2d 540 aLayerMax = Max (aLayerMax, aLayerIter.Value()->LayerId());
50d06d8f 541 }
50d06d8f 542 return aLayerMax;
543}
544
545//=======================================================================
1c728f2d 546//function : Layers
547//purpose :
548//=======================================================================
549const NCollection_List<Handle(Graphic3d_Layer)>& OpenGl_View::Layers() const
550{
551 return myZLayers.Layers();
552}
553
554//=======================================================================
555//function : Layer
50d06d8f 556//purpose :
557//=======================================================================
1c728f2d 558Handle(Graphic3d_Layer) OpenGl_View::Layer (const Graphic3d_ZLayerId theLayerId) const
50d06d8f 559{
1c728f2d 560 Handle(Graphic3d_Layer) aLayer;
561 if (theLayerId != Graphic3d_ZLayerId_UNKNOWN)
50d06d8f 562 {
1c728f2d 563 myZLayers.LayerIDs().Find (theLayerId, aLayer);
50d06d8f 564 }
1c728f2d 565 return aLayer;
50d06d8f 566}
567
568//=======================================================================
1c728f2d 569//function : MinMaxValues
50d06d8f 570//purpose :
571//=======================================================================
1c728f2d 572Bnd_Box OpenGl_View::MinMaxValues (const Standard_Boolean theToIncludeAuxiliary) const
50d06d8f 573{
1c728f2d 574 if (!IsDefined())
50d06d8f 575 {
1c728f2d 576 return Bnd_Box();
50d06d8f 577 }
578
1c728f2d 579 Bnd_Box aBox = base_type::MinMaxValues (theToIncludeAuxiliary);
580
71d8ccc7 581 // add bounding box of gradient/texture background for proper Z-fit
582 if (theToIncludeAuxiliary
71d8ccc7 583 && (myBgTextureArray->IsDefined()
584 || myBgGradientArray->IsDefined()))
585 {
1c728f2d 586 const Handle(Graphic3d_Camera)& aCamera = Camera();
587 Graphic3d_Vec2i aWinSize;
588 Window()->Size (aWinSize.x(), aWinSize.y());
589
71d8ccc7 590 // Background is drawn using 2D transformation persistence
591 // (e.g. it is actually placed in 3D coordinates within active camera position).
592 // We add here full-screen plane with 2D transformation persistence
593 // for simplicity (myBgTextureArray might define a little bit different options
594 // but it is updated within ::Render())
1c728f2d 595 const Graphic3d_Mat4d& aProjectionMat = aCamera->ProjectionMatrix();
596 const Graphic3d_Mat4d& aWorldViewMat = aCamera->OrientationMatrix();
7c3ef2f7 597 Graphic3d_BndBox3d aBox2d (Graphic3d_Vec3d (0.0, 0.0, 0.0),
1c728f2d 598 Graphic3d_Vec3d (double(aWinSize.x()), double(aWinSize.y()), 0.0));
71d8ccc7 599
778cd667 600 Graphic3d_TransformPers aTrsfPers (Graphic3d_TMF_2d, Aspect_TOTP_LEFT_LOWER);
1c728f2d 601 aTrsfPers.Apply (aCamera,
71d8ccc7 602 aProjectionMat,
603 aWorldViewMat,
1c728f2d 604 aWinSize.x(),
605 aWinSize.y(),
71d8ccc7 606 aBox2d);
7c3ef2f7 607 aBox.Add (gp_Pnt (aBox2d.CornerMin().x(), aBox2d.CornerMin().y(), aBox2d.CornerMin().z()));
608 aBox.Add (gp_Pnt (aBox2d.CornerMax().x(), aBox2d.CornerMax().y(), aBox2d.CornerMax().z()));
71d8ccc7 609 }
610
611 return aBox;
50d06d8f 612}
613
614//=======================================================================
c357e426 615//function : FBO
616//purpose :
617//=======================================================================
b128c892 618Handle(Standard_Transient) OpenGl_View::FBO() const
c357e426 619{
b128c892 620 return Handle(Standard_Transient)(myFBO);
c357e426 621}
2166f0fa 622
c357e426 623//=======================================================================
624//function : SetFBO
625//purpose :
626//=======================================================================
b128c892 627void OpenGl_View::SetFBO (const Handle(Standard_Transient)& theFbo)
2166f0fa 628{
b128c892 629 myFBO = Handle(OpenGl_FrameBuffer)::DownCast (theFbo);
2166f0fa
SK
630}
631
c357e426 632//=======================================================================
633//function : FBOCreate
634//purpose :
635//=======================================================================
b128c892 636Handle(Standard_Transient) OpenGl_View::FBOCreate (const Standard_Integer theWidth,
637 const Standard_Integer theHeight)
c357e426 638{
639 return myWorkspace->FBOCreate (theWidth, theHeight);
640}
2166f0fa 641
c357e426 642//=======================================================================
643//function : FBORelease
644//purpose :
645//=======================================================================
b128c892 646void OpenGl_View::FBORelease (Handle(Standard_Transient)& theFbo)
2166f0fa 647{
b128c892 648 Handle(OpenGl_FrameBuffer) aFrameBuffer = Handle(OpenGl_FrameBuffer)::DownCast (theFbo);
649 if (aFrameBuffer.IsNull())
650 {
651 return;
652 }
653
654 myWorkspace->FBORelease (aFrameBuffer);
655 theFbo.Nullify();
c357e426 656}
657
658//=======================================================================
659//function : FBOGetDimensions
660//purpose :
661//=======================================================================
b128c892 662void OpenGl_View::FBOGetDimensions (const Handle(Standard_Transient)& theFbo,
c357e426 663 Standard_Integer& theWidth,
664 Standard_Integer& theHeight,
665 Standard_Integer& theWidthMax,
666 Standard_Integer& theHeightMax)
667{
b128c892 668 const Handle(OpenGl_FrameBuffer) aFrameBuffer = Handle(OpenGl_FrameBuffer)::DownCast (theFbo);
669 if (aFrameBuffer.IsNull())
670 {
671 return;
672 }
673
c357e426 674 theWidth = aFrameBuffer->GetVPSizeX(); // current viewport size
675 theHeight = aFrameBuffer->GetVPSizeY();
676 theWidthMax = aFrameBuffer->GetSizeX(); // texture size
677 theHeightMax = aFrameBuffer->GetSizeY();
678}
679
680//=======================================================================
681//function : FBOChangeViewport
682//purpose :
683//=======================================================================
b128c892 684void OpenGl_View::FBOChangeViewport (const Handle(Standard_Transient)& theFbo,
c357e426 685 const Standard_Integer theWidth,
686 const Standard_Integer theHeight)
687{
b128c892 688 const Handle(OpenGl_FrameBuffer) aFrameBuffer = Handle(OpenGl_FrameBuffer)::DownCast (theFbo);
689 if (aFrameBuffer.IsNull())
690 {
691 return;
692 }
693
c357e426 694 aFrameBuffer->ChangeViewport (theWidth, theHeight);
695}
696
c357e426 697//=======================================================================
698//function : displayStructure
699//purpose :
700//=======================================================================
701void OpenGl_View::displayStructure (const Handle(Graphic3d_CStructure)& theStructure,
702 const Standard_Integer thePriority)
703{
704 const OpenGl_Structure* aStruct = reinterpret_cast<const OpenGl_Structure*> (theStructure.operator->());
705 const Graphic3d_ZLayerId aZLayer = aStruct->ZLayer();
706 myZLayers.AddStructure (aStruct, aZLayer, thePriority);
707}
708
709//=======================================================================
710//function : eraseStructure
711//purpose :
712//=======================================================================
713void OpenGl_View::eraseStructure (const Handle(Graphic3d_CStructure)& theStructure)
714{
715 const OpenGl_Structure* aStruct = reinterpret_cast<const OpenGl_Structure*> (theStructure.operator->());
716 myZLayers.RemoveStructure (aStruct);
717}
718
719//=======================================================================
720//function : changeZLayer
721//purpose :
722//=======================================================================
723void OpenGl_View::changeZLayer (const Handle(Graphic3d_CStructure)& theStructure,
724 const Graphic3d_ZLayerId theNewLayerId)
725{
726 const Graphic3d_ZLayerId anOldLayer = theStructure->ZLayer();
727 const OpenGl_Structure* aStruct = reinterpret_cast<const OpenGl_Structure*> (theStructure.operator->());
728 myZLayers.ChangeLayer (aStruct, anOldLayer, theNewLayerId);
cfece3ef 729 Update (anOldLayer);
730 Update (theNewLayerId);
c357e426 731}
732
733//=======================================================================
734//function : changePriority
735//purpose :
736//=======================================================================
737void OpenGl_View::changePriority (const Handle(Graphic3d_CStructure)& theStructure,
738 const Standard_Integer theNewPriority)
739{
740 const Graphic3d_ZLayerId aLayerId = theStructure->ZLayer();
741 const OpenGl_Structure* aStruct = reinterpret_cast<const OpenGl_Structure*> (theStructure.operator->());
742 myZLayers.ChangePriority (aStruct, aLayerId, theNewPriority);
2166f0fa 743}
26d9c835 744
745//=======================================================================
746//function : DiagnosticInformation
747//purpose :
748//=======================================================================
749void OpenGl_View::DiagnosticInformation (TColStd_IndexedDataMapOfStringString& theDict,
750 Graphic3d_DiagnosticInfo theFlags) const
751{
752 Handle(OpenGl_Context) aCtx = myWorkspace->GetGlContext();
753 if (!myWorkspace->Activate()
754 || aCtx.IsNull())
755 {
756 return;
757 }
758
759 aCtx->DiagnosticInformation (theDict, theFlags);
760 if ((theFlags & Graphic3d_DiagnosticInfo_FrameBuffer) != 0)
761 {
762 TCollection_AsciiString aResRatio (myRenderParams.ResolutionRatio());
763 theDict.ChangeFromIndex (theDict.Add ("ResolutionRatio", aResRatio)) = aResRatio;
764 }
765}
79b544e6 766
767//=======================================================================
768//function : StatisticInformation
769//purpose :
770//=======================================================================
771void OpenGl_View::StatisticInformation (TColStd_IndexedDataMapOfStringString& theDict) const
772{
773 if (const Handle(OpenGl_Context)& aCtx = myWorkspace->GetGlContext())
774 {
775 const Handle(OpenGl_FrameStats)& aStats = aCtx->FrameStats();
776 const Graphic3d_RenderingParams& aRendParams = myWorkspace->View()->RenderingParams();
777 aStats->FormatStats (theDict, aRendParams.CollectedStats);
778 }
779}
780
781//=======================================================================
782//function : StatisticInformation
783//purpose :
784//=======================================================================
785TCollection_AsciiString OpenGl_View::StatisticInformation() const
786{
787 if (const Handle(OpenGl_Context)& aCtx = myWorkspace->GetGlContext())
788 {
789 const Handle(OpenGl_FrameStats)& aStats = aCtx->FrameStats();
790 const Graphic3d_RenderingParams& aRendParams = myWorkspace->View()->RenderingParams();
791 return aStats->FormatStats (aRendParams.CollectedStats);
792 }
793 return TCollection_AsciiString();
794}