0024739: TKOpenGl - port ray-tracing from OpenCL to GLSL for better integration and...
[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// =======================================================================
186void OpenGl_Group::AddPrimitiveArray (const Handle(Graphic3d_ArrayOfPrimitives)& thePrim,
187 const Standard_Boolean theToEvalMinMax)
188{
189 if (IsDeleted()
190 || !thePrim->IsValid())
191 {
192 return;
193 }
194
195 OpenGl_PrimitiveArray* anArray = new OpenGl_PrimitiveArray ((CALL_DEF_PARRAY *)thePrim->Array());
196 AddElement (anArray);
197
198 Graphic3d_Group::AddPrimitiveArray (thePrim, theToEvalMinMax);
199}
200
201// =======================================================================
202// function : Text
203// purpose :
204// =======================================================================
205void OpenGl_Group::Text (const Standard_CString theTextUtf,
206 const Graphic3d_Vertex& thePoint,
207 const Standard_Real theHeight,
208 const Quantity_PlaneAngle theAngle,
209 const Graphic3d_TextPath theTp,
210 const Graphic3d_HorizontalTextAlignment theHta,
211 const Graphic3d_VerticalTextAlignment theVta,
212 const Standard_Boolean theToEvalMinMax)
213{
214 if (IsDeleted())
215 {
216 return;
217 }
218
219 OpenGl_TextParam aParams;
220 OpenGl_Structure* aStruct = GlStruct();
221 aParams.Height = int ((theHeight < 2.0) ? aStruct->GlDriver()->DefaultTextHeight() : theHeight);
222 aParams.HAlign = theHta;
223 aParams.VAlign = theVta;
224 const OpenGl_Vec3 aPoint (thePoint.X(), thePoint.Y(), thePoint.Z());
225 OpenGl_Text* aText = new OpenGl_Text (theTextUtf, aPoint, aParams);
226 AddElement (aText);
227 Graphic3d_Group::Text (theTextUtf, thePoint, theHeight, theAngle,
228 theTp, theHta, theVta, theToEvalMinMax);
229}
230
231// =======================================================================
232// function : UserDraw
233// purpose :
234// =======================================================================
235void OpenGl_Group::UserDraw (const Standard_Address theObject,
236 const Standard_Boolean theToEvalMinMax,
237 const Standard_Boolean theContainsFacet)
238{
239 if (IsDeleted())
240 {
241 return;
242 }
243
244 OpenGl_Structure* aStruct = GlStruct();
245 if (aStruct->GlDriver()->UserDrawCallback() == NULL)
246 {
247 return;
248 }
249
250 Graphic3d_CUserDraw aUserDraw;
251 aUserDraw.Data = theObject;
252 aUserDraw.Bounds = theToEvalMinMax ? &myBounds : NULL;
253 OpenGl_Element* aUserDrawElem = aStruct->GlDriver()->UserDrawCallback()(&aUserDraw);
254 if (aUserDrawElem != NULL)
255 {
256 AddElement (aUserDrawElem);
257 }
258 Graphic3d_Group::UserDraw (theObject, theToEvalMinMax, theContainsFacet);
259}
260
261// =======================================================================
262// function : SetFlippingOptions
263// purpose :
264// =======================================================================
265void OpenGl_Group::SetFlippingOptions (const Standard_Boolean theIsEnabled,
266 const gp_Ax2& theRefPlane)
267{
268 OpenGl_Flipper* aFlipper = new OpenGl_Flipper (theRefPlane);
269 aFlipper->SetOptions (theIsEnabled);
270 AddElement (aFlipper);
271}
272
273// =======================================================================
274// function : SetStencilTestOptions
275// purpose :
276// =======================================================================
277void OpenGl_Group::SetStencilTestOptions (const Standard_Boolean theIsEnabled)
278{
279 OpenGl_StencilTest* aStencilTest = new OpenGl_StencilTest();
280 aStencilTest->SetOptions (theIsEnabled);
281 AddElement (aStencilTest);
282}
283
284// =======================================================================
4269bd1b 285// function : AddElement
286// purpose :
287// =======================================================================
5322131b 288void OpenGl_Group::AddElement (OpenGl_Element* theElem)
2166f0fa 289{
e276548b 290 OpenGl_ElementNode *aNode = new OpenGl_ElementNode();
291
e276548b 292 aNode->elem = theElem;
293 aNode->next = NULL;
294 (myLast? myLast->next : myFirst) = aNode;
295 myLast = aNode;
296
e276548b 297 if (OpenGl_Raytrace::IsRaytracedElement (aNode))
298 {
299 myModificationState++;
300 myIsRaytracable = Standard_True;
2166f0fa 301
b64d84be 302 OpenGl_Structure* aStruct = GlStruct();
303 if (aStruct != NULL)
e276548b 304 {
b64d84be 305 aStruct->UpdateStateWithAncestorStructures();
306 aStruct->SetRaytracableWithAncestorStructures();
e276548b 307 }
308 }
2166f0fa
SK
309}
310
4269bd1b 311// =======================================================================
312// function : Render
313// purpose :
314// =======================================================================
bf75be98 315void OpenGl_Group::Render (const Handle(OpenGl_Workspace)& theWorkspace) const
2166f0fa
SK
316{
317 // Is rendering in ADD or IMMEDIATE mode?
4269bd1b 318 const Handle(OpenGl_RenderFilter)& aFilter = theWorkspace->GetRenderFilter();
2166f0fa
SK
319
320 // Setup aspects
bf75be98 321 const OpenGl_AspectLine* aBackAspectLine = theWorkspace->AspectLine (Standard_False);
322 const OpenGl_AspectFace* aBackAspectFace = theWorkspace->AspectFace (Standard_False);
323 const OpenGl_AspectMarker* aBackAspectMarker = theWorkspace->AspectMarker (Standard_False);
324 const OpenGl_AspectText* aBackAspectText = theWorkspace->AspectText (Standard_False);
4269bd1b 325 Standard_Boolean isLineSet = myAspectLine && myAspectLine->RenderFiltered (theWorkspace, aFilter);
326 Standard_Boolean isFaceSet = myAspectFace && myAspectFace->RenderFiltered (theWorkspace, aFilter);
327 Standard_Boolean isMarkerSet = myAspectMarker && myAspectMarker->RenderFiltered (theWorkspace, aFilter);
328 Standard_Boolean isTextSet = myAspectText && myAspectText->RenderFiltered (theWorkspace, aFilter);
2166f0fa
SK
329
330 // Render group elements
bf75be98 331 for (OpenGl_ElementNode* aNodeIter = myFirst; aNodeIter != NULL; aNodeIter = aNodeIter->next)
2166f0fa 332 {
5322131b 333 aNodeIter->elem->RenderFiltered (theWorkspace, aFilter);
2166f0fa
SK
334 }
335
336 // Restore aspects
4269bd1b 337 if (isLineSet)
338 theWorkspace->SetAspectLine (aBackAspectLine);
339 if (isFaceSet)
340 theWorkspace->SetAspectFace (aBackAspectFace);
341 if (isMarkerSet)
342 theWorkspace->SetAspectMarker (aBackAspectMarker);
343 if (isTextSet)
344 theWorkspace->SetAspectText (aBackAspectText);
2166f0fa
SK
345}
346
4269bd1b 347// =======================================================================
b64d84be 348// function : Clear
349// purpose :
350// =======================================================================
351void OpenGl_Group::Clear (const Standard_Boolean theToUpdateStructureMgr)
352{
353 if (IsDeleted())
354 {
355 return;
356 }
357
358 OpenGl_Structure* aStruct = GlStruct();
359 const Handle(OpenGl_Context)& aCtx = aStruct->GlDriver()->GetSharedContext();
360
361 Release (aCtx);
362 Graphic3d_Group::Clear (theToUpdateStructureMgr);
363}
364
365// =======================================================================
4269bd1b 366// function : Release
367// purpose :
368// =======================================================================
5e27df78 369void OpenGl_Group::Release (const Handle(OpenGl_Context)& theGlCtx)
370{
371 // Delete elements
372 while (myFirst != NULL)
373 {
374 OpenGl_ElementNode* aNext = myFirst->next;
375 OpenGl_Element::Destroy (theGlCtx, myFirst->elem);
376 delete myFirst;
377 myFirst = aNext;
378 }
379 myLast = NULL;
380
381 OpenGl_Element::Destroy (theGlCtx, myAspectLine);
382 OpenGl_Element::Destroy (theGlCtx, myAspectFace);
383 OpenGl_Element::Destroy (theGlCtx, myAspectMarker);
384 OpenGl_Element::Destroy (theGlCtx, myAspectText);
385}