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 myBackfacing (Graphic3d_TOBM_AUTOMATIC),
59 myBgColor (Quantity_NOC_BLACK),
60 myCamera (new Graphic3d_Camera()),
61 myToShowGradTrihedron (false),
62 myZLayers (Structure_MAX_PRIORITY - Structure_MIN_PRIORITY + 1),
63 myStateCounter (theCounter),
64 myCurrLightSourceState (theCounter->Increment()),
66 myLastLightSourceState (0, 0),
67 myFboColorFormat (GL_RGBA8),
68 myFboDepthFormat (GL_DEPTH24_STENCIL8),
69 myToFlipOutput (Standard_False),
71 myHasFboBlit (Standard_True),
72 myToDisableOIT (Standard_False),
73 myToDisableOITMSAA (Standard_False),
74 myToDisableMSAA (Standard_False),
75 myTransientDrawToFront (Standard_True),
76 myBackBufferRestored (Standard_False),
77 myIsImmediateDrawn (Standard_False),
78 myTextureParams (new OpenGl_AspectFace()),
79 myBgGradientArray (new OpenGl_BackgroundArray (Graphic3d_TOB_GRADIENT)),
80 myBgTextureArray (new OpenGl_BackgroundArray (Graphic3d_TOB_TEXTURE)),
81 // ray-tracing fields initialization
82 myRaytraceInitStatus (OpenGl_RT_NONE),
83 myIsRaytraceDataValid (Standard_False),
84 myIsRaytraceWarnTextures (Standard_False),
85 myRaytraceBVHBuilder (new BVH_BinnedBuilder<Standard_ShortReal, 3, BVH_Constants_NbBinsBest> (BVH_Constants_LeafNodeSizeAverage,
86 BVH_Constants_MaxTreeDepth,
88 OSD_Parallel::NbLogicalProcessors() + 1)),
89 myRaytraceSceneRadius (0.0f),
90 myRaytraceSceneEpsilon (1.0e-6f),
91 myToUpdateEnvironmentMap (Standard_False),
92 myRaytraceLayerListState (0),
93 myPrevCameraApertureRadius(0.f),
94 myPrevCameraFocalPlaneDist(0.f)
96 myWorkspace = new OpenGl_Workspace (this, NULL);
98 Handle(Graphic3d_CLight) aLight = new Graphic3d_CLight (Graphic3d_TOLS_AMBIENT);
99 aLight->SetHeadlight (false);
100 aLight->SetColor (Quantity_NOC_WHITE);
101 myNoShadingLight = new Graphic3d_LightSet();
102 myNoShadingLight->Add (aLight);
104 myMainSceneFbos[0] = new OpenGl_FrameBuffer();
105 myMainSceneFbos[1] = new OpenGl_FrameBuffer();
106 myMainSceneFbosOit[0] = new OpenGl_FrameBuffer();
107 myMainSceneFbosOit[1] = new OpenGl_FrameBuffer();
108 myImmediateSceneFbos[0] = new OpenGl_FrameBuffer();
109 myImmediateSceneFbos[1] = new OpenGl_FrameBuffer();
110 myImmediateSceneFbosOit[0] = new OpenGl_FrameBuffer();
111 myImmediateSceneFbosOit[1] = new OpenGl_FrameBuffer();
112 myOpenGlFBO = new OpenGl_FrameBuffer();
113 myOpenGlFBO2 = new OpenGl_FrameBuffer();
114 myRaytraceFBO1[0] = new OpenGl_FrameBuffer();
115 myRaytraceFBO1[1] = new OpenGl_FrameBuffer();
116 myRaytraceFBO2[0] = new OpenGl_FrameBuffer();
117 myRaytraceFBO2[1] = new OpenGl_FrameBuffer();
120 // =======================================================================
121 // function : Destructor
123 // =======================================================================
124 OpenGl_View::~OpenGl_View()
126 ReleaseGlResources (NULL); // ensure ReleaseGlResources() was called within valid context
127 OpenGl_Element::Destroy (NULL, myBgGradientArray);
128 OpenGl_Element::Destroy (NULL, myBgTextureArray);
129 OpenGl_Element::Destroy (NULL, myTextureParams);
132 // =======================================================================
133 // function : ReleaseGlResources
135 // =======================================================================
136 void OpenGl_View::ReleaseGlResources (const Handle(OpenGl_Context)& theCtx)
138 myGraduatedTrihedron.Release (theCtx.operator->());
139 myFrameStatsPrs.Release (theCtx.operator->());
141 if (!myTextureEnv.IsNull())
143 for (OpenGl_TextureSet::Iterator aTextureIter (myTextureEnv); aTextureIter.More(); aTextureIter.Next())
145 theCtx->DelayedRelease (aTextureIter.ChangeValue());
146 aTextureIter.ChangeValue().Nullify();
148 myTextureEnv.Nullify();
151 if (myTextureParams != NULL)
153 myTextureParams->Release (theCtx.operator->());
155 if (myBgGradientArray != NULL)
157 myBgGradientArray->Release (theCtx.operator->());
159 if (myBgTextureArray != NULL)
161 myBgTextureArray->Release (theCtx.operator->());
164 myMainSceneFbos[0] ->Release (theCtx.operator->());
165 myMainSceneFbos[1] ->Release (theCtx.operator->());
166 myMainSceneFbosOit[0] ->Release (theCtx.operator->());
167 myMainSceneFbosOit[1] ->Release (theCtx.operator->());
168 myImmediateSceneFbos[0] ->Release (theCtx.operator->());
169 myImmediateSceneFbos[1] ->Release (theCtx.operator->());
170 myImmediateSceneFbosOit[0]->Release (theCtx.operator->());
171 myImmediateSceneFbosOit[1]->Release (theCtx.operator->());
172 myOpenGlFBO ->Release (theCtx.operator->());
173 myOpenGlFBO2 ->Release (theCtx.operator->());
174 myFullScreenQuad .Release (theCtx.operator->());
175 myFullScreenQuadFlip .Release (theCtx.operator->());
177 releaseRaytraceResources (theCtx);
180 // =======================================================================
183 // =======================================================================
184 void OpenGl_View::Remove()
191 myDriver->RemoveView (this);
194 Graphic3d_CView::Remove();
197 // =======================================================================
198 // function : SetTextureEnv
200 // =======================================================================
201 void OpenGl_View::SetCamera(const Handle(Graphic3d_Camera)& theCamera)
203 myCamera = theCamera;
206 // =======================================================================
207 // function : SetLocalOrigin
209 // =======================================================================
210 void OpenGl_View::SetLocalOrigin (const gp_XYZ& theOrigin)
212 myLocalOrigin = theOrigin;
213 const Handle(OpenGl_Context)& aCtx = myWorkspace->GetGlContext();
216 aCtx->ShaderManager()->SetLocalOrigin (theOrigin);
220 // =======================================================================
221 // function : SetTextureEnv
223 // =======================================================================
224 void OpenGl_View::SetTextureEnv (const Handle(Graphic3d_TextureEnv)& theTextureEnv)
226 Handle(OpenGl_Context) aCtx = myWorkspace->GetGlContext();
227 if (!aCtx.IsNull() && !myTextureEnv.IsNull())
229 for (OpenGl_TextureSet::Iterator aTextureIter (myTextureEnv); aTextureIter.More(); aTextureIter.Next())
231 aCtx->DelayedRelease (aTextureIter.ChangeValue());
232 aTextureIter.ChangeValue().Nullify();
236 myToUpdateEnvironmentMap = Standard_True;
237 myTextureEnvData = theTextureEnv;
238 myTextureEnv.Nullify();
239 initTextureEnv (aCtx);
242 // =======================================================================
243 // function : initTextureEnv
245 // =======================================================================
246 void OpenGl_View::initTextureEnv (const Handle(OpenGl_Context)& theContext)
248 if (myTextureEnvData.IsNull()
249 || theContext.IsNull()
250 || !theContext->MakeCurrent())
255 myTextureEnv = new OpenGl_TextureSet (1);
256 Handle(OpenGl_Texture)& aTextureEnv = myTextureEnv->ChangeFirst();
257 aTextureEnv = new OpenGl_Texture (myTextureEnvData->GetId(), myTextureEnvData->GetParams());
258 Handle(Image_PixMap) anImage = myTextureEnvData->GetImage();
259 if (!anImage.IsNull())
261 aTextureEnv->Init (theContext, *anImage.operator->(), myTextureEnvData->Type());
265 // =======================================================================
266 // function : SetImmediateModeDrawToFront
268 // =======================================================================
269 Standard_Boolean OpenGl_View::SetImmediateModeDrawToFront (const Standard_Boolean theDrawToFrontBuffer)
271 const Standard_Boolean aPrevMode = myTransientDrawToFront;
272 myTransientDrawToFront = theDrawToFrontBuffer;
276 // =======================================================================
277 // function : SetWindow
279 // =======================================================================
280 void OpenGl_View::SetWindow (const Handle(Aspect_Window)& theWindow,
281 const Aspect_RenderingContext theContext)
283 myWindow = myDriver->CreateRenderWindow (theWindow, theContext);
284 Standard_ASSERT_RAISE (!myWindow.IsNull(),
285 "OpenGl_View::SetWindow, "
286 "Failed to create OpenGl window.");
288 myWorkspace = new OpenGl_Workspace (this, myWindow);
289 myWorldViewProjState.Reset();
290 myToUpdateEnvironmentMap = Standard_True;
291 myHasFboBlit = Standard_True;
294 // Environment texture resource does not support lazy initialization.
295 initTextureEnv (myWorkspace->GetGlContext());
298 // =======================================================================
299 // function : Resized
301 // =======================================================================
302 void OpenGl_View::Resized()
304 if (myWindow.IsNull())
310 // =======================================================================
311 // function : SetMinMaxValuesCallback
313 // =======================================================================
314 static void SetMinMaxValuesCallback (Graphic3d_CView* theView)
316 OpenGl_View* aView = dynamic_cast<OpenGl_View*>(theView);
320 Bnd_Box aBox = theView->MinMaxValues();
323 gp_Pnt aMin = aBox.CornerMin();
324 gp_Pnt aMax = aBox.CornerMax();
326 Graphic3d_Vec3 aMinVec ((Standard_ShortReal )aMin.X(), (Standard_ShortReal )aMin.Y(), (Standard_ShortReal )aMin.Z());
327 Graphic3d_Vec3 aMaxVec ((Standard_ShortReal )aMax.X(), (Standard_ShortReal )aMax.Y(), (Standard_ShortReal )aMax.Z());
328 aView->GraduatedTrihedronMinMaxValues (aMinVec, aMaxVec);
332 // =======================================================================
333 // function : GraduatedTrihedronDisplay
335 // =======================================================================
336 void OpenGl_View::GraduatedTrihedronDisplay (const Graphic3d_GraduatedTrihedron& theTrihedronData)
338 myGTrihedronData = theTrihedronData;
339 myGTrihedronData.PtrView = this;
340 myGTrihedronData.CubicAxesCallback = SetMinMaxValuesCallback;
341 myGraduatedTrihedron.SetValues (myGTrihedronData);
342 myToShowGradTrihedron = true;
345 // =======================================================================
346 // function : GraduatedTrihedronErase
348 // =======================================================================
349 void OpenGl_View::GraduatedTrihedronErase()
351 myGTrihedronData.PtrView = NULL;
352 myGraduatedTrihedron.Release (myWorkspace->GetGlContext().operator->());
353 myToShowGradTrihedron = false;
356 // =======================================================================
357 // function : GraduatedTrihedronMinMaxValues
359 // =======================================================================
360 void OpenGl_View::GraduatedTrihedronMinMaxValues (const Graphic3d_Vec3 theMin, const Graphic3d_Vec3 theMax)
362 myGraduatedTrihedron.SetMinMax (theMin, theMax);
365 // =======================================================================
366 // function : BufferDump
368 // =======================================================================
369 Standard_Boolean OpenGl_View::BufferDump (Image_PixMap& theImage, const Graphic3d_BufferType& theBufferType)
371 if (theBufferType != Graphic3d_BT_RGB_RayTraceHdrLeft)
373 return myWorkspace->BufferDump(myFBO, theImage, theBufferType);
376 if (!myRaytraceParameters.AdaptiveScreenSampling)
378 return myWorkspace->BufferDump(myAccumFrames % 2 ? myRaytraceFBO2[0] : myRaytraceFBO1[0], theImage, theBufferType);
381 #if defined(GL_ES_VERSION_2_0)
384 if (theImage.Format() != Image_Format_RGBF)
389 const GLuint aW = myRaytraceOutputTexture[0]->SizeX();
390 const GLuint aH = myRaytraceOutputTexture[0]->SizeY();
391 if (aW / 3 != theImage.SizeX() || aH / 2 != theImage.SizeY())
396 std::vector<GLfloat> aValues;
399 aValues.resize (aW * aH);
401 catch (const std::bad_alloc&)
406 glBindTexture (GL_TEXTURE_RECTANGLE, myRaytraceOutputTexture[0]->TextureId());
407 glGetTexImage (GL_TEXTURE_RECTANGLE, 0, OpenGl_TextureFormat::Create<GLfloat, 1>().Format(), GL_FLOAT, &aValues[0]);
408 glBindTexture (GL_TEXTURE_RECTANGLE, 0);
409 for (unsigned int aRow = 0; aRow < aH; aRow += 2)
411 for (unsigned int aCol = 0; aCol < aW; aCol += 3)
413 float* anImageValue = theImage.ChangeValue<float[3]> ((aH - aRow) / 2 - 1, aCol / 3);
414 float aInvNbSamples = 1.f / aValues[aRow * aW + aCol + aW];
415 anImageValue[0] = aValues[aRow * aW + aCol] * aInvNbSamples;
416 anImageValue[1] = aValues[aRow * aW + aCol + 1] * aInvNbSamples;
417 anImageValue[2] = aValues[aRow * aW + aCol + 1 + aW] * aInvNbSamples;
425 // =======================================================================
426 // function : Background
428 // =======================================================================
429 Aspect_Background OpenGl_View::Background() const
431 return Aspect_Background (myBgColor.GetRGB());
434 // =======================================================================
435 // function : SetBackground
437 // =======================================================================
438 void OpenGl_View::SetBackground (const Aspect_Background& theBackground)
440 myBgColor.SetRGB (theBackground.Color());
443 // =======================================================================
444 // function : GradientBackground
446 // =======================================================================
447 Aspect_GradientBackground OpenGl_View::GradientBackground() const
449 Quantity_Color aColor1, aColor2;
450 aColor1.SetValues (myBgGradientArray->GradientColor (0).r(),
451 myBgGradientArray->GradientColor (0).g(),
452 myBgGradientArray->GradientColor (0).b(), Quantity_TOC_RGB);
453 aColor2.SetValues (myBgGradientArray->GradientColor (1).r(),
454 myBgGradientArray->GradientColor (1).g(),
455 myBgGradientArray->GradientColor (1).b(), Quantity_TOC_RGB);
456 return Aspect_GradientBackground (aColor1, aColor2, myBgGradientArray->GradientFillMethod());
459 // =======================================================================
460 // function : SetGradientBackground
462 // =======================================================================
463 void OpenGl_View::SetGradientBackground (const Aspect_GradientBackground& theBackground)
465 Quantity_Color aColor1, aColor2;
466 theBackground.Colors (aColor1, aColor2);
467 myBgGradientArray->SetGradientParameters (aColor1, aColor2, theBackground.BgGradientFillMethod());
470 // =======================================================================
471 // function : SetBackgroundImage
473 // =======================================================================
474 void OpenGl_View::SetBackgroundImage (const TCollection_AsciiString& theFilePath)
476 // Prepare aspect for texture storage
477 myBackgroundImagePath = theFilePath;
478 Handle(Graphic3d_AspectFillArea3d) anAspect = new Graphic3d_AspectFillArea3d();
479 Handle(Graphic3d_Texture2Dmanual) aTextureMap = new Graphic3d_Texture2Dmanual (TCollection_AsciiString (theFilePath));
480 aTextureMap->EnableRepeat();
481 aTextureMap->DisableModulate();
482 aTextureMap->GetParams()->SetGenMode (Graphic3d_TOTM_MANUAL,
483 Graphic3d_Vec4 (0.0f, 0.0f, 0.0f, 0.0f),
484 Graphic3d_Vec4 (0.0f, 0.0f, 0.0f, 0.0f));
485 anAspect->SetTextureMap (aTextureMap);
486 anAspect->SetInteriorStyle (Aspect_IS_SOLID);
487 anAspect->SetSuppressBackFaces (false);
488 // Enable texture mapping
489 if (aTextureMap->IsDone())
491 anAspect->SetTextureMapOn();
495 anAspect->SetTextureMapOff();
499 // Set texture parameters
500 myTextureParams->SetAspect (anAspect);
503 // =======================================================================
504 // function : BackgroundImageStyle
506 // =======================================================================
507 Aspect_FillMethod OpenGl_View::BackgroundImageStyle() const
509 return myBgTextureArray->TextureFillMethod();
512 // =======================================================================
513 // function : SetBackgroundImageStyle
515 // =======================================================================
516 void OpenGl_View::SetBackgroundImageStyle (const Aspect_FillMethod theFillStyle)
518 myBgTextureArray->SetTextureFillMethod (theFillStyle);
521 //=======================================================================
522 //function : AddZLayer
524 //=======================================================================
525 void OpenGl_View::AddZLayer (const Graphic3d_ZLayerId theLayerId)
527 myZLayers.AddLayer (theLayerId);
530 //=======================================================================
531 //function : RemoveZLayer
533 //=======================================================================
534 void OpenGl_View::RemoveZLayer (const Graphic3d_ZLayerId theLayerId)
536 myZLayers.RemoveLayer (theLayerId);
539 //=======================================================================
540 //function : SetZLayerSettings
542 //=======================================================================
543 void OpenGl_View::SetZLayerSettings (const Graphic3d_ZLayerId theLayerId,
544 const Graphic3d_ZLayerSettings& theSettings)
546 myZLayers.SetLayerSettings (theLayerId, theSettings);
549 //=======================================================================
550 //function : ZLayerMax
552 //=======================================================================
553 Standard_Integer OpenGl_View::ZLayerMax() const
555 Standard_Integer aLayerMax = Graphic3d_ZLayerId_Default;
556 for (OpenGl_LayerSeqIds::Iterator aMapIt(myZLayers.LayerIDs()); aMapIt.More(); aMapIt.Next())
558 aLayerMax = Max (aLayerMax, aMapIt.Value());
564 //=======================================================================
565 //function : InvalidateZLayerBoundingBox
567 //=======================================================================
568 void OpenGl_View::InvalidateZLayerBoundingBox (const Graphic3d_ZLayerId theLayerId) const
570 if (myZLayers.LayerIDs().IsBound (theLayerId))
572 myZLayers.Layer (theLayerId).InvalidateBoundingBox();
576 const Standard_Integer aLayerMax = ZLayerMax();
577 for (Standard_Integer aLayerId = Graphic3d_ZLayerId_Default; aLayerId < aLayerMax; ++aLayerId)
579 if (myZLayers.LayerIDs().IsBound (aLayerId))
581 const OpenGl_Layer& aLayer = myZLayers.Layer (aLayerId);
582 if (aLayer.NbOfTransformPersistenceObjects() > 0)
584 aLayer.InvalidateBoundingBox();
591 //=======================================================================
592 //function : ZLayerBoundingBox
594 //=======================================================================
595 Bnd_Box OpenGl_View::ZLayerBoundingBox (const Graphic3d_ZLayerId theLayerId,
596 const Handle(Graphic3d_Camera)& theCamera,
597 const Standard_Integer theWindowWidth,
598 const Standard_Integer theWindowHeight,
599 const Standard_Boolean theToIncludeAuxiliary) const
602 if (myZLayers.LayerIDs().IsBound (theLayerId))
604 aBox = myZLayers.Layer (theLayerId).BoundingBox (Identification(),
608 theToIncludeAuxiliary);
611 // add bounding box of gradient/texture background for proper Z-fit
612 if (theToIncludeAuxiliary
613 && theLayerId == Graphic3d_ZLayerId_BotOSD
614 && (myBgTextureArray->IsDefined()
615 || myBgGradientArray->IsDefined()))
617 // Background is drawn using 2D transformation persistence
618 // (e.g. it is actually placed in 3D coordinates within active camera position).
619 // We add here full-screen plane with 2D transformation persistence
620 // for simplicity (myBgTextureArray might define a little bit different options
621 // but it is updated within ::Render())
622 const Graphic3d_Mat4d& aProjectionMat = theCamera->ProjectionMatrix();
623 const Graphic3d_Mat4d& aWorldViewMat = theCamera->OrientationMatrix();
624 Graphic3d_BndBox3d aBox2d (Graphic3d_Vec3d (0.0, 0.0, 0.0),
625 Graphic3d_Vec3d (double(theWindowWidth), double(theWindowHeight), 0.0));
627 Graphic3d_TransformPers aTrsfPers (Graphic3d_TMF_2d, Aspect_TOTP_LEFT_LOWER);
628 aTrsfPers.Apply (theCamera,
634 aBox.Add (gp_Pnt (aBox2d.CornerMin().x(), aBox2d.CornerMin().y(), aBox2d.CornerMin().z()));
635 aBox.Add (gp_Pnt (aBox2d.CornerMax().x(), aBox2d.CornerMax().y(), aBox2d.CornerMax().z()));
641 //=======================================================================
642 //function : considerZoomPersistenceObjects
644 //=======================================================================
645 Standard_Real OpenGl_View::considerZoomPersistenceObjects (const Graphic3d_ZLayerId theLayerId,
646 const Handle(Graphic3d_Camera)& theCamera,
647 const Standard_Integer theWindowWidth,
648 const Standard_Integer theWindowHeight) const
650 if (myZLayers.LayerIDs().IsBound (theLayerId))
652 return myZLayers.Layer (theLayerId).considerZoomPersistenceObjects (Identification(),
661 //=======================================================================
664 //=======================================================================
665 Handle(Standard_Transient) OpenGl_View::FBO() const
667 return Handle(Standard_Transient)(myFBO);
670 //=======================================================================
673 //=======================================================================
674 void OpenGl_View::SetFBO (const Handle(Standard_Transient)& theFbo)
676 myFBO = Handle(OpenGl_FrameBuffer)::DownCast (theFbo);
679 //=======================================================================
680 //function : FBOCreate
682 //=======================================================================
683 Handle(Standard_Transient) OpenGl_View::FBOCreate (const Standard_Integer theWidth,
684 const Standard_Integer theHeight)
686 return myWorkspace->FBOCreate (theWidth, theHeight);
689 //=======================================================================
690 //function : FBORelease
692 //=======================================================================
693 void OpenGl_View::FBORelease (Handle(Standard_Transient)& theFbo)
695 Handle(OpenGl_FrameBuffer) aFrameBuffer = Handle(OpenGl_FrameBuffer)::DownCast (theFbo);
696 if (aFrameBuffer.IsNull())
701 myWorkspace->FBORelease (aFrameBuffer);
705 //=======================================================================
706 //function : FBOGetDimensions
708 //=======================================================================
709 void OpenGl_View::FBOGetDimensions (const Handle(Standard_Transient)& theFbo,
710 Standard_Integer& theWidth,
711 Standard_Integer& theHeight,
712 Standard_Integer& theWidthMax,
713 Standard_Integer& theHeightMax)
715 const Handle(OpenGl_FrameBuffer) aFrameBuffer = Handle(OpenGl_FrameBuffer)::DownCast (theFbo);
716 if (aFrameBuffer.IsNull())
721 theWidth = aFrameBuffer->GetVPSizeX(); // current viewport size
722 theHeight = aFrameBuffer->GetVPSizeY();
723 theWidthMax = aFrameBuffer->GetSizeX(); // texture size
724 theHeightMax = aFrameBuffer->GetSizeY();
727 //=======================================================================
728 //function : FBOChangeViewport
730 //=======================================================================
731 void OpenGl_View::FBOChangeViewport (const Handle(Standard_Transient)& theFbo,
732 const Standard_Integer theWidth,
733 const Standard_Integer theHeight)
735 const Handle(OpenGl_FrameBuffer) aFrameBuffer = Handle(OpenGl_FrameBuffer)::DownCast (theFbo);
736 if (aFrameBuffer.IsNull())
741 aFrameBuffer->ChangeViewport (theWidth, theHeight);
744 // =======================================================================
747 // =======================================================================
749 Standard_Boolean OpenGl_View::Export (const Standard_CString theFileName,
750 const Graphic3d_ExportFormat theFormat,
751 const Graphic3d_SortType theSortType)
753 // gl2psBeginPage() will call OpenGL functions
754 // so we should activate correct GL context before redraw scene call
755 if (!myWorkspace->Activate())
757 return Standard_False;
760 Standard_Integer aFormat = -1;
761 Standard_Integer aSortType = Graphic3d_ST_BSP_Tree;
764 case Graphic3d_EF_PostScript:
767 case Graphic3d_EF_EnhPostScript:
770 case Graphic3d_EF_TEX:
773 case Graphic3d_EF_PDF:
776 case Graphic3d_EF_SVG:
779 case Graphic3d_EF_PGF:
782 case Graphic3d_EF_EMF:
783 //aFormat = GL2PS_EMF;
784 aFormat = GL2PS_PGF + 1; // 6
787 // unsupported format
788 return Standard_False;
793 case Graphic3d_ST_Simple:
794 aSortType = GL2PS_SIMPLE_SORT;
796 case Graphic3d_ST_BSP_Tree:
797 aSortType = GL2PS_BSP_SORT;
804 aViewport[2] = myWindow->Width();
805 aViewport[3] = myWindow->Height();
807 GLint aBufferSize = 1024 * 1024;
808 GLint anErrCode = GL2PS_SUCCESS;
810 // gl2ps uses standard write functions and do not check locale
811 Standard_CLocaleSentry aLocaleSentry;
813 while (aBufferSize > 0)
815 // current patch for EMF support in gl2ps uses WinAPI functions to create file
816 FILE* aFileH = (theFormat != Graphic3d_EF_EMF) ? fopen (theFileName, "wb") : NULL;
817 anErrCode = gl2psBeginPage ("", "", aViewport, aFormat, aSortType,
818 GL2PS_DRAW_BACKGROUND | GL2PS_OCCLUSION_CULL | GL2PS_BEST_ROOT/* | GL2PS_SIMPLE_LINE_OFFSET*/,
820 0, 0, 0, aBufferSize, aFileH, theFileName);
821 if (anErrCode != GL2PS_SUCCESS)
823 // initialization failed
830 anErrCode = gl2psEndPage();
834 if (anErrCode == GL2PS_OVERFLOW)
839 return anErrCode == GL2PS_SUCCESS;
842 Standard_Boolean OpenGl_View::Export (const Standard_CString /*theFileName*/,
843 const Graphic3d_ExportFormat /*theFormat*/,
844 const Graphic3d_SortType /*theSortType*/)
846 return Standard_False;
850 //=======================================================================
851 //function : displayStructure
853 //=======================================================================
854 void OpenGl_View::displayStructure (const Handle(Graphic3d_CStructure)& theStructure,
855 const Standard_Integer thePriority)
857 const OpenGl_Structure* aStruct = reinterpret_cast<const OpenGl_Structure*> (theStructure.operator->());
858 const Graphic3d_ZLayerId aZLayer = aStruct->ZLayer();
859 myZLayers.AddStructure (aStruct, aZLayer, thePriority);
862 //=======================================================================
863 //function : eraseStructure
865 //=======================================================================
866 void OpenGl_View::eraseStructure (const Handle(Graphic3d_CStructure)& theStructure)
868 const OpenGl_Structure* aStruct = reinterpret_cast<const OpenGl_Structure*> (theStructure.operator->());
869 myZLayers.RemoveStructure (aStruct);
872 //=======================================================================
873 //function : changeZLayer
875 //=======================================================================
876 void OpenGl_View::changeZLayer (const Handle(Graphic3d_CStructure)& theStructure,
877 const Graphic3d_ZLayerId theNewLayerId)
879 const Graphic3d_ZLayerId anOldLayer = theStructure->ZLayer();
880 const OpenGl_Structure* aStruct = reinterpret_cast<const OpenGl_Structure*> (theStructure.operator->());
881 myZLayers.ChangeLayer (aStruct, anOldLayer, theNewLayerId);
883 Update (theNewLayerId);
886 //=======================================================================
887 //function : changePriority
889 //=======================================================================
890 void OpenGl_View::changePriority (const Handle(Graphic3d_CStructure)& theStructure,
891 const Standard_Integer theNewPriority)
893 const Graphic3d_ZLayerId aLayerId = theStructure->ZLayer();
894 const OpenGl_Structure* aStruct = reinterpret_cast<const OpenGl_Structure*> (theStructure.operator->());
895 myZLayers.ChangePriority (aStruct, aLayerId, theNewPriority);
898 //=======================================================================
899 //function : DiagnosticInformation
901 //=======================================================================
902 void OpenGl_View::DiagnosticInformation (TColStd_IndexedDataMapOfStringString& theDict,
903 Graphic3d_DiagnosticInfo theFlags) const
905 Handle(OpenGl_Context) aCtx = myWorkspace->GetGlContext();
906 if (!myWorkspace->Activate()
912 aCtx->DiagnosticInformation (theDict, theFlags);
913 if ((theFlags & Graphic3d_DiagnosticInfo_FrameBuffer) != 0)
915 TCollection_AsciiString aResRatio (myRenderParams.ResolutionRatio());
916 theDict.ChangeFromIndex (theDict.Add ("ResolutionRatio", aResRatio)) = aResRatio;