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