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_Workspace.hxx>
18 #include <OpenGl_ArbFBO.hxx>
19 #include <OpenGl_AspectLine.hxx>
20 #include <OpenGl_AspectFace.hxx>
21 #include <OpenGl_AspectMarker.hxx>
22 #include <OpenGl_AspectText.hxx>
23 #include <OpenGl_Context.hxx>
24 #include <OpenGl_Element.hxx>
25 #include <OpenGl_FrameBuffer.hxx>
26 #include <OpenGl_GlCore15.hxx>
27 #include <OpenGl_SceneGeometry.hxx>
28 #include <OpenGl_Structure.hxx>
29 #include <OpenGl_Sampler.hxx>
30 #include <OpenGl_Texture.hxx>
31 #include <OpenGl_View.hxx>
32 #include <OpenGl_Window.hxx>
34 #include <Graphic3d_TextureParams.hxx>
35 #include <Graphic3d_TransformUtils.hxx>
36 #include <NCollection_AlignedAllocator.hxx>
38 IMPLEMENT_STANDARD_RTTIEXT(OpenGl_Workspace,Standard_Transient)
39 IMPLEMENT_STANDARD_RTTIEXT(OpenGl_RaytraceFilter,OpenGl_RenderFilter)
43 /* OCC22216 NOTE: linker dependency can be switched off by undefining macro.
44 Pragma comment for gl2ps.lib is defined only here. */
46 #pragma comment( lib, "gl2ps.lib" )
52 static const OpenGl_Vec4 THE_WHITE_COLOR (1.0f, 1.0f, 1.0f, 1.0f);
53 static const OpenGl_Vec4 THE_BLACK_COLOR (0.0f, 0.0f, 0.0f, 1.0f);
55 static const OpenGl_AspectLine myDefaultAspectLine;
56 static const OpenGl_AspectFace myDefaultAspectFace;
57 static const OpenGl_AspectMarker myDefaultAspectMarker;
58 static const OpenGl_AspectText myDefaultAspectText;
60 static const OpenGl_Matrix myDefaultMatrix =
62 {{ 1.0F, 0.0F, 0.0F, 0.0F },
63 { 0.0F, 1.0F, 0.0F, 0.0F },
64 { 0.0F, 0.0F, 1.0F, 0.0F },
65 { 0.0F, 0.0F, 0.0F, 1.0F }}
70 // =======================================================================
73 // =======================================================================
74 void OpenGl_Material::Init (const Graphic3d_MaterialAspect& theMat,
75 const Quantity_Color& theInteriorColor)
77 const bool isPhysic = theMat.MaterialType (Graphic3d_MATERIAL_PHYSIC) == Standard_True;
80 if (theMat.ReflectionMode (Graphic3d_TOR_AMBIENT))
82 const OpenGl_Vec3& aSrcAmb = isPhysic ? theMat.AmbientColor() : theInteriorColor;
83 Ambient = OpenGl_Vec4 (aSrcAmb * (float )theMat.Ambient(), 1.0f);
87 Ambient = THE_BLACK_COLOR;
90 // diffusion component
91 if (theMat.ReflectionMode (Graphic3d_TOR_DIFFUSE))
93 const OpenGl_Vec3& aSrcDif = isPhysic ? theMat.DiffuseColor() : theInteriorColor;
94 Diffuse = OpenGl_Vec4 (aSrcDif * (float )theMat.Diffuse(), 1.0f);
98 Diffuse = THE_BLACK_COLOR;
101 // specular component
102 if (theMat.ReflectionMode (Graphic3d_TOR_SPECULAR))
104 const OpenGl_Vec3& aSrcSpe = isPhysic ? (const OpenGl_Vec3& )theMat.SpecularColor() : THE_WHITE_COLOR.rgb();
105 Specular = OpenGl_Vec4 (aSrcSpe * (float )theMat.Specular(), 1.0f);
109 Specular = THE_BLACK_COLOR;
112 // emission component
113 if (theMat.ReflectionMode (Graphic3d_TOR_EMISSION))
115 const OpenGl_Vec3& aSrcEms = isPhysic ? theMat.EmissiveColor() : theInteriorColor;
116 Emission = OpenGl_Vec4 (aSrcEms * (float )theMat.Emissive(), 1.0f);
120 Emission = THE_BLACK_COLOR;
123 ChangeShine() = 128.0f * float(theMat.Shininess());
124 ChangeTransparency() = 1.0f - (float )theMat.Transparency();
127 // =======================================================================
128 // function : OpenGl_Workspace
130 // =======================================================================
131 OpenGl_Workspace::OpenGl_Workspace (OpenGl_View* theView, const Handle(OpenGl_Window)& theWindow)
134 myWindow (theWindow),
135 myGlContext (!theWindow.IsNull() ? theWindow->GetGlContext() : NULL),
136 myUseZBuffer (Standard_True),
137 myUseDepthWrite (Standard_True),
139 myAspectLineSet (&myDefaultAspectLine),
140 myAspectFaceSet (&myDefaultAspectFace),
141 myAspectMarkerSet (&myDefaultAspectMarker),
142 myAspectTextSet (&myDefaultAspectText),
143 myAspectFaceAppliedWithHL (false),
145 ViewMatrix_applied (&myDefaultMatrix),
146 StructureMatrix_applied (&myDefaultMatrix),
147 myToAllowFaceCulling (false),
148 myModelViewMatrix (myDefaultMatrix)
150 if (!myGlContext.IsNull() && myGlContext->MakeCurrent())
152 myGlContext->core11fwd->glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
154 // General initialization of the context
155 #if !defined(GL_ES_VERSION_2_0)
156 if (myGlContext->core11 != NULL)
158 // Eviter d'avoir les faces mal orientees en noir.
159 // Pourrait etre utiliser pour detecter les problemes d'orientation
160 glLightModeli ((GLenum )GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
162 // Optimisation pour le Fog et l'antialiasing
163 glHint (GL_FOG_HINT, GL_FASTEST);
164 glHint (GL_POINT_SMOOTH_HINT, GL_FASTEST);
167 glHint (GL_LINE_SMOOTH_HINT, GL_FASTEST);
168 glHint (GL_POLYGON_SMOOTH_HINT, GL_FASTEST);
172 myDefaultCappingAlgoFilter = new OpenGl_CappingAlgoFilter();
174 myNoneCulling .Aspect()->SetSuppressBackFaces (false);
175 myNoneCulling .Aspect()->SetDrawEdges (false);
176 myFrontCulling.Aspect()->SetSuppressBackFaces (true);
177 myFrontCulling.Aspect()->SetDrawEdges (false);
180 // =======================================================================
181 // function : Activate
183 // =======================================================================
184 Standard_Boolean OpenGl_Workspace::Activate()
186 if (myWindow.IsNull() || !myWindow->Activate())
188 return Standard_False;
191 ViewMatrix_applied = &myDefaultMatrix;
192 StructureMatrix_applied = &myDefaultMatrix;
194 ResetAppliedAspect();
196 return Standard_True;
199 //=======================================================================
200 //function : ResetAppliedAspect
201 //purpose : Sets default values of GL parameters in accordance with default aspects
202 //=======================================================================
203 void OpenGl_Workspace::ResetAppliedAspect()
205 myGlContext->BindDefaultVao();
207 NamedStatus = !myTextureBound.IsNull() ? OPENGL_NS_TEXTURE : 0;
208 myHighlightStyle.Nullify();
209 myToAllowFaceCulling = false;
210 myAspectLineSet = &myDefaultAspectLine;
211 myAspectFaceSet = &myDefaultAspectFace;
212 myAspectFaceApplied.Nullify();
213 myAspectMarkerSet = &myDefaultAspectMarker;
214 myAspectMarkerApplied.Nullify();
215 myAspectTextSet = &myDefaultAspectText;
216 myPolygonOffsetApplied= Graphic3d_PolygonOffset();
223 myGlContext->SetTypeOfLine (myDefaultAspectLine.Aspect()->Type());
224 myGlContext->SetLineWidth (myDefaultAspectLine.Aspect()->Width());
227 // =======================================================================
228 // function : DisableTexture
230 // =======================================================================
231 Handle(OpenGl_Texture) OpenGl_Workspace::DisableTexture()
233 if (myTextureBound.IsNull())
235 return myTextureBound;
238 const Handle(OpenGl_Sampler)& aSampler = myGlContext->TextureSampler();
239 if (!aSampler.IsNull())
241 aSampler->Unbind (*myGlContext);
244 #if !defined(GL_ES_VERSION_2_0)
245 // reset texture matrix because some code may expect it is identity
246 if (myGlContext->core11 != NULL)
248 GLint aMatrixMode = GL_TEXTURE;
249 glGetIntegerv (GL_MATRIX_MODE, &aMatrixMode);
250 glMatrixMode (GL_TEXTURE);
252 glMatrixMode (aMatrixMode);
256 myTextureBound->Unbind (myGlContext);
257 switch (myTextureBound->GetTarget())
259 #if !defined(GL_ES_VERSION_2_0)
262 if (myGlContext->core11 != NULL)
264 if (myTextureBound->GetParams()->GenMode() != GL_NONE)
266 glDisable (GL_TEXTURE_GEN_S);
268 glDisable (GL_TEXTURE_1D);
275 #if !defined(GL_ES_VERSION_2_0)
276 if (myGlContext->core11 != NULL)
278 if (myTextureBound->GetParams()->GenMode() != GL_NONE)
280 glDisable (GL_TEXTURE_GEN_S);
281 glDisable (GL_TEXTURE_GEN_T);
282 if (myTextureBound->GetParams()->GenMode() == Graphic3d_TOTM_SPRITE)
284 glDisable (GL_POINT_SPRITE);
287 glDisable (GL_TEXTURE_2D);
295 Handle(OpenGl_Texture) aPrevTexture = myTextureBound;
296 myTextureBound.Nullify();
300 // =======================================================================
301 // function : setTextureParams
303 // =======================================================================
304 void OpenGl_Workspace::setTextureParams (Handle(OpenGl_Texture)& theTexture,
305 const Handle(Graphic3d_TextureParams)& theParams)
307 const Handle(Graphic3d_TextureParams)& aParams = theParams.IsNull() ? theTexture->GetParams() : theParams;
308 if (aParams.IsNull())
313 #if !defined(GL_ES_VERSION_2_0)
314 if (myGlContext->core11 != NULL)
316 GLint anEnvMode = GL_MODULATE; // lighting mode
317 if (!aParams->IsModulate())
319 anEnvMode = GL_DECAL;
320 if (theTexture->GetFormat() == GL_ALPHA
321 || theTexture->GetFormat() == GL_LUMINANCE)
323 anEnvMode = GL_REPLACE;
327 // setup generation of texture coordinates
328 switch (aParams->GenMode())
330 case Graphic3d_TOTM_OBJECT:
332 glTexGeni (GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
333 glTexGenfv (GL_S, GL_OBJECT_PLANE, aParams->GenPlaneS().GetData());
334 if (theTexture->GetTarget() != GL_TEXTURE_1D)
336 glTexGeni (GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
337 glTexGenfv (GL_T, GL_OBJECT_PLANE, aParams->GenPlaneT().GetData());
341 case Graphic3d_TOTM_SPHERE:
343 glTexGeni (GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
344 if (theTexture->GetTarget() != GL_TEXTURE_1D)
346 glTexGeni (GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
350 case Graphic3d_TOTM_EYE:
352 myGlContext->WorldViewState.Push();
354 myGlContext->WorldViewState.SetIdentity();
355 myGlContext->ApplyWorldViewMatrix();
357 glTexGeni (GL_S, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
358 glTexGenfv (GL_S, GL_EYE_PLANE, aParams->GenPlaneS().GetData());
360 if (theTexture->GetTarget() != GL_TEXTURE_1D)
362 glTexGeni (GL_T, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
363 glTexGenfv (GL_T, GL_EYE_PLANE, aParams->GenPlaneT().GetData());
366 myGlContext->WorldViewState.Pop();
370 case Graphic3d_TOTM_SPRITE:
372 if (myGlContext->core20fwd != NULL)
374 glEnable (GL_POINT_SPRITE);
375 glTexEnvi (GL_POINT_SPRITE, GL_COORD_REPLACE, GL_TRUE);
376 anEnvMode = GL_REPLACE;
380 case Graphic3d_TOTM_MANUAL:
385 glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, anEnvMode);
389 // get active sampler object to override default texture parameters
390 const Handle(OpenGl_Sampler)& aSampler = myGlContext->TextureSampler();
392 // setup texture filtering and wrapping
393 //if (theTexture->GetParams() != theParams)
394 const GLenum aFilter = (aParams->Filter() == Graphic3d_TOTF_NEAREST) ? GL_NEAREST : GL_LINEAR;
395 const GLenum aWrapMode = aParams->IsRepeat() ? GL_REPEAT : myGlContext->TextureWrapClamp();
396 switch (theTexture->GetTarget())
398 #if !defined(GL_ES_VERSION_2_0)
401 if (aSampler.IsNull() || !aSampler->IsValid())
403 glTexParameteri (GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, aFilter);
404 glTexParameteri (GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, aFilter);
405 glTexParameteri (GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, aWrapMode);
409 aSampler->SetParameter (*myGlContext, GL_TEXTURE_MAG_FILTER, aFilter);
410 aSampler->SetParameter (*myGlContext, GL_TEXTURE_MIN_FILTER, aFilter);
411 aSampler->SetParameter (*myGlContext, GL_TEXTURE_WRAP_S, aWrapMode);
419 GLenum aFilterMin = aFilter;
420 if (theTexture->HasMipmaps())
422 aFilterMin = GL_NEAREST_MIPMAP_NEAREST;
423 if (aParams->Filter() == Graphic3d_TOTF_BILINEAR)
425 aFilterMin = GL_LINEAR_MIPMAP_NEAREST;
427 else if (aParams->Filter() == Graphic3d_TOTF_TRILINEAR)
429 aFilterMin = GL_LINEAR_MIPMAP_LINEAR;
432 if (myGlContext->extAnis)
434 // setup degree of anisotropy filter
435 const GLint aMaxDegree = myGlContext->MaxDegreeOfAnisotropy();
437 switch (aParams->AnisoFilter())
439 case Graphic3d_LOTA_QUALITY:
441 aDegree = aMaxDegree;
444 case Graphic3d_LOTA_MIDDLE:
446 aDegree = (aMaxDegree <= 4) ? 2 : (aMaxDegree / 2);
449 case Graphic3d_LOTA_FAST:
454 case Graphic3d_LOTA_OFF:
462 if (aSampler.IsNull() || !aSampler->IsValid())
464 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, aDegree);
468 aSampler->SetParameter (*myGlContext, GL_TEXTURE_MAX_ANISOTROPY_EXT, aDegree);
473 if (aSampler.IsNull() || !aSampler->IsValid())
475 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, aFilterMin);
476 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, aFilter);
477 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, aWrapMode);
478 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, aWrapMode);
482 aSampler->SetParameter (*myGlContext, GL_TEXTURE_MIN_FILTER, aFilterMin);
483 aSampler->SetParameter (*myGlContext, GL_TEXTURE_MAG_FILTER, aFilter);
484 aSampler->SetParameter (*myGlContext, GL_TEXTURE_WRAP_S, aWrapMode);
485 aSampler->SetParameter (*myGlContext, GL_TEXTURE_WRAP_T, aWrapMode);
493 switch (theTexture->GetTarget())
495 #if !defined(GL_ES_VERSION_2_0)
498 if (myGlContext->core11 != NULL)
500 if (aParams->GenMode() != Graphic3d_TOTM_MANUAL)
502 glEnable (GL_TEXTURE_GEN_S);
504 glEnable (GL_TEXTURE_1D);
511 #if !defined(GL_ES_VERSION_2_0)
512 if (myGlContext->core11 != NULL)
514 if (aParams->GenMode() != Graphic3d_TOTM_MANUAL)
516 glEnable (GL_TEXTURE_GEN_S);
517 glEnable (GL_TEXTURE_GEN_T);
519 glEnable (GL_TEXTURE_2D);
527 theTexture->SetParams (aParams);
530 // =======================================================================
531 // function : EnableTexture
533 // =======================================================================
534 Handle(OpenGl_Texture) OpenGl_Workspace::EnableTexture (const Handle(OpenGl_Texture)& theTexture,
535 const Handle(Graphic3d_TextureParams)& theParams)
537 if (theTexture.IsNull() || !theTexture->IsValid())
539 return DisableTexture();
542 if (myTextureBound == theTexture
543 && (theParams.IsNull() || theParams == theTexture->GetParams()))
546 return myTextureBound;
549 Handle(OpenGl_Texture) aPrevTexture = DisableTexture();
550 myTextureBound = theTexture;
551 myTextureBound->Bind (myGlContext);
552 setTextureParams (myTextureBound, theParams);
554 // If custom sampler object is available it will be
555 // used for overriding default texture parameters
556 const Handle(OpenGl_Sampler)& aSampler = myGlContext->TextureSampler();
558 if (!aSampler.IsNull() && aSampler->IsValid())
560 aSampler->Bind (*myGlContext);
566 // =======================================================================
567 // function : updateMaterial
569 // =======================================================================
570 void OpenGl_Workspace::updateMaterial (const int theFlag)
572 // Case of hidden line
573 if (myAspectFaceSet->Aspect()->InteriorStyle() == Aspect_IS_HIDDENLINE)
575 // copy all values including line edge aspect
576 *myAspectFaceHl.Aspect().operator->() = *myAspectFaceSet->Aspect();
577 myAspectFaceHl.SetAspectEdge (myAspectFaceSet->AspectEdge());
578 myAspectFaceHl.Aspect()->SetInteriorColor (myView->BackgroundColor().GetRGB());
579 myAspectFaceHl.SetNoLighting (true);
580 myAspectFaceSet = &myAspectFaceHl;
584 const Graphic3d_MaterialAspect* aSrcMat = &myAspectFaceSet->Aspect()->FrontMaterial();
585 const Quantity_Color* aSrcIntColor = &myAspectFaceSet->Aspect()->InteriorColor();
586 GLenum aFace = GL_FRONT_AND_BACK;
587 if (theFlag == TEL_BACK_MATERIAL)
590 aSrcMat = &myAspectFaceSet->Aspect()->BackMaterial();
591 aSrcIntColor = &myAspectFaceSet->Aspect()->BackInteriorColor();
593 else if (myAspectFaceSet->Aspect()->Distinguish()
594 && !(NamedStatus & OPENGL_NS_RESMAT))
599 myMatTmp.Init (*aSrcMat, *aSrcIntColor);
600 if (!myHighlightStyle.IsNull())
602 myMatTmp.SetColor (myHighlightStyle->ColorRGBA());
605 // handling transparency
606 if (NamedStatus & OPENGL_NS_2NDPASSDO)
609 myMatTmp.Diffuse.a() = aSrcMat->EnvReflexion();
613 if (aSrcMat->EnvReflexion() != 0.0f)
615 // if the material reflects the environment scene, the second pass is needed
616 NamedStatus |= OPENGL_NS_2NDPASSNEED;
619 const float aTransp = (float )aSrcMat->Transparency();
622 // render transparent
623 myMatTmp.Diffuse.a() = 1.0f - aTransp;
624 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
628 glDepthMask (GL_FALSE);
634 glBlendFunc (GL_ONE, GL_ZERO);
635 glDisable (GL_BLEND);
638 glDepthMask (GL_TRUE);
643 // do not update material properties in case of zero reflection mode,
644 // because GL lighting will be disabled by OpenGl_PrimitiveArray::DrawArray() anyway.
645 if (myAspectFaceSet->IsNoLighting())
651 if (NamedStatus & OPENGL_NS_RESMAT)
653 #if !defined(GL_ES_VERSION_2_0)
654 if (myGlContext->core11 != NULL)
656 myGlContext->core11->glMaterialfv (aFace, GL_AMBIENT, myMatTmp.Ambient.GetData());
657 myGlContext->core11->glMaterialfv (aFace, GL_DIFFUSE, myMatTmp.Diffuse.GetData());
658 myGlContext->core11->glMaterialfv (aFace, GL_SPECULAR, myMatTmp.Specular.GetData());
659 myGlContext->core11->glMaterialfv (aFace, GL_EMISSION, myMatTmp.Emission.GetData());
660 myGlContext->core11->glMaterialf (aFace, GL_SHININESS, myMatTmp.Shine());
664 if (theFlag == TEL_FRONT_MATERIAL)
666 myMatFront = myMatTmp;
667 myMatBack = myMatTmp;
671 myMatBack = myMatTmp;
674 NamedStatus &= ~OPENGL_NS_RESMAT;
679 OpenGl_Material& anOld = (theFlag == TEL_FRONT_MATERIAL)
682 #if !defined(GL_ES_VERSION_2_0)
683 if (myGlContext->core11 != NULL)
685 if (myMatTmp.Ambient.r() != anOld.Ambient.r()
686 || myMatTmp.Ambient.g() != anOld.Ambient.g()
687 || myMatTmp.Ambient.b() != anOld.Ambient.b())
689 myGlContext->core11->glMaterialfv (aFace, GL_AMBIENT, myMatTmp.Ambient.GetData());
691 if (myMatTmp.Diffuse.r() != anOld.Diffuse.r()
692 || myMatTmp.Diffuse.g() != anOld.Diffuse.g()
693 || myMatTmp.Diffuse.b() != anOld.Diffuse.b()
694 || fabs (myMatTmp.Diffuse.a() - anOld.Diffuse.a()) > 0.01f)
696 myGlContext->core11->glMaterialfv (aFace, GL_DIFFUSE, myMatTmp.Diffuse.GetData());
698 if (myMatTmp.Specular.r() != anOld.Specular.r()
699 || myMatTmp.Specular.g() != anOld.Specular.g()
700 || myMatTmp.Specular.b() != anOld.Specular.b())
702 myGlContext->core11->glMaterialfv (aFace, GL_SPECULAR, myMatTmp.Specular.GetData());
704 if (myMatTmp.Emission.r() != anOld.Emission.r()
705 || myMatTmp.Emission.g() != anOld.Emission.g()
706 || myMatTmp.Emission.b() != anOld.Emission.b())
708 myGlContext->core11->glMaterialfv (aFace, GL_EMISSION, myMatTmp.Emission.GetData());
710 if (myMatTmp.Shine() != anOld.Shine())
712 myGlContext->core11->glMaterialf (aFace, GL_SHININESS, myMatTmp.Shine());
717 if (aFace == GL_FRONT_AND_BACK)
719 myMatBack = myMatTmp;
723 // =======================================================================
724 // function : SetAspectLine
726 // =======================================================================
727 const OpenGl_AspectLine* OpenGl_Workspace::SetAspectLine (const OpenGl_AspectLine* theAspect)
729 const OpenGl_AspectLine* aPrevAspectLine = myAspectLineSet;
730 myAspectLineSet = theAspect;
731 return aPrevAspectLine;
734 // =======================================================================
735 // function : SetAspectFace
737 // =======================================================================
738 const OpenGl_AspectFace * OpenGl_Workspace::SetAspectFace (const OpenGl_AspectFace* theAspect)
740 const OpenGl_AspectFace* aPrevAspectFace = myAspectFaceSet;
741 myAspectFaceSet = theAspect;
742 return aPrevAspectFace;
745 // =======================================================================
746 // function : SetAspectMarker
748 // =======================================================================
749 const OpenGl_AspectMarker* OpenGl_Workspace::SetAspectMarker (const OpenGl_AspectMarker* theAspect)
751 const OpenGl_AspectMarker* aPrevAspectMarker = myAspectMarkerSet;
752 myAspectMarkerSet = theAspect;
753 return aPrevAspectMarker;
756 // =======================================================================
757 // function : SetAspectText
759 // =======================================================================
760 const OpenGl_AspectText * OpenGl_Workspace::SetAspectText (const OpenGl_AspectText* theAspect)
762 const OpenGl_AspectText* aPrevAspectText = myAspectTextSet;
763 myAspectTextSet = theAspect;
764 return aPrevAspectText;
767 // =======================================================================
768 // function : ApplyAspectFace
770 // =======================================================================
771 const OpenGl_AspectFace* OpenGl_Workspace::ApplyAspectFace()
773 if (myView->BackfacingModel() == Graphic3d_TOBM_AUTOMATIC)
775 // manage back face culling mode, disable culling when clipping is enabled
776 bool toSuppressBackFaces = myToAllowFaceCulling
777 && myAspectFaceSet->Aspect()->ToSuppressBackFaces();
778 if (toSuppressBackFaces)
780 if (myGlContext->Clipping().IsClippingOrCappingOn()
781 || myAspectFaceSet->Aspect()->InteriorStyle() == Aspect_IS_HATCH)
783 toSuppressBackFaces = false;
786 if (toSuppressBackFaces)
788 if (!(NamedStatus & OPENGL_NS_2NDPASSDO)
789 && (float )myAspectFaceSet->Aspect()->FrontMaterial().Transparency() != 0.0f)
791 // disable culling in case of translucent shading aspect
792 toSuppressBackFaces = false;
795 myGlContext->SetCullBackFaces (toSuppressBackFaces);
798 if (myAspectFaceSet->Aspect() == myAspectFaceApplied
799 && !myHighlightStyle.IsNull() == myAspectFaceAppliedWithHL)
801 return myAspectFaceSet;
803 myAspectFaceAppliedWithHL = !myHighlightStyle.IsNull();
805 #if !defined(GL_ES_VERSION_2_0)
806 const Aspect_InteriorStyle anIntstyle = myAspectFaceSet->Aspect()->InteriorStyle();
807 if (myAspectFaceApplied.IsNull()
808 || myAspectFaceApplied->InteriorStyle() != anIntstyle)
812 case Aspect_IS_EMPTY:
813 case Aspect_IS_HOLLOW:
815 myGlContext->SetPolygonMode (GL_LINE);
818 case Aspect_IS_HATCH:
820 myGlContext->SetPolygonMode (GL_FILL);
821 myGlContext->SetPolygonHatchEnabled (true);
824 case Aspect_IS_SOLID:
825 case Aspect_IS_HIDDENLINE:
827 myGlContext->SetPolygonMode (GL_FILL);
828 myGlContext->SetPolygonHatchEnabled (false);
831 case Aspect_IS_POINT:
833 myGlContext->SetPolygonMode (GL_POINT);
839 if (anIntstyle == Aspect_IS_HATCH)
841 myGlContext->SetPolygonHatchStyle (myAspectFaceSet->Aspect()->HatchStyle());
845 // Aspect_POM_None means: do not change current settings
846 if ((myAspectFaceSet->Aspect()->PolygonOffset().Mode & Aspect_POM_None) != Aspect_POM_None)
848 if (myPolygonOffsetApplied.Mode != myAspectFaceSet->Aspect()->PolygonOffset().Mode
849 || myPolygonOffsetApplied.Factor != myAspectFaceSet->Aspect()->PolygonOffset().Factor
850 || myPolygonOffsetApplied.Units != myAspectFaceSet->Aspect()->PolygonOffset().Units)
852 SetPolygonOffset (myAspectFaceSet->Aspect()->PolygonOffset());
856 updateMaterial (TEL_FRONT_MATERIAL);
857 if (myAspectFaceSet->Aspect()->Distinguish())
859 updateMaterial (TEL_BACK_MATERIAL);
862 if (myAspectFaceSet->Aspect()->ToMapTexture())
864 EnableTexture (myAspectFaceSet->TextureRes (myGlContext),
865 myAspectFaceSet->TextureParams());
869 if (!myEnvironmentTexture.IsNull())
871 EnableTexture (myEnvironmentTexture,
872 myEnvironmentTexture->GetParams());
880 myAspectFaceApplied = myAspectFaceSet->Aspect();
881 return myAspectFaceSet;
884 //=======================================================================
885 //function : SetPolygonOffset
887 //=======================================================================
888 void OpenGl_Workspace::SetPolygonOffset (const Graphic3d_PolygonOffset& theParams)
890 myPolygonOffsetApplied = theParams;
892 if ((theParams.Mode & Aspect_POM_Fill) == Aspect_POM_Fill)
894 glEnable (GL_POLYGON_OFFSET_FILL);
898 glDisable (GL_POLYGON_OFFSET_FILL);
901 #if !defined(GL_ES_VERSION_2_0)
902 if ((theParams.Mode & Aspect_POM_Line) == Aspect_POM_Line)
904 glEnable (GL_POLYGON_OFFSET_LINE);
908 glDisable (GL_POLYGON_OFFSET_LINE);
911 if ((theParams.Mode & Aspect_POM_Point) == Aspect_POM_Point)
913 glEnable (GL_POLYGON_OFFSET_POINT);
917 glDisable (GL_POLYGON_OFFSET_POINT);
920 glPolygonOffset (theParams.Factor, theParams.Units);
923 // =======================================================================
924 // function : ApplyAspectMarker
926 // =======================================================================
927 const OpenGl_AspectMarker* OpenGl_Workspace::ApplyAspectMarker()
929 if (myAspectMarkerSet->Aspect() != myAspectMarkerApplied)
931 if (myAspectMarkerApplied.IsNull()
932 || (myAspectMarkerSet->Aspect()->Scale() != myAspectMarkerApplied->Scale()))
934 #if !defined(GL_ES_VERSION_2_0)
935 glPointSize (myAspectMarkerSet->Aspect()->Scale());
937 gl2psPointSize (myAspectMarkerSet->Aspect()->Scale());
941 myAspectMarkerApplied = myAspectMarkerSet->Aspect();
943 return myAspectMarkerSet;
946 // =======================================================================
949 // =======================================================================
950 Standard_Integer OpenGl_Workspace::Width() const
952 return !myView->GlWindow().IsNull() ? myView->GlWindow()->Width() : 0;
955 // =======================================================================
958 // =======================================================================
959 Standard_Integer OpenGl_Workspace::Height() const
961 return !myView->GlWindow().IsNull() ? myView->GlWindow()->Height() : 0;
964 // =======================================================================
965 // function : IsCullingEnabled
967 // =======================================================================
968 Standard_Boolean OpenGl_Workspace::IsCullingEnabled() const
970 return myView->IsCullingEnabled();
973 // =======================================================================
974 // function : FBOCreate
976 // =======================================================================
977 Handle(OpenGl_FrameBuffer) OpenGl_Workspace::FBOCreate (const Standard_Integer theWidth,
978 const Standard_Integer theHeight)
980 // activate OpenGL context
982 return Handle(OpenGl_FrameBuffer)();
987 const Handle(OpenGl_Context)& aCtx = GetGlContext();
988 Handle(OpenGl_FrameBuffer) aFrameBuffer = new OpenGl_FrameBuffer();
989 if (!aFrameBuffer->Init (aCtx, theWidth, theHeight, GL_RGBA8, GL_DEPTH24_STENCIL8, 0))
991 aFrameBuffer->Release (aCtx.operator->());
992 return Handle(OpenGl_FrameBuffer)();
997 // =======================================================================
998 // function : FBORelease
1000 // =======================================================================
1001 void OpenGl_Workspace::FBORelease (Handle(OpenGl_FrameBuffer)& theFbo)
1003 // activate OpenGL context
1010 theFbo->Release (GetGlContext().operator->());
1014 // =======================================================================
1015 // function : getAligned
1017 // =======================================================================
1018 inline Standard_Size getAligned (const Standard_Size theNumber,
1019 const Standard_Size theAlignment)
1021 return theNumber + theAlignment - 1 - (theNumber - 1) % theAlignment;
1024 template<typename T>
1025 inline void convertRowFromRgba (T* theRgbRow,
1026 const Image_ColorRGBA* theRgbaRow,
1027 const Standard_Size theWidth)
1029 for (Standard_Size aCol = 0; aCol < theWidth; ++aCol)
1031 const Image_ColorRGBA& anRgba = theRgbaRow[aCol];
1032 T& anRgb = theRgbRow[aCol];
1033 anRgb.r() = anRgba.r();
1034 anRgb.g() = anRgba.g();
1035 anRgb.b() = anRgba.b();
1039 // =======================================================================
1040 // function : BufferDump
1042 // =======================================================================
1043 Standard_Boolean OpenGl_Workspace::BufferDump (const Handle(OpenGl_FrameBuffer)& theFbo,
1044 Image_PixMap& theImage,
1045 const Graphic3d_BufferType& theBufferType)
1047 if (theImage.IsEmpty()
1050 return Standard_False;
1055 bool toSwapRgbaBgra = false;
1056 bool toConvRgba2Rgb = false;
1057 switch (theImage.Format())
1059 #if !defined(GL_ES_VERSION_2_0)
1060 case Image_PixMap::ImgGray:
1061 aFormat = GL_DEPTH_COMPONENT;
1062 aType = GL_UNSIGNED_BYTE;
1064 case Image_PixMap::ImgGrayF:
1065 aFormat = GL_DEPTH_COMPONENT;
1068 case Image_PixMap::ImgRGB:
1070 aType = GL_UNSIGNED_BYTE;
1072 case Image_PixMap::ImgBGR:
1074 aType = GL_UNSIGNED_BYTE;
1076 case Image_PixMap::ImgBGRA:
1077 case Image_PixMap::ImgBGR32:
1079 aType = GL_UNSIGNED_BYTE;
1081 case Image_PixMap::ImgBGRF:
1085 case Image_PixMap::ImgBGRAF:
1090 case Image_PixMap::ImgGray:
1091 case Image_PixMap::ImgGrayF:
1092 case Image_PixMap::ImgBGRF:
1093 case Image_PixMap::ImgBGRAF:
1094 return Standard_False;
1095 case Image_PixMap::ImgBGRA:
1096 case Image_PixMap::ImgBGR32:
1098 aType = GL_UNSIGNED_BYTE;
1099 toSwapRgbaBgra = true;
1101 case Image_PixMap::ImgBGR:
1102 case Image_PixMap::ImgRGB:
1104 aType = GL_UNSIGNED_BYTE;
1105 toConvRgba2Rgb = true;
1108 case Image_PixMap::ImgRGBA:
1109 case Image_PixMap::ImgRGB32:
1111 aType = GL_UNSIGNED_BYTE;
1113 case Image_PixMap::ImgRGBF:
1117 case Image_PixMap::ImgRGBAF:
1121 case Image_PixMap::ImgAlpha:
1122 case Image_PixMap::ImgAlphaF:
1123 return Standard_False; // GL_ALPHA is no more supported in core context
1124 case Image_PixMap::ImgUNKNOWN:
1125 return Standard_False;
1130 return Standard_False;
1133 #if !defined(GL_ES_VERSION_2_0)
1134 GLint aReadBufferPrev = GL_BACK;
1135 if (theBufferType == Graphic3d_BT_Depth
1136 && aFormat != GL_DEPTH_COMPONENT)
1138 return Standard_False;
1141 (void )theBufferType;
1145 if (!theFbo.IsNull() && theFbo->IsValid())
1147 theFbo->BindBuffer (GetGlContext());
1151 #if !defined(GL_ES_VERSION_2_0)
1152 glGetIntegerv (GL_READ_BUFFER, &aReadBufferPrev);
1153 GLint aDrawBufferPrev = GL_BACK;
1154 glGetIntegerv (GL_DRAW_BUFFER, &aDrawBufferPrev);
1155 glReadBuffer (aDrawBufferPrev);
1160 const GLint anAligment = Min (GLint(theImage.MaxRowAligmentBytes()), 8); // limit to 8 bytes for OpenGL
1161 glPixelStorei (GL_PACK_ALIGNMENT, anAligment);
1162 bool isBatchCopy = !theImage.IsTopDown();
1164 const GLint anExtraBytes = GLint(theImage.RowExtraBytes());
1165 GLint aPixelsWidth = GLint(theImage.SizeRowBytes() / theImage.SizePixelBytes());
1166 Standard_Size aSizeRowBytesEstim = getAligned (theImage.SizePixelBytes() * aPixelsWidth, anAligment);
1167 if (anExtraBytes < anAligment)
1171 else if (aSizeRowBytesEstim != theImage.SizeRowBytes())
1174 isBatchCopy = false;
1176 #if !defined(GL_ES_VERSION_2_0)
1177 glPixelStorei (GL_PACK_ROW_LENGTH, aPixelsWidth);
1179 if (aPixelsWidth != 0)
1181 isBatchCopy = false;
1186 Handle(NCollection_BaseAllocator) anAlloc = new NCollection_AlignedAllocator (16);
1187 const Standard_Size aRowSize = theImage.SizeX() * 4;
1188 NCollection_Buffer aRowBuffer (anAlloc);
1189 if (!aRowBuffer.Allocate (aRowSize))
1191 return Standard_False;
1194 for (Standard_Size aRow = 0; aRow < theImage.SizeY(); ++aRow)
1196 // Image_PixMap rows indexation always starts from the upper corner
1197 // while order in memory depends on the flag and processed by ChangeRow() method
1198 glReadPixels (0, GLint(theImage.SizeY() - aRow - 1), GLsizei (theImage.SizeX()), 1, aFormat, aType, aRowBuffer.ChangeData());
1199 const Image_ColorRGBA* aRowDataRgba = (const Image_ColorRGBA* )aRowBuffer.Data();
1200 if (theImage.Format() == Image_PixMap::ImgBGR)
1202 convertRowFromRgba ((Image_ColorBGR* )theImage.ChangeRow (aRow), aRowDataRgba, theImage.SizeX());
1206 convertRowFromRgba ((Image_ColorRGB* )theImage.ChangeRow (aRow), aRowDataRgba, theImage.SizeX());
1210 else if (!isBatchCopy)
1213 for (Standard_Size aRow = 0; aRow < theImage.SizeY(); ++aRow)
1215 // Image_PixMap rows indexation always starts from the upper corner
1216 // while order in memory depends on the flag and processed by ChangeRow() method
1217 glReadPixels (0, GLint(theImage.SizeY() - aRow - 1), GLsizei (theImage.SizeX()), 1, aFormat, aType, theImage.ChangeRow (aRow));
1222 glReadPixels (0, 0, GLsizei (theImage.SizeX()), GLsizei (theImage.SizeY()), aFormat, aType, theImage.ChangeData());
1224 const bool hasErrors = myGlContext->ResetErrors (true);
1226 glPixelStorei (GL_PACK_ALIGNMENT, 1);
1227 #if !defined(GL_ES_VERSION_2_0)
1228 glPixelStorei (GL_PACK_ROW_LENGTH, 0);
1231 if (!theFbo.IsNull() && theFbo->IsValid())
1233 theFbo->UnbindBuffer (GetGlContext());
1237 #if !defined(GL_ES_VERSION_2_0)
1238 glReadBuffer (aReadBufferPrev);
1244 Image_PixMap::SwapRgbaBgra (theImage);
1250 // =======================================================================
1251 // function : CanRender
1253 // =======================================================================
1254 Standard_Boolean OpenGl_RaytraceFilter::CanRender (const OpenGl_Element* theElement)
1256 Standard_Boolean aPrevFilterResult = Standard_True;
1257 if (!myPrevRenderFilter.IsNull())
1259 aPrevFilterResult = myPrevRenderFilter->CanRender (theElement);
1261 return aPrevFilterResult &&
1262 !OpenGl_Raytrace::IsRaytracedElement (theElement);