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)
45 // =======================================================================
46 // function : Constructor
48 // =======================================================================
49 OpenGl_View::OpenGl_View (const Handle(Graphic3d_StructureManager)& theMgr,
50 const Handle(OpenGl_GraphicDriver)& theDriver,
51 const Handle(OpenGl_Caps)& theCaps,
52 OpenGl_StateCounter* theCounter)
53 : Graphic3d_CView (theMgr),
54 myDriver (theDriver.operator->()),
56 myWasRedrawnGL (Standard_False),
57 myCulling (Standard_True),
58 myShadingModel (Graphic3d_TOSM_FACET),
59 myBackfacing (Graphic3d_TOBM_AUTOMATIC),
60 myBgColor (Quantity_NOC_BLACK),
61 myCamera (new Graphic3d_Camera()),
62 myToShowGradTrihedron (false),
63 myZLayers (Structure_MAX_PRIORITY - Structure_MIN_PRIORITY + 1),
64 myStateCounter (theCounter),
65 myCurrLightSourceState (theCounter->Increment()),
67 myLastLightSourceState (0, 0),
68 #if !defined(GL_ES_VERSION_2_0)
69 myFboColorFormat (GL_RGBA8),
71 myFboColorFormat (GL_RGBA),
73 myFboDepthFormat (GL_DEPTH24_STENCIL8),
74 myToFlipOutput (Standard_False),
76 myHasFboBlit (Standard_True),
77 myToDisableOIT (Standard_False),
78 myToDisableOITMSAA (Standard_False),
79 myToDisableMSAA (Standard_False),
80 myTransientDrawToFront (Standard_True),
81 myBackBufferRestored (Standard_False),
82 myIsImmediateDrawn (Standard_False),
83 myTextureParams (new OpenGl_AspectFace()),
84 myBgGradientArray (new OpenGl_BackgroundArray (Graphic3d_TOB_GRADIENT)),
85 myBgTextureArray (new OpenGl_BackgroundArray (Graphic3d_TOB_TEXTURE)),
86 // ray-tracing fields initialization
87 myRaytraceInitStatus (OpenGl_RT_NONE),
88 myIsRaytraceDataValid (Standard_False),
89 myIsRaytraceWarnTextures (Standard_False),
90 myRaytraceBVHBuilder (new BVH_BinnedBuilder<Standard_ShortReal, 3, BVH_Constants_NbBinsBest> (BVH_Constants_LeafNodeSizeAverage,
91 BVH_Constants_MaxTreeDepth,
93 OSD_Parallel::NbLogicalProcessors() + 1)),
94 myRaytraceSceneRadius (0.0f),
95 myRaytraceSceneEpsilon (1.0e-6f),
96 myToUpdateEnvironmentMap (Standard_False),
97 myRaytraceLayerListState (0),
98 myPrevCameraApertureRadius(0.f),
99 myPrevCameraFocalPlaneDist(0.f)
101 myWorkspace = new OpenGl_Workspace (this, NULL);
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);
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();
125 // =======================================================================
126 // function : Destructor
128 // =======================================================================
129 OpenGl_View::~OpenGl_View()
131 ReleaseGlResources (NULL); // ensure ReleaseGlResources() was called within valid context
132 OpenGl_Element::Destroy (NULL, myBgGradientArray);
133 OpenGl_Element::Destroy (NULL, myBgTextureArray);
134 OpenGl_Element::Destroy (NULL, myTextureParams);
137 // =======================================================================
138 // function : ReleaseGlResources
140 // =======================================================================
141 void OpenGl_View::ReleaseGlResources (const Handle(OpenGl_Context)& theCtx)
143 myGraduatedTrihedron.Release (theCtx.operator->());
144 myFrameStatsPrs.Release (theCtx.operator->());
146 if (!myTextureEnv.IsNull())
148 for (OpenGl_TextureSet::Iterator aTextureIter (myTextureEnv); aTextureIter.More(); aTextureIter.Next())
150 theCtx->DelayedRelease (aTextureIter.ChangeValue());
151 aTextureIter.ChangeValue().Nullify();
153 myTextureEnv.Nullify();
156 if (myTextureParams != NULL)
158 myTextureParams->Release (theCtx.operator->());
160 if (myBgGradientArray != NULL)
162 myBgGradientArray->Release (theCtx.operator->());
164 if (myBgTextureArray != NULL)
166 myBgTextureArray->Release (theCtx.operator->());
169 myMainSceneFbos[0] ->Release (theCtx.operator->());
170 myMainSceneFbos[1] ->Release (theCtx.operator->());
171 myMainSceneFbosOit[0] ->Release (theCtx.operator->());
172 myMainSceneFbosOit[1] ->Release (theCtx.operator->());
173 myImmediateSceneFbos[0] ->Release (theCtx.operator->());
174 myImmediateSceneFbos[1] ->Release (theCtx.operator->());
175 myImmediateSceneFbosOit[0]->Release (theCtx.operator->());
176 myImmediateSceneFbosOit[1]->Release (theCtx.operator->());
177 myOpenGlFBO ->Release (theCtx.operator->());
178 myOpenGlFBO2 ->Release (theCtx.operator->());
179 myFullScreenQuad .Release (theCtx.operator->());
180 myFullScreenQuadFlip .Release (theCtx.operator->());
182 releaseRaytraceResources (theCtx);
185 // =======================================================================
188 // =======================================================================
189 void OpenGl_View::Remove()
196 myDriver->RemoveView (this);
199 Graphic3d_CView::Remove();
202 // =======================================================================
203 // function : SetTextureEnv
205 // =======================================================================
206 void OpenGl_View::SetCamera(const Handle(Graphic3d_Camera)& theCamera)
208 myCamera = theCamera;
211 // =======================================================================
212 // function : SetLocalOrigin
214 // =======================================================================
215 void OpenGl_View::SetLocalOrigin (const gp_XYZ& theOrigin)
217 myLocalOrigin = theOrigin;
218 const Handle(OpenGl_Context)& aCtx = myWorkspace->GetGlContext();
221 aCtx->ShaderManager()->SetLocalOrigin (theOrigin);
225 // =======================================================================
226 // function : SetTextureEnv
228 // =======================================================================
229 void OpenGl_View::SetTextureEnv (const Handle(Graphic3d_TextureEnv)& theTextureEnv)
231 Handle(OpenGl_Context) aCtx = myWorkspace->GetGlContext();
232 if (!aCtx.IsNull() && !myTextureEnv.IsNull())
234 for (OpenGl_TextureSet::Iterator aTextureIter (myTextureEnv); aTextureIter.More(); aTextureIter.Next())
236 aCtx->DelayedRelease (aTextureIter.ChangeValue());
237 aTextureIter.ChangeValue().Nullify();
241 myToUpdateEnvironmentMap = Standard_True;
242 myTextureEnvData = theTextureEnv;
243 myTextureEnv.Nullify();
244 initTextureEnv (aCtx);
247 // =======================================================================
248 // function : initTextureEnv
250 // =======================================================================
251 void OpenGl_View::initTextureEnv (const Handle(OpenGl_Context)& theContext)
253 if (myTextureEnvData.IsNull()
254 || theContext.IsNull()
255 || !theContext->MakeCurrent())
260 myTextureEnv = new OpenGl_TextureSet (1);
261 Handle(OpenGl_Texture)& aTextureEnv = myTextureEnv->ChangeFirst();
262 aTextureEnv = new OpenGl_Texture (myTextureEnvData->GetId(), myTextureEnvData->GetParams());
263 Handle(Image_PixMap) anImage = myTextureEnvData->GetImage();
264 if (!anImage.IsNull())
266 aTextureEnv->Init (theContext, *anImage.operator->(), myTextureEnvData->Type());
270 // =======================================================================
271 // function : SetImmediateModeDrawToFront
273 // =======================================================================
274 Standard_Boolean OpenGl_View::SetImmediateModeDrawToFront (const Standard_Boolean theDrawToFrontBuffer)
276 const Standard_Boolean aPrevMode = myTransientDrawToFront;
277 myTransientDrawToFront = theDrawToFrontBuffer;
281 // =======================================================================
282 // function : SetWindow
284 // =======================================================================
285 void OpenGl_View::SetWindow (const Handle(Aspect_Window)& theWindow,
286 const Aspect_RenderingContext theContext)
288 myWindow = myDriver->CreateRenderWindow (theWindow, theContext);
289 Standard_ASSERT_RAISE (!myWindow.IsNull(),
290 "OpenGl_View::SetWindow, "
291 "Failed to create OpenGl window.");
293 myWorkspace = new OpenGl_Workspace (this, myWindow);
294 myWorldViewProjState.Reset();
295 myToUpdateEnvironmentMap = Standard_True;
296 myHasFboBlit = Standard_True;
299 // Environment texture resource does not support lazy initialization.
300 initTextureEnv (myWorkspace->GetGlContext());
303 // =======================================================================
304 // function : Resized
306 // =======================================================================
307 void OpenGl_View::Resized()
309 if (myWindow.IsNull())
315 // =======================================================================
316 // function : SetMinMaxValuesCallback
318 // =======================================================================
319 static void SetMinMaxValuesCallback (Graphic3d_CView* theView)
321 OpenGl_View* aView = dynamic_cast<OpenGl_View*>(theView);
325 Bnd_Box aBox = theView->MinMaxValues();
328 gp_Pnt aMin = aBox.CornerMin();
329 gp_Pnt aMax = aBox.CornerMax();
331 Graphic3d_Vec3 aMinVec ((Standard_ShortReal )aMin.X(), (Standard_ShortReal )aMin.Y(), (Standard_ShortReal )aMin.Z());
332 Graphic3d_Vec3 aMaxVec ((Standard_ShortReal )aMax.X(), (Standard_ShortReal )aMax.Y(), (Standard_ShortReal )aMax.Z());
333 aView->GraduatedTrihedronMinMaxValues (aMinVec, aMaxVec);
337 // =======================================================================
338 // function : GraduatedTrihedronDisplay
340 // =======================================================================
341 void OpenGl_View::GraduatedTrihedronDisplay (const Graphic3d_GraduatedTrihedron& theTrihedronData)
343 myGTrihedronData = theTrihedronData;
344 myGTrihedronData.PtrView = this;
345 myGTrihedronData.CubicAxesCallback = SetMinMaxValuesCallback;
346 myGraduatedTrihedron.SetValues (myGTrihedronData);
347 myToShowGradTrihedron = true;
350 // =======================================================================
351 // function : GraduatedTrihedronErase
353 // =======================================================================
354 void OpenGl_View::GraduatedTrihedronErase()
356 myGTrihedronData.PtrView = NULL;
357 myGraduatedTrihedron.Release (myWorkspace->GetGlContext().operator->());
358 myToShowGradTrihedron = false;
361 // =======================================================================
362 // function : GraduatedTrihedronMinMaxValues
364 // =======================================================================
365 void OpenGl_View::GraduatedTrihedronMinMaxValues (const Graphic3d_Vec3 theMin, const Graphic3d_Vec3 theMax)
367 myGraduatedTrihedron.SetMinMax (theMin, theMax);
370 // =======================================================================
371 // function : BufferDump
373 // =======================================================================
374 Standard_Boolean OpenGl_View::BufferDump (Image_PixMap& theImage, const Graphic3d_BufferType& theBufferType)
376 if (theBufferType != Graphic3d_BT_RGB_RayTraceHdrLeft)
378 return myWorkspace->BufferDump(myFBO, theImage, theBufferType);
381 if (!myRaytraceParameters.AdaptiveScreenSampling)
383 return myWorkspace->BufferDump(myAccumFrames % 2 ? myRaytraceFBO2[0] : myRaytraceFBO1[0], theImage, theBufferType);
386 #if defined(GL_ES_VERSION_2_0)
389 if (theImage.Format() != Image_Format_RGBF)
394 const GLuint aW = myRaytraceOutputTexture[0]->SizeX();
395 const GLuint aH = myRaytraceOutputTexture[0]->SizeY();
396 if (aW / 3 != theImage.SizeX() || aH / 2 != theImage.SizeY())
401 std::vector<GLfloat> aValues;
404 aValues.resize (aW * aH);
406 catch (const std::bad_alloc&)
411 glBindTexture (GL_TEXTURE_RECTANGLE, myRaytraceOutputTexture[0]->TextureId());
412 glGetTexImage (GL_TEXTURE_RECTANGLE, 0, OpenGl_TextureFormat::Create<GLfloat, 1>().Format(), GL_FLOAT, &aValues[0]);
413 glBindTexture (GL_TEXTURE_RECTANGLE, 0);
414 for (unsigned int aRow = 0; aRow < aH; aRow += 2)
416 for (unsigned int aCol = 0; aCol < aW; aCol += 3)
418 float* anImageValue = theImage.ChangeValue<float[3]> ((aH - aRow) / 2 - 1, aCol / 3);
419 float aInvNbSamples = 1.f / aValues[aRow * aW + aCol + aW];
420 anImageValue[0] = aValues[aRow * aW + aCol] * aInvNbSamples;
421 anImageValue[1] = aValues[aRow * aW + aCol + 1] * aInvNbSamples;
422 anImageValue[2] = aValues[aRow * aW + aCol + 1 + aW] * aInvNbSamples;
430 // =======================================================================
431 // function : Background
433 // =======================================================================
434 Aspect_Background OpenGl_View::Background() const
436 return Aspect_Background (myBgColor.GetRGB());
439 // =======================================================================
440 // function : SetBackground
442 // =======================================================================
443 void OpenGl_View::SetBackground (const Aspect_Background& theBackground)
445 myBgColor.SetRGB (theBackground.Color());
448 // =======================================================================
449 // function : GradientBackground
451 // =======================================================================
452 Aspect_GradientBackground OpenGl_View::GradientBackground() const
454 Quantity_Color aColor1, aColor2;
455 aColor1.SetValues (myBgGradientArray->GradientColor (0).r(),
456 myBgGradientArray->GradientColor (0).g(),
457 myBgGradientArray->GradientColor (0).b(), Quantity_TOC_RGB);
458 aColor2.SetValues (myBgGradientArray->GradientColor (1).r(),
459 myBgGradientArray->GradientColor (1).g(),
460 myBgGradientArray->GradientColor (1).b(), Quantity_TOC_RGB);
461 return Aspect_GradientBackground (aColor1, aColor2, myBgGradientArray->GradientFillMethod());
464 // =======================================================================
465 // function : SetGradientBackground
467 // =======================================================================
468 void OpenGl_View::SetGradientBackground (const Aspect_GradientBackground& theBackground)
470 Quantity_Color aColor1, aColor2;
471 theBackground.Colors (aColor1, aColor2);
472 myBgGradientArray->SetGradientParameters (aColor1, aColor2, theBackground.BgGradientFillMethod());
475 // =======================================================================
476 // function : SetBackgroundImage
478 // =======================================================================
479 void OpenGl_View::SetBackgroundImage (const TCollection_AsciiString& theFilePath)
481 // Prepare aspect for texture storage
482 myBackgroundImagePath = theFilePath;
483 Handle(Graphic3d_AspectFillArea3d) anAspect = new Graphic3d_AspectFillArea3d();
484 Handle(Graphic3d_Texture2Dmanual) aTextureMap = new Graphic3d_Texture2Dmanual (TCollection_AsciiString (theFilePath));
485 aTextureMap->EnableRepeat();
486 aTextureMap->DisableModulate();
487 aTextureMap->GetParams()->SetGenMode (Graphic3d_TOTM_MANUAL,
488 Graphic3d_Vec4 (0.0f, 0.0f, 0.0f, 0.0f),
489 Graphic3d_Vec4 (0.0f, 0.0f, 0.0f, 0.0f));
490 anAspect->SetTextureMap (aTextureMap);
491 anAspect->SetInteriorStyle (Aspect_IS_SOLID);
492 anAspect->SetSuppressBackFaces (false);
493 // Enable texture mapping
494 if (aTextureMap->IsDone())
496 anAspect->SetTextureMapOn();
500 anAspect->SetTextureMapOff();
504 // Set texture parameters
505 myTextureParams->SetAspect (anAspect);
508 // =======================================================================
509 // function : BackgroundImageStyle
511 // =======================================================================
512 Aspect_FillMethod OpenGl_View::BackgroundImageStyle() const
514 return myBgTextureArray->TextureFillMethod();
517 // =======================================================================
518 // function : SetBackgroundImageStyle
520 // =======================================================================
521 void OpenGl_View::SetBackgroundImageStyle (const Aspect_FillMethod theFillStyle)
523 myBgTextureArray->SetTextureFillMethod (theFillStyle);
526 //=======================================================================
527 //function : AddZLayer
529 //=======================================================================
530 void OpenGl_View::AddZLayer (const Graphic3d_ZLayerId theLayerId)
532 myZLayers.AddLayer (theLayerId);
535 //=======================================================================
536 //function : RemoveZLayer
538 //=======================================================================
539 void OpenGl_View::RemoveZLayer (const Graphic3d_ZLayerId theLayerId)
541 myZLayers.RemoveLayer (theLayerId);
544 //=======================================================================
545 //function : SetZLayerSettings
547 //=======================================================================
548 void OpenGl_View::SetZLayerSettings (const Graphic3d_ZLayerId theLayerId,
549 const Graphic3d_ZLayerSettings& theSettings)
551 myZLayers.SetLayerSettings (theLayerId, theSettings);
554 //=======================================================================
555 //function : ZLayerMax
557 //=======================================================================
558 Standard_Integer OpenGl_View::ZLayerMax() const
560 Standard_Integer aLayerMax = Graphic3d_ZLayerId_Default;
561 for (OpenGl_LayerSeqIds::Iterator aMapIt(myZLayers.LayerIDs()); aMapIt.More(); aMapIt.Next())
563 aLayerMax = Max (aLayerMax, aMapIt.Value());
569 //=======================================================================
570 //function : InvalidateZLayerBoundingBox
572 //=======================================================================
573 void OpenGl_View::InvalidateZLayerBoundingBox (const Graphic3d_ZLayerId theLayerId) const
575 if (myZLayers.LayerIDs().IsBound (theLayerId))
577 myZLayers.Layer (theLayerId).InvalidateBoundingBox();
581 const Standard_Integer aLayerMax = ZLayerMax();
582 for (Standard_Integer aLayerId = Graphic3d_ZLayerId_Default; aLayerId < aLayerMax; ++aLayerId)
584 if (myZLayers.LayerIDs().IsBound (aLayerId))
586 const OpenGl_Layer& aLayer = myZLayers.Layer (aLayerId);
587 if (aLayer.NbOfTransformPersistenceObjects() > 0)
589 aLayer.InvalidateBoundingBox();
596 //=======================================================================
597 //function : ZLayerBoundingBox
599 //=======================================================================
600 Bnd_Box OpenGl_View::ZLayerBoundingBox (const Graphic3d_ZLayerId theLayerId,
601 const Handle(Graphic3d_Camera)& theCamera,
602 const Standard_Integer theWindowWidth,
603 const Standard_Integer theWindowHeight,
604 const Standard_Boolean theToIncludeAuxiliary) const
607 if (myZLayers.LayerIDs().IsBound (theLayerId))
609 aBox = myZLayers.Layer (theLayerId).BoundingBox (Identification(),
613 theToIncludeAuxiliary);
616 // add bounding box of gradient/texture background for proper Z-fit
617 if (theToIncludeAuxiliary
618 && theLayerId == Graphic3d_ZLayerId_BotOSD
619 && (myBgTextureArray->IsDefined()
620 || myBgGradientArray->IsDefined()))
622 // Background is drawn using 2D transformation persistence
623 // (e.g. it is actually placed in 3D coordinates within active camera position).
624 // We add here full-screen plane with 2D transformation persistence
625 // for simplicity (myBgTextureArray might define a little bit different options
626 // but it is updated within ::Render())
627 const Graphic3d_Mat4d& aProjectionMat = theCamera->ProjectionMatrix();
628 const Graphic3d_Mat4d& aWorldViewMat = theCamera->OrientationMatrix();
629 Graphic3d_BndBox3d aBox2d (Graphic3d_Vec3d (0.0, 0.0, 0.0),
630 Graphic3d_Vec3d (double(theWindowWidth), double(theWindowHeight), 0.0));
632 Graphic3d_TransformPers aTrsfPers (Graphic3d_TMF_2d, Aspect_TOTP_LEFT_LOWER);
633 aTrsfPers.Apply (theCamera,
639 aBox.Add (gp_Pnt (aBox2d.CornerMin().x(), aBox2d.CornerMin().y(), aBox2d.CornerMin().z()));
640 aBox.Add (gp_Pnt (aBox2d.CornerMax().x(), aBox2d.CornerMax().y(), aBox2d.CornerMax().z()));
646 //=======================================================================
647 //function : considerZoomPersistenceObjects
649 //=======================================================================
650 Standard_Real OpenGl_View::considerZoomPersistenceObjects (const Graphic3d_ZLayerId theLayerId,
651 const Handle(Graphic3d_Camera)& theCamera,
652 const Standard_Integer theWindowWidth,
653 const Standard_Integer theWindowHeight) const
655 if (myZLayers.LayerIDs().IsBound (theLayerId))
657 return myZLayers.Layer (theLayerId).considerZoomPersistenceObjects (Identification(),
666 //=======================================================================
669 //=======================================================================
670 Handle(Standard_Transient) OpenGl_View::FBO() const
672 return Handle(Standard_Transient)(myFBO);
675 //=======================================================================
678 //=======================================================================
679 void OpenGl_View::SetFBO (const Handle(Standard_Transient)& theFbo)
681 myFBO = Handle(OpenGl_FrameBuffer)::DownCast (theFbo);
684 //=======================================================================
685 //function : FBOCreate
687 //=======================================================================
688 Handle(Standard_Transient) OpenGl_View::FBOCreate (const Standard_Integer theWidth,
689 const Standard_Integer theHeight)
691 return myWorkspace->FBOCreate (theWidth, theHeight);
694 //=======================================================================
695 //function : FBORelease
697 //=======================================================================
698 void OpenGl_View::FBORelease (Handle(Standard_Transient)& theFbo)
700 Handle(OpenGl_FrameBuffer) aFrameBuffer = Handle(OpenGl_FrameBuffer)::DownCast (theFbo);
701 if (aFrameBuffer.IsNull())
706 myWorkspace->FBORelease (aFrameBuffer);
710 //=======================================================================
711 //function : FBOGetDimensions
713 //=======================================================================
714 void OpenGl_View::FBOGetDimensions (const Handle(Standard_Transient)& theFbo,
715 Standard_Integer& theWidth,
716 Standard_Integer& theHeight,
717 Standard_Integer& theWidthMax,
718 Standard_Integer& theHeightMax)
720 const Handle(OpenGl_FrameBuffer) aFrameBuffer = Handle(OpenGl_FrameBuffer)::DownCast (theFbo);
721 if (aFrameBuffer.IsNull())
726 theWidth = aFrameBuffer->GetVPSizeX(); // current viewport size
727 theHeight = aFrameBuffer->GetVPSizeY();
728 theWidthMax = aFrameBuffer->GetSizeX(); // texture size
729 theHeightMax = aFrameBuffer->GetSizeY();
732 //=======================================================================
733 //function : FBOChangeViewport
735 //=======================================================================
736 void OpenGl_View::FBOChangeViewport (const Handle(Standard_Transient)& theFbo,
737 const Standard_Integer theWidth,
738 const Standard_Integer theHeight)
740 const Handle(OpenGl_FrameBuffer) aFrameBuffer = Handle(OpenGl_FrameBuffer)::DownCast (theFbo);
741 if (aFrameBuffer.IsNull())
746 aFrameBuffer->ChangeViewport (theWidth, theHeight);
749 // =======================================================================
752 // =======================================================================
754 Standard_Boolean OpenGl_View::Export (const Standard_CString theFileName,
755 const Graphic3d_ExportFormat theFormat,
756 const Graphic3d_SortType theSortType)
758 // gl2psBeginPage() will call OpenGL functions
759 // so we should activate correct GL context before redraw scene call
760 if (!myWorkspace->Activate())
762 return Standard_False;
765 Standard_Integer aFormat = -1;
766 Standard_Integer aSortType = Graphic3d_ST_BSP_Tree;
769 case Graphic3d_EF_PostScript:
772 case Graphic3d_EF_EnhPostScript:
775 case Graphic3d_EF_TEX:
778 case Graphic3d_EF_PDF:
781 case Graphic3d_EF_SVG:
784 case Graphic3d_EF_PGF:
787 case Graphic3d_EF_EMF:
788 //aFormat = GL2PS_EMF;
789 aFormat = GL2PS_PGF + 1; // 6
792 // unsupported format
793 return Standard_False;
798 case Graphic3d_ST_Simple:
799 aSortType = GL2PS_SIMPLE_SORT;
801 case Graphic3d_ST_BSP_Tree:
802 aSortType = GL2PS_BSP_SORT;
809 aViewport[2] = myWindow->Width();
810 aViewport[3] = myWindow->Height();
812 GLint aBufferSize = 1024 * 1024;
813 GLint anErrCode = GL2PS_SUCCESS;
815 // gl2ps uses standard write functions and do not check locale
816 Standard_CLocaleSentry aLocaleSentry;
818 while (aBufferSize > 0)
820 // current patch for EMF support in gl2ps uses WinAPI functions to create file
821 FILE* aFileH = (theFormat != Graphic3d_EF_EMF) ? fopen (theFileName, "wb") : NULL;
822 anErrCode = gl2psBeginPage ("", "", aViewport, aFormat, aSortType,
823 GL2PS_DRAW_BACKGROUND | GL2PS_OCCLUSION_CULL | GL2PS_BEST_ROOT/* | GL2PS_SIMPLE_LINE_OFFSET*/,
825 0, 0, 0, aBufferSize, aFileH, theFileName);
826 if (anErrCode != GL2PS_SUCCESS)
828 // initialization failed
835 anErrCode = gl2psEndPage();
839 if (anErrCode == GL2PS_OVERFLOW)
844 return anErrCode == GL2PS_SUCCESS;
847 Standard_Boolean OpenGl_View::Export (const Standard_CString /*theFileName*/,
848 const Graphic3d_ExportFormat /*theFormat*/,
849 const Graphic3d_SortType /*theSortType*/)
851 return Standard_False;
855 //=======================================================================
856 //function : displayStructure
858 //=======================================================================
859 void OpenGl_View::displayStructure (const Handle(Graphic3d_CStructure)& theStructure,
860 const Standard_Integer thePriority)
862 const OpenGl_Structure* aStruct = reinterpret_cast<const OpenGl_Structure*> (theStructure.operator->());
863 const Graphic3d_ZLayerId aZLayer = aStruct->ZLayer();
864 myZLayers.AddStructure (aStruct, aZLayer, thePriority);
867 //=======================================================================
868 //function : eraseStructure
870 //=======================================================================
871 void OpenGl_View::eraseStructure (const Handle(Graphic3d_CStructure)& theStructure)
873 const OpenGl_Structure* aStruct = reinterpret_cast<const OpenGl_Structure*> (theStructure.operator->());
874 myZLayers.RemoveStructure (aStruct);
877 //=======================================================================
878 //function : changeZLayer
880 //=======================================================================
881 void OpenGl_View::changeZLayer (const Handle(Graphic3d_CStructure)& theStructure,
882 const Graphic3d_ZLayerId theNewLayerId)
884 const Graphic3d_ZLayerId anOldLayer = theStructure->ZLayer();
885 const OpenGl_Structure* aStruct = reinterpret_cast<const OpenGl_Structure*> (theStructure.operator->());
886 myZLayers.ChangeLayer (aStruct, anOldLayer, theNewLayerId);
888 Update (theNewLayerId);
891 //=======================================================================
892 //function : changePriority
894 //=======================================================================
895 void OpenGl_View::changePriority (const Handle(Graphic3d_CStructure)& theStructure,
896 const Standard_Integer theNewPriority)
898 const Graphic3d_ZLayerId aLayerId = theStructure->ZLayer();
899 const OpenGl_Structure* aStruct = reinterpret_cast<const OpenGl_Structure*> (theStructure.operator->());
900 myZLayers.ChangePriority (aStruct, aLayerId, theNewPriority);
903 //=======================================================================
904 //function : DiagnosticInformation
906 //=======================================================================
907 void OpenGl_View::DiagnosticInformation (TColStd_IndexedDataMapOfStringString& theDict,
908 Graphic3d_DiagnosticInfo theFlags) const
910 Handle(OpenGl_Context) aCtx = myWorkspace->GetGlContext();
911 if (!myWorkspace->Activate()
917 aCtx->DiagnosticInformation (theDict, theFlags);
918 if ((theFlags & Graphic3d_DiagnosticInfo_FrameBuffer) != 0)
920 TCollection_AsciiString aResRatio (myRenderParams.ResolutionRatio());
921 theDict.ChangeFromIndex (theDict.Add ("ResolutionRatio", aResRatio)) = aResRatio;