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>
20 #include <OpenGl_tgl_funcs.hxx>
22 #include <Graphic3d_GraphicDriver.hxx>
23 #include <Graphic3d_TextureParams.hxx>
24 #include <Graphic3d_Texture2Dmanual.hxx>
25 #include <Graphic3d_TransformUtils.hxx>
26 #include <Image_AlienPixMap.hxx>
28 #include <NCollection_Mat4.hxx>
30 #include <OpenGl_AspectLine.hxx>
31 #include <OpenGl_Context.hxx>
32 #include <OpenGl_Matrix.hxx>
33 #include <OpenGl_Workspace.hxx>
34 #include <OpenGl_View.hxx>
35 #include <OpenGl_Trihedron.hxx>
36 #include <OpenGl_GraduatedTrihedron.hxx>
37 #include <OpenGl_PrimitiveArray.hxx>
38 #include <OpenGl_PrinterContext.hxx>
39 #include <OpenGl_ShaderManager.hxx>
40 #include <OpenGl_ShaderProgram.hxx>
41 #include <OpenGl_Structure.hxx>
42 #include <OpenGl_ArbFBO.hxx>
48 static const GLfloat THE_DEFAULT_AMBIENT[4] = { 0.0f, 0.0f, 0.0f, 1.0f };
49 static const GLfloat THE_DEFAULT_SPOT_DIR[3] = { 0.0f, 0.0f, -1.0f };
50 static const GLfloat THE_DEFAULT_SPOT_EXPONENT = 0.0f;
51 static const GLfloat THE_DEFAULT_SPOT_CUTOFF = 180.0f;
54 extern void InitLayerProp (const int theListId); //szvgl: defined in OpenGl_GraphicDriver_Layer.cxx
56 #if !defined(GL_ES_VERSION_2_0)
58 //=======================================================================
59 //function : bindLight
61 //=======================================================================
62 static void bindLight (const OpenGl_Light& theLight,
64 Graphic3d_Vec4& theAmbientColor,
65 const Handle(OpenGl_Workspace)& theWorkspace)
67 // Only 8 lights in OpenGL...
68 if (theLightGlId > GL_LIGHT7)
73 if (theLight.Type == Graphic3d_TOLS_AMBIENT)
75 // add RGBA intensity of the ambient light
76 theAmbientColor += theLight.Color;
80 const Handle(OpenGl_Context)& aContext = theWorkspace->GetGlContext();
82 // the light is a headlight?
83 if (theLight.IsHeadlight)
85 aContext->WorldViewState.Push();
86 aContext->WorldViewState.SetIdentity();
88 aContext->ApplyWorldViewMatrix();
92 switch (theLight.Type)
94 case Graphic3d_TOLS_AMBIENT : break; // handled by separate if-clause at beginning of method
95 case Graphic3d_TOLS_DIRECTIONAL:
97 // if the last parameter of GL_POSITION, is zero, the corresponding light source is a Directional one
98 const OpenGl_Vec4 anInfDir = -theLight.Direction;
100 // to create a realistic effect, set the GL_SPECULAR parameter to the same value as the GL_DIFFUSE.
101 glLightfv (theLightGlId, GL_AMBIENT, THE_DEFAULT_AMBIENT);
102 glLightfv (theLightGlId, GL_DIFFUSE, theLight.Color.GetData());
103 glLightfv (theLightGlId, GL_SPECULAR, theLight.Color.GetData());
104 glLightfv (theLightGlId, GL_POSITION, anInfDir.GetData());
105 glLightfv (theLightGlId, GL_SPOT_DIRECTION, THE_DEFAULT_SPOT_DIR);
106 glLightf (theLightGlId, GL_SPOT_EXPONENT, THE_DEFAULT_SPOT_EXPONENT);
107 glLightf (theLightGlId, GL_SPOT_CUTOFF, THE_DEFAULT_SPOT_CUTOFF);
110 case Graphic3d_TOLS_POSITIONAL:
112 // to create a realistic effect, set the GL_SPECULAR parameter to the same value as the GL_DIFFUSE
113 glLightfv (theLightGlId, GL_AMBIENT, THE_DEFAULT_AMBIENT);
114 glLightfv (theLightGlId, GL_DIFFUSE, theLight.Color.GetData());
115 glLightfv (theLightGlId, GL_SPECULAR, theLight.Color.GetData());
116 glLightfv (theLightGlId, GL_POSITION, theLight.Position.GetData());
117 glLightfv (theLightGlId, GL_SPOT_DIRECTION, THE_DEFAULT_SPOT_DIR);
118 glLightf (theLightGlId, GL_SPOT_EXPONENT, THE_DEFAULT_SPOT_EXPONENT);
119 glLightf (theLightGlId, GL_SPOT_CUTOFF, THE_DEFAULT_SPOT_CUTOFF);
120 glLightf (theLightGlId, GL_CONSTANT_ATTENUATION, theLight.ConstAttenuation());
121 glLightf (theLightGlId, GL_LINEAR_ATTENUATION, theLight.LinearAttenuation());
122 glLightf (theLightGlId, GL_QUADRATIC_ATTENUATION, 0.0);
125 case Graphic3d_TOLS_SPOT:
127 glLightfv (theLightGlId, GL_AMBIENT, THE_DEFAULT_AMBIENT);
128 glLightfv (theLightGlId, GL_DIFFUSE, theLight.Color.GetData());
129 glLightfv (theLightGlId, GL_SPECULAR, theLight.Color.GetData());
130 glLightfv (theLightGlId, GL_POSITION, theLight.Position.GetData());
131 glLightfv (theLightGlId, GL_SPOT_DIRECTION, theLight.Direction.GetData());
132 glLightf (theLightGlId, GL_SPOT_EXPONENT, theLight.Concentration() * 128.0f);
133 glLightf (theLightGlId, GL_SPOT_CUTOFF, (theLight.Angle() * 180.0f) / GLfloat(M_PI));
134 glLightf (theLightGlId, GL_CONSTANT_ATTENUATION, theLight.ConstAttenuation());
135 glLightf (theLightGlId, GL_LINEAR_ATTENUATION, theLight.LinearAttenuation());
136 glLightf (theLightGlId, GL_QUADRATIC_ATTENUATION, 0.0f);
141 // restore matrix in case of headlight
142 if (theLight.IsHeadlight)
144 aContext->WorldViewState.Pop();
147 glEnable (theLightGlId++);
151 //=======================================================================
152 //function : drawBackground
154 //=======================================================================
155 void OpenGl_View::drawBackground (const Handle(OpenGl_Workspace)& theWorkspace)
157 const Handle(OpenGl_Context)& aCtx = theWorkspace->GetGlContext();
159 if ((theWorkspace->NamedStatus & OPENGL_NS_WHITEBACK) != 0 // no background
160 || (!myBgTextureArray->IsDefined() // no texture
161 && !myBgGradientArray->IsDefined())) // no gradient
166 const Standard_Boolean wasUsedZBuffer = theWorkspace->SetUseZBuffer (Standard_False);
169 aCtx->core11fwd->glDisable (GL_DEPTH_TEST);
172 aCtx->ProjectionState.Push();
173 aCtx->WorldViewState.Push();
174 aCtx->ModelWorldState.Push();
175 aCtx->ProjectionState.SetIdentity();
176 aCtx->WorldViewState.SetIdentity();
177 aCtx->ModelWorldState.SetIdentity();
178 aCtx->ApplyProjectionMatrix();
179 aCtx->ApplyModelViewMatrix();
181 // Drawing background gradient if:
182 // - gradient fill type is not Aspect_GFM_NONE and
183 // - either background texture is no specified or it is drawn in Aspect_FM_CENTERED mode
184 if (myBgGradientArray->IsDefined()
185 && (!myTextureParams->DoTextureMap()
186 || myBgTextureArray->TextureFillMethod() == Aspect_FM_CENTERED
187 || myBgTextureArray->TextureFillMethod() == Aspect_FM_NONE))
189 #if !defined(GL_ES_VERSION_2_0)
190 GLint aShadingModelOld = GL_SMOOTH;
191 if (aCtx->core11 != NULL)
193 aCtx->core11fwd->glDisable (GL_LIGHTING);
194 aCtx->core11fwd->glGetIntegerv (GL_SHADE_MODEL, &aShadingModelOld);
195 aCtx->core11->glShadeModel (GL_SMOOTH);
199 if (myBgGradientArray->IsDataChanged())
201 myBgGradientArray->Init (theWorkspace);
204 myBgGradientArray->Render (theWorkspace);
206 #if !defined(GL_ES_VERSION_2_0)
207 if (aCtx->core11 != NULL)
209 aCtx->core11->glShadeModel (aShadingModelOld);
214 // Drawing background image if it is defined
215 // (texture is defined and fill type is not Aspect_FM_NONE)
216 if (myBgTextureArray->IsDefined()
217 && myTextureParams->DoTextureMap())
219 aCtx->core11fwd->glDisable (GL_BLEND);
221 const OpenGl_AspectFace* anOldAspectFace = theWorkspace->SetAspectFace (myTextureParams);
223 if (myBgTextureArray->IsDataChanged()
224 || myBgTextureArray->IsViewSizeChanged (theWorkspace))
226 myBgTextureArray->Init (theWorkspace);
229 myBgTextureArray->Render (theWorkspace);
232 theWorkspace->SetAspectFace (anOldAspectFace);
235 aCtx->ModelWorldState.Pop();
236 aCtx->WorldViewState.Pop();
237 aCtx->ProjectionState.Pop();
238 aCtx->ApplyProjectionMatrix();
239 aCtx->ApplyModelViewMatrix();
243 theWorkspace->SetUseZBuffer (Standard_True);
244 aCtx->core11fwd->glEnable (GL_DEPTH_TEST);
248 //=======================================================================
251 //=======================================================================
252 void OpenGl_View::Redraw()
254 if (myRenderParams.Method == Graphic3d_RM_RAYTRACING
255 && !myCaps->vboDisable
256 && !myCaps->keepArrayData)
260 myDeviceLostFlag = Standard_True;
263 myCaps->keepArrayData = Standard_True;
266 if (!myWorkspace->Activate())
271 myWindow->SetSwapInterval();
274 const Graphic3d_StereoMode aStereoMode = myRenderParams.StereoMode;
275 Graphic3d_Camera::Projection aProjectType = myCamera->ProjectionType();
276 Handle(OpenGl_Context) aCtx = myWorkspace->GetGlContext();
278 // release pending GL resources
279 aCtx->ReleaseDelayed();
281 // fetch OpenGl context state
284 OpenGl_FrameBuffer* aFrameBuffer = (OpenGl_FrameBuffer* )myFBO;
285 bool toSwap = aCtx->IsRender()
286 && !aCtx->caps->buffersNoSwap
287 && aFrameBuffer == NULL;
289 Standard_Integer aSizeX = aFrameBuffer != NULL ? aFrameBuffer->GetVPSizeX() : myWindow->Width();
290 Standard_Integer aSizeY = aFrameBuffer != NULL ? aFrameBuffer->GetVPSizeY() : myWindow->Height();
292 // determine multisampling parameters
293 Standard_Integer aNbSamples = Max (Min (myRenderParams.NbMsaaSamples, aCtx->MaxMsaaSamples()), 0);
296 aNbSamples = OpenGl_Context::GetPowerOfTwo (aNbSamples, aCtx->MaxMsaaSamples());
299 if ( aFrameBuffer == NULL
300 && !aCtx->DefaultFrameBuffer().IsNull()
301 && aCtx->DefaultFrameBuffer()->IsValid())
303 aFrameBuffer = aCtx->DefaultFrameBuffer().operator->();
307 && (myTransientDrawToFront
308 || aProjectType == Graphic3d_Camera::Projection_Stereo
311 if (myMainSceneFbos[0]->GetVPSizeX() != aSizeX
312 || myMainSceneFbos[0]->GetVPSizeY() != aSizeY
313 || myMainSceneFbos[0]->NbSamples() != aNbSamples)
315 // prepare FBOs containing main scene
316 // for further blitting and rendering immediate presentations on top
317 if (aCtx->core20fwd != NULL)
319 myMainSceneFbos[0]->Init (aCtx, aSizeX, aSizeY, myFboColorFormat, myFboDepthFormat, aNbSamples);
321 if (!aCtx->caps->useSystemBuffer && myMainSceneFbos[0]->IsValid())
323 myImmediateSceneFbos[0]->InitLazy (aCtx, *myMainSceneFbos[0]);
329 myMainSceneFbos [0]->Release (aCtx.operator->());
330 myMainSceneFbos [1]->Release (aCtx.operator->());
331 myImmediateSceneFbos[0]->Release (aCtx.operator->());
332 myImmediateSceneFbos[1]->Release (aCtx.operator->());
333 myMainSceneFbos [0]->ChangeViewport (0, 0);
334 myMainSceneFbos [1]->ChangeViewport (0, 0);
335 myImmediateSceneFbos[0]->ChangeViewport (0, 0);
336 myImmediateSceneFbos[1]->ChangeViewport (0, 0);
339 if (aProjectType == Graphic3d_Camera::Projection_Stereo
340 && myMainSceneFbos[0]->IsValid())
342 myMainSceneFbos[1]->InitLazy (aCtx, *myMainSceneFbos[0]);
343 if (!myMainSceneFbos[1]->IsValid())
346 aProjectType = Graphic3d_Camera::Projection_Perspective;
348 else if (!myTransientDrawToFront)
352 else if (!aCtx->HasStereoBuffers() || aStereoMode != Graphic3d_StereoMode_QuadBuffer)
354 myImmediateSceneFbos[0]->InitLazy (aCtx, *myMainSceneFbos[0]);
355 myImmediateSceneFbos[1]->InitLazy (aCtx, *myMainSceneFbos[0]);
356 if (!myImmediateSceneFbos[0]->IsValid()
357 || !myImmediateSceneFbos[1]->IsValid())
359 aProjectType = Graphic3d_Camera::Projection_Perspective;
364 if (aProjectType == Graphic3d_Camera::Projection_Stereo)
366 OpenGl_FrameBuffer* aMainFbos[2] =
368 myMainSceneFbos[0]->IsValid() ? myMainSceneFbos[0].operator->() : NULL,
369 myMainSceneFbos[1]->IsValid() ? myMainSceneFbos[1].operator->() : NULL
371 OpenGl_FrameBuffer* anImmFbos[2] =
373 myImmediateSceneFbos[0]->IsValid() ? myImmediateSceneFbos[0].operator->() : NULL,
374 myImmediateSceneFbos[1]->IsValid() ? myImmediateSceneFbos[1].operator->() : NULL
377 if (!myTransientDrawToFront)
379 anImmFbos[0] = aMainFbos[0];
380 anImmFbos[1] = aMainFbos[1];
382 else if (aStereoMode == Graphic3d_StereoMode_SoftPageFlip
383 || aStereoMode == Graphic3d_StereoMode_QuadBuffer)
389 #if !defined(GL_ES_VERSION_2_0)
390 aCtx->SetReadDrawBuffer (aStereoMode == Graphic3d_StereoMode_QuadBuffer ? GL_BACK_LEFT : GL_BACK);
392 redraw (Graphic3d_Camera::Projection_MonoLeftEye, aMainFbos[0]);
393 myBackBufferRestored = Standard_True;
394 myIsImmediateDrawn = Standard_False;
395 #if !defined(GL_ES_VERSION_2_0)
396 aCtx->SetReadDrawBuffer (aStereoMode == Graphic3d_StereoMode_QuadBuffer ? GL_BACK_LEFT : GL_BACK);
398 if (!redrawImmediate (Graphic3d_Camera::Projection_MonoLeftEye, aMainFbos[0], anImmFbos[0]))
402 else if (aStereoMode == Graphic3d_StereoMode_SoftPageFlip && toSwap)
407 #if !defined(GL_ES_VERSION_2_0)
408 aCtx->SetReadDrawBuffer (aStereoMode == Graphic3d_StereoMode_QuadBuffer ? GL_BACK_RIGHT : GL_BACK);
410 redraw (Graphic3d_Camera::Projection_MonoRightEye, aMainFbos[1]);
411 myBackBufferRestored = Standard_True;
412 myIsImmediateDrawn = Standard_False;
413 if (!redrawImmediate (Graphic3d_Camera::Projection_MonoRightEye, aMainFbos[1], anImmFbos[1]))
418 if (anImmFbos[0] != NULL)
420 drawStereoPair (aFrameBuffer);
425 OpenGl_FrameBuffer* aMainFbo = myMainSceneFbos[0]->IsValid() ? myMainSceneFbos[0].operator->() : NULL;
426 OpenGl_FrameBuffer* anImmFbo = aFrameBuffer;
427 if (!aCtx->caps->useSystemBuffer && myImmediateSceneFbos[0]->IsValid())
429 anImmFbo = myImmediateSceneFbos[0].operator->();
432 #if !defined(GL_ES_VERSION_2_0)
434 && aFrameBuffer == NULL)
436 aCtx->SetReadDrawBuffer (GL_BACK);
439 redraw (aProjectType, aMainFbo != NULL ? aMainFbo : aFrameBuffer);
440 myBackBufferRestored = Standard_True;
441 myIsImmediateDrawn = Standard_False;
442 if (!redrawImmediate (aProjectType, aMainFbo, anImmFbo))
448 && anImmFbo != aFrameBuffer)
450 blitBuffers (anImmFbo, aFrameBuffer, myToFlipOutput);
454 #if defined(_WIN32) && defined(HAVE_VIDEOCAPTURE)
455 if (OpenGl_AVIWriter_AllowWriting (myWindow->PlatformWindow()->NativeHandle()))
458 glGetIntegerv (GL_VIEWPORT, params);
459 int nWidth = params[2] & ~0x7;
460 int nHeight = params[3] & ~0x7;
462 const int nBitsPerPixel = 24;
463 GLubyte* aDumpData = new GLubyte[nWidth * nHeight * nBitsPerPixel / 8];
465 glPixelStorei (GL_PACK_ALIGNMENT, 1);
466 glReadPixels (0, 0, nWidth, nHeight, GL_BGR_EXT, GL_UNSIGNED_BYTE, aDumpData);
467 OpenGl_AVIWriter_AVIWriter (aDumpData, nWidth, nHeight, nBitsPerPixel);
472 if (myRenderParams.Method == Graphic3d_RM_RAYTRACING
473 && myRenderParams.IsGlobalIlluminationEnabled)
485 if (!myMainSceneFbos[0]->IsValid())
487 myBackBufferRestored = Standard_False;
492 aCtx->core11fwd->glFlush();
495 // reset render mode state
498 myWasRedrawnGL = Standard_True;
501 // =======================================================================
502 // function : RedrawImmediate
504 // =======================================================================
505 void OpenGl_View::RedrawImmediate()
507 if (!myWorkspace->Activate())
510 Handle(OpenGl_Context) aCtx = myWorkspace->GetGlContext();
511 if (!myTransientDrawToFront
512 || !myBackBufferRestored
513 || (aCtx->caps->buffersNoSwap && !myMainSceneFbos[0]->IsValid()))
519 const Graphic3d_StereoMode aStereoMode = myRenderParams.StereoMode;
520 Graphic3d_Camera::Projection aProjectType = myCamera->ProjectionType();
521 OpenGl_FrameBuffer* aFrameBuffer = (OpenGl_FrameBuffer* )myFBO;
523 if ( aFrameBuffer == NULL
524 && !aCtx->DefaultFrameBuffer().IsNull()
525 && aCtx->DefaultFrameBuffer()->IsValid())
527 aFrameBuffer = aCtx->DefaultFrameBuffer().operator->();
530 if (aProjectType == Graphic3d_Camera::Projection_Stereo)
532 if (myMainSceneFbos[0]->IsValid()
533 && !myMainSceneFbos[1]->IsValid())
535 aProjectType = Graphic3d_Camera::Projection_Perspective;
540 if (aProjectType == Graphic3d_Camera::Projection_Stereo)
542 OpenGl_FrameBuffer* aMainFbos[2] =
544 myMainSceneFbos[0]->IsValid() ? myMainSceneFbos[0].operator->() : NULL,
545 myMainSceneFbos[1]->IsValid() ? myMainSceneFbos[1].operator->() : NULL
547 OpenGl_FrameBuffer* anImmFbos[2] =
549 myImmediateSceneFbos[0]->IsValid() ? myImmediateSceneFbos[0].operator->() : NULL,
550 myImmediateSceneFbos[1]->IsValid() ? myImmediateSceneFbos[1].operator->() : NULL
552 if (aStereoMode == Graphic3d_StereoMode_SoftPageFlip
553 || aStereoMode == Graphic3d_StereoMode_QuadBuffer)
559 if (aCtx->arbFBO != NULL)
561 aCtx->arbFBO->glBindFramebuffer (GL_FRAMEBUFFER, OpenGl_FrameBuffer::NO_FRAMEBUFFER);
563 #if !defined(GL_ES_VERSION_2_0)
564 if (anImmFbos[0] == NULL)
566 aCtx->SetReadDrawBuffer (aStereoMode == Graphic3d_StereoMode_QuadBuffer ? GL_BACK_LEFT : GL_BACK);
569 toSwap = redrawImmediate (Graphic3d_Camera::Projection_MonoLeftEye,
572 Standard_True) || toSwap;
573 if (aStereoMode == Graphic3d_StereoMode_SoftPageFlip
575 && !aCtx->caps->buffersNoSwap)
580 if (aCtx->arbFBO != NULL)
582 aCtx->arbFBO->glBindFramebuffer (GL_FRAMEBUFFER, OpenGl_FrameBuffer::NO_FRAMEBUFFER);
584 #if !defined(GL_ES_VERSION_2_0)
585 if (anImmFbos[1] == NULL)
587 aCtx->SetReadDrawBuffer (aStereoMode == Graphic3d_StereoMode_QuadBuffer ? GL_BACK_RIGHT : GL_BACK);
590 toSwap = redrawImmediate (Graphic3d_Camera::Projection_MonoRightEye,
593 Standard_True) || toSwap;
594 if (anImmFbos[0] != NULL)
596 drawStereoPair (aFrameBuffer);
601 OpenGl_FrameBuffer* aMainFbo = myMainSceneFbos[0]->IsValid() ? myMainSceneFbos[0].operator->() : NULL;
602 OpenGl_FrameBuffer* anImmFbo = aFrameBuffer;
603 if (!aCtx->caps->useSystemBuffer && myImmediateSceneFbos[0]->IsValid())
605 anImmFbo = myImmediateSceneFbos[0].operator->();
607 #if !defined(GL_ES_VERSION_2_0)
608 if (aMainFbo == NULL)
610 aCtx->SetReadDrawBuffer (GL_BACK);
613 toSwap = redrawImmediate (aProjectType,
616 Standard_True) || toSwap;
618 && anImmFbo != aFrameBuffer)
620 blitBuffers (anImmFbo, aFrameBuffer, myToFlipOutput);
627 if (toSwap && !aCtx->caps->buffersNoSwap)
633 aCtx->core11fwd->glFlush();
636 myWasRedrawnGL = Standard_True;
639 // =======================================================================
642 // =======================================================================
643 void OpenGl_View::redraw (const Graphic3d_Camera::Projection theProjection, OpenGl_FrameBuffer* theReadDrawFbo)
645 Handle(OpenGl_Context) aCtx = myWorkspace->GetGlContext();
646 if (theReadDrawFbo != NULL)
648 theReadDrawFbo->BindBuffer (aCtx);
649 theReadDrawFbo->SetupViewport (aCtx);
653 aCtx->core11fwd->glViewport (0, 0, myWindow->Width(), myWindow->Height());
656 // request reset of material
657 myWorkspace->NamedStatus |= OPENGL_NS_RESMAT;
658 myWorkspace->UseZBuffer() = Standard_True;
659 myWorkspace->UseDepthWrite() = Standard_True;
660 GLbitfield toClear = GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT;
661 glDepthFunc (GL_LEQUAL);
662 glDepthMask (GL_TRUE);
663 glEnable (GL_DEPTH_TEST);
665 #if !defined(GL_ES_VERSION_2_0)
668 glClearDepthf (1.0f);
671 if (myWorkspace->NamedStatus & OPENGL_NS_WHITEBACK)
673 // set background to white
674 glClearColor (1.0f, 1.0f, 1.0f, 1.0f);
678 glClearColor (myBgColor.rgb[0], myBgColor.rgb[1], myBgColor.rgb[2], 0.0f);
683 render (theProjection, theReadDrawFbo, Standard_False);
686 // =======================================================================
687 // function : redrawMonoImmediate
689 // =======================================================================
690 bool OpenGl_View::redrawImmediate (const Graphic3d_Camera::Projection theProjection,
691 OpenGl_FrameBuffer* theReadFbo,
692 OpenGl_FrameBuffer* theDrawFbo,
693 const Standard_Boolean theIsPartialUpdate)
695 Handle(OpenGl_Context) aCtx = myWorkspace->GetGlContext();
696 GLboolean toCopyBackToFront = GL_FALSE;
697 if (!myTransientDrawToFront)
699 myBackBufferRestored = Standard_False;
701 else if (theReadFbo != NULL
702 && theReadFbo->IsValid()
705 if (!blitBuffers (theReadFbo, theDrawFbo))
710 else if (theDrawFbo == NULL)
712 #if !defined(GL_ES_VERSION_2_0)
713 aCtx->core11fwd->glGetBooleanv (GL_DOUBLEBUFFER, &toCopyBackToFront);
715 if (toCopyBackToFront)
717 if (!HasImmediateStructures()
718 && !theIsPartialUpdate)
720 // prefer Swap Buffers within Redraw in compatibility mode (without FBO)
727 myBackBufferRestored = Standard_False;
732 myBackBufferRestored = Standard_False;
734 myIsImmediateDrawn = Standard_True;
736 myWorkspace->UseZBuffer() = Standard_True;
737 myWorkspace->UseDepthWrite() = Standard_True;
738 glDepthFunc (GL_LEQUAL);
739 glDepthMask (GL_TRUE);
740 glEnable (GL_DEPTH_TEST);
741 #if !defined(GL_ES_VERSION_2_0)
744 glClearDepthf (1.0f);
747 render (theProjection, theDrawFbo, Standard_True);
749 return !toCopyBackToFront;
752 //=======================================================================
755 //=======================================================================
756 void OpenGl_View::render (Graphic3d_Camera::Projection theProjection,
757 OpenGl_FrameBuffer* theOutputFBO,
758 const Standard_Boolean theToDrawImmediate)
760 // ==================================
761 // Step 1: Prepare for render
762 // ==================================
764 const Handle(OpenGl_Context)& aContext = myWorkspace->GetGlContext();
766 #if !defined(GL_ES_VERSION_2_0)
767 // Disable current clipping planes
768 if (aContext->core11 != NULL)
770 const Standard_Integer aMaxPlanes = aContext->MaxClipPlanes();
771 for (Standard_Integer aClipPlaneId = GL_CLIP_PLANE0; aClipPlaneId < GL_CLIP_PLANE0 + aMaxPlanes; ++aClipPlaneId)
773 aContext->core11fwd->glDisable (aClipPlaneId);
778 // Update states of OpenGl_BVHTreeSelector (frustum culling algorithm).
779 myBVHSelector.SetViewVolume (myCamera);
781 const Handle(OpenGl_ShaderManager)& aManager = aContext->ShaderManager();
782 if (StateInfo (myCurrLightSourceState, aManager->LightSourceState().Index()) != myLastLightSourceState)
784 aManager->UpdateLightSourceStateTo (myShadingModel == Graphic3d_TOSM_NONE ? &OpenGl_NoShadingLight() : &myLights);
785 myLastLightSourceState = StateInfo (myCurrLightSourceState, aManager->LightSourceState().Index());
788 // Update matrices if camera has changed.
789 Graphic3d_WorldViewProjState aWVPState = myCamera->WorldViewProjState();
790 const Standard_Boolean isCameraChanged = myWorldViewProjState != aWVPState;
791 const Standard_Boolean isSameView = aManager->IsSameView (this);
794 aContext->ProjectionState.SetCurrent (myCamera->ProjectionMatrixF());
795 aContext->WorldViewState .SetCurrent (myCamera->OrientationMatrixF());
799 // Apply new matrix state if camera has changed or this view differs from the one
800 // that was previously used for configuring matrices of shader manager
801 // (ApplyProjectionMatrix and ApplyWorldViewMatrix will affect the manager).
802 if (isCameraChanged || !isSameView)
804 aContext->ApplyProjectionMatrix();
805 aContext->ApplyWorldViewMatrix();
808 if (aManager->ModelWorldState().Index() == 0)
810 aContext->ShaderManager()->UpdateModelWorldStateTo (OpenGl_Mat4());
813 myWorldViewProjState = aWVPState;
815 // ====================================
816 // Step 2: Redraw background
817 // ====================================
820 if (!theToDrawImmediate)
822 drawBackground (myWorkspace);
825 #if !defined(GL_ES_VERSION_2_0)
826 // Switch off lighting by default
827 if (aContext->core11 != NULL)
829 glDisable(GL_LIGHTING);
833 // =================================
834 // Step 3: Redraw main plane
835 // =================================
837 // Setup face culling
838 GLboolean isCullFace = GL_FALSE;
839 if (myBackfacing != Graphic3d_TOBM_AUTOMATIC)
841 isCullFace = glIsEnabled (GL_CULL_FACE);
842 if (myBackfacing == Graphic3d_TOBM_DISABLE)
844 glEnable (GL_CULL_FACE);
845 glCullFace (GL_BACK);
848 glDisable (GL_CULL_FACE);
851 #if !defined(GL_ES_VERSION_2_0)
852 // if the view is scaled normal vectors are scaled to unit
853 // length for correct displaying of shaded objects
854 const gp_Pnt anAxialScale = myCamera->AxialScale();
855 if (anAxialScale.X() != 1.F ||
856 anAxialScale.Y() != 1.F ||
857 anAxialScale.Z() != 1.F)
859 aContext->SetGlNormalizeEnabled (Standard_True);
863 aContext->SetGlNormalizeEnabled (Standard_False);
868 && aContext->core11 != NULL)
870 Standard_Real aFogFrontConverted = (Standard_Real )myFog.Front + myCamera->Distance();
871 if (myCamera->ZFar() < aFogFrontConverted)
873 aFogFrontConverted = myCamera->ZFar();
874 myFog.Front = (Standard_ShortReal )(aFogFrontConverted - myCamera->Distance());
877 Standard_Real aFogBackConverted = (Standard_Real )myFog.Back + myCamera->Distance();
878 if (myCamera->ZFar() < aFogFrontConverted)
880 aFogBackConverted = myCamera->ZFar();
881 myFog.Back = (Standard_ShortReal )(aFogBackConverted - myCamera->Distance());
884 if (aFogFrontConverted > aFogBackConverted)
886 myFog.Front = (Standard_ShortReal )(aFogFrontConverted - myCamera->Distance());
887 myFog.Back = (Standard_ShortReal )(aFogBackConverted - myCamera->Distance());
890 glFogi(GL_FOG_MODE, GL_LINEAR);
891 glFogf(GL_FOG_START, (Standard_ShortReal )aFogFrontConverted);
892 glFogf(GL_FOG_END, (Standard_ShortReal )aFogBackConverted);
893 glFogfv(GL_FOG_COLOR, myFog.Color.rgb);
896 else if (aContext->core11 != NULL)
901 // Apply InteriorShadingMethod
902 if (aContext->core11 != NULL)
904 aContext->core11->glShadeModel (myShadingModel == Graphic3d_TOSM_FACET
905 || myShadingModel == Graphic3d_TOSM_NONE ? GL_FLAT : GL_SMOOTH);
909 aManager->SetShadingModel (myShadingModel);
911 // Apply AntiAliasing
913 myWorkspace->NamedStatus |= OPENGL_NS_ANTIALIASING;
915 myWorkspace->NamedStatus &= ~OPENGL_NS_ANTIALIASING;
917 if (!aManager->IsEmpty())
919 aManager->UpdateClippingState();
923 if (theProjection == Graphic3d_Camera::Projection_MonoLeftEye)
925 aContext->ProjectionState.SetCurrent (myCamera->ProjectionStereoLeftF());
926 aContext->ApplyProjectionMatrix();
928 else if (theProjection == Graphic3d_Camera::Projection_MonoRightEye)
930 aContext->ProjectionState.SetCurrent (myCamera->ProjectionStereoRightF());
931 aContext->ApplyProjectionMatrix();
933 renderScene (theProjection, theOutputFBO, theToDrawImmediate);
935 // ===============================
937 // ===============================
939 // Resetting GL parameters according to the default aspects
940 // in order to synchronize GL state with the graphic driver state
941 // before drawing auxiliary stuff (trihedrons, overlayer)
942 myWorkspace->ResetAppliedAspect();
944 aContext->ChangeClipping().RemoveAll (aContext);
946 if (!aManager->IsEmpty())
948 aManager->ResetMaterialStates();
949 aManager->RevertClippingState();
951 // We need to disable (unbind) all shaders programs to ensure
952 // that all objects without specified aspect will be drawn
953 // correctly (such as background)
954 aContext->BindProgram (NULL);
958 if (!theToDrawImmediate)
960 renderTrihedron (myWorkspace);
962 // Restore face culling
963 if (myBackfacing != Graphic3d_TOBM_AUTOMATIC)
967 glEnable (GL_CULL_FACE);
968 glCullFace (GL_BACK);
971 glDisable (GL_CULL_FACE);
975 // ==============================================================
976 // Step 6: Keep shader manager informed about last View
977 // ==============================================================
979 if (!aManager.IsNull())
981 aManager->SetLastView (this);
985 // =======================================================================
986 // function : InvalidateBVHData
988 // =======================================================================
989 void OpenGl_View::InvalidateBVHData (const Graphic3d_ZLayerId theLayerId)
991 myZLayers.InvalidateBVHData (theLayerId);
994 //=======================================================================
995 //function : renderStructs
997 //=======================================================================
998 void OpenGl_View::renderStructs (Graphic3d_Camera::Projection theProjection,
999 OpenGl_FrameBuffer* theReadDrawFbo,
1000 const Standard_Boolean theToDrawImmediate)
1002 if ( myZLayers.NbStructures() <= 0 )
1005 Handle(OpenGl_Context) aCtx = myWorkspace->GetGlContext();
1006 if ( (myWorkspace->NamedStatus & OPENGL_NS_2NDPASSNEED) == 0 )
1008 #if !defined(GL_ES_VERSION_2_0)
1009 const int anAntiAliasingMode = myWorkspace->AntiAliasingMode();
1012 if ( !myAntiAliasing )
1014 #if !defined(GL_ES_VERSION_2_0)
1015 if (aCtx->core11 != NULL)
1017 glDisable (GL_POINT_SMOOTH);
1019 glDisable(GL_LINE_SMOOTH);
1020 if( anAntiAliasingMode & 2 ) glDisable(GL_POLYGON_SMOOTH);
1022 glBlendFunc (GL_ONE, GL_ZERO);
1023 glDisable (GL_BLEND);
1027 #if !defined(GL_ES_VERSION_2_0)
1028 if (aCtx->core11 != NULL)
1030 glEnable(GL_POINT_SMOOTH);
1032 glEnable(GL_LINE_SMOOTH);
1033 if( anAntiAliasingMode & 2 ) glEnable(GL_POLYGON_SMOOTH);
1035 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1036 glEnable (GL_BLEND);
1040 Standard_Boolean toRenderGL = theToDrawImmediate ||
1041 myRenderParams.Method != Graphic3d_RM_RAYTRACING ||
1042 myRaytraceInitStatus == OpenGl_RT_FAIL ||
1047 toRenderGL = !initRaytraceResources (aCtx) ||
1048 !updateRaytraceGeometry (OpenGl_GUM_CHECK, myId, aCtx);
1050 toRenderGL |= !myIsRaytraceDataValid; // if no ray-trace data use OpenGL
1054 const Standard_Integer aSizeX = theReadDrawFbo != NULL ? theReadDrawFbo->GetVPSizeX() : myWindow->Width();
1055 const Standard_Integer aSizeY = theReadDrawFbo != NULL ? theReadDrawFbo->GetVPSizeY() : myWindow->Height();
1056 myOpenGlFBO ->InitLazy (aCtx, aSizeX, aSizeY, myFboColorFormat, myFboDepthFormat, 0);
1058 if (myRaytraceFilter.IsNull())
1059 myRaytraceFilter = new OpenGl_RaytraceFilter;
1061 myRaytraceFilter->SetPrevRenderFilter (myWorkspace->GetRenderFilter());
1063 if (theReadDrawFbo != NULL)
1064 theReadDrawFbo->UnbindBuffer (aCtx);
1066 // Prepare preliminary OpenGL output
1067 if (aCtx->arbFBOBlit != NULL)
1069 // Render bottom OSD layer
1070 myZLayers.Render (myWorkspace, theToDrawImmediate, OpenGl_LF_Bottom);
1072 myWorkspace->SetRenderFilter (myRaytraceFilter);
1074 if (theReadDrawFbo != NULL)
1076 theReadDrawFbo->BindReadBuffer (aCtx);
1080 aCtx->arbFBO->glBindFramebuffer (GL_READ_FRAMEBUFFER, 0);
1083 myOpenGlFBO->BindDrawBuffer (aCtx);
1085 aCtx->arbFBOBlit->glBlitFramebuffer (0, 0, aSizeX, aSizeY,
1086 0, 0, aSizeX, aSizeY,
1087 GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT,
1090 // Render non-polygonal elements in default layer
1091 myZLayers.Render (myWorkspace, theToDrawImmediate, OpenGl_LF_Default);
1093 myWorkspace->SetRenderFilter (myRaytraceFilter->PrevRenderFilter());
1096 if (theReadDrawFbo != NULL)
1098 theReadDrawFbo->BindBuffer (aCtx);
1102 aCtx->arbFBO->glBindFramebuffer (GL_FRAMEBUFFER, 0);
1105 // Ray-tracing polygonal primitive arrays
1106 raytrace (aSizeX, aSizeY, theProjection, theReadDrawFbo, aCtx);
1108 // Render upper (top and topmost) OpenGL layers
1109 myZLayers.Render (myWorkspace, theToDrawImmediate, OpenGl_LF_Upper);
1113 // Redraw 3D scene using OpenGL in standard
1114 // mode or in case of ray-tracing failure
1117 myZLayers.Render (myWorkspace, theToDrawImmediate, OpenGl_LF_All);
1119 // Set flag that scene was redrawn by standard pipeline
1120 myWasRedrawnGL = Standard_True;
1124 //=======================================================================
1125 //function : renderTrihedron
1127 //=======================================================================
1128 void OpenGl_View::renderTrihedron (const Handle(OpenGl_Workspace) &theWorkspace)
1130 // display global trihedron
1131 if (myToShowTrihedron)
1133 myTrihedron.Render (theWorkspace);
1135 if (myToShowGradTrihedron)
1137 myGraduatedTrihedron.Render (theWorkspace);
1141 // =======================================================================
1142 // function : Invalidate
1144 // =======================================================================
1145 void OpenGl_View::Invalidate()
1147 myBackBufferRestored = Standard_False;
1150 //=======================================================================
1151 //function : renderScene
1153 //=======================================================================
1154 void OpenGl_View::renderScene (Graphic3d_Camera::Projection theProjection,
1155 OpenGl_FrameBuffer* theReadDrawFbo,
1156 const Standard_Boolean theToDrawImmediate)
1158 const Handle(OpenGl_Context)& aContext = myWorkspace->GetGlContext();
1160 if (myZClip.Back.IsOn || myZClip.Front.IsOn)
1162 Handle(Graphic3d_ClipPlane) aPlaneBack;
1163 Handle(Graphic3d_ClipPlane) aPlaneFront;
1165 if (myZClip.Back.IsOn)
1167 Standard_Real aClipBackConverted = (Standard_Real )myZClip.Front.Limit + myCamera->Distance();
1168 if (myCamera->ZFar() < aClipBackConverted)
1170 aClipBackConverted = myCamera->ZFar();
1171 myZClip.Back.Limit = (Standard_ShortReal )(aClipBackConverted - myCamera->Distance());
1173 const Graphic3d_ClipPlane::Equation aBackEquation (0.0, 0.0, 1.0, (Standard_ShortReal )aClipBackConverted);
1174 aPlaneBack = new Graphic3d_ClipPlane (aBackEquation);
1177 if (myZClip.Front.IsOn)
1179 Standard_Real aClipFrontConverted = (Standard_Real )myZClip.Front.Limit + myCamera->Distance();
1180 if (myCamera->ZNear() > aClipFrontConverted)
1182 aClipFrontConverted = myCamera->ZNear();
1183 myZClip.Front.Limit = (Standard_ShortReal )(aClipFrontConverted - myCamera->Distance());
1185 const Graphic3d_ClipPlane::Equation aFrontEquation (0.0, 0.0, -1.0, (Standard_ShortReal )-aClipFrontConverted);
1186 aPlaneFront = new Graphic3d_ClipPlane (aFrontEquation);
1189 // Specify slicing planes with identity transformation
1190 if (!aPlaneBack.IsNull() || !aPlaneFront.IsNull())
1192 Graphic3d_SequenceOfHClipPlane aSlicingPlanes;
1193 if (!aPlaneBack.IsNull())
1195 aSlicingPlanes.Append (aPlaneBack);
1198 if (!aPlaneFront.IsNull())
1200 aSlicingPlanes.Append (aPlaneFront);
1203 // add planes at loaded view matrix state
1204 aContext->ChangeClipping().AddView (aContext, aSlicingPlanes);
1209 // set printing scale/tiling transformation
1210 Handle(OpenGl_PrinterContext) aPrintContext = myWorkspace->PrinterContext();
1211 if (!aPrintContext.IsNull())
1213 aContext->ProjectionState.Push();
1214 aContext->ProjectionState.SetCurrent (aPrintContext->ProjTransformation() * aContext->ProjectionState.Current());
1215 aContext->ApplyProjectionMatrix();
1219 // Specify clipping planes in view transformation space
1220 if (!myClipPlanes.IsEmpty())
1222 Graphic3d_SequenceOfHClipPlane aUserPlanes;
1223 Graphic3d_SequenceOfHClipPlane::Iterator aClippingIt (myClipPlanes);
1224 for (; aClippingIt.More(); aClippingIt.Next())
1226 const Handle(Graphic3d_ClipPlane)& aClipPlane = aClippingIt.Value();
1227 if (aClipPlane->IsOn())
1229 aUserPlanes.Append (aClipPlane);
1233 if (!aUserPlanes.IsEmpty())
1235 aContext->ChangeClipping().AddWorldLazy (aContext, aUserPlanes);
1238 if (!aContext->ShaderManager()->IsEmpty())
1240 aContext->ShaderManager()->UpdateClippingState();
1244 #if !defined(GL_ES_VERSION_2_0)
1246 if (aContext->core11 != NULL)
1249 Graphic3d_Vec4 anAmbientColor (THE_DEFAULT_AMBIENT[0],
1250 THE_DEFAULT_AMBIENT[1],
1251 THE_DEFAULT_AMBIENT[2],
1252 THE_DEFAULT_AMBIENT[3]);
1253 GLenum aLightGlId = GL_LIGHT0;
1255 OpenGl_ListOfLight::Iterator aLightIt (myShadingModel == Graphic3d_TOSM_NONE ? OpenGl_NoShadingLight() : myLights);
1256 for (; aLightIt.More(); aLightIt.Next())
1258 bindLight (aLightIt.Value(), aLightGlId, anAmbientColor, myWorkspace);
1261 // apply accumulated ambient color
1262 anAmbientColor.a() = 1.0f;
1263 glLightModelfv (GL_LIGHT_MODEL_AMBIENT, anAmbientColor.GetData());
1265 if (aLightGlId != GL_LIGHT0)
1267 glEnable (GL_LIGHTING);
1269 // switch off unused lights
1270 for (; aLightGlId <= GL_LIGHT7; ++aLightGlId)
1272 glDisable (aLightGlId);
1277 // Clear status bitfields
1278 myWorkspace->NamedStatus &= ~(OPENGL_NS_2NDPASSNEED | OPENGL_NS_2NDPASSDO);
1280 // Update state of surface detail level
1281 myWorkspace->GetGlContext()->ShaderManager()->UpdateSurfaceDetailStateTo (mySurfaceDetail);
1283 // Added PCT for handling of textures
1284 switch (mySurfaceDetail)
1286 case Graphic3d_TOD_NONE:
1287 myWorkspace->NamedStatus |= OPENGL_NS_FORBIDSETTEX;
1288 myWorkspace->DisableTexture();
1290 renderStructs (theProjection, theReadDrawFbo, theToDrawImmediate);
1293 case Graphic3d_TOD_ENVIRONMENT:
1294 myWorkspace->NamedStatus |= OPENGL_NS_FORBIDSETTEX;
1295 if (myRenderParams.Method != Graphic3d_RM_RAYTRACING)
1297 myWorkspace->EnableTexture (myTextureEnv);
1300 renderStructs (theProjection, theReadDrawFbo, theToDrawImmediate);
1301 myWorkspace->DisableTexture();
1304 case Graphic3d_TOD_ALL:
1306 myWorkspace->NamedStatus &= ~OPENGL_NS_FORBIDSETTEX;
1308 renderStructs (theProjection, theReadDrawFbo, theToDrawImmediate);
1309 myWorkspace->DisableTexture();
1312 if (myWorkspace->NamedStatus & OPENGL_NS_2NDPASSNEED)
1314 myWorkspace->NamedStatus |= OPENGL_NS_2NDPASSDO;
1315 if (myRenderParams.Method != Graphic3d_RM_RAYTRACING)
1317 myWorkspace->EnableTexture (myTextureEnv);
1320 // Remember OpenGl properties
1321 GLint aSaveBlendDst = GL_ONE_MINUS_SRC_ALPHA, aSaveBlendSrc = GL_SRC_ALPHA;
1322 GLint aSaveZbuffFunc;
1323 GLboolean aSaveZbuffWrite;
1324 glGetBooleanv (GL_DEPTH_WRITEMASK, &aSaveZbuffWrite);
1325 glGetIntegerv (GL_DEPTH_FUNC, &aSaveZbuffFunc);
1326 #if !defined(GL_ES_VERSION_2_0)
1327 glGetIntegerv (GL_BLEND_DST, &aSaveBlendDst);
1328 glGetIntegerv (GL_BLEND_SRC, &aSaveBlendSrc);
1330 GLboolean wasZbuffEnabled = glIsEnabled (GL_DEPTH_TEST);
1331 GLboolean wasBlendEnabled = glIsEnabled (GL_BLEND);
1333 // Change the properties for second rendering pass
1334 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1335 glEnable (GL_BLEND);
1337 glDepthFunc (GL_EQUAL);
1338 glDepthMask (GL_FALSE);
1339 glEnable (GL_DEPTH_TEST);
1341 myWorkspace->NamedStatus |= OPENGL_NS_FORBIDSETTEX;
1344 renderStructs (theProjection, theReadDrawFbo, theToDrawImmediate);
1345 myWorkspace->DisableTexture();
1347 // Restore properties back
1348 glBlendFunc (aSaveBlendSrc, aSaveBlendDst);
1349 if (!wasBlendEnabled)
1350 glDisable (GL_BLEND);
1352 glDepthFunc (aSaveZbuffFunc);
1353 glDepthMask (aSaveZbuffWrite);
1354 if (!wasZbuffEnabled)
1355 glDisable (GL_DEPTH_FUNC);
1360 // Apply restored view matrix.
1361 aContext->ApplyWorldViewMatrix();
1364 // set printing scale/tiling transformation
1365 if (!aPrintContext.IsNull())
1367 aContext->ProjectionState.Pop();
1368 aContext->ApplyProjectionMatrix();
1373 // =======================================================================
1374 // function : bindDefaultFbo
1376 // =======================================================================
1377 void OpenGl_View::bindDefaultFbo (OpenGl_FrameBuffer* theCustomFbo)
1379 Handle(OpenGl_Context) aCtx = myWorkspace->GetGlContext();
1380 OpenGl_FrameBuffer* anFbo = (theCustomFbo != NULL && theCustomFbo->IsValid())
1382 : (!aCtx->DefaultFrameBuffer().IsNull()
1383 && aCtx->DefaultFrameBuffer()->IsValid()
1384 ? aCtx->DefaultFrameBuffer().operator->()
1388 anFbo->BindBuffer (aCtx);
1392 #if !defined(GL_ES_VERSION_2_0)
1393 aCtx->SetReadDrawBuffer (GL_BACK);
1395 if (aCtx->arbFBO != NULL)
1397 aCtx->arbFBO->glBindFramebuffer (GL_FRAMEBUFFER, OpenGl_FrameBuffer::NO_FRAMEBUFFER);
1401 aCtx->core11fwd->glViewport (0, 0, myWindow->Width(), myWindow->Height());
1404 // =======================================================================
1405 // function : initBlitQuad
1407 // =======================================================================
1408 OpenGl_VertexBuffer* OpenGl_View::initBlitQuad (const Standard_Boolean theToFlip)
1410 OpenGl_VertexBuffer* aVerts = NULL;
1413 aVerts = &myFullScreenQuad;
1414 if (!aVerts->IsValid())
1416 OpenGl_Vec4 aQuad[4] =
1418 OpenGl_Vec4( 1.0f, -1.0f, 1.0f, 0.0f),
1419 OpenGl_Vec4( 1.0f, 1.0f, 1.0f, 1.0f),
1420 OpenGl_Vec4(-1.0f, -1.0f, 0.0f, 0.0f),
1421 OpenGl_Vec4(-1.0f, 1.0f, 0.0f, 1.0f)
1423 aVerts->Init (myWorkspace->GetGlContext(), 4, 4, aQuad[0].GetData());
1428 aVerts = &myFullScreenQuadFlip;
1429 if (!aVerts->IsValid())
1431 OpenGl_Vec4 aQuad[4] =
1433 OpenGl_Vec4( 1.0f, -1.0f, 1.0f, 1.0f),
1434 OpenGl_Vec4( 1.0f, 1.0f, 1.0f, 0.0f),
1435 OpenGl_Vec4(-1.0f, -1.0f, 0.0f, 1.0f),
1436 OpenGl_Vec4(-1.0f, 1.0f, 0.0f, 0.0f)
1438 aVerts->Init (myWorkspace->GetGlContext(), 4, 4, aQuad[0].GetData());
1444 // =======================================================================
1445 // function : blitBuffers
1447 // =======================================================================
1448 bool OpenGl_View::blitBuffers (OpenGl_FrameBuffer* theReadFbo,
1449 OpenGl_FrameBuffer* theDrawFbo,
1450 const Standard_Boolean theToFlip)
1452 Handle(OpenGl_Context) aCtx = myWorkspace->GetGlContext();
1453 if (theReadFbo == NULL || aCtx->IsFeedback())
1457 else if (theReadFbo == theDrawFbo)
1462 // clear destination before blitting
1463 if (theDrawFbo != NULL
1464 && theDrawFbo->IsValid())
1466 theDrawFbo->BindBuffer (aCtx);
1470 aCtx->arbFBO->glBindFramebuffer (GL_FRAMEBUFFER, OpenGl_FrameBuffer::NO_FRAMEBUFFER);
1472 #if !defined(GL_ES_VERSION_2_0)
1473 aCtx->core20fwd->glClearDepth (1.0);
1475 aCtx->core20fwd->glClearDepthf (1.0f);
1477 aCtx->core20fwd->glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
1479 #if !defined(GL_ES_VERSION_2_0)
1480 if (aCtx->arbFBOBlit != NULL
1481 && theReadFbo->NbSamples() != 0)
1483 GLbitfield aCopyMask = 0;
1484 theReadFbo->BindReadBuffer (aCtx);
1485 if (theDrawFbo != NULL
1486 && theDrawFbo->IsValid())
1488 theDrawFbo->BindDrawBuffer (aCtx);
1489 if (theDrawFbo->HasColor()
1490 && theReadFbo->HasColor())
1492 aCopyMask |= GL_COLOR_BUFFER_BIT;
1494 if (theDrawFbo->HasDepth()
1495 && theReadFbo->HasDepth())
1497 aCopyMask |= GL_DEPTH_BUFFER_BIT;
1502 if (theReadFbo->HasColor())
1504 aCopyMask |= GL_COLOR_BUFFER_BIT;
1506 if (theReadFbo->HasDepth())
1508 aCopyMask |= GL_DEPTH_BUFFER_BIT;
1510 aCtx->arbFBO->glBindFramebuffer (GL_DRAW_FRAMEBUFFER, OpenGl_FrameBuffer::NO_FRAMEBUFFER);
1513 // we don't copy stencil buffer here... does it matter for performance?
1514 aCtx->arbFBOBlit->glBlitFramebuffer (0, 0, theReadFbo->GetVPSizeX(), theReadFbo->GetVPSizeY(),
1515 0, 0, theReadFbo->GetVPSizeX(), theReadFbo->GetVPSizeY(),
1516 aCopyMask, GL_NEAREST);
1517 if (theDrawFbo != NULL
1518 && theDrawFbo->IsValid())
1520 theDrawFbo->BindBuffer (aCtx);
1524 aCtx->arbFBO->glBindFramebuffer (GL_FRAMEBUFFER, OpenGl_FrameBuffer::NO_FRAMEBUFFER);
1530 aCtx->core20fwd->glDepthFunc (GL_ALWAYS);
1531 aCtx->core20fwd->glDepthMask (GL_TRUE);
1532 aCtx->core20fwd->glEnable (GL_DEPTH_TEST);
1534 myWorkspace->DisableTexture();
1536 OpenGl_VertexBuffer* aVerts = initBlitQuad (theToFlip);
1537 const Handle(OpenGl_ShaderManager)& aManager = aCtx->ShaderManager();
1538 if (aVerts->IsValid()
1539 && aManager->BindFboBlitProgram())
1541 theReadFbo->ColorTexture() ->Bind (aCtx, GL_TEXTURE0 + 0);
1542 theReadFbo->DepthStencilTexture()->Bind (aCtx, GL_TEXTURE0 + 1);
1543 aVerts->BindVertexAttrib (aCtx, Graphic3d_TOA_POS);
1545 aCtx->core20fwd->glDrawArrays (GL_TRIANGLE_STRIP, 0, 4);
1547 aVerts->UnbindVertexAttrib (aCtx, Graphic3d_TOA_POS);
1548 theReadFbo->DepthStencilTexture()->Unbind (aCtx, GL_TEXTURE0 + 1);
1549 theReadFbo->ColorTexture() ->Unbind (aCtx, GL_TEXTURE0 + 0);
1550 aCtx->BindProgram (NULL);
1554 TCollection_ExtendedString aMsg = TCollection_ExtendedString()
1555 + "Error! FBO blitting has failed";
1556 aCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION,
1557 GL_DEBUG_TYPE_ERROR,
1559 GL_DEBUG_SEVERITY_HIGH,
1561 myHasFboBlit = Standard_False;
1562 theReadFbo->Release (aCtx.operator->());
1569 // =======================================================================
1570 // function : drawStereoPair
1572 // =======================================================================
1573 void OpenGl_View::drawStereoPair (OpenGl_FrameBuffer* theDrawFbo)
1575 const Handle(OpenGl_Context)& aCtx = myWorkspace->GetGlContext();
1576 bindDefaultFbo (theDrawFbo);
1577 OpenGl_FrameBuffer* aPair[2] =
1579 myImmediateSceneFbos[0]->IsValid() ? myImmediateSceneFbos[0].operator->() : NULL,
1580 myImmediateSceneFbos[1]->IsValid() ? myImmediateSceneFbos[1].operator->() : NULL
1582 if (aPair[0] == NULL
1584 || !myTransientDrawToFront)
1586 aPair[0] = myMainSceneFbos[0]->IsValid() ? myMainSceneFbos[0].operator->() : NULL;
1587 aPair[1] = myMainSceneFbos[1]->IsValid() ? myMainSceneFbos[1].operator->() : NULL;
1590 if (aPair[0] == NULL
1591 || aPair[1] == NULL)
1596 if (aPair[0]->NbSamples() != 0)
1598 // resolve MSAA buffers before drawing
1599 if (!myOpenGlFBO ->InitLazy (aCtx, aPair[0]->GetVPSizeX(), aPair[0]->GetVPSizeY(), myFboColorFormat, myFboDepthFormat, 0)
1600 || !myOpenGlFBO2->InitLazy (aCtx, aPair[0]->GetVPSizeX(), aPair[0]->GetVPSizeY(), myFboColorFormat, 0, 0))
1602 aCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION,
1603 GL_DEBUG_TYPE_ERROR,
1605 GL_DEBUG_SEVERITY_HIGH,
1606 "Error! Unable to allocate FBO for blitting stereo pair");
1607 bindDefaultFbo (theDrawFbo);
1611 if (!blitBuffers (aPair[0], myOpenGlFBO .operator->(), Standard_False)
1612 || !blitBuffers (aPair[1], myOpenGlFBO2.operator->(), Standard_False))
1614 bindDefaultFbo (theDrawFbo);
1618 aPair[0] = myOpenGlFBO .operator->();
1619 aPair[1] = myOpenGlFBO2.operator->();
1620 bindDefaultFbo (theDrawFbo);
1625 Standard_Integer left;
1626 Standard_Integer top;
1627 Standard_Integer right;
1628 Standard_Integer bottom;
1629 Standard_Integer dx() { return right - left; }
1630 Standard_Integer dy() { return bottom - top; }
1633 myWindow->PlatformWindow()->Position (aGeom.left, aGeom.top, aGeom.right, aGeom.bottom);
1635 Standard_Boolean toReverse = myRenderParams.ToReverseStereo;
1636 const Standard_Boolean isOddY = (aGeom.top + aGeom.dy()) % 2 == 1;
1637 const Standard_Boolean isOddX = aGeom.left % 2 == 1;
1639 && (myRenderParams.StereoMode == Graphic3d_StereoMode_RowInterlaced
1640 || myRenderParams.StereoMode == Graphic3d_StereoMode_ChessBoard))
1642 toReverse = !toReverse;
1645 && (myRenderParams.StereoMode == Graphic3d_StereoMode_ColumnInterlaced
1646 || myRenderParams.StereoMode == Graphic3d_StereoMode_ChessBoard))
1648 toReverse = !toReverse;
1653 std::swap (aPair[0], aPair[1]);
1656 aCtx->core20fwd->glDepthFunc (GL_ALWAYS);
1657 aCtx->core20fwd->glDepthMask (GL_TRUE);
1658 aCtx->core20fwd->glEnable (GL_DEPTH_TEST);
1660 myWorkspace->DisableTexture();
1661 OpenGl_VertexBuffer* aVerts = initBlitQuad (myToFlipOutput);
1663 const Handle(OpenGl_ShaderManager)& aManager = aCtx->ShaderManager();
1664 if (aVerts->IsValid()
1665 && aManager->BindStereoProgram (myRenderParams.StereoMode))
1667 if (myRenderParams.StereoMode == Graphic3d_StereoMode_Anaglyph)
1669 OpenGl_Mat4 aFilterL, aFilterR;
1670 aFilterL.SetDiagonal (Graphic3d_Vec4 (0.0f, 0.0f, 0.0f, 0.0f));
1671 aFilterR.SetDiagonal (Graphic3d_Vec4 (0.0f, 0.0f, 0.0f, 0.0f));
1672 switch (myRenderParams.AnaglyphFilter)
1674 case Graphic3d_RenderingParams::Anaglyph_RedCyan_Simple:
1676 aFilterL.SetRow (0, Graphic3d_Vec4 (1.0f, 0.0f, 0.0f, 0.0f));
1677 aFilterR.SetRow (1, Graphic3d_Vec4 (0.0f, 1.0f, 0.0f, 0.0f));
1678 aFilterR.SetRow (2, Graphic3d_Vec4 (0.0f, 0.0f, 1.0f, 0.0f));
1681 case Graphic3d_RenderingParams::Anaglyph_RedCyan_Optimized:
1683 aFilterL.SetRow (0, Graphic3d_Vec4 ( 0.4154f, 0.4710f, 0.16666667f, 0.0f));
1684 aFilterL.SetRow (1, Graphic3d_Vec4 (-0.0458f, -0.0484f, -0.0257f, 0.0f));
1685 aFilterL.SetRow (2, Graphic3d_Vec4 (-0.0547f, -0.0615f, 0.0128f, 0.0f));
1686 aFilterL.SetRow (3, Graphic3d_Vec4 ( 0.0f, 0.0f, 0.0f, 0.0f));
1687 aFilterR.SetRow (0, Graphic3d_Vec4 (-0.01090909f, -0.03636364f, -0.00606061f, 0.0f));
1688 aFilterR.SetRow (1, Graphic3d_Vec4 ( 0.37560000f, 0.73333333f, 0.01111111f, 0.0f));
1689 aFilterR.SetRow (2, Graphic3d_Vec4 (-0.06510000f, -0.12870000f, 1.29710000f, 0.0f));
1690 aFilterR.SetRow (3, Graphic3d_Vec4 ( 0.0f, 0.0f, 0.0f, 0.0f));
1693 case Graphic3d_RenderingParams::Anaglyph_YellowBlue_Simple:
1695 aFilterL.SetRow (0, Graphic3d_Vec4 (1.0f, 0.0f, 0.0f, 0.0f));
1696 aFilterL.SetRow (1, Graphic3d_Vec4 (0.0f, 1.0f, 0.0f, 0.0f));
1697 aFilterR.SetRow (2, Graphic3d_Vec4 (0.0f, 0.0f, 1.0f, 0.0f));
1700 case Graphic3d_RenderingParams::Anaglyph_YellowBlue_Optimized:
1702 aFilterL.SetRow (0, Graphic3d_Vec4 ( 1.062f, -0.205f, 0.299f, 0.0f));
1703 aFilterL.SetRow (1, Graphic3d_Vec4 (-0.026f, 0.908f, 0.068f, 0.0f));
1704 aFilterL.SetRow (2, Graphic3d_Vec4 (-0.038f, -0.173f, 0.022f, 0.0f));
1705 aFilterL.SetRow (3, Graphic3d_Vec4 ( 0.0f, 0.0f, 0.0f, 0.0f));
1706 aFilterR.SetRow (0, Graphic3d_Vec4 (-0.016f, -0.123f, -0.017f, 0.0f));
1707 aFilterR.SetRow (1, Graphic3d_Vec4 ( 0.006f, 0.062f, -0.017f, 0.0f));
1708 aFilterR.SetRow (2, Graphic3d_Vec4 ( 0.094f, 0.185f, 0.911f, 0.0f));
1709 aFilterR.SetRow (3, Graphic3d_Vec4 ( 0.0f, 0.0f, 0.0f, 0.0f));
1712 case Graphic3d_RenderingParams::Anaglyph_GreenMagenta_Simple:
1714 aFilterR.SetRow (0, Graphic3d_Vec4 (1.0f, 0.0f, 0.0f, 0.0f));
1715 aFilterL.SetRow (1, Graphic3d_Vec4 (0.0f, 1.0f, 0.0f, 0.0f));
1716 aFilterR.SetRow (2, Graphic3d_Vec4 (0.0f, 0.0f, 1.0f, 0.0f));
1719 case Graphic3d_RenderingParams::Anaglyph_UserDefined:
1721 aFilterL = myRenderParams.AnaglyphLeft;
1722 aFilterR = myRenderParams.AnaglyphRight;
1726 aCtx->ActiveProgram()->SetUniform (aCtx, "uMultL", aFilterL);
1727 aCtx->ActiveProgram()->SetUniform (aCtx, "uMultR", aFilterR);
1730 aPair[0]->ColorTexture()->Bind (aCtx, GL_TEXTURE0 + 0);
1731 aPair[1]->ColorTexture()->Bind (aCtx, GL_TEXTURE0 + 1);
1732 aVerts->BindVertexAttrib (aCtx, 0);
1734 aCtx->core20fwd->glDrawArrays (GL_TRIANGLE_STRIP, 0, 4);
1736 aVerts->UnbindVertexAttrib (aCtx, 0);
1737 aPair[1]->ColorTexture()->Unbind (aCtx, GL_TEXTURE0 + 1);
1738 aPair[0]->ColorTexture()->Unbind (aCtx, GL_TEXTURE0 + 0);
1742 TCollection_ExtendedString aMsg = TCollection_ExtendedString()
1743 + "Error! Anaglyph has failed";
1744 aCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION,
1745 GL_DEBUG_TYPE_ERROR,
1747 GL_DEBUG_SEVERITY_HIGH,
1752 // =======================================================================
1753 // function : copyBackToFront
1755 // =======================================================================
1756 void OpenGl_View::copyBackToFront()
1758 #if !defined(GL_ES_VERSION_2_0)
1760 OpenGl_Mat4 aProjectMat;
1761 Graphic3d_TransformUtils::Ortho2D (aProjectMat,
1762 0.f, static_cast<GLfloat> (myWindow->Width()), 0.f, static_cast<GLfloat> (myWindow->Height()));
1764 Handle(OpenGl_Context) aCtx = myWorkspace->GetGlContext();
1765 aCtx->WorldViewState.Push();
1766 aCtx->ProjectionState.Push();
1768 aCtx->WorldViewState.SetIdentity();
1769 aCtx->ProjectionState.SetCurrent (aProjectMat);
1771 aCtx->ApplyProjectionMatrix();
1772 aCtx->ApplyWorldViewMatrix();
1774 aCtx->DisableFeatures();
1776 switch (aCtx->DrawBuffer())
1780 aCtx->SetReadBuffer (GL_BACK_LEFT);
1781 aCtx->SetDrawBuffer (GL_FRONT_LEFT);
1786 aCtx->SetReadBuffer (GL_BACK_RIGHT);
1787 aCtx->SetDrawBuffer (GL_FRONT_RIGHT);
1792 aCtx->SetReadBuffer (GL_BACK);
1793 aCtx->SetDrawBuffer (GL_FRONT);
1798 glRasterPos2i (0, 0);
1799 glCopyPixels (0, 0, myWindow->Width() + 1, myWindow->Height() + 1, GL_COLOR);
1800 //glCopyPixels (0, 0, myWidth + 1, myHeight + 1, GL_DEPTH);
1802 aCtx->EnableFeatures();
1804 aCtx->WorldViewState.Pop();
1805 aCtx->ProjectionState.Pop();
1806 aCtx->ApplyProjectionMatrix();
1808 // read/write from front buffer now
1809 aCtx->SetReadBuffer (aCtx->DrawBuffer());
1811 myIsImmediateDrawn = Standard_False;