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_Texture.hxx>
29 #include <OpenGl_View.hxx>
30 #include <OpenGl_Workspace.hxx>
32 #include <Graphic3d_TextureParams.hxx>
34 #if defined(_WIN32) && defined(HAVE_VIDEOCAPTURE)
35 #include <OpenGl_AVIWriter.hxx>
38 IMPLEMENT_STANDARD_HANDLE(OpenGl_Workspace,OpenGl_Window)
39 IMPLEMENT_STANDARD_RTTIEXT(OpenGl_Workspace,OpenGl_Window)
43 static const TEL_COLOUR THE_WHITE_COLOR = { { 1.0f, 1.0f, 1.0f, 1.0f } };
44 static const OpenGl_Vec4 THE_BLACK_COLOR (0.0f, 0.0f, 0.0f, 1.0f);
46 static const OpenGl_AspectLine myDefaultAspectLine;
47 static const OpenGl_AspectFace myDefaultAspectFace;
48 static const OpenGl_AspectMarker myDefaultAspectMarker;
49 static const OpenGl_AspectText myDefaultAspectText;
51 static const OpenGl_TextParam myDefaultTextParam =
53 16, Graphic3d_HTA_LEFT, Graphic3d_VTA_BOTTOM
56 static const OpenGl_Matrix myDefaultMatrix =
58 {{ 1.0F, 0.0F, 0.0F, 0.0F },
59 { 0.0F, 1.0F, 0.0F, 0.0F },
60 { 0.0F, 0.0F, 1.0F, 0.0F },
61 { 0.0F, 0.0F, 0.0F, 1.0F }}
66 // =======================================================================
69 // =======================================================================
70 void OpenGl_Material::Init (const OPENGL_SURF_PROP& theProp)
73 if (theProp.color_mask & OPENGL_AMBIENT_MASK)
75 const float* aSrcAmb = theProp.isphysic ? theProp.ambcol.rgb : theProp.matcol.rgb;
76 Ambient = OpenGl_Vec4 (aSrcAmb[0] * theProp.amb,
77 aSrcAmb[1] * theProp.amb,
78 aSrcAmb[2] * theProp.amb,
83 Ambient = THE_BLACK_COLOR;
86 // diffusion component
87 if (theProp.color_mask & OPENGL_DIFFUSE_MASK)
89 const float* aSrcDif = theProp.isphysic ? theProp.difcol.rgb : theProp.matcol.rgb;
90 Diffuse = OpenGl_Vec4 (aSrcDif[0] * theProp.diff,
91 aSrcDif[1] * theProp.diff,
92 aSrcDif[2] * theProp.diff,
97 Diffuse = THE_BLACK_COLOR;
100 // specular component
101 if (theProp.color_mask & OPENGL_SPECULAR_MASK)
103 const float* aSrcSpe = theProp.isphysic ? theProp.speccol.rgb : THE_WHITE_COLOR.rgb;
104 Specular = OpenGl_Vec4 (aSrcSpe[0] * theProp.spec,
105 aSrcSpe[1] * theProp.spec,
106 aSrcSpe[2] * theProp.spec,
111 Specular = THE_BLACK_COLOR;
114 // emission component
115 if (theProp.color_mask & OPENGL_EMISSIVE_MASK)
117 const float* aSrcEms = theProp.isphysic ? theProp.emscol.rgb : theProp.matcol.rgb;
118 Emission = OpenGl_Vec4 (aSrcEms[0] * theProp.emsv,
119 aSrcEms[1] * theProp.emsv,
120 aSrcEms[2] * theProp.emsv,
125 Emission = THE_BLACK_COLOR;
128 ChangeShine() = theProp.shine;
129 ChangeTransparency() = theProp.trans;
132 // =======================================================================
133 // function : OpenGl_Workspace
135 // =======================================================================
136 OpenGl_Workspace::OpenGl_Workspace (const Handle(OpenGl_GraphicDriver)& theDriver,
137 const CALL_DEF_WINDOW& theCWindow,
138 Aspect_RenderingContext theGContext,
139 const Handle(OpenGl_Caps)& theCaps,
140 const Handle(OpenGl_Context)& theShareCtx)
141 : OpenGl_Window (theDriver, theCWindow, theGContext, theCaps, theShareCtx),
143 HighlightColor (&THE_WHITE_COLOR),
145 myComputeInitStatus (OpenGl_RT_NONE),
146 myIsRaytraceDataValid (Standard_False),
147 myViewModificationStatus (0),
148 myLayersModificationStatus (0),
150 myRaytraceFilter (new OpenGl_RaytraceFilter()),
151 myToRedrawGL (Standard_True),
152 myAntiAliasingMode (3),
153 myTransientDrawToFront (Standard_True),
154 myBackBufferRestored (Standard_False),
155 myIsImmediateDrawn (Standard_False),
156 myUseTransparency (Standard_False),
157 myUseZBuffer (Standard_False),
158 myUseDepthTest (Standard_True),
159 myUseGLLight (Standard_True),
160 myIsCullingEnabled (Standard_False),
163 AspectLine_set (&myDefaultAspectLine),
164 AspectLine_applied (NULL),
165 AspectFace_set (&myDefaultAspectFace),
166 AspectFace_applied (NULL),
167 AspectMarker_set (&myDefaultAspectMarker),
168 AspectMarker_applied (NULL),
169 AspectText_set (&myDefaultAspectText),
170 AspectText_applied (NULL),
171 TextParam_set (&myDefaultTextParam),
172 TextParam_applied (NULL),
173 ViewMatrix_applied (&myDefaultMatrix),
174 StructureMatrix_applied (&myDefaultMatrix),
175 myCullingMode (TelCullUndefined),
176 myModelViewMatrix (myDefaultMatrix),
177 PolygonOffset_applied (THE_DEFAULT_POFFSET)
179 myGlContext->core11fwd->glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
181 if (!myGlContext->GetResource ("OpenGl_LineAttributes", myLineAttribs))
183 // share and register for release once the resource is no longer used
184 myLineAttribs = new OpenGl_LineAttributes();
185 myGlContext->ShareResource ("OpenGl_LineAttributes", myLineAttribs);
186 myLineAttribs->Init (myGlContext);
189 // General initialization of the context
191 #if !defined(GL_ES_VERSION_2_0)
192 // Eviter d'avoir les faces mal orientees en noir.
193 // Pourrait etre utiliser pour detecter les problemes d'orientation
194 glLightModeli ((GLenum )GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
196 // Optimisation pour le Fog et l'antialiasing
197 glHint (GL_FOG_HINT, GL_FASTEST);
198 glHint (GL_POINT_SMOOTH_HINT, GL_FASTEST);
199 glHint (GL_LINE_SMOOTH_HINT, GL_FASTEST);
200 glHint (GL_POLYGON_SMOOTH_HINT, GL_FASTEST);
204 const char* anAaEnv = ::getenv ("CALL_OPENGL_ANTIALIASING_MODE");
208 if (sscanf (anAaEnv, "%d", &v) > 0) myAntiAliasingMode = v;
212 // =======================================================================
213 // function : SetImmediateModeDrawToFront
215 // =======================================================================
216 Standard_Boolean OpenGl_Workspace::SetImmediateModeDrawToFront (const Standard_Boolean theDrawToFrontBuffer)
218 const Standard_Boolean aPrevMode = myTransientDrawToFront;
219 myTransientDrawToFront = theDrawToFrontBuffer;
223 // =======================================================================
224 // function : ~OpenGl_Workspace
226 // =======================================================================
227 OpenGl_Workspace::~OpenGl_Workspace()
229 if (!myLineAttribs.IsNull())
231 myLineAttribs.Nullify();
232 myGlContext->ReleaseResource ("OpenGl_LineAttributes", Standard_True);
235 ReleaseRaytraceResources();
238 // =======================================================================
239 // function : Activate
241 // =======================================================================
242 Standard_Boolean OpenGl_Workspace::Activate()
244 if (!OpenGl_Window::Activate())
245 return Standard_False;
247 ViewMatrix_applied = &myDefaultMatrix;
248 StructureMatrix_applied = &myDefaultMatrix;
250 ResetAppliedAspect();
252 return Standard_True;
255 // =======================================================================
256 // function : UseTransparency
257 // purpose : call_togl_transparency
258 // =======================================================================
259 void OpenGl_Workspace::UseTransparency (const Standard_Boolean theFlag)
261 myUseTransparency = theFlag;
264 //=======================================================================
265 //function : ResetAppliedAspect
266 //purpose : Sets default values of GL parameters in accordance with default aspects
267 //=======================================================================
268 void OpenGl_Workspace::ResetAppliedAspect()
270 NamedStatus = !myTextureBound.IsNull() ? OPENGL_NS_TEXTURE : 0;
271 HighlightColor = &THE_WHITE_COLOR;
272 AspectLine_set = &myDefaultAspectLine;
273 AspectLine_applied = NULL;
274 AspectFace_set = &myDefaultAspectFace;
275 AspectFace_applied = NULL;
276 AspectMarker_set = &myDefaultAspectMarker;
277 AspectMarker_applied = NULL;
278 AspectText_set = &myDefaultAspectText;
279 AspectText_applied = NULL;
280 TextParam_set = &myDefaultTextParam;
281 TextParam_applied = NULL;
282 PolygonOffset_applied = THE_DEFAULT_POFFSET;
283 myCullingMode = TelCullUndefined;
285 AspectLine(Standard_True);
286 AspectFace(Standard_True);
287 AspectMarker(Standard_True);
288 AspectText(Standard_True);
291 // =======================================================================
292 // function : DisableTexture
294 // =======================================================================
295 Handle(OpenGl_Texture) OpenGl_Workspace::DisableTexture()
297 if (myTextureBound.IsNull())
299 return myTextureBound;
302 #if !defined(GL_ES_VERSION_2_0)
303 // reset texture matrix because some code may expect it is identity
304 GLint aMatrixMode = GL_TEXTURE;
305 glGetIntegerv (GL_MATRIX_MODE, &aMatrixMode);
306 glMatrixMode (GL_TEXTURE);
308 glMatrixMode (aMatrixMode);
311 myTextureBound->Unbind (myGlContext);
312 switch (myTextureBound->GetTarget())
314 #if !defined(GL_ES_VERSION_2_0)
318 if (myTextureBound->GetParams()->GenMode() != GL_NONE)
320 glDisable (GL_TEXTURE_GEN_S);
322 glDisable (GL_TEXTURE_1D);
328 #if !defined(GL_ES_VERSION_2_0)
329 if (myTextureBound->GetParams()->GenMode() != GL_NONE)
331 glDisable (GL_TEXTURE_GEN_S);
332 glDisable (GL_TEXTURE_GEN_T);
333 if (myTextureBound->GetParams()->GenMode() == Graphic3d_TOTM_SPRITE)
335 glDisable (GL_POINT_SPRITE);
339 glDisable (GL_TEXTURE_2D);
345 Handle(OpenGl_Texture) aPrevTexture = myTextureBound;
346 myTextureBound.Nullify();
350 // =======================================================================
351 // function : setTextureParams
353 // =======================================================================
354 void OpenGl_Workspace::setTextureParams (Handle(OpenGl_Texture)& theTexture,
355 const Handle(Graphic3d_TextureParams)& theParams)
357 const Handle(Graphic3d_TextureParams)& aParams = theParams.IsNull() ? theTexture->GetParams() : theParams;
358 if (aParams.IsNull())
363 #if !defined(GL_ES_VERSION_2_0)
364 GLint aMatrixMode = GL_TEXTURE;
365 glGetIntegerv (GL_MATRIX_MODE, &aMatrixMode);
367 // setup texture matrix
368 glMatrixMode (GL_TEXTURE);
370 const Graphic3d_Vec2& aScale = aParams->Scale();
371 const Graphic3d_Vec2& aTrans = aParams->Translation();
372 glScalef ( aScale.x(), aScale.y(), 1.0f);
373 glTranslatef (-aTrans.x(), -aTrans.y(), 0.0f);
374 glRotatef (-aParams->Rotation(), 0.0f, 0.0f, 1.0f);
376 // setup generation of texture coordinates
377 switch (aParams->GenMode())
379 case Graphic3d_TOTM_OBJECT:
381 glTexGeni (GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
382 glTexGenfv (GL_S, GL_OBJECT_PLANE, aParams->GenPlaneS().GetData());
383 if (theTexture->GetTarget() != GL_TEXTURE_1D)
385 glTexGeni (GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
386 glTexGenfv (GL_T, GL_OBJECT_PLANE, aParams->GenPlaneT().GetData());
390 case Graphic3d_TOTM_SPHERE:
392 glTexGeni (GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
393 if (theTexture->GetTarget() != GL_TEXTURE_1D)
395 glTexGeni (GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
399 case Graphic3d_TOTM_EYE:
401 glMatrixMode (GL_MODELVIEW);
405 glTexGeni (GL_S, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
406 glTexGenfv (GL_S, GL_EYE_PLANE, aParams->GenPlaneS().GetData());
408 if (theTexture->GetTarget() != GL_TEXTURE_1D)
410 glTexGeni (GL_T, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
411 glTexGenfv (GL_T, GL_EYE_PLANE, aParams->GenPlaneT().GetData());
416 case Graphic3d_TOTM_SPRITE:
418 if (GetGlContext()->core20 != NULL)
420 glEnable (GL_POINT_SPRITE);
421 glTexEnvi (GL_POINT_SPRITE, GL_COORD_REPLACE, GL_TRUE);
422 glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
423 GetGlContext()->core15->glPointParameteri (GL_POINT_SPRITE_COORD_ORIGIN, GL_LOWER_LEFT);
427 case Graphic3d_TOTM_MANUAL:
432 if (aParams->GenMode() != Graphic3d_TOTM_SPRITE)
434 glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, aParams->IsModulate() ? GL_MODULATE : GL_DECAL);
438 // setup texture filtering and wrapping
439 //if (theTexture->GetParams() != theParams)
440 const GLenum aFilter = (aParams->Filter() == Graphic3d_TOTF_NEAREST) ? GL_NEAREST : GL_LINEAR;
441 const GLenum aWrapMode = aParams->IsRepeat() ? GL_REPEAT : myGlContext->TextureWrapClamp();
442 switch (theTexture->GetTarget())
444 #if !defined(GL_ES_VERSION_2_0)
447 glTexParameteri (GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, aFilter);
448 glTexParameteri (GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, aFilter);
449 glTexParameteri (GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, aWrapMode);
455 GLenum aFilterMin = aFilter;
456 if (theTexture->HasMipmaps())
458 aFilterMin = GL_NEAREST_MIPMAP_NEAREST;
459 if (aParams->Filter() == Graphic3d_TOTF_BILINEAR)
461 aFilterMin = GL_LINEAR_MIPMAP_NEAREST;
463 else if (aParams->Filter() == Graphic3d_TOTF_TRILINEAR)
465 aFilterMin = GL_LINEAR_MIPMAP_LINEAR;
468 if (myGlContext->extAnis)
470 // setup degree of anisotropy filter
471 const GLint aMaxDegree = myGlContext->MaxDegreeOfAnisotropy();
472 switch (aParams->AnisoFilter())
474 case Graphic3d_LOTA_QUALITY:
476 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, aMaxDegree);
479 case Graphic3d_LOTA_MIDDLE:
482 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, (aMaxDegree <= 4) ? 2 : (aMaxDegree / 2));
485 case Graphic3d_LOTA_FAST:
487 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, 2);
490 case Graphic3d_LOTA_OFF:
493 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, 1);
499 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, aFilterMin);
500 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, aFilter);
501 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, aWrapMode);
502 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, aWrapMode);
508 switch (theTexture->GetTarget())
510 #if !defined(GL_ES_VERSION_2_0)
513 if (aParams->GenMode() != Graphic3d_TOTM_MANUAL)
515 glEnable (GL_TEXTURE_GEN_S);
517 glEnable (GL_TEXTURE_1D);
523 #if !defined(GL_ES_VERSION_2_0)
524 if (aParams->GenMode() != Graphic3d_TOTM_MANUAL)
526 glEnable (GL_TEXTURE_GEN_S);
527 glEnable (GL_TEXTURE_GEN_T);
530 glEnable (GL_TEXTURE_2D);
536 #if !defined(GL_ES_VERSION_2_0)
537 glMatrixMode (aMatrixMode); // turn back active matrix
539 theTexture->SetParams (aParams);
542 // =======================================================================
543 // function : EnableTexture
545 // =======================================================================
546 Handle(OpenGl_Texture) OpenGl_Workspace::EnableTexture (const Handle(OpenGl_Texture)& theTexture,
547 const Handle(Graphic3d_TextureParams)& theParams)
549 if (theTexture.IsNull() || !theTexture->IsValid())
551 return DisableTexture();
554 if (myTextureBound == theTexture
555 && (theParams.IsNull() || theParams == theTexture->GetParams()))
558 return myTextureBound;
561 Handle(OpenGl_Texture) aPrevTexture = DisableTexture();
562 myTextureBound = theTexture;
563 myTextureBound->Bind (myGlContext);
564 setTextureParams (myTextureBound, theParams);
569 // =======================================================================
572 // =======================================================================
573 void OpenGl_Workspace::Redraw (const Graphic3d_CView& theCView,
574 const Aspect_CLayer2d& theCUnderLayer,
575 const Aspect_CLayer2d& theCOverLayer)
583 myIsCullingEnabled = theCView.IsCullingEnabled;
585 // release pending GL resources
586 Handle(OpenGl_Context) aGlCtx = GetGlContext();
587 aGlCtx->ReleaseDelayed();
589 // fetch OpenGl context state
590 aGlCtx->FetchState();
592 Tint toSwap = (aGlCtx->IsRender() && !aGlCtx->caps->buffersNoSwap) ? 1 : 0; // swap buffers
593 GLint aViewPortBack[4];
594 OpenGl_FrameBuffer* aFrameBuffer = (OpenGl_FrameBuffer* )theCView.ptrFBO;
595 if (aFrameBuffer != NULL)
597 glGetIntegerv (GL_VIEWPORT, aViewPortBack);
598 aFrameBuffer->SetupViewport (aGlCtx);
599 toSwap = 0; // no need to swap buffers
602 myToRedrawGL = Standard_True;
603 if (theCView.RenderParams.Method == Graphic3d_RM_RAYTRACING
604 && myComputeInitStatus != OpenGl_RT_FAIL)
606 if (UpdateRaytraceGeometry (OpenGl_GUM_CHECK) && myIsRaytraceDataValid)
608 myToRedrawGL = Standard_False;
610 // Only non-raytracable structures should be rendered in OpenGL mode.
611 Handle(OpenGl_RenderFilter) aRenderFilter = GetRenderFilter();
612 myRaytraceFilter->SetPrevRenderFilter (aRenderFilter);
613 SetRenderFilter (myRaytraceFilter);
615 Standard_Integer aSizeX = aFrameBuffer != NULL ? aFrameBuffer->GetVPSizeX() : myWidth;
616 Standard_Integer aSizeY = aFrameBuffer != NULL ? aFrameBuffer->GetVPSizeY() : myHeight;
618 if (myOpenGlFBO.IsNull())
620 myOpenGlFBO = new OpenGl_FrameBuffer();
622 if (myOpenGlFBO->GetVPSizeX() != aSizeX
623 || myOpenGlFBO->GetVPSizeY() != aSizeY)
625 myOpenGlFBO->Init (aGlCtx, aSizeX, aSizeY);
628 // OverLayer and UnderLayer shouldn't be drawn by OpenGL.
629 // They will be drawn during ray-tracing.
630 Aspect_CLayer2d anEmptyCLayer;
631 anEmptyCLayer.ptrLayer = NULL;
633 myOpenGlFBO->BindBuffer (aGlCtx);
634 redraw1 (theCView, anEmptyCLayer, anEmptyCLayer, 0);
635 myOpenGlFBO->UnbindBuffer (aGlCtx);
637 const Standard_Boolean isImmediate = !myView->ImmediateStructures().IsEmpty();
638 Raytrace (theCView, aSizeX, aSizeY, isImmediate ? 0 : toSwap,
639 theCOverLayer, theCUnderLayer, aFrameBuffer);
643 RedrawImmediate (theCView, theCUnderLayer, theCOverLayer, Standard_True);
646 SetRenderFilter (aRenderFilter);
648 theCView.WasRedrawnGL = Standard_False;
654 // draw entire frame using normal OpenGL pipeline
655 if (aFrameBuffer != NULL)
657 aFrameBuffer->BindBuffer (aGlCtx);
660 const Standard_Boolean isImmediate = !myView->ImmediateStructures().IsEmpty();
661 redraw1 (theCView, theCUnderLayer, theCOverLayer, isImmediate ? 0 : toSwap);
664 RedrawImmediate (theCView, theCUnderLayer, theCOverLayer, Standard_True);
667 theCView.WasRedrawnGL = Standard_True;
670 if (aFrameBuffer != NULL)
672 aFrameBuffer->UnbindBuffer (aGlCtx);
673 // move back original viewport
674 glViewport (aViewPortBack[0], aViewPortBack[1], aViewPortBack[2], aViewPortBack[3]);
677 #if defined(_WIN32) && defined(HAVE_VIDEOCAPTURE)
678 if (OpenGl_AVIWriter_AllowWriting (theCView.DefWindow.XWindow))
681 glGetIntegerv (GL_VIEWPORT, params);
682 int nWidth = params[2] & ~0x7;
683 int nHeight = params[3] & ~0x7;
685 const int nBitsPerPixel = 24;
686 GLubyte* aDumpData = new GLubyte[nWidth * nHeight * nBitsPerPixel / 8];
688 glPixelStorei (GL_PACK_ALIGNMENT, 1);
689 glReadPixels (0, 0, nWidth, nHeight, GL_BGR_EXT, GL_UNSIGNED_BYTE, aDumpData);
690 OpenGl_AVIWriter_AVIWriter (aDumpData, nWidth, nHeight, nBitsPerPixel);
695 // reset render mode state
696 aGlCtx->FetchState();
699 // =======================================================================
700 // function : redraw1
702 // =======================================================================
703 void OpenGl_Workspace::redraw1 (const Graphic3d_CView& theCView,
704 const Aspect_CLayer2d& theCUnderLayer,
705 const Aspect_CLayer2d& theCOverLayer,
713 // request reset of material
714 NamedStatus |= OPENGL_NS_RESMAT;
716 GLbitfield toClear = GL_COLOR_BUFFER_BIT;
719 glDepthFunc (GL_LEQUAL);
720 glDepthMask (GL_TRUE);
723 glEnable (GL_DEPTH_TEST);
727 glDisable (GL_DEPTH_TEST);
730 #if !defined(GL_ES_VERSION_2_0)
733 glClearDepthf (1.0f);
735 toClear |= GL_DEPTH_BUFFER_BIT;
739 glDisable (GL_DEPTH_TEST);
744 // set background to black
745 glClearColor (0.0f, 0.0f, 0.0f, 0.0f);
746 toClear |= GL_DEPTH_BUFFER_BIT; //
750 if (NamedStatus & OPENGL_NS_WHITEBACK)
752 // set background to white
753 glClearColor (1.0f, 1.0f, 1.0f, 1.0f);
754 toClear |= GL_DEPTH_BUFFER_BIT;
758 glClearColor (myBgColor.rgb[0], myBgColor.rgb[1], myBgColor.rgb[2], 0.0f);
763 Handle(OpenGl_Workspace) aWS (this);
764 myView->Render (myPrintContext, aWS, theCView, theCUnderLayer, theCOverLayer);
769 GetGlContext()->SwapBuffers();
770 myBackBufferRestored = Standard_False;
771 myIsImmediateDrawn = Standard_False;
776 myBackBufferRestored = Standard_True;//
777 myIsImmediateDrawn = Standard_False;//
781 // =======================================================================
782 // function : copyBackToFront
784 // =======================================================================
785 void OpenGl_Workspace::copyBackToFront()
787 #if !defined(GL_ES_VERSION_2_0)
788 glMatrixMode (GL_PROJECTION);
791 gluOrtho2D (0.0, (GLdouble )myWidth, 0.0, (GLdouble )myHeight);
793 glMatrixMode (GL_MODELVIEW);
799 glDrawBuffer (GL_FRONT);
800 glReadBuffer (GL_BACK);
802 glRasterPos2i (0, 0);
803 glCopyPixels (0, 0, myWidth + 1, myHeight + 1, GL_COLOR);
807 glMatrixMode (GL_PROJECTION);
809 glMatrixMode (GL_MODELVIEW);
812 glDrawBuffer (GL_BACK);
815 myIsImmediateDrawn = Standard_False;
818 // =======================================================================
819 // function : DisplayCallback
821 // =======================================================================
822 void OpenGl_Workspace::DisplayCallback (const Graphic3d_CView& theCView,
823 Standard_Integer theReason)
825 if (theCView.GDisplayCB == NULL)
830 Aspect_GraphicCallbackStruct aCallData;
831 aCallData.reason = theReason;
832 aCallData.glContext = GetGlContext();
833 aCallData.wsID = theCView.WsId;
834 aCallData.viewID = theCView.ViewId;
835 theCView.GDisplayCB (theCView.DefWindow.XWindow, theCView.GClientData, &aCallData);
838 // =======================================================================
839 // function : RedrawImmediate
841 // =======================================================================
842 void OpenGl_Workspace::RedrawImmediate (const Graphic3d_CView& theCView,
843 const Aspect_CLayer2d& theCUnderLayer,
844 const Aspect_CLayer2d& theCOverLayer,
845 const Standard_Boolean theToForce)
852 GLboolean isDoubleBuffer = GL_FALSE;
853 #if !defined(GL_ES_VERSION_2_0)
854 glGetBooleanv (GL_DOUBLEBUFFER, &isDoubleBuffer);
856 if (myView->ImmediateStructures().IsEmpty())
859 || !myIsImmediateDrawn)
861 myIsImmediateDrawn = Standard_False;
865 if (myBackBufferRestored
873 Redraw (theCView, theCUnderLayer, theCOverLayer);
878 if (isDoubleBuffer && myTransientDrawToFront)
880 if (!myBackBufferRestored)
882 Redraw (theCView, theCUnderLayer, theCOverLayer);
886 MakeFrontBufCurrent();
890 myBackBufferRestored = Standard_False;
892 myIsImmediateDrawn = Standard_True;
894 NamedStatus |= OPENGL_NS_IMMEDIATE;
895 ///glDisable (GL_LIGHTING);
896 glDisable (GL_DEPTH_TEST);
898 Handle(OpenGl_Workspace) aWS (this);
899 for (OpenGl_SequenceOfStructure::Iterator anIter (myView->ImmediateStructures());
900 anIter.More(); anIter.Next())
902 const OpenGl_Structure* aStructure = anIter.Value();
903 aStructure->Render (aWS);
906 NamedStatus &= ~OPENGL_NS_IMMEDIATE;
908 if (isDoubleBuffer && myTransientDrawToFront)
911 MakeBackBufCurrent();