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->());
145 if (!myTextureEnv.IsNull())
147 for (OpenGl_TextureSet::Iterator aTextureIter (myTextureEnv); aTextureIter.More(); aTextureIter.Next())
149 theCtx->DelayedRelease (aTextureIter.ChangeValue());
150 aTextureIter.ChangeValue().Nullify();
152 myTextureEnv.Nullify();
155 if (myTextureParams != NULL)
157 myTextureParams->Release (theCtx.operator->());
159 if (myBgGradientArray != NULL)
161 myBgGradientArray->Release (theCtx.operator->());
163 if (myBgTextureArray != NULL)
165 myBgTextureArray->Release (theCtx.operator->());
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->());
181 releaseRaytraceResources (theCtx);
184 // =======================================================================
187 // =======================================================================
188 void OpenGl_View::Remove()
195 myDriver->RemoveView (this);
198 Graphic3d_CView::Remove();
201 // =======================================================================
202 // function : SetTextureEnv
204 // =======================================================================
205 void OpenGl_View::SetCamera(const Handle(Graphic3d_Camera)& theCamera)
207 myCamera = theCamera;
210 // =======================================================================
211 // function : SetLocalOrigin
213 // =======================================================================
214 void OpenGl_View::SetLocalOrigin (const gp_XYZ& theOrigin)
216 myLocalOrigin = theOrigin;
217 const Handle(OpenGl_Context)& aCtx = myWorkspace->GetGlContext();
220 aCtx->ShaderManager()->SetLocalOrigin (theOrigin);
224 // =======================================================================
225 // function : SetTextureEnv
227 // =======================================================================
228 void OpenGl_View::SetTextureEnv (const Handle(Graphic3d_TextureEnv)& theTextureEnv)
230 Handle(OpenGl_Context) aCtx = myWorkspace->GetGlContext();
231 if (!aCtx.IsNull() && !myTextureEnv.IsNull())
233 for (OpenGl_TextureSet::Iterator aTextureIter (myTextureEnv); aTextureIter.More(); aTextureIter.Next())
235 aCtx->DelayedRelease (aTextureIter.ChangeValue());
236 aTextureIter.ChangeValue().Nullify();
240 myToUpdateEnvironmentMap = Standard_True;
241 myTextureEnvData = theTextureEnv;
242 myTextureEnv.Nullify();
243 initTextureEnv (aCtx);
246 // =======================================================================
247 // function : initTextureEnv
249 // =======================================================================
250 void OpenGl_View::initTextureEnv (const Handle(OpenGl_Context)& theContext)
252 if (myTextureEnvData.IsNull()
253 || theContext.IsNull()
254 || !theContext->MakeCurrent())
259 myTextureEnv = new OpenGl_TextureSet (1);
260 Handle(OpenGl_Texture)& aTextureEnv = myTextureEnv->ChangeFirst();
261 aTextureEnv = new OpenGl_Texture (myTextureEnvData->GetId(), myTextureEnvData->GetParams());
262 Handle(Image_PixMap) anImage = myTextureEnvData->GetImage();
263 if (!anImage.IsNull())
265 aTextureEnv->Init (theContext, *anImage.operator->(), myTextureEnvData->Type());
269 // =======================================================================
270 // function : SetImmediateModeDrawToFront
272 // =======================================================================
273 Standard_Boolean OpenGl_View::SetImmediateModeDrawToFront (const Standard_Boolean theDrawToFrontBuffer)
275 const Standard_Boolean aPrevMode = myTransientDrawToFront;
276 myTransientDrawToFront = theDrawToFrontBuffer;
280 // =======================================================================
281 // function : SetWindow
283 // =======================================================================
284 void OpenGl_View::SetWindow (const Handle(Aspect_Window)& theWindow,
285 const Aspect_RenderingContext theContext)
287 myWindow = myDriver->CreateRenderWindow (theWindow, theContext);
288 Standard_ASSERT_RAISE (!myWindow.IsNull(),
289 "OpenGl_View::SetWindow, "
290 "Failed to create OpenGl window.");
292 myWorkspace = new OpenGl_Workspace (this, myWindow);
293 myWorldViewProjState.Reset();
294 myToUpdateEnvironmentMap = Standard_True;
295 myHasFboBlit = Standard_True;
298 // Environment texture resource does not support lazy initialization.
299 initTextureEnv (myWorkspace->GetGlContext());
302 // =======================================================================
303 // function : Resized
305 // =======================================================================
306 void OpenGl_View::Resized()
308 if (myWindow.IsNull())
314 // =======================================================================
315 // function : SetMinMaxValuesCallback
317 // =======================================================================
318 static void SetMinMaxValuesCallback (Graphic3d_CView* theView)
320 OpenGl_View* aView = dynamic_cast<OpenGl_View*>(theView);
324 Bnd_Box aBox = theView->MinMaxValues();
327 gp_Pnt aMin = aBox.CornerMin();
328 gp_Pnt aMax = aBox.CornerMax();
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);
336 // =======================================================================
337 // function : GraduatedTrihedronDisplay
339 // =======================================================================
340 void OpenGl_View::GraduatedTrihedronDisplay (const Graphic3d_GraduatedTrihedron& theTrihedronData)
342 myGTrihedronData = theTrihedronData;
343 myGTrihedronData.PtrView = this;
344 myGTrihedronData.CubicAxesCallback = SetMinMaxValuesCallback;
345 myGraduatedTrihedron.SetValues (myGTrihedronData);
346 myToShowGradTrihedron = true;
349 // =======================================================================
350 // function : GraduatedTrihedronErase
352 // =======================================================================
353 void OpenGl_View::GraduatedTrihedronErase()
355 myGTrihedronData.PtrView = NULL;
356 myGraduatedTrihedron.Release (myWorkspace->GetGlContext().operator->());
357 myToShowGradTrihedron = false;
360 // =======================================================================
361 // function : GraduatedTrihedronMinMaxValues
363 // =======================================================================
364 void OpenGl_View::GraduatedTrihedronMinMaxValues (const Graphic3d_Vec3 theMin, const Graphic3d_Vec3 theMax)
366 myGraduatedTrihedron.SetMinMax (theMin, theMax);
369 // =======================================================================
370 // function : BufferDump
372 // =======================================================================
373 Standard_Boolean OpenGl_View::BufferDump (Image_PixMap& theImage, const Graphic3d_BufferType& theBufferType)
375 if (theBufferType != Graphic3d_BT_RGB_RayTraceHdrLeft)
377 return myWorkspace->BufferDump(myFBO, theImage, theBufferType);
380 if (!myRaytraceParameters.AdaptiveScreenSampling)
382 return myWorkspace->BufferDump(myAccumFrames % 2 ? myRaytraceFBO2[0] : myRaytraceFBO1[0], theImage, theBufferType);
385 #if defined(GL_ES_VERSION_2_0)
388 if (theImage.Format() != Image_Format_RGBF)
393 const GLuint aW = myRaytraceOutputTexture[0]->SizeX();
394 const GLuint aH = myRaytraceOutputTexture[0]->SizeY();
395 if (aW / 3 != theImage.SizeX() || aH / 2 != theImage.SizeY())
400 std::vector<GLfloat> aValues;
403 aValues.resize (aW * aH);
405 catch (const std::bad_alloc&)
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)
415 for (unsigned int aCol = 0; aCol < aW; aCol += 3)
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;
429 // =======================================================================
430 // function : Background
432 // =======================================================================
433 Aspect_Background OpenGl_View::Background() const
435 return Aspect_Background (myBgColor.GetRGB());
438 // =======================================================================
439 // function : SetBackground
441 // =======================================================================
442 void OpenGl_View::SetBackground (const Aspect_Background& theBackground)
444 myBgColor.SetRGB (theBackground.Color());
447 // =======================================================================
448 // function : GradientBackground
450 // =======================================================================
451 Aspect_GradientBackground OpenGl_View::GradientBackground() const
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());
463 // =======================================================================
464 // function : SetGradientBackground
466 // =======================================================================
467 void OpenGl_View::SetGradientBackground (const Aspect_GradientBackground& theBackground)
469 Quantity_Color aColor1, aColor2;
470 theBackground.Colors (aColor1, aColor2);
471 myBgGradientArray->SetGradientParameters (aColor1, aColor2, theBackground.BgGradientFillMethod());
474 // =======================================================================
475 // function : SetBackgroundImage
477 // =======================================================================
478 void OpenGl_View::SetBackgroundImage (const TCollection_AsciiString& theFilePath)
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);
491 anAspect->SetSuppressBackFaces (false);
492 // Enable texture mapping
493 if (aTextureMap->IsDone())
495 anAspect->SetTextureMapOn();
499 anAspect->SetTextureMapOff();
503 // Set texture parameters
504 myTextureParams->SetAspect (anAspect);
507 // =======================================================================
508 // function : BackgroundImageStyle
510 // =======================================================================
511 Aspect_FillMethod OpenGl_View::BackgroundImageStyle() const
513 return myBgTextureArray->TextureFillMethod();
516 // =======================================================================
517 // function : SetBackgroundImageStyle
519 // =======================================================================
520 void OpenGl_View::SetBackgroundImageStyle (const Aspect_FillMethod theFillStyle)
522 myBgTextureArray->SetTextureFillMethod (theFillStyle);
525 //=======================================================================
526 //function : AddZLayer
528 //=======================================================================
529 void OpenGl_View::AddZLayer (const Graphic3d_ZLayerId theLayerId)
531 myZLayers.AddLayer (theLayerId);
534 //=======================================================================
535 //function : RemoveZLayer
537 //=======================================================================
538 void OpenGl_View::RemoveZLayer (const Graphic3d_ZLayerId theLayerId)
540 myZLayers.RemoveLayer (theLayerId);
543 //=======================================================================
544 //function : SetZLayerSettings
546 //=======================================================================
547 void OpenGl_View::SetZLayerSettings (const Graphic3d_ZLayerId theLayerId,
548 const Graphic3d_ZLayerSettings& theSettings)
550 myZLayers.SetLayerSettings (theLayerId, theSettings);
553 //=======================================================================
554 //function : ZLayerMax
556 //=======================================================================
557 Standard_Integer OpenGl_View::ZLayerMax() const
559 Standard_Integer aLayerMax = Graphic3d_ZLayerId_Default;
560 for (OpenGl_LayerSeqIds::Iterator aMapIt(myZLayers.LayerIDs()); aMapIt.More(); aMapIt.Next())
562 aLayerMax = Max (aLayerMax, aMapIt.Value());
568 //=======================================================================
569 //function : InvalidateZLayerBoundingBox
571 //=======================================================================
572 void OpenGl_View::InvalidateZLayerBoundingBox (const Graphic3d_ZLayerId theLayerId) const
574 if (myZLayers.LayerIDs().IsBound (theLayerId))
576 myZLayers.Layer (theLayerId).InvalidateBoundingBox();
580 const Standard_Integer aLayerMax = ZLayerMax();
581 for (Standard_Integer aLayerId = Graphic3d_ZLayerId_Default; aLayerId < aLayerMax; ++aLayerId)
583 if (myZLayers.LayerIDs().IsBound (aLayerId))
585 const OpenGl_Layer& aLayer = myZLayers.Layer (aLayerId);
586 if (aLayer.NbOfTransformPersistenceObjects() > 0)
588 aLayer.InvalidateBoundingBox();
595 //=======================================================================
596 //function : ZLayerBoundingBox
598 //=======================================================================
599 Bnd_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
606 if (myZLayers.LayerIDs().IsBound (theLayerId))
608 aBox = myZLayers.Layer (theLayerId).BoundingBox (Identification(),
612 theToIncludeAuxiliary);
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()))
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())
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));
631 Graphic3d_TransformPers aTrsfPers (Graphic3d_TMF_2d, Aspect_TOTP_LEFT_LOWER);
632 aTrsfPers.Apply (theCamera,
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()));
645 //=======================================================================
646 //function : considerZoomPersistenceObjects
648 //=======================================================================
649 Standard_Real OpenGl_View::considerZoomPersistenceObjects (const Graphic3d_ZLayerId theLayerId,
650 const Handle(Graphic3d_Camera)& theCamera,
651 const Standard_Integer theWindowWidth,
652 const Standard_Integer theWindowHeight) const
654 if (myZLayers.LayerIDs().IsBound (theLayerId))
656 return myZLayers.Layer (theLayerId).considerZoomPersistenceObjects (Identification(),
665 //=======================================================================
668 //=======================================================================
669 Handle(Standard_Transient) OpenGl_View::FBO() const
671 return Handle(Standard_Transient)(myFBO);
674 //=======================================================================
677 //=======================================================================
678 void OpenGl_View::SetFBO (const Handle(Standard_Transient)& theFbo)
680 myFBO = Handle(OpenGl_FrameBuffer)::DownCast (theFbo);
683 //=======================================================================
684 //function : FBOCreate
686 //=======================================================================
687 Handle(Standard_Transient) OpenGl_View::FBOCreate (const Standard_Integer theWidth,
688 const Standard_Integer theHeight)
690 return myWorkspace->FBOCreate (theWidth, theHeight);
693 //=======================================================================
694 //function : FBORelease
696 //=======================================================================
697 void OpenGl_View::FBORelease (Handle(Standard_Transient)& theFbo)
699 Handle(OpenGl_FrameBuffer) aFrameBuffer = Handle(OpenGl_FrameBuffer)::DownCast (theFbo);
700 if (aFrameBuffer.IsNull())
705 myWorkspace->FBORelease (aFrameBuffer);
709 //=======================================================================
710 //function : FBOGetDimensions
712 //=======================================================================
713 void OpenGl_View::FBOGetDimensions (const Handle(Standard_Transient)& theFbo,
714 Standard_Integer& theWidth,
715 Standard_Integer& theHeight,
716 Standard_Integer& theWidthMax,
717 Standard_Integer& theHeightMax)
719 const Handle(OpenGl_FrameBuffer) aFrameBuffer = Handle(OpenGl_FrameBuffer)::DownCast (theFbo);
720 if (aFrameBuffer.IsNull())
725 theWidth = aFrameBuffer->GetVPSizeX(); // current viewport size
726 theHeight = aFrameBuffer->GetVPSizeY();
727 theWidthMax = aFrameBuffer->GetSizeX(); // texture size
728 theHeightMax = aFrameBuffer->GetSizeY();
731 //=======================================================================
732 //function : FBOChangeViewport
734 //=======================================================================
735 void OpenGl_View::FBOChangeViewport (const Handle(Standard_Transient)& theFbo,
736 const Standard_Integer theWidth,
737 const Standard_Integer theHeight)
739 const Handle(OpenGl_FrameBuffer) aFrameBuffer = Handle(OpenGl_FrameBuffer)::DownCast (theFbo);
740 if (aFrameBuffer.IsNull())
745 aFrameBuffer->ChangeViewport (theWidth, theHeight);
748 // =======================================================================
751 // =======================================================================
753 Standard_Boolean OpenGl_View::Export (const Standard_CString theFileName,
754 const Graphic3d_ExportFormat theFormat,
755 const Graphic3d_SortType theSortType)
757 // gl2psBeginPage() will call OpenGL functions
758 // so we should activate correct GL context before redraw scene call
759 if (!myWorkspace->Activate())
761 return Standard_False;
764 Standard_Integer aFormat = -1;
765 Standard_Integer aSortType = Graphic3d_ST_BSP_Tree;
768 case Graphic3d_EF_PostScript:
771 case Graphic3d_EF_EnhPostScript:
774 case Graphic3d_EF_TEX:
777 case Graphic3d_EF_PDF:
780 case Graphic3d_EF_SVG:
783 case Graphic3d_EF_PGF:
786 case Graphic3d_EF_EMF:
787 //aFormat = GL2PS_EMF;
788 aFormat = GL2PS_PGF + 1; // 6
791 // unsupported format
792 return Standard_False;
797 case Graphic3d_ST_Simple:
798 aSortType = GL2PS_SIMPLE_SORT;
800 case Graphic3d_ST_BSP_Tree:
801 aSortType = GL2PS_BSP_SORT;
808 aViewport[2] = myWindow->Width();
809 aViewport[3] = myWindow->Height();
811 GLint aBufferSize = 1024 * 1024;
812 GLint anErrCode = GL2PS_SUCCESS;
814 // gl2ps uses standard write functions and do not check locale
815 Standard_CLocaleSentry aLocaleSentry;
817 while (aBufferSize > 0)
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*/,
824 0, 0, 0, aBufferSize, aFileH, theFileName);
825 if (anErrCode != GL2PS_SUCCESS)
827 // initialization failed
834 anErrCode = gl2psEndPage();
838 if (anErrCode == GL2PS_OVERFLOW)
843 return anErrCode == GL2PS_SUCCESS;
846 Standard_Boolean OpenGl_View::Export (const Standard_CString /*theFileName*/,
847 const Graphic3d_ExportFormat /*theFormat*/,
848 const Graphic3d_SortType /*theSortType*/)
850 return Standard_False;
854 //=======================================================================
855 //function : displayStructure
857 //=======================================================================
858 void OpenGl_View::displayStructure (const Handle(Graphic3d_CStructure)& theStructure,
859 const Standard_Integer thePriority)
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);
866 //=======================================================================
867 //function : eraseStructure
869 //=======================================================================
870 void OpenGl_View::eraseStructure (const Handle(Graphic3d_CStructure)& theStructure)
872 const OpenGl_Structure* aStruct = reinterpret_cast<const OpenGl_Structure*> (theStructure.operator->());
873 myZLayers.RemoveStructure (aStruct);
876 //=======================================================================
877 //function : changeZLayer
879 //=======================================================================
880 void OpenGl_View::changeZLayer (const Handle(Graphic3d_CStructure)& theStructure,
881 const Graphic3d_ZLayerId theNewLayerId)
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);
887 Update (theNewLayerId);
890 //=======================================================================
891 //function : changePriority
893 //=======================================================================
894 void OpenGl_View::changePriority (const Handle(Graphic3d_CStructure)& theStructure,
895 const Standard_Integer theNewPriority)
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);
902 //=======================================================================
903 //function : DiagnosticInformation
905 //=======================================================================
906 void OpenGl_View::DiagnosticInformation (TColStd_IndexedDataMapOfStringString& theDict,
907 Graphic3d_DiagnosticInfo theFlags) const
909 Handle(OpenGl_Context) aCtx = myWorkspace->GetGlContext();
910 if (!myWorkspace->Activate()
916 aCtx->DiagnosticInformation (theDict, theFlags);
917 if ((theFlags & Graphic3d_DiagnosticInfo_FrameBuffer) != 0)
919 TCollection_AsciiString aResRatio (myRenderParams.ResolutionRatio());
920 theDict.ChangeFromIndex (theDict.Add ("ResolutionRatio", aResRatio)) = aResRatio;