0026886: Visualization, TKV3d - eliminate global variables
[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>
c357e426 21#include <OpenGl_SceneGeometry.hxx>
b64d84be 22#include <OpenGl_StencilTest.hxx>
e276548b 23#include <OpenGl_Structure.hxx>
b64d84be 24#include <OpenGl_Text.hxx>
bf75be98 25#include <OpenGl_Workspace.hxx>
2166f0fa 26
b64d84be 27#include <Graphic3d_ArrayOfPrimitives.hxx>
b64d84be 28#include <Graphic3d_GroupDefinitionError.hxx>
29
b64d84be 30
92efcf78 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),
d4aaad5b 45 myIsRaytracable (Standard_False)
2166f0fa 46{
b64d84be 47 Handle(OpenGl_Structure) aStruct = Handle(OpenGl_Structure)::DownCast (myStructure->CStructure());
c04c30b3 48 if (aStruct.IsNull())
b64d84be 49 {
50 Graphic3d_GroupDefinitionError::Raise ("OpenGl_Group should be created by OpenGl_Structure!");
51 }
2166f0fa
SK
52}
53
4269bd1b 54// =======================================================================
55// function : ~OpenGl_Group
56// purpose :
57// =======================================================================
2166f0fa
SK
58OpenGl_Group::~OpenGl_Group()
59{
5e27df78 60 Release (Handle(OpenGl_Context)());
2166f0fa
SK
61}
62
4269bd1b 63// =======================================================================
b64d84be 64// function : UpdateAspectLine
4269bd1b 65// purpose :
66// =======================================================================
b64d84be 67void OpenGl_Group::UpdateAspectLine (const Standard_Boolean theIsGlobal)
2166f0fa 68{
b64d84be 69 if (!ContextLine.IsDef)
70 {
71 return;
72 }
73
2166f0fa
SK
74 if (theIsGlobal || myFirst == NULL)
75 {
76 if (myAspectLine == NULL)
fd4a6963 77 {
2166f0fa 78 myAspectLine = new OpenGl_AspectLine();
fd4a6963 79 }
b64d84be 80 myAspectLine->SetAspect (ContextLine);
2166f0fa
SK
81 }
82 else
83 {
84 OpenGl_AspectLine* anAspectLine = new OpenGl_AspectLine();
b64d84be 85 anAspectLine->SetAspect (ContextLine);
5322131b 86 AddElement (anAspectLine);
2166f0fa
SK
87 }
88}
89
4269bd1b 90// =======================================================================
b64d84be 91// function : UpdateAspectFace
4269bd1b 92// purpose :
93// =======================================================================
b64d84be 94void OpenGl_Group::UpdateAspectFace (const Standard_Boolean theIsGlobal)
2166f0fa 95{
b64d84be 96 if (!ContextFillArea.IsDef)
97 {
98 return;
99 }
100
2166f0fa
SK
101 if (theIsGlobal || myFirst == NULL)
102 {
103 if (myAspectFace == NULL)
bf75be98 104 {
2166f0fa 105 myAspectFace = new OpenGl_AspectFace();
bf75be98 106 }
b64d84be 107 myAspectFace->SetAspect (ContextFillArea);
2166f0fa
SK
108 }
109 else
110 {
111 OpenGl_AspectFace* anAspectFace = new OpenGl_AspectFace();
b64d84be 112 anAspectFace->SetAspect (ContextFillArea);
5322131b 113 AddElement (anAspectFace);
2166f0fa 114 }
e276548b 115
e276548b 116 if (myIsRaytracable)
117 {
b64d84be 118 OpenGl_Structure* aStruct = GlStruct();
119 if (aStruct != NULL)
e276548b 120 {
d4aaad5b 121 aStruct->UpdateStateIfRaytracable (Standard_False);
e276548b 122 }
123 }
2166f0fa
SK
124}
125
4269bd1b 126// =======================================================================
b64d84be 127// function : UpdateAspectMarker
4269bd1b 128// purpose :
129// =======================================================================
b64d84be 130void OpenGl_Group::UpdateAspectMarker (const Standard_Boolean theIsGlobal)
2166f0fa 131{
b64d84be 132 if (!ContextMarker.IsDef)
133 {
134 return;
135 }
136
2166f0fa
SK
137 if (theIsGlobal || myFirst == NULL)
138 {
139 if (myAspectMarker == NULL)
a577aaab 140 {
2166f0fa 141 myAspectMarker = new OpenGl_AspectMarker();
a577aaab 142 }
b64d84be 143 myAspectMarker->SetAspect (ContextMarker);
2166f0fa
SK
144 }
145 else
146 {
147 OpenGl_AspectMarker* anAspectMarker = new OpenGl_AspectMarker();
b64d84be 148 anAspectMarker->SetAspect (ContextMarker);
5322131b 149 AddElement (anAspectMarker);
2166f0fa
SK
150 }
151}
152
4269bd1b 153// =======================================================================
b64d84be 154// function : UpdateAspectText
4269bd1b 155// purpose :
156// =======================================================================
b64d84be 157void OpenGl_Group::UpdateAspectText (const Standard_Boolean theIsGlobal)
2166f0fa 158{
b64d84be 159 if (!ContextText.IsDef)
160 {
161 return;
162 }
163
2166f0fa
SK
164 if (theIsGlobal || myFirst == NULL)
165 {
166 if (myAspectText == NULL)
fd4a6963 167 {
2166f0fa 168 myAspectText = new OpenGl_AspectText();
fd4a6963 169 }
b64d84be 170 myAspectText->SetAspect (ContextText);
2166f0fa
SK
171 }
172 else
173 {
174 OpenGl_AspectText* anAspectText = new OpenGl_AspectText();
b64d84be 175 anAspectText->SetAspect (ContextText);
5322131b 176 AddElement (anAspectText);
2166f0fa
SK
177 }
178}
179
b64d84be 180// =======================================================================
181// function : AddPrimitiveArray
182// purpose :
183// =======================================================================
871fa103 184void 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)
b64d84be 189{
190 if (IsDeleted()
871fa103 191 || theAttribs.IsNull())
b64d84be 192 {
193 return;
194 }
195
8d3f219f 196 OpenGl_Structure* aStruct = GlStruct();
197 const OpenGl_GraphicDriver* aDriver = aStruct->GlDriver();
198
199 OpenGl_PrimitiveArray* anArray = new OpenGl_PrimitiveArray (aDriver, theType, theIndices, theAttribs, theBounds);
b64d84be 200 AddElement (anArray);
201
871fa103 202 Graphic3d_Group::AddPrimitiveArray (theType, theIndices, theAttribs, theBounds, theToEvalMinMax);
b64d84be 203}
204
205// =======================================================================
206// function : Text
207// purpose :
208// =======================================================================
209void 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)
217{
218 if (IsDeleted())
219 {
220 return;
221 }
222
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);
230 AddElement (aText);
231 Graphic3d_Group::Text (theTextUtf, thePoint, theHeight, theAngle,
232 theTp, theHta, theVta, theToEvalMinMax);
233}
234
ce01ec26 235// =======================================================================
236// function : Text
237// purpose :
238// =======================================================================
239void 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{
248 if (IsDeleted())
249 {
250 return;
251 }
252
253 OpenGl_TextParam aParams;
254 OpenGl_Structure* aStruct = GlStruct();
255
256 aParams.Height = int ((theHeight < 2.0) ? aStruct->GlDriver()->DefaultTextHeight() : theHeight);
257 aParams.HAlign = theHTA;
258 aParams.VAlign = theVTA;
259
260 OpenGl_Text* aText = new OpenGl_Text (theTextUtf, theOrientation, aParams);
261
262 AddElement (aText);
263
264 Graphic3d_Group::Text (theTextUtf,
265 theOrientation,
266 theHeight,
267 theAngle,
268 theTp,
269 theHTA,
270 theVTA,
271 theToEvalMinMax);
272
273}
274
b64d84be 275// =======================================================================
276// function : SetFlippingOptions
277// purpose :
278// =======================================================================
279void OpenGl_Group::SetFlippingOptions (const Standard_Boolean theIsEnabled,
280 const gp_Ax2& theRefPlane)
281{
282 OpenGl_Flipper* aFlipper = new OpenGl_Flipper (theRefPlane);
283 aFlipper->SetOptions (theIsEnabled);
284 AddElement (aFlipper);
285}
286
287// =======================================================================
288// function : SetStencilTestOptions
289// purpose :
290// =======================================================================
291void OpenGl_Group::SetStencilTestOptions (const Standard_Boolean theIsEnabled)
292{
293 OpenGl_StencilTest* aStencilTest = new OpenGl_StencilTest();
294 aStencilTest->SetOptions (theIsEnabled);
295 AddElement (aStencilTest);
296}
297
4269bd1b 298// =======================================================================
299// function : AddElement
300// purpose :
301// =======================================================================
5322131b 302void OpenGl_Group::AddElement (OpenGl_Element* theElem)
2166f0fa 303{
e276548b 304 OpenGl_ElementNode *aNode = new OpenGl_ElementNode();
305
e276548b 306 aNode->elem = theElem;
307 aNode->next = NULL;
308 (myLast? myLast->next : myFirst) = aNode;
309 myLast = aNode;
310
e276548b 311 if (OpenGl_Raytrace::IsRaytracedElement (aNode))
312 {
e276548b 313 myIsRaytracable = Standard_True;
2166f0fa 314
b64d84be 315 OpenGl_Structure* aStruct = GlStruct();
316 if (aStruct != NULL)
e276548b 317 {
d4aaad5b 318 aStruct->UpdateStateIfRaytracable (Standard_False);
e276548b 319 }
320 }
2166f0fa
SK
321}
322
4269bd1b 323// =======================================================================
324// function : Render
325// purpose :
326// =======================================================================
bf75be98 327void OpenGl_Group::Render (const Handle(OpenGl_Workspace)& theWorkspace) const
2166f0fa
SK
328{
329 // Is rendering in ADD or IMMEDIATE mode?
4269bd1b 330 const Handle(OpenGl_RenderFilter)& aFilter = theWorkspace->GetRenderFilter();
2166f0fa
SK
331
332 // Setup aspects
bf75be98 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);
4269bd1b 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);
2166f0fa
SK
341
342 // Render group elements
bf75be98 343 for (OpenGl_ElementNode* aNodeIter = myFirst; aNodeIter != NULL; aNodeIter = aNodeIter->next)
2166f0fa 344 {
5322131b 345 aNodeIter->elem->RenderFiltered (theWorkspace, aFilter);
2166f0fa
SK
346 }
347
348 // Restore aspects
4269bd1b 349 if (isLineSet)
350 theWorkspace->SetAspectLine (aBackAspectLine);
351 if (isFaceSet)
352 theWorkspace->SetAspectFace (aBackAspectFace);
353 if (isMarkerSet)
354 theWorkspace->SetAspectMarker (aBackAspectMarker);
355 if (isTextSet)
356 theWorkspace->SetAspectText (aBackAspectText);
2166f0fa
SK
357}
358
b64d84be 359// =======================================================================
360// function : Clear
361// purpose :
362// =======================================================================
363void OpenGl_Group::Clear (const Standard_Boolean theToUpdateStructureMgr)
364{
365 if (IsDeleted())
366 {
367 return;
368 }
369
370 OpenGl_Structure* aStruct = GlStruct();
371 const Handle(OpenGl_Context)& aCtx = aStruct->GlDriver()->GetSharedContext();
372
373 Release (aCtx);
374 Graphic3d_Group::Clear (theToUpdateStructureMgr);
d4aaad5b 375
376 myIsRaytracable = Standard_False;
b64d84be 377}
378
4269bd1b 379// =======================================================================
380// function : Release
381// purpose :
382// =======================================================================
5e27df78 383void OpenGl_Group::Release (const Handle(OpenGl_Context)& theGlCtx)
384{
385 // Delete elements
386 while (myFirst != NULL)
387 {
388 OpenGl_ElementNode* aNext = myFirst->next;
10b9c7df 389 OpenGl_Element::Destroy (theGlCtx.operator->(), myFirst->elem);
5e27df78 390 delete myFirst;
391 myFirst = aNext;
392 }
393 myLast = NULL;
394
10b9c7df 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);
5e27df78 399}