0025063: Visualization - 2D objects are not displayed at some camera positions
[occt.git] / src / OpenGl / OpenGl_Group.cxx
1 // Created on: 2011-08-01
2 // Created by: Sergey ZERCHANINOV
3 // Copyright (c) 2011-2014 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
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.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15
16 #include <OpenGl_Group.hxx>
17
18 #include <OpenGl_GraphicDriver.hxx>
19 #include <OpenGl_Flipper.hxx>
20 #include <OpenGl_PrimitiveArray.hxx>
21 #include <OpenGl_StencilTest.hxx>
22 #include <OpenGl_Structure.hxx>
23 #include <OpenGl_Text.hxx>
24 #include <OpenGl_Workspace.hxx>
25
26 #include <Graphic3d_ArrayOfPrimitives.hxx>
27 #include <Graphic3d_CUserDraw.hxx>
28 #include <Graphic3d_GroupDefinitionError.hxx>
29
30 IMPLEMENT_STANDARD_HANDLE (OpenGl_Group, Graphic3d_Group)
31 IMPLEMENT_STANDARD_RTTIEXT(OpenGl_Group, Graphic3d_Group)
32
33 // =======================================================================
34 // function : OpenGl_Group
35 // purpose  :
36 // =======================================================================
37 OpenGl_Group::OpenGl_Group (const Handle(Graphic3d_Structure)& theStruct)
38 : Graphic3d_Group (theStruct),
39   myAspectLine(NULL),
40   myAspectFace(NULL),
41   myAspectMarker(NULL),
42   myAspectText(NULL),
43   myFirst(NULL),
44   myLast(NULL),
45   myIsRaytracable (Standard_False),
46   myModificationState (0)
47 {
48   Handle(OpenGl_Structure) aStruct = Handle(OpenGl_Structure)::DownCast (myStructure->CStructure());
49   if (aStruct == NULL)
50   {
51     Graphic3d_GroupDefinitionError::Raise ("OpenGl_Group should be created by OpenGl_Structure!");
52   }
53 }
54
55 // =======================================================================
56 // function : ~OpenGl_Group
57 // purpose  :
58 // =======================================================================
59 OpenGl_Group::~OpenGl_Group()
60 {
61   Release (Handle(OpenGl_Context)());
62 }
63
64 // =======================================================================
65 // function : UpdateAspectLine
66 // purpose  :
67 // =======================================================================
68 void OpenGl_Group::UpdateAspectLine (const Standard_Boolean theIsGlobal)
69 {
70   if (!ContextLine.IsDef)
71   {
72     return;
73   }
74
75   if (theIsGlobal || myFirst == NULL)
76   {
77     if (myAspectLine == NULL)
78     {
79       myAspectLine = new OpenGl_AspectLine();
80     }
81     myAspectLine->SetAspect (ContextLine);
82   }
83   else
84   {
85     OpenGl_AspectLine* anAspectLine = new OpenGl_AspectLine();
86     anAspectLine->SetAspect (ContextLine);
87     AddElement (anAspectLine);
88   }
89 }
90
91 // =======================================================================
92 // function : UpdateAspectFace
93 // purpose  :
94 // =======================================================================
95 void OpenGl_Group::UpdateAspectFace (const Standard_Boolean theIsGlobal)
96 {
97   if (!ContextFillArea.IsDef)
98   {
99     return;
100   }
101
102   if (theIsGlobal || myFirst == NULL)
103   {
104     if (myAspectFace == NULL)
105     {
106       myAspectFace = new OpenGl_AspectFace();
107     }
108     myAspectFace->SetAspect (ContextFillArea);
109   }
110   else
111   {
112     OpenGl_AspectFace* anAspectFace = new OpenGl_AspectFace();
113     anAspectFace->SetAspect (ContextFillArea);
114     AddElement (anAspectFace);
115   }
116
117   if (myIsRaytracable)
118   {
119     ++myModificationState;
120     OpenGl_Structure* aStruct = GlStruct();
121     if (aStruct != NULL)
122     {
123       aStruct->UpdateStateWithAncestorStructures();
124     }
125   }
126 }
127
128 // =======================================================================
129 // function : UpdateAspectMarker
130 // purpose  :
131 // =======================================================================
132 void OpenGl_Group::UpdateAspectMarker (const Standard_Boolean theIsGlobal)
133 {
134   if (!ContextMarker.IsDef)
135   {
136     return;
137   }
138
139   if (theIsGlobal || myFirst == NULL)
140   {
141     if (myAspectMarker == NULL)
142     {
143       myAspectMarker = new OpenGl_AspectMarker();
144     }
145     myAspectMarker->SetAspect (ContextMarker);
146   }
147   else
148   {
149     OpenGl_AspectMarker* anAspectMarker = new OpenGl_AspectMarker();
150     anAspectMarker->SetAspect (ContextMarker);
151     AddElement (anAspectMarker);
152   }
153 }
154
155 // =======================================================================
156 // function : UpdateAspectText
157 // purpose  :
158 // =======================================================================
159 void OpenGl_Group::UpdateAspectText (const Standard_Boolean theIsGlobal)
160 {
161   if (!ContextText.IsDef)
162   {
163     return;
164   }
165
166   if (theIsGlobal || myFirst == NULL)
167   {
168     if (myAspectText == NULL)
169     {
170       myAspectText = new OpenGl_AspectText();
171     }
172     myAspectText->SetAspect (ContextText);
173   }
174   else
175   {
176     OpenGl_AspectText* anAspectText = new OpenGl_AspectText();
177     anAspectText->SetAspect (ContextText);
178     AddElement (anAspectText);
179   }
180 }
181
182 // =======================================================================
183 // function : AddPrimitiveArray
184 // purpose  :
185 // =======================================================================
186 void OpenGl_Group::AddPrimitiveArray (const Graphic3d_TypeOfPrimitiveArray theType,
187                                       const Handle(Graphic3d_IndexBuffer)& theIndices,
188                                       const Handle(Graphic3d_Buffer)&      theAttribs,
189                                       const Handle(Graphic3d_BoundBuffer)& theBounds,
190                                       const Standard_Boolean               theToEvalMinMax)
191 {
192   if (IsDeleted()
193    || theAttribs.IsNull())
194   {
195     return;
196   }
197
198   OpenGl_PrimitiveArray* anArray = new OpenGl_PrimitiveArray (theType, theIndices, theAttribs, theBounds);
199   AddElement (anArray);
200
201   Graphic3d_Group::AddPrimitiveArray (theType, theIndices, theAttribs, theBounds, theToEvalMinMax);
202 }
203
204 // =======================================================================
205 // function : Text
206 // purpose  :
207 // =======================================================================
208 void OpenGl_Group::Text (const Standard_CString                  theTextUtf,
209                          const Graphic3d_Vertex&                 thePoint,
210                          const Standard_Real                     theHeight,
211                          const Quantity_PlaneAngle               theAngle,
212                          const Graphic3d_TextPath                theTp,
213                          const Graphic3d_HorizontalTextAlignment theHta,
214                          const Graphic3d_VerticalTextAlignment   theVta,
215                          const Standard_Boolean                  theToEvalMinMax)
216 {
217   if (IsDeleted())
218   {
219     return;
220   }
221
222   OpenGl_TextParam  aParams;
223   OpenGl_Structure* aStruct = GlStruct();
224   aParams.Height = int ((theHeight < 2.0) ? aStruct->GlDriver()->DefaultTextHeight() : theHeight);
225   aParams.HAlign = theHta;
226   aParams.VAlign = theVta;
227   const OpenGl_Vec3 aPoint (thePoint.X(), thePoint.Y(), thePoint.Z());
228   OpenGl_Text* aText = new OpenGl_Text (theTextUtf, aPoint, aParams);
229   AddElement (aText);
230   Graphic3d_Group::Text (theTextUtf, thePoint, theHeight, theAngle,
231                          theTp, theHta, theVta, theToEvalMinMax);
232 }
233
234 // =======================================================================
235 // function : UserDraw
236 // purpose  :
237 // =======================================================================
238 void OpenGl_Group::UserDraw (const Standard_Address theObject,
239                              const Standard_Boolean theToEvalMinMax,
240                              const Standard_Boolean theContainsFacet)
241 {
242   if (IsDeleted())
243   {
244     return;
245   }
246
247   OpenGl_Structure* aStruct = GlStruct();
248   if (aStruct->GlDriver()->UserDrawCallback() == NULL)
249   {
250     return;
251   }
252
253   Graphic3d_CUserDraw aUserDraw;
254   aUserDraw.Data   = theObject;
255   aUserDraw.Bounds = theToEvalMinMax ? &myBounds : NULL;
256   OpenGl_Element* aUserDrawElem = aStruct->GlDriver()->UserDrawCallback()(&aUserDraw);
257   if (aUserDrawElem != NULL)
258   {
259     AddElement (aUserDrawElem);
260   }
261   Graphic3d_Group::UserDraw (theObject, theToEvalMinMax, theContainsFacet);
262 }
263
264 // =======================================================================
265 // function : SetFlippingOptions
266 // purpose  :
267 // =======================================================================
268 void OpenGl_Group::SetFlippingOptions (const Standard_Boolean theIsEnabled,
269                                        const gp_Ax2&          theRefPlane)
270 {
271   OpenGl_Flipper* aFlipper = new OpenGl_Flipper (theRefPlane);
272   aFlipper->SetOptions (theIsEnabled);
273   AddElement (aFlipper);
274 }
275
276 // =======================================================================
277 // function : SetStencilTestOptions
278 // purpose  :
279 // =======================================================================
280 void OpenGl_Group::SetStencilTestOptions (const Standard_Boolean theIsEnabled)
281 {
282   OpenGl_StencilTest* aStencilTest = new OpenGl_StencilTest();
283   aStencilTest->SetOptions (theIsEnabled);
284   AddElement (aStencilTest);
285 }
286
287 // =======================================================================
288 // function : AddElement
289 // purpose  :
290 // =======================================================================
291 void OpenGl_Group::AddElement (OpenGl_Element* theElem)
292 {
293   OpenGl_ElementNode *aNode = new OpenGl_ElementNode();
294
295   aNode->elem = theElem;
296   aNode->next = NULL;
297   (myLast? myLast->next : myFirst) = aNode;
298   myLast = aNode;
299
300   if (OpenGl_Raytrace::IsRaytracedElement (aNode))
301   {
302     myModificationState++;
303     myIsRaytracable = Standard_True;
304
305     OpenGl_Structure* aStruct = GlStruct();
306     if (aStruct != NULL)
307     {
308       aStruct->UpdateStateWithAncestorStructures();
309       aStruct->SetRaytracableWithAncestorStructures();
310     }
311   }
312 }
313
314 // =======================================================================
315 // function : Render
316 // purpose  :
317 // =======================================================================
318 void OpenGl_Group::Render (const Handle(OpenGl_Workspace)& theWorkspace) const
319 {
320   // Is rendering in ADD or IMMEDIATE mode?
321   const Handle(OpenGl_RenderFilter)& aFilter = theWorkspace->GetRenderFilter();
322
323   // Setup aspects
324   const OpenGl_AspectLine*   aBackAspectLine   = theWorkspace->AspectLine   (Standard_False);
325   const OpenGl_AspectFace*   aBackAspectFace   = theWorkspace->AspectFace   (Standard_False);
326   const OpenGl_AspectMarker* aBackAspectMarker = theWorkspace->AspectMarker (Standard_False);
327   const OpenGl_AspectText*   aBackAspectText   = theWorkspace->AspectText   (Standard_False);
328   Standard_Boolean isLineSet   = myAspectLine   && myAspectLine->RenderFiltered (theWorkspace, aFilter);
329   Standard_Boolean isFaceSet   = myAspectFace   && myAspectFace->RenderFiltered (theWorkspace, aFilter);
330   Standard_Boolean isMarkerSet = myAspectMarker && myAspectMarker->RenderFiltered (theWorkspace, aFilter);
331   Standard_Boolean isTextSet   = myAspectText   && myAspectText->RenderFiltered (theWorkspace, aFilter);
332
333   // Render group elements
334   for (OpenGl_ElementNode* aNodeIter = myFirst; aNodeIter != NULL; aNodeIter = aNodeIter->next)
335   {
336     aNodeIter->elem->RenderFiltered (theWorkspace, aFilter);
337   }
338
339   // Restore aspects
340   if (isLineSet)
341     theWorkspace->SetAspectLine (aBackAspectLine);
342   if (isFaceSet)
343     theWorkspace->SetAspectFace (aBackAspectFace);
344   if (isMarkerSet)
345     theWorkspace->SetAspectMarker (aBackAspectMarker);
346   if (isTextSet)
347     theWorkspace->SetAspectText (aBackAspectText);
348 }
349
350 // =======================================================================
351 // function : Clear
352 // purpose  :
353 // =======================================================================
354 void OpenGl_Group::Clear (const Standard_Boolean theToUpdateStructureMgr)
355 {
356   if (IsDeleted())
357   {
358     return;
359   }
360
361   OpenGl_Structure* aStruct = GlStruct();
362   const Handle(OpenGl_Context)& aCtx = aStruct->GlDriver()->GetSharedContext();
363
364   Release (aCtx);
365   Graphic3d_Group::Clear (theToUpdateStructureMgr);
366 }
367
368 // =======================================================================
369 // function : Release
370 // purpose  :
371 // =======================================================================
372 void OpenGl_Group::Release (const Handle(OpenGl_Context)& theGlCtx)
373 {
374   // Delete elements
375   while (myFirst != NULL)
376   {
377     OpenGl_ElementNode* aNext = myFirst->next;
378     OpenGl_Element::Destroy (theGlCtx, myFirst->elem);
379     delete myFirst;
380     myFirst = aNext;
381   }
382   myLast = NULL;
383
384   OpenGl_Element::Destroy (theGlCtx, myAspectLine);
385   OpenGl_Element::Destroy (theGlCtx, myAspectFace);
386   OpenGl_Element::Destroy (theGlCtx, myAspectMarker);
387   OpenGl_Element::Destroy (theGlCtx, myAspectText);
388 }