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>
37 IMPLEMENT_STANDARD_RTTIEXT(OpenGl_Workspace,Standard_Transient)
38 IMPLEMENT_STANDARD_RTTIEXT(OpenGl_RaytraceFilter,OpenGl_RenderFilter)
42 /* OCC22216 NOTE: linker dependency can be switched off by undefining macro.
43 Pragma comment for gl2ps.lib is defined only here. */
45 #pragma comment( lib, "gl2ps.lib" )
51 static const OpenGl_Vec4 THE_WHITE_COLOR (1.0f, 1.0f, 1.0f, 1.0f);
52 static const OpenGl_Vec4 THE_BLACK_COLOR (0.0f, 0.0f, 0.0f, 1.0f);
54 static const OpenGl_AspectLine myDefaultAspectLine;
55 static const OpenGl_AspectFace myDefaultAspectFace;
56 static const OpenGl_AspectMarker myDefaultAspectMarker;
57 static const OpenGl_AspectText myDefaultAspectText;
59 static const OpenGl_Matrix myDefaultMatrix =
61 {{ 1.0F, 0.0F, 0.0F, 0.0F },
62 { 0.0F, 1.0F, 0.0F, 0.0F },
63 { 0.0F, 0.0F, 1.0F, 0.0F },
64 { 0.0F, 0.0F, 0.0F, 1.0F }}
69 // =======================================================================
72 // =======================================================================
73 void OpenGl_Material::Init (const Graphic3d_MaterialAspect& theMat,
74 const Quantity_Color& theInteriorColor)
76 const bool isPhysic = theMat.MaterialType (Graphic3d_MATERIAL_PHYSIC) == Standard_True;
79 if (theMat.ReflectionMode (Graphic3d_TOR_AMBIENT))
81 const OpenGl_Vec3& aSrcAmb = isPhysic ? theMat.AmbientColor() : theInteriorColor;
82 Ambient = OpenGl_Vec4 (aSrcAmb * (float )theMat.Ambient(), 1.0f);
86 Ambient = THE_BLACK_COLOR;
89 // diffusion component
90 if (theMat.ReflectionMode (Graphic3d_TOR_DIFFUSE))
92 const OpenGl_Vec3& aSrcDif = isPhysic ? theMat.DiffuseColor() : theInteriorColor;
93 Diffuse = OpenGl_Vec4 (aSrcDif * (float )theMat.Diffuse(), 1.0f);
97 Diffuse = THE_BLACK_COLOR;
100 // specular component
101 if (theMat.ReflectionMode (Graphic3d_TOR_SPECULAR))
103 const OpenGl_Vec3& aSrcSpe = isPhysic ? (const OpenGl_Vec3& )theMat.SpecularColor() : THE_WHITE_COLOR.rgb();
104 Specular = OpenGl_Vec4 (aSrcSpe * (float )theMat.Specular(), 1.0f);
108 Specular = THE_BLACK_COLOR;
111 // emission component
112 if (theMat.ReflectionMode (Graphic3d_TOR_EMISSION))
114 const OpenGl_Vec3& aSrcEms = isPhysic ? theMat.EmissiveColor() : theInteriorColor;
115 Emission = OpenGl_Vec4 (aSrcEms * (float )theMat.Emissive(), 1.0f);
119 Emission = THE_BLACK_COLOR;
122 ChangeShine() = 128.0f * float(theMat.Shininess());
123 ChangeTransparency() = 1.0f - (float )theMat.Transparency();
126 // =======================================================================
127 // function : OpenGl_Workspace
129 // =======================================================================
130 OpenGl_Workspace::OpenGl_Workspace (OpenGl_View* theView, const Handle(OpenGl_Window)& theWindow)
132 HighlightColor (&THE_WHITE_COLOR),
134 myWindow (theWindow),
135 myGlContext (!theWindow.IsNull() ? theWindow->GetGlContext() : NULL),
136 myUseZBuffer (Standard_True),
137 myUseDepthWrite (Standard_True),
138 myUseGLLight (Standard_True),
140 myAspectLineSet (&myDefaultAspectLine),
141 myAspectFaceSet (&myDefaultAspectFace),
142 myAspectMarkerSet (&myDefaultAspectMarker),
143 myAspectTextSet (&myDefaultAspectText),
144 myAspectFaceAppliedWithHL (false),
146 ViewMatrix_applied (&myDefaultMatrix),
147 StructureMatrix_applied (&myDefaultMatrix),
148 myToAllowFaceCulling (false),
149 myToHighlight (false),
150 myModelViewMatrix (myDefaultMatrix)
152 if (!myGlContext.IsNull() && myGlContext->MakeCurrent())
154 myGlContext->core11fwd->glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
156 if (!myGlContext->GetResource ("OpenGl_LineAttributes", myLineAttribs))
158 // share and register for release once the resource is no longer used
159 myLineAttribs = new OpenGl_LineAttributes();
160 myGlContext->ShareResource ("OpenGl_LineAttributes", myLineAttribs);
161 myLineAttribs->Init (myGlContext);
164 // General initialization of the context
166 #if !defined(GL_ES_VERSION_2_0)
167 if (myGlContext->core11 != NULL)
169 // Eviter d'avoir les faces mal orientees en noir.
170 // Pourrait etre utiliser pour detecter les problemes d'orientation
171 glLightModeli ((GLenum )GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
173 // Optimisation pour le Fog et l'antialiasing
174 glHint (GL_FOG_HINT, GL_FASTEST);
175 glHint (GL_POINT_SMOOTH_HINT, GL_FASTEST);
178 glHint (GL_LINE_SMOOTH_HINT, GL_FASTEST);
179 glHint (GL_POLYGON_SMOOTH_HINT, GL_FASTEST);
183 myDefaultCappingAlgoFilter = new OpenGl_CappingAlgoFilter();
185 myNoneCulling .Aspect()->SetSuppressBackFaces (false);
186 myNoneCulling .Aspect()->SetDrawEdges (false);
187 myFrontCulling.Aspect()->SetSuppressBackFaces (true);
188 myFrontCulling.Aspect()->SetDrawEdges (false);
191 // =======================================================================
192 // function : ~OpenGl_Workspace
194 // =======================================================================
195 OpenGl_Workspace::~OpenGl_Workspace()
197 if (!myLineAttribs.IsNull())
199 myLineAttribs.Nullify();
200 myGlContext->ReleaseResource ("OpenGl_LineAttributes", Standard_True);
204 // =======================================================================
205 // function : Activate
207 // =======================================================================
208 Standard_Boolean OpenGl_Workspace::Activate()
210 if (myWindow.IsNull() || !myWindow->Activate())
212 return Standard_False;
215 ViewMatrix_applied = &myDefaultMatrix;
216 StructureMatrix_applied = &myDefaultMatrix;
218 ResetAppliedAspect();
220 return Standard_True;
223 //=======================================================================
224 //function : ResetAppliedAspect
225 //purpose : Sets default values of GL parameters in accordance with default aspects
226 //=======================================================================
227 void OpenGl_Workspace::ResetAppliedAspect()
229 myGlContext->BindDefaultVao();
231 NamedStatus = !myTextureBound.IsNull() ? OPENGL_NS_TEXTURE : 0;
232 HighlightColor = &THE_WHITE_COLOR;
233 myToAllowFaceCulling = false;
234 myAspectLineSet = &myDefaultAspectLine;
235 myAspectFaceSet = &myDefaultAspectFace;
236 myAspectFaceApplied.Nullify();
237 myAspectMarkerSet = &myDefaultAspectMarker;
238 myAspectMarkerApplied.Nullify();
239 myAspectTextSet = &myDefaultAspectText;
240 myPolygonOffsetApplied= Graphic3d_PolygonOffset();
247 myGlContext->SetTypeOfLine (myDefaultAspectLine.Aspect()->Type());
248 myGlContext->SetLineWidth (myDefaultAspectLine.Aspect()->Width());
251 // =======================================================================
252 // function : DisableTexture
254 // =======================================================================
255 Handle(OpenGl_Texture) OpenGl_Workspace::DisableTexture()
257 if (myTextureBound.IsNull())
259 return myTextureBound;
262 const Handle(OpenGl_Sampler)& aSampler = myGlContext->TextureSampler();
263 if (!aSampler.IsNull())
265 aSampler->Unbind (*myGlContext);
268 #if !defined(GL_ES_VERSION_2_0)
269 // reset texture matrix because some code may expect it is identity
270 if (myGlContext->core11 != NULL)
272 GLint aMatrixMode = GL_TEXTURE;
273 glGetIntegerv (GL_MATRIX_MODE, &aMatrixMode);
274 glMatrixMode (GL_TEXTURE);
276 glMatrixMode (aMatrixMode);
280 myTextureBound->Unbind (myGlContext);
281 switch (myTextureBound->GetTarget())
283 #if !defined(GL_ES_VERSION_2_0)
286 if (myGlContext->core11 != NULL)
288 if (myTextureBound->GetParams()->GenMode() != GL_NONE)
290 glDisable (GL_TEXTURE_GEN_S);
292 glDisable (GL_TEXTURE_1D);
299 #if !defined(GL_ES_VERSION_2_0)
300 if (myGlContext->core11 != NULL)
302 if (myTextureBound->GetParams()->GenMode() != GL_NONE)
304 glDisable (GL_TEXTURE_GEN_S);
305 glDisable (GL_TEXTURE_GEN_T);
306 if (myTextureBound->GetParams()->GenMode() == Graphic3d_TOTM_SPRITE)
308 glDisable (GL_POINT_SPRITE);
311 glDisable (GL_TEXTURE_2D);
319 Handle(OpenGl_Texture) aPrevTexture = myTextureBound;
320 myTextureBound.Nullify();
324 // =======================================================================
325 // function : setTextureParams
327 // =======================================================================
328 void OpenGl_Workspace::setTextureParams (Handle(OpenGl_Texture)& theTexture,
329 const Handle(Graphic3d_TextureParams)& theParams)
331 const Handle(Graphic3d_TextureParams)& aParams = theParams.IsNull() ? theTexture->GetParams() : theParams;
332 if (aParams.IsNull())
337 #if !defined(GL_ES_VERSION_2_0)
338 if (myGlContext->core11 != NULL)
340 GLint anEnvMode = GL_MODULATE; // lighting mode
341 if (!aParams->IsModulate())
343 anEnvMode = GL_DECAL;
344 if (theTexture->GetFormat() == GL_ALPHA
345 || theTexture->GetFormat() == GL_LUMINANCE)
347 anEnvMode = GL_REPLACE;
351 // setup generation of texture coordinates
352 switch (aParams->GenMode())
354 case Graphic3d_TOTM_OBJECT:
356 glTexGeni (GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
357 glTexGenfv (GL_S, GL_OBJECT_PLANE, aParams->GenPlaneS().GetData());
358 if (theTexture->GetTarget() != GL_TEXTURE_1D)
360 glTexGeni (GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
361 glTexGenfv (GL_T, GL_OBJECT_PLANE, aParams->GenPlaneT().GetData());
365 case Graphic3d_TOTM_SPHERE:
367 glTexGeni (GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
368 if (theTexture->GetTarget() != GL_TEXTURE_1D)
370 glTexGeni (GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
374 case Graphic3d_TOTM_EYE:
376 myGlContext->WorldViewState.Push();
378 myGlContext->WorldViewState.SetIdentity();
379 myGlContext->ApplyWorldViewMatrix();
381 glTexGeni (GL_S, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
382 glTexGenfv (GL_S, GL_EYE_PLANE, aParams->GenPlaneS().GetData());
384 if (theTexture->GetTarget() != GL_TEXTURE_1D)
386 glTexGeni (GL_T, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
387 glTexGenfv (GL_T, GL_EYE_PLANE, aParams->GenPlaneT().GetData());
390 myGlContext->WorldViewState.Pop();
394 case Graphic3d_TOTM_SPRITE:
396 if (myGlContext->core20fwd != NULL)
398 glEnable (GL_POINT_SPRITE);
399 glTexEnvi (GL_POINT_SPRITE, GL_COORD_REPLACE, GL_TRUE);
400 anEnvMode = GL_REPLACE;
404 case Graphic3d_TOTM_MANUAL:
409 glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, anEnvMode);
413 // get active sampler object to override default texture parameters
414 const Handle(OpenGl_Sampler)& aSampler = myGlContext->TextureSampler();
416 // setup texture filtering and wrapping
417 //if (theTexture->GetParams() != theParams)
418 const GLenum aFilter = (aParams->Filter() == Graphic3d_TOTF_NEAREST) ? GL_NEAREST : GL_LINEAR;
419 const GLenum aWrapMode = aParams->IsRepeat() ? GL_REPEAT : myGlContext->TextureWrapClamp();
420 switch (theTexture->GetTarget())
422 #if !defined(GL_ES_VERSION_2_0)
425 if (aSampler.IsNull() || !aSampler->IsValid())
427 glTexParameteri (GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, aFilter);
428 glTexParameteri (GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, aFilter);
429 glTexParameteri (GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, aWrapMode);
433 aSampler->SetParameter (*myGlContext, GL_TEXTURE_MAG_FILTER, aFilter);
434 aSampler->SetParameter (*myGlContext, GL_TEXTURE_MIN_FILTER, aFilter);
435 aSampler->SetParameter (*myGlContext, GL_TEXTURE_WRAP_S, aWrapMode);
443 GLenum aFilterMin = aFilter;
444 if (theTexture->HasMipmaps())
446 aFilterMin = GL_NEAREST_MIPMAP_NEAREST;
447 if (aParams->Filter() == Graphic3d_TOTF_BILINEAR)
449 aFilterMin = GL_LINEAR_MIPMAP_NEAREST;
451 else if (aParams->Filter() == Graphic3d_TOTF_TRILINEAR)
453 aFilterMin = GL_LINEAR_MIPMAP_LINEAR;
456 if (myGlContext->extAnis)
458 // setup degree of anisotropy filter
459 const GLint aMaxDegree = myGlContext->MaxDegreeOfAnisotropy();
461 switch (aParams->AnisoFilter())
463 case Graphic3d_LOTA_QUALITY:
465 aDegree = aMaxDegree;
468 case Graphic3d_LOTA_MIDDLE:
470 aDegree = (aMaxDegree <= 4) ? 2 : (aMaxDegree / 2);
473 case Graphic3d_LOTA_FAST:
478 case Graphic3d_LOTA_OFF:
486 if (aSampler.IsNull() || !aSampler->IsValid())
488 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, aDegree);
492 aSampler->SetParameter (*myGlContext, GL_TEXTURE_MAX_ANISOTROPY_EXT, aDegree);
497 if (aSampler.IsNull() || !aSampler->IsValid())
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);
506 aSampler->SetParameter (*myGlContext, GL_TEXTURE_MIN_FILTER, aFilterMin);
507 aSampler->SetParameter (*myGlContext, GL_TEXTURE_MAG_FILTER, aFilter);
508 aSampler->SetParameter (*myGlContext, GL_TEXTURE_WRAP_S, aWrapMode);
509 aSampler->SetParameter (*myGlContext, GL_TEXTURE_WRAP_T, aWrapMode);
517 switch (theTexture->GetTarget())
519 #if !defined(GL_ES_VERSION_2_0)
522 if (myGlContext->core11 != NULL)
524 if (aParams->GenMode() != Graphic3d_TOTM_MANUAL)
526 glEnable (GL_TEXTURE_GEN_S);
528 glEnable (GL_TEXTURE_1D);
535 #if !defined(GL_ES_VERSION_2_0)
536 if (myGlContext->core11 != NULL)
538 if (aParams->GenMode() != Graphic3d_TOTM_MANUAL)
540 glEnable (GL_TEXTURE_GEN_S);
541 glEnable (GL_TEXTURE_GEN_T);
543 glEnable (GL_TEXTURE_2D);
551 theTexture->SetParams (aParams);
554 // =======================================================================
555 // function : EnableTexture
557 // =======================================================================
558 Handle(OpenGl_Texture) OpenGl_Workspace::EnableTexture (const Handle(OpenGl_Texture)& theTexture,
559 const Handle(Graphic3d_TextureParams)& theParams)
561 if (theTexture.IsNull() || !theTexture->IsValid())
563 return DisableTexture();
566 if (myTextureBound == theTexture
567 && (theParams.IsNull() || theParams == theTexture->GetParams()))
570 return myTextureBound;
573 Handle(OpenGl_Texture) aPrevTexture = DisableTexture();
574 myTextureBound = theTexture;
575 myTextureBound->Bind (myGlContext);
576 setTextureParams (myTextureBound, theParams);
578 // If custom sampler object is available it will be
579 // used for overriding default texture parameters
580 const Handle(OpenGl_Sampler)& aSampler = myGlContext->TextureSampler();
582 if (!aSampler.IsNull() && aSampler->IsValid())
584 aSampler->Bind (*myGlContext);
590 // =======================================================================
591 // function : updateMaterial
593 // =======================================================================
594 void OpenGl_Workspace::updateMaterial (const int theFlag)
596 // Case of hidden line
597 if (myAspectFaceSet->Aspect()->InteriorStyle() == Aspect_IS_HIDDENLINE)
599 // copy all values including line edge aspect
600 *myAspectFaceHl.Aspect().operator->() = *myAspectFaceSet->Aspect();
601 myAspectFaceHl.SetAspectEdge (myAspectFaceSet->AspectEdge());
602 myAspectFaceHl.Aspect()->SetInteriorColor (myView->BackgroundColor().GetRGB());
603 myAspectFaceHl.SetNoLighting (true);
604 myAspectFaceSet = &myAspectFaceHl;
608 const Graphic3d_MaterialAspect* aSrcMat = &myAspectFaceSet->Aspect()->FrontMaterial();
609 const Quantity_Color* aSrcIntColor = &myAspectFaceSet->Aspect()->InteriorColor();
610 GLenum aFace = GL_FRONT_AND_BACK;
611 if (theFlag == TEL_BACK_MATERIAL)
614 aSrcMat = &myAspectFaceSet->Aspect()->BackMaterial();
615 aSrcIntColor = &myAspectFaceSet->Aspect()->BackInteriorColor();
617 else if (myAspectFaceSet->Aspect()->Distinguish()
618 && !(NamedStatus & OPENGL_NS_RESMAT))
623 myMatTmp.Init (*aSrcMat, *aSrcIntColor);
626 myMatTmp.SetColor (*HighlightColor);
629 // handling transparency
630 if (NamedStatus & OPENGL_NS_2NDPASSDO)
633 myMatTmp.Diffuse.a() = aSrcMat->EnvReflexion();
637 if (aSrcMat->EnvReflexion() != 0.0f)
639 // if the material reflects the environment scene, the second pass is needed
640 NamedStatus |= OPENGL_NS_2NDPASSNEED;
643 const float aTransp = (float )aSrcMat->Transparency();
646 // render transparent
647 myMatTmp.Diffuse.a() = 1.0f - aTransp;
648 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
652 glDepthMask (GL_FALSE);
658 glBlendFunc (GL_ONE, GL_ZERO);
659 glDisable (GL_BLEND);
662 glDepthMask (GL_TRUE);
667 // do not update material properties in case of zero reflection mode,
668 // because GL lighting will be disabled by OpenGl_PrimitiveArray::DrawArray() anyway.
669 if (myAspectFaceSet->IsNoLighting())
675 if (NamedStatus & OPENGL_NS_RESMAT)
677 #if !defined(GL_ES_VERSION_2_0)
678 if (myGlContext->core11 != NULL)
680 myGlContext->core11->glMaterialfv (aFace, GL_AMBIENT, myMatTmp.Ambient.GetData());
681 myGlContext->core11->glMaterialfv (aFace, GL_DIFFUSE, myMatTmp.Diffuse.GetData());
682 myGlContext->core11->glMaterialfv (aFace, GL_SPECULAR, myMatTmp.Specular.GetData());
683 myGlContext->core11->glMaterialfv (aFace, GL_EMISSION, myMatTmp.Emission.GetData());
684 myGlContext->core11->glMaterialf (aFace, GL_SHININESS, myMatTmp.Shine());
688 if (theFlag == TEL_FRONT_MATERIAL)
690 myMatFront = myMatTmp;
691 myMatBack = myMatTmp;
695 myMatBack = myMatTmp;
698 NamedStatus &= ~OPENGL_NS_RESMAT;
703 OpenGl_Material& anOld = (theFlag == TEL_FRONT_MATERIAL)
706 #if !defined(GL_ES_VERSION_2_0)
707 if (myGlContext->core11 != NULL)
709 if (myMatTmp.Ambient.r() != anOld.Ambient.r()
710 || myMatTmp.Ambient.g() != anOld.Ambient.g()
711 || myMatTmp.Ambient.b() != anOld.Ambient.b())
713 myGlContext->core11->glMaterialfv (aFace, GL_AMBIENT, myMatTmp.Ambient.GetData());
715 if (myMatTmp.Diffuse.r() != anOld.Diffuse.r()
716 || myMatTmp.Diffuse.g() != anOld.Diffuse.g()
717 || myMatTmp.Diffuse.b() != anOld.Diffuse.b()
718 || fabs (myMatTmp.Diffuse.a() - anOld.Diffuse.a()) > 0.01f)
720 myGlContext->core11->glMaterialfv (aFace, GL_DIFFUSE, myMatTmp.Diffuse.GetData());
722 if (myMatTmp.Specular.r() != anOld.Specular.r()
723 || myMatTmp.Specular.g() != anOld.Specular.g()
724 || myMatTmp.Specular.b() != anOld.Specular.b())
726 myGlContext->core11->glMaterialfv (aFace, GL_SPECULAR, myMatTmp.Specular.GetData());
728 if (myMatTmp.Emission.r() != anOld.Emission.r()
729 || myMatTmp.Emission.g() != anOld.Emission.g()
730 || myMatTmp.Emission.b() != anOld.Emission.b())
732 myGlContext->core11->glMaterialfv (aFace, GL_EMISSION, myMatTmp.Emission.GetData());
734 if (myMatTmp.Shine() != anOld.Shine())
736 myGlContext->core11->glMaterialf (aFace, GL_SHININESS, myMatTmp.Shine());
741 if (aFace == GL_FRONT_AND_BACK)
743 myMatBack = myMatTmp;
747 // =======================================================================
748 // function : SetAspectLine
750 // =======================================================================
751 const OpenGl_AspectLine* OpenGl_Workspace::SetAspectLine (const OpenGl_AspectLine* theAspect)
753 const OpenGl_AspectLine* aPrevAspectLine = myAspectLineSet;
754 myAspectLineSet = theAspect;
755 return aPrevAspectLine;
758 // =======================================================================
759 // function : SetAspectFace
761 // =======================================================================
762 const OpenGl_AspectFace * OpenGl_Workspace::SetAspectFace (const OpenGl_AspectFace* theAspect)
764 const OpenGl_AspectFace* aPrevAspectFace = myAspectFaceSet;
765 myAspectFaceSet = theAspect;
766 return aPrevAspectFace;
769 // =======================================================================
770 // function : SetAspectMarker
772 // =======================================================================
773 const OpenGl_AspectMarker* OpenGl_Workspace::SetAspectMarker (const OpenGl_AspectMarker* theAspect)
775 const OpenGl_AspectMarker* aPrevAspectMarker = myAspectMarkerSet;
776 myAspectMarkerSet = theAspect;
777 return aPrevAspectMarker;
780 // =======================================================================
781 // function : SetAspectText
783 // =======================================================================
784 const OpenGl_AspectText * OpenGl_Workspace::SetAspectText (const OpenGl_AspectText* theAspect)
786 const OpenGl_AspectText* aPrevAspectText = myAspectTextSet;
787 myAspectTextSet = theAspect;
788 return aPrevAspectText;
791 // =======================================================================
792 // function : ApplyAspectFace
794 // =======================================================================
795 const OpenGl_AspectFace* OpenGl_Workspace::ApplyAspectFace()
797 if (myView->BackfacingModel() == Graphic3d_TOBM_AUTOMATIC)
799 // manage back face culling mode, disable culling when clipping is enabled
800 bool toSuppressBackFaces = myToAllowFaceCulling
801 && myAspectFaceSet->Aspect()->ToSuppressBackFaces();
802 if (toSuppressBackFaces)
804 if (myGlContext->Clipping().IsClippingOrCappingOn()
805 || myAspectFaceSet->Aspect()->InteriorStyle() == Aspect_IS_HATCH)
807 toSuppressBackFaces = false;
810 if (toSuppressBackFaces)
812 if (!(NamedStatus & OPENGL_NS_2NDPASSDO)
813 && (float )myAspectFaceSet->Aspect()->FrontMaterial().Transparency() != 0.0f)
815 // disable culling in case of translucent shading aspect
816 toSuppressBackFaces = false;
819 myGlContext->SetCullBackFaces (toSuppressBackFaces);
822 if (myAspectFaceSet->Aspect() == myAspectFaceApplied
823 && myToHighlight == myAspectFaceAppliedWithHL)
825 return myAspectFaceSet;
827 myAspectFaceAppliedWithHL = myToHighlight;
829 #if !defined(GL_ES_VERSION_2_0)
830 const Aspect_InteriorStyle anIntstyle = myAspectFaceSet->Aspect()->InteriorStyle();
831 if (myAspectFaceApplied.IsNull()
832 || myAspectFaceApplied->InteriorStyle() != anIntstyle)
836 case Aspect_IS_EMPTY:
837 case Aspect_IS_HOLLOW:
839 glPolygonMode (GL_FRONT_AND_BACK, GL_LINE);
842 case Aspect_IS_HATCH:
844 glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
845 myLineAttribs->SetTypeOfHatch (!myAspectFaceApplied.IsNull() ? myAspectFaceApplied->HatchStyle() : Aspect_HS_SOLID);
848 case Aspect_IS_SOLID:
849 case Aspect_IS_HIDDENLINE:
851 glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
852 if (myGlContext->core11 != NULL)
854 glDisable (GL_POLYGON_STIPPLE);
858 case Aspect_IS_POINT:
860 glPolygonMode (GL_FRONT_AND_BACK, GL_POINT);
866 if (anIntstyle == Aspect_IS_HATCH)
868 const Aspect_HatchStyle hatchstyle = myAspectFaceSet->Aspect()->HatchStyle();
869 if (myAspectFaceApplied.IsNull()
870 || myAspectFaceApplied->HatchStyle() != hatchstyle)
872 myLineAttribs->SetTypeOfHatch (hatchstyle);
877 // Aspect_POM_None means: do not change current settings
878 if ((myAspectFaceSet->Aspect()->PolygonOffset().Mode & Aspect_POM_None) != Aspect_POM_None)
880 if (myPolygonOffsetApplied.Mode != myAspectFaceSet->Aspect()->PolygonOffset().Mode
881 || myPolygonOffsetApplied.Factor != myAspectFaceSet->Aspect()->PolygonOffset().Factor
882 || myPolygonOffsetApplied.Units != myAspectFaceSet->Aspect()->PolygonOffset().Units)
884 SetPolygonOffset (myAspectFaceSet->Aspect()->PolygonOffset());
888 updateMaterial (TEL_FRONT_MATERIAL);
889 if (myAspectFaceSet->Aspect()->Distinguish())
891 updateMaterial (TEL_BACK_MATERIAL);
894 if (myAspectFaceSet->Aspect()->ToMapTexture())
896 EnableTexture (myAspectFaceSet->TextureRes (myGlContext),
897 myAspectFaceSet->TextureParams());
901 if (!myEnvironmentTexture.IsNull())
903 EnableTexture (myEnvironmentTexture,
904 myEnvironmentTexture->GetParams());
912 myAspectFaceApplied = myAspectFaceSet->Aspect();
913 return myAspectFaceSet;
916 //=======================================================================
917 //function : SetPolygonOffset
919 //=======================================================================
920 void OpenGl_Workspace::SetPolygonOffset (const Graphic3d_PolygonOffset& theParams)
922 myPolygonOffsetApplied = theParams;
924 if ((theParams.Mode & Aspect_POM_Fill) == Aspect_POM_Fill)
926 glEnable (GL_POLYGON_OFFSET_FILL);
930 glDisable (GL_POLYGON_OFFSET_FILL);
933 #if !defined(GL_ES_VERSION_2_0)
934 if ((theParams.Mode & Aspect_POM_Line) == Aspect_POM_Line)
936 glEnable (GL_POLYGON_OFFSET_LINE);
940 glDisable (GL_POLYGON_OFFSET_LINE);
943 if ((theParams.Mode & Aspect_POM_Point) == Aspect_POM_Point)
945 glEnable (GL_POLYGON_OFFSET_POINT);
949 glDisable (GL_POLYGON_OFFSET_POINT);
952 glPolygonOffset (theParams.Factor, theParams.Units);
955 // =======================================================================
956 // function : ApplyAspectMarker
958 // =======================================================================
959 const OpenGl_AspectMarker* OpenGl_Workspace::ApplyAspectMarker()
961 if (myAspectMarkerSet->Aspect() != myAspectMarkerApplied)
963 if (myAspectMarkerApplied.IsNull()
964 || (myAspectMarkerSet->Aspect()->Scale() != myAspectMarkerApplied->Scale()))
966 #if !defined(GL_ES_VERSION_2_0)
967 glPointSize (myAspectMarkerSet->Aspect()->Scale());
969 gl2psPointSize (myAspectMarkerSet->Aspect()->Scale());
973 myAspectMarkerApplied = myAspectMarkerSet->Aspect();
975 return myAspectMarkerSet;
978 // =======================================================================
981 // =======================================================================
982 Standard_Integer OpenGl_Workspace::Width() const
984 return !myView->GlWindow().IsNull() ? myView->GlWindow()->Width() : 0;
987 // =======================================================================
990 // =======================================================================
991 Standard_Integer OpenGl_Workspace::Height() const
993 return !myView->GlWindow().IsNull() ? myView->GlWindow()->Height() : 0;
996 // =======================================================================
997 // function : UseGLLight
999 // =======================================================================
1000 Standard_Boolean OpenGl_Workspace::UseGLLight() const
1002 return myView->IsGLLightEnabled();
1005 // =======================================================================
1006 // function : IsCullingEnabled
1008 // =======================================================================
1009 Standard_Boolean OpenGl_Workspace::IsCullingEnabled() const
1011 return myView->IsCullingEnabled();
1014 // =======================================================================
1015 // function : FBOCreate
1017 // =======================================================================
1018 Handle(OpenGl_FrameBuffer) OpenGl_Workspace::FBOCreate (const Standard_Integer theWidth,
1019 const Standard_Integer theHeight)
1021 // activate OpenGL context
1023 return Handle(OpenGl_FrameBuffer)();
1028 const Handle(OpenGl_Context)& aCtx = GetGlContext();
1029 Handle(OpenGl_FrameBuffer) aFrameBuffer = new OpenGl_FrameBuffer();
1030 if (!aFrameBuffer->Init (aCtx, theWidth, theHeight, GL_RGBA8, GL_DEPTH24_STENCIL8, 0))
1032 aFrameBuffer->Release (aCtx.operator->());
1033 return Handle(OpenGl_FrameBuffer)();
1035 return aFrameBuffer;
1038 // =======================================================================
1039 // function : FBORelease
1041 // =======================================================================
1042 void OpenGl_Workspace::FBORelease (Handle(OpenGl_FrameBuffer)& theFbo)
1044 // activate OpenGL context
1051 theFbo->Release (GetGlContext().operator->());
1055 inline bool getDataFormat (const Image_PixMap& theData,
1056 GLenum& thePixelFormat,
1057 GLenum& theDataType)
1059 thePixelFormat = GL_RGB;
1060 theDataType = GL_UNSIGNED_BYTE;
1061 switch (theData.Format())
1063 #if !defined(GL_ES_VERSION_2_0)
1064 case Image_PixMap::ImgGray:
1065 thePixelFormat = GL_DEPTH_COMPONENT;
1066 theDataType = GL_UNSIGNED_BYTE;
1068 case Image_PixMap::ImgGrayF:
1069 thePixelFormat = GL_DEPTH_COMPONENT;
1070 theDataType = GL_FLOAT;
1072 case Image_PixMap::ImgBGR:
1073 thePixelFormat = GL_BGR;
1074 theDataType = GL_UNSIGNED_BYTE;
1076 case Image_PixMap::ImgBGRA:
1077 case Image_PixMap::ImgBGR32:
1078 thePixelFormat = GL_BGRA;
1079 theDataType = GL_UNSIGNED_BYTE;
1081 case Image_PixMap::ImgBGRF:
1082 thePixelFormat = GL_BGR;
1083 theDataType = GL_FLOAT;
1085 case Image_PixMap::ImgBGRAF:
1086 thePixelFormat = GL_BGRA;
1087 theDataType = GL_FLOAT;
1090 case Image_PixMap::ImgGray:
1091 case Image_PixMap::ImgGrayF:
1092 case Image_PixMap::ImgBGR:
1093 case Image_PixMap::ImgBGRA:
1094 case Image_PixMap::ImgBGR32:
1095 case Image_PixMap::ImgBGRF:
1096 case Image_PixMap::ImgBGRAF:
1099 case Image_PixMap::ImgRGB:
1100 thePixelFormat = GL_RGB;
1101 theDataType = GL_UNSIGNED_BYTE;
1103 case Image_PixMap::ImgRGBA:
1104 case Image_PixMap::ImgRGB32:
1105 thePixelFormat = GL_RGBA;
1106 theDataType = GL_UNSIGNED_BYTE;
1108 case Image_PixMap::ImgRGBF:
1109 thePixelFormat = GL_RGB;
1110 theDataType = GL_FLOAT;
1112 case Image_PixMap::ImgRGBAF:
1113 thePixelFormat = GL_RGBA;
1114 theDataType = GL_FLOAT;
1116 case Image_PixMap::ImgAlpha:
1117 case Image_PixMap::ImgAlphaF:
1118 return false; // GL_ALPHA is no more supported in core context
1119 case Image_PixMap::ImgUNKNOWN:
1125 // =======================================================================
1126 // function : getAligned
1128 // =======================================================================
1129 inline Standard_Size getAligned (const Standard_Size theNumber,
1130 const Standard_Size theAlignment)
1132 return theNumber + theAlignment - 1 - (theNumber - 1) % theAlignment;
1135 // =======================================================================
1136 // function : BufferDump
1138 // =======================================================================
1139 Standard_Boolean OpenGl_Workspace::BufferDump (const Handle(OpenGl_FrameBuffer)& theFbo,
1140 Image_PixMap& theImage,
1141 const Graphic3d_BufferType& theBufferType)
1143 GLenum aFormat, aType;
1144 if (theImage.IsEmpty()
1145 || !getDataFormat (theImage, aFormat, aType)
1148 return Standard_False;
1150 #if !defined(GL_ES_VERSION_2_0)
1151 GLint aReadBufferPrev = GL_BACK;
1152 if (theBufferType == Graphic3d_BT_Depth
1153 && aFormat != GL_DEPTH_COMPONENT)
1155 return Standard_False;
1158 (void )theBufferType;
1162 if (!theFbo.IsNull() && theFbo->IsValid())
1164 theFbo->BindBuffer (GetGlContext());
1168 #if !defined(GL_ES_VERSION_2_0)
1169 glGetIntegerv (GL_READ_BUFFER, &aReadBufferPrev);
1170 GLint aDrawBufferPrev = GL_BACK;
1171 glGetIntegerv (GL_DRAW_BUFFER, &aDrawBufferPrev);
1172 glReadBuffer (aDrawBufferPrev);
1177 const GLint anAligment = Min (GLint(theImage.MaxRowAligmentBytes()), 8); // limit to 8 bytes for OpenGL
1178 glPixelStorei (GL_PACK_ALIGNMENT, anAligment);
1179 bool isBatchCopy = !theImage.IsTopDown();
1181 const GLint anExtraBytes = GLint(theImage.RowExtraBytes());
1182 GLint aPixelsWidth = GLint(theImage.SizeRowBytes() / theImage.SizePixelBytes());
1183 Standard_Size aSizeRowBytesEstim = getAligned (theImage.SizePixelBytes() * aPixelsWidth, anAligment);
1184 if (anExtraBytes < anAligment)
1188 else if (aSizeRowBytesEstim != theImage.SizeRowBytes())
1191 isBatchCopy = false;
1193 #if !defined(GL_ES_VERSION_2_0)
1194 glPixelStorei (GL_PACK_ROW_LENGTH, aPixelsWidth);
1196 if (aPixelsWidth != 0)
1198 isBatchCopy = false;
1205 for (Standard_Size aRow = 0; aRow < theImage.SizeY(); ++aRow)
1207 // Image_PixMap rows indexation always starts from the upper corner
1208 // while order in memory depends on the flag and processed by ChangeRow() method
1209 glReadPixels (0, GLint(theImage.SizeY() - aRow - 1), GLsizei (theImage.SizeX()), 1, aFormat, aType, theImage.ChangeRow (aRow));
1214 glReadPixels (0, 0, GLsizei (theImage.SizeX()), GLsizei (theImage.SizeY()), aFormat, aType, theImage.ChangeData());
1217 glPixelStorei (GL_PACK_ALIGNMENT, 1);
1218 #if !defined(GL_ES_VERSION_2_0)
1219 glPixelStorei (GL_PACK_ROW_LENGTH, 0);
1222 if (!theFbo.IsNull() && theFbo->IsValid())
1224 theFbo->UnbindBuffer (GetGlContext());
1228 #if !defined(GL_ES_VERSION_2_0)
1229 glReadBuffer (aReadBufferPrev);
1232 return Standard_True;
1235 // =======================================================================
1236 // function : CanRender
1238 // =======================================================================
1239 Standard_Boolean OpenGl_RaytraceFilter::CanRender (const OpenGl_Element* theElement)
1241 Standard_Boolean aPrevFilterResult = Standard_True;
1242 if (!myPrevRenderFilter.IsNull())
1244 aPrevFilterResult = myPrevRenderFilter->CanRender (theElement);
1246 return aPrevFilterResult &&
1247 !OpenGl_Raytrace::IsRaytracedElement (theElement);