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