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)
253 OpenGl_TextParam aParams;
254 OpenGl_Structure* aStruct = GlStruct();
256 aParams.Height = int ((theHeight < 2.0) ? aStruct->GlDriver()->DefaultTextHeight() : theHeight);
257 aParams.HAlign = theHTA;
258 aParams.VAlign = theVTA;
260 OpenGl_Text* aText = new OpenGl_Text (theTextUtf, theOrientation, aParams);
264 Graphic3d_Group::Text (theTextUtf,
275 // =======================================================================
276 // function : SetFlippingOptions
278 // =======================================================================
279 void OpenGl_Group::SetFlippingOptions (const Standard_Boolean theIsEnabled,
280 const gp_Ax2& theRefPlane)
282 OpenGl_Flipper* aFlipper = new OpenGl_Flipper (theRefPlane);
283 aFlipper->SetOptions (theIsEnabled);
284 AddElement (aFlipper);
287 // =======================================================================
288 // function : SetStencilTestOptions
290 // =======================================================================
291 void OpenGl_Group::SetStencilTestOptions (const Standard_Boolean theIsEnabled)
293 OpenGl_StencilTest* aStencilTest = new OpenGl_StencilTest();
294 aStencilTest->SetOptions (theIsEnabled);
295 AddElement (aStencilTest);
298 // =======================================================================
299 // function : AddElement
301 // =======================================================================
302 void OpenGl_Group::AddElement (OpenGl_Element* theElem)
304 OpenGl_ElementNode *aNode = new OpenGl_ElementNode();
306 aNode->elem = theElem;
308 (myLast? myLast->next : myFirst) = aNode;
311 if (OpenGl_Raytrace::IsRaytracedElement (aNode))
313 myIsRaytracable = Standard_True;
315 OpenGl_Structure* aStruct = GlStruct();
318 aStruct->UpdateStateIfRaytracable (Standard_False);
323 // =======================================================================
326 // =======================================================================
327 void OpenGl_Group::Render (const Handle(OpenGl_Workspace)& theWorkspace) const
329 // Is rendering in ADD or IMMEDIATE mode?
330 const Handle(OpenGl_RenderFilter)& aFilter = theWorkspace->GetRenderFilter();
333 const OpenGl_AspectLine* aBackAspectLine = theWorkspace->AspectLine (Standard_False);
334 const OpenGl_AspectFace* aBackAspectFace = theWorkspace->AspectFace (Standard_False);
335 const OpenGl_AspectMarker* aBackAspectMarker = theWorkspace->AspectMarker (Standard_False);
336 const OpenGl_AspectText* aBackAspectText = theWorkspace->AspectText (Standard_False);
337 Standard_Boolean isLineSet = myAspectLine && myAspectLine->RenderFiltered (theWorkspace, aFilter);
338 Standard_Boolean isFaceSet = myAspectFace && myAspectFace->RenderFiltered (theWorkspace, aFilter);
339 Standard_Boolean isMarkerSet = myAspectMarker && myAspectMarker->RenderFiltered (theWorkspace, aFilter);
340 Standard_Boolean isTextSet = myAspectText && myAspectText->RenderFiltered (theWorkspace, aFilter);
342 // Render group elements
343 for (OpenGl_ElementNode* aNodeIter = myFirst; aNodeIter != NULL; aNodeIter = aNodeIter->next)
345 aNodeIter->elem->RenderFiltered (theWorkspace, aFilter);
350 theWorkspace->SetAspectLine (aBackAspectLine);
352 theWorkspace->SetAspectFace (aBackAspectFace);
354 theWorkspace->SetAspectMarker (aBackAspectMarker);
356 theWorkspace->SetAspectText (aBackAspectText);
359 // =======================================================================
362 // =======================================================================
363 void OpenGl_Group::Clear (const Standard_Boolean theToUpdateStructureMgr)
370 OpenGl_Structure* aStruct = GlStruct();
371 const Handle(OpenGl_Context)& aCtx = aStruct->GlDriver()->GetSharedContext();
374 Graphic3d_Group::Clear (theToUpdateStructureMgr);
376 myIsRaytracable = Standard_False;
379 // =======================================================================
380 // function : Release
382 // =======================================================================
383 void OpenGl_Group::Release (const Handle(OpenGl_Context)& theGlCtx)
386 while (myFirst != NULL)
388 OpenGl_ElementNode* aNext = myFirst->next;
389 OpenGl_Element::Destroy (theGlCtx.operator->(), myFirst->elem);
395 OpenGl_Element::Destroy (theGlCtx.operator->(), myAspectLine);
396 OpenGl_Element::Destroy (theGlCtx.operator->(), myAspectFace);
397 OpenGl_Element::Destroy (theGlCtx.operator->(), myAspectMarker);
398 OpenGl_Element::Destroy (theGlCtx.operator->(), myAspectText);