0028030: Algorith GeomLib_CheckCurveOnSurface takes too much time for Bspline curves...
[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 {
496 for (Standard_Integer aLayerId = Graphic3d_ZLayerId_Default; aLayerId < ZLayerMax(); ++aLayerId)
497 {
498 if (myZLayers.LayerIDs().IsBound (aLayerId))
499 {
500 const OpenGl_Layer& aLayer = myZLayers.Layer (aLayerId);
501 if (aLayer.NbOfTransformPersistenceObjects() > 0)
502 {
503 aLayer.InvalidateBoundingBox();
504 }
505 }
506 }
507 }
508}
509
510//=======================================================================
511//function : ZLayerBoundingBox
512//purpose :
513//=======================================================================
7c3ef2f7 514Bnd_Box OpenGl_View::ZLayerBoundingBox (const Graphic3d_ZLayerId theLayerId,
515 const Handle(Graphic3d_Camera)& theCamera,
516 const Standard_Integer theWindowWidth,
517 const Standard_Integer theWindowHeight,
518 const Standard_Boolean theToIncludeAuxiliary) const
50d06d8f 519{
7c3ef2f7 520 Bnd_Box aBox;
50d06d8f 521 if (myZLayers.LayerIDs().IsBound (theLayerId))
522 {
71d8ccc7 523 aBox = myZLayers.Layer (theLayerId).BoundingBox (Identification(),
50d06d8f 524 theCamera,
525 theWindowWidth,
526 theWindowHeight,
3fe9ce0e 527 theToIncludeAuxiliary);
50d06d8f 528 }
529
71d8ccc7 530 // add bounding box of gradient/texture background for proper Z-fit
531 if (theToIncludeAuxiliary
532 && theLayerId == Graphic3d_ZLayerId_BotOSD
533 && (myBgTextureArray->IsDefined()
534 || myBgGradientArray->IsDefined()))
535 {
536 // Background is drawn using 2D transformation persistence
537 // (e.g. it is actually placed in 3D coordinates within active camera position).
538 // We add here full-screen plane with 2D transformation persistence
539 // for simplicity (myBgTextureArray might define a little bit different options
540 // but it is updated within ::Render())
7c3ef2f7 541 const Graphic3d_Mat4d& aProjectionMat = theCamera->ProjectionMatrix();
542 const Graphic3d_Mat4d& aWorldViewMat = theCamera->OrientationMatrix();
543 Graphic3d_BndBox3d aBox2d (Graphic3d_Vec3d (0.0, 0.0, 0.0),
544 Graphic3d_Vec3d (double(theWindowWidth), double(theWindowHeight), 0.0));
71d8ccc7 545
778cd667 546 Graphic3d_TransformPers aTrsfPers (Graphic3d_TMF_2d, Aspect_TOTP_LEFT_LOWER);
71d8ccc7 547 aTrsfPers.Apply (theCamera,
548 aProjectionMat,
549 aWorldViewMat,
550 theWindowWidth,
551 theWindowHeight,
552 aBox2d);
7c3ef2f7 553 aBox.Add (gp_Pnt (aBox2d.CornerMin().x(), aBox2d.CornerMin().y(), aBox2d.CornerMin().z()));
554 aBox.Add (gp_Pnt (aBox2d.CornerMax().x(), aBox2d.CornerMax().y(), aBox2d.CornerMax().z()));
71d8ccc7 555 }
556
557 return aBox;
50d06d8f 558}
559
560//=======================================================================
561//function : considerZoomPersistenceObjects
562//purpose :
563//=======================================================================
564Standard_Real OpenGl_View::considerZoomPersistenceObjects (const Graphic3d_ZLayerId theLayerId,
565 const Handle(Graphic3d_Camera)& theCamera,
566 const Standard_Integer theWindowWidth,
3fe9ce0e 567 const Standard_Integer theWindowHeight) const
50d06d8f 568{
1d92133e 569 if (myZLayers.LayerIDs().IsBound (theLayerId))
50d06d8f 570 {
571 return myZLayers.Layer (theLayerId).considerZoomPersistenceObjects (Identification(),
572 theCamera,
573 theWindowWidth,
3fe9ce0e 574 theWindowHeight);
50d06d8f 575 }
576
577 return 1.0;
578}
579
580//=======================================================================
c357e426 581//function : FBO
582//purpose :
583//=======================================================================
b128c892 584Handle(Standard_Transient) OpenGl_View::FBO() const
c357e426 585{
b128c892 586 return Handle(Standard_Transient)(myFBO);
c357e426 587}
2166f0fa 588
c357e426 589//=======================================================================
590//function : SetFBO
591//purpose :
592//=======================================================================
b128c892 593void OpenGl_View::SetFBO (const Handle(Standard_Transient)& theFbo)
2166f0fa 594{
b128c892 595 myFBO = Handle(OpenGl_FrameBuffer)::DownCast (theFbo);
2166f0fa
SK
596}
597
c357e426 598//=======================================================================
599//function : FBOCreate
600//purpose :
601//=======================================================================
b128c892 602Handle(Standard_Transient) OpenGl_View::FBOCreate (const Standard_Integer theWidth,
603 const Standard_Integer theHeight)
c357e426 604{
605 return myWorkspace->FBOCreate (theWidth, theHeight);
606}
2166f0fa 607
c357e426 608//=======================================================================
609//function : FBORelease
610//purpose :
611//=======================================================================
b128c892 612void OpenGl_View::FBORelease (Handle(Standard_Transient)& theFbo)
2166f0fa 613{
b128c892 614 Handle(OpenGl_FrameBuffer) aFrameBuffer = Handle(OpenGl_FrameBuffer)::DownCast (theFbo);
615 if (aFrameBuffer.IsNull())
616 {
617 return;
618 }
619
620 myWorkspace->FBORelease (aFrameBuffer);
621 theFbo.Nullify();
c357e426 622}
623
624//=======================================================================
625//function : FBOGetDimensions
626//purpose :
627//=======================================================================
b128c892 628void OpenGl_View::FBOGetDimensions (const Handle(Standard_Transient)& theFbo,
c357e426 629 Standard_Integer& theWidth,
630 Standard_Integer& theHeight,
631 Standard_Integer& theWidthMax,
632 Standard_Integer& theHeightMax)
633{
b128c892 634 const Handle(OpenGl_FrameBuffer) aFrameBuffer = Handle(OpenGl_FrameBuffer)::DownCast (theFbo);
635 if (aFrameBuffer.IsNull())
636 {
637 return;
638 }
639
c357e426 640 theWidth = aFrameBuffer->GetVPSizeX(); // current viewport size
641 theHeight = aFrameBuffer->GetVPSizeY();
642 theWidthMax = aFrameBuffer->GetSizeX(); // texture size
643 theHeightMax = aFrameBuffer->GetSizeY();
644}
645
646//=======================================================================
647//function : FBOChangeViewport
648//purpose :
649//=======================================================================
b128c892 650void OpenGl_View::FBOChangeViewport (const Handle(Standard_Transient)& theFbo,
c357e426 651 const Standard_Integer theWidth,
652 const Standard_Integer theHeight)
653{
b128c892 654 const Handle(OpenGl_FrameBuffer) aFrameBuffer = Handle(OpenGl_FrameBuffer)::DownCast (theFbo);
655 if (aFrameBuffer.IsNull())
656 {
657 return;
658 }
659
c357e426 660 aFrameBuffer->ChangeViewport (theWidth, theHeight);
661}
662
663// =======================================================================
664// function : Export
665// purpose :
666// =======================================================================
667#ifdef HAVE_GL2PS
668Standard_Boolean OpenGl_View::Export (const Standard_CString theFileName,
669 const Graphic3d_ExportFormat theFormat,
670 const Graphic3d_SortType theSortType)
671{
672 // gl2psBeginPage() will call OpenGL functions
673 // so we should activate correct GL context before redraw scene call
674 if (!myWorkspace->Activate())
675 {
676 return Standard_False;
677 }
678
679 Standard_Integer aFormat = -1;
680 Standard_Integer aSortType = Graphic3d_ST_BSP_Tree;
681 switch (theFormat)
682 {
683 case Graphic3d_EF_PostScript:
684 aFormat = GL2PS_PS;
685 break;
686 case Graphic3d_EF_EnhPostScript:
687 aFormat = GL2PS_EPS;
688 break;
689 case Graphic3d_EF_TEX:
690 aFormat = GL2PS_TEX;
691 break;
692 case Graphic3d_EF_PDF:
693 aFormat = GL2PS_PDF;
694 break;
695 case Graphic3d_EF_SVG:
696 aFormat = GL2PS_SVG;
697 break;
698 case Graphic3d_EF_PGF:
699 aFormat = GL2PS_PGF;
700 break;
701 case Graphic3d_EF_EMF:
702 //aFormat = GL2PS_EMF;
703 aFormat = GL2PS_PGF + 1; // 6
704 break;
705 default:
706 // unsupported format
707 return Standard_False;
708 }
709
710 switch (theSortType)
711 {
712 case Graphic3d_ST_Simple:
713 aSortType = GL2PS_SIMPLE_SORT;
714 break;
715 case Graphic3d_ST_BSP_Tree:
716 aSortType = GL2PS_BSP_SORT;
717 break;
718 }
719
720 GLint aViewport[4];
721 aViewport[0] = 0;
722 aViewport[1] = 0;
723 aViewport[2] = myWindow->Width();
724 aViewport[3] = myWindow->Height();
725
726 GLint aBufferSize = 1024 * 1024;
727 GLint anErrCode = GL2PS_SUCCESS;
728
729 // gl2ps uses standard write functions and do not check locale
730 Standard_CLocaleSentry aLocaleSentry;
731
732 while (aBufferSize > 0)
733 {
734 // current patch for EMF support in gl2ps uses WinAPI functions to create file
735 FILE* aFileH = (theFormat != Graphic3d_EF_EMF) ? fopen (theFileName, "wb") : NULL;
736 anErrCode = gl2psBeginPage ("", "", aViewport, aFormat, aSortType,
737 GL2PS_DRAW_BACKGROUND | GL2PS_OCCLUSION_CULL | GL2PS_BEST_ROOT/* | GL2PS_SIMPLE_LINE_OFFSET*/,
738 GL_RGBA, 0, NULL,
739 0, 0, 0, aBufferSize, aFileH, theFileName);
740 if (anErrCode != GL2PS_SUCCESS)
741 {
742 // initialization failed
743 if (aFileH != NULL)
744 fclose (aFileH);
745 break;
746 }
747 Redraw();
748
749 anErrCode = gl2psEndPage();
750 if (aFileH != NULL)
751 fclose (aFileH);
752
753 if (anErrCode == GL2PS_OVERFLOW)
754 aBufferSize *= 2;
755 else
756 break;
757 }
758 return anErrCode == GL2PS_SUCCESS;
759}
760#else
761Standard_Boolean OpenGl_View::Export (const Standard_CString /*theFileName*/,
762 const Graphic3d_ExportFormat /*theFormat*/,
763 const Graphic3d_SortType /*theSortType*/)
764{
765 return Standard_False;
766}
767#endif
768
769//=======================================================================
770//function : displayStructure
771//purpose :
772//=======================================================================
773void OpenGl_View::displayStructure (const Handle(Graphic3d_CStructure)& theStructure,
774 const Standard_Integer thePriority)
775{
776 const OpenGl_Structure* aStruct = reinterpret_cast<const OpenGl_Structure*> (theStructure.operator->());
777 const Graphic3d_ZLayerId aZLayer = aStruct->ZLayer();
778 myZLayers.AddStructure (aStruct, aZLayer, thePriority);
779}
780
781//=======================================================================
782//function : eraseStructure
783//purpose :
784//=======================================================================
785void OpenGl_View::eraseStructure (const Handle(Graphic3d_CStructure)& theStructure)
786{
787 const OpenGl_Structure* aStruct = reinterpret_cast<const OpenGl_Structure*> (theStructure.operator->());
788 myZLayers.RemoveStructure (aStruct);
789}
790
791//=======================================================================
792//function : changeZLayer
793//purpose :
794//=======================================================================
795void OpenGl_View::changeZLayer (const Handle(Graphic3d_CStructure)& theStructure,
796 const Graphic3d_ZLayerId theNewLayerId)
797{
798 const Graphic3d_ZLayerId anOldLayer = theStructure->ZLayer();
799 const OpenGl_Structure* aStruct = reinterpret_cast<const OpenGl_Structure*> (theStructure.operator->());
800 myZLayers.ChangeLayer (aStruct, anOldLayer, theNewLayerId);
50d06d8f 801 Update (Aspect_TOU_WAIT, anOldLayer);
802 Update (Aspect_TOU_WAIT, theNewLayerId);
c357e426 803}
804
805//=======================================================================
806//function : changePriority
807//purpose :
808//=======================================================================
809void OpenGl_View::changePriority (const Handle(Graphic3d_CStructure)& theStructure,
810 const Standard_Integer theNewPriority)
811{
812 const Graphic3d_ZLayerId aLayerId = theStructure->ZLayer();
813 const OpenGl_Structure* aStruct = reinterpret_cast<const OpenGl_Structure*> (theStructure.operator->());
814 myZLayers.ChangePriority (aStruct, aLayerId, theNewPriority);
2166f0fa 815}
26d9c835 816
817//=======================================================================
818//function : DiagnosticInformation
819//purpose :
820//=======================================================================
821void OpenGl_View::DiagnosticInformation (TColStd_IndexedDataMapOfStringString& theDict,
822 Graphic3d_DiagnosticInfo theFlags) const
823{
824 Handle(OpenGl_Context) aCtx = myWorkspace->GetGlContext();
825 if (!myWorkspace->Activate()
826 || aCtx.IsNull())
827 {
828 return;
829 }
830
831 aCtx->DiagnosticInformation (theDict, theFlags);
832 if ((theFlags & Graphic3d_DiagnosticInfo_FrameBuffer) != 0)
833 {
834 TCollection_AsciiString aResRatio (myRenderParams.ResolutionRatio());
835 theDict.ChangeFromIndex (theDict.Add ("ResolutionRatio", aResRatio)) = aResRatio;
836 }
837}