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 // enable two-side lighting by default
157 glLightModeli ((GLenum )GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
158 glHint (GL_POINT_SMOOTH_HINT, GL_FASTEST);
159 if (myGlContext->caps->ffpEnable)
161 glHint (GL_FOG_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 if (myGlContext->caps->ffpEnable)
202 myGlContext->ShaderManager()->PushState (Handle(OpenGl_ShaderProgram)());
204 return Standard_True;
207 //=======================================================================
208 //function : ResetAppliedAspect
209 //purpose : Sets default values of GL parameters in accordance with default aspects
210 //=======================================================================
211 void OpenGl_Workspace::ResetAppliedAspect()
213 myGlContext->BindDefaultVao();
215 myHighlightStyle.Nullify();
216 myToAllowFaceCulling = false;
217 myAspectLineSet = &myDefaultAspectLine;
218 myAspectFaceSet = &myDefaultAspectFace;
219 myAspectFaceApplied.Nullify();
220 myAspectMarkerSet = &myDefaultAspectMarker;
221 myAspectMarkerApplied.Nullify();
222 myAspectTextSet = &myDefaultAspectText;
223 myGlContext->SetPolygonOffset (Graphic3d_PolygonOffset());
230 myGlContext->SetTypeOfLine (myDefaultAspectLine.Aspect()->Type());
231 myGlContext->SetLineWidth (myDefaultAspectLine.Aspect()->Width());
234 // =======================================================================
235 // function : SetAspectLine
237 // =======================================================================
238 const OpenGl_AspectLine* OpenGl_Workspace::SetAspectLine (const OpenGl_AspectLine* theAspect)
240 const OpenGl_AspectLine* aPrevAspectLine = myAspectLineSet;
241 myAspectLineSet = theAspect;
242 return aPrevAspectLine;
245 // =======================================================================
246 // function : SetAspectFace
248 // =======================================================================
249 const OpenGl_AspectFace * OpenGl_Workspace::SetAspectFace (const OpenGl_AspectFace* theAspect)
251 const OpenGl_AspectFace* aPrevAspectFace = myAspectFaceSet;
252 myAspectFaceSet = theAspect;
253 return aPrevAspectFace;
256 // =======================================================================
257 // function : SetAspectMarker
259 // =======================================================================
260 const OpenGl_AspectMarker* OpenGl_Workspace::SetAspectMarker (const OpenGl_AspectMarker* theAspect)
262 const OpenGl_AspectMarker* aPrevAspectMarker = myAspectMarkerSet;
263 myAspectMarkerSet = theAspect;
264 return aPrevAspectMarker;
267 // =======================================================================
268 // function : SetAspectText
270 // =======================================================================
271 const OpenGl_AspectText * OpenGl_Workspace::SetAspectText (const OpenGl_AspectText* theAspect)
273 const OpenGl_AspectText* aPrevAspectText = myAspectTextSet;
274 myAspectTextSet = theAspect;
275 return aPrevAspectText;
278 // =======================================================================
279 // function : ApplyAspectFace
281 // =======================================================================
282 const OpenGl_AspectFace* OpenGl_Workspace::ApplyAspectFace()
284 if (myView->BackfacingModel() == Graphic3d_TOBM_AUTOMATIC)
286 // manage back face culling mode, disable culling when clipping is enabled
287 bool toSuppressBackFaces = myToAllowFaceCulling
288 && myAspectFaceSet->Aspect()->ToSuppressBackFaces();
289 if (toSuppressBackFaces)
291 if (myGlContext->Clipping().IsClippingOrCappingOn()
292 || myAspectFaceSet->Aspect()->InteriorStyle() == Aspect_IS_HATCH)
294 toSuppressBackFaces = false;
297 if (toSuppressBackFaces)
299 if ((float )myAspectFaceSet->Aspect()->FrontMaterial().Transparency() != 0.0f)
301 // disable culling in case of translucent shading aspect
302 toSuppressBackFaces = false;
305 myGlContext->SetCullBackFaces (toSuppressBackFaces);
308 if (myAspectFaceSet->Aspect() == myAspectFaceApplied
309 && myHighlightStyle == myAspectFaceAppliedWithHL)
311 return myAspectFaceSet;
313 myAspectFaceAppliedWithHL = myHighlightStyle;
315 #if !defined(GL_ES_VERSION_2_0)
316 const Aspect_InteriorStyle anIntstyle = myAspectFaceSet->Aspect()->InteriorStyle();
317 if (myAspectFaceApplied.IsNull()
318 || myAspectFaceApplied->InteriorStyle() != anIntstyle)
322 case Aspect_IS_EMPTY:
323 case Aspect_IS_HOLLOW:
325 myGlContext->SetPolygonMode (GL_LINE);
328 case Aspect_IS_HATCH:
330 myGlContext->SetPolygonMode (GL_FILL);
331 myGlContext->SetPolygonHatchEnabled (true);
334 case Aspect_IS_SOLID:
335 case Aspect_IS_HIDDENLINE:
337 myGlContext->SetPolygonMode (GL_FILL);
338 myGlContext->SetPolygonHatchEnabled (false);
341 case Aspect_IS_POINT:
343 myGlContext->SetPolygonMode (GL_POINT);
349 if (anIntstyle == Aspect_IS_HATCH)
351 myGlContext->SetPolygonHatchStyle (myAspectFaceSet->Aspect()->HatchStyle());
355 // Aspect_POM_None means: do not change current settings
356 if ((myAspectFaceSet->Aspect()->PolygonOffset().Mode & Aspect_POM_None) != Aspect_POM_None)
358 myGlContext->SetPolygonOffset (myAspectFaceSet->Aspect()->PolygonOffset());
361 // Case of hidden line
362 if (myAspectFaceSet->Aspect()->InteriorStyle() == Aspect_IS_HIDDENLINE)
364 // copy all values including line edge aspect
365 *myAspectFaceHl.Aspect() = *myAspectFaceSet->Aspect();
366 myAspectFaceHl.SetAspectEdge (myAspectFaceSet->AspectEdge());
367 myAspectFaceHl.Aspect()->SetShadingModel (Graphic3d_TOSM_UNLIT);
368 myAspectFaceHl.Aspect()->SetInteriorColor (myView->BackgroundColor().GetRGB());
369 myAspectFaceHl.SetNoLighting();
370 myAspectFaceSet = &myAspectFaceHl;
374 myGlContext->SetShadingMaterial (myAspectFaceSet, myHighlightStyle);
377 if (myAspectFaceSet->Aspect()->ToMapTexture())
379 myGlContext->BindTextures (myAspectFaceSet->TextureSet (myGlContext));
383 myGlContext->BindTextures (myEnvironmentTexture);
386 myAspectFaceApplied = myAspectFaceSet->Aspect();
387 return myAspectFaceSet;
390 // =======================================================================
391 // function : ApplyAspectMarker
393 // =======================================================================
394 const OpenGl_AspectMarker* OpenGl_Workspace::ApplyAspectMarker()
396 if (myAspectMarkerSet->Aspect() != myAspectMarkerApplied)
398 if (myAspectMarkerApplied.IsNull()
399 || (myAspectMarkerSet->Aspect()->Scale() != myAspectMarkerApplied->Scale()))
401 #if !defined(GL_ES_VERSION_2_0)
402 glPointSize (myAspectMarkerSet->Aspect()->Scale());
404 gl2psPointSize (myAspectMarkerSet->Aspect()->Scale());
408 myAspectMarkerApplied = myAspectMarkerSet->Aspect();
410 return myAspectMarkerSet;
413 // =======================================================================
416 // =======================================================================
417 Standard_Integer OpenGl_Workspace::Width() const
419 return !myView->GlWindow().IsNull() ? myView->GlWindow()->Width() : 0;
422 // =======================================================================
425 // =======================================================================
426 Standard_Integer OpenGl_Workspace::Height() const
428 return !myView->GlWindow().IsNull() ? myView->GlWindow()->Height() : 0;
431 // =======================================================================
432 // function : IsCullingEnabled
434 // =======================================================================
435 Standard_Boolean OpenGl_Workspace::IsCullingEnabled() const
437 return myView->IsCullingEnabled();
440 // =======================================================================
441 // function : FBOCreate
443 // =======================================================================
444 Handle(OpenGl_FrameBuffer) OpenGl_Workspace::FBOCreate (const Standard_Integer theWidth,
445 const Standard_Integer theHeight)
447 // activate OpenGL context
449 return Handle(OpenGl_FrameBuffer)();
452 const Handle(OpenGl_Context)& aCtx = GetGlContext();
453 aCtx->BindTextures (Handle(OpenGl_TextureSet)());
454 Handle(OpenGl_FrameBuffer) aFrameBuffer = new OpenGl_FrameBuffer();
455 if (!aFrameBuffer->Init (aCtx, theWidth, theHeight, GL_RGBA8, GL_DEPTH24_STENCIL8, 0))
457 aFrameBuffer->Release (aCtx.operator->());
458 return Handle(OpenGl_FrameBuffer)();
463 // =======================================================================
464 // function : FBORelease
466 // =======================================================================
467 void OpenGl_Workspace::FBORelease (Handle(OpenGl_FrameBuffer)& theFbo)
469 // activate OpenGL context
476 theFbo->Release (GetGlContext().operator->());
480 // =======================================================================
481 // function : BufferDump
483 // =======================================================================
484 Standard_Boolean OpenGl_Workspace::BufferDump (const Handle(OpenGl_FrameBuffer)& theFbo,
485 Image_PixMap& theImage,
486 const Graphic3d_BufferType& theBufferType)
488 return !theImage.IsEmpty()
490 && OpenGl_FrameBuffer::BufferDump (GetGlContext(), theFbo, theImage, theBufferType);
493 // =======================================================================
494 // function : ShouldRender
496 // =======================================================================
497 Standard_Boolean OpenGl_RaytraceFilter::ShouldRender (const Handle(OpenGl_Workspace)& theWorkspace,
498 const OpenGl_Element* theElement)
500 Standard_Boolean aPrevFilterResult = Standard_True;
501 if (!myPrevRenderFilter.IsNull())
503 aPrevFilterResult = myPrevRenderFilter->ShouldRender (theWorkspace, theElement);
505 return aPrevFilterResult &&
506 !OpenGl_Raytrace::IsRaytracedElement (theElement);