1 // Created on: 2011-08-01
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_CappingAlgo.hxx>
17 #include <OpenGl_Context.hxx>
18 #include <OpenGl_GlCore11.hxx>
19 #include <OpenGl_GraphicDriver.hxx>
20 #include <OpenGl_ShaderManager.hxx>
21 #include <OpenGl_ShaderProgram.hxx>
22 #include <OpenGl_StructureShadow.hxx>
23 #include <OpenGl_Vec.hxx>
24 #include <OpenGl_View.hxx>
25 #include <OpenGl_Workspace.hxx>
27 #include <Graphic3d_SequenceOfHClipPlane.hxx>
30 IMPLEMENT_STANDARD_RTTIEXT(OpenGl_Structure,Graphic3d_CStructure)
32 //! Auxiliary class for bounding box presentation
33 class OpenGl_BndBoxPrs : public OpenGl_Element
39 OpenGl_BndBoxPrs (const Graphic3d_BndBox4f& theBndBox)
41 const float Xm = theBndBox.CornerMin().x();
42 const float Ym = theBndBox.CornerMin().y();
43 const float Zm = theBndBox.CornerMin().z();
44 const float XM = theBndBox.CornerMax().x();
45 const float YM = theBndBox.CornerMax().y();
46 const float ZM = theBndBox.CornerMax().z();
48 myVerts[0] = OpenGl_Vec3 (Xm, Ym, Zm);
49 myVerts[1] = OpenGl_Vec3 (Xm, Ym, ZM);
50 myVerts[2] = OpenGl_Vec3 (Xm, YM, ZM);
51 myVerts[3] = OpenGl_Vec3 (Xm, YM, Zm);
52 myVerts[4] = OpenGl_Vec3 (Xm, Ym, Zm);
53 myVerts[5] = OpenGl_Vec3 (XM, Ym, Zm);
54 myVerts[6] = OpenGl_Vec3 (XM, Ym, ZM);
55 myVerts[7] = OpenGl_Vec3 (XM, YM, ZM);
56 myVerts[8] = OpenGl_Vec3 (XM, YM, Zm);
57 myVerts[9] = OpenGl_Vec3 (XM, Ym, Zm);
58 myVerts[10] = OpenGl_Vec3 (XM, YM, Zm);
59 myVerts[11] = OpenGl_Vec3 (Xm, YM, Zm);
60 myVerts[12] = OpenGl_Vec3 (Xm, YM, ZM);
61 myVerts[13] = OpenGl_Vec3 (XM, YM, ZM);
62 myVerts[14] = OpenGl_Vec3 (XM, Ym, ZM);
63 myVerts[15] = OpenGl_Vec3 (Xm, Ym, ZM);
66 //! Render presentation
67 virtual void Render (const Handle(OpenGl_Workspace)& theWorkspace) const
69 #if !defined(GL_ES_VERSION_2_0)
71 const Handle(OpenGl_Texture) aPrevTexture = theWorkspace->DisableTexture();
73 glDisable (GL_LIGHTING);
75 // Use highlight colors
76 theWorkspace->GetGlContext()->core11->glColor3fv (theWorkspace->LineColor().rgb);
78 glEnableClientState (GL_VERTEX_ARRAY);
79 glVertexPointer (3, GL_FLOAT, 0, (GLfloat* )&myVerts);
80 glDrawArrays (GL_LINE_STRIP, 0, 16);
81 glDisableClientState (GL_VERTEX_ARRAY);
84 if (!aPrevTexture.IsNull())
86 theWorkspace->EnableTexture (aPrevTexture);
93 //! Release graphical resources
94 virtual void Release (OpenGl_Context*)
101 //! Protected destructor
102 virtual ~OpenGl_BndBoxPrs() {}
106 OpenGl_Vec3 myVerts[16]; //!< vertices array
110 DEFINE_STANDARD_ALLOC
114 /*----------------------------------------------------------------------*/
116 // =======================================================================
117 // function : OpenGl_Structure
119 // =======================================================================
120 OpenGl_Structure::OpenGl_Structure (const Handle(Graphic3d_StructureManager)& theManager)
121 : Graphic3d_CStructure (theManager),
122 myHighlightColor (NULL),
123 myInstancedStructure (NULL),
124 myIsRaytracable (Standard_False),
125 myModificationState (0),
126 myIsCulled (Standard_True),
127 myIsMirrored (Standard_False)
132 // =======================================================================
133 // function : ~OpenGl_Structure
135 // =======================================================================
136 OpenGl_Structure::~OpenGl_Structure()
138 Release (Handle(OpenGl_Context)());
141 // =======================================================================
142 // function : UpdateTransformation
144 // =======================================================================
145 void OpenGl_Structure::UpdateTransformation()
147 const OpenGl_Mat4& aMat = Graphic3d_CStructure::Transformation;
148 Standard_ShortReal aDet =
149 aMat.GetValue(0, 0) * (aMat.GetValue(1, 1) * aMat.GetValue(2, 2) - aMat.GetValue(2, 1) * aMat.GetValue(1, 2)) -
150 aMat.GetValue(0, 1) * (aMat.GetValue(1, 0) * aMat.GetValue(2, 2) - aMat.GetValue(2, 0) * aMat.GetValue(1, 2)) +
151 aMat.GetValue(0, 2) * (aMat.GetValue(1, 0) * aMat.GetValue(2, 1) - aMat.GetValue(2, 0) * aMat.GetValue(1, 1));
153 // Determinant of transform matrix less then 0 means that mirror transform applied.
154 myIsMirrored = aDet < 0.0f;
158 ++myModificationState;
162 // =======================================================================
163 // function : clearHighlightBox
165 // =======================================================================
166 void OpenGl_Structure::clearHighlightBox (const Handle(OpenGl_Context)& theGlCtx)
168 if (!myHighlightBox.IsNull())
170 myHighlightBox->Release (theGlCtx);
171 myHighlightBox.Nullify();
175 // =======================================================================
176 // function : HighlightWithColor
178 // =======================================================================
179 void OpenGl_Structure::HighlightWithColor (const Graphic3d_Vec3& theColor,
180 const Standard_Boolean theToCreate)
182 const Handle(OpenGl_Context)& aContext = GlDriver()->GetSharedContext();
184 setHighlightColor (aContext, theColor);
186 clearHighlightColor (aContext);
189 // =======================================================================
190 // function : HighlightWithBndBox
192 // =======================================================================
193 void OpenGl_Structure::HighlightWithBndBox (const Handle(Graphic3d_Structure)& theStruct,
194 const Standard_Boolean theToCreate)
196 const Handle(OpenGl_Context)& aContext = GlDriver()->GetSharedContext();
199 clearHighlightBox (aContext);
203 if (!myHighlightBox.IsNull())
205 myHighlightBox->Release (aContext);
209 myHighlightBox = new OpenGl_Group (theStruct);
212 CALL_DEF_CONTEXTLINE& aContextLine = myHighlightBox->ChangeContextLine();
213 aContextLine.IsDef = 1;
214 aContextLine.Color = HighlightColor;
215 aContextLine.LineType = Aspect_TOL_SOLID;
216 aContextLine.Width = 1.0f;
217 myHighlightBox->UpdateAspectLine (Standard_True);
219 OpenGl_BndBoxPrs* aBndBoxPrs = new OpenGl_BndBoxPrs (myBndBox);
220 myHighlightBox->AddElement (aBndBoxPrs);
223 // =======================================================================
224 // function : setHighlightColor
226 // =======================================================================
227 void OpenGl_Structure::setHighlightColor (const Handle(OpenGl_Context)& theGlCtx,
228 const Graphic3d_Vec3& theColor)
230 clearHighlightBox (theGlCtx);
231 if (myHighlightColor == NULL)
233 myHighlightColor = new TEL_COLOUR();
236 myHighlightColor->rgb[0] = theColor.r();
237 myHighlightColor->rgb[1] = theColor.g();
238 myHighlightColor->rgb[2] = theColor.b();
239 myHighlightColor->rgb[3] = 1.F;
242 // =======================================================================
243 // function : clearHighlightColor
245 // =======================================================================
246 void OpenGl_Structure::clearHighlightColor (const Handle(OpenGl_Context)& theGlCtx)
248 clearHighlightBox(theGlCtx);
249 delete myHighlightColor;
250 myHighlightColor = NULL;
253 // =======================================================================
254 // function : OnVisibilityChanged
256 // =======================================================================
257 void OpenGl_Structure::OnVisibilityChanged()
261 ++myModificationState;
265 // =======================================================================
266 // function : IsRaytracable
268 // =======================================================================
269 Standard_Boolean OpenGl_Structure::IsRaytracable() const
271 if (!myGroups.IsEmpty())
273 return myIsRaytracable; // geometry structure
275 else if (myInstancedStructure != NULL)
277 return myInstancedStructure->IsRaytracable(); // instance structure
280 return Standard_False; // has no any groups or structures
283 // =======================================================================
284 // function : UpdateRaytracableState
286 // =======================================================================
287 void OpenGl_Structure::UpdateStateIfRaytracable (const Standard_Boolean toCheck) const
289 myIsRaytracable = !toCheck || OpenGl_Raytrace::IsRaytracedStructure (this);
293 ++myModificationState;
297 // =======================================================================
298 // function : Connect
300 // =======================================================================
301 void OpenGl_Structure::Connect (Graphic3d_CStructure& theStructure)
303 OpenGl_Structure* aStruct = static_cast<OpenGl_Structure*> (&theStructure);
305 Standard_ASSERT_RAISE (myInstancedStructure == NULL || myInstancedStructure == aStruct,
306 "Error! Instanced structure is already defined");
308 myInstancedStructure = aStruct;
310 if (aStruct->IsRaytracable())
312 UpdateStateIfRaytracable (Standard_False);
316 // =======================================================================
317 // function : Disconnect
319 // =======================================================================
320 void OpenGl_Structure::Disconnect (Graphic3d_CStructure& theStructure)
322 OpenGl_Structure* aStruct = static_cast<OpenGl_Structure*> (&theStructure);
324 if (myInstancedStructure == aStruct)
326 myInstancedStructure = NULL;
328 if (aStruct->IsRaytracable())
330 UpdateStateIfRaytracable();
335 // =======================================================================
336 // function : NewGroup
338 // =======================================================================
339 Handle(Graphic3d_Group) OpenGl_Structure::NewGroup (const Handle(Graphic3d_Structure)& theStruct)
341 Handle(OpenGl_Group) aGroup = new OpenGl_Group (theStruct);
342 myGroups.Append (aGroup);
346 // =======================================================================
347 // function : RemoveGroup
349 // =======================================================================
350 void OpenGl_Structure::RemoveGroup (const Handle(Graphic3d_Group)& theGroup)
352 if (theGroup.IsNull())
357 for (Graphic3d_SequenceOfGroup::Iterator aGroupIter (myGroups); aGroupIter.More(); aGroupIter.Next())
359 // Check for the given group
360 if (aGroupIter.Value() == theGroup)
362 const Standard_Boolean wasRaytracable =
363 static_cast<const OpenGl_Group&> (*theGroup).IsRaytracable();
365 theGroup->Clear (Standard_False);
369 UpdateStateIfRaytracable();
372 myGroups.Remove (aGroupIter);
378 // =======================================================================
381 // =======================================================================
382 void OpenGl_Structure::Clear()
384 Clear (GlDriver()->GetSharedContext());
387 // =======================================================================
390 // =======================================================================
391 void OpenGl_Structure::Clear (const Handle(OpenGl_Context)& theGlCtx)
393 Standard_Boolean aRaytracableGroupDeleted (Standard_False);
396 for (OpenGl_Structure::GroupIterator aGroupIter (myGroups); aGroupIter.More(); aGroupIter.Next())
398 aRaytracableGroupDeleted |= aGroupIter.Value()->IsRaytracable();
401 aGroupIter.ChangeValue()->Release (theGlCtx);
405 if (aRaytracableGroupDeleted)
407 myIsRaytracable = Standard_False;
410 Is2dText = Standard_False;
411 IsForHighlight = Standard_False;
414 // =======================================================================
415 // function : renderGeometry
417 // =======================================================================
418 void OpenGl_Structure::renderGeometry (const Handle(OpenGl_Workspace)& theWorkspace,
419 bool& theHasClosed) const
421 if (myInstancedStructure != NULL)
423 myInstancedStructure->renderGeometry (theWorkspace, theHasClosed);
426 for (OpenGl_Structure::GroupIterator aGroupIter (myGroups); aGroupIter.More(); aGroupIter.Next())
428 theHasClosed = theHasClosed || aGroupIter.Value()->IsClosed();
429 aGroupIter.Value()->Render (theWorkspace);
433 // =======================================================================
434 // function : renderClosedGeometry
436 // =======================================================================
437 void OpenGl_Structure::renderClosedGeometry (const Handle(OpenGl_Workspace)& theWorkspace) const
439 if (myInstancedStructure != NULL)
441 myInstancedStructure->renderClosedGeometry (theWorkspace);
444 for (OpenGl_Structure::GroupIterator aGroupIter (myGroups); aGroupIter.More(); aGroupIter.Next())
446 if (aGroupIter.Value()->IsClosed())
448 aGroupIter.Value()->Render (theWorkspace);
453 // =======================================================================
456 // =======================================================================
457 void OpenGl_Structure::Render (const Handle(OpenGl_Workspace) &theWorkspace) const
459 // Process the structure only if visible
465 const Handle(OpenGl_Context)& aCtx = theWorkspace->GetGlContext();
467 // Render named status
470 theWorkspace->SetHighlight (true);
473 // Apply local transformation
474 aCtx->ModelWorldState.Push();
475 aCtx->ModelWorldState.SetCurrent (Transformation);
477 const Standard_Boolean anOldGlNormalize = aCtx->IsGlNormalizeEnabled();
479 #if !defined(GL_ES_VERSION_2_0)
480 // detect scale transform
481 if (aCtx->core11 != NULL)
483 const Standard_ShortReal aScaleX = Transformation.GetRow (0).xyz().SquareModulus();
484 if (Abs (aScaleX - 1.f) > Precision::Confusion())
486 aCtx->SetGlNormalizeEnabled (Standard_True);
491 if (TransformPersistence.Flags)
493 OpenGl_Mat4 aProjection = aCtx->ProjectionState.Current();
494 OpenGl_Mat4 aWorldView = aCtx->WorldViewState.Current();
495 TransformPersistence.Apply (aProjection, aWorldView, theWorkspace->Width(), theWorkspace->Height());
497 aCtx->ProjectionState.Push();
498 aCtx->WorldViewState.Push();
499 aCtx->ProjectionState.SetCurrent (aProjection);
500 aCtx->WorldViewState.SetCurrent (aWorldView);
501 aCtx->ApplyProjectionMatrix();
503 #if !defined(GL_ES_VERSION_2_0)
504 if (!aCtx->IsGlNormalizeEnabled()
505 && aCtx->core11 != NULL)
507 const Standard_Real aScale = Graphic3d_TransformUtils::ScaleFactor<Standard_ShortReal> (aWorldView);
508 if (Abs (aScale - 1.0f) > Precision::Confusion())
510 aCtx->SetGlNormalizeEnabled (Standard_True);
516 // Take into account transform persistence
517 aCtx->ApplyModelViewMatrix();
520 const OpenGl_AspectLine* aPrevAspectLine = theWorkspace->AspectLine();
521 const OpenGl_AspectFace* aPrevAspectFace = theWorkspace->AspectFace();
522 const OpenGl_AspectMarker* aPrevAspectMarker = theWorkspace->AspectMarker();
523 const OpenGl_AspectText* aPrevAspectText = theWorkspace->AspectText();
525 // Apply correction for mirror transform
528 aCtx->core11fwd->glFrontFace (GL_CW);
531 // Apply highlight color
532 const TEL_COLOUR *aHighlightColor = theWorkspace->HighlightColor;
533 if (myHighlightColor)
534 theWorkspace->HighlightColor = myHighlightColor;
536 // Set up plane equations for non-structure transformed global model-view matrix
537 // List of planes to be applied to context state
538 NCollection_Handle<Graphic3d_SequenceOfHClipPlane> aUserPlanes;
540 // Collect clipping planes of structure scope
541 if (!myClipPlanes.IsEmpty())
543 Graphic3d_SequenceOfHClipPlane::Iterator aClippingIter (myClipPlanes);
544 for (; aClippingIter.More(); aClippingIter.Next())
546 const Handle(Graphic3d_ClipPlane)& aClipPlane = aClippingIter.Value();
547 if (!aClipPlane->IsOn())
552 if (aUserPlanes.IsNull())
554 aUserPlanes = new Graphic3d_SequenceOfHClipPlane();
557 aUserPlanes->Append (aClipPlane);
561 if (!aUserPlanes.IsNull() && !aUserPlanes->IsEmpty())
563 // add planes at loaded view matrix state
564 aCtx->ChangeClipping().AddWorld (aCtx, *aUserPlanes);
566 // Set OCCT state uniform variables
567 if (!aCtx->ShaderManager()->IsEmpty())
569 aCtx->ShaderManager()->UpdateClippingState();
574 bool hasClosedPrims = false;
575 renderGeometry (theWorkspace, hasClosedPrims);
577 // Reset correction for mirror transform
580 aCtx->core11fwd->glFrontFace (GL_CCW);
583 // Render capping for structure groups
585 && !aCtx->Clipping().Planes().IsEmpty())
587 OpenGl_CappingAlgo::RenderCapping (theWorkspace, *this);
590 // Revert structure clippings
591 if (!aUserPlanes.IsNull() && !aUserPlanes->IsEmpty())
593 aCtx->ChangeClipping().Remove (aCtx, *aUserPlanes);
595 // Set OCCT state uniform variables
596 if (!aCtx->ShaderManager()->IsEmpty())
598 aCtx->ShaderManager()->RevertClippingState();
602 // Restore local transformation
603 aCtx->ModelWorldState.Pop();
604 aCtx->SetGlNormalizeEnabled (anOldGlNormalize);
605 if (TransformPersistence.Flags)
607 aCtx->ProjectionState.Pop();
608 aCtx->WorldViewState.Pop();
609 aCtx->ApplyProjectionMatrix();
612 // Restore highlight color
613 theWorkspace->HighlightColor = aHighlightColor;
616 theWorkspace->SetAspectLine (aPrevAspectLine);
617 theWorkspace->SetAspectFace (aPrevAspectFace);
618 theWorkspace->SetAspectMarker (aPrevAspectMarker);
619 theWorkspace->SetAspectText (aPrevAspectText);
621 // Apply highlight box
622 if (!myHighlightBox.IsNull())
624 myHighlightBox->Render (theWorkspace);
627 // Restore named status
628 theWorkspace->SetHighlight (false);
631 // =======================================================================
632 // function : Release
634 // =======================================================================
635 void OpenGl_Structure::Release (const Handle(OpenGl_Context)& theGlCtx)
639 clearHighlightColor (theGlCtx);
642 // =======================================================================
643 // function : ReleaseGlResources
645 // =======================================================================
646 void OpenGl_Structure::ReleaseGlResources (const Handle(OpenGl_Context)& theGlCtx)
648 for (OpenGl_Structure::GroupIterator aGroupIter (myGroups); aGroupIter.More(); aGroupIter.Next())
650 aGroupIter.ChangeValue()->Release (theGlCtx);
652 if (!myHighlightBox.IsNull())
654 myHighlightBox->Release (theGlCtx.operator->());
658 //=======================================================================
659 //function : ShadowLink
661 //=======================================================================
662 Handle(Graphic3d_CStructure) OpenGl_Structure::ShadowLink (const Handle(Graphic3d_StructureManager)& theManager) const
664 return new OpenGl_StructureShadow (theManager, this);