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