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 myUseTransparency (Standard_False),
160 myUseZBuffer (Standard_False),
161 myUseDepthTest (Standard_True),
162 myUseGLLight (Standard_True),
163 myIsCullingEnabled (Standard_False),
166 AspectLine_set (&myDefaultAspectLine),
167 AspectLine_applied (NULL),
168 AspectFace_set (&myDefaultAspectFace),
169 AspectFace_applied (NULL),
170 AspectMarker_set (&myDefaultAspectMarker),
171 AspectMarker_applied (NULL),
172 AspectText_set (&myDefaultAspectText),
173 AspectText_applied (NULL),
174 TextParam_set (&myDefaultTextParam),
175 TextParam_applied (NULL),
176 ViewMatrix_applied (&myDefaultMatrix),
177 StructureMatrix_applied (&myDefaultMatrix),
178 myCullingMode (TelCullUndefined),
179 myModelViewMatrix (myDefaultMatrix),
180 PolygonOffset_applied (THE_DEFAULT_POFFSET)
182 myGlContext->core11fwd->glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
184 if (!myGlContext->GetResource ("OpenGl_LineAttributes", myLineAttribs))
186 // share and register for release once the resource is no longer used
187 myLineAttribs = new OpenGl_LineAttributes();
188 myGlContext->ShareResource ("OpenGl_LineAttributes", myLineAttribs);
189 myLineAttribs->Init (myGlContext);
192 // General initialization of the context
194 #if !defined(GL_ES_VERSION_2_0)
195 // Eviter d'avoir les faces mal orientees en noir.
196 // Pourrait etre utiliser pour detecter les problemes d'orientation
197 glLightModeli ((GLenum )GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
199 // Optimisation pour le Fog et l'antialiasing
200 glHint (GL_FOG_HINT, GL_FASTEST);
201 glHint (GL_POINT_SMOOTH_HINT, GL_FASTEST);
202 glHint (GL_LINE_SMOOTH_HINT, GL_FASTEST);
203 glHint (GL_POLYGON_SMOOTH_HINT, GL_FASTEST);
207 const char* anAaEnv = ::getenv ("CALL_OPENGL_ANTIALIASING_MODE");
211 if (sscanf (anAaEnv, "%d", &v) > 0) myAntiAliasingMode = v;
215 // =======================================================================
216 // function : SetImmediateModeDrawToFront
218 // =======================================================================
219 Standard_Boolean OpenGl_Workspace::SetImmediateModeDrawToFront (const Standard_Boolean theDrawToFrontBuffer)
221 const Standard_Boolean aPrevMode = myTransientDrawToFront;
222 myTransientDrawToFront = theDrawToFrontBuffer;
226 // =======================================================================
227 // function : ~OpenGl_Workspace
229 // =======================================================================
230 OpenGl_Workspace::~OpenGl_Workspace()
232 if (!myLineAttribs.IsNull())
234 myLineAttribs.Nullify();
235 myGlContext->ReleaseResource ("OpenGl_LineAttributes", Standard_True);
238 ReleaseRaytraceResources();
241 // =======================================================================
242 // function : Activate
244 // =======================================================================
245 Standard_Boolean OpenGl_Workspace::Activate()
247 if (!OpenGl_Window::Activate())
248 return Standard_False;
250 ViewMatrix_applied = &myDefaultMatrix;
251 StructureMatrix_applied = &myDefaultMatrix;
253 ResetAppliedAspect();
255 return Standard_True;
258 // =======================================================================
259 // function : UseTransparency
260 // purpose : call_togl_transparency
261 // =======================================================================
262 void OpenGl_Workspace::UseTransparency (const Standard_Boolean theFlag)
264 myUseTransparency = theFlag;
267 //=======================================================================
268 //function : ResetAppliedAspect
269 //purpose : Sets default values of GL parameters in accordance with default aspects
270 //=======================================================================
271 void OpenGl_Workspace::ResetAppliedAspect()
273 NamedStatus = !myTextureBound.IsNull() ? OPENGL_NS_TEXTURE : 0;
274 HighlightColor = &THE_WHITE_COLOR;
275 AspectLine_set = &myDefaultAspectLine;
276 AspectLine_applied = NULL;
277 AspectFace_set = &myDefaultAspectFace;
278 AspectFace_applied = NULL;
279 AspectMarker_set = &myDefaultAspectMarker;
280 AspectMarker_applied = NULL;
281 AspectText_set = &myDefaultAspectText;
282 AspectText_applied = NULL;
283 TextParam_set = &myDefaultTextParam;
284 TextParam_applied = NULL;
285 PolygonOffset_applied = THE_DEFAULT_POFFSET;
286 myCullingMode = TelCullUndefined;
288 AspectLine(Standard_True);
289 AspectFace(Standard_True);
290 AspectMarker(Standard_True);
291 AspectText(Standard_True);
294 // =======================================================================
295 // function : DisableTexture
297 // =======================================================================
298 Handle(OpenGl_Texture) OpenGl_Workspace::DisableTexture()
300 if (myTextureBound.IsNull())
302 return myTextureBound;
305 #if !defined(GL_ES_VERSION_2_0)
306 // reset texture matrix because some code may expect it is identity
307 GLint aMatrixMode = GL_TEXTURE;
308 glGetIntegerv (GL_MATRIX_MODE, &aMatrixMode);
309 glMatrixMode (GL_TEXTURE);
311 glMatrixMode (aMatrixMode);
314 myTextureBound->Unbind (myGlContext);
315 switch (myTextureBound->GetTarget())
317 #if !defined(GL_ES_VERSION_2_0)
321 if (myTextureBound->GetParams()->GenMode() != GL_NONE)
323 glDisable (GL_TEXTURE_GEN_S);
325 glDisable (GL_TEXTURE_1D);
331 #if !defined(GL_ES_VERSION_2_0)
332 if (myTextureBound->GetParams()->GenMode() != GL_NONE)
334 glDisable (GL_TEXTURE_GEN_S);
335 glDisable (GL_TEXTURE_GEN_T);
336 if (myTextureBound->GetParams()->GenMode() == Graphic3d_TOTM_SPRITE)
338 glDisable (GL_POINT_SPRITE);
341 glDisable (GL_TEXTURE_2D);
348 Handle(OpenGl_Texture) aPrevTexture = myTextureBound;
349 myTextureBound.Nullify();
353 // =======================================================================
354 // function : setTextureParams
356 // =======================================================================
357 void OpenGl_Workspace::setTextureParams (Handle(OpenGl_Texture)& theTexture,
358 const Handle(Graphic3d_TextureParams)& theParams)
360 const Handle(Graphic3d_TextureParams)& aParams = theParams.IsNull() ? theTexture->GetParams() : theParams;
361 if (aParams.IsNull())
366 #if !defined(GL_ES_VERSION_2_0)
367 GLint aMatrixMode = GL_TEXTURE;
368 glGetIntegerv (GL_MATRIX_MODE, &aMatrixMode);
370 // setup texture matrix
371 glMatrixMode (GL_TEXTURE);
372 OpenGl_Mat4 aTextureMat;
373 const Graphic3d_Vec2& aScale = aParams->Scale();
374 const Graphic3d_Vec2& aTrans = aParams->Translation();
375 OpenGl_Utils::Scale (aTextureMat, aScale.x(), aScale.y(), 1.0f);
376 OpenGl_Utils::Translate (aTextureMat, -aTrans.x(), -aTrans.y(), 0.0f);
377 OpenGl_Utils::Rotate (aTextureMat, -aParams->Rotation(), 0.0f, 0.0f, 1.0f);
378 glLoadMatrixf (aTextureMat);
380 // setup generation of texture coordinates
381 switch (aParams->GenMode())
383 case Graphic3d_TOTM_OBJECT:
385 glTexGeni (GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
386 glTexGenfv (GL_S, GL_OBJECT_PLANE, aParams->GenPlaneS().GetData());
387 if (theTexture->GetTarget() != GL_TEXTURE_1D)
389 glTexGeni (GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
390 glTexGenfv (GL_T, GL_OBJECT_PLANE, aParams->GenPlaneT().GetData());
394 case Graphic3d_TOTM_SPHERE:
396 glTexGeni (GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
397 if (theTexture->GetTarget() != GL_TEXTURE_1D)
399 glTexGeni (GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
403 case Graphic3d_TOTM_EYE:
405 myGlContext->WorldViewState.Push();
407 myGlContext->WorldViewState.SetIdentity();
408 myGlContext->ApplyWorldViewMatrix();
410 glTexGeni (GL_S, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
411 glTexGenfv (GL_S, GL_EYE_PLANE, aParams->GenPlaneS().GetData());
413 if (theTexture->GetTarget() != GL_TEXTURE_1D)
415 glTexGeni (GL_T, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
416 glTexGenfv (GL_T, GL_EYE_PLANE, aParams->GenPlaneT().GetData());
419 myGlContext->WorldViewState.Pop();
423 case Graphic3d_TOTM_SPRITE:
425 if (GetGlContext()->core20 != NULL)
427 glEnable (GL_POINT_SPRITE);
428 glTexEnvi (GL_POINT_SPRITE, GL_COORD_REPLACE, GL_TRUE);
429 glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
430 GetGlContext()->core15->glPointParameteri (GL_POINT_SPRITE_COORD_ORIGIN, GL_LOWER_LEFT);
434 case Graphic3d_TOTM_MANUAL:
439 if (aParams->GenMode() != Graphic3d_TOTM_SPRITE)
441 glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, aParams->IsModulate() ? GL_MODULATE : GL_DECAL);
445 // get active sampler object to override default texture parameters
446 const Handle(OpenGl_Sampler)& aSampler = myGlContext->TextureSampler();
448 // setup texture filtering and wrapping
449 //if (theTexture->GetParams() != theParams)
450 const GLenum aFilter = (aParams->Filter() == Graphic3d_TOTF_NEAREST) ? GL_NEAREST : GL_LINEAR;
451 const GLenum aWrapMode = aParams->IsRepeat() ? GL_REPEAT : myGlContext->TextureWrapClamp();
452 switch (theTexture->GetTarget())
454 #if !defined(GL_ES_VERSION_2_0)
457 if (aSampler.IsNull() || !aSampler->IsValid())
459 glTexParameteri (GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, aFilter);
460 glTexParameteri (GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, aFilter);
461 glTexParameteri (GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, aWrapMode);
465 aSampler->SetParameter (*myGlContext, GL_TEXTURE_MAG_FILTER, aFilter);
466 aSampler->SetParameter (*myGlContext, GL_TEXTURE_MIN_FILTER, aFilter);
467 aSampler->SetParameter (*myGlContext, GL_TEXTURE_WRAP_S, aWrapMode);
475 GLenum aFilterMin = aFilter;
476 if (theTexture->HasMipmaps())
478 aFilterMin = GL_NEAREST_MIPMAP_NEAREST;
479 if (aParams->Filter() == Graphic3d_TOTF_BILINEAR)
481 aFilterMin = GL_LINEAR_MIPMAP_NEAREST;
483 else if (aParams->Filter() == Graphic3d_TOTF_TRILINEAR)
485 aFilterMin = GL_LINEAR_MIPMAP_LINEAR;
488 if (myGlContext->extAnis)
490 // setup degree of anisotropy filter
491 const GLint aMaxDegree = myGlContext->MaxDegreeOfAnisotropy();
493 switch (aParams->AnisoFilter())
495 case Graphic3d_LOTA_QUALITY:
497 aDegree = aMaxDegree;
500 case Graphic3d_LOTA_MIDDLE:
502 aDegree = (aMaxDegree <= 4) ? 2 : (aMaxDegree / 2);
505 case Graphic3d_LOTA_FAST:
510 case Graphic3d_LOTA_OFF:
518 if (aSampler.IsNull() || !aSampler->IsValid())
520 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, aDegree);
524 aSampler->SetParameter (*myGlContext, GL_TEXTURE_MAX_ANISOTROPY_EXT, aDegree);
529 if (aSampler.IsNull() || !aSampler->IsValid())
531 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, aFilterMin);
532 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, aFilter);
533 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, aWrapMode);
534 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, aWrapMode);
538 aSampler->SetParameter (*myGlContext, GL_TEXTURE_MIN_FILTER, aFilterMin);
539 aSampler->SetParameter (*myGlContext, GL_TEXTURE_MAG_FILTER, aFilter);
540 aSampler->SetParameter (*myGlContext, GL_TEXTURE_WRAP_S, aWrapMode);
541 aSampler->SetParameter (*myGlContext, GL_TEXTURE_WRAP_T, aWrapMode);
549 switch (theTexture->GetTarget())
551 #if !defined(GL_ES_VERSION_2_0)
554 if (aParams->GenMode() != Graphic3d_TOTM_MANUAL)
556 glEnable (GL_TEXTURE_GEN_S);
558 glEnable (GL_TEXTURE_1D);
564 #if !defined(GL_ES_VERSION_2_0)
565 if (aParams->GenMode() != Graphic3d_TOTM_MANUAL)
567 glEnable (GL_TEXTURE_GEN_S);
568 glEnable (GL_TEXTURE_GEN_T);
570 glEnable (GL_TEXTURE_2D);
577 #if !defined(GL_ES_VERSION_2_0)
578 glMatrixMode (aMatrixMode); // turn back active matrix
580 theTexture->SetParams (aParams);
583 // =======================================================================
584 // function : EnableTexture
586 // =======================================================================
587 Handle(OpenGl_Texture) OpenGl_Workspace::EnableTexture (const Handle(OpenGl_Texture)& theTexture,
588 const Handle(Graphic3d_TextureParams)& theParams)
590 if (theTexture.IsNull() || !theTexture->IsValid())
592 return DisableTexture();
595 if (myTextureBound == theTexture
596 && (theParams.IsNull() || theParams == theTexture->GetParams()))
599 return myTextureBound;
602 Handle(OpenGl_Texture) aPrevTexture = DisableTexture();
603 myTextureBound = theTexture;
604 myTextureBound->Bind (myGlContext);
605 setTextureParams (myTextureBound, theParams);
607 // If custom sampler object is available it will be
608 // used for overriding default texture parameters
609 const Handle(OpenGl_Sampler)& aSampler = myGlContext->TextureSampler();
611 if (!aSampler.IsNull() && aSampler->IsValid())
613 aSampler->Bind (*myGlContext);
619 // =======================================================================
622 // =======================================================================
623 void OpenGl_Workspace::Redraw (const Graphic3d_CView& theCView,
624 const Aspect_CLayer2d& theCUnderLayer,
625 const Aspect_CLayer2d& theCOverLayer)
633 myIsCullingEnabled = theCView.IsCullingEnabled;
635 // release pending GL resources
636 Handle(OpenGl_Context) aGlCtx = GetGlContext();
637 aGlCtx->ReleaseDelayed();
639 // fetch OpenGl context state
640 aGlCtx->FetchState();
642 Tint toSwap = (aGlCtx->IsRender() && !aGlCtx->caps->buffersNoSwap) ? 1 : 0; // swap buffers
643 OpenGl_FrameBuffer* aFrameBuffer = (OpenGl_FrameBuffer* )theCView.ptrFBO;
644 if (aFrameBuffer != NULL)
646 aFrameBuffer->SetupViewport (aGlCtx);
647 toSwap = 0; // no need to swap buffers
651 aGlCtx->core11fwd->glViewport (0, 0, myWidth, myHeight);
654 myToRedrawGL = Standard_True;
655 if (theCView.RenderParams.Method == Graphic3d_RM_RAYTRACING
656 && myComputeInitStatus != OpenGl_RT_FAIL)
658 if (UpdateRaytraceGeometry (OpenGl_GUM_CHECK) && myIsRaytraceDataValid)
660 myToRedrawGL = Standard_False;
662 // Only non-raytracable structures should be rendered in OpenGL mode.
663 Handle(OpenGl_RenderFilter) aRenderFilter = GetRenderFilter();
664 myRaytraceFilter->SetPrevRenderFilter (aRenderFilter);
665 SetRenderFilter (myRaytraceFilter);
667 Standard_Integer aSizeX = aFrameBuffer != NULL ? aFrameBuffer->GetVPSizeX() : myWidth;
668 Standard_Integer aSizeY = aFrameBuffer != NULL ? aFrameBuffer->GetVPSizeY() : myHeight;
670 if (myOpenGlFBO.IsNull())
672 myOpenGlFBO = new OpenGl_FrameBuffer();
674 if (myOpenGlFBO->GetVPSizeX() != aSizeX
675 || myOpenGlFBO->GetVPSizeY() != aSizeY)
677 myOpenGlFBO->Init (aGlCtx, aSizeX, aSizeY);
680 // OverLayer and UnderLayer shouldn't be drawn by OpenGL.
681 // They will be drawn during ray-tracing.
682 Aspect_CLayer2d anEmptyCLayer;
683 anEmptyCLayer.ptrLayer = NULL;
685 myOpenGlFBO->BindBuffer (aGlCtx);
686 redraw1 (theCView, anEmptyCLayer, anEmptyCLayer, 0);
687 myOpenGlFBO->UnbindBuffer (aGlCtx);
689 const Standard_Boolean isImmediate = !myView->ImmediateStructures().IsEmpty();
690 Raytrace (theCView, aSizeX, aSizeY, isImmediate ? 0 : toSwap,
691 theCOverLayer, theCUnderLayer, aFrameBuffer);
695 RedrawImmediate (theCView, theCUnderLayer, theCOverLayer, Standard_True);
698 SetRenderFilter (aRenderFilter);
700 theCView.WasRedrawnGL = Standard_False;
706 // draw entire frame using normal OpenGL pipeline
707 if (aFrameBuffer != NULL)
709 aFrameBuffer->BindBuffer (aGlCtx);
712 const Standard_Boolean isImmediate = !myView->ImmediateStructures().IsEmpty();
713 redraw1 (theCView, theCUnderLayer, theCOverLayer, isImmediate ? 0 : toSwap);
716 RedrawImmediate (theCView, theCUnderLayer, theCOverLayer, Standard_True);
719 theCView.WasRedrawnGL = Standard_True;
722 if (aFrameBuffer != NULL)
724 aFrameBuffer->UnbindBuffer (aGlCtx);
725 // move back original viewport
726 aGlCtx->core11fwd->glViewport (0, 0, myWidth, myHeight);
729 #if defined(_WIN32) && defined(HAVE_VIDEOCAPTURE)
730 if (OpenGl_AVIWriter_AllowWriting (theCView.DefWindow.XWindow))
733 glGetIntegerv (GL_VIEWPORT, params);
734 int nWidth = params[2] & ~0x7;
735 int nHeight = params[3] & ~0x7;
737 const int nBitsPerPixel = 24;
738 GLubyte* aDumpData = new GLubyte[nWidth * nHeight * nBitsPerPixel / 8];
740 glPixelStorei (GL_PACK_ALIGNMENT, 1);
741 glReadPixels (0, 0, nWidth, nHeight, GL_BGR_EXT, GL_UNSIGNED_BYTE, aDumpData);
742 OpenGl_AVIWriter_AVIWriter (aDumpData, nWidth, nHeight, nBitsPerPixel);
747 // reset render mode state
748 aGlCtx->FetchState();
751 // =======================================================================
752 // function : redraw1
754 // =======================================================================
755 void OpenGl_Workspace::redraw1 (const Graphic3d_CView& theCView,
756 const Aspect_CLayer2d& theCUnderLayer,
757 const Aspect_CLayer2d& theCOverLayer,
765 // request reset of material
766 NamedStatus |= OPENGL_NS_RESMAT;
768 GLbitfield toClear = GL_COLOR_BUFFER_BIT;
771 glDepthFunc (GL_LEQUAL);
772 glDepthMask (GL_TRUE);
775 glEnable (GL_DEPTH_TEST);
779 glDisable (GL_DEPTH_TEST);
782 #if !defined(GL_ES_VERSION_2_0)
785 glClearDepthf (1.0f);
787 toClear |= GL_DEPTH_BUFFER_BIT;
791 glDisable (GL_DEPTH_TEST);
796 // set background to black
797 glClearColor (0.0f, 0.0f, 0.0f, 0.0f);
798 toClear |= GL_DEPTH_BUFFER_BIT; //
802 if (NamedStatus & OPENGL_NS_WHITEBACK)
804 // set background to white
805 glClearColor (1.0f, 1.0f, 1.0f, 1.0f);
806 toClear |= GL_DEPTH_BUFFER_BIT;
810 glClearColor (myBgColor.rgb[0], myBgColor.rgb[1], myBgColor.rgb[2], 0.0f);
815 Handle(OpenGl_Workspace) aWS (this);
816 myView->Render (myPrintContext, aWS, theCView, theCUnderLayer, theCOverLayer);
821 GetGlContext()->SwapBuffers();
822 myBackBufferRestored = Standard_False;
823 myIsImmediateDrawn = Standard_False;
828 myBackBufferRestored = Standard_True;//
829 myIsImmediateDrawn = Standard_False;//
833 // =======================================================================
834 // function : copyBackToFront
836 // =======================================================================
837 void OpenGl_Workspace::copyBackToFront()
839 #if !defined(GL_ES_VERSION_2_0)
841 OpenGl_Mat4 aProjectMat;
842 OpenGl_Utils::Ortho2D<Standard_ShortReal> (aProjectMat,
843 0.f, static_cast<GLfloat> (myWidth), 0.f, static_cast<GLfloat> (myHeight));
845 myGlContext->WorldViewState.Push();
846 myGlContext->ProjectionState.Push();
848 myGlContext->WorldViewState.SetIdentity();
849 myGlContext->ProjectionState.SetCurrent (aProjectMat);
851 myGlContext->ApplyProjectionMatrix();
852 myGlContext->ApplyWorldViewMatrix();
856 glDrawBuffer (GL_FRONT);
857 glReadBuffer (GL_BACK);
859 glRasterPos2i (0, 0);
860 glCopyPixels (0, 0, myWidth + 1, myHeight + 1, GL_COLOR);
864 myGlContext->WorldViewState.Pop();
865 myGlContext->ProjectionState.Pop();
866 myGlContext->ApplyProjectionMatrix();
867 glDrawBuffer (GL_BACK);
870 myIsImmediateDrawn = Standard_False;
873 // =======================================================================
874 // function : DisplayCallback
876 // =======================================================================
877 void OpenGl_Workspace::DisplayCallback (const Graphic3d_CView& theCView,
878 Standard_Integer theReason)
880 if (theCView.GDisplayCB == NULL)
885 Aspect_GraphicCallbackStruct aCallData;
886 aCallData.reason = theReason;
887 aCallData.glContext = GetGlContext();
888 aCallData.wsID = theCView.WsId;
889 aCallData.viewID = theCView.ViewId;
890 theCView.GDisplayCB (theCView.DefWindow.XWindow, theCView.GClientData, &aCallData);
893 // =======================================================================
894 // function : RedrawImmediate
896 // =======================================================================
897 void OpenGl_Workspace::RedrawImmediate (const Graphic3d_CView& theCView,
898 const Aspect_CLayer2d& theCUnderLayer,
899 const Aspect_CLayer2d& theCOverLayer,
900 const Standard_Boolean theToForce)
907 GLboolean isDoubleBuffer = GL_FALSE;
908 #if !defined(GL_ES_VERSION_2_0)
909 glGetBooleanv (GL_DOUBLEBUFFER, &isDoubleBuffer);
911 if (myView->ImmediateStructures().IsEmpty())
914 || !myIsImmediateDrawn)
916 myIsImmediateDrawn = Standard_False;
920 if (myBackBufferRestored
928 Redraw (theCView, theCUnderLayer, theCOverLayer);
933 if (isDoubleBuffer && myTransientDrawToFront)
935 if (!myBackBufferRestored)
937 Redraw (theCView, theCUnderLayer, theCOverLayer);
941 MakeFrontBufCurrent();
945 myBackBufferRestored = Standard_False;
947 myIsImmediateDrawn = Standard_True;
949 NamedStatus |= OPENGL_NS_IMMEDIATE;
950 ///glDisable (GL_LIGHTING);
951 glDisable (GL_DEPTH_TEST);
953 Handle(OpenGl_Workspace) aWS (this);
954 for (OpenGl_SequenceOfStructure::Iterator anIter (myView->ImmediateStructures());
955 anIter.More(); anIter.Next())
957 const OpenGl_Structure* aStructure = anIter.Value();
958 aStructure->Render (aWS);
961 NamedStatus &= ~OPENGL_NS_IMMEDIATE;
963 if (isDoubleBuffer && myTransientDrawToFront)
966 MakeBackBufCurrent();