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