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