0031687: Draw Harness, ViewerTest - extend command vrenderparams with option updating...
[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),
ba00aab7 60 mySRgbState (-1),
61 myFboColorFormat (GL_SRGB8_ALPHA8), // note that GL_SRGB8 is not required to be renderable, unlike GL_RGB8, GL_RGBA8, GL_SRGB8_ALPHA8
3c4b62a4 62 myFboDepthFormat (GL_DEPTH24_STENCIL8),
63 myToFlipOutput (Standard_False),
b40cdc2b 64 //
c357e426 65 myFrameCounter (0),
66 myHasFboBlit (Standard_True),
a1073ae2 67 myToDisableOIT (Standard_False),
68 myToDisableOITMSAA (Standard_False),
7ccf8676 69 myToDisableMSAA (Standard_False),
c357e426 70 myTransientDrawToFront (Standard_True),
71 myBackBufferRestored (Standard_False),
72 myIsImmediateDrawn (Standard_False),
bf5f0ca2 73 myTextureParams (new OpenGl_Aspects()),
077a220c 74 myCubeMapParams (new OpenGl_Aspects()),
75 myBackgroundType (Graphic3d_TOB_NONE),
67312b79 76 myPBREnvState (OpenGl_PBREnvState_NONEXISTENT),
77 myPBREnvRequest (OpenGl_PBREnvRequest_NONE),
91c60b57 78 // ray-tracing fields initialization
c357e426 79 myRaytraceInitStatus (OpenGl_RT_NONE),
80 myIsRaytraceDataValid (Standard_False),
91c60b57 81 myIsRaytraceWarnTextures (Standard_False),
f5b72419 82 myRaytraceBVHBuilder (new BVH_BinnedBuilder<Standard_ShortReal, 3, BVH_Constants_NbBinsBest> (BVH_Constants_LeafNodeSizeAverage,
83 BVH_Constants_MaxTreeDepth,
84 Standard_False,
85 OSD_Parallel::NbLogicalProcessors() + 1)),
86 myRaytraceSceneRadius (0.0f),
87 myRaytraceSceneEpsilon (1.0e-6f),
91c60b57 88 myToUpdateEnvironmentMap (Standard_False),
b27ab03d 89 myRaytraceLayerListState (0),
90 myPrevCameraApertureRadius(0.f),
91 myPrevCameraFocalPlaneDist(0.f)
2166f0fa 92{
077a220c 93 for (int i = 0; i < Graphic3d_TypeOfBackground_NB; ++i)
94 {
95 myBackgrounds[i] = new OpenGl_BackgroundArray(Graphic3d_TypeOfBackground(i));
96 }
97
c357e426 98 myWorkspace = new OpenGl_Workspace (this, NULL);
2166f0fa 99
992ed6b3 100 Handle(Graphic3d_CLight) aLight = new Graphic3d_CLight (Graphic3d_TOLS_AMBIENT);
101 aLight->SetHeadlight (false);
102 aLight->SetColor (Quantity_NOC_WHITE);
8445efca 103 myLights = new Graphic3d_LightSet();
992ed6b3 104 myNoShadingLight = new Graphic3d_LightSet();
105 myNoShadingLight->Add (aLight);
016e5959 106
a1073ae2 107 myMainSceneFbos[0] = new OpenGl_FrameBuffer();
108 myMainSceneFbos[1] = new OpenGl_FrameBuffer();
109 myMainSceneFbosOit[0] = new OpenGl_FrameBuffer();
110 myMainSceneFbosOit[1] = new OpenGl_FrameBuffer();
111 myImmediateSceneFbos[0] = new OpenGl_FrameBuffer();
112 myImmediateSceneFbos[1] = new OpenGl_FrameBuffer();
113 myImmediateSceneFbosOit[0] = new OpenGl_FrameBuffer();
114 myImmediateSceneFbosOit[1] = new OpenGl_FrameBuffer();
b40cdc2b 115 myXrSceneFbo = new OpenGl_FrameBuffer();
a1073ae2 116 myOpenGlFBO = new OpenGl_FrameBuffer();
117 myOpenGlFBO2 = new OpenGl_FrameBuffer();
118 myRaytraceFBO1[0] = new OpenGl_FrameBuffer();
119 myRaytraceFBO1[1] = new OpenGl_FrameBuffer();
120 myRaytraceFBO2[0] = new OpenGl_FrameBuffer();
121 myRaytraceFBO2[1] = new OpenGl_FrameBuffer();
c357e426 122}
123
124// =======================================================================
125// function : Destructor
126// purpose :
127// =======================================================================
0b0320e7 128OpenGl_View::~OpenGl_View()
2166f0fa 129{
bf75be98 130 ReleaseGlResources (NULL); // ensure ReleaseGlResources() was called within valid context
077a220c 131 for (int i = 0; i < Graphic3d_TypeOfBackground_NB; ++i)
132 {
133 OpenGl_Element::Destroy(NULL, myBackgrounds[i]);
134 }
135
0b0320e7 136 OpenGl_Element::Destroy (NULL, myTextureParams);
077a220c 137 OpenGl_Element::Destroy (NULL, myCubeMapParams);
bf75be98 138}
139
c357e426 140// =======================================================================
ba00aab7 141// function : releaseSrgbResources
c357e426 142// purpose :
143// =======================================================================
ba00aab7 144void OpenGl_View::releaseSrgbResources (const Handle(OpenGl_Context)& theCtx)
bf75be98 145{
ba00aab7 146 myRenderParams.RebuildRayTracingShaders = true;
30f0ad28 147
bf75be98 148 if (!myTextureEnv.IsNull())
149 {
ad67e367 150 if (!theCtx.IsNull())
cc8cbabe 151 {
ad67e367 152 for (OpenGl_TextureSet::Iterator aTextureIter (myTextureEnv); aTextureIter.More(); aTextureIter.Next())
153 {
154 theCtx->DelayedRelease (aTextureIter.ChangeValue());
155 aTextureIter.ChangeValue().Nullify();
156 }
cc8cbabe 157 }
bf75be98 158 myTextureEnv.Nullify();
159 }
0b0320e7 160
161 if (myTextureParams != NULL)
162 {
ad67e367 163 myTextureParams->Release (theCtx.get());
0b0320e7 164 }
077a220c 165
166 if (myCubeMapParams != NULL)
0b0320e7 167 {
077a220c 168 myCubeMapParams->Release (theCtx.get());
0b0320e7 169 }
077a220c 170
171 for (int i = 0; i < Graphic3d_TypeOfBackground_NB; ++i)
bf75be98 172 {
077a220c 173 if (myBackgrounds[i] != NULL)
174 {
175 myBackgrounds[i]->Release (theCtx.get());
176 }
bf75be98 177 }
91c60b57 178
ad67e367 179 myMainSceneFbos[0] ->Release (theCtx.get());
180 myMainSceneFbos[1] ->Release (theCtx.get());
181 myMainSceneFbosOit[0] ->Release (theCtx.get());
182 myMainSceneFbosOit[1] ->Release (theCtx.get());
183 myImmediateSceneFbos[0] ->Release (theCtx.get());
184 myImmediateSceneFbos[1] ->Release (theCtx.get());
185 myImmediateSceneFbosOit[0]->Release (theCtx.get());
186 myImmediateSceneFbosOit[1]->Release (theCtx.get());
b40cdc2b 187 myXrSceneFbo ->Release (theCtx.get());
ad67e367 188 myOpenGlFBO ->Release (theCtx.get());
189 myOpenGlFBO2 ->Release (theCtx.get());
190 myFullScreenQuad .Release (theCtx.get());
191 myFullScreenQuadFlip .Release (theCtx.get());
c357e426 192
ba00aab7 193 // Technically we should also re-initialize all sRGB/RGB8 color textures.
194 // But for now consider this sRGB disabling/enabling to be done at application start-up
195 // and re-create dynamically only frame buffers.
196}
197
198// =======================================================================
199// function : ReleaseGlResources
200// purpose :
201// =======================================================================
202void OpenGl_View::ReleaseGlResources (const Handle(OpenGl_Context)& theCtx)
203{
204 myGraduatedTrihedron.Release (theCtx.get());
205 myFrameStatsPrs.Release (theCtx.get());
206
207 releaseSrgbResources (theCtx);
208
91c60b57 209 releaseRaytraceResources (theCtx);
67312b79 210
211 if (!myPBREnvironment.IsNull())
212 {
213 myPBREnvironment->Release (theCtx.get());
214 }
b40cdc2b 215 ReleaseXR();
bf75be98 216}
2166f0fa 217
c357e426 218// =======================================================================
219// function : Remove
220// purpose :
221// =======================================================================
222void OpenGl_View::Remove()
bf75be98 223{
c357e426 224 if (IsRemoved())
bf75be98 225 {
c357e426 226 return;
bf75be98 227 }
228
c357e426 229 myDriver->RemoveView (this);
230 myWindow.Nullify();
231
232 Graphic3d_CView::Remove();
233}
234
7c3ef2f7 235// =======================================================================
236// function : SetLocalOrigin
237// purpose :
238// =======================================================================
239void OpenGl_View::SetLocalOrigin (const gp_XYZ& theOrigin)
240{
241 myLocalOrigin = theOrigin;
242 const Handle(OpenGl_Context)& aCtx = myWorkspace->GetGlContext();
243 if (!aCtx.IsNull())
244 {
245 aCtx->ShaderManager()->SetLocalOrigin (theOrigin);
246 }
247}
248
c357e426 249// =======================================================================
250// function : SetTextureEnv
251// purpose :
252// =======================================================================
253void OpenGl_View::SetTextureEnv (const Handle(Graphic3d_TextureEnv)& theTextureEnv)
254{
255 Handle(OpenGl_Context) aCtx = myWorkspace->GetGlContext();
256 if (!aCtx.IsNull() && !myTextureEnv.IsNull())
257 {
cc8cbabe 258 for (OpenGl_TextureSet::Iterator aTextureIter (myTextureEnv); aTextureIter.More(); aTextureIter.Next())
259 {
260 aCtx->DelayedRelease (aTextureIter.ChangeValue());
261 aTextureIter.ChangeValue().Nullify();
262 }
c357e426 263 }
264
265 myToUpdateEnvironmentMap = Standard_True;
266 myTextureEnvData = theTextureEnv;
267 myTextureEnv.Nullify();
268 initTextureEnv (aCtx);
269}
270
271// =======================================================================
272// function : initTextureEnv
273// purpose :
274// =======================================================================
275void OpenGl_View::initTextureEnv (const Handle(OpenGl_Context)& theContext)
276{
277 if (myTextureEnvData.IsNull()
278 || theContext.IsNull()
279 || !theContext->MakeCurrent())
bf75be98 280 {
281 return;
282 }
283
72f6dc61 284 Handle(OpenGl_Texture) aTextureEnv = new OpenGl_Texture (myTextureEnvData->GetId(), myTextureEnvData->GetParams());
faff3767 285 if (Handle(Image_PixMap) anImage = myTextureEnvData->GetImage (theContext->SupportedTextureFormats()))
c357e426 286 {
ba00aab7 287 aTextureEnv->Init (theContext, *anImage, myTextureEnvData->Type(), true);
c357e426 288 }
72f6dc61 289 myTextureEnv = new OpenGl_TextureSet (aTextureEnv);
290 myTextureEnv->ChangeTextureSetBits() = Graphic3d_TextureSetBits_BaseColor;
c357e426 291}
e276548b 292
c357e426 293// =======================================================================
294// function : SetImmediateModeDrawToFront
295// purpose :
296// =======================================================================
297Standard_Boolean OpenGl_View::SetImmediateModeDrawToFront (const Standard_Boolean theDrawToFrontBuffer)
298{
299 const Standard_Boolean aPrevMode = myTransientDrawToFront;
300 myTransientDrawToFront = theDrawToFrontBuffer;
301 return aPrevMode;
e276548b 302}
303
c357e426 304// =======================================================================
305// function : SetWindow
306// purpose :
307// =======================================================================
308void OpenGl_View::SetWindow (const Handle(Aspect_Window)& theWindow,
a521d90d 309 const Aspect_RenderingContext theContext)
e276548b 310{
c357e426 311 myWindow = myDriver->CreateRenderWindow (theWindow, theContext);
312 Standard_ASSERT_RAISE (!myWindow.IsNull(),
313 "OpenGl_View::SetWindow, "
314 "Failed to create OpenGl window.");
e276548b 315
c357e426 316 myWorkspace = new OpenGl_Workspace (this, myWindow);
317 myWorldViewProjState.Reset();
91c60b57 318 myToUpdateEnvironmentMap = Standard_True;
c357e426 319 myHasFboBlit = Standard_True;
320 Invalidate();
321
322 // Environment texture resource does not support lazy initialization.
323 initTextureEnv (myWorkspace->GetGlContext());
324}
325
326// =======================================================================
327// function : Resized
328// purpose :
329// =======================================================================
330void OpenGl_View::Resized()
331{
332 if (myWindow.IsNull())
333 return;
334
335 myWindow->Resize();
336}
337
c357e426 338// =======================================================================
339// function : SetMinMaxValuesCallback
340// purpose :
341// =======================================================================
342static void SetMinMaxValuesCallback (Graphic3d_CView* theView)
343{
344 OpenGl_View* aView = dynamic_cast<OpenGl_View*>(theView);
345 if (aView == NULL)
346 return;
347
348 Bnd_Box aBox = theView->MinMaxValues();
349 if (!aBox.IsVoid())
2166f0fa 350 {
c357e426 351 gp_Pnt aMin = aBox.CornerMin();
352 gp_Pnt aMax = aBox.CornerMax();
353
354 Graphic3d_Vec3 aMinVec ((Standard_ShortReal )aMin.X(), (Standard_ShortReal )aMin.Y(), (Standard_ShortReal )aMin.Z());
355 Graphic3d_Vec3 aMaxVec ((Standard_ShortReal )aMax.X(), (Standard_ShortReal )aMax.Y(), (Standard_ShortReal )aMax.Z());
356 aView->GraduatedTrihedronMinMaxValues (aMinVec, aMaxVec);
2166f0fa
SK
357 }
358}
359
c357e426 360// =======================================================================
361// function : GraduatedTrihedronDisplay
362// purpose :
363// =======================================================================
364void OpenGl_View::GraduatedTrihedronDisplay (const Graphic3d_GraduatedTrihedron& theTrihedronData)
365{
366 myGTrihedronData = theTrihedronData;
367 myGTrihedronData.PtrView = this;
368 myGTrihedronData.CubicAxesCallback = SetMinMaxValuesCallback;
369 myGraduatedTrihedron.SetValues (myGTrihedronData);
370 myToShowGradTrihedron = true;
371}
2166f0fa 372
c357e426 373// =======================================================================
374// function : GraduatedTrihedronErase
375// purpose :
376// =======================================================================
377void OpenGl_View::GraduatedTrihedronErase()
2166f0fa 378{
c357e426 379 myGTrihedronData.PtrView = NULL;
380 myGraduatedTrihedron.Release (myWorkspace->GetGlContext().operator->());
381 myToShowGradTrihedron = false;
2166f0fa
SK
382}
383
c357e426 384// =======================================================================
385// function : GraduatedTrihedronMinMaxValues
386// purpose :
387// =======================================================================
388void OpenGl_View::GraduatedTrihedronMinMaxValues (const Graphic3d_Vec3 theMin, const Graphic3d_Vec3 theMax)
389{
390 myGraduatedTrihedron.SetMinMax (theMin, theMax);
391}
2166f0fa 392
c357e426 393// =======================================================================
394// function : BufferDump
395// purpose :
396// =======================================================================
397Standard_Boolean OpenGl_View::BufferDump (Image_PixMap& theImage, const Graphic3d_BufferType& theBufferType)
398{
38d90bb3 399 if (theBufferType != Graphic3d_BT_RGB_RayTraceHdrLeft)
400 {
401 return myWorkspace->BufferDump(myFBO, theImage, theBufferType);
402 }
403
404 if (!myRaytraceParameters.AdaptiveScreenSampling)
405 {
406 return myWorkspace->BufferDump(myAccumFrames % 2 ? myRaytraceFBO2[0] : myRaytraceFBO1[0], theImage, theBufferType);
407 }
408
409#if defined(GL_ES_VERSION_2_0)
410 return false;
411#else
412 if (theImage.Format() != Image_Format_RGBF)
413 {
414 return false;
415 }
416
417 const GLuint aW = myRaytraceOutputTexture[0]->SizeX();
418 const GLuint aH = myRaytraceOutputTexture[0]->SizeY();
419 if (aW / 3 != theImage.SizeX() || aH / 2 != theImage.SizeY())
420 {
421 return false;
422 }
423
424 std::vector<GLfloat> aValues;
425 try
426 {
427 aValues.resize (aW * aH);
428 }
429 catch (const std::bad_alloc&)
430 {
431 return false;
432 }
433
434 glBindTexture (GL_TEXTURE_RECTANGLE, myRaytraceOutputTexture[0]->TextureId());
435 glGetTexImage (GL_TEXTURE_RECTANGLE, 0, OpenGl_TextureFormat::Create<GLfloat, 1>().Format(), GL_FLOAT, &aValues[0]);
436 glBindTexture (GL_TEXTURE_RECTANGLE, 0);
437 for (unsigned int aRow = 0; aRow < aH; aRow += 2)
438 {
439 for (unsigned int aCol = 0; aCol < aW; aCol += 3)
440 {
441 float* anImageValue = theImage.ChangeValue<float[3]> ((aH - aRow) / 2 - 1, aCol / 3);
442 float aInvNbSamples = 1.f / aValues[aRow * aW + aCol + aW];
443 anImageValue[0] = aValues[aRow * aW + aCol] * aInvNbSamples;
444 anImageValue[1] = aValues[aRow * aW + aCol + 1] * aInvNbSamples;
445 anImageValue[2] = aValues[aRow * aW + aCol + 1 + aW] * aInvNbSamples;
446 }
447 }
448
449 return true;
450#endif
c357e426 451}
452
c357e426 453// =======================================================================
454// function : GradientBackground
455// purpose :
456// =======================================================================
457Aspect_GradientBackground OpenGl_View::GradientBackground() const
458{
459 Quantity_Color aColor1, aColor2;
077a220c 460 aColor1.SetValues (myBackgrounds[Graphic3d_TOB_GRADIENT]->GradientColor (0).r(),
461 myBackgrounds[Graphic3d_TOB_GRADIENT]->GradientColor (0).g(),
462 myBackgrounds[Graphic3d_TOB_GRADIENT]->GradientColor (0).b(), Quantity_TOC_RGB);
463 aColor2.SetValues (myBackgrounds[Graphic3d_TOB_GRADIENT]->GradientColor (1).r(),
464 myBackgrounds[Graphic3d_TOB_GRADIENT]->GradientColor (1).g(),
465 myBackgrounds[Graphic3d_TOB_GRADIENT]->GradientColor (1).b(), Quantity_TOC_RGB);
466 return Aspect_GradientBackground (aColor1, aColor2, myBackgrounds[Graphic3d_TOB_GRADIENT]->GradientFillMethod());
c357e426 467}
2166f0fa 468
c357e426 469// =======================================================================
470// function : SetGradientBackground
471// purpose :
472// =======================================================================
473void OpenGl_View::SetGradientBackground (const Aspect_GradientBackground& theBackground)
2166f0fa 474{
c357e426 475 Quantity_Color aColor1, aColor2;
476 theBackground.Colors (aColor1, aColor2);
077a220c 477 myBackgrounds[Graphic3d_TOB_GRADIENT]->SetGradientParameters (aColor1, aColor2, theBackground.BgGradientFillMethod());
478
479 myBackgroundType = Graphic3d_TOB_GRADIENT;
c357e426 480}
481
482// =======================================================================
483// function : SetBackgroundImage
484// purpose :
485// =======================================================================
486void OpenGl_View::SetBackgroundImage (const TCollection_AsciiString& theFilePath)
487{
488 // Prepare aspect for texture storage
489 myBackgroundImagePath = theFilePath;
490 Handle(Graphic3d_AspectFillArea3d) anAspect = new Graphic3d_AspectFillArea3d();
491 Handle(Graphic3d_Texture2Dmanual) aTextureMap = new Graphic3d_Texture2Dmanual (TCollection_AsciiString (theFilePath));
492 aTextureMap->EnableRepeat();
493 aTextureMap->DisableModulate();
494 aTextureMap->GetParams()->SetGenMode (Graphic3d_TOTM_MANUAL,
495 Graphic3d_Vec4 (0.0f, 0.0f, 0.0f, 0.0f),
496 Graphic3d_Vec4 (0.0f, 0.0f, 0.0f, 0.0f));
497 anAspect->SetTextureMap (aTextureMap);
498 anAspect->SetInteriorStyle (Aspect_IS_SOLID);
b6472664 499 anAspect->SetSuppressBackFaces (false);
c357e426 500 // Enable texture mapping
501 if (aTextureMap->IsDone())
2166f0fa 502 {
c357e426 503 anAspect->SetTextureMapOn();
2166f0fa
SK
504 }
505 else
506 {
c357e426 507 anAspect->SetTextureMapOff();
508 return;
509 }
2166f0fa 510
c357e426 511 // Set texture parameters
512 myTextureParams->SetAspect (anAspect);
077a220c 513
514 myBackgroundType = Graphic3d_TOB_TEXTURE;
c357e426 515}
2166f0fa 516
c357e426 517// =======================================================================
518// function : BackgroundImageStyle
519// purpose :
520// =======================================================================
521Aspect_FillMethod OpenGl_View::BackgroundImageStyle() const
522{
077a220c 523 return myBackgrounds[Graphic3d_TOB_TEXTURE]->TextureFillMethod();
2166f0fa
SK
524}
525
c357e426 526// =======================================================================
527// function : SetBackgroundImageStyle
528// purpose :
529// =======================================================================
530void OpenGl_View::SetBackgroundImageStyle (const Aspect_FillMethod theFillStyle)
531{
077a220c 532 myBackgrounds[Graphic3d_TOB_TEXTURE]->SetTextureFillMethod (theFillStyle);
533}
534
535// =======================================================================
536// function : BackgroundCubeMap
537// purpose :
538// =======================================================================
539Handle(Graphic3d_CubeMap) OpenGl_View::BackgroundCubeMap() const
540{
541 return myBackgroundCubeMap;
542}
67312b79 543
544// =======================================================================
545// function : SpecIBLMapLevels
546// purpose :
547// =======================================================================
548unsigned int OpenGl_View::SpecIBLMapLevels() const
549{
550 return myPBREnvironment.IsNull() ? 0 : myPBREnvironment->SpecMapLevelsNumber();
551}
077a220c 552
553// =======================================================================
554// function : SetBackgroundCubeMap
555// purpose :
556// =======================================================================
67312b79 557void OpenGl_View::SetBackgroundCubeMap (const Handle(Graphic3d_CubeMap)& theCubeMap,
558 Standard_Boolean theToUpdatePBREnv)
077a220c 559{
560 myBackgroundCubeMap = theCubeMap;
67312b79 561 if (theCubeMap.IsNull())
562 {
563 if (theToUpdatePBREnv)
564 {
565 myPBREnvRequest = OpenGl_PBREnvRequest_CLEAR;
566 }
567 if (myBackgroundType == Graphic3d_TOB_CUBEMAP)
568 {
569 myBackgroundType = Graphic3d_TOB_NONE;
570 }
571 return;
572 }
077a220c 573
67312b79 574 theCubeMap ->SetMipmapsGeneration (Standard_True);
575 Handle(Graphic3d_AspectFillArea3d) anAspect = new Graphic3d_AspectFillArea3d();
576 Handle(Graphic3d_TextureSet) aTextureSet = new Graphic3d_TextureSet (myBackgroundCubeMap);
577 anAspect->SetInteriorStyle (Aspect_IS_SOLID);
578 anAspect->SetSuppressBackFaces (false);
579 anAspect->SetTextureSet (aTextureSet);
077a220c 580
581 const Handle(OpenGl_Context)& aCtx = myWorkspace->GetGlContext();
582 if (!aCtx.IsNull())
583 {
584 anAspect->SetShaderProgram (aCtx->ShaderManager()->GetBgCubeMapProgram());
585 }
67312b79 586 anAspect->SetTextureMapOn (theCubeMap->IsDone());
587 myCubeMapParams->SetAspect (anAspect);
077a220c 588
67312b79 589 if (theToUpdatePBREnv)
077a220c 590 {
67312b79 591 myPBREnvRequest = OpenGl_PBREnvRequest_BAKE;
077a220c 592 }
077a220c 593 const OpenGl_Aspects* anAspectsBackup = myWorkspace->SetAspects (myCubeMapParams);
594 myWorkspace->ApplyAspects();
595 myWorkspace->SetAspects (anAspectsBackup);
596 myWorkspace->ApplyAspects();
597
598 myBackgroundType = Graphic3d_TOB_CUBEMAP;
c357e426 599}
2166f0fa 600
c357e426 601//=======================================================================
1c728f2d 602//function : InsertLayerBefore
c357e426 603//purpose :
604//=======================================================================
1c728f2d 605void OpenGl_View::InsertLayerBefore (const Graphic3d_ZLayerId theLayerId,
077a220c 606 const Graphic3d_ZLayerSettings& theSettings,
607 const Graphic3d_ZLayerId theLayerAfter)
2166f0fa 608{
1c728f2d 609 myZLayers.InsertLayerBefore (theLayerId, theSettings, theLayerAfter);
610}
611
612//=======================================================================
613//function : InsertLayerAfter
614//purpose :
615//=======================================================================
616void OpenGl_View::InsertLayerAfter (const Graphic3d_ZLayerId theLayerId,
077a220c 617 const Graphic3d_ZLayerSettings& theSettings,
618 const Graphic3d_ZLayerId theLayerBefore)
1c728f2d 619{
620 myZLayers.InsertLayerAfter (theLayerId, theSettings, theLayerBefore);
2166f0fa
SK
621}
622
c357e426 623//=======================================================================
624//function : RemoveZLayer
625//purpose :
626//=======================================================================
627void OpenGl_View::RemoveZLayer (const Graphic3d_ZLayerId theLayerId)
628{
629 myZLayers.RemoveLayer (theLayerId);
630}
2166f0fa 631
c357e426 632//=======================================================================
633//function : SetZLayerSettings
634//purpose :
635//=======================================================================
636void OpenGl_View::SetZLayerSettings (const Graphic3d_ZLayerId theLayerId,
637 const Graphic3d_ZLayerSettings& theSettings)
2166f0fa 638{
c357e426 639 myZLayers.SetLayerSettings (theLayerId, theSettings);
2166f0fa
SK
640}
641
50d06d8f 642//=======================================================================
643//function : ZLayerMax
644//purpose :
645//=======================================================================
646Standard_Integer OpenGl_View::ZLayerMax() const
647{
648 Standard_Integer aLayerMax = Graphic3d_ZLayerId_Default;
1c728f2d 649 for (NCollection_List<Handle(Graphic3d_Layer)>::Iterator aLayerIter (myZLayers.Layers()); aLayerIter.More(); aLayerIter.Next())
50d06d8f 650 {
1c728f2d 651 aLayerMax = Max (aLayerMax, aLayerIter.Value()->LayerId());
50d06d8f 652 }
50d06d8f 653 return aLayerMax;
654}
655
656//=======================================================================
1c728f2d 657//function : Layers
658//purpose :
659//=======================================================================
660const NCollection_List<Handle(Graphic3d_Layer)>& OpenGl_View::Layers() const
661{
662 return myZLayers.Layers();
663}
664
665//=======================================================================
666//function : Layer
50d06d8f 667//purpose :
668//=======================================================================
1c728f2d 669Handle(Graphic3d_Layer) OpenGl_View::Layer (const Graphic3d_ZLayerId theLayerId) const
50d06d8f 670{
1c728f2d 671 Handle(Graphic3d_Layer) aLayer;
672 if (theLayerId != Graphic3d_ZLayerId_UNKNOWN)
50d06d8f 673 {
1c728f2d 674 myZLayers.LayerIDs().Find (theLayerId, aLayer);
50d06d8f 675 }
1c728f2d 676 return aLayer;
50d06d8f 677}
678
679//=======================================================================
1c728f2d 680//function : MinMaxValues
50d06d8f 681//purpose :
682//=======================================================================
1c728f2d 683Bnd_Box OpenGl_View::MinMaxValues (const Standard_Boolean theToIncludeAuxiliary) const
50d06d8f 684{
1c728f2d 685 if (!IsDefined())
50d06d8f 686 {
1c728f2d 687 return Bnd_Box();
50d06d8f 688 }
689
1c728f2d 690 Bnd_Box aBox = base_type::MinMaxValues (theToIncludeAuxiliary);
691
71d8ccc7 692 return aBox;
50d06d8f 693}
694
695//=======================================================================
c357e426 696//function : FBO
697//purpose :
698//=======================================================================
b128c892 699Handle(Standard_Transient) OpenGl_View::FBO() const
c357e426 700{
b128c892 701 return Handle(Standard_Transient)(myFBO);
c357e426 702}
2166f0fa 703
c357e426 704//=======================================================================
705//function : SetFBO
706//purpose :
707//=======================================================================
b128c892 708void OpenGl_View::SetFBO (const Handle(Standard_Transient)& theFbo)
2166f0fa 709{
b128c892 710 myFBO = Handle(OpenGl_FrameBuffer)::DownCast (theFbo);
2166f0fa
SK
711}
712
c357e426 713//=======================================================================
714//function : FBOCreate
715//purpose :
716//=======================================================================
b128c892 717Handle(Standard_Transient) OpenGl_View::FBOCreate (const Standard_Integer theWidth,
718 const Standard_Integer theHeight)
c357e426 719{
720 return myWorkspace->FBOCreate (theWidth, theHeight);
721}
2166f0fa 722
c357e426 723//=======================================================================
724//function : FBORelease
725//purpose :
726//=======================================================================
b128c892 727void OpenGl_View::FBORelease (Handle(Standard_Transient)& theFbo)
2166f0fa 728{
b128c892 729 Handle(OpenGl_FrameBuffer) aFrameBuffer = Handle(OpenGl_FrameBuffer)::DownCast (theFbo);
730 if (aFrameBuffer.IsNull())
731 {
732 return;
733 }
734
735 myWorkspace->FBORelease (aFrameBuffer);
736 theFbo.Nullify();
c357e426 737}
738
739//=======================================================================
740//function : FBOGetDimensions
741//purpose :
742//=======================================================================
b128c892 743void OpenGl_View::FBOGetDimensions (const Handle(Standard_Transient)& theFbo,
c357e426 744 Standard_Integer& theWidth,
745 Standard_Integer& theHeight,
746 Standard_Integer& theWidthMax,
747 Standard_Integer& theHeightMax)
748{
b128c892 749 const Handle(OpenGl_FrameBuffer) aFrameBuffer = Handle(OpenGl_FrameBuffer)::DownCast (theFbo);
750 if (aFrameBuffer.IsNull())
751 {
752 return;
753 }
754
c357e426 755 theWidth = aFrameBuffer->GetVPSizeX(); // current viewport size
756 theHeight = aFrameBuffer->GetVPSizeY();
757 theWidthMax = aFrameBuffer->GetSizeX(); // texture size
758 theHeightMax = aFrameBuffer->GetSizeY();
759}
760
761//=======================================================================
762//function : FBOChangeViewport
763//purpose :
764//=======================================================================
b128c892 765void OpenGl_View::FBOChangeViewport (const Handle(Standard_Transient)& theFbo,
c357e426 766 const Standard_Integer theWidth,
767 const Standard_Integer theHeight)
768{
b128c892 769 const Handle(OpenGl_FrameBuffer) aFrameBuffer = Handle(OpenGl_FrameBuffer)::DownCast (theFbo);
770 if (aFrameBuffer.IsNull())
771 {
772 return;
773 }
774
c357e426 775 aFrameBuffer->ChangeViewport (theWidth, theHeight);
776}
777
c357e426 778//=======================================================================
779//function : displayStructure
780//purpose :
781//=======================================================================
782void OpenGl_View::displayStructure (const Handle(Graphic3d_CStructure)& theStructure,
783 const Standard_Integer thePriority)
784{
785 const OpenGl_Structure* aStruct = reinterpret_cast<const OpenGl_Structure*> (theStructure.operator->());
786 const Graphic3d_ZLayerId aZLayer = aStruct->ZLayer();
787 myZLayers.AddStructure (aStruct, aZLayer, thePriority);
788}
789
790//=======================================================================
791//function : eraseStructure
792//purpose :
793//=======================================================================
794void OpenGl_View::eraseStructure (const Handle(Graphic3d_CStructure)& theStructure)
795{
796 const OpenGl_Structure* aStruct = reinterpret_cast<const OpenGl_Structure*> (theStructure.operator->());
797 myZLayers.RemoveStructure (aStruct);
798}
799
800//=======================================================================
801//function : changeZLayer
802//purpose :
803//=======================================================================
804void OpenGl_View::changeZLayer (const Handle(Graphic3d_CStructure)& theStructure,
805 const Graphic3d_ZLayerId theNewLayerId)
806{
807 const Graphic3d_ZLayerId anOldLayer = theStructure->ZLayer();
808 const OpenGl_Structure* aStruct = reinterpret_cast<const OpenGl_Structure*> (theStructure.operator->());
809 myZLayers.ChangeLayer (aStruct, anOldLayer, theNewLayerId);
cfece3ef 810 Update (anOldLayer);
811 Update (theNewLayerId);
c357e426 812}
813
814//=======================================================================
815//function : changePriority
816//purpose :
817//=======================================================================
818void OpenGl_View::changePriority (const Handle(Graphic3d_CStructure)& theStructure,
819 const Standard_Integer theNewPriority)
820{
821 const Graphic3d_ZLayerId aLayerId = theStructure->ZLayer();
822 const OpenGl_Structure* aStruct = reinterpret_cast<const OpenGl_Structure*> (theStructure.operator->());
823 myZLayers.ChangePriority (aStruct, aLayerId, theNewPriority);
2166f0fa 824}
26d9c835 825
826//=======================================================================
827//function : DiagnosticInformation
828//purpose :
829//=======================================================================
830void OpenGl_View::DiagnosticInformation (TColStd_IndexedDataMapOfStringString& theDict,
831 Graphic3d_DiagnosticInfo theFlags) const
832{
b40cdc2b 833 base_type::DiagnosticInformation (theDict, theFlags);
26d9c835 834 Handle(OpenGl_Context) aCtx = myWorkspace->GetGlContext();
835 if (!myWorkspace->Activate()
836 || aCtx.IsNull())
837 {
838 return;
839 }
840
841 aCtx->DiagnosticInformation (theDict, theFlags);
842 if ((theFlags & Graphic3d_DiagnosticInfo_FrameBuffer) != 0)
843 {
844 TCollection_AsciiString aResRatio (myRenderParams.ResolutionRatio());
845 theDict.ChangeFromIndex (theDict.Add ("ResolutionRatio", aResRatio)) = aResRatio;
846 }
847}
79b544e6 848
849//=======================================================================
850//function : StatisticInformation
851//purpose :
852//=======================================================================
853void OpenGl_View::StatisticInformation (TColStd_IndexedDataMapOfStringString& theDict) const
854{
855 if (const Handle(OpenGl_Context)& aCtx = myWorkspace->GetGlContext())
856 {
857 const Handle(OpenGl_FrameStats)& aStats = aCtx->FrameStats();
858 const Graphic3d_RenderingParams& aRendParams = myWorkspace->View()->RenderingParams();
859 aStats->FormatStats (theDict, aRendParams.CollectedStats);
860 }
861}
862
863//=======================================================================
864//function : StatisticInformation
865//purpose :
866//=======================================================================
867TCollection_AsciiString OpenGl_View::StatisticInformation() const
868{
869 if (const Handle(OpenGl_Context)& aCtx = myWorkspace->GetGlContext())
870 {
871 const Handle(OpenGl_FrameStats)& aStats = aCtx->FrameStats();
872 const Graphic3d_RenderingParams& aRendParams = myWorkspace->View()->RenderingParams();
873 return aStats->FormatStats (aRendParams.CollectedStats);
874 }
875 return TCollection_AsciiString();
876}