1 // Created on: 2011-09-20
2 // Created by: Sergey ZERCHANINOV
3 // Copyright (c) 2011-2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
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
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.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 #include <OpenGl_View.hxx>
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>
25 #include <OpenGl_Context.hxx>
26 #include <OpenGl_FrameBuffer.hxx>
27 #include <OpenGl_GlCore11.hxx>
28 #include <OpenGl_GraduatedTrihedron.hxx>
29 #include <OpenGl_GraphicDriver.hxx>
30 #include <OpenGl_ShaderManager.hxx>
31 #include <OpenGl_Texture.hxx>
32 #include <OpenGl_Window.hxx>
33 #include <OpenGl_Workspace.hxx>
34 #include <OSD_Parallel.hxx>
35 #include <Standard_CLocaleSentry.hxx>
37 #include "../Graphic3d/Graphic3d_Structure.pxx"
39 IMPLEMENT_STANDARD_RTTIEXT(OpenGl_View,Graphic3d_CView)
41 // =======================================================================
42 // function : Constructor
44 // =======================================================================
45 OpenGl_View::OpenGl_View (const Handle(Graphic3d_StructureManager)& theMgr,
46 const Handle(OpenGl_GraphicDriver)& theDriver,
47 const Handle(OpenGl_Caps)& theCaps,
48 OpenGl_StateCounter* theCounter)
49 : Graphic3d_CView (theMgr),
50 myDriver (theDriver.operator->()),
52 myWasRedrawnGL (Standard_False),
53 myBackfacing (Graphic3d_TOBM_AUTOMATIC),
54 myToShowGradTrihedron (false),
55 myZLayers (Structure_MAX_PRIORITY - Structure_MIN_PRIORITY + 1),
56 myStateCounter (theCounter),
57 myCurrLightSourceState (theCounter->Increment()),
59 myLastLightSourceState (0, 0),
61 myFboColorFormat (GL_SRGB8_ALPHA8), // note that GL_SRGB8 is not required to be renderable, unlike GL_RGB8, GL_RGBA8, GL_SRGB8_ALPHA8
62 myFboDepthFormat (GL_DEPTH24_STENCIL8),
63 myToFlipOutput (Standard_False),
65 myHasFboBlit (Standard_True),
66 myToDisableOIT (Standard_False),
67 myToDisableOITMSAA (Standard_False),
68 myToDisableMSAA (Standard_False),
69 myTransientDrawToFront (Standard_True),
70 myBackBufferRestored (Standard_False),
71 myIsImmediateDrawn (Standard_False),
72 myTextureParams (new OpenGl_Aspects()),
73 myCubeMapParams (new OpenGl_Aspects()),
74 myBackgroundType (Graphic3d_TOB_NONE),
75 myPBREnvState (OpenGl_PBREnvState_NONEXISTENT),
76 myPBREnvRequest (OpenGl_PBREnvRequest_NONE),
77 // ray-tracing fields initialization
78 myRaytraceInitStatus (OpenGl_RT_NONE),
79 myIsRaytraceDataValid (Standard_False),
80 myIsRaytraceWarnTextures (Standard_False),
81 myRaytraceBVHBuilder (new BVH_BinnedBuilder<Standard_ShortReal, 3, BVH_Constants_NbBinsBest> (BVH_Constants_LeafNodeSizeAverage,
82 BVH_Constants_MaxTreeDepth,
84 OSD_Parallel::NbLogicalProcessors() + 1)),
85 myRaytraceSceneRadius (0.0f),
86 myRaytraceSceneEpsilon (1.0e-6f),
87 myToUpdateEnvironmentMap (Standard_False),
88 myRaytraceLayerListState (0),
89 myPrevCameraApertureRadius(0.f),
90 myPrevCameraFocalPlaneDist(0.f)
92 for (int i = 0; i < Graphic3d_TypeOfBackground_NB; ++i)
94 myBackgrounds[i] = new OpenGl_BackgroundArray(Graphic3d_TypeOfBackground(i));
97 myWorkspace = new OpenGl_Workspace (this, NULL);
99 Handle(Graphic3d_CLight) aLight = new Graphic3d_CLight (Graphic3d_TOLS_AMBIENT);
100 aLight->SetHeadlight (false);
101 aLight->SetColor (Quantity_NOC_WHITE);
102 myLights = new Graphic3d_LightSet();
103 myNoShadingLight = new Graphic3d_LightSet();
104 myNoShadingLight->Add (aLight);
106 myMainSceneFbos[0] = new OpenGl_FrameBuffer();
107 myMainSceneFbos[1] = new OpenGl_FrameBuffer();
108 myMainSceneFbosOit[0] = new OpenGl_FrameBuffer();
109 myMainSceneFbosOit[1] = new OpenGl_FrameBuffer();
110 myImmediateSceneFbos[0] = new OpenGl_FrameBuffer();
111 myImmediateSceneFbos[1] = new OpenGl_FrameBuffer();
112 myImmediateSceneFbosOit[0] = new OpenGl_FrameBuffer();
113 myImmediateSceneFbosOit[1] = new OpenGl_FrameBuffer();
114 myOpenGlFBO = new OpenGl_FrameBuffer();
115 myOpenGlFBO2 = new OpenGl_FrameBuffer();
116 myRaytraceFBO1[0] = new OpenGl_FrameBuffer();
117 myRaytraceFBO1[1] = new OpenGl_FrameBuffer();
118 myRaytraceFBO2[0] = new OpenGl_FrameBuffer();
119 myRaytraceFBO2[1] = new OpenGl_FrameBuffer();
122 // =======================================================================
123 // function : Destructor
125 // =======================================================================
126 OpenGl_View::~OpenGl_View()
128 ReleaseGlResources (NULL); // ensure ReleaseGlResources() was called within valid context
129 for (int i = 0; i < Graphic3d_TypeOfBackground_NB; ++i)
131 OpenGl_Element::Destroy(NULL, myBackgrounds[i]);
134 OpenGl_Element::Destroy (NULL, myTextureParams);
135 OpenGl_Element::Destroy (NULL, myCubeMapParams);
138 // =======================================================================
139 // function : releaseSrgbResources
141 // =======================================================================
142 void OpenGl_View::releaseSrgbResources (const Handle(OpenGl_Context)& theCtx)
144 myRenderParams.RebuildRayTracingShaders = true;
146 if (!myTextureEnv.IsNull())
148 if (!theCtx.IsNull())
150 for (OpenGl_TextureSet::Iterator aTextureIter (myTextureEnv); aTextureIter.More(); aTextureIter.Next())
152 theCtx->DelayedRelease (aTextureIter.ChangeValue());
153 aTextureIter.ChangeValue().Nullify();
156 myTextureEnv.Nullify();
159 if (myTextureParams != NULL)
161 myTextureParams->Release (theCtx.get());
164 if (myCubeMapParams != NULL)
166 myCubeMapParams->Release (theCtx.get());
169 for (int i = 0; i < Graphic3d_TypeOfBackground_NB; ++i)
171 if (myBackgrounds[i] != NULL)
173 myBackgrounds[i]->Release (theCtx.get());
177 myMainSceneFbos[0] ->Release (theCtx.get());
178 myMainSceneFbos[1] ->Release (theCtx.get());
179 myMainSceneFbosOit[0] ->Release (theCtx.get());
180 myMainSceneFbosOit[1] ->Release (theCtx.get());
181 myImmediateSceneFbos[0] ->Release (theCtx.get());
182 myImmediateSceneFbos[1] ->Release (theCtx.get());
183 myImmediateSceneFbosOit[0]->Release (theCtx.get());
184 myImmediateSceneFbosOit[1]->Release (theCtx.get());
185 myOpenGlFBO ->Release (theCtx.get());
186 myOpenGlFBO2 ->Release (theCtx.get());
187 myFullScreenQuad .Release (theCtx.get());
188 myFullScreenQuadFlip .Release (theCtx.get());
190 // Technically we should also re-initialize all sRGB/RGB8 color textures.
191 // But for now consider this sRGB disabling/enabling to be done at application start-up
192 // and re-create dynamically only frame buffers.
195 // =======================================================================
196 // function : ReleaseGlResources
198 // =======================================================================
199 void OpenGl_View::ReleaseGlResources (const Handle(OpenGl_Context)& theCtx)
201 myGraduatedTrihedron.Release (theCtx.get());
202 myFrameStatsPrs.Release (theCtx.get());
204 releaseSrgbResources (theCtx);
206 releaseRaytraceResources (theCtx);
208 if (!myPBREnvironment.IsNull())
210 myPBREnvironment->Release (theCtx.get());
214 // =======================================================================
217 // =======================================================================
218 void OpenGl_View::Remove()
225 myDriver->RemoveView (this);
228 Graphic3d_CView::Remove();
231 // =======================================================================
232 // function : SetLocalOrigin
234 // =======================================================================
235 void OpenGl_View::SetLocalOrigin (const gp_XYZ& theOrigin)
237 myLocalOrigin = theOrigin;
238 const Handle(OpenGl_Context)& aCtx = myWorkspace->GetGlContext();
241 aCtx->ShaderManager()->SetLocalOrigin (theOrigin);
245 // =======================================================================
246 // function : SetTextureEnv
248 // =======================================================================
249 void OpenGl_View::SetTextureEnv (const Handle(Graphic3d_TextureEnv)& theTextureEnv)
251 Handle(OpenGl_Context) aCtx = myWorkspace->GetGlContext();
252 if (!aCtx.IsNull() && !myTextureEnv.IsNull())
254 for (OpenGl_TextureSet::Iterator aTextureIter (myTextureEnv); aTextureIter.More(); aTextureIter.Next())
256 aCtx->DelayedRelease (aTextureIter.ChangeValue());
257 aTextureIter.ChangeValue().Nullify();
261 myToUpdateEnvironmentMap = Standard_True;
262 myTextureEnvData = theTextureEnv;
263 myTextureEnv.Nullify();
264 initTextureEnv (aCtx);
267 // =======================================================================
268 // function : initTextureEnv
270 // =======================================================================
271 void OpenGl_View::initTextureEnv (const Handle(OpenGl_Context)& theContext)
273 if (myTextureEnvData.IsNull()
274 || theContext.IsNull()
275 || !theContext->MakeCurrent())
280 myTextureEnv = new OpenGl_TextureSet (1);
281 Handle(OpenGl_Texture)& aTextureEnv = myTextureEnv->ChangeFirst();
282 aTextureEnv = new OpenGl_Texture (myTextureEnvData->GetId(), myTextureEnvData->GetParams());
283 Handle(Image_PixMap) anImage = myTextureEnvData->GetImage();
284 if (!anImage.IsNull())
286 aTextureEnv->Init (theContext, *anImage, myTextureEnvData->Type(), true);
290 // =======================================================================
291 // function : SetImmediateModeDrawToFront
293 // =======================================================================
294 Standard_Boolean OpenGl_View::SetImmediateModeDrawToFront (const Standard_Boolean theDrawToFrontBuffer)
296 const Standard_Boolean aPrevMode = myTransientDrawToFront;
297 myTransientDrawToFront = theDrawToFrontBuffer;
301 // =======================================================================
302 // function : SetWindow
304 // =======================================================================
305 void OpenGl_View::SetWindow (const Handle(Aspect_Window)& theWindow,
306 const Aspect_RenderingContext theContext)
308 myWindow = myDriver->CreateRenderWindow (theWindow, theContext);
309 Standard_ASSERT_RAISE (!myWindow.IsNull(),
310 "OpenGl_View::SetWindow, "
311 "Failed to create OpenGl window.");
313 myWorkspace = new OpenGl_Workspace (this, myWindow);
314 myWorldViewProjState.Reset();
315 myToUpdateEnvironmentMap = Standard_True;
316 myHasFboBlit = Standard_True;
319 // Environment texture resource does not support lazy initialization.
320 initTextureEnv (myWorkspace->GetGlContext());
323 // =======================================================================
324 // function : Resized
326 // =======================================================================
327 void OpenGl_View::Resized()
329 if (myWindow.IsNull())
335 // =======================================================================
336 // function : SetMinMaxValuesCallback
338 // =======================================================================
339 static void SetMinMaxValuesCallback (Graphic3d_CView* theView)
341 OpenGl_View* aView = dynamic_cast<OpenGl_View*>(theView);
345 Bnd_Box aBox = theView->MinMaxValues();
348 gp_Pnt aMin = aBox.CornerMin();
349 gp_Pnt aMax = aBox.CornerMax();
351 Graphic3d_Vec3 aMinVec ((Standard_ShortReal )aMin.X(), (Standard_ShortReal )aMin.Y(), (Standard_ShortReal )aMin.Z());
352 Graphic3d_Vec3 aMaxVec ((Standard_ShortReal )aMax.X(), (Standard_ShortReal )aMax.Y(), (Standard_ShortReal )aMax.Z());
353 aView->GraduatedTrihedronMinMaxValues (aMinVec, aMaxVec);
357 // =======================================================================
358 // function : GraduatedTrihedronDisplay
360 // =======================================================================
361 void OpenGl_View::GraduatedTrihedronDisplay (const Graphic3d_GraduatedTrihedron& theTrihedronData)
363 myGTrihedronData = theTrihedronData;
364 myGTrihedronData.PtrView = this;
365 myGTrihedronData.CubicAxesCallback = SetMinMaxValuesCallback;
366 myGraduatedTrihedron.SetValues (myGTrihedronData);
367 myToShowGradTrihedron = true;
370 // =======================================================================
371 // function : GraduatedTrihedronErase
373 // =======================================================================
374 void OpenGl_View::GraduatedTrihedronErase()
376 myGTrihedronData.PtrView = NULL;
377 myGraduatedTrihedron.Release (myWorkspace->GetGlContext().operator->());
378 myToShowGradTrihedron = false;
381 // =======================================================================
382 // function : GraduatedTrihedronMinMaxValues
384 // =======================================================================
385 void OpenGl_View::GraduatedTrihedronMinMaxValues (const Graphic3d_Vec3 theMin, const Graphic3d_Vec3 theMax)
387 myGraduatedTrihedron.SetMinMax (theMin, theMax);
390 // =======================================================================
391 // function : BufferDump
393 // =======================================================================
394 Standard_Boolean OpenGl_View::BufferDump (Image_PixMap& theImage, const Graphic3d_BufferType& theBufferType)
396 if (theBufferType != Graphic3d_BT_RGB_RayTraceHdrLeft)
398 return myWorkspace->BufferDump(myFBO, theImage, theBufferType);
401 if (!myRaytraceParameters.AdaptiveScreenSampling)
403 return myWorkspace->BufferDump(myAccumFrames % 2 ? myRaytraceFBO2[0] : myRaytraceFBO1[0], theImage, theBufferType);
406 #if defined(GL_ES_VERSION_2_0)
409 if (theImage.Format() != Image_Format_RGBF)
414 const GLuint aW = myRaytraceOutputTexture[0]->SizeX();
415 const GLuint aH = myRaytraceOutputTexture[0]->SizeY();
416 if (aW / 3 != theImage.SizeX() || aH / 2 != theImage.SizeY())
421 std::vector<GLfloat> aValues;
424 aValues.resize (aW * aH);
426 catch (const std::bad_alloc&)
431 glBindTexture (GL_TEXTURE_RECTANGLE, myRaytraceOutputTexture[0]->TextureId());
432 glGetTexImage (GL_TEXTURE_RECTANGLE, 0, OpenGl_TextureFormat::Create<GLfloat, 1>().Format(), GL_FLOAT, &aValues[0]);
433 glBindTexture (GL_TEXTURE_RECTANGLE, 0);
434 for (unsigned int aRow = 0; aRow < aH; aRow += 2)
436 for (unsigned int aCol = 0; aCol < aW; aCol += 3)
438 float* anImageValue = theImage.ChangeValue<float[3]> ((aH - aRow) / 2 - 1, aCol / 3);
439 float aInvNbSamples = 1.f / aValues[aRow * aW + aCol + aW];
440 anImageValue[0] = aValues[aRow * aW + aCol] * aInvNbSamples;
441 anImageValue[1] = aValues[aRow * aW + aCol + 1] * aInvNbSamples;
442 anImageValue[2] = aValues[aRow * aW + aCol + 1 + aW] * aInvNbSamples;
450 // =======================================================================
451 // function : GradientBackground
453 // =======================================================================
454 Aspect_GradientBackground OpenGl_View::GradientBackground() const
456 Quantity_Color aColor1, aColor2;
457 aColor1.SetValues (myBackgrounds[Graphic3d_TOB_GRADIENT]->GradientColor (0).r(),
458 myBackgrounds[Graphic3d_TOB_GRADIENT]->GradientColor (0).g(),
459 myBackgrounds[Graphic3d_TOB_GRADIENT]->GradientColor (0).b(), Quantity_TOC_RGB);
460 aColor2.SetValues (myBackgrounds[Graphic3d_TOB_GRADIENT]->GradientColor (1).r(),
461 myBackgrounds[Graphic3d_TOB_GRADIENT]->GradientColor (1).g(),
462 myBackgrounds[Graphic3d_TOB_GRADIENT]->GradientColor (1).b(), Quantity_TOC_RGB);
463 return Aspect_GradientBackground (aColor1, aColor2, myBackgrounds[Graphic3d_TOB_GRADIENT]->GradientFillMethod());
466 // =======================================================================
467 // function : SetGradientBackground
469 // =======================================================================
470 void OpenGl_View::SetGradientBackground (const Aspect_GradientBackground& theBackground)
472 Quantity_Color aColor1, aColor2;
473 theBackground.Colors (aColor1, aColor2);
474 myBackgrounds[Graphic3d_TOB_GRADIENT]->SetGradientParameters (aColor1, aColor2, theBackground.BgGradientFillMethod());
476 myBackgroundType = Graphic3d_TOB_GRADIENT;
479 // =======================================================================
480 // function : SetBackgroundImage
482 // =======================================================================
483 void OpenGl_View::SetBackgroundImage (const TCollection_AsciiString& theFilePath)
485 // Prepare aspect for texture storage
486 myBackgroundImagePath = theFilePath;
487 Handle(Graphic3d_AspectFillArea3d) anAspect = new Graphic3d_AspectFillArea3d();
488 Handle(Graphic3d_Texture2Dmanual) aTextureMap = new Graphic3d_Texture2Dmanual (TCollection_AsciiString (theFilePath));
489 aTextureMap->EnableRepeat();
490 aTextureMap->DisableModulate();
491 aTextureMap->GetParams()->SetGenMode (Graphic3d_TOTM_MANUAL,
492 Graphic3d_Vec4 (0.0f, 0.0f, 0.0f, 0.0f),
493 Graphic3d_Vec4 (0.0f, 0.0f, 0.0f, 0.0f));
494 anAspect->SetTextureMap (aTextureMap);
495 anAspect->SetInteriorStyle (Aspect_IS_SOLID);
496 anAspect->SetSuppressBackFaces (false);
497 // Enable texture mapping
498 if (aTextureMap->IsDone())
500 anAspect->SetTextureMapOn();
504 anAspect->SetTextureMapOff();
508 // Set texture parameters
509 myTextureParams->SetAspect (anAspect);
511 myBackgroundType = Graphic3d_TOB_TEXTURE;
514 // =======================================================================
515 // function : BackgroundImageStyle
517 // =======================================================================
518 Aspect_FillMethod OpenGl_View::BackgroundImageStyle() const
520 return myBackgrounds[Graphic3d_TOB_TEXTURE]->TextureFillMethod();
523 // =======================================================================
524 // function : SetBackgroundImageStyle
526 // =======================================================================
527 void OpenGl_View::SetBackgroundImageStyle (const Aspect_FillMethod theFillStyle)
529 myBackgrounds[Graphic3d_TOB_TEXTURE]->SetTextureFillMethod (theFillStyle);
532 // =======================================================================
533 // function : BackgroundCubeMap
535 // =======================================================================
536 Handle(Graphic3d_CubeMap) OpenGl_View::BackgroundCubeMap() const
538 return myBackgroundCubeMap;
541 // =======================================================================
542 // function : SpecIBLMapLevels
544 // =======================================================================
545 unsigned int OpenGl_View::SpecIBLMapLevels() const
547 return myPBREnvironment.IsNull() ? 0 : myPBREnvironment->SpecMapLevelsNumber();
550 // =======================================================================
551 // function : SetBackgroundCubeMap
553 // =======================================================================
554 void OpenGl_View::SetBackgroundCubeMap (const Handle(Graphic3d_CubeMap)& theCubeMap,
555 Standard_Boolean theToUpdatePBREnv)
557 myBackgroundCubeMap = theCubeMap;
558 if (theCubeMap.IsNull())
560 if (theToUpdatePBREnv)
562 myPBREnvRequest = OpenGl_PBREnvRequest_CLEAR;
564 if (myBackgroundType == Graphic3d_TOB_CUBEMAP)
566 myBackgroundType = Graphic3d_TOB_NONE;
571 theCubeMap ->SetMipmapsGeneration (Standard_True);
572 Handle(Graphic3d_AspectFillArea3d) anAspect = new Graphic3d_AspectFillArea3d();
573 Handle(Graphic3d_TextureSet) aTextureSet = new Graphic3d_TextureSet (myBackgroundCubeMap);
574 anAspect->SetInteriorStyle (Aspect_IS_SOLID);
575 anAspect->SetSuppressBackFaces (false);
576 anAspect->SetTextureSet (aTextureSet);
578 const Handle(OpenGl_Context)& aCtx = myWorkspace->GetGlContext();
581 anAspect->SetShaderProgram (aCtx->ShaderManager()->GetBgCubeMapProgram());
583 anAspect->SetTextureMapOn (theCubeMap->IsDone());
584 myCubeMapParams->SetAspect (anAspect);
586 if (theToUpdatePBREnv)
588 myPBREnvRequest = OpenGl_PBREnvRequest_BAKE;
590 const OpenGl_Aspects* anAspectsBackup = myWorkspace->SetAspects (myCubeMapParams);
591 myWorkspace->ApplyAspects();
592 myWorkspace->SetAspects (anAspectsBackup);
593 myWorkspace->ApplyAspects();
595 myBackgroundType = Graphic3d_TOB_CUBEMAP;
598 //=======================================================================
599 //function : InsertLayerBefore
601 //=======================================================================
602 void OpenGl_View::InsertLayerBefore (const Graphic3d_ZLayerId theLayerId,
603 const Graphic3d_ZLayerSettings& theSettings,
604 const Graphic3d_ZLayerId theLayerAfter)
606 myZLayers.InsertLayerBefore (theLayerId, theSettings, theLayerAfter);
609 //=======================================================================
610 //function : InsertLayerAfter
612 //=======================================================================
613 void OpenGl_View::InsertLayerAfter (const Graphic3d_ZLayerId theLayerId,
614 const Graphic3d_ZLayerSettings& theSettings,
615 const Graphic3d_ZLayerId theLayerBefore)
617 myZLayers.InsertLayerAfter (theLayerId, theSettings, theLayerBefore);
620 //=======================================================================
621 //function : RemoveZLayer
623 //=======================================================================
624 void OpenGl_View::RemoveZLayer (const Graphic3d_ZLayerId theLayerId)
626 myZLayers.RemoveLayer (theLayerId);
629 //=======================================================================
630 //function : SetZLayerSettings
632 //=======================================================================
633 void OpenGl_View::SetZLayerSettings (const Graphic3d_ZLayerId theLayerId,
634 const Graphic3d_ZLayerSettings& theSettings)
636 myZLayers.SetLayerSettings (theLayerId, theSettings);
639 //=======================================================================
640 //function : ZLayerMax
642 //=======================================================================
643 Standard_Integer OpenGl_View::ZLayerMax() const
645 Standard_Integer aLayerMax = Graphic3d_ZLayerId_Default;
646 for (NCollection_List<Handle(Graphic3d_Layer)>::Iterator aLayerIter (myZLayers.Layers()); aLayerIter.More(); aLayerIter.Next())
648 aLayerMax = Max (aLayerMax, aLayerIter.Value()->LayerId());
653 //=======================================================================
656 //=======================================================================
657 const NCollection_List<Handle(Graphic3d_Layer)>& OpenGl_View::Layers() const
659 return myZLayers.Layers();
662 //=======================================================================
665 //=======================================================================
666 Handle(Graphic3d_Layer) OpenGl_View::Layer (const Graphic3d_ZLayerId theLayerId) const
668 Handle(Graphic3d_Layer) aLayer;
669 if (theLayerId != Graphic3d_ZLayerId_UNKNOWN)
671 myZLayers.LayerIDs().Find (theLayerId, aLayer);
676 //=======================================================================
677 //function : MinMaxValues
679 //=======================================================================
680 Bnd_Box OpenGl_View::MinMaxValues (const Standard_Boolean theToIncludeAuxiliary) const
687 Bnd_Box aBox = base_type::MinMaxValues (theToIncludeAuxiliary);
689 // add bounding box of gradient/texture background for proper Z-fit
690 if (theToIncludeAuxiliary
691 && (myBackgrounds[Graphic3d_TOB_TEXTURE]->IsDefined()
692 || myBackgrounds[Graphic3d_TOB_GRADIENT]->IsDefined()))
694 const Handle(Graphic3d_Camera)& aCamera = Camera();
695 Graphic3d_Vec2i aWinSize;
696 Window()->Size (aWinSize.x(), aWinSize.y());
698 // Background is drawn using 2D transformation persistence
699 // (e.g. it is actually placed in 3D coordinates within active camera position).
700 // We add here full-screen plane with 2D transformation persistence
701 // for simplicity (myBgTextureArray might define a little bit different options
702 // but it is updated within ::Render())
703 const Graphic3d_Mat4d& aProjectionMat = aCamera->ProjectionMatrix();
704 const Graphic3d_Mat4d& aWorldViewMat = aCamera->OrientationMatrix();
705 Graphic3d_BndBox3d aBox2d (Graphic3d_Vec3d (0.0, 0.0, 0.0),
706 Graphic3d_Vec3d (double(aWinSize.x()), double(aWinSize.y()), 0.0));
708 Graphic3d_TransformPers aTrsfPers (Graphic3d_TMF_2d, Aspect_TOTP_LEFT_LOWER);
709 aTrsfPers.Apply (aCamera,
715 aBox.Add (gp_Pnt (aBox2d.CornerMin().x(), aBox2d.CornerMin().y(), aBox2d.CornerMin().z()));
716 aBox.Add (gp_Pnt (aBox2d.CornerMax().x(), aBox2d.CornerMax().y(), aBox2d.CornerMax().z()));
722 //=======================================================================
725 //=======================================================================
726 Handle(Standard_Transient) OpenGl_View::FBO() const
728 return Handle(Standard_Transient)(myFBO);
731 //=======================================================================
734 //=======================================================================
735 void OpenGl_View::SetFBO (const Handle(Standard_Transient)& theFbo)
737 myFBO = Handle(OpenGl_FrameBuffer)::DownCast (theFbo);
740 //=======================================================================
741 //function : FBOCreate
743 //=======================================================================
744 Handle(Standard_Transient) OpenGl_View::FBOCreate (const Standard_Integer theWidth,
745 const Standard_Integer theHeight)
747 return myWorkspace->FBOCreate (theWidth, theHeight);
750 //=======================================================================
751 //function : FBORelease
753 //=======================================================================
754 void OpenGl_View::FBORelease (Handle(Standard_Transient)& theFbo)
756 Handle(OpenGl_FrameBuffer) aFrameBuffer = Handle(OpenGl_FrameBuffer)::DownCast (theFbo);
757 if (aFrameBuffer.IsNull())
762 myWorkspace->FBORelease (aFrameBuffer);
766 //=======================================================================
767 //function : FBOGetDimensions
769 //=======================================================================
770 void OpenGl_View::FBOGetDimensions (const Handle(Standard_Transient)& theFbo,
771 Standard_Integer& theWidth,
772 Standard_Integer& theHeight,
773 Standard_Integer& theWidthMax,
774 Standard_Integer& theHeightMax)
776 const Handle(OpenGl_FrameBuffer) aFrameBuffer = Handle(OpenGl_FrameBuffer)::DownCast (theFbo);
777 if (aFrameBuffer.IsNull())
782 theWidth = aFrameBuffer->GetVPSizeX(); // current viewport size
783 theHeight = aFrameBuffer->GetVPSizeY();
784 theWidthMax = aFrameBuffer->GetSizeX(); // texture size
785 theHeightMax = aFrameBuffer->GetSizeY();
788 //=======================================================================
789 //function : FBOChangeViewport
791 //=======================================================================
792 void OpenGl_View::FBOChangeViewport (const Handle(Standard_Transient)& theFbo,
793 const Standard_Integer theWidth,
794 const Standard_Integer theHeight)
796 const Handle(OpenGl_FrameBuffer) aFrameBuffer = Handle(OpenGl_FrameBuffer)::DownCast (theFbo);
797 if (aFrameBuffer.IsNull())
802 aFrameBuffer->ChangeViewport (theWidth, theHeight);
805 //=======================================================================
806 //function : displayStructure
808 //=======================================================================
809 void OpenGl_View::displayStructure (const Handle(Graphic3d_CStructure)& theStructure,
810 const Standard_Integer thePriority)
812 const OpenGl_Structure* aStruct = reinterpret_cast<const OpenGl_Structure*> (theStructure.operator->());
813 const Graphic3d_ZLayerId aZLayer = aStruct->ZLayer();
814 myZLayers.AddStructure (aStruct, aZLayer, thePriority);
817 //=======================================================================
818 //function : eraseStructure
820 //=======================================================================
821 void OpenGl_View::eraseStructure (const Handle(Graphic3d_CStructure)& theStructure)
823 const OpenGl_Structure* aStruct = reinterpret_cast<const OpenGl_Structure*> (theStructure.operator->());
824 myZLayers.RemoveStructure (aStruct);
827 //=======================================================================
828 //function : changeZLayer
830 //=======================================================================
831 void OpenGl_View::changeZLayer (const Handle(Graphic3d_CStructure)& theStructure,
832 const Graphic3d_ZLayerId theNewLayerId)
834 const Graphic3d_ZLayerId anOldLayer = theStructure->ZLayer();
835 const OpenGl_Structure* aStruct = reinterpret_cast<const OpenGl_Structure*> (theStructure.operator->());
836 myZLayers.ChangeLayer (aStruct, anOldLayer, theNewLayerId);
838 Update (theNewLayerId);
841 //=======================================================================
842 //function : changePriority
844 //=======================================================================
845 void OpenGl_View::changePriority (const Handle(Graphic3d_CStructure)& theStructure,
846 const Standard_Integer theNewPriority)
848 const Graphic3d_ZLayerId aLayerId = theStructure->ZLayer();
849 const OpenGl_Structure* aStruct = reinterpret_cast<const OpenGl_Structure*> (theStructure.operator->());
850 myZLayers.ChangePriority (aStruct, aLayerId, theNewPriority);
853 //=======================================================================
854 //function : DiagnosticInformation
856 //=======================================================================
857 void OpenGl_View::DiagnosticInformation (TColStd_IndexedDataMapOfStringString& theDict,
858 Graphic3d_DiagnosticInfo theFlags) const
860 Handle(OpenGl_Context) aCtx = myWorkspace->GetGlContext();
861 if (!myWorkspace->Activate()
867 aCtx->DiagnosticInformation (theDict, theFlags);
868 if ((theFlags & Graphic3d_DiagnosticInfo_FrameBuffer) != 0)
870 TCollection_AsciiString aResRatio (myRenderParams.ResolutionRatio());
871 theDict.ChangeFromIndex (theDict.Add ("ResolutionRatio", aResRatio)) = aResRatio;
875 //=======================================================================
876 //function : StatisticInformation
878 //=======================================================================
879 void OpenGl_View::StatisticInformation (TColStd_IndexedDataMapOfStringString& theDict) const
881 if (const Handle(OpenGl_Context)& aCtx = myWorkspace->GetGlContext())
883 const Handle(OpenGl_FrameStats)& aStats = aCtx->FrameStats();
884 const Graphic3d_RenderingParams& aRendParams = myWorkspace->View()->RenderingParams();
885 aStats->FormatStats (theDict, aRendParams.CollectedStats);
889 //=======================================================================
890 //function : StatisticInformation
892 //=======================================================================
893 TCollection_AsciiString OpenGl_View::StatisticInformation() const
895 if (const Handle(OpenGl_Context)& aCtx = myWorkspace->GetGlContext())
897 const Handle(OpenGl_FrameStats)& aStats = aCtx->FrameStats();
898 const Graphic3d_RenderingParams& aRendParams = myWorkspace->View()->RenderingParams();
899 return aStats->FormatStats (aRendParams.CollectedStats);
901 return TCollection_AsciiString();