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