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