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.
16 #include <OpenGl_GlCore15.hxx>
18 #include <InterfaceGraphic.hxx>
20 #include <OpenGl_AspectLine.hxx>
21 #include <OpenGl_AspectFace.hxx>
22 #include <OpenGl_AspectMarker.hxx>
23 #include <OpenGl_AspectText.hxx>
24 #include <OpenGl_Context.hxx>
25 #include <OpenGl_Element.hxx>
26 #include <OpenGl_FrameBuffer.hxx>
27 #include <OpenGl_Structure.hxx>
28 #include <OpenGl_Sampler.hxx>
29 #include <OpenGl_Texture.hxx>
30 #include <OpenGl_Utils.hxx>
31 #include <OpenGl_View.hxx>
32 #include <OpenGl_Workspace.hxx>
34 #include <Graphic3d_TextureParams.hxx>
36 #if defined(_WIN32) && defined(HAVE_VIDEOCAPTURE)
37 #include <OpenGl_AVIWriter.hxx>
40 IMPLEMENT_STANDARD_HANDLE(OpenGl_Workspace,OpenGl_Window)
41 IMPLEMENT_STANDARD_RTTIEXT(OpenGl_Workspace,OpenGl_Window)
45 static const TEL_COLOUR THE_WHITE_COLOR = { { 1.0f, 1.0f, 1.0f, 1.0f } };
46 static const OpenGl_Vec4 THE_BLACK_COLOR (0.0f, 0.0f, 0.0f, 1.0f);
48 static const OpenGl_AspectLine myDefaultAspectLine;
49 static const OpenGl_AspectFace myDefaultAspectFace;
50 static const OpenGl_AspectMarker myDefaultAspectMarker;
51 static const OpenGl_AspectText myDefaultAspectText;
53 static const OpenGl_TextParam myDefaultTextParam =
55 16, Graphic3d_HTA_LEFT, Graphic3d_VTA_BOTTOM
58 static const OpenGl_Matrix myDefaultMatrix =
60 {{ 1.0F, 0.0F, 0.0F, 0.0F },
61 { 0.0F, 1.0F, 0.0F, 0.0F },
62 { 0.0F, 0.0F, 1.0F, 0.0F },
63 { 0.0F, 0.0F, 0.0F, 1.0F }}
68 // =======================================================================
71 // =======================================================================
72 void OpenGl_Material::Init (const OPENGL_SURF_PROP& theProp)
75 if (theProp.color_mask & OPENGL_AMBIENT_MASK)
77 const float* aSrcAmb = theProp.isphysic ? theProp.ambcol.rgb : theProp.matcol.rgb;
78 Ambient = OpenGl_Vec4 (aSrcAmb[0] * theProp.amb,
79 aSrcAmb[1] * theProp.amb,
80 aSrcAmb[2] * theProp.amb,
85 Ambient = THE_BLACK_COLOR;
88 // diffusion component
89 if (theProp.color_mask & OPENGL_DIFFUSE_MASK)
91 const float* aSrcDif = theProp.isphysic ? theProp.difcol.rgb : theProp.matcol.rgb;
92 Diffuse = OpenGl_Vec4 (aSrcDif[0] * theProp.diff,
93 aSrcDif[1] * theProp.diff,
94 aSrcDif[2] * theProp.diff,
99 Diffuse = THE_BLACK_COLOR;
102 // specular component
103 if (theProp.color_mask & OPENGL_SPECULAR_MASK)
105 const float* aSrcSpe = theProp.isphysic ? theProp.speccol.rgb : THE_WHITE_COLOR.rgb;
106 Specular = OpenGl_Vec4 (aSrcSpe[0] * theProp.spec,
107 aSrcSpe[1] * theProp.spec,
108 aSrcSpe[2] * theProp.spec,
113 Specular = THE_BLACK_COLOR;
116 // emission component
117 if (theProp.color_mask & OPENGL_EMISSIVE_MASK)
119 const float* aSrcEms = theProp.isphysic ? theProp.emscol.rgb : theProp.matcol.rgb;
120 Emission = OpenGl_Vec4 (aSrcEms[0] * theProp.emsv,
121 aSrcEms[1] * theProp.emsv,
122 aSrcEms[2] * theProp.emsv,
127 Emission = THE_BLACK_COLOR;
130 ChangeShine() = theProp.shine;
131 ChangeTransparency() = theProp.trans;
134 // =======================================================================
135 // function : OpenGl_Workspace
137 // =======================================================================
138 OpenGl_Workspace::OpenGl_Workspace (const Handle(OpenGl_GraphicDriver)& theDriver,
139 const CALL_DEF_WINDOW& theCWindow,
140 Aspect_RenderingContext theGContext,
141 const Handle(OpenGl_Caps)& theCaps,
142 const Handle(OpenGl_Context)& theShareCtx)
143 : OpenGl_Window (theDriver, theCWindow, theGContext, theCaps, theShareCtx),
145 HighlightColor (&THE_WHITE_COLOR),
147 myComputeInitStatus (OpenGl_RT_NONE),
148 myIsRaytraceDataValid (Standard_False),
149 myIsRaytraceWarnTextures (Standard_False),
150 myViewModificationStatus (0),
151 myLayersModificationStatus (0),
153 myRaytraceFilter (new OpenGl_RaytraceFilter()),
154 myToRedrawGL (Standard_True),
155 myAntiAliasingMode (3),
156 myTransientDrawToFront (Standard_True),
157 myBackBufferRestored (Standard_False),
158 myIsImmediateDrawn (Standard_False),
159 myUseZBuffer (Standard_False),
160 myUseDepthTest (Standard_True),
161 myUseGLLight (Standard_True),
162 myIsCullingEnabled (Standard_False),
165 AspectLine_set (&myDefaultAspectLine),
166 AspectLine_applied (NULL),
167 AspectFace_set (&myDefaultAspectFace),
168 AspectFace_applied (NULL),
169 AspectMarker_set (&myDefaultAspectMarker),
170 AspectMarker_applied (NULL),
171 AspectText_set (&myDefaultAspectText),
172 AspectText_applied (NULL),
173 TextParam_set (&myDefaultTextParam),
174 TextParam_applied (NULL),
175 ViewMatrix_applied (&myDefaultMatrix),
176 StructureMatrix_applied (&myDefaultMatrix),
177 myCullingMode (TelCullUndefined),
178 myModelViewMatrix (myDefaultMatrix),
179 PolygonOffset_applied (THE_DEFAULT_POFFSET)
181 myGlContext->core11fwd->glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
183 if (!myGlContext->GetResource ("OpenGl_LineAttributes", myLineAttribs))
185 // share and register for release once the resource is no longer used
186 myLineAttribs = new OpenGl_LineAttributes();
187 myGlContext->ShareResource ("OpenGl_LineAttributes", myLineAttribs);
188 myLineAttribs->Init (myGlContext);
191 // General initialization of the context
193 #if !defined(GL_ES_VERSION_2_0)
194 // Eviter d'avoir les faces mal orientees en noir.
195 // Pourrait etre utiliser pour detecter les problemes d'orientation
196 glLightModeli ((GLenum )GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
198 // Optimisation pour le Fog et l'antialiasing
199 glHint (GL_FOG_HINT, GL_FASTEST);
200 glHint (GL_POINT_SMOOTH_HINT, GL_FASTEST);
201 glHint (GL_LINE_SMOOTH_HINT, GL_FASTEST);
202 glHint (GL_POLYGON_SMOOTH_HINT, GL_FASTEST);
206 const char* anAaEnv = ::getenv ("CALL_OPENGL_ANTIALIASING_MODE");
210 if (sscanf (anAaEnv, "%d", &v) > 0) myAntiAliasingMode = v;
214 // =======================================================================
215 // function : SetImmediateModeDrawToFront
217 // =======================================================================
218 Standard_Boolean OpenGl_Workspace::SetImmediateModeDrawToFront (const Standard_Boolean theDrawToFrontBuffer)
220 const Standard_Boolean aPrevMode = myTransientDrawToFront;
221 myTransientDrawToFront = theDrawToFrontBuffer;
225 // =======================================================================
226 // function : ~OpenGl_Workspace
228 // =======================================================================
229 OpenGl_Workspace::~OpenGl_Workspace()
231 if (!myLineAttribs.IsNull())
233 myLineAttribs.Nullify();
234 myGlContext->ReleaseResource ("OpenGl_LineAttributes", Standard_True);
237 ReleaseRaytraceResources();
240 // =======================================================================
241 // function : Activate
243 // =======================================================================
244 Standard_Boolean OpenGl_Workspace::Activate()
246 if (!OpenGl_Window::Activate())
247 return Standard_False;
249 ViewMatrix_applied = &myDefaultMatrix;
250 StructureMatrix_applied = &myDefaultMatrix;
252 ResetAppliedAspect();
254 return Standard_True;
257 //=======================================================================
258 //function : ResetAppliedAspect
259 //purpose : Sets default values of GL parameters in accordance with default aspects
260 //=======================================================================
261 void OpenGl_Workspace::ResetAppliedAspect()
263 NamedStatus = !myTextureBound.IsNull() ? OPENGL_NS_TEXTURE : 0;
264 HighlightColor = &THE_WHITE_COLOR;
265 AspectLine_set = &myDefaultAspectLine;
266 AspectLine_applied = NULL;
267 AspectFace_set = &myDefaultAspectFace;
268 AspectFace_applied = NULL;
269 AspectMarker_set = &myDefaultAspectMarker;
270 AspectMarker_applied = NULL;
271 AspectText_set = &myDefaultAspectText;
272 AspectText_applied = NULL;
273 TextParam_set = &myDefaultTextParam;
274 TextParam_applied = NULL;
275 PolygonOffset_applied = THE_DEFAULT_POFFSET;
276 myCullingMode = TelCullUndefined;
278 AspectLine(Standard_True);
279 AspectFace(Standard_True);
280 AspectMarker(Standard_True);
281 AspectText(Standard_True);
284 // =======================================================================
285 // function : DisableTexture
287 // =======================================================================
288 Handle(OpenGl_Texture) OpenGl_Workspace::DisableTexture()
290 if (myTextureBound.IsNull())
292 return myTextureBound;
295 const Handle(OpenGl_Sampler)& aSampler = myGlContext->TextureSampler();
296 if (!aSampler.IsNull())
298 aSampler->Unbind (*myGlContext);
301 #if !defined(GL_ES_VERSION_2_0)
302 // reset texture matrix because some code may expect it is identity
303 GLint aMatrixMode = GL_TEXTURE;
304 glGetIntegerv (GL_MATRIX_MODE, &aMatrixMode);
305 glMatrixMode (GL_TEXTURE);
307 glMatrixMode (aMatrixMode);
310 myTextureBound->Unbind (myGlContext);
311 switch (myTextureBound->GetTarget())
313 #if !defined(GL_ES_VERSION_2_0)
317 if (myTextureBound->GetParams()->GenMode() != GL_NONE)
319 glDisable (GL_TEXTURE_GEN_S);
321 glDisable (GL_TEXTURE_1D);
327 #if !defined(GL_ES_VERSION_2_0)
328 if (myTextureBound->GetParams()->GenMode() != GL_NONE)
330 glDisable (GL_TEXTURE_GEN_S);
331 glDisable (GL_TEXTURE_GEN_T);
332 if (myTextureBound->GetParams()->GenMode() == Graphic3d_TOTM_SPRITE)
334 glDisable (GL_POINT_SPRITE);
337 glDisable (GL_TEXTURE_2D);
344 Handle(OpenGl_Texture) aPrevTexture = myTextureBound;
345 myTextureBound.Nullify();
349 // =======================================================================
350 // function : setTextureParams
352 // =======================================================================
353 void OpenGl_Workspace::setTextureParams (Handle(OpenGl_Texture)& theTexture,
354 const Handle(Graphic3d_TextureParams)& theParams)
356 const Handle(Graphic3d_TextureParams)& aParams = theParams.IsNull() ? theTexture->GetParams() : theParams;
357 if (aParams.IsNull())
362 #if !defined(GL_ES_VERSION_2_0)
363 GLint aMatrixMode = GL_TEXTURE;
364 glGetIntegerv (GL_MATRIX_MODE, &aMatrixMode);
366 // setup texture matrix
367 glMatrixMode (GL_TEXTURE);
368 OpenGl_Mat4 aTextureMat;
369 const Graphic3d_Vec2& aScale = aParams->Scale();
370 const Graphic3d_Vec2& aTrans = aParams->Translation();
371 OpenGl_Utils::Scale (aTextureMat, aScale.x(), aScale.y(), 1.0f);
372 OpenGl_Utils::Translate (aTextureMat, -aTrans.x(), -aTrans.y(), 0.0f);
373 OpenGl_Utils::Rotate (aTextureMat, -aParams->Rotation(), 0.0f, 0.0f, 1.0f);
374 glLoadMatrixf (aTextureMat);
376 GLint anEnvMode = GL_MODULATE; // lighting mode
377 if (!aParams->IsModulate())
379 anEnvMode = GL_DECAL;
380 if (theTexture->GetFormat() == GL_ALPHA
381 || theTexture->GetFormat() == GL_LUMINANCE)
383 anEnvMode = GL_REPLACE;
387 // setup generation of texture coordinates
388 switch (aParams->GenMode())
390 case Graphic3d_TOTM_OBJECT:
392 glTexGeni (GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
393 glTexGenfv (GL_S, GL_OBJECT_PLANE, aParams->GenPlaneS().GetData());
394 if (theTexture->GetTarget() != GL_TEXTURE_1D)
396 glTexGeni (GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
397 glTexGenfv (GL_T, GL_OBJECT_PLANE, aParams->GenPlaneT().GetData());
401 case Graphic3d_TOTM_SPHERE:
403 glTexGeni (GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
404 if (theTexture->GetTarget() != GL_TEXTURE_1D)
406 glTexGeni (GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
410 case Graphic3d_TOTM_EYE:
412 myGlContext->WorldViewState.Push();
414 myGlContext->WorldViewState.SetIdentity();
415 myGlContext->ApplyWorldViewMatrix();
417 glTexGeni (GL_S, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
418 glTexGenfv (GL_S, GL_EYE_PLANE, aParams->GenPlaneS().GetData());
420 if (theTexture->GetTarget() != GL_TEXTURE_1D)
422 glTexGeni (GL_T, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
423 glTexGenfv (GL_T, GL_EYE_PLANE, aParams->GenPlaneT().GetData());
426 myGlContext->WorldViewState.Pop();
430 case Graphic3d_TOTM_SPRITE:
432 if (GetGlContext()->core20 != NULL)
434 glEnable (GL_POINT_SPRITE);
435 glTexEnvi (GL_POINT_SPRITE, GL_COORD_REPLACE, GL_TRUE);
436 anEnvMode = GL_REPLACE;
437 GetGlContext()->core15->glPointParameteri (GL_POINT_SPRITE_COORD_ORIGIN, GL_LOWER_LEFT);
441 case Graphic3d_TOTM_MANUAL:
446 glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, anEnvMode);
449 // get active sampler object to override default texture parameters
450 const Handle(OpenGl_Sampler)& aSampler = myGlContext->TextureSampler();
452 // setup texture filtering and wrapping
453 //if (theTexture->GetParams() != theParams)
454 const GLenum aFilter = (aParams->Filter() == Graphic3d_TOTF_NEAREST) ? GL_NEAREST : GL_LINEAR;
455 const GLenum aWrapMode = aParams->IsRepeat() ? GL_REPEAT : myGlContext->TextureWrapClamp();
456 switch (theTexture->GetTarget())
458 #if !defined(GL_ES_VERSION_2_0)
461 if (aSampler.IsNull() || !aSampler->IsValid())
463 glTexParameteri (GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, aFilter);
464 glTexParameteri (GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, aFilter);
465 glTexParameteri (GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, aWrapMode);
469 aSampler->SetParameter (*myGlContext, GL_TEXTURE_MAG_FILTER, aFilter);
470 aSampler->SetParameter (*myGlContext, GL_TEXTURE_MIN_FILTER, aFilter);
471 aSampler->SetParameter (*myGlContext, GL_TEXTURE_WRAP_S, aWrapMode);
479 GLenum aFilterMin = aFilter;
480 if (theTexture->HasMipmaps())
482 aFilterMin = GL_NEAREST_MIPMAP_NEAREST;
483 if (aParams->Filter() == Graphic3d_TOTF_BILINEAR)
485 aFilterMin = GL_LINEAR_MIPMAP_NEAREST;
487 else if (aParams->Filter() == Graphic3d_TOTF_TRILINEAR)
489 aFilterMin = GL_LINEAR_MIPMAP_LINEAR;
492 if (myGlContext->extAnis)
494 // setup degree of anisotropy filter
495 const GLint aMaxDegree = myGlContext->MaxDegreeOfAnisotropy();
497 switch (aParams->AnisoFilter())
499 case Graphic3d_LOTA_QUALITY:
501 aDegree = aMaxDegree;
504 case Graphic3d_LOTA_MIDDLE:
506 aDegree = (aMaxDegree <= 4) ? 2 : (aMaxDegree / 2);
509 case Graphic3d_LOTA_FAST:
514 case Graphic3d_LOTA_OFF:
522 if (aSampler.IsNull() || !aSampler->IsValid())
524 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, aDegree);
528 aSampler->SetParameter (*myGlContext, GL_TEXTURE_MAX_ANISOTROPY_EXT, aDegree);
533 if (aSampler.IsNull() || !aSampler->IsValid())
535 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, aFilterMin);
536 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, aFilter);
537 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, aWrapMode);
538 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, aWrapMode);
542 aSampler->SetParameter (*myGlContext, GL_TEXTURE_MIN_FILTER, aFilterMin);
543 aSampler->SetParameter (*myGlContext, GL_TEXTURE_MAG_FILTER, aFilter);
544 aSampler->SetParameter (*myGlContext, GL_TEXTURE_WRAP_S, aWrapMode);
545 aSampler->SetParameter (*myGlContext, GL_TEXTURE_WRAP_T, aWrapMode);
553 switch (theTexture->GetTarget())
555 #if !defined(GL_ES_VERSION_2_0)
558 if (aParams->GenMode() != Graphic3d_TOTM_MANUAL)
560 glEnable (GL_TEXTURE_GEN_S);
562 glEnable (GL_TEXTURE_1D);
568 #if !defined(GL_ES_VERSION_2_0)
569 if (aParams->GenMode() != Graphic3d_TOTM_MANUAL)
571 glEnable (GL_TEXTURE_GEN_S);
572 glEnable (GL_TEXTURE_GEN_T);
574 glEnable (GL_TEXTURE_2D);
581 #if !defined(GL_ES_VERSION_2_0)
582 glMatrixMode (aMatrixMode); // turn back active matrix
584 theTexture->SetParams (aParams);
587 // =======================================================================
588 // function : EnableTexture
590 // =======================================================================
591 Handle(OpenGl_Texture) OpenGl_Workspace::EnableTexture (const Handle(OpenGl_Texture)& theTexture,
592 const Handle(Graphic3d_TextureParams)& theParams)
594 if (theTexture.IsNull() || !theTexture->IsValid())
596 return DisableTexture();
599 if (myTextureBound == theTexture
600 && (theParams.IsNull() || theParams == theTexture->GetParams()))
603 return myTextureBound;
606 Handle(OpenGl_Texture) aPrevTexture = DisableTexture();
607 myTextureBound = theTexture;
608 myTextureBound->Bind (myGlContext);
609 setTextureParams (myTextureBound, theParams);
611 // If custom sampler object is available it will be
612 // used for overriding default texture parameters
613 const Handle(OpenGl_Sampler)& aSampler = myGlContext->TextureSampler();
615 if (!aSampler.IsNull() && aSampler->IsValid())
617 aSampler->Bind (*myGlContext);
623 // =======================================================================
626 // =======================================================================
627 void OpenGl_Workspace::Redraw (const Graphic3d_CView& theCView,
628 const Aspect_CLayer2d& theCUnderLayer,
629 const Aspect_CLayer2d& theCOverLayer)
637 myIsCullingEnabled = theCView.IsCullingEnabled;
639 // release pending GL resources
640 Handle(OpenGl_Context) aGlCtx = GetGlContext();
641 aGlCtx->ReleaseDelayed();
643 // fetch OpenGl context state
644 aGlCtx->FetchState();
646 Tint toSwap = (aGlCtx->IsRender() && !aGlCtx->caps->buffersNoSwap) ? 1 : 0; // swap buffers
647 OpenGl_FrameBuffer* aFrameBuffer = (OpenGl_FrameBuffer* )theCView.ptrFBO;
648 if (aFrameBuffer != NULL)
650 aFrameBuffer->SetupViewport (aGlCtx);
651 toSwap = 0; // no need to swap buffers
655 aGlCtx->core11fwd->glViewport (0, 0, myWidth, myHeight);
658 myToRedrawGL = Standard_True;
659 if (theCView.RenderParams.Method == Graphic3d_RM_RAYTRACING
660 && myComputeInitStatus != OpenGl_RT_FAIL)
662 if (UpdateRaytraceGeometry (OpenGl_GUM_CHECK) && myIsRaytraceDataValid)
664 myToRedrawGL = Standard_False;
666 // Only non-raytracable structures should be rendered in OpenGL mode.
667 Handle(OpenGl_RenderFilter) aRenderFilter = GetRenderFilter();
668 myRaytraceFilter->SetPrevRenderFilter (aRenderFilter);
669 SetRenderFilter (myRaytraceFilter);
671 Standard_Integer aSizeX = aFrameBuffer != NULL ? aFrameBuffer->GetVPSizeX() : myWidth;
672 Standard_Integer aSizeY = aFrameBuffer != NULL ? aFrameBuffer->GetVPSizeY() : myHeight;
674 if (myOpenGlFBO.IsNull())
676 myOpenGlFBO = new OpenGl_FrameBuffer();
678 if (myOpenGlFBO->GetVPSizeX() != aSizeX
679 || myOpenGlFBO->GetVPSizeY() != aSizeY)
681 myOpenGlFBO->Init (aGlCtx, aSizeX, aSizeY);
684 // OverLayer and UnderLayer shouldn't be drawn by OpenGL.
685 // They will be drawn during ray-tracing.
686 Aspect_CLayer2d anEmptyCLayer;
687 anEmptyCLayer.ptrLayer = NULL;
689 myOpenGlFBO->BindBuffer (aGlCtx);
690 redraw1 (theCView, anEmptyCLayer, anEmptyCLayer, 0);
691 myOpenGlFBO->UnbindBuffer (aGlCtx);
693 const Standard_Boolean isImmediate = myView->HasImmediateStructures();
694 Raytrace (theCView, aSizeX, aSizeY, isImmediate ? 0 : toSwap,
695 theCOverLayer, theCUnderLayer, aFrameBuffer);
699 RedrawImmediate (theCView, theCUnderLayer, theCOverLayer, Standard_True);
702 SetRenderFilter (aRenderFilter);
704 theCView.WasRedrawnGL = Standard_False;
710 // draw entire frame using normal OpenGL pipeline
711 if (aFrameBuffer != NULL)
713 aFrameBuffer->BindBuffer (aGlCtx);
716 const Standard_Boolean isImmediate = myView->HasImmediateStructures();
717 redraw1 (theCView, theCUnderLayer, theCOverLayer, isImmediate ? 0 : toSwap);
720 RedrawImmediate (theCView, theCUnderLayer, theCOverLayer, Standard_True);
723 theCView.WasRedrawnGL = Standard_True;
726 if (aFrameBuffer != NULL)
728 aFrameBuffer->UnbindBuffer (aGlCtx);
729 // move back original viewport
730 aGlCtx->core11fwd->glViewport (0, 0, myWidth, myHeight);
733 #if defined(_WIN32) && defined(HAVE_VIDEOCAPTURE)
734 if (OpenGl_AVIWriter_AllowWriting (theCView.DefWindow.XWindow))
737 glGetIntegerv (GL_VIEWPORT, params);
738 int nWidth = params[2] & ~0x7;
739 int nHeight = params[3] & ~0x7;
741 const int nBitsPerPixel = 24;
742 GLubyte* aDumpData = new GLubyte[nWidth * nHeight * nBitsPerPixel / 8];
744 glPixelStorei (GL_PACK_ALIGNMENT, 1);
745 glReadPixels (0, 0, nWidth, nHeight, GL_BGR_EXT, GL_UNSIGNED_BYTE, aDumpData);
746 OpenGl_AVIWriter_AVIWriter (aDumpData, nWidth, nHeight, nBitsPerPixel);
751 // reset render mode state
752 aGlCtx->FetchState();
755 // =======================================================================
756 // function : redraw1
758 // =======================================================================
759 void OpenGl_Workspace::redraw1 (const Graphic3d_CView& theCView,
760 const Aspect_CLayer2d& theCUnderLayer,
761 const Aspect_CLayer2d& theCOverLayer,
769 // request reset of material
770 NamedStatus |= OPENGL_NS_RESMAT;
772 GLbitfield toClear = GL_COLOR_BUFFER_BIT;
775 glDepthFunc (GL_LEQUAL);
776 glDepthMask (GL_TRUE);
779 glEnable (GL_DEPTH_TEST);
783 glDisable (GL_DEPTH_TEST);
786 #if !defined(GL_ES_VERSION_2_0)
789 glClearDepthf (1.0f);
791 toClear |= GL_DEPTH_BUFFER_BIT;
795 glDisable (GL_DEPTH_TEST);
800 // set background to black
801 glClearColor (0.0f, 0.0f, 0.0f, 0.0f);
802 toClear |= GL_DEPTH_BUFFER_BIT; //
806 if (NamedStatus & OPENGL_NS_WHITEBACK)
808 // set background to white
809 glClearColor (1.0f, 1.0f, 1.0f, 1.0f);
810 toClear |= GL_DEPTH_BUFFER_BIT;
814 glClearColor (myBgColor.rgb[0], myBgColor.rgb[1], myBgColor.rgb[2], 0.0f);
819 Handle(OpenGl_Workspace) aWS (this);
820 myView->Render (myPrintContext, aWS, theCView, theCUnderLayer, theCOverLayer, Standard_False);
825 GetGlContext()->SwapBuffers();
826 myBackBufferRestored = Standard_False;
827 myIsImmediateDrawn = Standard_False;
832 myBackBufferRestored = Standard_True;//
833 myIsImmediateDrawn = Standard_False;//
837 // =======================================================================
838 // function : copyBackToFront
840 // =======================================================================
841 void OpenGl_Workspace::copyBackToFront()
843 #if !defined(GL_ES_VERSION_2_0)
845 OpenGl_Mat4 aProjectMat;
846 OpenGl_Utils::Ortho2D<Standard_ShortReal> (aProjectMat,
847 0.f, static_cast<GLfloat> (myWidth), 0.f, static_cast<GLfloat> (myHeight));
849 myGlContext->WorldViewState.Push();
850 myGlContext->ProjectionState.Push();
852 myGlContext->WorldViewState.SetIdentity();
853 myGlContext->ProjectionState.SetCurrent (aProjectMat);
855 myGlContext->ApplyProjectionMatrix();
856 myGlContext->ApplyWorldViewMatrix();
860 glDrawBuffer (GL_FRONT);
861 glReadBuffer (GL_BACK);
863 glRasterPos2i (0, 0);
864 glCopyPixels (0, 0, myWidth + 1, myHeight + 1, GL_COLOR);
865 //glCopyPixels (0, 0, myWidth + 1, myHeight + 1, GL_DEPTH);
869 myGlContext->WorldViewState.Pop();
870 myGlContext->ProjectionState.Pop();
871 myGlContext->ApplyProjectionMatrix();
872 glDrawBuffer (GL_BACK);
875 myIsImmediateDrawn = Standard_False;
878 // =======================================================================
879 // function : DisplayCallback
881 // =======================================================================
882 void OpenGl_Workspace::DisplayCallback (const Graphic3d_CView& theCView,
883 Standard_Integer theReason)
885 if (theCView.GDisplayCB == NULL)
890 Aspect_GraphicCallbackStruct aCallData;
891 aCallData.reason = theReason;
892 aCallData.glContext = GetGlContext();
893 aCallData.wsID = theCView.WsId;
894 aCallData.viewID = theCView.ViewId;
895 theCView.GDisplayCB (theCView.DefWindow.XWindow, theCView.GClientData, &aCallData);
898 // =======================================================================
899 // function : RedrawImmediate
901 // =======================================================================
902 void OpenGl_Workspace::RedrawImmediate (const Graphic3d_CView& theCView,
903 const Aspect_CLayer2d& theCUnderLayer,
904 const Aspect_CLayer2d& theCOverLayer,
905 const Standard_Boolean theToForce)
912 GLboolean isDoubleBuffer = GL_FALSE;
913 #if !defined(GL_ES_VERSION_2_0)
914 glGetBooleanv (GL_DOUBLEBUFFER, &isDoubleBuffer);
916 if (!myView->HasImmediateStructures())
919 || !myIsImmediateDrawn)
921 myIsImmediateDrawn = Standard_False;
925 if (myBackBufferRestored
933 Redraw (theCView, theCUnderLayer, theCOverLayer);
938 if (isDoubleBuffer && myTransientDrawToFront)
940 if (!myBackBufferRestored)
942 Redraw (theCView, theCUnderLayer, theCOverLayer);
946 MakeFrontBufCurrent();
950 myBackBufferRestored = Standard_False;
952 myIsImmediateDrawn = Standard_True;
954 Handle(OpenGl_Workspace) aWS (this);
958 glDepthFunc (GL_LEQUAL);
959 glDepthMask (GL_TRUE);
962 glEnable (GL_DEPTH_TEST);
966 glDisable (GL_DEPTH_TEST);
969 #if !defined(GL_ES_VERSION_2_0)
972 glClearDepthf (1.0f);
977 glDisable (GL_DEPTH_TEST);
980 myView->Render (myPrintContext, aWS, theCView, theCUnderLayer, theCOverLayer, Standard_True);
981 if (!myView->ImmediateStructures().IsEmpty())
983 glDisable (GL_DEPTH_TEST);
985 for (OpenGl_SequenceOfStructure::Iterator anIter (myView->ImmediateStructures());
986 anIter.More(); anIter.Next())
988 const OpenGl_Structure* aStructure = anIter.Value();
989 if (!aStructure->IsVisible())
994 aStructure->Render (aWS);
997 if (isDoubleBuffer && myTransientDrawToFront)
1000 MakeBackBufCurrent();