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