0029293: Boolean Operations algorithm does not preserve the orientations of the faces
[occt.git] / src / OpenGl / OpenGl_View_Redraw.cxx
CommitLineData
c357e426 1// Created on: 2011-09-20
2// Created by: Sergey ZERCHANINOV
3// Copyright (c) 2011-2014 OPEN CASCADE SAS
4//
5// This file is part of Open CASCADE Technology software library.
6//
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
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.
12//
13// Alternatively, this file may be used under the terms of Open CASCADE
14// commercial license or contractual agreement.
15
16#include <stdio.h>
17#include <stdlib.h>
18
19#include <OpenGl_GlCore11.hxx>
c357e426 20
21#include <Graphic3d_GraphicDriver.hxx>
851dacdb 22#include <Graphic3d_StructureManager.hxx>
c357e426 23#include <Graphic3d_TextureParams.hxx>
24#include <Graphic3d_Texture2Dmanual.hxx>
25#include <Graphic3d_TransformUtils.hxx>
26#include <Image_AlienPixMap.hxx>
27
28#include <NCollection_Mat4.hxx>
29
30#include <OpenGl_AspectLine.hxx>
31#include <OpenGl_Context.hxx>
32#include <OpenGl_Matrix.hxx>
33#include <OpenGl_Workspace.hxx>
34#include <OpenGl_View.hxx>
c357e426 35#include <OpenGl_GraduatedTrihedron.hxx>
36#include <OpenGl_PrimitiveArray.hxx>
c357e426 37#include <OpenGl_ShaderManager.hxx>
38#include <OpenGl_ShaderProgram.hxx>
39#include <OpenGl_Structure.hxx>
40#include <OpenGl_ArbFBO.hxx>
41
a0b49de4 42namespace
43{
44 //! Format Frame Buffer format for logging messages.
45 static TCollection_AsciiString printFboFormat (const Handle(OpenGl_FrameBuffer)& theFbo)
46 {
47 return TCollection_AsciiString() + theFbo->GetInitVPSizeX() + "x" + theFbo->GetInitVPSizeY() + "@" + theFbo->NbSamples();
48 }
49
50 //! Return TRUE if Frame Buffer initialized has failed with the same parameters.
51 static bool checkWasFailedFbo (const Handle(OpenGl_FrameBuffer)& theFboToCheck,
52 Standard_Integer theSizeX,
53 Standard_Integer theSizeY,
54 Standard_Integer theNbSamples)
55 {
56 return !theFboToCheck->IsValid()
57 && theFboToCheck->GetInitVPSizeX() == theSizeX
58 && theFboToCheck->GetInitVPSizeY() == theSizeY
59 && theFboToCheck->NbSamples() == theNbSamples;
60 }
61
62 //! Return TRUE if Frame Buffer initialized has failed with the same parameters.
63 static bool checkWasFailedFbo (const Handle(OpenGl_FrameBuffer)& theFboToCheck,
64 const Handle(OpenGl_FrameBuffer)& theFboRef)
65 {
66 return checkWasFailedFbo (theFboToCheck, theFboRef->GetVPSizeX(), theFboRef->GetVPSizeY(), theFboRef->NbSamples());
67 }
68}
69
c357e426 70//=======================================================================
a521d90d 71//function : drawBackground
c357e426 72//purpose :
73//=======================================================================
a521d90d 74void OpenGl_View::drawBackground (const Handle(OpenGl_Workspace)& theWorkspace)
c357e426 75{
76 const Handle(OpenGl_Context)& aCtx = theWorkspace->GetGlContext();
77
8613985b 78 if (!myBgTextureArray->IsDefined() // no texture
79 && !myBgGradientArray->IsDefined()) // no gradient
c357e426 80 {
81 return;
82 }
83
84 const Standard_Boolean wasUsedZBuffer = theWorkspace->SetUseZBuffer (Standard_False);
85 if (wasUsedZBuffer)
86 {
87 aCtx->core11fwd->glDisable (GL_DEPTH_TEST);
88 }
89
c357e426 90 // Drawing background gradient if:
91 // - gradient fill type is not Aspect_GFM_NONE and
92 // - either background texture is no specified or it is drawn in Aspect_FM_CENTERED mode
93 if (myBgGradientArray->IsDefined()
b6472664 94 && (!myTextureParams->Aspect()->ToMapTexture()
c357e426 95 || myBgTextureArray->TextureFillMethod() == Aspect_FM_CENTERED
96 || myBgTextureArray->TextureFillMethod() == Aspect_FM_NONE))
97 {
98 #if !defined(GL_ES_VERSION_2_0)
99 GLint aShadingModelOld = GL_SMOOTH;
100 if (aCtx->core11 != NULL)
101 {
102 aCtx->core11fwd->glDisable (GL_LIGHTING);
103 aCtx->core11fwd->glGetIntegerv (GL_SHADE_MODEL, &aShadingModelOld);
104 aCtx->core11->glShadeModel (GL_SMOOTH);
105 }
106 #endif
107
c357e426 108 myBgGradientArray->Render (theWorkspace);
109
110 #if !defined(GL_ES_VERSION_2_0)
111 if (aCtx->core11 != NULL)
112 {
113 aCtx->core11->glShadeModel (aShadingModelOld);
114 }
115 #endif
116 }
117
118 // Drawing background image if it is defined
119 // (texture is defined and fill type is not Aspect_FM_NONE)
120 if (myBgTextureArray->IsDefined()
b6472664 121 && myTextureParams->Aspect()->ToMapTexture())
c357e426 122 {
123 aCtx->core11fwd->glDisable (GL_BLEND);
124
125 const OpenGl_AspectFace* anOldAspectFace = theWorkspace->SetAspectFace (myTextureParams);
c357e426 126 myBgTextureArray->Render (theWorkspace);
c357e426 127 theWorkspace->SetAspectFace (anOldAspectFace);
128 }
129
c357e426 130 if (wasUsedZBuffer)
131 {
132 theWorkspace->SetUseZBuffer (Standard_True);
133 aCtx->core11fwd->glEnable (GL_DEPTH_TEST);
134 }
135}
136
137//=======================================================================
138//function : Redraw
139//purpose :
140//=======================================================================
141void OpenGl_View::Redraw()
142{
7ccf8676 143 const Standard_Boolean wasDisabledMSAA = myToDisableMSAA;
144 const Standard_Boolean hadFboBlit = myHasFboBlit;
c357e426 145 if (myRenderParams.Method == Graphic3d_RM_RAYTRACING
146 && !myCaps->vboDisable
147 && !myCaps->keepArrayData)
148 {
851dacdb 149 // caps are shared across all views, thus we need to invalidate all of them
150 // if (myWasRedrawnGL) { myStructureManager->SetDeviceLost(); }
151 myDriver->setDeviceLost();
c357e426 152 myCaps->keepArrayData = Standard_True;
153 }
154
155 if (!myWorkspace->Activate())
156 {
157 return;
158 }
159
160 myWindow->SetSwapInterval();
161
162 ++myFrameCounter;
7c3ef2f7 163 const Graphic3d_StereoMode aStereoMode = myRenderParams.StereoMode;
164 Graphic3d_Camera::Projection aProjectType = myCamera->ProjectionType();
165 Handle(OpenGl_Context) aCtx = myWorkspace->GetGlContext();
c357e426 166
167 // release pending GL resources
168 aCtx->ReleaseDelayed();
169
170 // fetch OpenGl context state
171 aCtx->FetchState();
172
b128c892 173 OpenGl_FrameBuffer* aFrameBuffer = myFBO.operator->();
c357e426 174 bool toSwap = aCtx->IsRender()
175 && !aCtx->caps->buffersNoSwap
176 && aFrameBuffer == NULL;
177
178 Standard_Integer aSizeX = aFrameBuffer != NULL ? aFrameBuffer->GetVPSizeX() : myWindow->Width();
179 Standard_Integer aSizeY = aFrameBuffer != NULL ? aFrameBuffer->GetVPSizeY() : myWindow->Height();
56689b27 180 Standard_Integer aRendSizeX = Standard_Integer(myRenderParams.RenderResolutionScale * aSizeX + 0.5f);
181 Standard_Integer aRendSizeY = Standard_Integer(myRenderParams.RenderResolutionScale * aSizeY + 0.5f);
c357e426 182
3c4b62a4 183 // determine multisampling parameters
56689b27 184 Standard_Integer aNbSamples = !myToDisableMSAA && aSizeX == aRendSizeX
7ccf8676 185 ? Max (Min (myRenderParams.NbMsaaSamples, aCtx->MaxMsaaSamples()), 0)
186 : 0;
3c4b62a4 187 if (aNbSamples != 0)
188 {
189 aNbSamples = OpenGl_Context::GetPowerOfTwo (aNbSamples, aCtx->MaxMsaaSamples());
190 }
191
a1073ae2 192 bool toUseOit = myRenderParams.TransparencyMethod == Graphic3d_RTM_BLEND_OIT
193 && checkOitCompatibility (aCtx, aNbSamples > 0);
194
195 const bool toInitImmediateFbo = myTransientDrawToFront
196 && (!aCtx->caps->useSystemBuffer || (toUseOit && HasImmediateStructures()));
197
c357e426 198 if ( aFrameBuffer == NULL
199 && !aCtx->DefaultFrameBuffer().IsNull()
200 && aCtx->DefaultFrameBuffer()->IsValid())
201 {
202 aFrameBuffer = aCtx->DefaultFrameBuffer().operator->();
203 }
204
205 if (myHasFboBlit
3c4b62a4 206 && (myTransientDrawToFront
207 || aProjectType == Graphic3d_Camera::Projection_Stereo
56689b27 208 || aNbSamples != 0
a1073ae2 209 || toUseOit
56689b27 210 || aSizeX != aRendSizeX))
c357e426 211 {
56689b27 212 if (myMainSceneFbos[0]->GetVPSizeX() != aRendSizeX
213 || myMainSceneFbos[0]->GetVPSizeY() != aRendSizeY
3c4b62a4 214 || myMainSceneFbos[0]->NbSamples() != aNbSamples)
c357e426 215 {
521b0d7f 216 if (!myTransientDrawToFront)
217 {
218 myImmediateSceneFbos[0]->Release (aCtx.operator->());
219 myImmediateSceneFbos[1]->Release (aCtx.operator->());
220 myImmediateSceneFbos[0]->ChangeViewport (0, 0);
221 myImmediateSceneFbos[1]->ChangeViewport (0, 0);
222 }
223
c357e426 224 // prepare FBOs containing main scene
225 // for further blitting and rendering immediate presentations on top
226 if (aCtx->core20fwd != NULL)
227 {
a0b49de4 228 const bool wasFailedMain0 = checkWasFailedFbo (myMainSceneFbos[0], aRendSizeX, aRendSizeY, aNbSamples);
229 if (!myMainSceneFbos[0]->Init (aCtx, aRendSizeX, aRendSizeY, myFboColorFormat, myFboDepthFormat, aNbSamples)
230 && !wasFailedMain0)
231 {
232 TCollection_ExtendedString aMsg = TCollection_ExtendedString() + "Error! Main FBO "
233 + printFboFormat (myMainSceneFbos[0]) + " initialization has failed";
234 aCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH, aMsg);
235 }
c357e426 236 }
a1073ae2 237 }
238 if (myMainSceneFbos[0]->IsValid() && (toInitImmediateFbo || myImmediateSceneFbos[0]->IsValid()))
239 {
a0b49de4 240 const bool wasFailedImm0 = checkWasFailedFbo (myImmediateSceneFbos[0], myMainSceneFbos[0]);
241 if (!myImmediateSceneFbos[0]->InitLazy (aCtx, *myMainSceneFbos[0])
242 && !wasFailedImm0)
243 {
244 TCollection_ExtendedString aMsg = TCollection_ExtendedString() + "Error! Immediate FBO "
245 + printFboFormat (myImmediateSceneFbos[0]) + " initialization has failed";
246 aCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH, aMsg);
247 }
c357e426 248 }
249 }
250 else
251 {
252 myMainSceneFbos [0]->Release (aCtx.operator->());
253 myMainSceneFbos [1]->Release (aCtx.operator->());
254 myImmediateSceneFbos[0]->Release (aCtx.operator->());
255 myImmediateSceneFbos[1]->Release (aCtx.operator->());
256 myMainSceneFbos [0]->ChangeViewport (0, 0);
257 myMainSceneFbos [1]->ChangeViewport (0, 0);
258 myImmediateSceneFbos[0]->ChangeViewport (0, 0);
259 myImmediateSceneFbos[1]->ChangeViewport (0, 0);
260 }
261
262 if (aProjectType == Graphic3d_Camera::Projection_Stereo
263 && myMainSceneFbos[0]->IsValid())
264 {
a0b49de4 265 const bool wasFailedMain1 = checkWasFailedFbo (myMainSceneFbos[1], myMainSceneFbos[0]);
266 if (!myMainSceneFbos[1]->InitLazy (aCtx, *myMainSceneFbos[0])
267 && !wasFailedMain1)
268 {
269 TCollection_ExtendedString aMsg = TCollection_ExtendedString() + "Error! Main FBO (second) "
270 + printFboFormat (myMainSceneFbos[1]) + " initialization has failed";
271 aCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH, aMsg);
272 }
c357e426 273 if (!myMainSceneFbos[1]->IsValid())
274 {
275 // no enough memory?
276 aProjectType = Graphic3d_Camera::Projection_Perspective;
277 }
278 else if (!myTransientDrawToFront)
279 {
280 //
281 }
282 else if (!aCtx->HasStereoBuffers() || aStereoMode != Graphic3d_StereoMode_QuadBuffer)
283 {
a0b49de4 284 const bool wasFailedImm0 = checkWasFailedFbo (myImmediateSceneFbos[0], myMainSceneFbos[0]);
285 const bool wasFailedImm1 = checkWasFailedFbo (myImmediateSceneFbos[1], myMainSceneFbos[0]);
286 if (!myImmediateSceneFbos[0]->InitLazy (aCtx, *myMainSceneFbos[0])
287 && !wasFailedImm0)
288 {
289 TCollection_ExtendedString aMsg = TCollection_ExtendedString() + "Error! Immediate FBO (first) "
290 + printFboFormat (myImmediateSceneFbos[0]) + " initialization has failed";
291 aCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH, aMsg);
292 }
293 if (!myImmediateSceneFbos[1]->InitLazy (aCtx, *myMainSceneFbos[0])
294 && !wasFailedImm1)
295 {
296 TCollection_ExtendedString aMsg = TCollection_ExtendedString() + "Error! Immediate FBO (first) "
297 + printFboFormat (myImmediateSceneFbos[1]) + " initialization has failed";
298 aCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH, aMsg);
299 }
c357e426 300 if (!myImmediateSceneFbos[0]->IsValid()
301 || !myImmediateSceneFbos[1]->IsValid())
302 {
303 aProjectType = Graphic3d_Camera::Projection_Perspective;
304 }
305 }
306 }
307
a1073ae2 308 // create color and coverage accumulation buffers required for OIT algorithm
309 if (toUseOit)
310 {
311 Standard_Integer anFboIt = 0;
312 for (; anFboIt < 2; ++anFboIt)
313 {
314 Handle(OpenGl_FrameBuffer)& aMainSceneFbo = myMainSceneFbos [anFboIt];
315 Handle(OpenGl_FrameBuffer)& aMainSceneFboOit = myMainSceneFbosOit [anFboIt];
316 Handle(OpenGl_FrameBuffer)& anImmediateSceneFbo = myImmediateSceneFbos [anFboIt];
317 Handle(OpenGl_FrameBuffer)& anImmediateSceneFboOit = myImmediateSceneFbosOit[anFboIt];
318 if (aMainSceneFbo->IsValid()
319 && (aMainSceneFboOit->GetVPSizeX() != aRendSizeX
320 || aMainSceneFboOit->GetVPSizeY() != aRendSizeY
321 || aMainSceneFboOit->NbSamples() != aNbSamples))
322 {
323 Standard_Integer aColorConfig = 0;
324 for (;;) // seemly responding to driver limitation (GL_FRAMEBUFFER_UNSUPPORTED)
325 {
326 if (myFboOitColorConfig.IsEmpty())
327 {
328 if (!chooseOitColorConfiguration (aCtx, aColorConfig++, myFboOitColorConfig))
329 {
330 break;
331 }
332 }
333 if (aMainSceneFboOit->Init (aCtx, aRendSizeX, aRendSizeY, myFboOitColorConfig, aMainSceneFbo->DepthStencilTexture(), aNbSamples))
334 {
335 break;
336 }
337 myFboOitColorConfig.Clear();
338 }
339 if (!aMainSceneFboOit->IsValid())
340 {
341 break;
342 }
343 }
344 else if (!aMainSceneFbo->IsValid())
345 {
346 aMainSceneFboOit->Release (aCtx.operator->());
347 aMainSceneFboOit->ChangeViewport (0, 0);
348 }
349
350 if (anImmediateSceneFbo->IsValid()
351 && (anImmediateSceneFboOit->GetVPSizeX() != aRendSizeX
352 || anImmediateSceneFboOit->GetVPSizeY() != aRendSizeY
353 || anImmediateSceneFboOit->NbSamples() != aNbSamples))
354 {
355 if (!anImmediateSceneFboOit->Init (aCtx, aRendSizeX, aRendSizeY, myFboOitColorConfig,
356 anImmediateSceneFbo->DepthStencilTexture(), aNbSamples))
357 {
358 break;
359 }
360 }
361 else if (!anImmediateSceneFbo->IsValid())
362 {
363 anImmediateSceneFboOit->Release (aCtx.operator->());
364 anImmediateSceneFboOit->ChangeViewport (0, 0);
365 }
366 }
367 if (anFboIt == 0) // only the first OIT framebuffer is mandatory
368 {
369 aCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH,
370 "Initialization of float texture framebuffer for use with\n"
371 " blended order-independent transparency rendering algorithm has failed.\n"
372 " Blended order-independent transparency will not be available.\n");
373 if (aNbSamples > 0)
374 {
375 myToDisableOITMSAA = Standard_True;
376 }
377 else
378 {
379 myToDisableOIT = Standard_True;
380 }
381 toUseOit = false;
382 }
383 }
384 if (!toUseOit && myMainSceneFbosOit[0]->IsValid())
385 {
386 myMainSceneFbosOit [0]->Release (aCtx.operator->());
387 myMainSceneFbosOit [1]->Release (aCtx.operator->());
388 myImmediateSceneFbosOit[0]->Release (aCtx.operator->());
389 myImmediateSceneFbosOit[1]->Release (aCtx.operator->());
390 myMainSceneFbosOit [0]->ChangeViewport (0, 0);
391 myMainSceneFbosOit [1]->ChangeViewport (0, 0);
392 myImmediateSceneFbosOit[0]->ChangeViewport (0, 0);
393 myImmediateSceneFbosOit[1]->ChangeViewport (0, 0);
394 }
395
c357e426 396 if (aProjectType == Graphic3d_Camera::Projection_Stereo)
397 {
398 OpenGl_FrameBuffer* aMainFbos[2] =
399 {
400 myMainSceneFbos[0]->IsValid() ? myMainSceneFbos[0].operator->() : NULL,
401 myMainSceneFbos[1]->IsValid() ? myMainSceneFbos[1].operator->() : NULL
402 };
a1073ae2 403 OpenGl_FrameBuffer* aMainFbosOit[2] =
404 {
405 myMainSceneFbosOit[0]->IsValid() ? myMainSceneFbosOit[0].operator->() : NULL,
406 myMainSceneFbosOit[1]->IsValid() ? myMainSceneFbosOit[1].operator->() :
407 myMainSceneFbosOit[0]->IsValid() ? myMainSceneFbosOit[0].operator->() : NULL
408 };
409
c357e426 410 OpenGl_FrameBuffer* anImmFbos[2] =
411 {
412 myImmediateSceneFbos[0]->IsValid() ? myImmediateSceneFbos[0].operator->() : NULL,
413 myImmediateSceneFbos[1]->IsValid() ? myImmediateSceneFbos[1].operator->() : NULL
414 };
a1073ae2 415 OpenGl_FrameBuffer* anImmFbosOit[2] =
416 {
417 myImmediateSceneFbosOit[0]->IsValid() ? myImmediateSceneFbosOit[0].operator->() : NULL,
418 myImmediateSceneFbosOit[1]->IsValid() ? myImmediateSceneFbosOit[1].operator->() :
419 myImmediateSceneFbosOit[0]->IsValid() ? myImmediateSceneFbosOit[0].operator->() : NULL
420 };
c357e426 421
422 if (!myTransientDrawToFront)
423 {
a1073ae2 424 anImmFbos [0] = aMainFbos [0];
425 anImmFbos [1] = aMainFbos [1];
426 anImmFbosOit[0] = aMainFbosOit[0];
427 anImmFbosOit[1] = aMainFbosOit[1];
c357e426 428 }
429 else if (aStereoMode == Graphic3d_StereoMode_SoftPageFlip
430 || aStereoMode == Graphic3d_StereoMode_QuadBuffer)
431 {
a1073ae2 432 anImmFbos [0] = NULL;
433 anImmFbos [1] = NULL;
434 anImmFbosOit[0] = NULL;
435 anImmFbosOit[1] = NULL;
c357e426 436 }
437
438 #if !defined(GL_ES_VERSION_2_0)
439 aCtx->SetReadDrawBuffer (aStereoMode == Graphic3d_StereoMode_QuadBuffer ? GL_BACK_LEFT : GL_BACK);
440 #endif
56689b27 441 aCtx->SetResolution (myRenderParams.Resolution, myRenderParams.ResolutionRatio(),
442 aMainFbos[0] != NULL ? myRenderParams.RenderResolutionScale : 1.0f);
443
a1073ae2 444 redraw (Graphic3d_Camera::Projection_MonoLeftEye, aMainFbos[0], aMainFbosOit[0]);
c357e426 445 myBackBufferRestored = Standard_True;
446 myIsImmediateDrawn = Standard_False;
447 #if !defined(GL_ES_VERSION_2_0)
448 aCtx->SetReadDrawBuffer (aStereoMode == Graphic3d_StereoMode_QuadBuffer ? GL_BACK_LEFT : GL_BACK);
449 #endif
56689b27 450 aCtx->SetResolution (myRenderParams.Resolution, myRenderParams.ResolutionRatio(),
451 anImmFbos[0] != NULL ? myRenderParams.RenderResolutionScale : 1.0f);
a1073ae2 452 if (!redrawImmediate (Graphic3d_Camera::Projection_MonoLeftEye, aMainFbos[0], anImmFbos[0], anImmFbosOit[0]))
c357e426 453 {
454 toSwap = false;
455 }
456 else if (aStereoMode == Graphic3d_StereoMode_SoftPageFlip && toSwap)
457 {
458 aCtx->SwapBuffers();
459 }
460
461 #if !defined(GL_ES_VERSION_2_0)
462 aCtx->SetReadDrawBuffer (aStereoMode == Graphic3d_StereoMode_QuadBuffer ? GL_BACK_RIGHT : GL_BACK);
463 #endif
56689b27 464 aCtx->SetResolution (myRenderParams.Resolution, myRenderParams.ResolutionRatio(),
465 aMainFbos[1] != NULL ? myRenderParams.RenderResolutionScale : 1.0f);
466
a1073ae2 467 redraw (Graphic3d_Camera::Projection_MonoRightEye, aMainFbos[1], aMainFbosOit[1]);
c357e426 468 myBackBufferRestored = Standard_True;
469 myIsImmediateDrawn = Standard_False;
56689b27 470 aCtx->SetResolution (myRenderParams.Resolution, myRenderParams.ResolutionRatio(),
471 anImmFbos[1] != NULL ? myRenderParams.RenderResolutionScale : 1.0f);
a1073ae2 472 if (!redrawImmediate (Graphic3d_Camera::Projection_MonoRightEye, aMainFbos[1], anImmFbos[1], anImmFbosOit[1]))
c357e426 473 {
474 toSwap = false;
475 }
476
477 if (anImmFbos[0] != NULL)
478 {
56689b27 479 aCtx->SetResolution (myRenderParams.Resolution, myRenderParams.ResolutionRatio(), 1.0f);
3c4b62a4 480 drawStereoPair (aFrameBuffer);
c357e426 481 }
482 }
483 else
484 {
a1073ae2 485 OpenGl_FrameBuffer* aMainFbo = myMainSceneFbos[0]->IsValid() ? myMainSceneFbos[0].operator->() : aFrameBuffer;
486 OpenGl_FrameBuffer* aMainFboOit = myMainSceneFbosOit[0]->IsValid() ? myMainSceneFbosOit[0].operator->() : NULL;
487 OpenGl_FrameBuffer* anImmFbo = aFrameBuffer;
488 OpenGl_FrameBuffer* anImmFboOit = NULL;
489 if (!myTransientDrawToFront)
c357e426 490 {
a1073ae2 491 anImmFbo = aMainFbo;
492 anImmFboOit = aMainFboOit;
c357e426 493 }
a1073ae2 494 else if (myImmediateSceneFbos[0]->IsValid())
521b0d7f 495 {
a1073ae2 496 anImmFbo = myImmediateSceneFbos[0].operator->();
497 anImmFboOit = myImmediateSceneFbosOit[0]->IsValid() ? myImmediateSceneFbosOit[0].operator->() : NULL;
521b0d7f 498 }
c357e426 499
500 #if !defined(GL_ES_VERSION_2_0)
3bffef55 501 if (aMainFbo == NULL)
c357e426 502 {
503 aCtx->SetReadDrawBuffer (GL_BACK);
504 }
505 #endif
56689b27 506 aCtx->SetResolution (myRenderParams.Resolution, myRenderParams.ResolutionRatio(),
507 aMainFbo != aFrameBuffer ? myRenderParams.RenderResolutionScale : 1.0f);
a1073ae2 508
509 redraw (aProjectType, aMainFbo, aMainFboOit);
c357e426 510 myBackBufferRestored = Standard_True;
511 myIsImmediateDrawn = Standard_False;
56689b27 512 aCtx->SetResolution (myRenderParams.Resolution, myRenderParams.ResolutionRatio(),
513 anImmFbo != aFrameBuffer ? myRenderParams.RenderResolutionScale : 1.0f);
a1073ae2 514 if (!redrawImmediate (aProjectType, aMainFbo, anImmFbo, anImmFboOit))
c357e426 515 {
516 toSwap = false;
517 }
518
519 if (anImmFbo != NULL
520 && anImmFbo != aFrameBuffer)
521 {
522 blitBuffers (anImmFbo, aFrameBuffer, myToFlipOutput);
523 }
524 }
525
bf02aa7d 526 if (myRenderParams.Method == Graphic3d_RM_RAYTRACING
527 && myRenderParams.IsGlobalIlluminationEnabled)
528 {
529 myAccumFrames++;
530 }
531
c357e426 532 // bind default FBO
533 bindDefaultFbo();
534
7ccf8676 535 if (wasDisabledMSAA != myToDisableMSAA
536 || hadFboBlit != myHasFboBlit)
537 {
538 // retry on error
539 Redraw();
540 }
541
8613985b 542 // reset state for safety
543 aCtx->BindProgram (Handle(OpenGl_ShaderProgram)());
544 aCtx->ShaderManager()->PushState (Handle(OpenGl_ShaderProgram)());
545
c357e426 546 // Swap the buffers
547 if (toSwap)
548 {
549 aCtx->SwapBuffers();
550 if (!myMainSceneFbos[0]->IsValid())
551 {
552 myBackBufferRestored = Standard_False;
553 }
554 }
555 else
556 {
557 aCtx->core11fwd->glFlush();
558 }
559
560 // reset render mode state
561 aCtx->FetchState();
562
563 myWasRedrawnGL = Standard_True;
564}
565
566// =======================================================================
567// function : RedrawImmediate
568// purpose :
569// =======================================================================
570void OpenGl_View::RedrawImmediate()
571{
572 if (!myWorkspace->Activate())
573 return;
574
575 Handle(OpenGl_Context) aCtx = myWorkspace->GetGlContext();
576 if (!myTransientDrawToFront
577 || !myBackBufferRestored
578 || (aCtx->caps->buffersNoSwap && !myMainSceneFbos[0]->IsValid()))
579 {
580 Redraw();
581 return;
582 }
583
584 const Graphic3d_StereoMode aStereoMode = myRenderParams.StereoMode;
585 Graphic3d_Camera::Projection aProjectType = myCamera->ProjectionType();
b128c892 586 OpenGl_FrameBuffer* aFrameBuffer = myFBO.operator->();
c357e426 587
588 if ( aFrameBuffer == NULL
589 && !aCtx->DefaultFrameBuffer().IsNull()
590 && aCtx->DefaultFrameBuffer()->IsValid())
591 {
592 aFrameBuffer = aCtx->DefaultFrameBuffer().operator->();
593 }
594
595 if (aProjectType == Graphic3d_Camera::Projection_Stereo)
596 {
597 if (myMainSceneFbos[0]->IsValid()
598 && !myMainSceneFbos[1]->IsValid())
599 {
600 aProjectType = Graphic3d_Camera::Projection_Perspective;
601 }
602 }
603
604 bool toSwap = false;
605 if (aProjectType == Graphic3d_Camera::Projection_Stereo)
606 {
607 OpenGl_FrameBuffer* aMainFbos[2] =
608 {
609 myMainSceneFbos[0]->IsValid() ? myMainSceneFbos[0].operator->() : NULL,
610 myMainSceneFbos[1]->IsValid() ? myMainSceneFbos[1].operator->() : NULL
611 };
612 OpenGl_FrameBuffer* anImmFbos[2] =
613 {
614 myImmediateSceneFbos[0]->IsValid() ? myImmediateSceneFbos[0].operator->() : NULL,
615 myImmediateSceneFbos[1]->IsValid() ? myImmediateSceneFbos[1].operator->() : NULL
616 };
a1073ae2 617 OpenGl_FrameBuffer* anImmFbosOit[2] =
618 {
619 myImmediateSceneFbosOit[0]->IsValid() ? myImmediateSceneFbosOit[0].operator->() : NULL,
620 myImmediateSceneFbosOit[1]->IsValid() ? myImmediateSceneFbosOit[1].operator->() :
621 myImmediateSceneFbosOit[0]->IsValid() ? myImmediateSceneFbosOit[0].operator->() : NULL
622 };
c357e426 623 if (aStereoMode == Graphic3d_StereoMode_SoftPageFlip
624 || aStereoMode == Graphic3d_StereoMode_QuadBuffer)
625 {
a1073ae2 626 anImmFbos[0] = NULL;
627 anImmFbos[1] = NULL;
628 anImmFbosOit[0] = NULL;
629 anImmFbosOit[1] = NULL;
c357e426 630 }
631
632 if (aCtx->arbFBO != NULL)
633 {
634 aCtx->arbFBO->glBindFramebuffer (GL_FRAMEBUFFER, OpenGl_FrameBuffer::NO_FRAMEBUFFER);
635 }
636 #if !defined(GL_ES_VERSION_2_0)
637 if (anImmFbos[0] == NULL)
638 {
639 aCtx->SetReadDrawBuffer (aStereoMode == Graphic3d_StereoMode_QuadBuffer ? GL_BACK_LEFT : GL_BACK);
640 }
641 #endif
56689b27 642
643 aCtx->SetResolution (myRenderParams.Resolution, myRenderParams.ResolutionRatio(),
644 anImmFbos[0] != NULL ? myRenderParams.RenderResolutionScale : 1.0f);
c357e426 645 toSwap = redrawImmediate (Graphic3d_Camera::Projection_MonoLeftEye,
646 aMainFbos[0],
647 anImmFbos[0],
a1073ae2 648 anImmFbosOit[0],
c357e426 649 Standard_True) || toSwap;
650 if (aStereoMode == Graphic3d_StereoMode_SoftPageFlip
651 && toSwap
652 && !aCtx->caps->buffersNoSwap)
653 {
654 aCtx->SwapBuffers();
655 }
656
657 if (aCtx->arbFBO != NULL)
658 {
659 aCtx->arbFBO->glBindFramebuffer (GL_FRAMEBUFFER, OpenGl_FrameBuffer::NO_FRAMEBUFFER);
660 }
661 #if !defined(GL_ES_VERSION_2_0)
662 if (anImmFbos[1] == NULL)
663 {
664 aCtx->SetReadDrawBuffer (aStereoMode == Graphic3d_StereoMode_QuadBuffer ? GL_BACK_RIGHT : GL_BACK);
665 }
666 #endif
56689b27 667 aCtx->SetResolution (myRenderParams.Resolution, myRenderParams.ResolutionRatio(),
668 anImmFbos[1] != NULL ? myRenderParams.RenderResolutionScale : 1.0f);
c357e426 669 toSwap = redrawImmediate (Graphic3d_Camera::Projection_MonoRightEye,
670 aMainFbos[1],
671 anImmFbos[1],
a1073ae2 672 anImmFbosOit[1],
c357e426 673 Standard_True) || toSwap;
674 if (anImmFbos[0] != NULL)
675 {
3c4b62a4 676 drawStereoPair (aFrameBuffer);
c357e426 677 }
678 }
679 else
680 {
681 OpenGl_FrameBuffer* aMainFbo = myMainSceneFbos[0]->IsValid() ? myMainSceneFbos[0].operator->() : NULL;
682 OpenGl_FrameBuffer* anImmFbo = aFrameBuffer;
a1073ae2 683 OpenGl_FrameBuffer* anImmFboOit = NULL;
684 if (myImmediateSceneFbos[0]->IsValid())
c357e426 685 {
a1073ae2 686 anImmFbo = myImmediateSceneFbos[0].operator->();
687 anImmFboOit = myImmediateSceneFbosOit[0]->IsValid() ? myImmediateSceneFbosOit[0].operator->() : NULL;
c357e426 688 }
689 #if !defined(GL_ES_VERSION_2_0)
690 if (aMainFbo == NULL)
691 {
692 aCtx->SetReadDrawBuffer (GL_BACK);
693 }
694 #endif
56689b27 695 aCtx->SetResolution (myRenderParams.Resolution, myRenderParams.ResolutionRatio(),
696 anImmFbo != aFrameBuffer ? myRenderParams.RenderResolutionScale : 1.0f);
c357e426 697 toSwap = redrawImmediate (aProjectType,
698 aMainFbo,
699 anImmFbo,
a1073ae2 700 anImmFboOit,
c357e426 701 Standard_True) || toSwap;
702 if (anImmFbo != NULL
703 && anImmFbo != aFrameBuffer)
704 {
705 blitBuffers (anImmFbo, aFrameBuffer, myToFlipOutput);
706 }
707 }
708
709 // bind default FBO
710 bindDefaultFbo();
711
8613985b 712 // reset state for safety
713 aCtx->BindProgram (Handle(OpenGl_ShaderProgram)());
714 aCtx->ShaderManager()->PushState (Handle(OpenGl_ShaderProgram)());
715
c357e426 716 if (toSwap && !aCtx->caps->buffersNoSwap)
717 {
718 aCtx->SwapBuffers();
719 }
720 else
721 {
722 aCtx->core11fwd->glFlush();
723 }
724
725 myWasRedrawnGL = Standard_True;
726}
727
728// =======================================================================
729// function : redraw
730// purpose :
731// =======================================================================
a1073ae2 732void OpenGl_View::redraw (const Graphic3d_Camera::Projection theProjection,
733 OpenGl_FrameBuffer* theReadDrawFbo,
734 OpenGl_FrameBuffer* theOitAccumFbo)
c357e426 735{
736 Handle(OpenGl_Context) aCtx = myWorkspace->GetGlContext();
737 if (theReadDrawFbo != NULL)
738 {
739 theReadDrawFbo->BindBuffer (aCtx);
740 theReadDrawFbo->SetupViewport (aCtx);
741 }
742 else
743 {
3bffef55 744 const Standard_Integer aViewport[4] = { 0, 0, myWindow->Width(), myWindow->Height() };
745 aCtx->ResizeViewport (aViewport);
c357e426 746 }
747
748 // request reset of material
8613985b 749 aCtx->ShaderManager()->UpdateMaterialState();
750
c357e426 751 myWorkspace->UseZBuffer() = Standard_True;
752 myWorkspace->UseDepthWrite() = Standard_True;
753 GLbitfield toClear = GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT;
754 glDepthFunc (GL_LEQUAL);
755 glDepthMask (GL_TRUE);
756 glEnable (GL_DEPTH_TEST);
757
758#if !defined(GL_ES_VERSION_2_0)
759 glClearDepth (1.0);
760#else
761 glClearDepthf (1.0f);
762#endif
763
8613985b 764 const OpenGl_Vec4& aBgColor = myBgColor;
765 glClearColor (aBgColor.r(), aBgColor.g(), aBgColor.b(), 0.0f);
c357e426 766
767 glClear (toClear);
768
a1073ae2 769 render (theProjection, theReadDrawFbo, theOitAccumFbo, Standard_False);
c357e426 770}
771
772// =======================================================================
773// function : redrawMonoImmediate
774// purpose :
775// =======================================================================
776bool OpenGl_View::redrawImmediate (const Graphic3d_Camera::Projection theProjection,
a1073ae2 777 OpenGl_FrameBuffer* theReadFbo,
778 OpenGl_FrameBuffer* theDrawFbo,
779 OpenGl_FrameBuffer* theOitAccumFbo,
780 const Standard_Boolean theIsPartialUpdate)
c357e426 781{
782 Handle(OpenGl_Context) aCtx = myWorkspace->GetGlContext();
783 GLboolean toCopyBackToFront = GL_FALSE;
521b0d7f 784 if (theDrawFbo == theReadFbo
785 && theDrawFbo != NULL)
c357e426 786 {
787 myBackBufferRestored = Standard_False;
788 }
789 else if (theReadFbo != NULL
790 && theReadFbo->IsValid()
791 && aCtx->IsRender())
792 {
793 if (!blitBuffers (theReadFbo, theDrawFbo))
794 {
795 return true;
796 }
797 }
798 else if (theDrawFbo == NULL)
799 {
800 #if !defined(GL_ES_VERSION_2_0)
801 aCtx->core11fwd->glGetBooleanv (GL_DOUBLEBUFFER, &toCopyBackToFront);
802 #endif
6cde53c4 803 if (toCopyBackToFront
804 && myTransientDrawToFront)
c357e426 805 {
806 if (!HasImmediateStructures()
807 && !theIsPartialUpdate)
808 {
809 // prefer Swap Buffers within Redraw in compatibility mode (without FBO)
810 return true;
811 }
a0b49de4 812 if (!copyBackToFront())
813 {
814 toCopyBackToFront = GL_FALSE;
815 myBackBufferRestored = Standard_False;
816 }
c357e426 817 }
818 else
819 {
820 myBackBufferRestored = Standard_False;
821 }
822 }
823 else
824 {
825 myBackBufferRestored = Standard_False;
826 }
827 myIsImmediateDrawn = Standard_True;
828
829 myWorkspace->UseZBuffer() = Standard_True;
830 myWorkspace->UseDepthWrite() = Standard_True;
831 glDepthFunc (GL_LEQUAL);
832 glDepthMask (GL_TRUE);
833 glEnable (GL_DEPTH_TEST);
834#if !defined(GL_ES_VERSION_2_0)
835 glClearDepth (1.0);
836#else
837 glClearDepthf (1.0f);
838#endif
839
a1073ae2 840 render (theProjection, theDrawFbo, theOitAccumFbo, Standard_True);
c357e426 841
842 return !toCopyBackToFront;
843}
844
845//=======================================================================
846//function : Render
847//purpose :
848//=======================================================================
849void OpenGl_View::render (Graphic3d_Camera::Projection theProjection,
850 OpenGl_FrameBuffer* theOutputFBO,
a1073ae2 851 OpenGl_FrameBuffer* theOitAccumFbo,
c357e426 852 const Standard_Boolean theToDrawImmediate)
853{
854 // ==================================
855 // Step 1: Prepare for render
856 // ==================================
857
858 const Handle(OpenGl_Context)& aContext = myWorkspace->GetGlContext();
859
860#if !defined(GL_ES_VERSION_2_0)
861 // Disable current clipping planes
862 if (aContext->core11 != NULL)
863 {
864 const Standard_Integer aMaxPlanes = aContext->MaxClipPlanes();
865 for (Standard_Integer aClipPlaneId = GL_CLIP_PLANE0; aClipPlaneId < GL_CLIP_PLANE0 + aMaxPlanes; ++aClipPlaneId)
866 {
867 aContext->core11fwd->glDisable (aClipPlaneId);
868 }
869 }
870#endif
871
872 // Update states of OpenGl_BVHTreeSelector (frustum culling algorithm).
873 myBVHSelector.SetViewVolume (myCamera);
91d96372 874 myBVHSelector.SetViewportSize (myWindow->Width(), myWindow->Height());
c357e426 875
876 const Handle(OpenGl_ShaderManager)& aManager = aContext->ShaderManager();
877 if (StateInfo (myCurrLightSourceState, aManager->LightSourceState().Index()) != myLastLightSourceState)
878 {
016e5959 879 aManager->UpdateLightSourceStateTo (myShadingModel == Graphic3d_TOSM_NONE ? &myNoShadingLight : &myLights);
c357e426 880 myLastLightSourceState = StateInfo (myCurrLightSourceState, aManager->LightSourceState().Index());
881 }
882
883 // Update matrices if camera has changed.
884 Graphic3d_WorldViewProjState aWVPState = myCamera->WorldViewProjState();
7c3ef2f7 885 if (myWorldViewProjState != aWVPState)
c357e426 886 {
bf02aa7d 887 myAccumFrames = 0;
7c3ef2f7 888 myWorldViewProjState = aWVPState;
c357e426 889 }
890
7c3ef2f7 891 myLocalOrigin.SetCoord (0.0, 0.0, 0.0);
892 aContext->ProjectionState.SetCurrent (myCamera->ProjectionMatrixF());
893 aContext->WorldViewState .SetCurrent (myCamera->OrientationMatrixF());
894 aContext->ApplyProjectionMatrix();
895 aContext->ApplyWorldViewMatrix();
c357e426 896 if (aManager->ModelWorldState().Index() == 0)
897 {
898 aContext->ShaderManager()->UpdateModelWorldStateTo (OpenGl_Mat4());
899 }
900
c357e426 901 // ====================================
902 // Step 2: Redraw background
903 // ====================================
904
905 // Render background
906 if (!theToDrawImmediate)
907 {
a521d90d 908 drawBackground (myWorkspace);
c357e426 909 }
910
911#if !defined(GL_ES_VERSION_2_0)
912 // Switch off lighting by default
913 if (aContext->core11 != NULL)
914 {
915 glDisable(GL_LIGHTING);
916 }
917#endif
918
919 // =================================
920 // Step 3: Redraw main plane
921 // =================================
922
923 // Setup face culling
924 GLboolean isCullFace = GL_FALSE;
925 if (myBackfacing != Graphic3d_TOBM_AUTOMATIC)
926 {
927 isCullFace = glIsEnabled (GL_CULL_FACE);
928 if (myBackfacing == Graphic3d_TOBM_DISABLE)
929 {
930 glEnable (GL_CULL_FACE);
931 glCullFace (GL_BACK);
932 }
933 else
934 glDisable (GL_CULL_FACE);
935 }
936
937#if !defined(GL_ES_VERSION_2_0)
938 // if the view is scaled normal vectors are scaled to unit
939 // length for correct displaying of shaded objects
940 const gp_Pnt anAxialScale = myCamera->AxialScale();
941 if (anAxialScale.X() != 1.F ||
942 anAxialScale.Y() != 1.F ||
943 anAxialScale.Z() != 1.F)
944 {
945 aContext->SetGlNormalizeEnabled (Standard_True);
946 }
947 else
948 {
949 aContext->SetGlNormalizeEnabled (Standard_False);
950 }
951
c357e426 952 // Apply InteriorShadingMethod
953 if (aContext->core11 != NULL)
954 {
955 aContext->core11->glShadeModel (myShadingModel == Graphic3d_TOSM_FACET
956 || myShadingModel == Graphic3d_TOSM_NONE ? GL_FLAT : GL_SMOOTH);
957 }
958#endif
959
960 aManager->SetShadingModel (myShadingModel);
c357e426 961
962 // Redraw 3d scene
963 if (theProjection == Graphic3d_Camera::Projection_MonoLeftEye)
964 {
965 aContext->ProjectionState.SetCurrent (myCamera->ProjectionStereoLeftF());
966 aContext->ApplyProjectionMatrix();
967 }
968 else if (theProjection == Graphic3d_Camera::Projection_MonoRightEye)
969 {
970 aContext->ProjectionState.SetCurrent (myCamera->ProjectionStereoRightF());
971 aContext->ApplyProjectionMatrix();
972 }
83da37b1 973
974 myWorkspace->SetEnvironmentTexture (myTextureEnv);
975
a1073ae2 976 renderScene (theProjection, theOutputFBO, theOitAccumFbo, theToDrawImmediate);
c357e426 977
cc8cbabe 978 myWorkspace->SetEnvironmentTexture (Handle(OpenGl_TextureSet)());
83da37b1 979
c357e426 980 // ===============================
981 // Step 4: Trihedron
982 // ===============================
983
984 // Resetting GL parameters according to the default aspects
985 // in order to synchronize GL state with the graphic driver state
986 // before drawing auxiliary stuff (trihedrons, overlayer)
c357e426 987 myWorkspace->ResetAppliedAspect();
988
c357e426 989 // Render trihedron
990 if (!theToDrawImmediate)
991 {
992 renderTrihedron (myWorkspace);
993
994 // Restore face culling
995 if (myBackfacing != Graphic3d_TOBM_AUTOMATIC)
996 {
997 if (isCullFace)
998 {
999 glEnable (GL_CULL_FACE);
1000 glCullFace (GL_BACK);
1001 }
1002 else
1003 glDisable (GL_CULL_FACE);
1004 }
1005 }
1006
8613985b 1007 // reset FFP state for safety
1008 aContext->BindProgram (Handle(OpenGl_ShaderProgram)());
1009 aContext->ShaderManager()->PushState (Handle(OpenGl_ShaderProgram)());
1010
c357e426 1011 // ==============================================================
1012 // Step 6: Keep shader manager informed about last View
1013 // ==============================================================
1014
1015 if (!aManager.IsNull())
1016 {
1017 aManager->SetLastView (this);
1018 }
1019}
1020
1021// =======================================================================
1022// function : InvalidateBVHData
1023// purpose :
1024// =======================================================================
1025void OpenGl_View::InvalidateBVHData (const Graphic3d_ZLayerId theLayerId)
1026{
1027 myZLayers.InvalidateBVHData (theLayerId);
1028}
1029
1030//=======================================================================
1031//function : renderStructs
1032//purpose :
1033//=======================================================================
bf02aa7d 1034void OpenGl_View::renderStructs (Graphic3d_Camera::Projection theProjection,
1035 OpenGl_FrameBuffer* theReadDrawFbo,
a1073ae2 1036 OpenGl_FrameBuffer* theOitAccumFbo,
bf02aa7d 1037 const Standard_Boolean theToDrawImmediate)
c357e426 1038{
1039 if ( myZLayers.NbStructures() <= 0 )
1040 return;
1041
1042 Handle(OpenGl_Context) aCtx = myWorkspace->GetGlContext();
c357e426 1043 Standard_Boolean toRenderGL = theToDrawImmediate ||
1044 myRenderParams.Method != Graphic3d_RM_RAYTRACING ||
1045 myRaytraceInitStatus == OpenGl_RT_FAIL ||
1046 aCtx->IsFeedback();
1047
1048 if (!toRenderGL)
1049 {
1050 toRenderGL = !initRaytraceResources (aCtx) ||
1051 !updateRaytraceGeometry (OpenGl_GUM_CHECK, myId, aCtx);
1052
1053 toRenderGL |= !myIsRaytraceDataValid; // if no ray-trace data use OpenGL
1054
1055 if (!toRenderGL)
1056 {
1057 const Standard_Integer aSizeX = theReadDrawFbo != NULL ? theReadDrawFbo->GetVPSizeX() : myWindow->Width();
1058 const Standard_Integer aSizeY = theReadDrawFbo != NULL ? theReadDrawFbo->GetVPSizeY() : myWindow->Height();
3c4b62a4 1059 myOpenGlFBO ->InitLazy (aCtx, aSizeX, aSizeY, myFboColorFormat, myFboDepthFormat, 0);
c357e426 1060
1061 if (myRaytraceFilter.IsNull())
1062 myRaytraceFilter = new OpenGl_RaytraceFilter;
1063
1064 myRaytraceFilter->SetPrevRenderFilter (myWorkspace->GetRenderFilter());
1065
1066 if (theReadDrawFbo != NULL)
1067 theReadDrawFbo->UnbindBuffer (aCtx);
1068
1069 // Prepare preliminary OpenGL output
1070 if (aCtx->arbFBOBlit != NULL)
1071 {
1072 // Render bottom OSD layer
a1073ae2 1073 myZLayers.Render (myWorkspace, theToDrawImmediate, OpenGl_LF_Bottom, theReadDrawFbo, theOitAccumFbo);
c357e426 1074
1075 myWorkspace->SetRenderFilter (myRaytraceFilter);
1076 {
1077 if (theReadDrawFbo != NULL)
1078 {
3a9b5dc8 1079 theReadDrawFbo->BindDrawBuffer (aCtx);
c357e426 1080 }
1081 else
1082 {
3a9b5dc8 1083 aCtx->arbFBO->glBindFramebuffer (GL_DRAW_FRAMEBUFFER, 0);
c357e426 1084 }
1085
c357e426 1086 // Render non-polygonal elements in default layer
a1073ae2 1087 myZLayers.Render (myWorkspace, theToDrawImmediate, OpenGl_LF_Default, theReadDrawFbo, theOitAccumFbo);
c357e426 1088 }
1089 myWorkspace->SetRenderFilter (myRaytraceFilter->PrevRenderFilter());
1090 }
1091
1092 if (theReadDrawFbo != NULL)
1093 {
1094 theReadDrawFbo->BindBuffer (aCtx);
1095 }
1096 else
1097 {
1098 aCtx->arbFBO->glBindFramebuffer (GL_FRAMEBUFFER, 0);
1099 }
1100
f55ba97f 1101 // Reset OpenGl aspects state to default to avoid enabling of
1102 // backface culling which is not supported in ray-tracing.
1103 myWorkspace->ResetAppliedAspect();
1104
c357e426 1105 // Ray-tracing polygonal primitive arrays
bf02aa7d 1106 raytrace (aSizeX, aSizeY, theProjection, theReadDrawFbo, aCtx);
c357e426 1107
1108 // Render upper (top and topmost) OpenGL layers
a1073ae2 1109 myZLayers.Render (myWorkspace, theToDrawImmediate, OpenGl_LF_Upper, theReadDrawFbo, theOitAccumFbo);
c357e426 1110 }
1111 }
1112
1113 // Redraw 3D scene using OpenGL in standard
1114 // mode or in case of ray-tracing failure
1115 if (toRenderGL)
1116 {
a1073ae2 1117 myZLayers.Render (myWorkspace, theToDrawImmediate, OpenGl_LF_All, theReadDrawFbo, theOitAccumFbo);
c357e426 1118
1119 // Set flag that scene was redrawn by standard pipeline
1120 myWasRedrawnGL = Standard_True;
1121 }
1122}
1123
1124//=======================================================================
1125//function : renderTrihedron
1126//purpose :
1127//=======================================================================
1128void OpenGl_View::renderTrihedron (const Handle(OpenGl_Workspace) &theWorkspace)
1129{
c357e426 1130 if (myToShowGradTrihedron)
1131 {
1132 myGraduatedTrihedron.Render (theWorkspace);
1133 }
1134}
1135
1136// =======================================================================
1137// function : Invalidate
1138// purpose :
1139// =======================================================================
1140void OpenGl_View::Invalidate()
1141{
1142 myBackBufferRestored = Standard_False;
1143}
1144
1145//=======================================================================
1146//function : renderScene
1147//purpose :
1148//=======================================================================
bf02aa7d 1149void OpenGl_View::renderScene (Graphic3d_Camera::Projection theProjection,
1150 OpenGl_FrameBuffer* theReadDrawFbo,
a1073ae2 1151 OpenGl_FrameBuffer* theOitAccumFbo,
bf02aa7d 1152 const Standard_Boolean theToDrawImmediate)
c357e426 1153{
1154 const Handle(OpenGl_Context)& aContext = myWorkspace->GetGlContext();
1155
c357e426 1156 // Specify clipping planes in view transformation space
3202bf1e 1157 aContext->ChangeClipping().Reset (aContext, myClipPlanes);
1158 if (!myClipPlanes.IsNull()
1159 && !myClipPlanes->IsEmpty())
c357e426 1160 {
deb02f86 1161 aContext->ShaderManager()->UpdateClippingState();
c357e426 1162 }
1163
a1073ae2 1164 renderStructs (theProjection, theReadDrawFbo, theOitAccumFbo, theToDrawImmediate);
cc8cbabe 1165 aContext->BindTextures (Handle(OpenGl_TextureSet)());
c357e426 1166
c357e426 1167 // Apply restored view matrix.
1168 aContext->ApplyWorldViewMatrix();
1169
3202bf1e 1170 aContext->ChangeClipping().Reset (aContext, Handle(Graphic3d_SequenceOfHClipPlane)());
1171 if (!myClipPlanes.IsNull()
1172 && !myClipPlanes->IsEmpty())
deb02f86 1173 {
1174 aContext->ShaderManager()->RevertClippingState();
deb02f86 1175 }
c357e426 1176}
1177
1178// =======================================================================
1179// function : bindDefaultFbo
1180// purpose :
1181// =======================================================================
1182void OpenGl_View::bindDefaultFbo (OpenGl_FrameBuffer* theCustomFbo)
1183{
1184 Handle(OpenGl_Context) aCtx = myWorkspace->GetGlContext();
1185 OpenGl_FrameBuffer* anFbo = (theCustomFbo != NULL && theCustomFbo->IsValid())
1186 ? theCustomFbo
1187 : (!aCtx->DefaultFrameBuffer().IsNull()
1188 && aCtx->DefaultFrameBuffer()->IsValid()
1189 ? aCtx->DefaultFrameBuffer().operator->()
1190 : NULL);
1191 if (anFbo != NULL)
1192 {
1193 anFbo->BindBuffer (aCtx);
521b0d7f 1194 anFbo->SetupViewport (aCtx);
c357e426 1195 }
1196 else
1197 {
1198 #if !defined(GL_ES_VERSION_2_0)
1199 aCtx->SetReadDrawBuffer (GL_BACK);
1200 #else
1201 if (aCtx->arbFBO != NULL)
1202 {
1203 aCtx->arbFBO->glBindFramebuffer (GL_FRAMEBUFFER, OpenGl_FrameBuffer::NO_FRAMEBUFFER);
1204 }
1205 #endif
3bffef55 1206 const Standard_Integer aViewport[4] = { 0, 0, myWindow->Width(), myWindow->Height() };
1207 aCtx->ResizeViewport (aViewport);
c357e426 1208 }
c357e426 1209}
1210
1211// =======================================================================
1212// function : initBlitQuad
1213// purpose :
1214// =======================================================================
1215OpenGl_VertexBuffer* OpenGl_View::initBlitQuad (const Standard_Boolean theToFlip)
1216{
1217 OpenGl_VertexBuffer* aVerts = NULL;
1218 if (!theToFlip)
1219 {
1220 aVerts = &myFullScreenQuad;
1221 if (!aVerts->IsValid())
1222 {
1223 OpenGl_Vec4 aQuad[4] =
1224 {
1225 OpenGl_Vec4( 1.0f, -1.0f, 1.0f, 0.0f),
1226 OpenGl_Vec4( 1.0f, 1.0f, 1.0f, 1.0f),
1227 OpenGl_Vec4(-1.0f, -1.0f, 0.0f, 0.0f),
1228 OpenGl_Vec4(-1.0f, 1.0f, 0.0f, 1.0f)
1229 };
1230 aVerts->Init (myWorkspace->GetGlContext(), 4, 4, aQuad[0].GetData());
1231 }
1232 }
1233 else
1234 {
1235 aVerts = &myFullScreenQuadFlip;
1236 if (!aVerts->IsValid())
1237 {
1238 OpenGl_Vec4 aQuad[4] =
1239 {
1240 OpenGl_Vec4( 1.0f, -1.0f, 1.0f, 1.0f),
1241 OpenGl_Vec4( 1.0f, 1.0f, 1.0f, 0.0f),
1242 OpenGl_Vec4(-1.0f, -1.0f, 0.0f, 1.0f),
1243 OpenGl_Vec4(-1.0f, 1.0f, 0.0f, 0.0f)
1244 };
1245 aVerts->Init (myWorkspace->GetGlContext(), 4, 4, aQuad[0].GetData());
1246 }
1247 }
1248 return aVerts;
1249}
1250
1251// =======================================================================
1252// function : blitBuffers
1253// purpose :
1254// =======================================================================
1255bool OpenGl_View::blitBuffers (OpenGl_FrameBuffer* theReadFbo,
1256 OpenGl_FrameBuffer* theDrawFbo,
1257 const Standard_Boolean theToFlip)
1258{
1259 Handle(OpenGl_Context) aCtx = myWorkspace->GetGlContext();
56689b27 1260 const Standard_Integer aReadSizeX = theReadFbo != NULL ? theReadFbo->GetVPSizeX() : myWindow->Width();
1261 const Standard_Integer aReadSizeY = theReadFbo != NULL ? theReadFbo->GetVPSizeY() : myWindow->Height();
1262 const Standard_Integer aDrawSizeX = theDrawFbo != NULL ? theDrawFbo->GetVPSizeX() : myWindow->Width();
1263 const Standard_Integer aDrawSizeY = theDrawFbo != NULL ? theDrawFbo->GetVPSizeY() : myWindow->Height();
c357e426 1264 if (theReadFbo == NULL || aCtx->IsFeedback())
1265 {
1266 return false;
1267 }
1268 else if (theReadFbo == theDrawFbo)
1269 {
1270 return true;
1271 }
1272
1273 // clear destination before blitting
1274 if (theDrawFbo != NULL
1275 && theDrawFbo->IsValid())
1276 {
1277 theDrawFbo->BindBuffer (aCtx);
1278 }
1279 else
1280 {
1281 aCtx->arbFBO->glBindFramebuffer (GL_FRAMEBUFFER, OpenGl_FrameBuffer::NO_FRAMEBUFFER);
1282 }
56689b27 1283 const Standard_Integer aViewport[4] = { 0, 0, aDrawSizeX, aDrawSizeY };
1284 aCtx->ResizeViewport (aViewport);
1285
c357e426 1286#if !defined(GL_ES_VERSION_2_0)
1287 aCtx->core20fwd->glClearDepth (1.0);
1288#else
1289 aCtx->core20fwd->glClearDepthf (1.0f);
1290#endif
1291 aCtx->core20fwd->glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
1292
3c4b62a4 1293 if (aCtx->arbFBOBlit != NULL
1294 && theReadFbo->NbSamples() != 0)
c357e426 1295 {
3c4b62a4 1296 GLbitfield aCopyMask = 0;
c357e426 1297 theReadFbo->BindReadBuffer (aCtx);
1298 if (theDrawFbo != NULL
1299 && theDrawFbo->IsValid())
1300 {
1301 theDrawFbo->BindDrawBuffer (aCtx);
3c4b62a4 1302 if (theDrawFbo->HasColor()
1303 && theReadFbo->HasColor())
1304 {
1305 aCopyMask |= GL_COLOR_BUFFER_BIT;
1306 }
1307 if (theDrawFbo->HasDepth()
1308 && theReadFbo->HasDepth())
1309 {
1310 aCopyMask |= GL_DEPTH_BUFFER_BIT;
1311 }
c357e426 1312 }
1313 else
1314 {
3c4b62a4 1315 if (theReadFbo->HasColor())
1316 {
1317 aCopyMask |= GL_COLOR_BUFFER_BIT;
1318 }
1319 if (theReadFbo->HasDepth())
1320 {
1321 aCopyMask |= GL_DEPTH_BUFFER_BIT;
1322 }
c357e426 1323 aCtx->arbFBO->glBindFramebuffer (GL_DRAW_FRAMEBUFFER, OpenGl_FrameBuffer::NO_FRAMEBUFFER);
1324 }
3c4b62a4 1325
c357e426 1326 // we don't copy stencil buffer here... does it matter for performance?
56689b27 1327 aCtx->arbFBOBlit->glBlitFramebuffer (0, 0, aReadSizeX, aReadSizeY,
1328 0, 0, aDrawSizeX, aDrawSizeY,
3c4b62a4 1329 aCopyMask, GL_NEAREST);
7ccf8676 1330 const int anErr = ::glGetError();
1331 if (anErr != GL_NO_ERROR)
1332 {
1333 // glBlitFramebuffer() might fail in several cases:
1334 // - Both FBOs have MSAA and they are samples number does not match.
1335 // OCCT checks that this does not happen,
1336 // however some graphics drivers provide an option for overriding MSAA.
1337 // In this case window MSAA might be non-zero (and application can not check it)
1338 // and might not match MSAA of our offscreen FBOs.
1339 // - Pixel formats of FBOs do not match.
1340 // This also might happen with window has pixel format,
1341 // e.g. Mesa fails blitting RGBA8 -> RGB8 while other drivers support this conversion.
1342 TCollection_ExtendedString aMsg = TCollection_ExtendedString() + "FBO blitting has failed [Error #" + anErr + "]\n"
1343 + " Please check your graphics driver settings or try updating driver.";
1344 if (theReadFbo->NbSamples() != 0)
1345 {
1346 myToDisableMSAA = true;
1347 aMsg += "\n MSAA settings should not be overridden by driver!";
1348 }
1349 aCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION,
1350 GL_DEBUG_TYPE_ERROR,
1351 0,
1352 GL_DEBUG_SEVERITY_HIGH,
1353 aMsg);
1354 }
1355
c357e426 1356 if (theDrawFbo != NULL
3c4b62a4 1357 && theDrawFbo->IsValid())
c357e426 1358 {
1359 theDrawFbo->BindBuffer (aCtx);
1360 }
1361 else
1362 {
1363 aCtx->arbFBO->glBindFramebuffer (GL_FRAMEBUFFER, OpenGl_FrameBuffer::NO_FRAMEBUFFER);
1364 }
1365 }
1366 else
c357e426 1367 {
1368 aCtx->core20fwd->glDepthFunc (GL_ALWAYS);
1369 aCtx->core20fwd->glDepthMask (GL_TRUE);
1370 aCtx->core20fwd->glEnable (GL_DEPTH_TEST);
1ce0716b 1371 #if defined(GL_ES_VERSION_2_0)
1372 if (!aCtx->IsGlGreaterEqual (3, 0)
1373 && !aCtx->extFragDepth)
1374 {
1375 aCtx->core20fwd->glDisable (GL_DEPTH_TEST);
1376 }
1377 #endif
c357e426 1378
cc8cbabe 1379 aCtx->BindTextures (Handle(OpenGl_TextureSet)());
c357e426 1380
56689b27 1381 const Graphic3d_TypeOfTextureFilter aFilter = (aDrawSizeX == aReadSizeX && aDrawSizeY == aReadSizeY) ? Graphic3d_TOTF_NEAREST : Graphic3d_TOTF_BILINEAR;
1382 const GLint aFilterGl = aFilter == Graphic3d_TOTF_NEAREST ? GL_NEAREST : GL_LINEAR;
1383
c357e426 1384 OpenGl_VertexBuffer* aVerts = initBlitQuad (theToFlip);
1385 const Handle(OpenGl_ShaderManager)& aManager = aCtx->ShaderManager();
1386 if (aVerts->IsValid()
1387 && aManager->BindFboBlitProgram())
1388 {
cc8cbabe 1389 theReadFbo->ColorTexture()->Bind (aCtx, Graphic3d_TextureUnit_0);
1390 if (theReadFbo->ColorTexture()->Sampler()->Parameters()->Filter() != aFilter)
56689b27 1391 {
cc8cbabe 1392 theReadFbo->ColorTexture()->Sampler()->Parameters()->SetFilter (aFilter);
56689b27 1393 aCtx->core20fwd->glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, aFilterGl);
1394 aCtx->core20fwd->glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, aFilterGl);
1395 }
1396
cc8cbabe 1397 theReadFbo->DepthStencilTexture()->Bind (aCtx, Graphic3d_TextureUnit_1);
1398 if (theReadFbo->DepthStencilTexture()->Sampler()->Parameters()->Filter() != aFilter)
56689b27 1399 {
cc8cbabe 1400 theReadFbo->DepthStencilTexture()->Sampler()->Parameters()->SetFilter (aFilter);
56689b27 1401 aCtx->core20fwd->glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, aFilterGl);
1402 aCtx->core20fwd->glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, aFilterGl);
1403 }
1404
c357e426 1405 aVerts->BindVertexAttrib (aCtx, Graphic3d_TOA_POS);
1406
1407 aCtx->core20fwd->glDrawArrays (GL_TRIANGLE_STRIP, 0, 4);
1408
1409 aVerts->UnbindVertexAttrib (aCtx, Graphic3d_TOA_POS);
cc8cbabe 1410 theReadFbo->DepthStencilTexture()->Unbind (aCtx, Graphic3d_TextureUnit_1);
1411 theReadFbo->ColorTexture() ->Unbind (aCtx, Graphic3d_TextureUnit_0);
a521d90d 1412 aCtx->BindProgram (NULL);
c357e426 1413 }
1414 else
1415 {
1416 TCollection_ExtendedString aMsg = TCollection_ExtendedString()
1417 + "Error! FBO blitting has failed";
3b523c4c 1418 aCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION,
1419 GL_DEBUG_TYPE_ERROR,
c357e426 1420 0,
3b523c4c 1421 GL_DEBUG_SEVERITY_HIGH,
c357e426 1422 aMsg);
1423 myHasFboBlit = Standard_False;
1424 theReadFbo->Release (aCtx.operator->());
1425 return true;
1426 }
1427 }
1428 return true;
1429}
1430
1431// =======================================================================
1432// function : drawStereoPair
1433// purpose :
1434// =======================================================================
3c4b62a4 1435void OpenGl_View::drawStereoPair (OpenGl_FrameBuffer* theDrawFbo)
c357e426 1436{
3c4b62a4 1437 const Handle(OpenGl_Context)& aCtx = myWorkspace->GetGlContext();
1438 bindDefaultFbo (theDrawFbo);
c357e426 1439 OpenGl_FrameBuffer* aPair[2] =
1440 {
1441 myImmediateSceneFbos[0]->IsValid() ? myImmediateSceneFbos[0].operator->() : NULL,
1442 myImmediateSceneFbos[1]->IsValid() ? myImmediateSceneFbos[1].operator->() : NULL
1443 };
1444 if (aPair[0] == NULL
1445 || aPair[1] == NULL
1446 || !myTransientDrawToFront)
1447 {
1448 aPair[0] = myMainSceneFbos[0]->IsValid() ? myMainSceneFbos[0].operator->() : NULL;
1449 aPair[1] = myMainSceneFbos[1]->IsValid() ? myMainSceneFbos[1].operator->() : NULL;
1450 }
1451
1452 if (aPair[0] == NULL
1453 || aPair[1] == NULL)
1454 {
1455 return;
1456 }
1457
3c4b62a4 1458 if (aPair[0]->NbSamples() != 0)
1459 {
1460 // resolve MSAA buffers before drawing
1461 if (!myOpenGlFBO ->InitLazy (aCtx, aPair[0]->GetVPSizeX(), aPair[0]->GetVPSizeY(), myFboColorFormat, myFboDepthFormat, 0)
1462 || !myOpenGlFBO2->InitLazy (aCtx, aPair[0]->GetVPSizeX(), aPair[0]->GetVPSizeY(), myFboColorFormat, 0, 0))
1463 {
82f443b6 1464 aCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION,
1465 GL_DEBUG_TYPE_ERROR,
3c4b62a4 1466 0,
82f443b6 1467 GL_DEBUG_SEVERITY_HIGH,
3c4b62a4 1468 "Error! Unable to allocate FBO for blitting stereo pair");
1469 bindDefaultFbo (theDrawFbo);
1470 return;
1471 }
1472
1473 if (!blitBuffers (aPair[0], myOpenGlFBO .operator->(), Standard_False)
1474 || !blitBuffers (aPair[1], myOpenGlFBO2.operator->(), Standard_False))
1475 {
1476 bindDefaultFbo (theDrawFbo);
1477 return;
1478 }
1479
1480 aPair[0] = myOpenGlFBO .operator->();
1481 aPair[1] = myOpenGlFBO2.operator->();
1482 bindDefaultFbo (theDrawFbo);
1483 }
1484
c357e426 1485 struct
1486 {
1487 Standard_Integer left;
1488 Standard_Integer top;
1489 Standard_Integer right;
1490 Standard_Integer bottom;
1491 Standard_Integer dx() { return right - left; }
1492 Standard_Integer dy() { return bottom - top; }
1493 } aGeom;
1494
1495 myWindow->PlatformWindow()->Position (aGeom.left, aGeom.top, aGeom.right, aGeom.bottom);
1496
1497 Standard_Boolean toReverse = myRenderParams.ToReverseStereo;
1498 const Standard_Boolean isOddY = (aGeom.top + aGeom.dy()) % 2 == 1;
1499 const Standard_Boolean isOddX = aGeom.left % 2 == 1;
1500 if (isOddY
1501 && (myRenderParams.StereoMode == Graphic3d_StereoMode_RowInterlaced
1502 || myRenderParams.StereoMode == Graphic3d_StereoMode_ChessBoard))
1503 {
1504 toReverse = !toReverse;
1505 }
1506 if (isOddX
1507 && (myRenderParams.StereoMode == Graphic3d_StereoMode_ColumnInterlaced
1508 || myRenderParams.StereoMode == Graphic3d_StereoMode_ChessBoard))
1509 {
1510 toReverse = !toReverse;
1511 }
1512
1513 if (toReverse)
1514 {
1515 std::swap (aPair[0], aPair[1]);
1516 }
1517
c357e426 1518 aCtx->core20fwd->glDepthFunc (GL_ALWAYS);
1519 aCtx->core20fwd->glDepthMask (GL_TRUE);
1520 aCtx->core20fwd->glEnable (GL_DEPTH_TEST);
1521
cc8cbabe 1522 aCtx->BindTextures (Handle(OpenGl_TextureSet)());
c357e426 1523 OpenGl_VertexBuffer* aVerts = initBlitQuad (myToFlipOutput);
1524
1525 const Handle(OpenGl_ShaderManager)& aManager = aCtx->ShaderManager();
1526 if (aVerts->IsValid()
1527 && aManager->BindStereoProgram (myRenderParams.StereoMode))
1528 {
1529 if (myRenderParams.StereoMode == Graphic3d_StereoMode_Anaglyph)
1530 {
1531 OpenGl_Mat4 aFilterL, aFilterR;
1532 aFilterL.SetDiagonal (Graphic3d_Vec4 (0.0f, 0.0f, 0.0f, 0.0f));
1533 aFilterR.SetDiagonal (Graphic3d_Vec4 (0.0f, 0.0f, 0.0f, 0.0f));
1534 switch (myRenderParams.AnaglyphFilter)
1535 {
1536 case Graphic3d_RenderingParams::Anaglyph_RedCyan_Simple:
1537 {
1538 aFilterL.SetRow (0, Graphic3d_Vec4 (1.0f, 0.0f, 0.0f, 0.0f));
1539 aFilterR.SetRow (1, Graphic3d_Vec4 (0.0f, 1.0f, 0.0f, 0.0f));
1540 aFilterR.SetRow (2, Graphic3d_Vec4 (0.0f, 0.0f, 1.0f, 0.0f));
1541 break;
1542 }
1543 case Graphic3d_RenderingParams::Anaglyph_RedCyan_Optimized:
1544 {
1545 aFilterL.SetRow (0, Graphic3d_Vec4 ( 0.4154f, 0.4710f, 0.16666667f, 0.0f));
1546 aFilterL.SetRow (1, Graphic3d_Vec4 (-0.0458f, -0.0484f, -0.0257f, 0.0f));
1547 aFilterL.SetRow (2, Graphic3d_Vec4 (-0.0547f, -0.0615f, 0.0128f, 0.0f));
1548 aFilterL.SetRow (3, Graphic3d_Vec4 ( 0.0f, 0.0f, 0.0f, 0.0f));
1549 aFilterR.SetRow (0, Graphic3d_Vec4 (-0.01090909f, -0.03636364f, -0.00606061f, 0.0f));
1550 aFilterR.SetRow (1, Graphic3d_Vec4 ( 0.37560000f, 0.73333333f, 0.01111111f, 0.0f));
1551 aFilterR.SetRow (2, Graphic3d_Vec4 (-0.06510000f, -0.12870000f, 1.29710000f, 0.0f));
1552 aFilterR.SetRow (3, Graphic3d_Vec4 ( 0.0f, 0.0f, 0.0f, 0.0f));
1553 break;
1554 }
1555 case Graphic3d_RenderingParams::Anaglyph_YellowBlue_Simple:
1556 {
1557 aFilterL.SetRow (0, Graphic3d_Vec4 (1.0f, 0.0f, 0.0f, 0.0f));
1558 aFilterL.SetRow (1, Graphic3d_Vec4 (0.0f, 1.0f, 0.0f, 0.0f));
1559 aFilterR.SetRow (2, Graphic3d_Vec4 (0.0f, 0.0f, 1.0f, 0.0f));
1560 break;
1561 }
1562 case Graphic3d_RenderingParams::Anaglyph_YellowBlue_Optimized:
1563 {
1564 aFilterL.SetRow (0, Graphic3d_Vec4 ( 1.062f, -0.205f, 0.299f, 0.0f));
1565 aFilterL.SetRow (1, Graphic3d_Vec4 (-0.026f, 0.908f, 0.068f, 0.0f));
1566 aFilterL.SetRow (2, Graphic3d_Vec4 (-0.038f, -0.173f, 0.022f, 0.0f));
1567 aFilterL.SetRow (3, Graphic3d_Vec4 ( 0.0f, 0.0f, 0.0f, 0.0f));
1568 aFilterR.SetRow (0, Graphic3d_Vec4 (-0.016f, -0.123f, -0.017f, 0.0f));
1569 aFilterR.SetRow (1, Graphic3d_Vec4 ( 0.006f, 0.062f, -0.017f, 0.0f));
1570 aFilterR.SetRow (2, Graphic3d_Vec4 ( 0.094f, 0.185f, 0.911f, 0.0f));
1571 aFilterR.SetRow (3, Graphic3d_Vec4 ( 0.0f, 0.0f, 0.0f, 0.0f));
1572 break;
1573 }
1574 case Graphic3d_RenderingParams::Anaglyph_GreenMagenta_Simple:
1575 {
1576 aFilterR.SetRow (0, Graphic3d_Vec4 (1.0f, 0.0f, 0.0f, 0.0f));
1577 aFilterL.SetRow (1, Graphic3d_Vec4 (0.0f, 1.0f, 0.0f, 0.0f));
1578 aFilterR.SetRow (2, Graphic3d_Vec4 (0.0f, 0.0f, 1.0f, 0.0f));
1579 break;
1580 }
1581 case Graphic3d_RenderingParams::Anaglyph_UserDefined:
1582 {
1583 aFilterL = myRenderParams.AnaglyphLeft;
1584 aFilterR = myRenderParams.AnaglyphRight;
1585 break;
1586 }
1587 }
1588 aCtx->ActiveProgram()->SetUniform (aCtx, "uMultL", aFilterL);
1589 aCtx->ActiveProgram()->SetUniform (aCtx, "uMultR", aFilterR);
1590 }
1591
cc8cbabe 1592 aPair[0]->ColorTexture()->Bind (aCtx, Graphic3d_TextureUnit_0);
1593 aPair[1]->ColorTexture()->Bind (aCtx, Graphic3d_TextureUnit_1);
c357e426 1594 aVerts->BindVertexAttrib (aCtx, 0);
1595
1596 aCtx->core20fwd->glDrawArrays (GL_TRIANGLE_STRIP, 0, 4);
1597
1598 aVerts->UnbindVertexAttrib (aCtx, 0);
cc8cbabe 1599 aPair[1]->ColorTexture()->Unbind (aCtx, Graphic3d_TextureUnit_1);
1600 aPair[0]->ColorTexture()->Unbind (aCtx, Graphic3d_TextureUnit_0);
c357e426 1601 }
1602 else
1603 {
1604 TCollection_ExtendedString aMsg = TCollection_ExtendedString()
1605 + "Error! Anaglyph has failed";
3b523c4c 1606 aCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION,
1607 GL_DEBUG_TYPE_ERROR,
c357e426 1608 0,
3b523c4c 1609 GL_DEBUG_SEVERITY_HIGH,
c357e426 1610 aMsg);
1611 }
1612}
1613
1614// =======================================================================
1615// function : copyBackToFront
1616// purpose :
1617// =======================================================================
a0b49de4 1618bool OpenGl_View::copyBackToFront()
c357e426 1619{
a0b49de4 1620 myIsImmediateDrawn = Standard_False;
c357e426 1621#if !defined(GL_ES_VERSION_2_0)
a0b49de4 1622 const Handle(OpenGl_Context)& aCtx = myWorkspace->GetGlContext();
1623 if (aCtx->core11 == NULL)
1624 {
1625 return false;
1626 }
c357e426 1627
1628 OpenGl_Mat4 aProjectMat;
1629 Graphic3d_TransformUtils::Ortho2D (aProjectMat,
8613985b 1630 0.0f, static_cast<GLfloat> (myWindow->Width()),
1631 0.0f, static_cast<GLfloat> (myWindow->Height()));
1632
c357e426 1633 aCtx->WorldViewState.Push();
1634 aCtx->ProjectionState.Push();
1635
1636 aCtx->WorldViewState.SetIdentity();
1637 aCtx->ProjectionState.SetCurrent (aProjectMat);
1638
1639 aCtx->ApplyProjectionMatrix();
1640 aCtx->ApplyWorldViewMatrix();
1641
8613985b 1642 // synchronize FFP state before copying pixels
1643 aCtx->BindProgram (Handle(OpenGl_ShaderProgram)());
1644 aCtx->ShaderManager()->PushState (Handle(OpenGl_ShaderProgram)());
c357e426 1645 aCtx->DisableFeatures();
1646
1647 switch (aCtx->DrawBuffer())
1648 {
1649 case GL_BACK_LEFT:
1650 {
1651 aCtx->SetReadBuffer (GL_BACK_LEFT);
1652 aCtx->SetDrawBuffer (GL_FRONT_LEFT);
1653 break;
1654 }
1655 case GL_BACK_RIGHT:
1656 {
1657 aCtx->SetReadBuffer (GL_BACK_RIGHT);
1658 aCtx->SetDrawBuffer (GL_FRONT_RIGHT);
1659 break;
1660 }
1661 default:
1662 {
1663 aCtx->SetReadBuffer (GL_BACK);
1664 aCtx->SetDrawBuffer (GL_FRONT);
1665 break;
1666 }
1667 }
1668
a0b49de4 1669 aCtx->core11->glRasterPos2i (0, 0);
1670 aCtx->core11->glCopyPixels (0, 0, myWindow->Width() + 1, myWindow->Height() + 1, GL_COLOR);
1671 //aCtx->core11->glCopyPixels (0, 0, myWidth + 1, myHeight + 1, GL_DEPTH);
c357e426 1672
1673 aCtx->EnableFeatures();
1674
1675 aCtx->WorldViewState.Pop();
1676 aCtx->ProjectionState.Pop();
1677 aCtx->ApplyProjectionMatrix();
1678
1679 // read/write from front buffer now
1680 aCtx->SetReadBuffer (aCtx->DrawBuffer());
a0b49de4 1681 return true;
1682#else
1683 return false;
c357e426 1684#endif
c357e426 1685}
a1073ae2 1686
1687// =======================================================================
1688// function : checkOitCompatibility
1689// purpose :
1690// =======================================================================
1691Standard_Boolean OpenGl_View::checkOitCompatibility (const Handle(OpenGl_Context)& theGlContext,
1692 const Standard_Boolean theMSAA)
1693{
1694 // determine if OIT is supported by current OpenGl context
1695 Standard_Boolean& aToDisableOIT = theMSAA ? myToDisableMSAA : myToDisableOIT;
1696 if (aToDisableOIT)
1697 {
1698 return Standard_False;
1699 }
1700
1701 TCollection_ExtendedString aCompatibilityMsg;
177781da 1702 if (theGlContext->hasFloatBuffer == OpenGl_FeatureNotAvailable
1703 && theGlContext->hasHalfFloatBuffer == OpenGl_FeatureNotAvailable)
a1073ae2 1704 {
1705 aCompatibilityMsg += "OpenGL context does not support floating-point RGBA color buffer format.\n";
1706 }
177781da 1707 if (theMSAA && theGlContext->hasSampleVariables == OpenGl_FeatureNotAvailable)
a1073ae2 1708 {
1709 aCompatibilityMsg += "Current version of GLSL does not support built-in sample variables.\n";
1710 }
177781da 1711 if (theGlContext->hasDrawBuffers == OpenGl_FeatureNotAvailable)
a1073ae2 1712 {
1713 aCompatibilityMsg += "OpenGL context does not support multiple draw buffers.\n";
1714 }
1715 if (aCompatibilityMsg.IsEmpty())
1716 {
1717 return Standard_True;
1718 }
1719
1720 aCompatibilityMsg += " Blended order-independent transparency will not be available.\n";
1721 theGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION,
1722 GL_DEBUG_TYPE_ERROR,
1723 0,
1724 GL_DEBUG_SEVERITY_HIGH,
1725 aCompatibilityMsg);
1726
1727 aToDisableOIT = Standard_True;
1728 return Standard_False;
1729}
1730
1731// =======================================================================
1732// function : chooseOitColorConfiguration
1733// purpose :
1734// =======================================================================
1735bool OpenGl_View::chooseOitColorConfiguration (const Handle(OpenGl_Context)& theGlContext,
1736 const Standard_Integer theConfigIndex,
1737 OpenGl_ColorFormats& theFormats)
1738{
1739 theFormats.Clear();
1740 switch (theConfigIndex)
1741 {
1742 case 0: // choose best applicable color format combination
1743 {
177781da 1744 theFormats.Append (theGlContext->hasHalfFloatBuffer != OpenGl_FeatureNotAvailable ? GL_RGBA16F : GL_RGBA32F);
1745 theFormats.Append (theGlContext->hasHalfFloatBuffer != OpenGl_FeatureNotAvailable ? GL_R16F : GL_R32F);
a1073ae2 1746 return true;
1747 }
1748 case 1: // choose non-optimal applicable color format combination
1749 {
177781da 1750 theFormats.Append (theGlContext->hasHalfFloatBuffer != OpenGl_FeatureNotAvailable ? GL_RGBA16F : GL_RGBA32F);
1751 theFormats.Append (theGlContext->hasHalfFloatBuffer != OpenGl_FeatureNotAvailable ? GL_RGBA16F : GL_RGBA32F);
a1073ae2 1752 return true;
1753 }
1754 }
1755 return false; // color combination does not exist
1756}