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> |
c357e426 | 32 | #include <OpenGl_Window.hxx> |
30f0ad28 | 33 | #include <OpenGl_Workspace.hxx> |
f5b72419 | 34 | #include <OSD_Parallel.hxx> |
c357e426 | 35 | #include <Standard_CLocaleSentry.hxx> |
2166f0fa | 36 | |
f5b72419 | 37 | #include "../Graphic3d/Graphic3d_Structure.pxx" |
38 | ||
92efcf78 | 39 | IMPLEMENT_STANDARD_RTTIEXT(OpenGl_View,Graphic3d_CView) |
40 | ||
c357e426 | 41 | // ======================================================================= |
42 | // function : Constructor | |
43 | // purpose : | |
44 | // ======================================================================= | |
45 | OpenGl_View::OpenGl_View (const Handle(Graphic3d_StructureManager)& theMgr, | |
46 | const Handle(OpenGl_GraphicDriver)& theDriver, | |
47 | const Handle(OpenGl_Caps)& theCaps, | |
c357e426 | 48 | OpenGl_StateCounter* theCounter) |
49 | : Graphic3d_CView (theMgr), | |
50 | myDriver (theDriver.operator->()), | |
51 | myCaps (theCaps), | |
c357e426 | 52 | myWasRedrawnGL (Standard_False), |
c357e426 | 53 | myBackfacing (Graphic3d_TOBM_AUTOMATIC), |
c357e426 | 54 | myToShowGradTrihedron (false), |
f5b72419 | 55 | myZLayers (Structure_MAX_PRIORITY - Structure_MIN_PRIORITY + 1), |
c357e426 | 56 | myStateCounter (theCounter), |
992ed6b3 | 57 | myCurrLightSourceState (theCounter->Increment()), |
58 | myLightsRevision (0), | |
0b0320e7 | 59 | myLastLightSourceState (0, 0), |
ba00aab7 | 60 | mySRgbState (-1), |
61 | myFboColorFormat (GL_SRGB8_ALPHA8), // note that GL_SRGB8 is not required to be renderable, unlike GL_RGB8, GL_RGBA8, GL_SRGB8_ALPHA8 | |
3c4b62a4 | 62 | myFboDepthFormat (GL_DEPTH24_STENCIL8), |
63 | myToFlipOutput (Standard_False), | |
c357e426 | 64 | myFrameCounter (0), |
65 | myHasFboBlit (Standard_True), | |
a1073ae2 | 66 | myToDisableOIT (Standard_False), |
67 | myToDisableOITMSAA (Standard_False), | |
7ccf8676 | 68 | myToDisableMSAA (Standard_False), |
c357e426 | 69 | myTransientDrawToFront (Standard_True), |
70 | myBackBufferRestored (Standard_False), | |
71 | myIsImmediateDrawn (Standard_False), | |
bf5f0ca2 | 72 | myTextureParams (new OpenGl_Aspects()), |
077a220c | 73 | myCubeMapParams (new OpenGl_Aspects()), |
74 | myBackgroundType (Graphic3d_TOB_NONE), | |
67312b79 | 75 | myPBREnvState (OpenGl_PBREnvState_NONEXISTENT), |
76 | myPBREnvRequest (OpenGl_PBREnvRequest_NONE), | |
91c60b57 | 77 | // ray-tracing fields initialization |
c357e426 | 78 | myRaytraceInitStatus (OpenGl_RT_NONE), |
79 | myIsRaytraceDataValid (Standard_False), | |
91c60b57 | 80 | myIsRaytraceWarnTextures (Standard_False), |
f5b72419 | 81 | myRaytraceBVHBuilder (new BVH_BinnedBuilder<Standard_ShortReal, 3, BVH_Constants_NbBinsBest> (BVH_Constants_LeafNodeSizeAverage, |
82 | BVH_Constants_MaxTreeDepth, | |
83 | Standard_False, | |
84 | OSD_Parallel::NbLogicalProcessors() + 1)), | |
85 | myRaytraceSceneRadius (0.0f), | |
86 | myRaytraceSceneEpsilon (1.0e-6f), | |
91c60b57 | 87 | myToUpdateEnvironmentMap (Standard_False), |
b27ab03d | 88 | myRaytraceLayerListState (0), |
89 | myPrevCameraApertureRadius(0.f), | |
90 | myPrevCameraFocalPlaneDist(0.f) | |
2166f0fa | 91 | { |
077a220c | 92 | for (int i = 0; i < Graphic3d_TypeOfBackground_NB; ++i) |
93 | { | |
94 | myBackgrounds[i] = new OpenGl_BackgroundArray(Graphic3d_TypeOfBackground(i)); | |
95 | } | |
96 | ||
c357e426 | 97 | myWorkspace = new OpenGl_Workspace (this, NULL); |
2166f0fa | 98 | |
992ed6b3 | 99 | Handle(Graphic3d_CLight) aLight = new Graphic3d_CLight (Graphic3d_TOLS_AMBIENT); |
100 | aLight->SetHeadlight (false); | |
101 | aLight->SetColor (Quantity_NOC_WHITE); | |
8445efca | 102 | myLights = new Graphic3d_LightSet(); |
992ed6b3 | 103 | myNoShadingLight = new Graphic3d_LightSet(); |
104 | myNoShadingLight->Add (aLight); | |
016e5959 | 105 | |
a1073ae2 | 106 | myMainSceneFbos[0] = new OpenGl_FrameBuffer(); |
107 | myMainSceneFbos[1] = new OpenGl_FrameBuffer(); | |
108 | myMainSceneFbosOit[0] = new OpenGl_FrameBuffer(); | |
109 | myMainSceneFbosOit[1] = new OpenGl_FrameBuffer(); | |
110 | myImmediateSceneFbos[0] = new OpenGl_FrameBuffer(); | |
111 | myImmediateSceneFbos[1] = new OpenGl_FrameBuffer(); | |
112 | myImmediateSceneFbosOit[0] = new OpenGl_FrameBuffer(); | |
113 | myImmediateSceneFbosOit[1] = new OpenGl_FrameBuffer(); | |
114 | myOpenGlFBO = new OpenGl_FrameBuffer(); | |
115 | myOpenGlFBO2 = new OpenGl_FrameBuffer(); | |
116 | myRaytraceFBO1[0] = new OpenGl_FrameBuffer(); | |
117 | myRaytraceFBO1[1] = new OpenGl_FrameBuffer(); | |
118 | myRaytraceFBO2[0] = new OpenGl_FrameBuffer(); | |
119 | myRaytraceFBO2[1] = new OpenGl_FrameBuffer(); | |
c357e426 | 120 | } |
121 | ||
122 | // ======================================================================= | |
123 | // function : Destructor | |
124 | // purpose : | |
125 | // ======================================================================= | |
0b0320e7 | 126 | OpenGl_View::~OpenGl_View() |
2166f0fa | 127 | { |
bf75be98 | 128 | ReleaseGlResources (NULL); // ensure ReleaseGlResources() was called within valid context |
077a220c | 129 | for (int i = 0; i < Graphic3d_TypeOfBackground_NB; ++i) |
130 | { | |
131 | OpenGl_Element::Destroy(NULL, myBackgrounds[i]); | |
132 | } | |
133 | ||
0b0320e7 | 134 | OpenGl_Element::Destroy (NULL, myTextureParams); |
077a220c | 135 | OpenGl_Element::Destroy (NULL, myCubeMapParams); |
bf75be98 | 136 | } |
137 | ||
c357e426 | 138 | // ======================================================================= |
ba00aab7 | 139 | // function : releaseSrgbResources |
c357e426 | 140 | // purpose : |
141 | // ======================================================================= | |
ba00aab7 | 142 | void OpenGl_View::releaseSrgbResources (const Handle(OpenGl_Context)& theCtx) |
bf75be98 | 143 | { |
ba00aab7 | 144 | myRenderParams.RebuildRayTracingShaders = true; |
30f0ad28 | 145 | |
bf75be98 | 146 | if (!myTextureEnv.IsNull()) |
147 | { | |
ad67e367 | 148 | if (!theCtx.IsNull()) |
cc8cbabe | 149 | { |
ad67e367 | 150 | for (OpenGl_TextureSet::Iterator aTextureIter (myTextureEnv); aTextureIter.More(); aTextureIter.Next()) |
151 | { | |
152 | theCtx->DelayedRelease (aTextureIter.ChangeValue()); | |
153 | aTextureIter.ChangeValue().Nullify(); | |
154 | } | |
cc8cbabe | 155 | } |
bf75be98 | 156 | myTextureEnv.Nullify(); |
157 | } | |
0b0320e7 | 158 | |
159 | if (myTextureParams != NULL) | |
160 | { | |
ad67e367 | 161 | myTextureParams->Release (theCtx.get()); |
0b0320e7 | 162 | } |
077a220c | 163 | |
164 | if (myCubeMapParams != NULL) | |
0b0320e7 | 165 | { |
077a220c | 166 | myCubeMapParams->Release (theCtx.get()); |
0b0320e7 | 167 | } |
077a220c | 168 | |
169 | for (int i = 0; i < Graphic3d_TypeOfBackground_NB; ++i) | |
bf75be98 | 170 | { |
077a220c | 171 | if (myBackgrounds[i] != NULL) |
172 | { | |
173 | myBackgrounds[i]->Release (theCtx.get()); | |
174 | } | |
bf75be98 | 175 | } |
91c60b57 | 176 | |
ad67e367 | 177 | myMainSceneFbos[0] ->Release (theCtx.get()); |
178 | myMainSceneFbos[1] ->Release (theCtx.get()); | |
179 | myMainSceneFbosOit[0] ->Release (theCtx.get()); | |
180 | myMainSceneFbosOit[1] ->Release (theCtx.get()); | |
181 | myImmediateSceneFbos[0] ->Release (theCtx.get()); | |
182 | myImmediateSceneFbos[1] ->Release (theCtx.get()); | |
183 | myImmediateSceneFbosOit[0]->Release (theCtx.get()); | |
184 | myImmediateSceneFbosOit[1]->Release (theCtx.get()); | |
185 | myOpenGlFBO ->Release (theCtx.get()); | |
186 | myOpenGlFBO2 ->Release (theCtx.get()); | |
187 | myFullScreenQuad .Release (theCtx.get()); | |
188 | myFullScreenQuadFlip .Release (theCtx.get()); | |
c357e426 | 189 | |
ba00aab7 | 190 | // Technically we should also re-initialize all sRGB/RGB8 color textures. |
191 | // But for now consider this sRGB disabling/enabling to be done at application start-up | |
192 | // and re-create dynamically only frame buffers. | |
193 | } | |
194 | ||
195 | // ======================================================================= | |
196 | // function : ReleaseGlResources | |
197 | // purpose : | |
198 | // ======================================================================= | |
199 | void OpenGl_View::ReleaseGlResources (const Handle(OpenGl_Context)& theCtx) | |
200 | { | |
201 | myGraduatedTrihedron.Release (theCtx.get()); | |
202 | myFrameStatsPrs.Release (theCtx.get()); | |
203 | ||
204 | releaseSrgbResources (theCtx); | |
205 | ||
91c60b57 | 206 | releaseRaytraceResources (theCtx); |
67312b79 | 207 | |
208 | if (!myPBREnvironment.IsNull()) | |
209 | { | |
210 | myPBREnvironment->Release (theCtx.get()); | |
211 | } | |
bf75be98 | 212 | } |
2166f0fa | 213 | |
c357e426 | 214 | // ======================================================================= |
215 | // function : Remove | |
216 | // purpose : | |
217 | // ======================================================================= | |
218 | void OpenGl_View::Remove() | |
bf75be98 | 219 | { |
c357e426 | 220 | if (IsRemoved()) |
bf75be98 | 221 | { |
c357e426 | 222 | return; |
bf75be98 | 223 | } |
224 | ||
c357e426 | 225 | myDriver->RemoveView (this); |
226 | myWindow.Nullify(); | |
227 | ||
228 | Graphic3d_CView::Remove(); | |
229 | } | |
230 | ||
7c3ef2f7 | 231 | // ======================================================================= |
232 | // function : SetLocalOrigin | |
233 | // purpose : | |
234 | // ======================================================================= | |
235 | void OpenGl_View::SetLocalOrigin (const gp_XYZ& theOrigin) | |
236 | { | |
237 | myLocalOrigin = theOrigin; | |
238 | const Handle(OpenGl_Context)& aCtx = myWorkspace->GetGlContext(); | |
239 | if (!aCtx.IsNull()) | |
240 | { | |
241 | aCtx->ShaderManager()->SetLocalOrigin (theOrigin); | |
242 | } | |
243 | } | |
244 | ||
c357e426 | 245 | // ======================================================================= |
246 | // function : SetTextureEnv | |
247 | // purpose : | |
248 | // ======================================================================= | |
249 | void OpenGl_View::SetTextureEnv (const Handle(Graphic3d_TextureEnv)& theTextureEnv) | |
250 | { | |
251 | Handle(OpenGl_Context) aCtx = myWorkspace->GetGlContext(); | |
252 | if (!aCtx.IsNull() && !myTextureEnv.IsNull()) | |
253 | { | |
cc8cbabe | 254 | for (OpenGl_TextureSet::Iterator aTextureIter (myTextureEnv); aTextureIter.More(); aTextureIter.Next()) |
255 | { | |
256 | aCtx->DelayedRelease (aTextureIter.ChangeValue()); | |
257 | aTextureIter.ChangeValue().Nullify(); | |
258 | } | |
c357e426 | 259 | } |
260 | ||
261 | myToUpdateEnvironmentMap = Standard_True; | |
262 | myTextureEnvData = theTextureEnv; | |
263 | myTextureEnv.Nullify(); | |
264 | initTextureEnv (aCtx); | |
265 | } | |
266 | ||
267 | // ======================================================================= | |
268 | // function : initTextureEnv | |
269 | // purpose : | |
270 | // ======================================================================= | |
271 | void OpenGl_View::initTextureEnv (const Handle(OpenGl_Context)& theContext) | |
272 | { | |
273 | if (myTextureEnvData.IsNull() | |
274 | || theContext.IsNull() | |
275 | || !theContext->MakeCurrent()) | |
bf75be98 | 276 | { |
277 | return; | |
278 | } | |
279 | ||
cc8cbabe | 280 | myTextureEnv = new OpenGl_TextureSet (1); |
281 | Handle(OpenGl_Texture)& aTextureEnv = myTextureEnv->ChangeFirst(); | |
282 | aTextureEnv = new OpenGl_Texture (myTextureEnvData->GetId(), myTextureEnvData->GetParams()); | |
c357e426 | 283 | Handle(Image_PixMap) anImage = myTextureEnvData->GetImage(); |
e276548b | 284 | if (!anImage.IsNull()) |
c357e426 | 285 | { |
ba00aab7 | 286 | aTextureEnv->Init (theContext, *anImage, myTextureEnvData->Type(), true); |
c357e426 | 287 | } |
288 | } | |
e276548b | 289 | |
c357e426 | 290 | // ======================================================================= |
291 | // function : SetImmediateModeDrawToFront | |
292 | // purpose : | |
293 | // ======================================================================= | |
294 | Standard_Boolean OpenGl_View::SetImmediateModeDrawToFront (const Standard_Boolean theDrawToFrontBuffer) | |
295 | { | |
296 | const Standard_Boolean aPrevMode = myTransientDrawToFront; | |
297 | myTransientDrawToFront = theDrawToFrontBuffer; | |
298 | return aPrevMode; | |
e276548b | 299 | } |
300 | ||
c357e426 | 301 | // ======================================================================= |
302 | // function : SetWindow | |
303 | // purpose : | |
304 | // ======================================================================= | |
305 | void OpenGl_View::SetWindow (const Handle(Aspect_Window)& theWindow, | |
a521d90d | 306 | const Aspect_RenderingContext theContext) |
e276548b | 307 | { |
c357e426 | 308 | myWindow = myDriver->CreateRenderWindow (theWindow, theContext); |
309 | Standard_ASSERT_RAISE (!myWindow.IsNull(), | |
310 | "OpenGl_View::SetWindow, " | |
311 | "Failed to create OpenGl window."); | |
e276548b | 312 | |
c357e426 | 313 | myWorkspace = new OpenGl_Workspace (this, myWindow); |
314 | myWorldViewProjState.Reset(); | |
91c60b57 | 315 | myToUpdateEnvironmentMap = Standard_True; |
c357e426 | 316 | myHasFboBlit = Standard_True; |
317 | Invalidate(); | |
318 | ||
319 | // Environment texture resource does not support lazy initialization. | |
320 | initTextureEnv (myWorkspace->GetGlContext()); | |
321 | } | |
322 | ||
323 | // ======================================================================= | |
324 | // function : Resized | |
325 | // purpose : | |
326 | // ======================================================================= | |
327 | void OpenGl_View::Resized() | |
328 | { | |
329 | if (myWindow.IsNull()) | |
330 | return; | |
331 | ||
332 | myWindow->Resize(); | |
333 | } | |
334 | ||
c357e426 | 335 | // ======================================================================= |
336 | // function : SetMinMaxValuesCallback | |
337 | // purpose : | |
338 | // ======================================================================= | |
339 | static void SetMinMaxValuesCallback (Graphic3d_CView* theView) | |
340 | { | |
341 | OpenGl_View* aView = dynamic_cast<OpenGl_View*>(theView); | |
342 | if (aView == NULL) | |
343 | return; | |
344 | ||
345 | Bnd_Box aBox = theView->MinMaxValues(); | |
346 | if (!aBox.IsVoid()) | |
2166f0fa | 347 | { |
c357e426 | 348 | gp_Pnt aMin = aBox.CornerMin(); |
349 | gp_Pnt aMax = aBox.CornerMax(); | |
350 | ||
351 | Graphic3d_Vec3 aMinVec ((Standard_ShortReal )aMin.X(), (Standard_ShortReal )aMin.Y(), (Standard_ShortReal )aMin.Z()); | |
352 | Graphic3d_Vec3 aMaxVec ((Standard_ShortReal )aMax.X(), (Standard_ShortReal )aMax.Y(), (Standard_ShortReal )aMax.Z()); | |
353 | aView->GraduatedTrihedronMinMaxValues (aMinVec, aMaxVec); | |
2166f0fa SK |
354 | } |
355 | } | |
356 | ||
c357e426 | 357 | // ======================================================================= |
358 | // function : GraduatedTrihedronDisplay | |
359 | // purpose : | |
360 | // ======================================================================= | |
361 | void OpenGl_View::GraduatedTrihedronDisplay (const Graphic3d_GraduatedTrihedron& theTrihedronData) | |
362 | { | |
363 | myGTrihedronData = theTrihedronData; | |
364 | myGTrihedronData.PtrView = this; | |
365 | myGTrihedronData.CubicAxesCallback = SetMinMaxValuesCallback; | |
366 | myGraduatedTrihedron.SetValues (myGTrihedronData); | |
367 | myToShowGradTrihedron = true; | |
368 | } | |
2166f0fa | 369 | |
c357e426 | 370 | // ======================================================================= |
371 | // function : GraduatedTrihedronErase | |
372 | // purpose : | |
373 | // ======================================================================= | |
374 | void OpenGl_View::GraduatedTrihedronErase() | |
2166f0fa | 375 | { |
c357e426 | 376 | myGTrihedronData.PtrView = NULL; |
377 | myGraduatedTrihedron.Release (myWorkspace->GetGlContext().operator->()); | |
378 | myToShowGradTrihedron = false; | |
2166f0fa SK |
379 | } |
380 | ||
c357e426 | 381 | // ======================================================================= |
382 | // function : GraduatedTrihedronMinMaxValues | |
383 | // purpose : | |
384 | // ======================================================================= | |
385 | void OpenGl_View::GraduatedTrihedronMinMaxValues (const Graphic3d_Vec3 theMin, const Graphic3d_Vec3 theMax) | |
386 | { | |
387 | myGraduatedTrihedron.SetMinMax (theMin, theMax); | |
388 | } | |
2166f0fa | 389 | |
c357e426 | 390 | // ======================================================================= |
391 | // function : BufferDump | |
392 | // purpose : | |
393 | // ======================================================================= | |
394 | Standard_Boolean OpenGl_View::BufferDump (Image_PixMap& theImage, const Graphic3d_BufferType& theBufferType) | |
395 | { | |
38d90bb3 | 396 | if (theBufferType != Graphic3d_BT_RGB_RayTraceHdrLeft) |
397 | { | |
398 | return myWorkspace->BufferDump(myFBO, theImage, theBufferType); | |
399 | } | |
400 | ||
401 | if (!myRaytraceParameters.AdaptiveScreenSampling) | |
402 | { | |
403 | return myWorkspace->BufferDump(myAccumFrames % 2 ? myRaytraceFBO2[0] : myRaytraceFBO1[0], theImage, theBufferType); | |
404 | } | |
405 | ||
406 | #if defined(GL_ES_VERSION_2_0) | |
407 | return false; | |
408 | #else | |
409 | if (theImage.Format() != Image_Format_RGBF) | |
410 | { | |
411 | return false; | |
412 | } | |
413 | ||
414 | const GLuint aW = myRaytraceOutputTexture[0]->SizeX(); | |
415 | const GLuint aH = myRaytraceOutputTexture[0]->SizeY(); | |
416 | if (aW / 3 != theImage.SizeX() || aH / 2 != theImage.SizeY()) | |
417 | { | |
418 | return false; | |
419 | } | |
420 | ||
421 | std::vector<GLfloat> aValues; | |
422 | try | |
423 | { | |
424 | aValues.resize (aW * aH); | |
425 | } | |
426 | catch (const std::bad_alloc&) | |
427 | { | |
428 | return false; | |
429 | } | |
430 | ||
431 | glBindTexture (GL_TEXTURE_RECTANGLE, myRaytraceOutputTexture[0]->TextureId()); | |
432 | glGetTexImage (GL_TEXTURE_RECTANGLE, 0, OpenGl_TextureFormat::Create<GLfloat, 1>().Format(), GL_FLOAT, &aValues[0]); | |
433 | glBindTexture (GL_TEXTURE_RECTANGLE, 0); | |
434 | for (unsigned int aRow = 0; aRow < aH; aRow += 2) | |
435 | { | |
436 | for (unsigned int aCol = 0; aCol < aW; aCol += 3) | |
437 | { | |
438 | float* anImageValue = theImage.ChangeValue<float[3]> ((aH - aRow) / 2 - 1, aCol / 3); | |
439 | float aInvNbSamples = 1.f / aValues[aRow * aW + aCol + aW]; | |
440 | anImageValue[0] = aValues[aRow * aW + aCol] * aInvNbSamples; | |
441 | anImageValue[1] = aValues[aRow * aW + aCol + 1] * aInvNbSamples; | |
442 | anImageValue[2] = aValues[aRow * aW + aCol + 1 + aW] * aInvNbSamples; | |
443 | } | |
444 | } | |
445 | ||
446 | return true; | |
447 | #endif | |
c357e426 | 448 | } |
449 | ||
c357e426 | 450 | // ======================================================================= |
451 | // function : GradientBackground | |
452 | // purpose : | |
453 | // ======================================================================= | |
454 | Aspect_GradientBackground OpenGl_View::GradientBackground() const | |
455 | { | |
456 | Quantity_Color aColor1, aColor2; | |
077a220c | 457 | aColor1.SetValues (myBackgrounds[Graphic3d_TOB_GRADIENT]->GradientColor (0).r(), |
458 | myBackgrounds[Graphic3d_TOB_GRADIENT]->GradientColor (0).g(), | |
459 | myBackgrounds[Graphic3d_TOB_GRADIENT]->GradientColor (0).b(), Quantity_TOC_RGB); | |
460 | aColor2.SetValues (myBackgrounds[Graphic3d_TOB_GRADIENT]->GradientColor (1).r(), | |
461 | myBackgrounds[Graphic3d_TOB_GRADIENT]->GradientColor (1).g(), | |
462 | myBackgrounds[Graphic3d_TOB_GRADIENT]->GradientColor (1).b(), Quantity_TOC_RGB); | |
463 | return Aspect_GradientBackground (aColor1, aColor2, myBackgrounds[Graphic3d_TOB_GRADIENT]->GradientFillMethod()); | |
c357e426 | 464 | } |
2166f0fa | 465 | |
c357e426 | 466 | // ======================================================================= |
467 | // function : SetGradientBackground | |
468 | // purpose : | |
469 | // ======================================================================= | |
470 | void OpenGl_View::SetGradientBackground (const Aspect_GradientBackground& theBackground) | |
2166f0fa | 471 | { |
c357e426 | 472 | Quantity_Color aColor1, aColor2; |
473 | theBackground.Colors (aColor1, aColor2); | |
077a220c | 474 | myBackgrounds[Graphic3d_TOB_GRADIENT]->SetGradientParameters (aColor1, aColor2, theBackground.BgGradientFillMethod()); |
475 | ||
476 | myBackgroundType = Graphic3d_TOB_GRADIENT; | |
c357e426 | 477 | } |
478 | ||
479 | // ======================================================================= | |
480 | // function : SetBackgroundImage | |
481 | // purpose : | |
482 | // ======================================================================= | |
483 | void OpenGl_View::SetBackgroundImage (const TCollection_AsciiString& theFilePath) | |
484 | { | |
485 | // Prepare aspect for texture storage | |
486 | myBackgroundImagePath = theFilePath; | |
487 | Handle(Graphic3d_AspectFillArea3d) anAspect = new Graphic3d_AspectFillArea3d(); | |
488 | Handle(Graphic3d_Texture2Dmanual) aTextureMap = new Graphic3d_Texture2Dmanual (TCollection_AsciiString (theFilePath)); | |
489 | aTextureMap->EnableRepeat(); | |
490 | aTextureMap->DisableModulate(); | |
491 | aTextureMap->GetParams()->SetGenMode (Graphic3d_TOTM_MANUAL, | |
492 | Graphic3d_Vec4 (0.0f, 0.0f, 0.0f, 0.0f), | |
493 | Graphic3d_Vec4 (0.0f, 0.0f, 0.0f, 0.0f)); | |
494 | anAspect->SetTextureMap (aTextureMap); | |
495 | anAspect->SetInteriorStyle (Aspect_IS_SOLID); | |
b6472664 | 496 | anAspect->SetSuppressBackFaces (false); |
c357e426 | 497 | // Enable texture mapping |
498 | if (aTextureMap->IsDone()) | |
2166f0fa | 499 | { |
c357e426 | 500 | anAspect->SetTextureMapOn(); |
2166f0fa SK |
501 | } |
502 | else | |
503 | { | |
c357e426 | 504 | anAspect->SetTextureMapOff(); |
505 | return; | |
506 | } | |
2166f0fa | 507 | |
c357e426 | 508 | // Set texture parameters |
509 | myTextureParams->SetAspect (anAspect); | |
077a220c | 510 | |
511 | myBackgroundType = Graphic3d_TOB_TEXTURE; | |
c357e426 | 512 | } |
2166f0fa | 513 | |
c357e426 | 514 | // ======================================================================= |
515 | // function : BackgroundImageStyle | |
516 | // purpose : | |
517 | // ======================================================================= | |
518 | Aspect_FillMethod OpenGl_View::BackgroundImageStyle() const | |
519 | { | |
077a220c | 520 | return myBackgrounds[Graphic3d_TOB_TEXTURE]->TextureFillMethod(); |
2166f0fa SK |
521 | } |
522 | ||
c357e426 | 523 | // ======================================================================= |
524 | // function : SetBackgroundImageStyle | |
525 | // purpose : | |
526 | // ======================================================================= | |
527 | void OpenGl_View::SetBackgroundImageStyle (const Aspect_FillMethod theFillStyle) | |
528 | { | |
077a220c | 529 | myBackgrounds[Graphic3d_TOB_TEXTURE]->SetTextureFillMethod (theFillStyle); |
530 | } | |
531 | ||
532 | // ======================================================================= | |
533 | // function : BackgroundCubeMap | |
534 | // purpose : | |
535 | // ======================================================================= | |
536 | Handle(Graphic3d_CubeMap) OpenGl_View::BackgroundCubeMap() const | |
537 | { | |
538 | return myBackgroundCubeMap; | |
539 | } | |
67312b79 | 540 | |
541 | // ======================================================================= | |
542 | // function : SpecIBLMapLevels | |
543 | // purpose : | |
544 | // ======================================================================= | |
545 | unsigned int OpenGl_View::SpecIBLMapLevels() const | |
546 | { | |
547 | return myPBREnvironment.IsNull() ? 0 : myPBREnvironment->SpecMapLevelsNumber(); | |
548 | } | |
077a220c | 549 | |
550 | // ======================================================================= | |
551 | // function : SetBackgroundCubeMap | |
552 | // purpose : | |
553 | // ======================================================================= | |
67312b79 | 554 | void OpenGl_View::SetBackgroundCubeMap (const Handle(Graphic3d_CubeMap)& theCubeMap, |
555 | Standard_Boolean theToUpdatePBREnv) | |
077a220c | 556 | { |
557 | myBackgroundCubeMap = theCubeMap; | |
67312b79 | 558 | if (theCubeMap.IsNull()) |
559 | { | |
560 | if (theToUpdatePBREnv) | |
561 | { | |
562 | myPBREnvRequest = OpenGl_PBREnvRequest_CLEAR; | |
563 | } | |
564 | if (myBackgroundType == Graphic3d_TOB_CUBEMAP) | |
565 | { | |
566 | myBackgroundType = Graphic3d_TOB_NONE; | |
567 | } | |
568 | return; | |
569 | } | |
077a220c | 570 | |
67312b79 | 571 | theCubeMap ->SetMipmapsGeneration (Standard_True); |
572 | Handle(Graphic3d_AspectFillArea3d) anAspect = new Graphic3d_AspectFillArea3d(); | |
573 | Handle(Graphic3d_TextureSet) aTextureSet = new Graphic3d_TextureSet (myBackgroundCubeMap); | |
574 | anAspect->SetInteriorStyle (Aspect_IS_SOLID); | |
575 | anAspect->SetSuppressBackFaces (false); | |
576 | anAspect->SetTextureSet (aTextureSet); | |
077a220c | 577 | |
578 | const Handle(OpenGl_Context)& aCtx = myWorkspace->GetGlContext(); | |
579 | if (!aCtx.IsNull()) | |
580 | { | |
581 | anAspect->SetShaderProgram (aCtx->ShaderManager()->GetBgCubeMapProgram()); | |
582 | } | |
67312b79 | 583 | anAspect->SetTextureMapOn (theCubeMap->IsDone()); |
584 | myCubeMapParams->SetAspect (anAspect); | |
077a220c | 585 | |
67312b79 | 586 | if (theToUpdatePBREnv) |
077a220c | 587 | { |
67312b79 | 588 | myPBREnvRequest = OpenGl_PBREnvRequest_BAKE; |
077a220c | 589 | } |
077a220c | 590 | const OpenGl_Aspects* anAspectsBackup = myWorkspace->SetAspects (myCubeMapParams); |
591 | myWorkspace->ApplyAspects(); | |
592 | myWorkspace->SetAspects (anAspectsBackup); | |
593 | myWorkspace->ApplyAspects(); | |
594 | ||
595 | myBackgroundType = Graphic3d_TOB_CUBEMAP; | |
c357e426 | 596 | } |
2166f0fa | 597 | |
c357e426 | 598 | //======================================================================= |
1c728f2d | 599 | //function : InsertLayerBefore |
c357e426 | 600 | //purpose : |
601 | //======================================================================= | |
1c728f2d | 602 | void OpenGl_View::InsertLayerBefore (const Graphic3d_ZLayerId theLayerId, |
077a220c | 603 | const Graphic3d_ZLayerSettings& theSettings, |
604 | const Graphic3d_ZLayerId theLayerAfter) | |
2166f0fa | 605 | { |
1c728f2d | 606 | myZLayers.InsertLayerBefore (theLayerId, theSettings, theLayerAfter); |
607 | } | |
608 | ||
609 | //======================================================================= | |
610 | //function : InsertLayerAfter | |
611 | //purpose : | |
612 | //======================================================================= | |
613 | void OpenGl_View::InsertLayerAfter (const Graphic3d_ZLayerId theLayerId, | |
077a220c | 614 | const Graphic3d_ZLayerSettings& theSettings, |
615 | const Graphic3d_ZLayerId theLayerBefore) | |
1c728f2d | 616 | { |
617 | myZLayers.InsertLayerAfter (theLayerId, theSettings, theLayerBefore); | |
2166f0fa SK |
618 | } |
619 | ||
c357e426 | 620 | //======================================================================= |
621 | //function : RemoveZLayer | |
622 | //purpose : | |
623 | //======================================================================= | |
624 | void OpenGl_View::RemoveZLayer (const Graphic3d_ZLayerId theLayerId) | |
625 | { | |
626 | myZLayers.RemoveLayer (theLayerId); | |
627 | } | |
2166f0fa | 628 | |
c357e426 | 629 | //======================================================================= |
630 | //function : SetZLayerSettings | |
631 | //purpose : | |
632 | //======================================================================= | |
633 | void OpenGl_View::SetZLayerSettings (const Graphic3d_ZLayerId theLayerId, | |
634 | const Graphic3d_ZLayerSettings& theSettings) | |
2166f0fa | 635 | { |
c357e426 | 636 | myZLayers.SetLayerSettings (theLayerId, theSettings); |
2166f0fa SK |
637 | } |
638 | ||
50d06d8f | 639 | //======================================================================= |
640 | //function : ZLayerMax | |
641 | //purpose : | |
642 | //======================================================================= | |
643 | Standard_Integer OpenGl_View::ZLayerMax() const | |
644 | { | |
645 | Standard_Integer aLayerMax = Graphic3d_ZLayerId_Default; | |
1c728f2d | 646 | for (NCollection_List<Handle(Graphic3d_Layer)>::Iterator aLayerIter (myZLayers.Layers()); aLayerIter.More(); aLayerIter.Next()) |
50d06d8f | 647 | { |
1c728f2d | 648 | aLayerMax = Max (aLayerMax, aLayerIter.Value()->LayerId()); |
50d06d8f | 649 | } |
50d06d8f | 650 | return aLayerMax; |
651 | } | |
652 | ||
653 | //======================================================================= | |
1c728f2d | 654 | //function : Layers |
655 | //purpose : | |
656 | //======================================================================= | |
657 | const NCollection_List<Handle(Graphic3d_Layer)>& OpenGl_View::Layers() const | |
658 | { | |
659 | return myZLayers.Layers(); | |
660 | } | |
661 | ||
662 | //======================================================================= | |
663 | //function : Layer | |
50d06d8f | 664 | //purpose : |
665 | //======================================================================= | |
1c728f2d | 666 | Handle(Graphic3d_Layer) OpenGl_View::Layer (const Graphic3d_ZLayerId theLayerId) const |
50d06d8f | 667 | { |
1c728f2d | 668 | Handle(Graphic3d_Layer) aLayer; |
669 | if (theLayerId != Graphic3d_ZLayerId_UNKNOWN) | |
50d06d8f | 670 | { |
1c728f2d | 671 | myZLayers.LayerIDs().Find (theLayerId, aLayer); |
50d06d8f | 672 | } |
1c728f2d | 673 | return aLayer; |
50d06d8f | 674 | } |
675 | ||
676 | //======================================================================= | |
1c728f2d | 677 | //function : MinMaxValues |
50d06d8f | 678 | //purpose : |
679 | //======================================================================= | |
1c728f2d | 680 | Bnd_Box OpenGl_View::MinMaxValues (const Standard_Boolean theToIncludeAuxiliary) const |
50d06d8f | 681 | { |
1c728f2d | 682 | if (!IsDefined()) |
50d06d8f | 683 | { |
1c728f2d | 684 | return Bnd_Box(); |
50d06d8f | 685 | } |
686 | ||
1c728f2d | 687 | Bnd_Box aBox = base_type::MinMaxValues (theToIncludeAuxiliary); |
688 | ||
71d8ccc7 | 689 | // add bounding box of gradient/texture background for proper Z-fit |
690 | if (theToIncludeAuxiliary | |
077a220c | 691 | && (myBackgrounds[Graphic3d_TOB_TEXTURE]->IsDefined() |
692 | || myBackgrounds[Graphic3d_TOB_GRADIENT]->IsDefined())) | |
71d8ccc7 | 693 | { |
1c728f2d | 694 | const Handle(Graphic3d_Camera)& aCamera = Camera(); |
695 | Graphic3d_Vec2i aWinSize; | |
696 | Window()->Size (aWinSize.x(), aWinSize.y()); | |
697 | ||
71d8ccc7 | 698 | // Background is drawn using 2D transformation persistence |
699 | // (e.g. it is actually placed in 3D coordinates within active camera position). | |
700 | // We add here full-screen plane with 2D transformation persistence | |
701 | // for simplicity (myBgTextureArray might define a little bit different options | |
702 | // but it is updated within ::Render()) | |
1c728f2d | 703 | const Graphic3d_Mat4d& aProjectionMat = aCamera->ProjectionMatrix(); |
704 | const Graphic3d_Mat4d& aWorldViewMat = aCamera->OrientationMatrix(); | |
7c3ef2f7 | 705 | Graphic3d_BndBox3d aBox2d (Graphic3d_Vec3d (0.0, 0.0, 0.0), |
1c728f2d | 706 | Graphic3d_Vec3d (double(aWinSize.x()), double(aWinSize.y()), 0.0)); |
71d8ccc7 | 707 | |
778cd667 | 708 | Graphic3d_TransformPers aTrsfPers (Graphic3d_TMF_2d, Aspect_TOTP_LEFT_LOWER); |
1c728f2d | 709 | aTrsfPers.Apply (aCamera, |
71d8ccc7 | 710 | aProjectionMat, |
711 | aWorldViewMat, | |
1c728f2d | 712 | aWinSize.x(), |
713 | aWinSize.y(), | |
71d8ccc7 | 714 | aBox2d); |
7c3ef2f7 | 715 | aBox.Add (gp_Pnt (aBox2d.CornerMin().x(), aBox2d.CornerMin().y(), aBox2d.CornerMin().z())); |
716 | aBox.Add (gp_Pnt (aBox2d.CornerMax().x(), aBox2d.CornerMax().y(), aBox2d.CornerMax().z())); | |
71d8ccc7 | 717 | } |
718 | ||
719 | return aBox; | |
50d06d8f | 720 | } |
721 | ||
722 | //======================================================================= | |
c357e426 | 723 | //function : FBO |
724 | //purpose : | |
725 | //======================================================================= | |
b128c892 | 726 | Handle(Standard_Transient) OpenGl_View::FBO() const |
c357e426 | 727 | { |
b128c892 | 728 | return Handle(Standard_Transient)(myFBO); |
c357e426 | 729 | } |
2166f0fa | 730 | |
c357e426 | 731 | //======================================================================= |
732 | //function : SetFBO | |
733 | //purpose : | |
734 | //======================================================================= | |
b128c892 | 735 | void OpenGl_View::SetFBO (const Handle(Standard_Transient)& theFbo) |
2166f0fa | 736 | { |
b128c892 | 737 | myFBO = Handle(OpenGl_FrameBuffer)::DownCast (theFbo); |
2166f0fa SK |
738 | } |
739 | ||
c357e426 | 740 | //======================================================================= |
741 | //function : FBOCreate | |
742 | //purpose : | |
743 | //======================================================================= | |
b128c892 | 744 | Handle(Standard_Transient) OpenGl_View::FBOCreate (const Standard_Integer theWidth, |
745 | const Standard_Integer theHeight) | |
c357e426 | 746 | { |
747 | return myWorkspace->FBOCreate (theWidth, theHeight); | |
748 | } | |
2166f0fa | 749 | |
c357e426 | 750 | //======================================================================= |
751 | //function : FBORelease | |
752 | //purpose : | |
753 | //======================================================================= | |
b128c892 | 754 | void OpenGl_View::FBORelease (Handle(Standard_Transient)& theFbo) |
2166f0fa | 755 | { |
b128c892 | 756 | Handle(OpenGl_FrameBuffer) aFrameBuffer = Handle(OpenGl_FrameBuffer)::DownCast (theFbo); |
757 | if (aFrameBuffer.IsNull()) | |
758 | { | |
759 | return; | |
760 | } | |
761 | ||
762 | myWorkspace->FBORelease (aFrameBuffer); | |
763 | theFbo.Nullify(); | |
c357e426 | 764 | } |
765 | ||
766 | //======================================================================= | |
767 | //function : FBOGetDimensions | |
768 | //purpose : | |
769 | //======================================================================= | |
b128c892 | 770 | void OpenGl_View::FBOGetDimensions (const Handle(Standard_Transient)& theFbo, |
c357e426 | 771 | Standard_Integer& theWidth, |
772 | Standard_Integer& theHeight, | |
773 | Standard_Integer& theWidthMax, | |
774 | Standard_Integer& theHeightMax) | |
775 | { | |
b128c892 | 776 | const Handle(OpenGl_FrameBuffer) aFrameBuffer = Handle(OpenGl_FrameBuffer)::DownCast (theFbo); |
777 | if (aFrameBuffer.IsNull()) | |
778 | { | |
779 | return; | |
780 | } | |
781 | ||
c357e426 | 782 | theWidth = aFrameBuffer->GetVPSizeX(); // current viewport size |
783 | theHeight = aFrameBuffer->GetVPSizeY(); | |
784 | theWidthMax = aFrameBuffer->GetSizeX(); // texture size | |
785 | theHeightMax = aFrameBuffer->GetSizeY(); | |
786 | } | |
787 | ||
788 | //======================================================================= | |
789 | //function : FBOChangeViewport | |
790 | //purpose : | |
791 | //======================================================================= | |
b128c892 | 792 | void OpenGl_View::FBOChangeViewport (const Handle(Standard_Transient)& theFbo, |
c357e426 | 793 | const Standard_Integer theWidth, |
794 | const Standard_Integer theHeight) | |
795 | { | |
b128c892 | 796 | const Handle(OpenGl_FrameBuffer) aFrameBuffer = Handle(OpenGl_FrameBuffer)::DownCast (theFbo); |
797 | if (aFrameBuffer.IsNull()) | |
798 | { | |
799 | return; | |
800 | } | |
801 | ||
c357e426 | 802 | aFrameBuffer->ChangeViewport (theWidth, theHeight); |
803 | } | |
804 | ||
c357e426 | 805 | //======================================================================= |
806 | //function : displayStructure | |
807 | //purpose : | |
808 | //======================================================================= | |
809 | void OpenGl_View::displayStructure (const Handle(Graphic3d_CStructure)& theStructure, | |
810 | const Standard_Integer thePriority) | |
811 | { | |
812 | const OpenGl_Structure* aStruct = reinterpret_cast<const OpenGl_Structure*> (theStructure.operator->()); | |
813 | const Graphic3d_ZLayerId aZLayer = aStruct->ZLayer(); | |
814 | myZLayers.AddStructure (aStruct, aZLayer, thePriority); | |
815 | } | |
816 | ||
817 | //======================================================================= | |
818 | //function : eraseStructure | |
819 | //purpose : | |
820 | //======================================================================= | |
821 | void OpenGl_View::eraseStructure (const Handle(Graphic3d_CStructure)& theStructure) | |
822 | { | |
823 | const OpenGl_Structure* aStruct = reinterpret_cast<const OpenGl_Structure*> (theStructure.operator->()); | |
824 | myZLayers.RemoveStructure (aStruct); | |
825 | } | |
826 | ||
827 | //======================================================================= | |
828 | //function : changeZLayer | |
829 | //purpose : | |
830 | //======================================================================= | |
831 | void OpenGl_View::changeZLayer (const Handle(Graphic3d_CStructure)& theStructure, | |
832 | const Graphic3d_ZLayerId theNewLayerId) | |
833 | { | |
834 | const Graphic3d_ZLayerId anOldLayer = theStructure->ZLayer(); | |
835 | const OpenGl_Structure* aStruct = reinterpret_cast<const OpenGl_Structure*> (theStructure.operator->()); | |
836 | myZLayers.ChangeLayer (aStruct, anOldLayer, theNewLayerId); | |
cfece3ef | 837 | Update (anOldLayer); |
838 | Update (theNewLayerId); | |
c357e426 | 839 | } |
840 | ||
841 | //======================================================================= | |
842 | //function : changePriority | |
843 | //purpose : | |
844 | //======================================================================= | |
845 | void OpenGl_View::changePriority (const Handle(Graphic3d_CStructure)& theStructure, | |
846 | const Standard_Integer theNewPriority) | |
847 | { | |
848 | const Graphic3d_ZLayerId aLayerId = theStructure->ZLayer(); | |
849 | const OpenGl_Structure* aStruct = reinterpret_cast<const OpenGl_Structure*> (theStructure.operator->()); | |
850 | myZLayers.ChangePriority (aStruct, aLayerId, theNewPriority); | |
2166f0fa | 851 | } |
26d9c835 | 852 | |
853 | //======================================================================= | |
854 | //function : DiagnosticInformation | |
855 | //purpose : | |
856 | //======================================================================= | |
857 | void OpenGl_View::DiagnosticInformation (TColStd_IndexedDataMapOfStringString& theDict, | |
858 | Graphic3d_DiagnosticInfo theFlags) const | |
859 | { | |
860 | Handle(OpenGl_Context) aCtx = myWorkspace->GetGlContext(); | |
861 | if (!myWorkspace->Activate() | |
862 | || aCtx.IsNull()) | |
863 | { | |
864 | return; | |
865 | } | |
866 | ||
867 | aCtx->DiagnosticInformation (theDict, theFlags); | |
868 | if ((theFlags & Graphic3d_DiagnosticInfo_FrameBuffer) != 0) | |
869 | { | |
870 | TCollection_AsciiString aResRatio (myRenderParams.ResolutionRatio()); | |
871 | theDict.ChangeFromIndex (theDict.Add ("ResolutionRatio", aResRatio)) = aResRatio; | |
872 | } | |
873 | } | |
79b544e6 | 874 | |
875 | //======================================================================= | |
876 | //function : StatisticInformation | |
877 | //purpose : | |
878 | //======================================================================= | |
879 | void OpenGl_View::StatisticInformation (TColStd_IndexedDataMapOfStringString& theDict) const | |
880 | { | |
881 | if (const Handle(OpenGl_Context)& aCtx = myWorkspace->GetGlContext()) | |
882 | { | |
883 | const Handle(OpenGl_FrameStats)& aStats = aCtx->FrameStats(); | |
884 | const Graphic3d_RenderingParams& aRendParams = myWorkspace->View()->RenderingParams(); | |
885 | aStats->FormatStats (theDict, aRendParams.CollectedStats); | |
886 | } | |
887 | } | |
888 | ||
889 | //======================================================================= | |
890 | //function : StatisticInformation | |
891 | //purpose : | |
892 | //======================================================================= | |
893 | TCollection_AsciiString OpenGl_View::StatisticInformation() const | |
894 | { | |
895 | if (const Handle(OpenGl_Context)& aCtx = myWorkspace->GetGlContext()) | |
896 | { | |
897 | const Handle(OpenGl_FrameStats)& aStats = aCtx->FrameStats(); | |
898 | const Graphic3d_RenderingParams& aRendParams = myWorkspace->View()->RenderingParams(); | |
899 | return aStats->FormatStats (aRendParams.CollectedStats); | |
900 | } | |
901 | return TCollection_AsciiString(); | |
902 | } |