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_ShaderManager.hxx>
31 #include <OpenGl_Texture.hxx>
32 #include <OpenGl_View.hxx>
33 #include <OpenGl_Window.hxx>
35 #include <Graphic3d_TextureParams.hxx>
36 #include <Graphic3d_TransformUtils.hxx>
37 #include <NCollection_AlignedAllocator.hxx>
39 IMPLEMENT_STANDARD_RTTIEXT(OpenGl_Workspace,Standard_Transient)
40 IMPLEMENT_STANDARD_RTTIEXT(OpenGl_RaytraceFilter,OpenGl_RenderFilter)
44 /* OCC22216 NOTE: linker dependency can be switched off by undefining macro.
45 Pragma comment for gl2ps.lib is defined only here. */
47 #pragma comment( lib, "gl2ps.lib" )
53 static const OpenGl_Vec4 THE_WHITE_COLOR (1.0f, 1.0f, 1.0f, 1.0f);
54 static const OpenGl_Vec4 THE_BLACK_COLOR (0.0f, 0.0f, 0.0f, 1.0f);
56 static const OpenGl_AspectLine myDefaultAspectLine;
57 static const OpenGl_AspectFace myDefaultAspectFace;
58 static const OpenGl_AspectMarker myDefaultAspectMarker;
59 static const OpenGl_AspectText myDefaultAspectText;
61 static const OpenGl_Matrix myDefaultMatrix =
63 {{ 1.0F, 0.0F, 0.0F, 0.0F },
64 { 0.0F, 1.0F, 0.0F, 0.0F },
65 { 0.0F, 0.0F, 1.0F, 0.0F },
66 { 0.0F, 0.0F, 0.0F, 1.0F }}
71 // =======================================================================
74 // =======================================================================
75 void OpenGl_Material::Init (const Graphic3d_MaterialAspect& theMat,
76 const Quantity_Color& theInteriorColor)
78 const bool isPhysic = theMat.MaterialType (Graphic3d_MATERIAL_PHYSIC);
79 ChangeShine() = 128.0f * theMat.Shininess();
80 ChangeTransparency() = theMat.Alpha();
83 if (theMat.ReflectionMode (Graphic3d_TOR_AMBIENT))
85 const OpenGl_Vec3& aSrcAmb = isPhysic ? theMat.AmbientColor() : theInteriorColor;
86 Ambient = OpenGl_Vec4 (aSrcAmb * theMat.Ambient(), 1.0f);
90 Ambient = THE_BLACK_COLOR;
93 // diffusion component
94 if (theMat.ReflectionMode (Graphic3d_TOR_DIFFUSE))
96 const OpenGl_Vec3& aSrcDif = isPhysic ? theMat.DiffuseColor() : theInteriorColor;
97 Diffuse = OpenGl_Vec4 (aSrcDif * theMat.Diffuse(), 1.0f);
101 Diffuse = THE_BLACK_COLOR;
104 // specular component
105 if (theMat.ReflectionMode (Graphic3d_TOR_SPECULAR))
107 const OpenGl_Vec3& aSrcSpe = isPhysic ? (const OpenGl_Vec3& )theMat.SpecularColor() : THE_WHITE_COLOR.rgb();
108 Specular = OpenGl_Vec4 (aSrcSpe * theMat.Specular(), 1.0f);
112 Specular = THE_BLACK_COLOR;
115 // emission component
116 if (theMat.ReflectionMode (Graphic3d_TOR_EMISSION))
118 const OpenGl_Vec3& aSrcEms = isPhysic ? theMat.EmissiveColor() : theInteriorColor;
119 Emission = OpenGl_Vec4 (aSrcEms * theMat.Emissive(), 1.0f);
123 Emission = THE_BLACK_COLOR;
127 // =======================================================================
128 // function : OpenGl_Workspace
130 // =======================================================================
131 OpenGl_Workspace::OpenGl_Workspace (OpenGl_View* theView, const Handle(OpenGl_Window)& theWindow)
133 myWindow (theWindow),
134 myGlContext (!theWindow.IsNull() ? theWindow->GetGlContext() : NULL),
135 myUseZBuffer (Standard_True),
136 myUseDepthWrite (Standard_True),
138 myAspectLineSet (&myDefaultAspectLine),
139 myAspectFaceSet (&myDefaultAspectFace),
140 myAspectMarkerSet (&myDefaultAspectMarker),
141 myAspectTextSet (&myDefaultAspectText),
143 ViewMatrix_applied (&myDefaultMatrix),
144 StructureMatrix_applied (&myDefaultMatrix),
145 myToAllowFaceCulling (false),
146 myModelViewMatrix (myDefaultMatrix)
148 if (!myGlContext.IsNull() && myGlContext->MakeCurrent())
150 myGlContext->core11fwd->glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
152 // General initialization of the context
153 #if !defined(GL_ES_VERSION_2_0)
154 if (myGlContext->core11 != NULL)
156 // Eviter d'avoir les faces mal orientees en noir.
157 // Pourrait etre utiliser pour detecter les problemes d'orientation
158 glLightModeli ((GLenum )GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
160 // Optimisation pour le Fog et l'antialiasing
161 glHint (GL_FOG_HINT, GL_FASTEST);
162 glHint (GL_POINT_SMOOTH_HINT, GL_FASTEST);
165 glHint (GL_LINE_SMOOTH_HINT, GL_FASTEST);
166 glHint (GL_POLYGON_SMOOTH_HINT, GL_FASTEST);
170 myDefaultCappingAlgoFilter = new OpenGl_CappingAlgoFilter();
172 myNoneCulling .Aspect()->SetSuppressBackFaces (false);
173 myNoneCulling .Aspect()->SetDrawEdges (false);
174 myFrontCulling.Aspect()->SetSuppressBackFaces (true);
175 myFrontCulling.Aspect()->SetDrawEdges (false);
178 // =======================================================================
179 // function : Activate
181 // =======================================================================
182 Standard_Boolean OpenGl_Workspace::Activate()
184 if (myWindow.IsNull() || !myWindow->Activate())
186 return Standard_False;
189 ViewMatrix_applied = &myDefaultMatrix;
190 StructureMatrix_applied = &myDefaultMatrix;
192 ResetAppliedAspect();
194 // reset state for safety
195 myGlContext->BindProgram (Handle(OpenGl_ShaderProgram)());
196 if (myGlContext->core20fwd != NULL)
198 myGlContext->core20fwd->glUseProgram (OpenGl_ShaderProgram::NO_PROGRAM);
200 myGlContext->ShaderManager()->PushState (Handle(OpenGl_ShaderProgram)());
201 return Standard_True;
204 //=======================================================================
205 //function : ResetAppliedAspect
206 //purpose : Sets default values of GL parameters in accordance with default aspects
207 //=======================================================================
208 void OpenGl_Workspace::ResetAppliedAspect()
210 myGlContext->BindDefaultVao();
212 myHighlightStyle.Nullify();
213 myToAllowFaceCulling = false;
214 myAspectLineSet = &myDefaultAspectLine;
215 myAspectFaceSet = &myDefaultAspectFace;
216 myAspectFaceApplied.Nullify();
217 myAspectMarkerSet = &myDefaultAspectMarker;
218 myAspectMarkerApplied.Nullify();
219 myAspectTextSet = &myDefaultAspectText;
220 myPolygonOffsetApplied= Graphic3d_PolygonOffset();
227 myGlContext->SetTypeOfLine (myDefaultAspectLine.Aspect()->Type());
228 myGlContext->SetLineWidth (myDefaultAspectLine.Aspect()->Width());
231 // =======================================================================
232 // function : SetAspectLine
234 // =======================================================================
235 const OpenGl_AspectLine* OpenGl_Workspace::SetAspectLine (const OpenGl_AspectLine* theAspect)
237 const OpenGl_AspectLine* aPrevAspectLine = myAspectLineSet;
238 myAspectLineSet = theAspect;
239 return aPrevAspectLine;
242 // =======================================================================
243 // function : SetAspectFace
245 // =======================================================================
246 const OpenGl_AspectFace * OpenGl_Workspace::SetAspectFace (const OpenGl_AspectFace* theAspect)
248 const OpenGl_AspectFace* aPrevAspectFace = myAspectFaceSet;
249 myAspectFaceSet = theAspect;
250 return aPrevAspectFace;
253 // =======================================================================
254 // function : SetAspectMarker
256 // =======================================================================
257 const OpenGl_AspectMarker* OpenGl_Workspace::SetAspectMarker (const OpenGl_AspectMarker* theAspect)
259 const OpenGl_AspectMarker* aPrevAspectMarker = myAspectMarkerSet;
260 myAspectMarkerSet = theAspect;
261 return aPrevAspectMarker;
264 // =======================================================================
265 // function : SetAspectText
267 // =======================================================================
268 const OpenGl_AspectText * OpenGl_Workspace::SetAspectText (const OpenGl_AspectText* theAspect)
270 const OpenGl_AspectText* aPrevAspectText = myAspectTextSet;
271 myAspectTextSet = theAspect;
272 return aPrevAspectText;
275 // =======================================================================
276 // function : ApplyAspectFace
278 // =======================================================================
279 const OpenGl_AspectFace* OpenGl_Workspace::ApplyAspectFace()
281 if (myView->BackfacingModel() == Graphic3d_TOBM_AUTOMATIC)
283 // manage back face culling mode, disable culling when clipping is enabled
284 bool toSuppressBackFaces = myToAllowFaceCulling
285 && myAspectFaceSet->Aspect()->ToSuppressBackFaces();
286 if (toSuppressBackFaces)
288 if (myGlContext->Clipping().IsClippingOrCappingOn()
289 || myAspectFaceSet->Aspect()->InteriorStyle() == Aspect_IS_HATCH)
291 toSuppressBackFaces = false;
294 if (toSuppressBackFaces)
296 if ((float )myAspectFaceSet->Aspect()->FrontMaterial().Transparency() != 0.0f)
298 // disable culling in case of translucent shading aspect
299 toSuppressBackFaces = false;
302 myGlContext->SetCullBackFaces (toSuppressBackFaces);
305 if (myAspectFaceSet->Aspect() == myAspectFaceApplied
306 && myHighlightStyle == myAspectFaceAppliedWithHL)
308 return myAspectFaceSet;
310 myAspectFaceAppliedWithHL = myHighlightStyle;
312 #if !defined(GL_ES_VERSION_2_0)
313 const Aspect_InteriorStyle anIntstyle = myAspectFaceSet->Aspect()->InteriorStyle();
314 if (myAspectFaceApplied.IsNull()
315 || myAspectFaceApplied->InteriorStyle() != anIntstyle)
319 case Aspect_IS_EMPTY:
320 case Aspect_IS_HOLLOW:
322 myGlContext->SetPolygonMode (GL_LINE);
325 case Aspect_IS_HATCH:
327 myGlContext->SetPolygonMode (GL_FILL);
328 myGlContext->SetPolygonHatchEnabled (true);
331 case Aspect_IS_SOLID:
332 case Aspect_IS_HIDDENLINE:
334 myGlContext->SetPolygonMode (GL_FILL);
335 myGlContext->SetPolygonHatchEnabled (false);
338 case Aspect_IS_POINT:
340 myGlContext->SetPolygonMode (GL_POINT);
346 if (anIntstyle == Aspect_IS_HATCH)
348 myGlContext->SetPolygonHatchStyle (myAspectFaceSet->Aspect()->HatchStyle());
352 // Aspect_POM_None means: do not change current settings
353 if ((myAspectFaceSet->Aspect()->PolygonOffset().Mode & Aspect_POM_None) != Aspect_POM_None)
355 if (myPolygonOffsetApplied.Mode != myAspectFaceSet->Aspect()->PolygonOffset().Mode
356 || myPolygonOffsetApplied.Factor != myAspectFaceSet->Aspect()->PolygonOffset().Factor
357 || myPolygonOffsetApplied.Units != myAspectFaceSet->Aspect()->PolygonOffset().Units)
359 SetPolygonOffset (myAspectFaceSet->Aspect()->PolygonOffset());
363 // Case of hidden line
364 if (myAspectFaceSet->Aspect()->InteriorStyle() == Aspect_IS_HIDDENLINE)
366 // copy all values including line edge aspect
367 *myAspectFaceHl.Aspect().operator->() = *myAspectFaceSet->Aspect();
368 myAspectFaceHl.SetAspectEdge (myAspectFaceSet->AspectEdge());
369 myAspectFaceHl.Aspect()->SetInteriorColor (myView->BackgroundColor().GetRGB());
370 myAspectFaceHl.SetNoLighting (true);
371 myAspectFaceSet = &myAspectFaceHl;
375 myGlContext->SetShadingMaterial (myAspectFaceSet, myHighlightStyle);
378 if (myAspectFaceSet->Aspect()->ToMapTexture())
380 myGlContext->BindTextures (myAspectFaceSet->TextureSet (myGlContext));
384 myGlContext->BindTextures (myEnvironmentTexture);
387 myAspectFaceApplied = myAspectFaceSet->Aspect();
388 return myAspectFaceSet;
391 //=======================================================================
392 //function : SetPolygonOffset
394 //=======================================================================
395 void OpenGl_Workspace::SetPolygonOffset (const Graphic3d_PolygonOffset& theParams)
397 myPolygonOffsetApplied = theParams;
399 if ((theParams.Mode & Aspect_POM_Fill) == Aspect_POM_Fill)
401 glEnable (GL_POLYGON_OFFSET_FILL);
405 glDisable (GL_POLYGON_OFFSET_FILL);
408 #if !defined(GL_ES_VERSION_2_0)
409 if ((theParams.Mode & Aspect_POM_Line) == Aspect_POM_Line)
411 glEnable (GL_POLYGON_OFFSET_LINE);
415 glDisable (GL_POLYGON_OFFSET_LINE);
418 if ((theParams.Mode & Aspect_POM_Point) == Aspect_POM_Point)
420 glEnable (GL_POLYGON_OFFSET_POINT);
424 glDisable (GL_POLYGON_OFFSET_POINT);
427 glPolygonOffset (theParams.Factor, theParams.Units);
430 // =======================================================================
431 // function : ApplyAspectMarker
433 // =======================================================================
434 const OpenGl_AspectMarker* OpenGl_Workspace::ApplyAspectMarker()
436 if (myAspectMarkerSet->Aspect() != myAspectMarkerApplied)
438 if (myAspectMarkerApplied.IsNull()
439 || (myAspectMarkerSet->Aspect()->Scale() != myAspectMarkerApplied->Scale()))
441 #if !defined(GL_ES_VERSION_2_0)
442 glPointSize (myAspectMarkerSet->Aspect()->Scale());
444 gl2psPointSize (myAspectMarkerSet->Aspect()->Scale());
448 myAspectMarkerApplied = myAspectMarkerSet->Aspect();
450 return myAspectMarkerSet;
453 // =======================================================================
456 // =======================================================================
457 Standard_Integer OpenGl_Workspace::Width() const
459 return !myView->GlWindow().IsNull() ? myView->GlWindow()->Width() : 0;
462 // =======================================================================
465 // =======================================================================
466 Standard_Integer OpenGl_Workspace::Height() const
468 return !myView->GlWindow().IsNull() ? myView->GlWindow()->Height() : 0;
471 // =======================================================================
472 // function : IsCullingEnabled
474 // =======================================================================
475 Standard_Boolean OpenGl_Workspace::IsCullingEnabled() const
477 return myView->IsCullingEnabled();
480 // =======================================================================
481 // function : FBOCreate
483 // =======================================================================
484 Handle(OpenGl_FrameBuffer) OpenGl_Workspace::FBOCreate (const Standard_Integer theWidth,
485 const Standard_Integer theHeight)
487 // activate OpenGL context
489 return Handle(OpenGl_FrameBuffer)();
492 const Handle(OpenGl_Context)& aCtx = GetGlContext();
493 aCtx->BindTextures (Handle(OpenGl_TextureSet)());
494 Handle(OpenGl_FrameBuffer) aFrameBuffer = new OpenGl_FrameBuffer();
495 if (!aFrameBuffer->Init (aCtx, theWidth, theHeight, GL_RGBA8, GL_DEPTH24_STENCIL8, 0))
497 aFrameBuffer->Release (aCtx.operator->());
498 return Handle(OpenGl_FrameBuffer)();
503 // =======================================================================
504 // function : FBORelease
506 // =======================================================================
507 void OpenGl_Workspace::FBORelease (Handle(OpenGl_FrameBuffer)& theFbo)
509 // activate OpenGL context
516 theFbo->Release (GetGlContext().operator->());
520 // =======================================================================
521 // function : BufferDump
523 // =======================================================================
524 Standard_Boolean OpenGl_Workspace::BufferDump (const Handle(OpenGl_FrameBuffer)& theFbo,
525 Image_PixMap& theImage,
526 const Graphic3d_BufferType& theBufferType)
528 return !theImage.IsEmpty()
530 && OpenGl_FrameBuffer::BufferDump (GetGlContext(), theFbo, theImage, theBufferType);
533 // =======================================================================
534 // function : ShouldRender
536 // =======================================================================
537 Standard_Boolean OpenGl_RaytraceFilter::ShouldRender (const Handle(OpenGl_Workspace)& theWorkspace,
538 const OpenGl_Element* theElement)
540 Standard_Boolean aPrevFilterResult = Standard_True;
541 if (!myPrevRenderFilter.IsNull())
543 aPrevFilterResult = myPrevRenderFilter->ShouldRender (theWorkspace, theElement);
545 return aPrevFilterResult &&
546 !OpenGl_Raytrace::IsRaytracedElement (theElement);