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_Group.hxx>
18 #include <OpenGl_GraphicDriver.hxx>
19 #include <OpenGl_Flipper.hxx>
20 #include <OpenGl_PrimitiveArray.hxx>
21 #include <OpenGl_SceneGeometry.hxx>
22 #include <OpenGl_StencilTest.hxx>
23 #include <OpenGl_Structure.hxx>
24 #include <OpenGl_Text.hxx>
25 #include <OpenGl_Workspace.hxx>
27 #include <Graphic3d_ArrayOfPrimitives.hxx>
28 #include <Graphic3d_GroupDefinitionError.hxx>
31 IMPLEMENT_STANDARD_RTTIEXT(OpenGl_Group,Graphic3d_Group)
33 // =======================================================================
34 // function : OpenGl_Group
36 // =======================================================================
37 OpenGl_Group::OpenGl_Group (const Handle(Graphic3d_Structure)& theStruct)
38 : Graphic3d_Group (theStruct),
45 myIsRaytracable (Standard_False)
47 Handle(OpenGl_Structure) aStruct = Handle(OpenGl_Structure)::DownCast (myStructure->CStructure());
50 throw Graphic3d_GroupDefinitionError("OpenGl_Group should be created by OpenGl_Structure!");
54 // =======================================================================
55 // function : ~OpenGl_Group
57 // =======================================================================
58 OpenGl_Group::~OpenGl_Group()
60 Release (Handle(OpenGl_Context)());
63 // =======================================================================
64 // function : SetGroupPrimitivesAspect
66 // =======================================================================
67 void OpenGl_Group::SetGroupPrimitivesAspect (const Handle(Graphic3d_AspectLine3d)& theAspect)
74 if (myAspectLine == NULL)
76 myAspectLine = new OpenGl_AspectLine (theAspect);
80 myAspectLine->SetAspect (theAspect);
85 // =======================================================================
86 // function : SetPrimitivesAspect
88 // =======================================================================
89 void OpenGl_Group::SetPrimitivesAspect (const Handle(Graphic3d_AspectLine3d)& theAspect)
91 if (myAspectLine == NULL)
93 SetGroupPrimitivesAspect (theAspect);
101 OpenGl_AspectLine* anAspectLine = new OpenGl_AspectLine (theAspect);
102 AddElement (anAspectLine);
106 // =======================================================================
107 // function : SetGroupPrimitivesAspect
109 // =======================================================================
110 void OpenGl_Group::SetGroupPrimitivesAspect (const Handle(Graphic3d_AspectFillArea3d)& theAspect)
117 if (myAspectFace == NULL)
119 myAspectFace = new OpenGl_AspectFace (theAspect);
123 myAspectFace->SetAspect (theAspect);
128 OpenGl_Structure* aStruct = GlStruct();
131 aStruct->UpdateStateIfRaytracable (Standard_False);
138 // =======================================================================
139 // function : SetPrimitivesAspect
141 // =======================================================================
142 void OpenGl_Group::SetPrimitivesAspect (const Handle(Graphic3d_AspectFillArea3d)& theAspect)
144 if (myAspectFace == NULL)
146 SetGroupPrimitivesAspect (theAspect);
149 else if (IsDeleted())
154 OpenGl_AspectFace* anAspectFace = new OpenGl_AspectFace (theAspect);
155 AddElement (anAspectFace);
159 // =======================================================================
160 // function : SetGroupPrimitivesAspect
162 // =======================================================================
163 void OpenGl_Group::SetGroupPrimitivesAspect (const Handle(Graphic3d_AspectMarker3d)& theAspMarker)
170 if (myAspectMarker == NULL)
172 myAspectMarker = new OpenGl_AspectMarker (theAspMarker);
176 myAspectMarker->SetAspect (theAspMarker);
181 // =======================================================================
182 // function : SetPrimitivesAspect
184 // =======================================================================
185 void OpenGl_Group::SetPrimitivesAspect (const Handle(Graphic3d_AspectMarker3d)& theAspMarker)
187 if (myAspectMarker == NULL)
189 SetGroupPrimitivesAspect (theAspMarker);
192 else if (IsDeleted())
197 OpenGl_AspectMarker* anAspectMarker = new OpenGl_AspectMarker (theAspMarker);
198 AddElement (anAspectMarker);
202 // =======================================================================
203 // function : SetGroupPrimitivesAspect
205 // =======================================================================
206 void OpenGl_Group::SetGroupPrimitivesAspect (const Handle(Graphic3d_AspectText3d)& theAspText)
213 if (myAspectText == NULL)
215 myAspectText = new OpenGl_AspectText (theAspText);
219 myAspectText->SetAspect (theAspText);
224 // =======================================================================
225 // function : SetPrimitivesAspect
227 // =======================================================================
228 void OpenGl_Group::SetPrimitivesAspect (const Handle(Graphic3d_AspectText3d)& theAspText)
230 if (myAspectText == NULL)
232 SetGroupPrimitivesAspect (theAspText);
235 else if (IsDeleted())
240 OpenGl_AspectText* anAspectText = new OpenGl_AspectText (theAspText);
241 AddElement (anAspectText);
245 // =======================================================================
246 // function : AddPrimitiveArray
248 // =======================================================================
249 void OpenGl_Group::AddPrimitiveArray (const Graphic3d_TypeOfPrimitiveArray theType,
250 const Handle(Graphic3d_IndexBuffer)& theIndices,
251 const Handle(Graphic3d_Buffer)& theAttribs,
252 const Handle(Graphic3d_BoundBuffer)& theBounds,
253 const Standard_Boolean theToEvalMinMax)
256 || theAttribs.IsNull())
261 OpenGl_Structure* aStruct = GlStruct();
262 const OpenGl_GraphicDriver* aDriver = aStruct->GlDriver();
264 OpenGl_PrimitiveArray* anArray = new OpenGl_PrimitiveArray (aDriver, theType, theIndices, theAttribs, theBounds);
265 AddElement (anArray);
267 Graphic3d_Group::AddPrimitiveArray (theType, theIndices, theAttribs, theBounds, theToEvalMinMax);
270 // =======================================================================
273 // =======================================================================
274 void OpenGl_Group::Text (const Standard_CString theTextUtf,
275 const Graphic3d_Vertex& thePoint,
276 const Standard_Real theHeight,
277 const Standard_Real theAngle,
278 const Graphic3d_TextPath theTp,
279 const Graphic3d_HorizontalTextAlignment theHta,
280 const Graphic3d_VerticalTextAlignment theVta,
281 const Standard_Boolean theToEvalMinMax)
288 OpenGl_TextParam aParams;
289 OpenGl_Structure* aStruct = GlStruct();
290 aParams.Height = int ((theHeight < 2.0) ? aStruct->GlDriver()->DefaultTextHeight() : theHeight);
291 aParams.HAlign = theHta;
292 aParams.VAlign = theVta;
293 const OpenGl_Vec3 aPoint (thePoint.X(), thePoint.Y(), thePoint.Z());
294 OpenGl_Text* aText = new OpenGl_Text (theTextUtf, aPoint, aParams);
296 Graphic3d_Group::Text (theTextUtf, thePoint, theHeight, theAngle,
297 theTp, theHta, theVta, theToEvalMinMax);
300 // =======================================================================
303 // =======================================================================
304 void OpenGl_Group::Text (const Standard_CString theTextUtf,
305 const gp_Ax2& theOrientation,
306 const Standard_Real theHeight,
307 const Standard_Real theAngle,
308 const Graphic3d_TextPath theTp,
309 const Graphic3d_HorizontalTextAlignment theHTA,
310 const Graphic3d_VerticalTextAlignment theVTA,
311 const Standard_Boolean theToEvalMinMax,
312 const Standard_Boolean theHasOwnAnchor)
319 OpenGl_TextParam aParams;
320 OpenGl_Structure* aStruct = GlStruct();
322 aParams.Height = int ((theHeight < 2.0) ? aStruct->GlDriver()->DefaultTextHeight() : theHeight);
323 aParams.HAlign = theHTA;
324 aParams.VAlign = theVTA;
326 OpenGl_Text* aText = new OpenGl_Text (theTextUtf, theOrientation, aParams, theHasOwnAnchor != Standard_False);
330 Graphic3d_Group::Text (theTextUtf,
342 // =======================================================================
343 // function : SetFlippingOptions
345 // =======================================================================
346 void OpenGl_Group::SetFlippingOptions (const Standard_Boolean theIsEnabled,
347 const gp_Ax2& theRefPlane)
349 OpenGl_Flipper* aFlipper = new OpenGl_Flipper (theRefPlane);
350 aFlipper->SetOptions (theIsEnabled);
351 AddElement (aFlipper);
354 // =======================================================================
355 // function : SetStencilTestOptions
357 // =======================================================================
358 void OpenGl_Group::SetStencilTestOptions (const Standard_Boolean theIsEnabled)
360 OpenGl_StencilTest* aStencilTest = new OpenGl_StencilTest();
361 aStencilTest->SetOptions (theIsEnabled);
362 AddElement (aStencilTest);
365 // =======================================================================
366 // function : AddElement
368 // =======================================================================
369 void OpenGl_Group::AddElement (OpenGl_Element* theElem)
371 OpenGl_ElementNode *aNode = new OpenGl_ElementNode();
373 aNode->elem = theElem;
375 (myLast? myLast->next : myFirst) = aNode;
378 if (OpenGl_Raytrace::IsRaytracedElement (aNode))
380 myIsRaytracable = Standard_True;
382 OpenGl_Structure* aStruct = GlStruct();
385 aStruct->UpdateStateIfRaytracable (Standard_False);
390 // =======================================================================
393 // =======================================================================
394 void OpenGl_Group::Render (const Handle(OpenGl_Workspace)& theWorkspace) const
396 const Handle(OpenGl_RenderFilter)& aFilter = theWorkspace->GetRenderFilter();
399 theWorkspace->SetAllowFaceCulling (myIsClosed
400 && !theWorkspace->GetGlContext()->Clipping().IsClippingOrCappingOn());
401 const OpenGl_AspectLine* aBackAspectLine = theWorkspace->AspectLine();
402 const OpenGl_AspectFace* aBackAspectFace = theWorkspace->AspectFace();
403 const OpenGl_AspectMarker* aBackAspectMarker = theWorkspace->AspectMarker();
404 const OpenGl_AspectText* aBackAspectText = theWorkspace->AspectText();
405 Standard_Boolean isLineSet = myAspectLine && myAspectLine->RenderFiltered (theWorkspace, aFilter);
406 Standard_Boolean isFaceSet = myAspectFace && myAspectFace->RenderFiltered (theWorkspace, aFilter);
407 Standard_Boolean isMarkerSet = myAspectMarker && myAspectMarker->RenderFiltered (theWorkspace, aFilter);
408 Standard_Boolean isTextSet = myAspectText && myAspectText->RenderFiltered (theWorkspace, aFilter);
410 // Render group elements
411 for (OpenGl_ElementNode* aNodeIter = myFirst; aNodeIter != NULL; aNodeIter = aNodeIter->next)
413 aNodeIter->elem->RenderFiltered (theWorkspace, aFilter);
418 theWorkspace->SetAspectLine (aBackAspectLine);
420 theWorkspace->SetAspectFace (aBackAspectFace);
422 theWorkspace->SetAspectMarker (aBackAspectMarker);
424 theWorkspace->SetAspectText (aBackAspectText);
427 // =======================================================================
430 // =======================================================================
431 void OpenGl_Group::Clear (const Standard_Boolean theToUpdateStructureMgr)
438 OpenGl_Structure* aStruct = GlStruct();
439 const Handle(OpenGl_Context)& aCtx = aStruct->GlDriver()->GetSharedContext();
442 Graphic3d_Group::Clear (theToUpdateStructureMgr);
444 myIsRaytracable = Standard_False;
447 // =======================================================================
448 // function : Release
450 // =======================================================================
451 void OpenGl_Group::Release (const Handle(OpenGl_Context)& theGlCtx)
454 while (myFirst != NULL)
456 OpenGl_ElementNode* aNext = myFirst->next;
457 OpenGl_Element::Destroy (theGlCtx.operator->(), myFirst->elem);
463 OpenGl_Element::Destroy (theGlCtx.operator->(), myAspectLine);
464 OpenGl_Element::Destroy (theGlCtx.operator->(), myAspectFace);
465 OpenGl_Element::Destroy (theGlCtx.operator->(), myAspectMarker);
466 OpenGl_Element::Destroy (theGlCtx.operator->(), myAspectText);