0029353: Optimization of TPrsStd_AISPresentation::SetSelectionMode()
[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#ifdef HAVE_GL2PS
42#include <gl2ps.h>
43#endif
2166f0fa 44
c357e426 45// =======================================================================
46// function : Constructor
47// purpose :
48// =======================================================================
49OpenGl_View::OpenGl_View (const Handle(Graphic3d_StructureManager)& theMgr,
50 const Handle(OpenGl_GraphicDriver)& theDriver,
51 const Handle(OpenGl_Caps)& theCaps,
c357e426 52 OpenGl_StateCounter* theCounter)
53: Graphic3d_CView (theMgr),
54 myDriver (theDriver.operator->()),
55 myCaps (theCaps),
c357e426 56 myWasRedrawnGL (Standard_False),
c357e426 57 myCulling (Standard_True),
58 myShadingModel (Graphic3d_TOSM_FACET),
c357e426 59 myBackfacing (Graphic3d_TOBM_AUTOMATIC),
b6472664 60 myBgColor (Quantity_NOC_BLACK),
c357e426 61 myCamera (new Graphic3d_Camera()),
c357e426 62 myToShowGradTrihedron (false),
f5b72419 63 myZLayers (Structure_MAX_PRIORITY - Structure_MIN_PRIORITY + 1),
c357e426 64 myStateCounter (theCounter),
992ed6b3 65 myCurrLightSourceState (theCounter->Increment()),
66 myLightsRevision (0),
0b0320e7 67 myLastLightSourceState (0, 0),
a1073ae2 68#if !defined(GL_ES_VERSION_2_0)
3c4b62a4 69 myFboColorFormat (GL_RGBA8),
a1073ae2 70#else
71 myFboColorFormat (GL_RGBA),
72#endif
3c4b62a4 73 myFboDepthFormat (GL_DEPTH24_STENCIL8),
74 myToFlipOutput (Standard_False),
c357e426 75 myFrameCounter (0),
76 myHasFboBlit (Standard_True),
a1073ae2 77 myToDisableOIT (Standard_False),
78 myToDisableOITMSAA (Standard_False),
7ccf8676 79 myToDisableMSAA (Standard_False),
c357e426 80 myTransientDrawToFront (Standard_True),
81 myBackBufferRestored (Standard_False),
82 myIsImmediateDrawn (Standard_False),
0b0320e7 83 myTextureParams (new OpenGl_AspectFace()),
84 myBgGradientArray (new OpenGl_BackgroundArray (Graphic3d_TOB_GRADIENT)),
91c60b57 85 myBgTextureArray (new OpenGl_BackgroundArray (Graphic3d_TOB_TEXTURE)),
86 // ray-tracing fields initialization
c357e426 87 myRaytraceInitStatus (OpenGl_RT_NONE),
88 myIsRaytraceDataValid (Standard_False),
91c60b57 89 myIsRaytraceWarnTextures (Standard_False),
f5b72419 90 myRaytraceBVHBuilder (new BVH_BinnedBuilder<Standard_ShortReal, 3, BVH_Constants_NbBinsBest> (BVH_Constants_LeafNodeSizeAverage,
91 BVH_Constants_MaxTreeDepth,
92 Standard_False,
93 OSD_Parallel::NbLogicalProcessors() + 1)),
94 myRaytraceSceneRadius (0.0f),
95 myRaytraceSceneEpsilon (1.0e-6f),
91c60b57 96 myToUpdateEnvironmentMap (Standard_False),
b27ab03d 97 myRaytraceLayerListState (0),
98 myPrevCameraApertureRadius(0.f),
99 myPrevCameraFocalPlaneDist(0.f)
2166f0fa 100{
c357e426 101 myWorkspace = new OpenGl_Workspace (this, NULL);
2166f0fa 102
992ed6b3 103 Handle(Graphic3d_CLight) aLight = new Graphic3d_CLight (Graphic3d_TOLS_AMBIENT);
104 aLight->SetHeadlight (false);
105 aLight->SetColor (Quantity_NOC_WHITE);
106 myNoShadingLight = new Graphic3d_LightSet();
107 myNoShadingLight->Add (aLight);
016e5959 108
a1073ae2 109 myMainSceneFbos[0] = new OpenGl_FrameBuffer();
110 myMainSceneFbos[1] = new OpenGl_FrameBuffer();
111 myMainSceneFbosOit[0] = new OpenGl_FrameBuffer();
112 myMainSceneFbosOit[1] = new OpenGl_FrameBuffer();
113 myImmediateSceneFbos[0] = new OpenGl_FrameBuffer();
114 myImmediateSceneFbos[1] = new OpenGl_FrameBuffer();
115 myImmediateSceneFbosOit[0] = new OpenGl_FrameBuffer();
116 myImmediateSceneFbosOit[1] = new OpenGl_FrameBuffer();
117 myOpenGlFBO = new OpenGl_FrameBuffer();
118 myOpenGlFBO2 = new OpenGl_FrameBuffer();
119 myRaytraceFBO1[0] = new OpenGl_FrameBuffer();
120 myRaytraceFBO1[1] = new OpenGl_FrameBuffer();
121 myRaytraceFBO2[0] = new OpenGl_FrameBuffer();
122 myRaytraceFBO2[1] = new OpenGl_FrameBuffer();
c357e426 123}
124
125// =======================================================================
126// function : Destructor
127// purpose :
128// =======================================================================
0b0320e7 129OpenGl_View::~OpenGl_View()
2166f0fa 130{
bf75be98 131 ReleaseGlResources (NULL); // ensure ReleaseGlResources() was called within valid context
0b0320e7 132 OpenGl_Element::Destroy (NULL, myBgGradientArray);
133 OpenGl_Element::Destroy (NULL, myBgTextureArray);
134 OpenGl_Element::Destroy (NULL, myTextureParams);
bf75be98 135}
136
c357e426 137// =======================================================================
138// function : ReleaseGlResources
139// purpose :
140// =======================================================================
bf75be98 141void OpenGl_View::ReleaseGlResources (const Handle(OpenGl_Context)& theCtx)
142{
536d98e2 143 myGraduatedTrihedron.Release (theCtx.operator->());
30f0ad28 144
bf75be98 145 if (!myTextureEnv.IsNull())
146 {
cc8cbabe 147 for (OpenGl_TextureSet::Iterator aTextureIter (myTextureEnv); aTextureIter.More(); aTextureIter.Next())
148 {
149 theCtx->DelayedRelease (aTextureIter.ChangeValue());
150 aTextureIter.ChangeValue().Nullify();
151 }
bf75be98 152 myTextureEnv.Nullify();
153 }
0b0320e7 154
155 if (myTextureParams != NULL)
156 {
157 myTextureParams->Release (theCtx.operator->());
158 }
159 if (myBgGradientArray != NULL)
160 {
161 myBgGradientArray->Release (theCtx.operator->());
162 }
163 if (myBgTextureArray != NULL)
bf75be98 164 {
0b0320e7 165 myBgTextureArray->Release (theCtx.operator->());
bf75be98 166 }
91c60b57 167
a1073ae2 168 myMainSceneFbos[0] ->Release (theCtx.operator->());
169 myMainSceneFbos[1] ->Release (theCtx.operator->());
170 myMainSceneFbosOit[0] ->Release (theCtx.operator->());
171 myMainSceneFbosOit[1] ->Release (theCtx.operator->());
172 myImmediateSceneFbos[0] ->Release (theCtx.operator->());
173 myImmediateSceneFbos[1] ->Release (theCtx.operator->());
174 myImmediateSceneFbosOit[0]->Release (theCtx.operator->());
175 myImmediateSceneFbosOit[1]->Release (theCtx.operator->());
176 myOpenGlFBO ->Release (theCtx.operator->());
177 myOpenGlFBO2 ->Release (theCtx.operator->());
178 myFullScreenQuad .Release (theCtx.operator->());
179 myFullScreenQuadFlip .Release (theCtx.operator->());
c357e426 180
91c60b57 181 releaseRaytraceResources (theCtx);
bf75be98 182}
2166f0fa 183
c357e426 184// =======================================================================
185// function : Remove
186// purpose :
187// =======================================================================
188void OpenGl_View::Remove()
bf75be98 189{
c357e426 190 if (IsRemoved())
bf75be98 191 {
c357e426 192 return;
bf75be98 193 }
194
c357e426 195 myDriver->RemoveView (this);
196 myWindow.Nullify();
197
198 Graphic3d_CView::Remove();
199}
200
201// =======================================================================
202// function : SetTextureEnv
203// purpose :
204// =======================================================================
7c3ef2f7 205void OpenGl_View::SetCamera(const Handle(Graphic3d_Camera)& theCamera)
206{
207 myCamera = theCamera;
208}
209
210// =======================================================================
211// function : SetLocalOrigin
212// purpose :
213// =======================================================================
214void OpenGl_View::SetLocalOrigin (const gp_XYZ& theOrigin)
215{
216 myLocalOrigin = theOrigin;
217 const Handle(OpenGl_Context)& aCtx = myWorkspace->GetGlContext();
218 if (!aCtx.IsNull())
219 {
220 aCtx->ShaderManager()->SetLocalOrigin (theOrigin);
221 }
222}
223
224// =======================================================================
225// function : SetTextureEnv
226// purpose :
227// =======================================================================
c357e426 228void OpenGl_View::SetTextureEnv (const Handle(Graphic3d_TextureEnv)& theTextureEnv)
229{
230 Handle(OpenGl_Context) aCtx = myWorkspace->GetGlContext();
231 if (!aCtx.IsNull() && !myTextureEnv.IsNull())
232 {
cc8cbabe 233 for (OpenGl_TextureSet::Iterator aTextureIter (myTextureEnv); aTextureIter.More(); aTextureIter.Next())
234 {
235 aCtx->DelayedRelease (aTextureIter.ChangeValue());
236 aTextureIter.ChangeValue().Nullify();
237 }
c357e426 238 }
239
240 myToUpdateEnvironmentMap = Standard_True;
241 myTextureEnvData = theTextureEnv;
242 myTextureEnv.Nullify();
243 initTextureEnv (aCtx);
244}
245
246// =======================================================================
247// function : initTextureEnv
248// purpose :
249// =======================================================================
250void OpenGl_View::initTextureEnv (const Handle(OpenGl_Context)& theContext)
251{
252 if (myTextureEnvData.IsNull()
253 || theContext.IsNull()
254 || !theContext->MakeCurrent())
bf75be98 255 {
256 return;
257 }
258
cc8cbabe 259 myTextureEnv = new OpenGl_TextureSet (1);
260 Handle(OpenGl_Texture)& aTextureEnv = myTextureEnv->ChangeFirst();
261 aTextureEnv = new OpenGl_Texture (myTextureEnvData->GetId(), myTextureEnvData->GetParams());
c357e426 262 Handle(Image_PixMap) anImage = myTextureEnvData->GetImage();
e276548b 263 if (!anImage.IsNull())
c357e426 264 {
cc8cbabe 265 aTextureEnv->Init (theContext, *anImage.operator->(), myTextureEnvData->Type());
c357e426 266 }
267}
e276548b 268
c357e426 269// =======================================================================
270// function : SetImmediateModeDrawToFront
271// purpose :
272// =======================================================================
273Standard_Boolean OpenGl_View::SetImmediateModeDrawToFront (const Standard_Boolean theDrawToFrontBuffer)
274{
275 const Standard_Boolean aPrevMode = myTransientDrawToFront;
276 myTransientDrawToFront = theDrawToFrontBuffer;
277 return aPrevMode;
e276548b 278}
279
c357e426 280// =======================================================================
281// function : SetWindow
282// purpose :
283// =======================================================================
284void OpenGl_View::SetWindow (const Handle(Aspect_Window)& theWindow,
a521d90d 285 const Aspect_RenderingContext theContext)
e276548b 286{
c357e426 287 myWindow = myDriver->CreateRenderWindow (theWindow, theContext);
288 Standard_ASSERT_RAISE (!myWindow.IsNull(),
289 "OpenGl_View::SetWindow, "
290 "Failed to create OpenGl window.");
e276548b 291
c357e426 292 myWorkspace = new OpenGl_Workspace (this, myWindow);
293 myWorldViewProjState.Reset();
91c60b57 294 myToUpdateEnvironmentMap = Standard_True;
c357e426 295 myHasFboBlit = Standard_True;
296 Invalidate();
297
298 // Environment texture resource does not support lazy initialization.
299 initTextureEnv (myWorkspace->GetGlContext());
300}
301
302// =======================================================================
303// function : Resized
304// purpose :
305// =======================================================================
306void OpenGl_View::Resized()
307{
308 if (myWindow.IsNull())
309 return;
310
311 myWindow->Resize();
312}
313
314// =======================================================================
c357e426 315// function : SetMinMaxValuesCallback
316// purpose :
317// =======================================================================
318static void SetMinMaxValuesCallback (Graphic3d_CView* theView)
319{
320 OpenGl_View* aView = dynamic_cast<OpenGl_View*>(theView);
321 if (aView == NULL)
322 return;
323
324 Bnd_Box aBox = theView->MinMaxValues();
325 if (!aBox.IsVoid())
2166f0fa 326 {
c357e426 327 gp_Pnt aMin = aBox.CornerMin();
328 gp_Pnt aMax = aBox.CornerMax();
329
330 Graphic3d_Vec3 aMinVec ((Standard_ShortReal )aMin.X(), (Standard_ShortReal )aMin.Y(), (Standard_ShortReal )aMin.Z());
331 Graphic3d_Vec3 aMaxVec ((Standard_ShortReal )aMax.X(), (Standard_ShortReal )aMax.Y(), (Standard_ShortReal )aMax.Z());
332 aView->GraduatedTrihedronMinMaxValues (aMinVec, aMaxVec);
2166f0fa
SK
333 }
334}
335
c357e426 336// =======================================================================
337// function : GraduatedTrihedronDisplay
338// purpose :
339// =======================================================================
340void OpenGl_View::GraduatedTrihedronDisplay (const Graphic3d_GraduatedTrihedron& theTrihedronData)
341{
342 myGTrihedronData = theTrihedronData;
343 myGTrihedronData.PtrView = this;
344 myGTrihedronData.CubicAxesCallback = SetMinMaxValuesCallback;
345 myGraduatedTrihedron.SetValues (myGTrihedronData);
346 myToShowGradTrihedron = true;
347}
2166f0fa 348
c357e426 349// =======================================================================
350// function : GraduatedTrihedronErase
351// purpose :
352// =======================================================================
353void OpenGl_View::GraduatedTrihedronErase()
2166f0fa 354{
c357e426 355 myGTrihedronData.PtrView = NULL;
356 myGraduatedTrihedron.Release (myWorkspace->GetGlContext().operator->());
357 myToShowGradTrihedron = false;
2166f0fa
SK
358}
359
c357e426 360// =======================================================================
361// function : GraduatedTrihedronMinMaxValues
362// purpose :
363// =======================================================================
364void OpenGl_View::GraduatedTrihedronMinMaxValues (const Graphic3d_Vec3 theMin, const Graphic3d_Vec3 theMax)
365{
366 myGraduatedTrihedron.SetMinMax (theMin, theMax);
367}
2166f0fa 368
c357e426 369// =======================================================================
c357e426 370// function : BufferDump
371// purpose :
372// =======================================================================
373Standard_Boolean OpenGl_View::BufferDump (Image_PixMap& theImage, const Graphic3d_BufferType& theBufferType)
374{
38d90bb3 375 if (theBufferType != Graphic3d_BT_RGB_RayTraceHdrLeft)
376 {
377 return myWorkspace->BufferDump(myFBO, theImage, theBufferType);
378 }
379
380 if (!myRaytraceParameters.AdaptiveScreenSampling)
381 {
382 return myWorkspace->BufferDump(myAccumFrames % 2 ? myRaytraceFBO2[0] : myRaytraceFBO1[0], theImage, theBufferType);
383 }
384
385#if defined(GL_ES_VERSION_2_0)
386 return false;
387#else
388 if (theImage.Format() != Image_Format_RGBF)
389 {
390 return false;
391 }
392
393 const GLuint aW = myRaytraceOutputTexture[0]->SizeX();
394 const GLuint aH = myRaytraceOutputTexture[0]->SizeY();
395 if (aW / 3 != theImage.SizeX() || aH / 2 != theImage.SizeY())
396 {
397 return false;
398 }
399
400 std::vector<GLfloat> aValues;
401 try
402 {
403 aValues.resize (aW * aH);
404 }
405 catch (const std::bad_alloc&)
406 {
407 return false;
408 }
409
410 glBindTexture (GL_TEXTURE_RECTANGLE, myRaytraceOutputTexture[0]->TextureId());
411 glGetTexImage (GL_TEXTURE_RECTANGLE, 0, OpenGl_TextureFormat::Create<GLfloat, 1>().Format(), GL_FLOAT, &aValues[0]);
412 glBindTexture (GL_TEXTURE_RECTANGLE, 0);
413 for (unsigned int aRow = 0; aRow < aH; aRow += 2)
414 {
415 for (unsigned int aCol = 0; aCol < aW; aCol += 3)
416 {
417 float* anImageValue = theImage.ChangeValue<float[3]> ((aH - aRow) / 2 - 1, aCol / 3);
418 float aInvNbSamples = 1.f / aValues[aRow * aW + aCol + aW];
419 anImageValue[0] = aValues[aRow * aW + aCol] * aInvNbSamples;
420 anImageValue[1] = aValues[aRow * aW + aCol + 1] * aInvNbSamples;
421 anImageValue[2] = aValues[aRow * aW + aCol + 1 + aW] * aInvNbSamples;
422 }
423 }
424
425 return true;
426#endif
c357e426 427}
428
429// =======================================================================
430// function : Background
431// purpose :
432// =======================================================================
433Aspect_Background OpenGl_View::Background() const
434{
b6472664 435 return Aspect_Background (myBgColor.GetRGB());
c357e426 436}
437
438// =======================================================================
439// function : SetBackground
440// purpose :
441// =======================================================================
442void OpenGl_View::SetBackground (const Aspect_Background& theBackground)
443{
b6472664 444 myBgColor.SetRGB (theBackground.Color());
c357e426 445}
446
447// =======================================================================
448// function : GradientBackground
449// purpose :
450// =======================================================================
451Aspect_GradientBackground OpenGl_View::GradientBackground() const
452{
453 Quantity_Color aColor1, aColor2;
454 aColor1.SetValues (myBgGradientArray->GradientColor (0).r(),
455 myBgGradientArray->GradientColor (0).g(),
456 myBgGradientArray->GradientColor (0).b(), Quantity_TOC_RGB);
457 aColor2.SetValues (myBgGradientArray->GradientColor (1).r(),
458 myBgGradientArray->GradientColor (1).g(),
459 myBgGradientArray->GradientColor (1).b(), Quantity_TOC_RGB);
460 return Aspect_GradientBackground (aColor1, aColor2, myBgGradientArray->GradientFillMethod());
461}
2166f0fa 462
c357e426 463// =======================================================================
464// function : SetGradientBackground
465// purpose :
466// =======================================================================
467void OpenGl_View::SetGradientBackground (const Aspect_GradientBackground& theBackground)
2166f0fa 468{
c357e426 469 Quantity_Color aColor1, aColor2;
470 theBackground.Colors (aColor1, aColor2);
471 myBgGradientArray->SetGradientParameters (aColor1, aColor2, theBackground.BgGradientFillMethod());
472}
473
474// =======================================================================
475// function : SetBackgroundImage
476// purpose :
477// =======================================================================
478void OpenGl_View::SetBackgroundImage (const TCollection_AsciiString& theFilePath)
479{
480 // Prepare aspect for texture storage
481 myBackgroundImagePath = theFilePath;
482 Handle(Graphic3d_AspectFillArea3d) anAspect = new Graphic3d_AspectFillArea3d();
483 Handle(Graphic3d_Texture2Dmanual) aTextureMap = new Graphic3d_Texture2Dmanual (TCollection_AsciiString (theFilePath));
484 aTextureMap->EnableRepeat();
485 aTextureMap->DisableModulate();
486 aTextureMap->GetParams()->SetGenMode (Graphic3d_TOTM_MANUAL,
487 Graphic3d_Vec4 (0.0f, 0.0f, 0.0f, 0.0f),
488 Graphic3d_Vec4 (0.0f, 0.0f, 0.0f, 0.0f));
489 anAspect->SetTextureMap (aTextureMap);
490 anAspect->SetInteriorStyle (Aspect_IS_SOLID);
b6472664 491 anAspect->SetSuppressBackFaces (false);
c357e426 492 // Enable texture mapping
493 if (aTextureMap->IsDone())
2166f0fa 494 {
c357e426 495 anAspect->SetTextureMapOn();
2166f0fa
SK
496 }
497 else
498 {
c357e426 499 anAspect->SetTextureMapOff();
500 return;
501 }
2166f0fa 502
c357e426 503 // Set texture parameters
504 myTextureParams->SetAspect (anAspect);
505}
2166f0fa 506
c357e426 507// =======================================================================
508// function : BackgroundImageStyle
509// purpose :
510// =======================================================================
511Aspect_FillMethod OpenGl_View::BackgroundImageStyle() const
512{
513 return myBgTextureArray->TextureFillMethod();
2166f0fa
SK
514}
515
c357e426 516// =======================================================================
517// function : SetBackgroundImageStyle
518// purpose :
519// =======================================================================
520void OpenGl_View::SetBackgroundImageStyle (const Aspect_FillMethod theFillStyle)
521{
522 myBgTextureArray->SetTextureFillMethod (theFillStyle);
523}
2166f0fa 524
c357e426 525//=======================================================================
526//function : AddZLayer
527//purpose :
528//=======================================================================
529void OpenGl_View::AddZLayer (const Graphic3d_ZLayerId theLayerId)
2166f0fa 530{
c357e426 531 myZLayers.AddLayer (theLayerId);
2166f0fa
SK
532}
533
c357e426 534//=======================================================================
535//function : RemoveZLayer
536//purpose :
537//=======================================================================
538void OpenGl_View::RemoveZLayer (const Graphic3d_ZLayerId theLayerId)
539{
540 myZLayers.RemoveLayer (theLayerId);
541}
2166f0fa 542
c357e426 543//=======================================================================
544//function : SetZLayerSettings
545//purpose :
546//=======================================================================
547void OpenGl_View::SetZLayerSettings (const Graphic3d_ZLayerId theLayerId,
548 const Graphic3d_ZLayerSettings& theSettings)
2166f0fa 549{
c357e426 550 myZLayers.SetLayerSettings (theLayerId, theSettings);
2166f0fa
SK
551}
552
c357e426 553//=======================================================================
50d06d8f 554//function : ZLayerMax
555//purpose :
556//=======================================================================
557Standard_Integer OpenGl_View::ZLayerMax() const
558{
559 Standard_Integer aLayerMax = Graphic3d_ZLayerId_Default;
560 for (OpenGl_LayerSeqIds::Iterator aMapIt(myZLayers.LayerIDs()); aMapIt.More(); aMapIt.Next())
561 {
562 aLayerMax = Max (aLayerMax, aMapIt.Value());
563 }
564
565 return aLayerMax;
566}
567
568//=======================================================================
569//function : InvalidateZLayerBoundingBox
570//purpose :
571//=======================================================================
572void OpenGl_View::InvalidateZLayerBoundingBox (const Graphic3d_ZLayerId theLayerId) const
573{
574 if (myZLayers.LayerIDs().IsBound (theLayerId))
575 {
576 myZLayers.Layer (theLayerId).InvalidateBoundingBox();
577 }
578 else
579 {
1475265b 580 const Standard_Integer aLayerMax = ZLayerMax();
581 for (Standard_Integer aLayerId = Graphic3d_ZLayerId_Default; aLayerId < aLayerMax; ++aLayerId)
50d06d8f 582 {
583 if (myZLayers.LayerIDs().IsBound (aLayerId))
584 {
585 const OpenGl_Layer& aLayer = myZLayers.Layer (aLayerId);
586 if (aLayer.NbOfTransformPersistenceObjects() > 0)
587 {
588 aLayer.InvalidateBoundingBox();
589 }
590 }
591 }
592 }
593}
594
595//=======================================================================
596//function : ZLayerBoundingBox
597//purpose :
598//=======================================================================
7c3ef2f7 599Bnd_Box OpenGl_View::ZLayerBoundingBox (const Graphic3d_ZLayerId theLayerId,
600 const Handle(Graphic3d_Camera)& theCamera,
601 const Standard_Integer theWindowWidth,
602 const Standard_Integer theWindowHeight,
603 const Standard_Boolean theToIncludeAuxiliary) const
50d06d8f 604{
7c3ef2f7 605 Bnd_Box aBox;
50d06d8f 606 if (myZLayers.LayerIDs().IsBound (theLayerId))
607 {
71d8ccc7 608 aBox = myZLayers.Layer (theLayerId).BoundingBox (Identification(),
50d06d8f 609 theCamera,
610 theWindowWidth,
611 theWindowHeight,
3fe9ce0e 612 theToIncludeAuxiliary);
50d06d8f 613 }
614
71d8ccc7 615 // add bounding box of gradient/texture background for proper Z-fit
616 if (theToIncludeAuxiliary
617 && theLayerId == Graphic3d_ZLayerId_BotOSD
618 && (myBgTextureArray->IsDefined()
619 || myBgGradientArray->IsDefined()))
620 {
621 // Background is drawn using 2D transformation persistence
622 // (e.g. it is actually placed in 3D coordinates within active camera position).
623 // We add here full-screen plane with 2D transformation persistence
624 // for simplicity (myBgTextureArray might define a little bit different options
625 // but it is updated within ::Render())
7c3ef2f7 626 const Graphic3d_Mat4d& aProjectionMat = theCamera->ProjectionMatrix();
627 const Graphic3d_Mat4d& aWorldViewMat = theCamera->OrientationMatrix();
628 Graphic3d_BndBox3d aBox2d (Graphic3d_Vec3d (0.0, 0.0, 0.0),
629 Graphic3d_Vec3d (double(theWindowWidth), double(theWindowHeight), 0.0));
71d8ccc7 630
778cd667 631 Graphic3d_TransformPers aTrsfPers (Graphic3d_TMF_2d, Aspect_TOTP_LEFT_LOWER);
71d8ccc7 632 aTrsfPers.Apply (theCamera,
633 aProjectionMat,
634 aWorldViewMat,
635 theWindowWidth,
636 theWindowHeight,
637 aBox2d);
7c3ef2f7 638 aBox.Add (gp_Pnt (aBox2d.CornerMin().x(), aBox2d.CornerMin().y(), aBox2d.CornerMin().z()));
639 aBox.Add (gp_Pnt (aBox2d.CornerMax().x(), aBox2d.CornerMax().y(), aBox2d.CornerMax().z()));
71d8ccc7 640 }
641
642 return aBox;
50d06d8f 643}
644
645//=======================================================================
646//function : considerZoomPersistenceObjects
647//purpose :
648//=======================================================================
649Standard_Real OpenGl_View::considerZoomPersistenceObjects (const Graphic3d_ZLayerId theLayerId,
650 const Handle(Graphic3d_Camera)& theCamera,
651 const Standard_Integer theWindowWidth,
3fe9ce0e 652 const Standard_Integer theWindowHeight) const
50d06d8f 653{
1d92133e 654 if (myZLayers.LayerIDs().IsBound (theLayerId))
50d06d8f 655 {
656 return myZLayers.Layer (theLayerId).considerZoomPersistenceObjects (Identification(),
657 theCamera,
658 theWindowWidth,
3fe9ce0e 659 theWindowHeight);
50d06d8f 660 }
661
662 return 1.0;
663}
664
665//=======================================================================
c357e426 666//function : FBO
667//purpose :
668//=======================================================================
b128c892 669Handle(Standard_Transient) OpenGl_View::FBO() const
c357e426 670{
b128c892 671 return Handle(Standard_Transient)(myFBO);
c357e426 672}
2166f0fa 673
c357e426 674//=======================================================================
675//function : SetFBO
676//purpose :
677//=======================================================================
b128c892 678void OpenGl_View::SetFBO (const Handle(Standard_Transient)& theFbo)
2166f0fa 679{
b128c892 680 myFBO = Handle(OpenGl_FrameBuffer)::DownCast (theFbo);
2166f0fa
SK
681}
682
c357e426 683//=======================================================================
684//function : FBOCreate
685//purpose :
686//=======================================================================
b128c892 687Handle(Standard_Transient) OpenGl_View::FBOCreate (const Standard_Integer theWidth,
688 const Standard_Integer theHeight)
c357e426 689{
690 return myWorkspace->FBOCreate (theWidth, theHeight);
691}
2166f0fa 692
c357e426 693//=======================================================================
694//function : FBORelease
695//purpose :
696//=======================================================================
b128c892 697void OpenGl_View::FBORelease (Handle(Standard_Transient)& theFbo)
2166f0fa 698{
b128c892 699 Handle(OpenGl_FrameBuffer) aFrameBuffer = Handle(OpenGl_FrameBuffer)::DownCast (theFbo);
700 if (aFrameBuffer.IsNull())
701 {
702 return;
703 }
704
705 myWorkspace->FBORelease (aFrameBuffer);
706 theFbo.Nullify();
c357e426 707}
708
709//=======================================================================
710//function : FBOGetDimensions
711//purpose :
712//=======================================================================
b128c892 713void OpenGl_View::FBOGetDimensions (const Handle(Standard_Transient)& theFbo,
c357e426 714 Standard_Integer& theWidth,
715 Standard_Integer& theHeight,
716 Standard_Integer& theWidthMax,
717 Standard_Integer& theHeightMax)
718{
b128c892 719 const Handle(OpenGl_FrameBuffer) aFrameBuffer = Handle(OpenGl_FrameBuffer)::DownCast (theFbo);
720 if (aFrameBuffer.IsNull())
721 {
722 return;
723 }
724
c357e426 725 theWidth = aFrameBuffer->GetVPSizeX(); // current viewport size
726 theHeight = aFrameBuffer->GetVPSizeY();
727 theWidthMax = aFrameBuffer->GetSizeX(); // texture size
728 theHeightMax = aFrameBuffer->GetSizeY();
729}
730
731//=======================================================================
732//function : FBOChangeViewport
733//purpose :
734//=======================================================================
b128c892 735void OpenGl_View::FBOChangeViewport (const Handle(Standard_Transient)& theFbo,
c357e426 736 const Standard_Integer theWidth,
737 const Standard_Integer theHeight)
738{
b128c892 739 const Handle(OpenGl_FrameBuffer) aFrameBuffer = Handle(OpenGl_FrameBuffer)::DownCast (theFbo);
740 if (aFrameBuffer.IsNull())
741 {
742 return;
743 }
744
c357e426 745 aFrameBuffer->ChangeViewport (theWidth, theHeight);
746}
747
748// =======================================================================
749// function : Export
750// purpose :
751// =======================================================================
752#ifdef HAVE_GL2PS
753Standard_Boolean OpenGl_View::Export (const Standard_CString theFileName,
754 const Graphic3d_ExportFormat theFormat,
755 const Graphic3d_SortType theSortType)
756{
757 // gl2psBeginPage() will call OpenGL functions
758 // so we should activate correct GL context before redraw scene call
759 if (!myWorkspace->Activate())
760 {
761 return Standard_False;
762 }
763
764 Standard_Integer aFormat = -1;
765 Standard_Integer aSortType = Graphic3d_ST_BSP_Tree;
766 switch (theFormat)
767 {
768 case Graphic3d_EF_PostScript:
769 aFormat = GL2PS_PS;
770 break;
771 case Graphic3d_EF_EnhPostScript:
772 aFormat = GL2PS_EPS;
773 break;
774 case Graphic3d_EF_TEX:
775 aFormat = GL2PS_TEX;
776 break;
777 case Graphic3d_EF_PDF:
778 aFormat = GL2PS_PDF;
779 break;
780 case Graphic3d_EF_SVG:
781 aFormat = GL2PS_SVG;
782 break;
783 case Graphic3d_EF_PGF:
784 aFormat = GL2PS_PGF;
785 break;
786 case Graphic3d_EF_EMF:
787 //aFormat = GL2PS_EMF;
788 aFormat = GL2PS_PGF + 1; // 6
789 break;
790 default:
791 // unsupported format
792 return Standard_False;
793 }
794
795 switch (theSortType)
796 {
797 case Graphic3d_ST_Simple:
798 aSortType = GL2PS_SIMPLE_SORT;
799 break;
800 case Graphic3d_ST_BSP_Tree:
801 aSortType = GL2PS_BSP_SORT;
802 break;
803 }
804
805 GLint aViewport[4];
806 aViewport[0] = 0;
807 aViewport[1] = 0;
808 aViewport[2] = myWindow->Width();
809 aViewport[3] = myWindow->Height();
810
811 GLint aBufferSize = 1024 * 1024;
812 GLint anErrCode = GL2PS_SUCCESS;
813
814 // gl2ps uses standard write functions and do not check locale
815 Standard_CLocaleSentry aLocaleSentry;
816
817 while (aBufferSize > 0)
818 {
819 // current patch for EMF support in gl2ps uses WinAPI functions to create file
820 FILE* aFileH = (theFormat != Graphic3d_EF_EMF) ? fopen (theFileName, "wb") : NULL;
821 anErrCode = gl2psBeginPage ("", "", aViewport, aFormat, aSortType,
822 GL2PS_DRAW_BACKGROUND | GL2PS_OCCLUSION_CULL | GL2PS_BEST_ROOT/* | GL2PS_SIMPLE_LINE_OFFSET*/,
823 GL_RGBA, 0, NULL,
824 0, 0, 0, aBufferSize, aFileH, theFileName);
825 if (anErrCode != GL2PS_SUCCESS)
826 {
827 // initialization failed
828 if (aFileH != NULL)
829 fclose (aFileH);
830 break;
831 }
832 Redraw();
833
834 anErrCode = gl2psEndPage();
835 if (aFileH != NULL)
836 fclose (aFileH);
837
838 if (anErrCode == GL2PS_OVERFLOW)
839 aBufferSize *= 2;
840 else
841 break;
842 }
843 return anErrCode == GL2PS_SUCCESS;
844}
845#else
846Standard_Boolean OpenGl_View::Export (const Standard_CString /*theFileName*/,
847 const Graphic3d_ExportFormat /*theFormat*/,
848 const Graphic3d_SortType /*theSortType*/)
849{
850 return Standard_False;
851}
852#endif
853
854//=======================================================================
855//function : displayStructure
856//purpose :
857//=======================================================================
858void OpenGl_View::displayStructure (const Handle(Graphic3d_CStructure)& theStructure,
859 const Standard_Integer thePriority)
860{
861 const OpenGl_Structure* aStruct = reinterpret_cast<const OpenGl_Structure*> (theStructure.operator->());
862 const Graphic3d_ZLayerId aZLayer = aStruct->ZLayer();
863 myZLayers.AddStructure (aStruct, aZLayer, thePriority);
864}
865
866//=======================================================================
867//function : eraseStructure
868//purpose :
869//=======================================================================
870void OpenGl_View::eraseStructure (const Handle(Graphic3d_CStructure)& theStructure)
871{
872 const OpenGl_Structure* aStruct = reinterpret_cast<const OpenGl_Structure*> (theStructure.operator->());
873 myZLayers.RemoveStructure (aStruct);
874}
875
876//=======================================================================
877//function : changeZLayer
878//purpose :
879//=======================================================================
880void OpenGl_View::changeZLayer (const Handle(Graphic3d_CStructure)& theStructure,
881 const Graphic3d_ZLayerId theNewLayerId)
882{
883 const Graphic3d_ZLayerId anOldLayer = theStructure->ZLayer();
884 const OpenGl_Structure* aStruct = reinterpret_cast<const OpenGl_Structure*> (theStructure.operator->());
885 myZLayers.ChangeLayer (aStruct, anOldLayer, theNewLayerId);
cfece3ef 886 Update (anOldLayer);
887 Update (theNewLayerId);
c357e426 888}
889
890//=======================================================================
891//function : changePriority
892//purpose :
893//=======================================================================
894void OpenGl_View::changePriority (const Handle(Graphic3d_CStructure)& theStructure,
895 const Standard_Integer theNewPriority)
896{
897 const Graphic3d_ZLayerId aLayerId = theStructure->ZLayer();
898 const OpenGl_Structure* aStruct = reinterpret_cast<const OpenGl_Structure*> (theStructure.operator->());
899 myZLayers.ChangePriority (aStruct, aLayerId, theNewPriority);
2166f0fa 900}
26d9c835 901
902//=======================================================================
903//function : DiagnosticInformation
904//purpose :
905//=======================================================================
906void OpenGl_View::DiagnosticInformation (TColStd_IndexedDataMapOfStringString& theDict,
907 Graphic3d_DiagnosticInfo theFlags) const
908{
909 Handle(OpenGl_Context) aCtx = myWorkspace->GetGlContext();
910 if (!myWorkspace->Activate()
911 || aCtx.IsNull())
912 {
913 return;
914 }
915
916 aCtx->DiagnosticInformation (theDict, theFlags);
917 if ((theFlags & Graphic3d_DiagnosticInfo_FrameBuffer) != 0)
918 {
919 TCollection_AsciiString aResRatio (myRenderParams.ResolutionRatio());
920 theDict.ChangeFromIndex (theDict.Add ("ResolutionRatio", aResRatio)) = aResRatio;
921 }
922}