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 Graphic3d_GroupDefinitionError::Raise ("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 : UpdateAspectLine
66 // =======================================================================
67 void OpenGl_Group::UpdateAspectLine (const Standard_Boolean theIsGlobal)
69 if (!ContextLine.IsDef)
74 if (theIsGlobal || myFirst == NULL)
76 if (myAspectLine == NULL)
78 myAspectLine = new OpenGl_AspectLine();
80 myAspectLine->SetAspect (ContextLine);
84 OpenGl_AspectLine* anAspectLine = new OpenGl_AspectLine();
85 anAspectLine->SetAspect (ContextLine);
86 AddElement (anAspectLine);
90 // =======================================================================
91 // function : UpdateAspectFace
93 // =======================================================================
94 void OpenGl_Group::UpdateAspectFace (const Standard_Boolean theIsGlobal)
96 if (!ContextFillArea.IsDef)
101 if (theIsGlobal || myFirst == NULL)
103 if (myAspectFace == NULL)
105 myAspectFace = new OpenGl_AspectFace();
107 myAspectFace->SetAspect (ContextFillArea);
111 OpenGl_AspectFace* anAspectFace = new OpenGl_AspectFace();
112 anAspectFace->SetAspect (ContextFillArea);
113 AddElement (anAspectFace);
118 OpenGl_Structure* aStruct = GlStruct();
121 aStruct->UpdateStateIfRaytracable (Standard_False);
126 // =======================================================================
127 // function : UpdateAspectMarker
129 // =======================================================================
130 void OpenGl_Group::UpdateAspectMarker (const Standard_Boolean theIsGlobal)
132 if (!ContextMarker.IsDef)
137 if (theIsGlobal || myFirst == NULL)
139 if (myAspectMarker == NULL)
141 myAspectMarker = new OpenGl_AspectMarker();
143 myAspectMarker->SetAspect (ContextMarker);
147 OpenGl_AspectMarker* anAspectMarker = new OpenGl_AspectMarker();
148 anAspectMarker->SetAspect (ContextMarker);
149 AddElement (anAspectMarker);
153 // =======================================================================
154 // function : UpdateAspectText
156 // =======================================================================
157 void OpenGl_Group::UpdateAspectText (const Standard_Boolean theIsGlobal)
159 if (!ContextText.IsDef)
164 if (theIsGlobal || myFirst == NULL)
166 if (myAspectText == NULL)
168 myAspectText = new OpenGl_AspectText();
170 myAspectText->SetAspect (ContextText);
174 OpenGl_AspectText* anAspectText = new OpenGl_AspectText();
175 anAspectText->SetAspect (ContextText);
176 AddElement (anAspectText);
180 // =======================================================================
181 // function : AddPrimitiveArray
183 // =======================================================================
184 void OpenGl_Group::AddPrimitiveArray (const Graphic3d_TypeOfPrimitiveArray theType,
185 const Handle(Graphic3d_IndexBuffer)& theIndices,
186 const Handle(Graphic3d_Buffer)& theAttribs,
187 const Handle(Graphic3d_BoundBuffer)& theBounds,
188 const Standard_Boolean theToEvalMinMax)
191 || theAttribs.IsNull())
196 OpenGl_Structure* aStruct = GlStruct();
197 const OpenGl_GraphicDriver* aDriver = aStruct->GlDriver();
199 OpenGl_PrimitiveArray* anArray = new OpenGl_PrimitiveArray (aDriver, theType, theIndices, theAttribs, theBounds);
200 AddElement (anArray);
202 Graphic3d_Group::AddPrimitiveArray (theType, theIndices, theAttribs, theBounds, theToEvalMinMax);
205 // =======================================================================
208 // =======================================================================
209 void OpenGl_Group::Text (const Standard_CString theTextUtf,
210 const Graphic3d_Vertex& thePoint,
211 const Standard_Real theHeight,
212 const Quantity_PlaneAngle theAngle,
213 const Graphic3d_TextPath theTp,
214 const Graphic3d_HorizontalTextAlignment theHta,
215 const Graphic3d_VerticalTextAlignment theVta,
216 const Standard_Boolean theToEvalMinMax)
223 OpenGl_TextParam aParams;
224 OpenGl_Structure* aStruct = GlStruct();
225 aParams.Height = int ((theHeight < 2.0) ? aStruct->GlDriver()->DefaultTextHeight() : theHeight);
226 aParams.HAlign = theHta;
227 aParams.VAlign = theVta;
228 const OpenGl_Vec3 aPoint (thePoint.X(), thePoint.Y(), thePoint.Z());
229 OpenGl_Text* aText = new OpenGl_Text (theTextUtf, aPoint, aParams);
231 Graphic3d_Group::Text (theTextUtf, thePoint, theHeight, theAngle,
232 theTp, theHta, theVta, theToEvalMinMax);
235 // =======================================================================
238 // =======================================================================
239 void OpenGl_Group::Text (const Standard_CString theTextUtf,
240 const gp_Ax2& theOrientation,
241 const Standard_Real theHeight,
242 const Quantity_PlaneAngle theAngle,
243 const Graphic3d_TextPath theTp,
244 const Graphic3d_HorizontalTextAlignment theHTA,
245 const Graphic3d_VerticalTextAlignment theVTA,
246 const Standard_Boolean theToEvalMinMax,
247 const Standard_Boolean theHasOwnAnchor)
254 OpenGl_TextParam aParams;
255 OpenGl_Structure* aStruct = GlStruct();
257 aParams.Height = int ((theHeight < 2.0) ? aStruct->GlDriver()->DefaultTextHeight() : theHeight);
258 aParams.HAlign = theHTA;
259 aParams.VAlign = theVTA;
261 OpenGl_Text* aText = new OpenGl_Text (theTextUtf, theOrientation, aParams, theHasOwnAnchor != Standard_False);
265 Graphic3d_Group::Text (theTextUtf,
277 // =======================================================================
278 // function : SetFlippingOptions
280 // =======================================================================
281 void OpenGl_Group::SetFlippingOptions (const Standard_Boolean theIsEnabled,
282 const gp_Ax2& theRefPlane)
284 OpenGl_Flipper* aFlipper = new OpenGl_Flipper (theRefPlane);
285 aFlipper->SetOptions (theIsEnabled);
286 AddElement (aFlipper);
289 // =======================================================================
290 // function : SetStencilTestOptions
292 // =======================================================================
293 void OpenGl_Group::SetStencilTestOptions (const Standard_Boolean theIsEnabled)
295 OpenGl_StencilTest* aStencilTest = new OpenGl_StencilTest();
296 aStencilTest->SetOptions (theIsEnabled);
297 AddElement (aStencilTest);
300 // =======================================================================
301 // function : AddElement
303 // =======================================================================
304 void OpenGl_Group::AddElement (OpenGl_Element* theElem)
306 OpenGl_ElementNode *aNode = new OpenGl_ElementNode();
308 aNode->elem = theElem;
310 (myLast? myLast->next : myFirst) = aNode;
313 if (OpenGl_Raytrace::IsRaytracedElement (aNode))
315 myIsRaytracable = Standard_True;
317 OpenGl_Structure* aStruct = GlStruct();
320 aStruct->UpdateStateIfRaytracable (Standard_False);
325 // =======================================================================
328 // =======================================================================
329 void OpenGl_Group::Render (const Handle(OpenGl_Workspace)& theWorkspace) const
331 // Is rendering in ADD or IMMEDIATE mode?
332 const Handle(OpenGl_RenderFilter)& aFilter = theWorkspace->GetRenderFilter();
335 const OpenGl_AspectLine* aBackAspectLine = theWorkspace->AspectLine (Standard_False);
336 const OpenGl_AspectFace* aBackAspectFace = theWorkspace->AspectFace (Standard_False);
337 const OpenGl_AspectMarker* aBackAspectMarker = theWorkspace->AspectMarker (Standard_False);
338 const OpenGl_AspectText* aBackAspectText = theWorkspace->AspectText (Standard_False);
339 Standard_Boolean isLineSet = myAspectLine && myAspectLine->RenderFiltered (theWorkspace, aFilter);
340 Standard_Boolean isFaceSet = myAspectFace && myAspectFace->RenderFiltered (theWorkspace, aFilter);
341 Standard_Boolean isMarkerSet = myAspectMarker && myAspectMarker->RenderFiltered (theWorkspace, aFilter);
342 Standard_Boolean isTextSet = myAspectText && myAspectText->RenderFiltered (theWorkspace, aFilter);
344 // Render group elements
345 for (OpenGl_ElementNode* aNodeIter = myFirst; aNodeIter != NULL; aNodeIter = aNodeIter->next)
347 aNodeIter->elem->RenderFiltered (theWorkspace, aFilter);
352 theWorkspace->SetAspectLine (aBackAspectLine);
354 theWorkspace->SetAspectFace (aBackAspectFace);
356 theWorkspace->SetAspectMarker (aBackAspectMarker);
358 theWorkspace->SetAspectText (aBackAspectText);
361 // =======================================================================
364 // =======================================================================
365 void OpenGl_Group::Clear (const Standard_Boolean theToUpdateStructureMgr)
372 OpenGl_Structure* aStruct = GlStruct();
373 const Handle(OpenGl_Context)& aCtx = aStruct->GlDriver()->GetSharedContext();
376 Graphic3d_Group::Clear (theToUpdateStructureMgr);
378 myIsRaytracable = Standard_False;
381 // =======================================================================
382 // function : Release
384 // =======================================================================
385 void OpenGl_Group::Release (const Handle(OpenGl_Context)& theGlCtx)
388 while (myFirst != NULL)
390 OpenGl_ElementNode* aNext = myFirst->next;
391 OpenGl_Element::Destroy (theGlCtx.operator->(), myFirst->elem);
397 OpenGl_Element::Destroy (theGlCtx.operator->(), myAspectLine);
398 OpenGl_Element::Destroy (theGlCtx.operator->(), myAspectFace);
399 OpenGl_Element::Destroy (theGlCtx.operator->(), myAspectMarker);
400 OpenGl_Element::Destroy (theGlCtx.operator->(), myAspectText);