0026348: Visualization, TKOpenGl - eliminate invalid NULL checks for transformation...
[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()),
73 myFBO (NULL),
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// =======================================================================
c357e426 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//=======================================================================
520Graphic3d_PtrFrameBuffer OpenGl_View::FBO() const
521{
522 return reinterpret_cast<Graphic3d_PtrFrameBuffer> (myFBO);
523}
2166f0fa 524
c357e426 525//=======================================================================
526//function : SetFBO
527//purpose :
528//=======================================================================
529void OpenGl_View::SetFBO (const Graphic3d_PtrFrameBuffer theFBO)
2166f0fa 530{
c357e426 531 myFBO = reinterpret_cast<OpenGl_FrameBuffer*> (theFBO);
2166f0fa
SK
532}
533
c357e426 534//=======================================================================
535//function : FBOCreate
536//purpose :
537//=======================================================================
538Graphic3d_PtrFrameBuffer OpenGl_View::FBOCreate (const Standard_Integer theWidth,
539 const Standard_Integer theHeight)
540{
541 return myWorkspace->FBOCreate (theWidth, theHeight);
542}
2166f0fa 543
c357e426 544//=======================================================================
545//function : FBORelease
546//purpose :
547//=======================================================================
548void OpenGl_View::FBORelease (Graphic3d_PtrFrameBuffer& theFBOPtr)
2166f0fa 549{
c357e426 550 myWorkspace->FBORelease (theFBOPtr);
551}
552
553//=======================================================================
554//function : FBOGetDimensions
555//purpose :
556//=======================================================================
557void OpenGl_View::FBOGetDimensions (const Graphic3d_PtrFrameBuffer theFBOPtr,
558 Standard_Integer& theWidth,
559 Standard_Integer& theHeight,
560 Standard_Integer& theWidthMax,
561 Standard_Integer& theHeightMax)
562{
563 const OpenGl_FrameBuffer* aFrameBuffer = (const OpenGl_FrameBuffer* )theFBOPtr;
564 theWidth = aFrameBuffer->GetVPSizeX(); // current viewport size
565 theHeight = aFrameBuffer->GetVPSizeY();
566 theWidthMax = aFrameBuffer->GetSizeX(); // texture size
567 theHeightMax = aFrameBuffer->GetSizeY();
568}
569
570//=======================================================================
571//function : FBOChangeViewport
572//purpose :
573//=======================================================================
574void OpenGl_View::FBOChangeViewport (Graphic3d_PtrFrameBuffer& theFBOPtr,
575 const Standard_Integer theWidth,
576 const Standard_Integer theHeight)
577{
578 OpenGl_FrameBuffer* aFrameBuffer = (OpenGl_FrameBuffer* )theFBOPtr;
579 aFrameBuffer->ChangeViewport (theWidth, theHeight);
580}
581
582// =======================================================================
583// function : Export
584// purpose :
585// =======================================================================
586#ifdef HAVE_GL2PS
587Standard_Boolean OpenGl_View::Export (const Standard_CString theFileName,
588 const Graphic3d_ExportFormat theFormat,
589 const Graphic3d_SortType theSortType)
590{
591 // gl2psBeginPage() will call OpenGL functions
592 // so we should activate correct GL context before redraw scene call
593 if (!myWorkspace->Activate())
594 {
595 return Standard_False;
596 }
597
598 Standard_Integer aFormat = -1;
599 Standard_Integer aSortType = Graphic3d_ST_BSP_Tree;
600 switch (theFormat)
601 {
602 case Graphic3d_EF_PostScript:
603 aFormat = GL2PS_PS;
604 break;
605 case Graphic3d_EF_EnhPostScript:
606 aFormat = GL2PS_EPS;
607 break;
608 case Graphic3d_EF_TEX:
609 aFormat = GL2PS_TEX;
610 break;
611 case Graphic3d_EF_PDF:
612 aFormat = GL2PS_PDF;
613 break;
614 case Graphic3d_EF_SVG:
615 aFormat = GL2PS_SVG;
616 break;
617 case Graphic3d_EF_PGF:
618 aFormat = GL2PS_PGF;
619 break;
620 case Graphic3d_EF_EMF:
621 //aFormat = GL2PS_EMF;
622 aFormat = GL2PS_PGF + 1; // 6
623 break;
624 default:
625 // unsupported format
626 return Standard_False;
627 }
628
629 switch (theSortType)
630 {
631 case Graphic3d_ST_Simple:
632 aSortType = GL2PS_SIMPLE_SORT;
633 break;
634 case Graphic3d_ST_BSP_Tree:
635 aSortType = GL2PS_BSP_SORT;
636 break;
637 }
638
639 GLint aViewport[4];
640 aViewport[0] = 0;
641 aViewport[1] = 0;
642 aViewport[2] = myWindow->Width();
643 aViewport[3] = myWindow->Height();
644
645 GLint aBufferSize = 1024 * 1024;
646 GLint anErrCode = GL2PS_SUCCESS;
647
648 // gl2ps uses standard write functions and do not check locale
649 Standard_CLocaleSentry aLocaleSentry;
650
651 while (aBufferSize > 0)
652 {
653 // current patch for EMF support in gl2ps uses WinAPI functions to create file
654 FILE* aFileH = (theFormat != Graphic3d_EF_EMF) ? fopen (theFileName, "wb") : NULL;
655 anErrCode = gl2psBeginPage ("", "", aViewport, aFormat, aSortType,
656 GL2PS_DRAW_BACKGROUND | GL2PS_OCCLUSION_CULL | GL2PS_BEST_ROOT/* | GL2PS_SIMPLE_LINE_OFFSET*/,
657 GL_RGBA, 0, NULL,
658 0, 0, 0, aBufferSize, aFileH, theFileName);
659 if (anErrCode != GL2PS_SUCCESS)
660 {
661 // initialization failed
662 if (aFileH != NULL)
663 fclose (aFileH);
664 break;
665 }
666 Redraw();
667
668 anErrCode = gl2psEndPage();
669 if (aFileH != NULL)
670 fclose (aFileH);
671
672 if (anErrCode == GL2PS_OVERFLOW)
673 aBufferSize *= 2;
674 else
675 break;
676 }
677 return anErrCode == GL2PS_SUCCESS;
678}
679#else
680Standard_Boolean OpenGl_View::Export (const Standard_CString /*theFileName*/,
681 const Graphic3d_ExportFormat /*theFormat*/,
682 const Graphic3d_SortType /*theSortType*/)
683{
684 return Standard_False;
685}
686#endif
687
688//=======================================================================
689//function : displayStructure
690//purpose :
691//=======================================================================
692void OpenGl_View::displayStructure (const Handle(Graphic3d_CStructure)& theStructure,
693 const Standard_Integer thePriority)
694{
695 const OpenGl_Structure* aStruct = reinterpret_cast<const OpenGl_Structure*> (theStructure.operator->());
696 const Graphic3d_ZLayerId aZLayer = aStruct->ZLayer();
697 myZLayers.AddStructure (aStruct, aZLayer, thePriority);
698}
699
700//=======================================================================
701//function : eraseStructure
702//purpose :
703//=======================================================================
704void OpenGl_View::eraseStructure (const Handle(Graphic3d_CStructure)& theStructure)
705{
706 const OpenGl_Structure* aStruct = reinterpret_cast<const OpenGl_Structure*> (theStructure.operator->());
707 myZLayers.RemoveStructure (aStruct);
708}
709
710//=======================================================================
711//function : changeZLayer
712//purpose :
713//=======================================================================
714void OpenGl_View::changeZLayer (const Handle(Graphic3d_CStructure)& theStructure,
715 const Graphic3d_ZLayerId theNewLayerId)
716{
717 const Graphic3d_ZLayerId anOldLayer = theStructure->ZLayer();
718 const OpenGl_Structure* aStruct = reinterpret_cast<const OpenGl_Structure*> (theStructure.operator->());
719 myZLayers.ChangeLayer (aStruct, anOldLayer, theNewLayerId);
720}
721
722//=======================================================================
723//function : changePriority
724//purpose :
725//=======================================================================
726void OpenGl_View::changePriority (const Handle(Graphic3d_CStructure)& theStructure,
727 const Standard_Integer theNewPriority)
728{
729 const Graphic3d_ZLayerId aLayerId = theStructure->ZLayer();
730 const OpenGl_Structure* aStruct = reinterpret_cast<const OpenGl_Structure*> (theStructure.operator->());
731 myZLayers.ChangePriority (aStruct, aLayerId, theNewPriority);
2166f0fa 732}