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