1 // Created on: 2011-09-20
2 // Created by: Sergey ZERCHANINOV
3 // Copyright (c) 2011-2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
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.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
19 #include <OpenGl_GlCore11.hxx>
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>
27 #include <NCollection_Mat4.hxx>
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>
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;
53 extern void InitLayerProp (const int theListId); //szvgl: defined in OpenGl_GraphicDriver_Layer.cxx
55 #if !defined(GL_ES_VERSION_2_0)
57 //=======================================================================
58 //function : bindLight
60 //=======================================================================
61 static void bindLight (const OpenGl_Light& theLight,
63 Graphic3d_Vec4& theAmbientColor,
64 const Handle(OpenGl_Workspace)& theWorkspace)
66 // Only 8 lights in OpenGL...
67 if (theLightGlId > GL_LIGHT7)
72 if (theLight.Type == Graphic3d_TOLS_AMBIENT)
74 // add RGBA intensity of the ambient light
75 theAmbientColor += theLight.Color;
79 const Handle(OpenGl_Context)& aContext = theWorkspace->GetGlContext();
81 // the light is a headlight?
82 if (theLight.IsHeadlight)
84 aContext->WorldViewState.Push();
85 aContext->WorldViewState.SetIdentity();
87 aContext->ApplyWorldViewMatrix();
91 switch (theLight.Type)
93 case Graphic3d_TOLS_AMBIENT : break; // handled by separate if-clause at beginning of method
94 case Graphic3d_TOLS_DIRECTIONAL:
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;
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);
109 case Graphic3d_TOLS_POSITIONAL:
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);
124 case Graphic3d_TOLS_SPOT:
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);
140 // restore matrix in case of headlight
141 if (theLight.IsHeadlight)
143 aContext->WorldViewState.Pop();
146 glEnable (theLightGlId++);
150 //=======================================================================
151 //function : drawBackground
153 //=======================================================================
154 void OpenGl_View::drawBackground (const Handle(OpenGl_Workspace)& theWorkspace)
156 const Handle(OpenGl_Context)& aCtx = theWorkspace->GetGlContext();
158 if ((theWorkspace->NamedStatus & OPENGL_NS_WHITEBACK) != 0 // no background
159 || (!myBgTextureArray->IsDefined() // no texture
160 && !myBgGradientArray->IsDefined())) // no gradient
165 const Standard_Boolean wasUsedZBuffer = theWorkspace->SetUseZBuffer (Standard_False);
168 aCtx->core11fwd->glDisable (GL_DEPTH_TEST);
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();
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()
184 && (!myTextureParams->Aspect()->ToMapTexture()
185 || myBgTextureArray->TextureFillMethod() == Aspect_FM_CENTERED
186 || myBgTextureArray->TextureFillMethod() == Aspect_FM_NONE))
188 #if !defined(GL_ES_VERSION_2_0)
189 GLint aShadingModelOld = GL_SMOOTH;
190 if (aCtx->core11 != NULL)
192 aCtx->core11fwd->glDisable (GL_LIGHTING);
193 aCtx->core11fwd->glGetIntegerv (GL_SHADE_MODEL, &aShadingModelOld);
194 aCtx->core11->glShadeModel (GL_SMOOTH);
198 if (myBgGradientArray->IsDataChanged())
200 myBgGradientArray->Init (theWorkspace);
203 myBgGradientArray->Render (theWorkspace);
205 #if !defined(GL_ES_VERSION_2_0)
206 if (aCtx->core11 != NULL)
208 aCtx->core11->glShadeModel (aShadingModelOld);
213 // Drawing background image if it is defined
214 // (texture is defined and fill type is not Aspect_FM_NONE)
215 if (myBgTextureArray->IsDefined()
216 && myTextureParams->Aspect()->ToMapTexture())
218 aCtx->core11fwd->glDisable (GL_BLEND);
220 const OpenGl_AspectFace* anOldAspectFace = theWorkspace->SetAspectFace (myTextureParams);
222 if (myBgTextureArray->IsDataChanged()
223 || myBgTextureArray->IsViewSizeChanged (theWorkspace))
225 myBgTextureArray->Init (theWorkspace);
228 myBgTextureArray->Render (theWorkspace);
231 theWorkspace->SetAspectFace (anOldAspectFace);
234 aCtx->ModelWorldState.Pop();
235 aCtx->WorldViewState.Pop();
236 aCtx->ProjectionState.Pop();
237 aCtx->ApplyProjectionMatrix();
238 aCtx->ApplyModelViewMatrix();
242 theWorkspace->SetUseZBuffer (Standard_True);
243 aCtx->core11fwd->glEnable (GL_DEPTH_TEST);
247 //=======================================================================
250 //=======================================================================
251 void OpenGl_View::Redraw()
253 const Standard_Boolean wasDisabledMSAA = myToDisableMSAA;
254 const Standard_Boolean hadFboBlit = myHasFboBlit;
255 if (myRenderParams.Method == Graphic3d_RM_RAYTRACING
256 && !myCaps->vboDisable
257 && !myCaps->keepArrayData)
261 myDeviceLostFlag = Standard_True;
264 myCaps->keepArrayData = Standard_True;
267 if (!myWorkspace->Activate())
272 myWindow->SetSwapInterval();
275 const Graphic3d_StereoMode aStereoMode = myRenderParams.StereoMode;
276 Graphic3d_Camera::Projection aProjectType = myCamera->ProjectionType();
277 Handle(OpenGl_Context) aCtx = myWorkspace->GetGlContext();
279 // release pending GL resources
280 aCtx->ReleaseDelayed();
282 // fetch OpenGl context state
285 // set resolution ratio
286 aCtx->SetResolutionRatio (RenderingParams().ResolutionRatio());
288 OpenGl_FrameBuffer* aFrameBuffer = myFBO.operator->();
289 bool toSwap = aCtx->IsRender()
290 && !aCtx->caps->buffersNoSwap
291 && aFrameBuffer == NULL;
293 Standard_Integer aSizeX = aFrameBuffer != NULL ? aFrameBuffer->GetVPSizeX() : myWindow->Width();
294 Standard_Integer aSizeY = aFrameBuffer != NULL ? aFrameBuffer->GetVPSizeY() : myWindow->Height();
296 // determine multisampling parameters
297 Standard_Integer aNbSamples = !myToDisableMSAA
298 ? Max (Min (myRenderParams.NbMsaaSamples, aCtx->MaxMsaaSamples()), 0)
302 aNbSamples = OpenGl_Context::GetPowerOfTwo (aNbSamples, aCtx->MaxMsaaSamples());
305 if ( aFrameBuffer == NULL
306 && !aCtx->DefaultFrameBuffer().IsNull()
307 && aCtx->DefaultFrameBuffer()->IsValid())
309 aFrameBuffer = aCtx->DefaultFrameBuffer().operator->();
313 && (myTransientDrawToFront
314 || aProjectType == Graphic3d_Camera::Projection_Stereo
317 if (myMainSceneFbos[0]->GetVPSizeX() != aSizeX
318 || myMainSceneFbos[0]->GetVPSizeY() != aSizeY
319 || myMainSceneFbos[0]->NbSamples() != aNbSamples)
321 // prepare FBOs containing main scene
322 // for further blitting and rendering immediate presentations on top
323 if (aCtx->core20fwd != NULL)
325 myMainSceneFbos[0]->Init (aCtx, aSizeX, aSizeY, myFboColorFormat, myFboDepthFormat, aNbSamples);
327 if (!aCtx->caps->useSystemBuffer && myMainSceneFbos[0]->IsValid())
329 myImmediateSceneFbos[0]->InitLazy (aCtx, *myMainSceneFbos[0]);
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);
345 if (aProjectType == Graphic3d_Camera::Projection_Stereo
346 && myMainSceneFbos[0]->IsValid())
348 myMainSceneFbos[1]->InitLazy (aCtx, *myMainSceneFbos[0]);
349 if (!myMainSceneFbos[1]->IsValid())
352 aProjectType = Graphic3d_Camera::Projection_Perspective;
354 else if (!myTransientDrawToFront)
358 else if (!aCtx->HasStereoBuffers() || aStereoMode != Graphic3d_StereoMode_QuadBuffer)
360 myImmediateSceneFbos[0]->InitLazy (aCtx, *myMainSceneFbos[0]);
361 myImmediateSceneFbos[1]->InitLazy (aCtx, *myMainSceneFbos[0]);
362 if (!myImmediateSceneFbos[0]->IsValid()
363 || !myImmediateSceneFbos[1]->IsValid())
365 aProjectType = Graphic3d_Camera::Projection_Perspective;
370 if (aProjectType == Graphic3d_Camera::Projection_Stereo)
372 OpenGl_FrameBuffer* aMainFbos[2] =
374 myMainSceneFbos[0]->IsValid() ? myMainSceneFbos[0].operator->() : NULL,
375 myMainSceneFbos[1]->IsValid() ? myMainSceneFbos[1].operator->() : NULL
377 OpenGl_FrameBuffer* anImmFbos[2] =
379 myImmediateSceneFbos[0]->IsValid() ? myImmediateSceneFbos[0].operator->() : NULL,
380 myImmediateSceneFbos[1]->IsValid() ? myImmediateSceneFbos[1].operator->() : NULL
383 if (!myTransientDrawToFront)
385 anImmFbos[0] = aMainFbos[0];
386 anImmFbos[1] = aMainFbos[1];
388 else if (aStereoMode == Graphic3d_StereoMode_SoftPageFlip
389 || aStereoMode == Graphic3d_StereoMode_QuadBuffer)
395 #if !defined(GL_ES_VERSION_2_0)
396 aCtx->SetReadDrawBuffer (aStereoMode == Graphic3d_StereoMode_QuadBuffer ? GL_BACK_LEFT : GL_BACK);
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);
404 if (!redrawImmediate (Graphic3d_Camera::Projection_MonoLeftEye, aMainFbos[0], anImmFbos[0]))
408 else if (aStereoMode == Graphic3d_StereoMode_SoftPageFlip && toSwap)
413 #if !defined(GL_ES_VERSION_2_0)
414 aCtx->SetReadDrawBuffer (aStereoMode == Graphic3d_StereoMode_QuadBuffer ? GL_BACK_RIGHT : GL_BACK);
416 redraw (Graphic3d_Camera::Projection_MonoRightEye, aMainFbos[1]);
417 myBackBufferRestored = Standard_True;
418 myIsImmediateDrawn = Standard_False;
419 if (!redrawImmediate (Graphic3d_Camera::Projection_MonoRightEye, aMainFbos[1], anImmFbos[1]))
424 if (anImmFbos[0] != NULL)
426 drawStereoPair (aFrameBuffer);
431 OpenGl_FrameBuffer* aMainFbo = myMainSceneFbos[0]->IsValid() ? myMainSceneFbos[0].operator->() : NULL;
432 OpenGl_FrameBuffer* anImmFbo = aFrameBuffer;
433 if (!aCtx->caps->useSystemBuffer && myImmediateSceneFbos[0]->IsValid())
435 anImmFbo = myImmediateSceneFbos[0].operator->();
438 #if !defined(GL_ES_VERSION_2_0)
440 && aFrameBuffer == NULL)
442 aCtx->SetReadDrawBuffer (GL_BACK);
445 redraw (aProjectType, aMainFbo != NULL ? aMainFbo : aFrameBuffer);
446 myBackBufferRestored = Standard_True;
447 myIsImmediateDrawn = Standard_False;
448 if (!redrawImmediate (aProjectType, aMainFbo, anImmFbo))
454 && anImmFbo != aFrameBuffer)
456 blitBuffers (anImmFbo, aFrameBuffer, myToFlipOutput);
460 #if defined(_WIN32) && defined(HAVE_VIDEOCAPTURE)
461 if (OpenGl_AVIWriter_AllowWriting (myWindow->PlatformWindow()->NativeHandle()))
464 glGetIntegerv (GL_VIEWPORT, params);
465 int nWidth = params[2] & ~0x7;
466 int nHeight = params[3] & ~0x7;
468 const int nBitsPerPixel = 24;
469 GLubyte* aDumpData = new GLubyte[nWidth * nHeight * nBitsPerPixel / 8];
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);
478 if (myRenderParams.Method == Graphic3d_RM_RAYTRACING
479 && myRenderParams.IsGlobalIlluminationEnabled)
487 if (wasDisabledMSAA != myToDisableMSAA
488 || hadFboBlit != myHasFboBlit)
498 if (!myMainSceneFbos[0]->IsValid())
500 myBackBufferRestored = Standard_False;
505 aCtx->core11fwd->glFlush();
508 // reset render mode state
511 myWasRedrawnGL = Standard_True;
514 // =======================================================================
515 // function : RedrawImmediate
517 // =======================================================================
518 void OpenGl_View::RedrawImmediate()
520 if (!myWorkspace->Activate())
523 Handle(OpenGl_Context) aCtx = myWorkspace->GetGlContext();
524 if (!myTransientDrawToFront
525 || !myBackBufferRestored
526 || (aCtx->caps->buffersNoSwap && !myMainSceneFbos[0]->IsValid()))
532 const Graphic3d_StereoMode aStereoMode = myRenderParams.StereoMode;
533 Graphic3d_Camera::Projection aProjectType = myCamera->ProjectionType();
534 OpenGl_FrameBuffer* aFrameBuffer = myFBO.operator->();
536 if ( aFrameBuffer == NULL
537 && !aCtx->DefaultFrameBuffer().IsNull()
538 && aCtx->DefaultFrameBuffer()->IsValid())
540 aFrameBuffer = aCtx->DefaultFrameBuffer().operator->();
543 if (aProjectType == Graphic3d_Camera::Projection_Stereo)
545 if (myMainSceneFbos[0]->IsValid()
546 && !myMainSceneFbos[1]->IsValid())
548 aProjectType = Graphic3d_Camera::Projection_Perspective;
553 if (aProjectType == Graphic3d_Camera::Projection_Stereo)
555 OpenGl_FrameBuffer* aMainFbos[2] =
557 myMainSceneFbos[0]->IsValid() ? myMainSceneFbos[0].operator->() : NULL,
558 myMainSceneFbos[1]->IsValid() ? myMainSceneFbos[1].operator->() : NULL
560 OpenGl_FrameBuffer* anImmFbos[2] =
562 myImmediateSceneFbos[0]->IsValid() ? myImmediateSceneFbos[0].operator->() : NULL,
563 myImmediateSceneFbos[1]->IsValid() ? myImmediateSceneFbos[1].operator->() : NULL
565 if (aStereoMode == Graphic3d_StereoMode_SoftPageFlip
566 || aStereoMode == Graphic3d_StereoMode_QuadBuffer)
572 if (aCtx->arbFBO != NULL)
574 aCtx->arbFBO->glBindFramebuffer (GL_FRAMEBUFFER, OpenGl_FrameBuffer::NO_FRAMEBUFFER);
576 #if !defined(GL_ES_VERSION_2_0)
577 if (anImmFbos[0] == NULL)
579 aCtx->SetReadDrawBuffer (aStereoMode == Graphic3d_StereoMode_QuadBuffer ? GL_BACK_LEFT : GL_BACK);
582 toSwap = redrawImmediate (Graphic3d_Camera::Projection_MonoLeftEye,
585 Standard_True) || toSwap;
586 if (aStereoMode == Graphic3d_StereoMode_SoftPageFlip
588 && !aCtx->caps->buffersNoSwap)
593 if (aCtx->arbFBO != NULL)
595 aCtx->arbFBO->glBindFramebuffer (GL_FRAMEBUFFER, OpenGl_FrameBuffer::NO_FRAMEBUFFER);
597 #if !defined(GL_ES_VERSION_2_0)
598 if (anImmFbos[1] == NULL)
600 aCtx->SetReadDrawBuffer (aStereoMode == Graphic3d_StereoMode_QuadBuffer ? GL_BACK_RIGHT : GL_BACK);
603 toSwap = redrawImmediate (Graphic3d_Camera::Projection_MonoRightEye,
606 Standard_True) || toSwap;
607 if (anImmFbos[0] != NULL)
609 drawStereoPair (aFrameBuffer);
614 OpenGl_FrameBuffer* aMainFbo = myMainSceneFbos[0]->IsValid() ? myMainSceneFbos[0].operator->() : NULL;
615 OpenGl_FrameBuffer* anImmFbo = aFrameBuffer;
616 if (!aCtx->caps->useSystemBuffer && myImmediateSceneFbos[0]->IsValid())
618 anImmFbo = myImmediateSceneFbos[0].operator->();
620 #if !defined(GL_ES_VERSION_2_0)
621 if (aMainFbo == NULL)
623 aCtx->SetReadDrawBuffer (GL_BACK);
626 toSwap = redrawImmediate (aProjectType,
629 Standard_True) || toSwap;
631 && anImmFbo != aFrameBuffer)
633 blitBuffers (anImmFbo, aFrameBuffer, myToFlipOutput);
640 if (toSwap && !aCtx->caps->buffersNoSwap)
646 aCtx->core11fwd->glFlush();
649 myWasRedrawnGL = Standard_True;
652 // =======================================================================
655 // =======================================================================
656 void OpenGl_View::redraw (const Graphic3d_Camera::Projection theProjection, OpenGl_FrameBuffer* theReadDrawFbo)
658 Handle(OpenGl_Context) aCtx = myWorkspace->GetGlContext();
659 if (theReadDrawFbo != NULL)
661 theReadDrawFbo->BindBuffer (aCtx);
662 theReadDrawFbo->SetupViewport (aCtx);
666 aCtx->core11fwd->glViewport (0, 0, myWindow->Width(), myWindow->Height());
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);
678 #if !defined(GL_ES_VERSION_2_0)
681 glClearDepthf (1.0f);
684 if (myWorkspace->NamedStatus & OPENGL_NS_WHITEBACK)
686 // set background to white
687 glClearColor (1.0f, 1.0f, 1.0f, 1.0f);
691 const OpenGl_Vec4& aBgColor = myBgColor;
692 glClearColor (aBgColor.r(), aBgColor.g(), aBgColor.b(), 0.0f);
697 render (theProjection, theReadDrawFbo, Standard_False);
700 // =======================================================================
701 // function : redrawMonoImmediate
703 // =======================================================================
704 bool OpenGl_View::redrawImmediate (const Graphic3d_Camera::Projection theProjection,
705 OpenGl_FrameBuffer* theReadFbo,
706 OpenGl_FrameBuffer* theDrawFbo,
707 const Standard_Boolean theIsPartialUpdate)
709 Handle(OpenGl_Context) aCtx = myWorkspace->GetGlContext();
710 GLboolean toCopyBackToFront = GL_FALSE;
711 if (!myTransientDrawToFront)
713 myBackBufferRestored = Standard_False;
715 else if (theReadFbo != NULL
716 && theReadFbo->IsValid()
719 if (!blitBuffers (theReadFbo, theDrawFbo))
724 else if (theDrawFbo == NULL)
726 #if !defined(GL_ES_VERSION_2_0)
727 aCtx->core11fwd->glGetBooleanv (GL_DOUBLEBUFFER, &toCopyBackToFront);
729 if (toCopyBackToFront)
731 if (!HasImmediateStructures()
732 && !theIsPartialUpdate)
734 // prefer Swap Buffers within Redraw in compatibility mode (without FBO)
741 myBackBufferRestored = Standard_False;
746 myBackBufferRestored = Standard_False;
748 myIsImmediateDrawn = Standard_True;
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)
758 glClearDepthf (1.0f);
761 render (theProjection, theDrawFbo, Standard_True);
763 return !toCopyBackToFront;
766 //=======================================================================
769 //=======================================================================
770 void OpenGl_View::render (Graphic3d_Camera::Projection theProjection,
771 OpenGl_FrameBuffer* theOutputFBO,
772 const Standard_Boolean theToDrawImmediate)
774 // ==================================
775 // Step 1: Prepare for render
776 // ==================================
778 const Handle(OpenGl_Context)& aContext = myWorkspace->GetGlContext();
780 #if !defined(GL_ES_VERSION_2_0)
781 // Disable current clipping planes
782 if (aContext->core11 != NULL)
784 const Standard_Integer aMaxPlanes = aContext->MaxClipPlanes();
785 for (Standard_Integer aClipPlaneId = GL_CLIP_PLANE0; aClipPlaneId < GL_CLIP_PLANE0 + aMaxPlanes; ++aClipPlaneId)
787 aContext->core11fwd->glDisable (aClipPlaneId);
792 // Update states of OpenGl_BVHTreeSelector (frustum culling algorithm).
793 myBVHSelector.SetViewVolume (myCamera);
794 myBVHSelector.SetViewportSize (myWindow->Width(), myWindow->Height());
796 const Handle(OpenGl_ShaderManager)& aManager = aContext->ShaderManager();
797 if (StateInfo (myCurrLightSourceState, aManager->LightSourceState().Index()) != myLastLightSourceState)
799 aManager->UpdateLightSourceStateTo (myShadingModel == Graphic3d_TOSM_NONE ? &myNoShadingLight : &myLights);
800 myLastLightSourceState = StateInfo (myCurrLightSourceState, aManager->LightSourceState().Index());
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);
809 aContext->ProjectionState.SetCurrent (myCamera->ProjectionMatrixF());
810 aContext->WorldViewState .SetCurrent (myCamera->OrientationMatrixF());
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)
819 aContext->ApplyProjectionMatrix();
820 aContext->ApplyWorldViewMatrix();
823 if (aManager->ModelWorldState().Index() == 0)
825 aContext->ShaderManager()->UpdateModelWorldStateTo (OpenGl_Mat4());
828 myWorldViewProjState = aWVPState;
830 // ====================================
831 // Step 2: Redraw background
832 // ====================================
835 if (!theToDrawImmediate)
837 drawBackground (myWorkspace);
840 #if !defined(GL_ES_VERSION_2_0)
841 // Switch off lighting by default
842 if (aContext->core11 != NULL)
844 glDisable(GL_LIGHTING);
848 // =================================
849 // Step 3: Redraw main plane
850 // =================================
852 // Setup face culling
853 GLboolean isCullFace = GL_FALSE;
854 if (myBackfacing != Graphic3d_TOBM_AUTOMATIC)
856 isCullFace = glIsEnabled (GL_CULL_FACE);
857 if (myBackfacing == Graphic3d_TOBM_DISABLE)
859 glEnable (GL_CULL_FACE);
860 glCullFace (GL_BACK);
863 glDisable (GL_CULL_FACE);
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)
874 aContext->SetGlNormalizeEnabled (Standard_True);
878 aContext->SetGlNormalizeEnabled (Standard_False);
883 && aContext->core11 != NULL)
885 Standard_Real aFogFrontConverted = (Standard_Real )myFog.Front + myCamera->Distance();
886 if (myCamera->ZFar() < aFogFrontConverted)
888 aFogFrontConverted = myCamera->ZFar();
889 myFog.Front = (Standard_ShortReal )(aFogFrontConverted - myCamera->Distance());
892 Standard_Real aFogBackConverted = (Standard_Real )myFog.Back + myCamera->Distance();
893 if (myCamera->ZFar() < aFogFrontConverted)
895 aFogBackConverted = myCamera->ZFar();
896 myFog.Back = (Standard_ShortReal )(aFogBackConverted - myCamera->Distance());
899 if (aFogFrontConverted > aFogBackConverted)
901 myFog.Front = (Standard_ShortReal )(aFogFrontConverted - myCamera->Distance());
902 myFog.Back = (Standard_ShortReal )(aFogBackConverted - myCamera->Distance());
905 glFogi(GL_FOG_MODE, GL_LINEAR);
906 glFogf(GL_FOG_START, (Standard_ShortReal )aFogFrontConverted);
907 glFogf(GL_FOG_END, (Standard_ShortReal )aFogBackConverted);
908 glFogfv(GL_FOG_COLOR, myFog.Color.GetData());
911 else if (aContext->core11 != NULL)
916 // Apply InteriorShadingMethod
917 if (aContext->core11 != NULL)
919 aContext->core11->glShadeModel (myShadingModel == Graphic3d_TOSM_FACET
920 || myShadingModel == Graphic3d_TOSM_NONE ? GL_FLAT : GL_SMOOTH);
924 aManager->SetShadingModel (myShadingModel);
926 // Apply AntiAliasing
928 myWorkspace->NamedStatus |= OPENGL_NS_ANTIALIASING;
930 myWorkspace->NamedStatus &= ~OPENGL_NS_ANTIALIASING;
932 if (!aManager->IsEmpty())
934 aManager->UpdateClippingState();
938 if (theProjection == Graphic3d_Camera::Projection_MonoLeftEye)
940 aContext->ProjectionState.SetCurrent (myCamera->ProjectionStereoLeftF());
941 aContext->ApplyProjectionMatrix();
943 else if (theProjection == Graphic3d_Camera::Projection_MonoRightEye)
945 aContext->ProjectionState.SetCurrent (myCamera->ProjectionStereoRightF());
946 aContext->ApplyProjectionMatrix();
949 myWorkspace->SetEnvironmentTexture (myTextureEnv);
951 renderScene (theProjection, theOutputFBO, theToDrawImmediate);
953 myWorkspace->SetEnvironmentTexture (Handle(OpenGl_Texture)());
955 // ===============================
957 // ===============================
959 // Resetting GL parameters according to the default aspects
960 // in order to synchronize GL state with the graphic driver state
961 // before drawing auxiliary stuff (trihedrons, overlayer)
962 myWorkspace->ResetAppliedAspect();
964 aContext->ChangeClipping().RemoveAll (aContext);
966 if (!aManager->IsEmpty())
968 aManager->RevertClippingState();
970 // We need to disable (unbind) all shaders programs to ensure
971 // that all objects without specified aspect will be drawn
972 // correctly (such as background)
973 aContext->BindProgram (NULL);
977 if (!theToDrawImmediate)
979 renderTrihedron (myWorkspace);
981 // Restore face culling
982 if (myBackfacing != Graphic3d_TOBM_AUTOMATIC)
986 glEnable (GL_CULL_FACE);
987 glCullFace (GL_BACK);
990 glDisable (GL_CULL_FACE);
994 // ==============================================================
995 // Step 6: Keep shader manager informed about last View
996 // ==============================================================
998 if (!aManager.IsNull())
1000 aManager->SetLastView (this);
1004 // =======================================================================
1005 // function : InvalidateBVHData
1007 // =======================================================================
1008 void OpenGl_View::InvalidateBVHData (const Graphic3d_ZLayerId theLayerId)
1010 myZLayers.InvalidateBVHData (theLayerId);
1013 //=======================================================================
1014 //function : renderStructs
1016 //=======================================================================
1017 void OpenGl_View::renderStructs (Graphic3d_Camera::Projection theProjection,
1018 OpenGl_FrameBuffer* theReadDrawFbo,
1019 const Standard_Boolean theToDrawImmediate)
1021 if ( myZLayers.NbStructures() <= 0 )
1024 Handle(OpenGl_Context) aCtx = myWorkspace->GetGlContext();
1025 if ( (myWorkspace->NamedStatus & OPENGL_NS_2NDPASSNEED) == 0 )
1027 #if !defined(GL_ES_VERSION_2_0)
1028 const int anAntiAliasingMode = myWorkspace->AntiAliasingMode();
1031 if ( !myAntiAliasing )
1033 #if !defined(GL_ES_VERSION_2_0)
1034 if (aCtx->core11 != NULL)
1036 glDisable (GL_POINT_SMOOTH);
1038 glDisable(GL_LINE_SMOOTH);
1039 if( anAntiAliasingMode & 2 ) glDisable(GL_POLYGON_SMOOTH);
1041 glBlendFunc (GL_ONE, GL_ZERO);
1042 glDisable (GL_BLEND);
1046 #if !defined(GL_ES_VERSION_2_0)
1047 if (aCtx->core11 != NULL)
1049 glEnable(GL_POINT_SMOOTH);
1051 glEnable(GL_LINE_SMOOTH);
1052 if( anAntiAliasingMode & 2 ) glEnable(GL_POLYGON_SMOOTH);
1054 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1055 glEnable (GL_BLEND);
1059 Standard_Boolean toRenderGL = theToDrawImmediate ||
1060 myRenderParams.Method != Graphic3d_RM_RAYTRACING ||
1061 myRaytraceInitStatus == OpenGl_RT_FAIL ||
1066 toRenderGL = !initRaytraceResources (aCtx) ||
1067 !updateRaytraceGeometry (OpenGl_GUM_CHECK, myId, aCtx);
1069 toRenderGL |= !myIsRaytraceDataValid; // if no ray-trace data use OpenGL
1073 const Standard_Integer aSizeX = theReadDrawFbo != NULL ? theReadDrawFbo->GetVPSizeX() : myWindow->Width();
1074 const Standard_Integer aSizeY = theReadDrawFbo != NULL ? theReadDrawFbo->GetVPSizeY() : myWindow->Height();
1075 myOpenGlFBO ->InitLazy (aCtx, aSizeX, aSizeY, myFboColorFormat, myFboDepthFormat, 0);
1077 if (myRaytraceFilter.IsNull())
1078 myRaytraceFilter = new OpenGl_RaytraceFilter;
1080 myRaytraceFilter->SetPrevRenderFilter (myWorkspace->GetRenderFilter());
1082 if (theReadDrawFbo != NULL)
1083 theReadDrawFbo->UnbindBuffer (aCtx);
1085 // Prepare preliminary OpenGL output
1086 if (aCtx->arbFBOBlit != NULL)
1088 // Render bottom OSD layer
1089 myZLayers.Render (myWorkspace, theToDrawImmediate, OpenGl_LF_Bottom);
1091 myWorkspace->SetRenderFilter (myRaytraceFilter);
1093 if (theReadDrawFbo != NULL)
1095 theReadDrawFbo->BindReadBuffer (aCtx);
1099 aCtx->arbFBO->glBindFramebuffer (GL_READ_FRAMEBUFFER, 0);
1102 myOpenGlFBO->BindDrawBuffer (aCtx);
1104 aCtx->arbFBOBlit->glBlitFramebuffer (0, 0, aSizeX, aSizeY,
1105 0, 0, aSizeX, aSizeY,
1106 GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT,
1109 // Render non-polygonal elements in default layer
1110 myZLayers.Render (myWorkspace, theToDrawImmediate, OpenGl_LF_Default);
1112 myWorkspace->SetRenderFilter (myRaytraceFilter->PrevRenderFilter());
1115 if (theReadDrawFbo != NULL)
1117 theReadDrawFbo->BindBuffer (aCtx);
1121 aCtx->arbFBO->glBindFramebuffer (GL_FRAMEBUFFER, 0);
1124 // Reset OpenGl aspects state to default to avoid enabling of
1125 // backface culling which is not supported in ray-tracing.
1126 myWorkspace->ResetAppliedAspect();
1128 // Ray-tracing polygonal primitive arrays
1129 raytrace (aSizeX, aSizeY, theProjection, theReadDrawFbo, aCtx);
1131 // Render upper (top and topmost) OpenGL layers
1132 myZLayers.Render (myWorkspace, theToDrawImmediate, OpenGl_LF_Upper);
1136 // Redraw 3D scene using OpenGL in standard
1137 // mode or in case of ray-tracing failure
1140 myZLayers.Render (myWorkspace, theToDrawImmediate, OpenGl_LF_All);
1142 // Set flag that scene was redrawn by standard pipeline
1143 myWasRedrawnGL = Standard_True;
1147 //=======================================================================
1148 //function : renderTrihedron
1150 //=======================================================================
1151 void OpenGl_View::renderTrihedron (const Handle(OpenGl_Workspace) &theWorkspace)
1153 // display global trihedron
1154 if (myToShowTrihedron)
1156 // disable environment texture
1157 Handle(OpenGl_Texture) anEnvironmentTexture = theWorkspace->EnvironmentTexture();
1158 theWorkspace->SetEnvironmentTexture (Handle(OpenGl_Texture)());
1160 myTrihedron.Render (theWorkspace);
1162 // restore environment texture
1163 theWorkspace->SetEnvironmentTexture (anEnvironmentTexture);
1165 if (myToShowGradTrihedron)
1167 myGraduatedTrihedron.Render (theWorkspace);
1171 // =======================================================================
1172 // function : Invalidate
1174 // =======================================================================
1175 void OpenGl_View::Invalidate()
1177 myBackBufferRestored = Standard_False;
1180 //=======================================================================
1181 //function : renderScene
1183 //=======================================================================
1184 void OpenGl_View::renderScene (Graphic3d_Camera::Projection theProjection,
1185 OpenGl_FrameBuffer* theReadDrawFbo,
1186 const Standard_Boolean theToDrawImmediate)
1188 const Handle(OpenGl_Context)& aContext = myWorkspace->GetGlContext();
1190 if (myZClip.Back.IsOn || myZClip.Front.IsOn)
1192 Handle(Graphic3d_ClipPlane) aPlaneBack;
1193 Handle(Graphic3d_ClipPlane) aPlaneFront;
1195 if (myZClip.Back.IsOn)
1197 Standard_Real aClipBackConverted = (Standard_Real )myZClip.Front.Limit + myCamera->Distance();
1198 if (myCamera->ZFar() < aClipBackConverted)
1200 aClipBackConverted = myCamera->ZFar();
1201 myZClip.Back.Limit = (Standard_ShortReal )(aClipBackConverted - myCamera->Distance());
1203 const Graphic3d_ClipPlane::Equation aBackEquation (0.0, 0.0, 1.0, (Standard_ShortReal )aClipBackConverted);
1204 aPlaneBack = new Graphic3d_ClipPlane (aBackEquation);
1207 if (myZClip.Front.IsOn)
1209 Standard_Real aClipFrontConverted = (Standard_Real )myZClip.Front.Limit + myCamera->Distance();
1210 if (myCamera->ZNear() > aClipFrontConverted)
1212 aClipFrontConverted = myCamera->ZNear();
1213 myZClip.Front.Limit = (Standard_ShortReal )(aClipFrontConverted - myCamera->Distance());
1215 const Graphic3d_ClipPlane::Equation aFrontEquation (0.0, 0.0, -1.0, (Standard_ShortReal )-aClipFrontConverted);
1216 aPlaneFront = new Graphic3d_ClipPlane (aFrontEquation);
1219 // Specify slicing planes with identity transformation
1220 if (!aPlaneBack.IsNull() || !aPlaneFront.IsNull())
1222 Graphic3d_SequenceOfHClipPlane aSlicingPlanes;
1223 if (!aPlaneBack.IsNull())
1225 aSlicingPlanes.Append (aPlaneBack);
1228 if (!aPlaneFront.IsNull())
1230 aSlicingPlanes.Append (aPlaneFront);
1233 // add planes at loaded view matrix state
1234 aContext->ChangeClipping().AddView (aContext, aSlicingPlanes);
1239 // set printing scale/tiling transformation
1240 Handle(OpenGl_PrinterContext) aPrintContext = myWorkspace->PrinterContext();
1241 if (!aPrintContext.IsNull())
1243 aContext->ProjectionState.Push();
1244 aContext->ProjectionState.SetCurrent (aPrintContext->ProjTransformation() * aContext->ProjectionState.Current());
1245 aContext->ApplyProjectionMatrix();
1249 // Specify clipping planes in view transformation space
1250 if (!myClipPlanes.IsEmpty())
1252 Graphic3d_SequenceOfHClipPlane aUserPlanes;
1253 Graphic3d_SequenceOfHClipPlane::Iterator aClippingIt (myClipPlanes);
1254 for (; aClippingIt.More(); aClippingIt.Next())
1256 const Handle(Graphic3d_ClipPlane)& aClipPlane = aClippingIt.Value();
1257 if (aClipPlane->IsOn())
1259 aUserPlanes.Append (aClipPlane);
1263 if (!aUserPlanes.IsEmpty())
1265 aContext->ChangeClipping().AddWorldLazy (aContext, aUserPlanes);
1268 if (!aContext->ShaderManager()->IsEmpty())
1270 aContext->ShaderManager()->UpdateClippingState();
1274 #if !defined(GL_ES_VERSION_2_0)
1276 if (aContext->core11 != NULL)
1279 Graphic3d_Vec4 anAmbientColor (THE_DEFAULT_AMBIENT[0],
1280 THE_DEFAULT_AMBIENT[1],
1281 THE_DEFAULT_AMBIENT[2],
1282 THE_DEFAULT_AMBIENT[3]);
1283 GLenum aLightGlId = GL_LIGHT0;
1285 OpenGl_ListOfLight::Iterator aLightIt (myShadingModel == Graphic3d_TOSM_NONE ? myNoShadingLight : myLights);
1286 for (; aLightIt.More(); aLightIt.Next())
1288 bindLight (aLightIt.Value(), aLightGlId, anAmbientColor, myWorkspace);
1291 // apply accumulated ambient color
1292 anAmbientColor.a() = 1.0f;
1293 glLightModelfv (GL_LIGHT_MODEL_AMBIENT, anAmbientColor.GetData());
1295 if (aLightGlId != GL_LIGHT0)
1297 glEnable (GL_LIGHTING);
1299 // switch off unused lights
1300 for (; aLightGlId <= GL_LIGHT7; ++aLightGlId)
1302 glDisable (aLightGlId);
1307 // Clear status bitfields
1308 myWorkspace->NamedStatus &= ~(OPENGL_NS_2NDPASSNEED | OPENGL_NS_2NDPASSDO);
1311 renderStructs (theProjection, theReadDrawFbo, theToDrawImmediate);
1312 myWorkspace->DisableTexture();
1315 if (myWorkspace->NamedStatus & OPENGL_NS_2NDPASSNEED)
1317 myWorkspace->NamedStatus |= OPENGL_NS_2NDPASSDO;
1319 // Remember OpenGl properties
1320 GLint aSaveBlendDst = GL_ONE_MINUS_SRC_ALPHA, aSaveBlendSrc = GL_SRC_ALPHA;
1321 GLint aSaveZbuffFunc;
1322 GLboolean aSaveZbuffWrite;
1323 glGetBooleanv (GL_DEPTH_WRITEMASK, &aSaveZbuffWrite);
1324 glGetIntegerv (GL_DEPTH_FUNC, &aSaveZbuffFunc);
1325 #if !defined(GL_ES_VERSION_2_0)
1326 glGetIntegerv (GL_BLEND_DST, &aSaveBlendDst);
1327 glGetIntegerv (GL_BLEND_SRC, &aSaveBlendSrc);
1329 GLboolean wasZbuffEnabled = glIsEnabled (GL_DEPTH_TEST);
1330 GLboolean wasBlendEnabled = glIsEnabled (GL_BLEND);
1332 // Change the properties for second rendering pass
1333 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1334 glEnable (GL_BLEND);
1336 glDepthFunc (GL_EQUAL);
1337 glDepthMask (GL_FALSE);
1338 glEnable (GL_DEPTH_TEST);
1341 renderStructs (theProjection, theReadDrawFbo, theToDrawImmediate);
1342 myWorkspace->DisableTexture();
1344 // Restore properties back
1345 glBlendFunc (aSaveBlendSrc, aSaveBlendDst);
1346 if (!wasBlendEnabled)
1347 glDisable (GL_BLEND);
1349 glDepthFunc (aSaveZbuffFunc);
1350 glDepthMask (aSaveZbuffWrite);
1351 if (!wasZbuffEnabled)
1352 glDisable (GL_DEPTH_FUNC);
1355 // Apply restored view matrix.
1356 aContext->ApplyWorldViewMatrix();
1359 // set printing scale/tiling transformation
1360 if (!aPrintContext.IsNull())
1362 aContext->ProjectionState.Pop();
1363 aContext->ApplyProjectionMatrix();
1368 // =======================================================================
1369 // function : bindDefaultFbo
1371 // =======================================================================
1372 void OpenGl_View::bindDefaultFbo (OpenGl_FrameBuffer* theCustomFbo)
1374 Handle(OpenGl_Context) aCtx = myWorkspace->GetGlContext();
1375 OpenGl_FrameBuffer* anFbo = (theCustomFbo != NULL && theCustomFbo->IsValid())
1377 : (!aCtx->DefaultFrameBuffer().IsNull()
1378 && aCtx->DefaultFrameBuffer()->IsValid()
1379 ? aCtx->DefaultFrameBuffer().operator->()
1383 anFbo->BindBuffer (aCtx);
1387 #if !defined(GL_ES_VERSION_2_0)
1388 aCtx->SetReadDrawBuffer (GL_BACK);
1390 if (aCtx->arbFBO != NULL)
1392 aCtx->arbFBO->glBindFramebuffer (GL_FRAMEBUFFER, OpenGl_FrameBuffer::NO_FRAMEBUFFER);
1396 aCtx->core11fwd->glViewport (0, 0, myWindow->Width(), myWindow->Height());
1399 // =======================================================================
1400 // function : initBlitQuad
1402 // =======================================================================
1403 OpenGl_VertexBuffer* OpenGl_View::initBlitQuad (const Standard_Boolean theToFlip)
1405 OpenGl_VertexBuffer* aVerts = NULL;
1408 aVerts = &myFullScreenQuad;
1409 if (!aVerts->IsValid())
1411 OpenGl_Vec4 aQuad[4] =
1413 OpenGl_Vec4( 1.0f, -1.0f, 1.0f, 0.0f),
1414 OpenGl_Vec4( 1.0f, 1.0f, 1.0f, 1.0f),
1415 OpenGl_Vec4(-1.0f, -1.0f, 0.0f, 0.0f),
1416 OpenGl_Vec4(-1.0f, 1.0f, 0.0f, 1.0f)
1418 aVerts->Init (myWorkspace->GetGlContext(), 4, 4, aQuad[0].GetData());
1423 aVerts = &myFullScreenQuadFlip;
1424 if (!aVerts->IsValid())
1426 OpenGl_Vec4 aQuad[4] =
1428 OpenGl_Vec4( 1.0f, -1.0f, 1.0f, 1.0f),
1429 OpenGl_Vec4( 1.0f, 1.0f, 1.0f, 0.0f),
1430 OpenGl_Vec4(-1.0f, -1.0f, 0.0f, 1.0f),
1431 OpenGl_Vec4(-1.0f, 1.0f, 0.0f, 0.0f)
1433 aVerts->Init (myWorkspace->GetGlContext(), 4, 4, aQuad[0].GetData());
1439 // =======================================================================
1440 // function : blitBuffers
1442 // =======================================================================
1443 bool OpenGl_View::blitBuffers (OpenGl_FrameBuffer* theReadFbo,
1444 OpenGl_FrameBuffer* theDrawFbo,
1445 const Standard_Boolean theToFlip)
1447 Handle(OpenGl_Context) aCtx = myWorkspace->GetGlContext();
1448 if (theReadFbo == NULL || aCtx->IsFeedback())
1452 else if (theReadFbo == theDrawFbo)
1457 // clear destination before blitting
1458 if (theDrawFbo != NULL
1459 && theDrawFbo->IsValid())
1461 theDrawFbo->BindBuffer (aCtx);
1465 aCtx->arbFBO->glBindFramebuffer (GL_FRAMEBUFFER, OpenGl_FrameBuffer::NO_FRAMEBUFFER);
1467 #if !defined(GL_ES_VERSION_2_0)
1468 aCtx->core20fwd->glClearDepth (1.0);
1470 aCtx->core20fwd->glClearDepthf (1.0f);
1472 aCtx->core20fwd->glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
1474 #if !defined(GL_ES_VERSION_2_0)
1475 if (aCtx->arbFBOBlit != NULL
1476 && theReadFbo->NbSamples() != 0)
1478 GLbitfield aCopyMask = 0;
1479 theReadFbo->BindReadBuffer (aCtx);
1480 if (theDrawFbo != NULL
1481 && theDrawFbo->IsValid())
1483 theDrawFbo->BindDrawBuffer (aCtx);
1484 if (theDrawFbo->HasColor()
1485 && theReadFbo->HasColor())
1487 aCopyMask |= GL_COLOR_BUFFER_BIT;
1489 if (theDrawFbo->HasDepth()
1490 && theReadFbo->HasDepth())
1492 aCopyMask |= GL_DEPTH_BUFFER_BIT;
1497 if (theReadFbo->HasColor())
1499 aCopyMask |= GL_COLOR_BUFFER_BIT;
1501 if (theReadFbo->HasDepth())
1503 aCopyMask |= GL_DEPTH_BUFFER_BIT;
1505 aCtx->arbFBO->glBindFramebuffer (GL_DRAW_FRAMEBUFFER, OpenGl_FrameBuffer::NO_FRAMEBUFFER);
1508 // we don't copy stencil buffer here... does it matter for performance?
1509 aCtx->arbFBOBlit->glBlitFramebuffer (0, 0, theReadFbo->GetVPSizeX(), theReadFbo->GetVPSizeY(),
1510 0, 0, theReadFbo->GetVPSizeX(), theReadFbo->GetVPSizeY(),
1511 aCopyMask, GL_NEAREST);
1512 const int anErr = ::glGetError();
1513 if (anErr != GL_NO_ERROR)
1515 // glBlitFramebuffer() might fail in several cases:
1516 // - Both FBOs have MSAA and they are samples number does not match.
1517 // OCCT checks that this does not happen,
1518 // however some graphics drivers provide an option for overriding MSAA.
1519 // In this case window MSAA might be non-zero (and application can not check it)
1520 // and might not match MSAA of our offscreen FBOs.
1521 // - Pixel formats of FBOs do not match.
1522 // This also might happen with window has pixel format,
1523 // e.g. Mesa fails blitting RGBA8 -> RGB8 while other drivers support this conversion.
1524 TCollection_ExtendedString aMsg = TCollection_ExtendedString() + "FBO blitting has failed [Error #" + anErr + "]\n"
1525 + " Please check your graphics driver settings or try updating driver.";
1526 if (theReadFbo->NbSamples() != 0)
1528 myToDisableMSAA = true;
1529 aMsg += "\n MSAA settings should not be overridden by driver!";
1531 aCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION,
1532 GL_DEBUG_TYPE_ERROR,
1534 GL_DEBUG_SEVERITY_HIGH,
1538 if (theDrawFbo != NULL
1539 && theDrawFbo->IsValid())
1541 theDrawFbo->BindBuffer (aCtx);
1545 aCtx->arbFBO->glBindFramebuffer (GL_FRAMEBUFFER, OpenGl_FrameBuffer::NO_FRAMEBUFFER);
1551 aCtx->core20fwd->glDepthFunc (GL_ALWAYS);
1552 aCtx->core20fwd->glDepthMask (GL_TRUE);
1553 aCtx->core20fwd->glEnable (GL_DEPTH_TEST);
1555 myWorkspace->DisableTexture();
1557 OpenGl_VertexBuffer* aVerts = initBlitQuad (theToFlip);
1558 const Handle(OpenGl_ShaderManager)& aManager = aCtx->ShaderManager();
1559 if (aVerts->IsValid()
1560 && aManager->BindFboBlitProgram())
1562 theReadFbo->ColorTexture() ->Bind (aCtx, GL_TEXTURE0 + 0);
1563 theReadFbo->DepthStencilTexture()->Bind (aCtx, GL_TEXTURE0 + 1);
1564 aVerts->BindVertexAttrib (aCtx, Graphic3d_TOA_POS);
1566 aCtx->core20fwd->glDrawArrays (GL_TRIANGLE_STRIP, 0, 4);
1568 aVerts->UnbindVertexAttrib (aCtx, Graphic3d_TOA_POS);
1569 theReadFbo->DepthStencilTexture()->Unbind (aCtx, GL_TEXTURE0 + 1);
1570 theReadFbo->ColorTexture() ->Unbind (aCtx, GL_TEXTURE0 + 0);
1571 aCtx->BindProgram (NULL);
1575 TCollection_ExtendedString aMsg = TCollection_ExtendedString()
1576 + "Error! FBO blitting has failed";
1577 aCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION,
1578 GL_DEBUG_TYPE_ERROR,
1580 GL_DEBUG_SEVERITY_HIGH,
1582 myHasFboBlit = Standard_False;
1583 theReadFbo->Release (aCtx.operator->());
1590 // =======================================================================
1591 // function : drawStereoPair
1593 // =======================================================================
1594 void OpenGl_View::drawStereoPair (OpenGl_FrameBuffer* theDrawFbo)
1596 const Handle(OpenGl_Context)& aCtx = myWorkspace->GetGlContext();
1597 bindDefaultFbo (theDrawFbo);
1598 OpenGl_FrameBuffer* aPair[2] =
1600 myImmediateSceneFbos[0]->IsValid() ? myImmediateSceneFbos[0].operator->() : NULL,
1601 myImmediateSceneFbos[1]->IsValid() ? myImmediateSceneFbos[1].operator->() : NULL
1603 if (aPair[0] == NULL
1605 || !myTransientDrawToFront)
1607 aPair[0] = myMainSceneFbos[0]->IsValid() ? myMainSceneFbos[0].operator->() : NULL;
1608 aPair[1] = myMainSceneFbos[1]->IsValid() ? myMainSceneFbos[1].operator->() : NULL;
1611 if (aPair[0] == NULL
1612 || aPair[1] == NULL)
1617 if (aPair[0]->NbSamples() != 0)
1619 // resolve MSAA buffers before drawing
1620 if (!myOpenGlFBO ->InitLazy (aCtx, aPair[0]->GetVPSizeX(), aPair[0]->GetVPSizeY(), myFboColorFormat, myFboDepthFormat, 0)
1621 || !myOpenGlFBO2->InitLazy (aCtx, aPair[0]->GetVPSizeX(), aPair[0]->GetVPSizeY(), myFboColorFormat, 0, 0))
1623 aCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION,
1624 GL_DEBUG_TYPE_ERROR,
1626 GL_DEBUG_SEVERITY_HIGH,
1627 "Error! Unable to allocate FBO for blitting stereo pair");
1628 bindDefaultFbo (theDrawFbo);
1632 if (!blitBuffers (aPair[0], myOpenGlFBO .operator->(), Standard_False)
1633 || !blitBuffers (aPair[1], myOpenGlFBO2.operator->(), Standard_False))
1635 bindDefaultFbo (theDrawFbo);
1639 aPair[0] = myOpenGlFBO .operator->();
1640 aPair[1] = myOpenGlFBO2.operator->();
1641 bindDefaultFbo (theDrawFbo);
1646 Standard_Integer left;
1647 Standard_Integer top;
1648 Standard_Integer right;
1649 Standard_Integer bottom;
1650 Standard_Integer dx() { return right - left; }
1651 Standard_Integer dy() { return bottom - top; }
1654 myWindow->PlatformWindow()->Position (aGeom.left, aGeom.top, aGeom.right, aGeom.bottom);
1656 Standard_Boolean toReverse = myRenderParams.ToReverseStereo;
1657 const Standard_Boolean isOddY = (aGeom.top + aGeom.dy()) % 2 == 1;
1658 const Standard_Boolean isOddX = aGeom.left % 2 == 1;
1660 && (myRenderParams.StereoMode == Graphic3d_StereoMode_RowInterlaced
1661 || myRenderParams.StereoMode == Graphic3d_StereoMode_ChessBoard))
1663 toReverse = !toReverse;
1666 && (myRenderParams.StereoMode == Graphic3d_StereoMode_ColumnInterlaced
1667 || myRenderParams.StereoMode == Graphic3d_StereoMode_ChessBoard))
1669 toReverse = !toReverse;
1674 std::swap (aPair[0], aPair[1]);
1677 aCtx->core20fwd->glDepthFunc (GL_ALWAYS);
1678 aCtx->core20fwd->glDepthMask (GL_TRUE);
1679 aCtx->core20fwd->glEnable (GL_DEPTH_TEST);
1681 myWorkspace->DisableTexture();
1682 OpenGl_VertexBuffer* aVerts = initBlitQuad (myToFlipOutput);
1684 const Handle(OpenGl_ShaderManager)& aManager = aCtx->ShaderManager();
1685 if (aVerts->IsValid()
1686 && aManager->BindStereoProgram (myRenderParams.StereoMode))
1688 if (myRenderParams.StereoMode == Graphic3d_StereoMode_Anaglyph)
1690 OpenGl_Mat4 aFilterL, aFilterR;
1691 aFilterL.SetDiagonal (Graphic3d_Vec4 (0.0f, 0.0f, 0.0f, 0.0f));
1692 aFilterR.SetDiagonal (Graphic3d_Vec4 (0.0f, 0.0f, 0.0f, 0.0f));
1693 switch (myRenderParams.AnaglyphFilter)
1695 case Graphic3d_RenderingParams::Anaglyph_RedCyan_Simple:
1697 aFilterL.SetRow (0, Graphic3d_Vec4 (1.0f, 0.0f, 0.0f, 0.0f));
1698 aFilterR.SetRow (1, Graphic3d_Vec4 (0.0f, 1.0f, 0.0f, 0.0f));
1699 aFilterR.SetRow (2, Graphic3d_Vec4 (0.0f, 0.0f, 1.0f, 0.0f));
1702 case Graphic3d_RenderingParams::Anaglyph_RedCyan_Optimized:
1704 aFilterL.SetRow (0, Graphic3d_Vec4 ( 0.4154f, 0.4710f, 0.16666667f, 0.0f));
1705 aFilterL.SetRow (1, Graphic3d_Vec4 (-0.0458f, -0.0484f, -0.0257f, 0.0f));
1706 aFilterL.SetRow (2, Graphic3d_Vec4 (-0.0547f, -0.0615f, 0.0128f, 0.0f));
1707 aFilterL.SetRow (3, Graphic3d_Vec4 ( 0.0f, 0.0f, 0.0f, 0.0f));
1708 aFilterR.SetRow (0, Graphic3d_Vec4 (-0.01090909f, -0.03636364f, -0.00606061f, 0.0f));
1709 aFilterR.SetRow (1, Graphic3d_Vec4 ( 0.37560000f, 0.73333333f, 0.01111111f, 0.0f));
1710 aFilterR.SetRow (2, Graphic3d_Vec4 (-0.06510000f, -0.12870000f, 1.29710000f, 0.0f));
1711 aFilterR.SetRow (3, Graphic3d_Vec4 ( 0.0f, 0.0f, 0.0f, 0.0f));
1714 case Graphic3d_RenderingParams::Anaglyph_YellowBlue_Simple:
1716 aFilterL.SetRow (0, Graphic3d_Vec4 (1.0f, 0.0f, 0.0f, 0.0f));
1717 aFilterL.SetRow (1, Graphic3d_Vec4 (0.0f, 1.0f, 0.0f, 0.0f));
1718 aFilterR.SetRow (2, Graphic3d_Vec4 (0.0f, 0.0f, 1.0f, 0.0f));
1721 case Graphic3d_RenderingParams::Anaglyph_YellowBlue_Optimized:
1723 aFilterL.SetRow (0, Graphic3d_Vec4 ( 1.062f, -0.205f, 0.299f, 0.0f));
1724 aFilterL.SetRow (1, Graphic3d_Vec4 (-0.026f, 0.908f, 0.068f, 0.0f));
1725 aFilterL.SetRow (2, Graphic3d_Vec4 (-0.038f, -0.173f, 0.022f, 0.0f));
1726 aFilterL.SetRow (3, Graphic3d_Vec4 ( 0.0f, 0.0f, 0.0f, 0.0f));
1727 aFilterR.SetRow (0, Graphic3d_Vec4 (-0.016f, -0.123f, -0.017f, 0.0f));
1728 aFilterR.SetRow (1, Graphic3d_Vec4 ( 0.006f, 0.062f, -0.017f, 0.0f));
1729 aFilterR.SetRow (2, Graphic3d_Vec4 ( 0.094f, 0.185f, 0.911f, 0.0f));
1730 aFilterR.SetRow (3, Graphic3d_Vec4 ( 0.0f, 0.0f, 0.0f, 0.0f));
1733 case Graphic3d_RenderingParams::Anaglyph_GreenMagenta_Simple:
1735 aFilterR.SetRow (0, Graphic3d_Vec4 (1.0f, 0.0f, 0.0f, 0.0f));
1736 aFilterL.SetRow (1, Graphic3d_Vec4 (0.0f, 1.0f, 0.0f, 0.0f));
1737 aFilterR.SetRow (2, Graphic3d_Vec4 (0.0f, 0.0f, 1.0f, 0.0f));
1740 case Graphic3d_RenderingParams::Anaglyph_UserDefined:
1742 aFilterL = myRenderParams.AnaglyphLeft;
1743 aFilterR = myRenderParams.AnaglyphRight;
1747 aCtx->ActiveProgram()->SetUniform (aCtx, "uMultL", aFilterL);
1748 aCtx->ActiveProgram()->SetUniform (aCtx, "uMultR", aFilterR);
1751 aPair[0]->ColorTexture()->Bind (aCtx, GL_TEXTURE0 + 0);
1752 aPair[1]->ColorTexture()->Bind (aCtx, GL_TEXTURE0 + 1);
1753 aVerts->BindVertexAttrib (aCtx, 0);
1755 aCtx->core20fwd->glDrawArrays (GL_TRIANGLE_STRIP, 0, 4);
1757 aVerts->UnbindVertexAttrib (aCtx, 0);
1758 aPair[1]->ColorTexture()->Unbind (aCtx, GL_TEXTURE0 + 1);
1759 aPair[0]->ColorTexture()->Unbind (aCtx, GL_TEXTURE0 + 0);
1763 TCollection_ExtendedString aMsg = TCollection_ExtendedString()
1764 + "Error! Anaglyph has failed";
1765 aCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION,
1766 GL_DEBUG_TYPE_ERROR,
1768 GL_DEBUG_SEVERITY_HIGH,
1773 // =======================================================================
1774 // function : copyBackToFront
1776 // =======================================================================
1777 void OpenGl_View::copyBackToFront()
1779 #if !defined(GL_ES_VERSION_2_0)
1781 OpenGl_Mat4 aProjectMat;
1782 Graphic3d_TransformUtils::Ortho2D (aProjectMat,
1783 0.f, static_cast<GLfloat> (myWindow->Width()), 0.f, static_cast<GLfloat> (myWindow->Height()));
1785 Handle(OpenGl_Context) aCtx = myWorkspace->GetGlContext();
1786 aCtx->WorldViewState.Push();
1787 aCtx->ProjectionState.Push();
1789 aCtx->WorldViewState.SetIdentity();
1790 aCtx->ProjectionState.SetCurrent (aProjectMat);
1792 aCtx->ApplyProjectionMatrix();
1793 aCtx->ApplyWorldViewMatrix();
1795 aCtx->DisableFeatures();
1797 switch (aCtx->DrawBuffer())
1801 aCtx->SetReadBuffer (GL_BACK_LEFT);
1802 aCtx->SetDrawBuffer (GL_FRONT_LEFT);
1807 aCtx->SetReadBuffer (GL_BACK_RIGHT);
1808 aCtx->SetDrawBuffer (GL_FRONT_RIGHT);
1813 aCtx->SetReadBuffer (GL_BACK);
1814 aCtx->SetDrawBuffer (GL_FRONT);
1819 glRasterPos2i (0, 0);
1820 glCopyPixels (0, 0, myWindow->Width() + 1, myWindow->Height() + 1, GL_COLOR);
1821 //glCopyPixels (0, 0, myWidth + 1, myHeight + 1, GL_DEPTH);
1823 aCtx->EnableFeatures();
1825 aCtx->WorldViewState.Pop();
1826 aCtx->ProjectionState.Pop();
1827 aCtx->ApplyProjectionMatrix();
1829 // read/write from front buffer now
1830 aCtx->SetReadBuffer (aCtx->DrawBuffer());
1832 myIsImmediateDrawn = Standard_False;