0026915: Visualization - return Handle from Graphic3d_CView::FBOCreate() instead...
[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>
2166f0fa 32#include <OpenGl_Trihedron.hxx>
c357e426 33#include <OpenGl_Window.hxx>
30f0ad28 34#include <OpenGl_Workspace.hxx>
c357e426 35#include <Standard_CLocaleSentry.hxx>
2166f0fa 36
c357e426 37#ifdef HAVE_GL2PS
38#include <gl2ps.h>
39#endif
2166f0fa
SK
40
41/*----------------------------------------------------------------------*/
42
f8ae3605 43namespace
c34dba32 44{
c357e426 45 static const OPENGL_ZCLIP myDefaultZClip = { { Standard_False, 0.F }, { Standard_False, 1.F } };
46 static const OPENGL_FOG myDefaultFog = { Standard_False, 0.F, 1.F, { { 0.F, 0.F, 0.F, 1.F } } };
47 static const TEL_COLOUR myDefaultBg = { { 0.F, 0.F, 0.F, 1.F } };
f8ae3605 48}
2166f0fa 49
c357e426 50// =======================================================================
51// function : Constructor
52// purpose :
53// =======================================================================
54OpenGl_View::OpenGl_View (const Handle(Graphic3d_StructureManager)& theMgr,
55 const Handle(OpenGl_GraphicDriver)& theDriver,
56 const Handle(OpenGl_Caps)& theCaps,
57 Standard_Boolean& theDeviceLostFlag,
58 OpenGl_StateCounter* theCounter)
59: Graphic3d_CView (theMgr),
60 myDriver (theDriver.operator->()),
61 myCaps (theCaps),
62 myDeviceLostFlag (theDeviceLostFlag),
63 myWasRedrawnGL (Standard_False),
64 myAntiAliasing (Standard_False),
65 myCulling (Standard_True),
66 myShadingModel (Graphic3d_TOSM_FACET),
67 mySurfaceDetail (Graphic3d_TOD_ALL),
68 myBackfacing (Graphic3d_TOBM_AUTOMATIC),
69 myBgColor (myDefaultBg),
70 myFog (myDefaultFog),
71 myZClip (myDefaultZClip),
72 myCamera (new Graphic3d_Camera()),
c357e426 73 myUseGLLight (Standard_True),
74 myToShowTrihedron (false),
75 myToShowGradTrihedron (false),
76 myStateCounter (theCounter),
0b0320e7 77 myLastLightSourceState (0, 0),
3c4b62a4 78 myFboColorFormat (GL_RGBA8),
79 myFboDepthFormat (GL_DEPTH24_STENCIL8),
80 myToFlipOutput (Standard_False),
c357e426 81 myFrameCounter (0),
82 myHasFboBlit (Standard_True),
83 myTransientDrawToFront (Standard_True),
84 myBackBufferRestored (Standard_False),
85 myIsImmediateDrawn (Standard_False),
0b0320e7 86 myTextureParams (new OpenGl_AspectFace()),
87 myBgGradientArray (new OpenGl_BackgroundArray (Graphic3d_TOB_GRADIENT)),
91c60b57 88 myBgTextureArray (new OpenGl_BackgroundArray (Graphic3d_TOB_TEXTURE)),
89 // ray-tracing fields initialization
c357e426 90 myRaytraceInitStatus (OpenGl_RT_NONE),
91 myIsRaytraceDataValid (Standard_False),
91c60b57 92 myIsRaytraceWarnTextures (Standard_False),
93 myToUpdateEnvironmentMap (Standard_False),
189f85a3 94 myLayerListState (0)
2166f0fa 95{
c357e426 96 myWorkspace = new OpenGl_Workspace (this, NULL);
2166f0fa 97
c357e426 98 // AA mode
99 const char* anAaEnv = ::getenv ("CALL_OPENGL_ANTIALIASING_MODE");
100 if (anAaEnv != NULL)
101 {
102 int v;
103 if (sscanf (anAaEnv, "%d", &v) > 0) myAntiAliasing = v;
104 }
105
106 myCurrLightSourceState = myStateCounter->Increment();
107 myMainSceneFbos[0] = new OpenGl_FrameBuffer();
108 myMainSceneFbos[1] = new OpenGl_FrameBuffer();
109 myImmediateSceneFbos[0] = new OpenGl_FrameBuffer();
110 myImmediateSceneFbos[1] = new OpenGl_FrameBuffer();
3c4b62a4 111 myOpenGlFBO = new OpenGl_FrameBuffer();
112 myOpenGlFBO2 = new OpenGl_FrameBuffer();
113 myRaytraceFBO1[0] = new OpenGl_FrameBuffer();
114 myRaytraceFBO1[1] = new OpenGl_FrameBuffer();
115 myRaytraceFBO2[0] = new OpenGl_FrameBuffer();
116 myRaytraceFBO2[1] = new OpenGl_FrameBuffer();
c357e426 117}
118
119// =======================================================================
120// function : Destructor
121// purpose :
122// =======================================================================
0b0320e7 123OpenGl_View::~OpenGl_View()
2166f0fa 124{
bf75be98 125 ReleaseGlResources (NULL); // ensure ReleaseGlResources() was called within valid context
0b0320e7 126 OpenGl_Element::Destroy (NULL, myBgGradientArray);
127 OpenGl_Element::Destroy (NULL, myBgTextureArray);
128 OpenGl_Element::Destroy (NULL, myTextureParams);
bf75be98 129}
130
c357e426 131// =======================================================================
132// function : ReleaseGlResources
133// purpose :
134// =======================================================================
bf75be98 135void OpenGl_View::ReleaseGlResources (const Handle(OpenGl_Context)& theCtx)
136{
536d98e2 137 myTrihedron .Release (theCtx.operator->());
138 myGraduatedTrihedron.Release (theCtx.operator->());
30f0ad28 139
bf75be98 140 if (!myTextureEnv.IsNull())
141 {
142 theCtx->DelayedRelease (myTextureEnv);
143 myTextureEnv.Nullify();
144 }
0b0320e7 145
146 if (myTextureParams != NULL)
147 {
148 myTextureParams->Release (theCtx.operator->());
149 }
150 if (myBgGradientArray != NULL)
151 {
152 myBgGradientArray->Release (theCtx.operator->());
153 }
154 if (myBgTextureArray != NULL)
bf75be98 155 {
0b0320e7 156 myBgTextureArray->Release (theCtx.operator->());
bf75be98 157 }
91c60b57 158
c357e426 159 myMainSceneFbos[0] ->Release (theCtx.operator->());
160 myMainSceneFbos[1] ->Release (theCtx.operator->());
161 myImmediateSceneFbos[0]->Release (theCtx.operator->());
162 myImmediateSceneFbos[1]->Release (theCtx.operator->());
3c4b62a4 163 myOpenGlFBO ->Release (theCtx.operator->());
164 myOpenGlFBO2 ->Release (theCtx.operator->());
c357e426 165 myFullScreenQuad .Release (theCtx.operator->());
166 myFullScreenQuadFlip .Release (theCtx.operator->());
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// =======================================================================
192void OpenGl_View::SetTextureEnv (const Handle(Graphic3d_TextureEnv)& theTextureEnv)
193{
194 Handle(OpenGl_Context) aCtx = myWorkspace->GetGlContext();
195 if (!aCtx.IsNull() && !myTextureEnv.IsNull())
196 {
197 aCtx->DelayedRelease (myTextureEnv);
198 }
199
200 myToUpdateEnvironmentMap = Standard_True;
201 myTextureEnvData = theTextureEnv;
202 myTextureEnv.Nullify();
203 initTextureEnv (aCtx);
204}
205
206// =======================================================================
207// function : initTextureEnv
208// purpose :
209// =======================================================================
210void OpenGl_View::initTextureEnv (const Handle(OpenGl_Context)& theContext)
211{
212 if (myTextureEnvData.IsNull()
213 || theContext.IsNull()
214 || !theContext->MakeCurrent())
bf75be98 215 {
216 return;
217 }
218
c357e426 219 myTextureEnv = new OpenGl_Texture (myTextureEnvData->GetParams());
220 Handle(Image_PixMap) anImage = myTextureEnvData->GetImage();
e276548b 221 if (!anImage.IsNull())
c357e426 222 {
223 myTextureEnv->Init (theContext, *anImage.operator->(), myTextureEnvData->Type());
224 }
225}
e276548b 226
c357e426 227// =======================================================================
228// function : SetImmediateModeDrawToFront
229// purpose :
230// =======================================================================
231Standard_Boolean OpenGl_View::SetImmediateModeDrawToFront (const Standard_Boolean theDrawToFrontBuffer)
232{
233 const Standard_Boolean aPrevMode = myTransientDrawToFront;
234 myTransientDrawToFront = theDrawToFrontBuffer;
235 return aPrevMode;
e276548b 236}
237
c357e426 238// =======================================================================
239// function : SetWindow
240// purpose :
241// =======================================================================
242void OpenGl_View::SetWindow (const Handle(Aspect_Window)& theWindow,
a521d90d 243 const Aspect_RenderingContext theContext)
e276548b 244{
c357e426 245 myWindow = myDriver->CreateRenderWindow (theWindow, theContext);
246 Standard_ASSERT_RAISE (!myWindow.IsNull(),
247 "OpenGl_View::SetWindow, "
248 "Failed to create OpenGl window.");
e276548b 249
c357e426 250 myWorkspace = new OpenGl_Workspace (this, myWindow);
251 myWorldViewProjState.Reset();
91c60b57 252 myToUpdateEnvironmentMap = Standard_True;
c357e426 253 myHasFboBlit = Standard_True;
254 Invalidate();
255
256 // Environment texture resource does not support lazy initialization.
257 initTextureEnv (myWorkspace->GetGlContext());
258}
259
260// =======================================================================
261// function : Resized
262// purpose :
263// =======================================================================
264void OpenGl_View::Resized()
265{
266 if (myWindow.IsNull())
267 return;
268
269 myWindow->Resize();
270}
271
272// =======================================================================
273// function : TriedronDisplay
274// purpose :
275// =======================================================================
276void OpenGl_View::TriedronDisplay (const Aspect_TypeOfTriedronPosition thePosition,
277 const Quantity_NameOfColor theColor,
278 const Standard_Real theScale,
279 const Standard_Boolean theAsWireframe)
280{
281 myToShowTrihedron = true;
282 myTrihedron.SetWireframe (theAsWireframe);
283 myTrihedron.SetPosition (thePosition);
284 myTrihedron.SetScale (theScale);
285 myTrihedron.SetLabelsColor (theColor);
2166f0fa
SK
286}
287
12381341 288// =======================================================================
c357e426 289// function : TriedronErase
12381341 290// purpose :
291// =======================================================================
c357e426 292void OpenGl_View::TriedronErase()
2166f0fa 293{
c357e426 294 myToShowTrihedron = false;
295 myTrihedron.Release (myWorkspace->GetGlContext().operator->());
296}
297
298// =======================================================================
299// function : ZBufferTriedronSetup
300// purpose :
301// =======================================================================
302void OpenGl_View::ZBufferTriedronSetup (const Quantity_NameOfColor theXColor,
303 const Quantity_NameOfColor theYColor,
304 const Quantity_NameOfColor theZColor,
305 const Standard_Real theSizeRatio,
306 const Standard_Real theAxisDiametr,
307 const Standard_Integer theNbFacettes)
308{
309 myTrihedron.SetArrowsColors (theXColor, theYColor, theZColor);
310 myTrihedron.SetSizeRatio (theSizeRatio);
311 myTrihedron.SetNbFacets (theNbFacettes);
312 myTrihedron.SetArrowDiameter (theAxisDiametr);
2166f0fa
SK
313}
314
12381341 315// =======================================================================
c357e426 316// function : TriedronEcho
12381341 317// purpose :
318// =======================================================================
c357e426 319void OpenGl_View::TriedronEcho (const Aspect_TypeOfTriedronEcho /*theType*/)
2166f0fa 320{
c357e426 321 // do nothing
322}
323
324// =======================================================================
325// function : SetMinMaxValuesCallback
326// purpose :
327// =======================================================================
328static void SetMinMaxValuesCallback (Graphic3d_CView* theView)
329{
330 OpenGl_View* aView = dynamic_cast<OpenGl_View*>(theView);
331 if (aView == NULL)
332 return;
333
334 Bnd_Box aBox = theView->MinMaxValues();
335 if (!aBox.IsVoid())
2166f0fa 336 {
c357e426 337 gp_Pnt aMin = aBox.CornerMin();
338 gp_Pnt aMax = aBox.CornerMax();
339
340 Graphic3d_Vec3 aMinVec ((Standard_ShortReal )aMin.X(), (Standard_ShortReal )aMin.Y(), (Standard_ShortReal )aMin.Z());
341 Graphic3d_Vec3 aMaxVec ((Standard_ShortReal )aMax.X(), (Standard_ShortReal )aMax.Y(), (Standard_ShortReal )aMax.Z());
342 aView->GraduatedTrihedronMinMaxValues (aMinVec, aMaxVec);
2166f0fa
SK
343 }
344}
345
c357e426 346// =======================================================================
347// function : GraduatedTrihedronDisplay
348// purpose :
349// =======================================================================
350void OpenGl_View::GraduatedTrihedronDisplay (const Graphic3d_GraduatedTrihedron& theTrihedronData)
351{
352 myGTrihedronData = theTrihedronData;
353 myGTrihedronData.PtrView = this;
354 myGTrihedronData.CubicAxesCallback = SetMinMaxValuesCallback;
355 myGraduatedTrihedron.SetValues (myGTrihedronData);
356 myToShowGradTrihedron = true;
357}
2166f0fa 358
c357e426 359// =======================================================================
360// function : GraduatedTrihedronErase
361// purpose :
362// =======================================================================
363void OpenGl_View::GraduatedTrihedronErase()
2166f0fa 364{
c357e426 365 myGTrihedronData.PtrView = NULL;
366 myGraduatedTrihedron.Release (myWorkspace->GetGlContext().operator->());
367 myToShowGradTrihedron = false;
2166f0fa
SK
368}
369
c357e426 370// =======================================================================
371// function : GraduatedTrihedronMinMaxValues
372// purpose :
373// =======================================================================
374void OpenGl_View::GraduatedTrihedronMinMaxValues (const Graphic3d_Vec3 theMin, const Graphic3d_Vec3 theMax)
375{
376 myGraduatedTrihedron.SetMinMax (theMin, theMax);
377}
2166f0fa 378
c357e426 379// =======================================================================
c357e426 380// function : BufferDump
381// purpose :
382// =======================================================================
383Standard_Boolean OpenGl_View::BufferDump (Image_PixMap& theImage, const Graphic3d_BufferType& theBufferType)
384{
385 return myWorkspace->BufferDump (myFBO, theImage, theBufferType);
386}
387
388// =======================================================================
389// function : Background
390// purpose :
391// =======================================================================
392Aspect_Background OpenGl_View::Background() const
393{
394 return Aspect_Background (Quantity_Color (myBgColor.rgb[0], myBgColor.rgb[1], myBgColor.rgb[2], Quantity_TOC_RGB));
395}
396
397// =======================================================================
398// function : SetBackground
399// purpose :
400// =======================================================================
401void OpenGl_View::SetBackground (const Aspect_Background& theBackground)
402{
403 Quantity_Color aBgColor = theBackground.Color();
404 myBgColor.rgb[0] = static_cast<float> (aBgColor.Red());
405 myBgColor.rgb[1] = static_cast<float> (aBgColor.Green());
406 myBgColor.rgb[2] = static_cast<float> (aBgColor.Blue());
407 myFog.Color = myBgColor;
408}
409
410// =======================================================================
411// function : GradientBackground
412// purpose :
413// =======================================================================
414Aspect_GradientBackground OpenGl_View::GradientBackground() const
415{
416 Quantity_Color aColor1, aColor2;
417 aColor1.SetValues (myBgGradientArray->GradientColor (0).r(),
418 myBgGradientArray->GradientColor (0).g(),
419 myBgGradientArray->GradientColor (0).b(), Quantity_TOC_RGB);
420 aColor2.SetValues (myBgGradientArray->GradientColor (1).r(),
421 myBgGradientArray->GradientColor (1).g(),
422 myBgGradientArray->GradientColor (1).b(), Quantity_TOC_RGB);
423 return Aspect_GradientBackground (aColor1, aColor2, myBgGradientArray->GradientFillMethod());
424}
2166f0fa 425
c357e426 426// =======================================================================
427// function : SetGradientBackground
428// purpose :
429// =======================================================================
430void OpenGl_View::SetGradientBackground (const Aspect_GradientBackground& theBackground)
2166f0fa 431{
c357e426 432 Quantity_Color aColor1, aColor2;
433 theBackground.Colors (aColor1, aColor2);
434 myBgGradientArray->SetGradientParameters (aColor1, aColor2, theBackground.BgGradientFillMethod());
435}
436
437// =======================================================================
438// function : SetBackgroundImage
439// purpose :
440// =======================================================================
441void OpenGl_View::SetBackgroundImage (const TCollection_AsciiString& theFilePath)
442{
443 // Prepare aspect for texture storage
444 myBackgroundImagePath = theFilePath;
445 Handle(Graphic3d_AspectFillArea3d) anAspect = new Graphic3d_AspectFillArea3d();
446 Handle(Graphic3d_Texture2Dmanual) aTextureMap = new Graphic3d_Texture2Dmanual (TCollection_AsciiString (theFilePath));
447 aTextureMap->EnableRepeat();
448 aTextureMap->DisableModulate();
449 aTextureMap->GetParams()->SetGenMode (Graphic3d_TOTM_MANUAL,
450 Graphic3d_Vec4 (0.0f, 0.0f, 0.0f, 0.0f),
451 Graphic3d_Vec4 (0.0f, 0.0f, 0.0f, 0.0f));
452 anAspect->SetTextureMap (aTextureMap);
453 anAspect->SetInteriorStyle (Aspect_IS_SOLID);
454 // Enable texture mapping
455 if (aTextureMap->IsDone())
2166f0fa 456 {
c357e426 457 anAspect->SetTextureMapOn();
2166f0fa
SK
458 }
459 else
460 {
c357e426 461 anAspect->SetTextureMapOff();
462 return;
463 }
2166f0fa 464
c357e426 465 // Set texture parameters
466 myTextureParams->SetAspect (anAspect);
467}
2166f0fa 468
c357e426 469// =======================================================================
470// function : BackgroundImageStyle
471// purpose :
472// =======================================================================
473Aspect_FillMethod OpenGl_View::BackgroundImageStyle() const
474{
475 return myBgTextureArray->TextureFillMethod();
2166f0fa
SK
476}
477
c357e426 478// =======================================================================
479// function : SetBackgroundImageStyle
480// purpose :
481// =======================================================================
482void OpenGl_View::SetBackgroundImageStyle (const Aspect_FillMethod theFillStyle)
483{
484 myBgTextureArray->SetTextureFillMethod (theFillStyle);
485}
2166f0fa 486
c357e426 487//=======================================================================
488//function : AddZLayer
489//purpose :
490//=======================================================================
491void OpenGl_View::AddZLayer (const Graphic3d_ZLayerId theLayerId)
2166f0fa 492{
c357e426 493 myZLayers.AddLayer (theLayerId);
2166f0fa
SK
494}
495
c357e426 496//=======================================================================
497//function : RemoveZLayer
498//purpose :
499//=======================================================================
500void OpenGl_View::RemoveZLayer (const Graphic3d_ZLayerId theLayerId)
501{
502 myZLayers.RemoveLayer (theLayerId);
503}
2166f0fa 504
c357e426 505//=======================================================================
506//function : SetZLayerSettings
507//purpose :
508//=======================================================================
509void OpenGl_View::SetZLayerSettings (const Graphic3d_ZLayerId theLayerId,
510 const Graphic3d_ZLayerSettings& theSettings)
2166f0fa 511{
c357e426 512 myZLayers.SetLayerSettings (theLayerId, theSettings);
2166f0fa
SK
513}
514
c357e426 515//=======================================================================
516//function : FBO
517//purpose :
518//=======================================================================
b128c892 519Handle(Standard_Transient) OpenGl_View::FBO() const
c357e426 520{
b128c892 521 return Handle(Standard_Transient)(myFBO);
c357e426 522}
2166f0fa 523
c357e426 524//=======================================================================
525//function : SetFBO
526//purpose :
527//=======================================================================
b128c892 528void OpenGl_View::SetFBO (const Handle(Standard_Transient)& theFbo)
2166f0fa 529{
b128c892 530 myFBO = Handle(OpenGl_FrameBuffer)::DownCast (theFbo);
2166f0fa
SK
531}
532
c357e426 533//=======================================================================
534//function : FBOCreate
535//purpose :
536//=======================================================================
b128c892 537Handle(Standard_Transient) OpenGl_View::FBOCreate (const Standard_Integer theWidth,
538 const Standard_Integer theHeight)
c357e426 539{
540 return myWorkspace->FBOCreate (theWidth, theHeight);
541}
2166f0fa 542
c357e426 543//=======================================================================
544//function : FBORelease
545//purpose :
546//=======================================================================
b128c892 547void OpenGl_View::FBORelease (Handle(Standard_Transient)& theFbo)
2166f0fa 548{
b128c892 549 Handle(OpenGl_FrameBuffer) aFrameBuffer = Handle(OpenGl_FrameBuffer)::DownCast (theFbo);
550 if (aFrameBuffer.IsNull())
551 {
552 return;
553 }
554
555 myWorkspace->FBORelease (aFrameBuffer);
556 theFbo.Nullify();
c357e426 557}
558
559//=======================================================================
560//function : FBOGetDimensions
561//purpose :
562//=======================================================================
b128c892 563void OpenGl_View::FBOGetDimensions (const Handle(Standard_Transient)& theFbo,
c357e426 564 Standard_Integer& theWidth,
565 Standard_Integer& theHeight,
566 Standard_Integer& theWidthMax,
567 Standard_Integer& theHeightMax)
568{
b128c892 569 const Handle(OpenGl_FrameBuffer) aFrameBuffer = Handle(OpenGl_FrameBuffer)::DownCast (theFbo);
570 if (aFrameBuffer.IsNull())
571 {
572 return;
573 }
574
c357e426 575 theWidth = aFrameBuffer->GetVPSizeX(); // current viewport size
576 theHeight = aFrameBuffer->GetVPSizeY();
577 theWidthMax = aFrameBuffer->GetSizeX(); // texture size
578 theHeightMax = aFrameBuffer->GetSizeY();
579}
580
581//=======================================================================
582//function : FBOChangeViewport
583//purpose :
584//=======================================================================
b128c892 585void OpenGl_View::FBOChangeViewport (const Handle(Standard_Transient)& theFbo,
c357e426 586 const Standard_Integer theWidth,
587 const Standard_Integer theHeight)
588{
b128c892 589 const Handle(OpenGl_FrameBuffer) aFrameBuffer = Handle(OpenGl_FrameBuffer)::DownCast (theFbo);
590 if (aFrameBuffer.IsNull())
591 {
592 return;
593 }
594
c357e426 595 aFrameBuffer->ChangeViewport (theWidth, theHeight);
596}
597
598// =======================================================================
599// function : Export
600// purpose :
601// =======================================================================
602#ifdef HAVE_GL2PS
603Standard_Boolean OpenGl_View::Export (const Standard_CString theFileName,
604 const Graphic3d_ExportFormat theFormat,
605 const Graphic3d_SortType theSortType)
606{
607 // gl2psBeginPage() will call OpenGL functions
608 // so we should activate correct GL context before redraw scene call
609 if (!myWorkspace->Activate())
610 {
611 return Standard_False;
612 }
613
614 Standard_Integer aFormat = -1;
615 Standard_Integer aSortType = Graphic3d_ST_BSP_Tree;
616 switch (theFormat)
617 {
618 case Graphic3d_EF_PostScript:
619 aFormat = GL2PS_PS;
620 break;
621 case Graphic3d_EF_EnhPostScript:
622 aFormat = GL2PS_EPS;
623 break;
624 case Graphic3d_EF_TEX:
625 aFormat = GL2PS_TEX;
626 break;
627 case Graphic3d_EF_PDF:
628 aFormat = GL2PS_PDF;
629 break;
630 case Graphic3d_EF_SVG:
631 aFormat = GL2PS_SVG;
632 break;
633 case Graphic3d_EF_PGF:
634 aFormat = GL2PS_PGF;
635 break;
636 case Graphic3d_EF_EMF:
637 //aFormat = GL2PS_EMF;
638 aFormat = GL2PS_PGF + 1; // 6
639 break;
640 default:
641 // unsupported format
642 return Standard_False;
643 }
644
645 switch (theSortType)
646 {
647 case Graphic3d_ST_Simple:
648 aSortType = GL2PS_SIMPLE_SORT;
649 break;
650 case Graphic3d_ST_BSP_Tree:
651 aSortType = GL2PS_BSP_SORT;
652 break;
653 }
654
655 GLint aViewport[4];
656 aViewport[0] = 0;
657 aViewport[1] = 0;
658 aViewport[2] = myWindow->Width();
659 aViewport[3] = myWindow->Height();
660
661 GLint aBufferSize = 1024 * 1024;
662 GLint anErrCode = GL2PS_SUCCESS;
663
664 // gl2ps uses standard write functions and do not check locale
665 Standard_CLocaleSentry aLocaleSentry;
666
667 while (aBufferSize > 0)
668 {
669 // current patch for EMF support in gl2ps uses WinAPI functions to create file
670 FILE* aFileH = (theFormat != Graphic3d_EF_EMF) ? fopen (theFileName, "wb") : NULL;
671 anErrCode = gl2psBeginPage ("", "", aViewport, aFormat, aSortType,
672 GL2PS_DRAW_BACKGROUND | GL2PS_OCCLUSION_CULL | GL2PS_BEST_ROOT/* | GL2PS_SIMPLE_LINE_OFFSET*/,
673 GL_RGBA, 0, NULL,
674 0, 0, 0, aBufferSize, aFileH, theFileName);
675 if (anErrCode != GL2PS_SUCCESS)
676 {
677 // initialization failed
678 if (aFileH != NULL)
679 fclose (aFileH);
680 break;
681 }
682 Redraw();
683
684 anErrCode = gl2psEndPage();
685 if (aFileH != NULL)
686 fclose (aFileH);
687
688 if (anErrCode == GL2PS_OVERFLOW)
689 aBufferSize *= 2;
690 else
691 break;
692 }
693 return anErrCode == GL2PS_SUCCESS;
694}
695#else
696Standard_Boolean OpenGl_View::Export (const Standard_CString /*theFileName*/,
697 const Graphic3d_ExportFormat /*theFormat*/,
698 const Graphic3d_SortType /*theSortType*/)
699{
700 return Standard_False;
701}
702#endif
703
704//=======================================================================
705//function : displayStructure
706//purpose :
707//=======================================================================
708void OpenGl_View::displayStructure (const Handle(Graphic3d_CStructure)& theStructure,
709 const Standard_Integer thePriority)
710{
711 const OpenGl_Structure* aStruct = reinterpret_cast<const OpenGl_Structure*> (theStructure.operator->());
712 const Graphic3d_ZLayerId aZLayer = aStruct->ZLayer();
713 myZLayers.AddStructure (aStruct, aZLayer, thePriority);
714}
715
716//=======================================================================
717//function : eraseStructure
718//purpose :
719//=======================================================================
720void OpenGl_View::eraseStructure (const Handle(Graphic3d_CStructure)& theStructure)
721{
722 const OpenGl_Structure* aStruct = reinterpret_cast<const OpenGl_Structure*> (theStructure.operator->());
723 myZLayers.RemoveStructure (aStruct);
724}
725
726//=======================================================================
727//function : changeZLayer
728//purpose :
729//=======================================================================
730void OpenGl_View::changeZLayer (const Handle(Graphic3d_CStructure)& theStructure,
731 const Graphic3d_ZLayerId theNewLayerId)
732{
733 const Graphic3d_ZLayerId anOldLayer = theStructure->ZLayer();
734 const OpenGl_Structure* aStruct = reinterpret_cast<const OpenGl_Structure*> (theStructure.operator->());
735 myZLayers.ChangeLayer (aStruct, anOldLayer, theNewLayerId);
736}
737
738//=======================================================================
739//function : changePriority
740//purpose :
741//=======================================================================
742void OpenGl_View::changePriority (const Handle(Graphic3d_CStructure)& theStructure,
743 const Standard_Integer theNewPriority)
744{
745 const Graphic3d_ZLayerId aLayerId = theStructure->ZLayer();
746 const OpenGl_Structure* aStruct = reinterpret_cast<const OpenGl_Structure*> (theStructure.operator->());
747 myZLayers.ChangePriority (aStruct, aLayerId, theNewPriority);
2166f0fa 748}