0024406: Visualization - crash on re-usage of the same primitive array
[occt.git] / src / OpenGl / OpenGl_Group.cxx
CommitLineData
b311480e 1// Created on: 2011-08-01
2// Created by: Sergey ZERCHANINOV
973c2be1 3// Copyright (c) 2011-2014 OPEN CASCADE SAS
b311480e 4//
973c2be1 5// This file is part of Open CASCADE Technology software library.
b311480e 6//
d5f74e42 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
973c2be1 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.
b311480e 12//
973c2be1 13// Alternatively, this file may be used under the terms of Open CASCADE
14// commercial license or contractual agreement.
2166f0fa 15
e276548b 16#include <OpenGl_Group.hxx>
b64d84be 17
18#include <OpenGl_GraphicDriver.hxx>
19#include <OpenGl_Flipper.hxx>
2166f0fa 20#include <OpenGl_PrimitiveArray.hxx>
b64d84be 21#include <OpenGl_StencilTest.hxx>
e276548b 22#include <OpenGl_Structure.hxx>
b64d84be 23#include <OpenGl_Text.hxx>
bf75be98 24#include <OpenGl_Workspace.hxx>
2166f0fa 25
b64d84be 26#include <Graphic3d_ArrayOfPrimitives.hxx>
27#include <Graphic3d_CUserDraw.hxx>
28#include <Graphic3d_GroupDefinitionError.hxx>
29
30IMPLEMENT_STANDARD_HANDLE (OpenGl_Group, Graphic3d_Group)
31IMPLEMENT_STANDARD_RTTIEXT(OpenGl_Group, Graphic3d_Group)
32
4269bd1b 33// =======================================================================
34// function : OpenGl_Group
35// purpose :
36// =======================================================================
b64d84be 37OpenGl_Group::OpenGl_Group (const Handle(Graphic3d_Structure)& theStruct)
38: Graphic3d_Group (theStruct),
39 myAspectLine(NULL),
e276548b 40 myAspectFace(NULL),
41 myAspectMarker(NULL),
42 myAspectText(NULL),
43 myFirst(NULL),
b64d84be 44 myLast(NULL),
45 myIsRaytracable (Standard_False),
46 myModificationState (0)
2166f0fa 47{
b64d84be 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 }
2166f0fa
SK
53}
54
4269bd1b 55// =======================================================================
56// function : ~OpenGl_Group
57// purpose :
58// =======================================================================
2166f0fa
SK
59OpenGl_Group::~OpenGl_Group()
60{
5e27df78 61 Release (Handle(OpenGl_Context)());
2166f0fa
SK
62}
63
4269bd1b 64// =======================================================================
b64d84be 65// function : UpdateAspectLine
4269bd1b 66// purpose :
67// =======================================================================
b64d84be 68void OpenGl_Group::UpdateAspectLine (const Standard_Boolean theIsGlobal)
2166f0fa 69{
b64d84be 70 if (!ContextLine.IsDef)
71 {
72 return;
73 }
74
2166f0fa
SK
75 if (theIsGlobal || myFirst == NULL)
76 {
77 if (myAspectLine == NULL)
fd4a6963 78 {
2166f0fa 79 myAspectLine = new OpenGl_AspectLine();
fd4a6963 80 }
b64d84be 81 myAspectLine->SetAspect (ContextLine);
2166f0fa
SK
82 }
83 else
84 {
85 OpenGl_AspectLine* anAspectLine = new OpenGl_AspectLine();
b64d84be 86 anAspectLine->SetAspect (ContextLine);
5322131b 87 AddElement (anAspectLine);
2166f0fa
SK
88 }
89}
90
4269bd1b 91// =======================================================================
b64d84be 92// function : UpdateAspectFace
4269bd1b 93// purpose :
94// =======================================================================
b64d84be 95void OpenGl_Group::UpdateAspectFace (const Standard_Boolean theIsGlobal)
2166f0fa 96{
b64d84be 97 if (!ContextFillArea.IsDef)
98 {
99 return;
100 }
101
2166f0fa
SK
102 if (theIsGlobal || myFirst == NULL)
103 {
104 if (myAspectFace == NULL)
bf75be98 105 {
2166f0fa 106 myAspectFace = new OpenGl_AspectFace();
bf75be98 107 }
b64d84be 108 myAspectFace->SetAspect (ContextFillArea);
2166f0fa
SK
109 }
110 else
111 {
112 OpenGl_AspectFace* anAspectFace = new OpenGl_AspectFace();
b64d84be 113 anAspectFace->SetAspect (ContextFillArea);
5322131b 114 AddElement (anAspectFace);
2166f0fa 115 }
e276548b 116
e276548b 117 if (myIsRaytracable)
118 {
b64d84be 119 ++myModificationState;
120 OpenGl_Structure* aStruct = GlStruct();
121 if (aStruct != NULL)
e276548b 122 {
b64d84be 123 aStruct->UpdateStateWithAncestorStructures();
e276548b 124 }
125 }
2166f0fa
SK
126}
127
4269bd1b 128// =======================================================================
b64d84be 129// function : UpdateAspectMarker
4269bd1b 130// purpose :
131// =======================================================================
b64d84be 132void OpenGl_Group::UpdateAspectMarker (const Standard_Boolean theIsGlobal)
2166f0fa 133{
b64d84be 134 if (!ContextMarker.IsDef)
135 {
136 return;
137 }
138
2166f0fa
SK
139 if (theIsGlobal || myFirst == NULL)
140 {
141 if (myAspectMarker == NULL)
a577aaab 142 {
2166f0fa 143 myAspectMarker = new OpenGl_AspectMarker();
a577aaab 144 }
b64d84be 145 myAspectMarker->SetAspect (ContextMarker);
2166f0fa
SK
146 }
147 else
148 {
149 OpenGl_AspectMarker* anAspectMarker = new OpenGl_AspectMarker();
b64d84be 150 anAspectMarker->SetAspect (ContextMarker);
5322131b 151 AddElement (anAspectMarker);
2166f0fa
SK
152 }
153}
154
4269bd1b 155// =======================================================================
b64d84be 156// function : UpdateAspectText
4269bd1b 157// purpose :
158// =======================================================================
b64d84be 159void OpenGl_Group::UpdateAspectText (const Standard_Boolean theIsGlobal)
2166f0fa 160{
b64d84be 161 if (!ContextText.IsDef)
162 {
163 return;
164 }
165
2166f0fa
SK
166 if (theIsGlobal || myFirst == NULL)
167 {
168 if (myAspectText == NULL)
fd4a6963 169 {
2166f0fa 170 myAspectText = new OpenGl_AspectText();
fd4a6963 171 }
b64d84be 172 myAspectText->SetAspect (ContextText);
2166f0fa
SK
173 }
174 else
175 {
176 OpenGl_AspectText* anAspectText = new OpenGl_AspectText();
b64d84be 177 anAspectText->SetAspect (ContextText);
5322131b 178 AddElement (anAspectText);
2166f0fa
SK
179 }
180}
181
4269bd1b 182// =======================================================================
b64d84be 183// function : AddPrimitiveArray
184// purpose :
185// =======================================================================
871fa103 186void 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)
b64d84be 191{
192 if (IsDeleted()
871fa103 193 || theAttribs.IsNull())
b64d84be 194 {
195 return;
196 }
197
871fa103 198 OpenGl_PrimitiveArray* anArray = new OpenGl_PrimitiveArray (theType, theIndices, theAttribs, theBounds);
b64d84be 199 AddElement (anArray);
200
871fa103 201 Graphic3d_Group::AddPrimitiveArray (theType, theIndices, theAttribs, theBounds, theToEvalMinMax);
b64d84be 202}
203
204// =======================================================================
205// function : Text
206// purpose :
207// =======================================================================
208void 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// =======================================================================
238void 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// =======================================================================
268void 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// =======================================================================
280void 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// =======================================================================
4269bd1b 288// function : AddElement
289// purpose :
290// =======================================================================
5322131b 291void OpenGl_Group::AddElement (OpenGl_Element* theElem)
2166f0fa 292{
e276548b 293 OpenGl_ElementNode *aNode = new OpenGl_ElementNode();
294
e276548b 295 aNode->elem = theElem;
296 aNode->next = NULL;
297 (myLast? myLast->next : myFirst) = aNode;
298 myLast = aNode;
299
e276548b 300 if (OpenGl_Raytrace::IsRaytracedElement (aNode))
301 {
302 myModificationState++;
303 myIsRaytracable = Standard_True;
2166f0fa 304
b64d84be 305 OpenGl_Structure* aStruct = GlStruct();
306 if (aStruct != NULL)
e276548b 307 {
b64d84be 308 aStruct->UpdateStateWithAncestorStructures();
309 aStruct->SetRaytracableWithAncestorStructures();
e276548b 310 }
311 }
2166f0fa
SK
312}
313
4269bd1b 314// =======================================================================
315// function : Render
316// purpose :
317// =======================================================================
bf75be98 318void OpenGl_Group::Render (const Handle(OpenGl_Workspace)& theWorkspace) const
2166f0fa
SK
319{
320 // Is rendering in ADD or IMMEDIATE mode?
4269bd1b 321 const Handle(OpenGl_RenderFilter)& aFilter = theWorkspace->GetRenderFilter();
2166f0fa
SK
322
323 // Setup aspects
bf75be98 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);
4269bd1b 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);
2166f0fa
SK
332
333 // Render group elements
bf75be98 334 for (OpenGl_ElementNode* aNodeIter = myFirst; aNodeIter != NULL; aNodeIter = aNodeIter->next)
2166f0fa 335 {
5322131b 336 aNodeIter->elem->RenderFiltered (theWorkspace, aFilter);
2166f0fa
SK
337 }
338
339 // Restore aspects
4269bd1b 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);
2166f0fa
SK
348}
349
4269bd1b 350// =======================================================================
b64d84be 351// function : Clear
352// purpose :
353// =======================================================================
354void 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// =======================================================================
4269bd1b 369// function : Release
370// purpose :
371// =======================================================================
5e27df78 372void 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}