0025442: Visualization, TKOpenGl - prevent inclusion of system header glxext.h
[occt.git] / src / OpenGl / OpenGl_Structure.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.
b311480e 15
4269bd1b 16#include <OpenGl_CappingAlgo.hxx>
17#include <OpenGl_Context.hxx>
30f0ad28 18#include <OpenGl_GlCore11.hxx>
63bcc448 19#include <OpenGl_GraphicDriver.hxx>
30f0ad28 20#include <OpenGl_ShaderManager.hxx>
21#include <OpenGl_ShaderProgram.hxx>
2bd4bfac 22#include <OpenGl_StructureShadow.hxx>
2166f0fa 23#include <OpenGl_telem_util.hxx>
30f0ad28 24#include <OpenGl_Vec.hxx>
25#include <OpenGl_View.hxx>
26#include <OpenGl_Workspace.hxx>
2166f0fa 27
51b10cd4 28#include <Graphic3d_SequenceOfHClipPlane_Handle.hxx>
b859a34d 29
63bcc448 30IMPLEMENT_STANDARD_HANDLE (OpenGl_Structure, Graphic3d_CStructure)
31IMPLEMENT_STANDARD_RTTIEXT(OpenGl_Structure, Graphic3d_CStructure)
32
27eed937 33//! Auxiliary class for bounding box presentation
34class OpenGl_BndBoxPrs : public OpenGl_Element
35{
36
37public:
38
39 //! Main constructor
b7cd4ba7 40 OpenGl_BndBoxPrs (const Graphic3d_BndBox4f& theBndBox)
41 {
42 const float Xm = theBndBox.CornerMin().x();
43 const float Ym = theBndBox.CornerMin().y();
44 const float Zm = theBndBox.CornerMin().z();
45 const float XM = theBndBox.CornerMax().x();
46 const float YM = theBndBox.CornerMax().y();
47 const float ZM = theBndBox.CornerMax().z();
48
27eed937 49 myVerts[0] = OpenGl_Vec3 (Xm, Ym, Zm);
50 myVerts[1] = OpenGl_Vec3 (Xm, Ym, ZM);
51 myVerts[2] = OpenGl_Vec3 (Xm, YM, ZM);
52 myVerts[3] = OpenGl_Vec3 (Xm, YM, Zm);
53 myVerts[4] = OpenGl_Vec3 (Xm, Ym, Zm);
54 myVerts[5] = OpenGl_Vec3 (XM, Ym, Zm);
55 myVerts[6] = OpenGl_Vec3 (XM, Ym, ZM);
56 myVerts[7] = OpenGl_Vec3 (XM, YM, ZM);
57 myVerts[8] = OpenGl_Vec3 (XM, YM, Zm);
58 myVerts[9] = OpenGl_Vec3 (XM, Ym, Zm);
59 myVerts[10] = OpenGl_Vec3 (XM, YM, Zm);
60 myVerts[11] = OpenGl_Vec3 (Xm, YM, Zm);
61 myVerts[12] = OpenGl_Vec3 (Xm, YM, ZM);
62 myVerts[13] = OpenGl_Vec3 (XM, YM, ZM);
63 myVerts[14] = OpenGl_Vec3 (XM, Ym, ZM);
64 myVerts[15] = OpenGl_Vec3 (Xm, Ym, ZM);
65 }
66
67 //! Render presentation
68 virtual void Render (const Handle(OpenGl_Workspace)& theWorkspace) const
69 {
ca3c13d1 70 #if !defined(GL_ES_VERSION_2_0)
27eed937 71 // Apply line aspect
72 const OpenGl_AspectLine* anAspectLine = theWorkspace->AspectLine (Standard_True);
73 const Handle(OpenGl_Texture) aPrevTexture = theWorkspace->DisableTexture();
74
75 glDisable (GL_LIGHTING);
679ecdee 76 if ((theWorkspace->NamedStatus & OPENGL_NS_IMMEDIATE) != 0)
27eed937 77 {
78 glDepthMask (GL_FALSE);
79 }
80
81 // Use highlight colors
ca3c13d1 82 theWorkspace->GetGlContext()->core11->glColor3fv ((theWorkspace->NamedStatus & OPENGL_NS_HIGHLIGHT) ? theWorkspace->HighlightColor->rgb : anAspectLine->Color().rgb);
27eed937 83
84 glEnableClientState (GL_VERTEX_ARRAY);
85 glVertexPointer (3, GL_FLOAT, 0, (GLfloat* )&myVerts);
86 glDrawArrays (GL_LINE_STRIP, 0, 16);
87 glDisableClientState (GL_VERTEX_ARRAY);
88
89 // restore aspects
90 if (!aPrevTexture.IsNull())
91 {
92 theWorkspace->EnableTexture (aPrevTexture);
93 }
ca3c13d1 94 #endif
27eed937 95 }
96
97 //! Release graphical resources
10b9c7df 98 virtual void Release (OpenGl_Context*)
27eed937 99 {
100 //
101 }
102
103protected:
104
105 //! Protected destructor
106 virtual ~OpenGl_BndBoxPrs() {}
107
108private:
109
110 OpenGl_Vec3 myVerts[16]; //!< vertices array
111
112public:
113
114 DEFINE_STANDARD_ALLOC
115
116};
2166f0fa
SK
117
118/*----------------------------------------------------------------------*/
119
4269bd1b 120// =======================================================================
121// function : call_util_transpose_mat
122// purpose :
123// =======================================================================
2166f0fa
SK
124static void call_util_transpose_mat (float tmat[16], float mat[4][4])
125{
126 int i, j;
127
128 for (i=0; i<4; i++)
129 for (j=0; j<4; j++)
130 tmat[j*4+i] = mat[i][j];
131}
132
4269bd1b 133// =======================================================================
134// function : OpenGl_Structure
135// purpose :
136// =======================================================================
63bcc448 137OpenGl_Structure::OpenGl_Structure (const Handle(Graphic3d_StructureManager)& theManager)
138: Graphic3d_CStructure (theManager),
139 myTransformation(NULL),
2166f0fa 140 myTransPers(NULL),
2166f0fa
SK
141 myAspectLine(NULL),
142 myAspectFace(NULL),
143 myAspectMarker(NULL),
144 myAspectText(NULL),
2166f0fa 145 myHighlightColor(NULL),
59f45b7c 146 myNamedStatus(0),
fc73a202 147 myZLayer(0),
148 myIsRaytracable (Standard_False),
b7cd4ba7 149 myModificationState (0),
7d9e854b 150 myIsCulled (Standard_True),
151 myIsMirrored (Standard_False)
2166f0fa 152{
63bcc448 153 UpdateNamedStatus();
2166f0fa
SK
154}
155
4269bd1b 156// =======================================================================
157// function : ~OpenGl_Structure
158// purpose :
159// =======================================================================
5e27df78 160OpenGl_Structure::~OpenGl_Structure()
2166f0fa 161{
5e27df78 162 Release (Handle(OpenGl_Context)());
163 delete myTransformation; myTransformation = NULL;
164 delete myTransPers; myTransPers = NULL;
2166f0fa
SK
165}
166
4269bd1b 167// =======================================================================
63bcc448 168// function : UpdateAspects
4269bd1b 169// purpose :
170// =======================================================================
63bcc448 171void OpenGl_Structure::UpdateAspects()
2166f0fa 172{
63bcc448 173 SetTransformPersistence (TransformPersistence);
174
175 if (ContextLine.IsDef)
176 SetAspectLine (ContextLine);
177
178 if (ContextFillArea.IsDef)
179 SetAspectFace (ContextFillArea);
180
181 if (ContextMarker.IsDef)
182 SetAspectMarker (ContextMarker);
183
184 if (ContextText.IsDef)
185 SetAspectText (ContextText);
186}
187
188// =======================================================================
189// function : UpdateTransformation
190// purpose :
191// =======================================================================
192void OpenGl_Structure::UpdateTransformation()
193{
194 if (myTransformation == NULL)
e276548b 195 {
5e27df78 196 myTransformation = new OpenGl_Matrix();
e276548b 197 }
2166f0fa 198
7d9e854b 199 Standard_ShortReal (*aMat)[4] = Graphic3d_CStructure::Transformation;
200
201 Standard_ShortReal aDet =
202 aMat[0][0] * (aMat[1][1] * aMat[2][2] - aMat[2][1] * aMat[1][2]) -
203 aMat[0][1] * (aMat[1][0] * aMat[2][2] - aMat[2][0] * aMat[1][2]) +
204 aMat[0][2] * (aMat[1][0] * aMat[2][1] - aMat[2][0] * aMat[1][1]);
205
206 // Determinant of transform matrix less then 0 means that mirror transform applied.
207 myIsMirrored = aDet < 0.0f;
208
63bcc448 209 matcpy (myTransformation->mat, &Graphic3d_CStructure::Transformation[0][0]);
e276548b 210
e276548b 211 if (myIsRaytracable)
212 {
213 UpdateStateWithAncestorStructures();
214 }
2166f0fa
SK
215}
216
4269bd1b 217// =======================================================================
218// function : SetTransformPersistence
219// purpose :
220// =======================================================================
2166f0fa
SK
221void OpenGl_Structure::SetTransformPersistence(const CALL_DEF_TRANSFORM_PERSISTENCE &ATransPers)
222{
223 if (!myTransPers)
224 myTransPers = new TEL_TRANSFORM_PERSISTENCE;
225
226 myTransPers->mode = ATransPers.Flag;
227 myTransPers->pointX = ATransPers.Point.x;
228 myTransPers->pointY = ATransPers.Point.y;
229 myTransPers->pointZ = ATransPers.Point.z;
b7cd4ba7 230 MarkAsNotCulled();
2166f0fa
SK
231}
232
4269bd1b 233// =======================================================================
234// function : SetAspectLine
235// purpose :
236// =======================================================================
fd4a6963 237void OpenGl_Structure::SetAspectLine (const CALL_DEF_CONTEXTLINE &theAspect)
2166f0fa
SK
238{
239 if (!myAspectLine)
fd4a6963 240 {
5e27df78 241 myAspectLine = new OpenGl_AspectLine();
fd4a6963 242 }
243 myAspectLine->SetAspect (theAspect);
2166f0fa
SK
244}
245
4269bd1b 246// =======================================================================
247// function : SetAspectFace
248// purpose :
249// =======================================================================
fd4a6963 250void OpenGl_Structure::SetAspectFace (const CALL_DEF_CONTEXTFILLAREA& theAspect)
2166f0fa
SK
251{
252 if (!myAspectFace)
bf75be98 253 {
5e27df78 254 myAspectFace = new OpenGl_AspectFace();
bf75be98 255 }
fd4a6963 256 myAspectFace->SetAspect (theAspect);
e276548b 257
e276548b 258 if (myIsRaytracable)
259 {
260 UpdateStateWithAncestorStructures();
261 }
2166f0fa
SK
262}
263
4269bd1b 264// =======================================================================
265// function : SetAspectMarker
266// purpose :
267// =======================================================================
fd4a6963 268void OpenGl_Structure::SetAspectMarker (const CALL_DEF_CONTEXTMARKER& theAspect)
2166f0fa
SK
269{
270 if (!myAspectMarker)
a577aaab 271 {
5e27df78 272 myAspectMarker = new OpenGl_AspectMarker();
a577aaab 273 }
fd4a6963 274 myAspectMarker->SetAspect (theAspect);
2166f0fa
SK
275}
276
4269bd1b 277// =======================================================================
278// function : SetAspectText
279// purpose :
280// =======================================================================
fd4a6963 281void OpenGl_Structure::SetAspectText (const CALL_DEF_CONTEXTTEXT &theAspect)
2166f0fa
SK
282{
283 if (!myAspectText)
fd4a6963 284 {
5e27df78 285 myAspectText = new OpenGl_AspectText();
fd4a6963 286 }
287 myAspectText->SetAspect (theAspect);
2166f0fa
SK
288}
289
4269bd1b 290// =======================================================================
b64d84be 291// function : clearHighlightBox
4269bd1b 292// purpose :
293// =======================================================================
b64d84be 294void OpenGl_Structure::clearHighlightBox (const Handle(OpenGl_Context)& theGlCtx)
2166f0fa 295{
b64d84be 296 if (!myHighlightBox.IsNull())
5e27df78 297 {
298 myHighlightBox->Release (theGlCtx);
b64d84be 299 myHighlightBox.Nullify();
2166f0fa
SK
300 }
301}
302
4269bd1b 303// =======================================================================
63bcc448 304// function : HighlightWithColor
305// purpose :
306// =======================================================================
307void OpenGl_Structure::HighlightWithColor (const Graphic3d_Vec3& theColor,
308 const Standard_Boolean theToCreate)
309{
7d9e854b 310 const Handle(OpenGl_Context)& aContext = GlDriver()->GetSharedContext();
63bcc448 311 if (theToCreate)
7d9e854b 312 setHighlightColor (aContext, theColor);
63bcc448 313 else
7d9e854b 314 clearHighlightColor (aContext);
63bcc448 315}
316
317// =======================================================================
318// function : HighlightWithBndBox
319// purpose :
320// =======================================================================
b64d84be 321void OpenGl_Structure::HighlightWithBndBox (const Handle(Graphic3d_Structure)& theStruct,
322 const Standard_Boolean theToCreate)
63bcc448 323{
7d9e854b 324 const Handle(OpenGl_Context)& aContext = GlDriver()->GetSharedContext();
b64d84be 325 if (!theToCreate)
326 {
7d9e854b 327 clearHighlightBox (aContext);
b64d84be 328 return;
329 }
330
331 if (!myHighlightBox.IsNull())
332 {
7d9e854b 333 myHighlightBox->Release (aContext);
b64d84be 334 }
63bcc448 335 else
b64d84be 336 {
337 myHighlightBox = new OpenGl_Group (theStruct);
338 }
339
340 CALL_DEF_CONTEXTLINE& aContextLine = myHighlightBox->ChangeContextLine();
341 aContextLine.IsDef = 1;
b7cd4ba7 342 aContextLine.Color = HighlightColor;
b64d84be 343 aContextLine.LineType = Aspect_TOL_SOLID;
344 aContextLine.Width = 1.0f;
345 myHighlightBox->UpdateAspectLine (Standard_True);
346
b7cd4ba7 347 OpenGl_BndBoxPrs* aBndBoxPrs = new OpenGl_BndBoxPrs (myBndBox);
b64d84be 348 myHighlightBox->AddElement (aBndBoxPrs);
63bcc448 349}
350
351// =======================================================================
b64d84be 352// function : setHighlightColor
4269bd1b 353// purpose :
354// =======================================================================
b64d84be 355void OpenGl_Structure::setHighlightColor (const Handle(OpenGl_Context)& theGlCtx,
63bcc448 356 const Graphic3d_Vec3& theColor)
2166f0fa 357{
b64d84be 358 clearHighlightBox (theGlCtx);
5e27df78 359 if (myHighlightColor == NULL)
360 {
361 myHighlightColor = new TEL_COLOUR();
362 }
2166f0fa 363
63bcc448 364 myHighlightColor->rgb[0] = theColor.r();
365 myHighlightColor->rgb[1] = theColor.g();
366 myHighlightColor->rgb[2] = theColor.b();
2166f0fa
SK
367 myHighlightColor->rgb[3] = 1.F;
368}
369
4269bd1b 370// =======================================================================
b64d84be 371// function : clearHighlightColor
4269bd1b 372// purpose :
373// =======================================================================
b64d84be 374void OpenGl_Structure::clearHighlightColor (const Handle(OpenGl_Context)& theGlCtx)
2166f0fa 375{
b64d84be 376 clearHighlightBox(theGlCtx);
5e27df78 377 delete myHighlightColor;
378 myHighlightColor = NULL;
2166f0fa
SK
379}
380
4269bd1b 381// =======================================================================
63bcc448 382// function : UpdateNamedStatus
e276548b 383// purpose :
384// =======================================================================
63bcc448 385void OpenGl_Structure::UpdateNamedStatus()
e276548b 386{
63bcc448 387 myNamedStatus = 0;
388 if (highlight) myNamedStatus |= OPENGL_NS_HIGHLIGHT;
389 if (!visible) myNamedStatus |= OPENGL_NS_HIDE;
e276548b 390
e276548b 391 if (myIsRaytracable)
392 {
393 UpdateStateWithAncestorStructures();
394 }
e276548b 395}
396
e276548b 397// =======================================================================
398// function : RegisterAncestorStructure
399// purpose :
400// =======================================================================
401void OpenGl_Structure::RegisterAncestorStructure (const OpenGl_Structure* theStructure) const
402{
403 for (OpenGl_ListOfStructure::Iterator anIt (myAncestorStructures); anIt.More(); anIt.Next())
404 {
405 if (anIt.Value() == theStructure)
406 {
407 return;
5322131b 408 }
e276548b 409 }
410
411 myAncestorStructures.Append (theStructure);
412}
413
414// =======================================================================
415// function : UnregisterAncestorStructure
416// purpose :
417// =======================================================================
418void OpenGl_Structure::UnregisterAncestorStructure (const OpenGl_Structure* theStructure) const
419{
420 for (OpenGl_ListOfStructure::Iterator anIt (myAncestorStructures); anIt.More(); anIt.Next())
421 {
422 if (anIt.Value() == theStructure)
423 {
424 myAncestorStructures.Remove (anIt);
425 return;
5322131b 426 }
e276548b 427 }
428}
429
430// =======================================================================
d5af8626 431// function : UnregisterFromAncestorStructure
432// purpose :
433// =======================================================================
434void OpenGl_Structure::UnregisterFromAncestorStructure() const
435{
436 for (OpenGl_ListOfStructure::Iterator anIta (myAncestorStructures); anIta.More(); anIta.Next())
437 {
438 OpenGl_Structure* anAncestor = const_cast<OpenGl_Structure*> (anIta.ChangeValue());
439
440 for (OpenGl_ListOfStructure::Iterator anIts (anAncestor->myConnected); anIts.More(); anIts.Next())
441 {
442 if (anIts.Value() == this)
443 {
444 anAncestor->myConnected.Remove (anIts);
445 return;
5322131b 446 }
d5af8626 447 }
448 }
449}
450
451// =======================================================================
e276548b 452// function : UpdateStateWithAncestorStructures
453// purpose :
454// =======================================================================
455void OpenGl_Structure::UpdateStateWithAncestorStructures() const
456{
457 myModificationState++;
458
459 for (OpenGl_ListOfStructure::Iterator anIt (myAncestorStructures); anIt.More(); anIt.Next())
460 {
461 anIt.Value()->UpdateStateWithAncestorStructures();
462 }
463}
464
465// =======================================================================
466// function : UpdateRaytracableWithAncestorStructures
467// purpose :
468// =======================================================================
469void OpenGl_Structure::UpdateRaytracableWithAncestorStructures() const
470{
471 myIsRaytracable = OpenGl_Raytrace::IsRaytracedStructure (this);
472
473 if (!myIsRaytracable)
474 {
475 for (OpenGl_ListOfStructure::Iterator anIt (myAncestorStructures); anIt.More(); anIt.Next())
476 {
477 anIt.Value()->UpdateRaytracableWithAncestorStructures();
478 }
479 }
480}
481
482// =======================================================================
483// function : SetRaytracableWithAncestorStructures
484// purpose :
485// =======================================================================
486void OpenGl_Structure::SetRaytracableWithAncestorStructures() const
487{
488 myIsRaytracable = Standard_True;
489
490 for (OpenGl_ListOfStructure::Iterator anIt (myAncestorStructures); anIt.More(); anIt.Next())
491 {
492 if (!anIt.Value()->IsRaytracable())
493 {
494 anIt.Value()->SetRaytracableWithAncestorStructures();
495 }
496 }
497}
498
e276548b 499// =======================================================================
4269bd1b 500// function : Connect
501// purpose :
502// =======================================================================
63bcc448 503void OpenGl_Structure::Connect (Graphic3d_CStructure& theStructure)
2166f0fa 504{
63bcc448 505 OpenGl_Structure* aStruct = (OpenGl_Structure* )&theStructure;
e276548b 506 Disconnect (theStructure);
63bcc448 507 myConnected.Append (aStruct);
e276548b 508
63bcc448 509 if (aStruct->IsRaytracable())
e276548b 510 {
511 UpdateStateWithAncestorStructures();
512 SetRaytracableWithAncestorStructures();
513 }
514
63bcc448 515 aStruct->RegisterAncestorStructure (this);
2166f0fa
SK
516}
517
4269bd1b 518// =======================================================================
519// function : Disconnect
520// purpose :
521// =======================================================================
63bcc448 522void OpenGl_Structure::Disconnect (Graphic3d_CStructure& theStructure)
2166f0fa 523{
63bcc448 524 OpenGl_Structure* aStruct = (OpenGl_Structure* )&theStructure;
525 for (OpenGl_ListOfStructure::Iterator anIter (myConnected); anIter.More(); anIter.Next())
2166f0fa
SK
526 {
527 // Check for the given structure
63bcc448 528 if (anIter.Value() == aStruct)
2166f0fa 529 {
63bcc448 530 myConnected.Remove (anIter);
e276548b 531
63bcc448 532 if (aStruct->IsRaytracable())
e276548b 533 {
534 UpdateStateWithAncestorStructures();
535 UpdateRaytracableWithAncestorStructures();
536 }
537
63bcc448 538 aStruct->UnregisterAncestorStructure (this);
2166f0fa
SK
539 return;
540 }
2166f0fa
SK
541 }
542}
543
4269bd1b 544// =======================================================================
b64d84be 545// function : NewGroup
4269bd1b 546// purpose :
547// =======================================================================
b64d84be 548Handle(Graphic3d_Group) OpenGl_Structure::NewGroup (const Handle(Graphic3d_Structure)& theStruct)
2166f0fa 549{
b64d84be 550 Handle(OpenGl_Group) aGroup = new OpenGl_Group (theStruct);
551 myGroups.Append (aGroup);
552 return aGroup;
2166f0fa
SK
553}
554
4269bd1b 555// =======================================================================
556// function : RemoveGroup
557// purpose :
558// =======================================================================
b64d84be 559void OpenGl_Structure::RemoveGroup (const Handle(Graphic3d_Group)& theGroup)
2166f0fa 560{
b64d84be 561 if (theGroup.IsNull())
562 {
563 return;
564 }
565
566 for (Graphic3d_SequenceOfGroup::Iterator aGroupIter (myGroups); aGroupIter.More(); aGroupIter.Next())
2166f0fa
SK
567 {
568 // Check for the given group
b64d84be 569 if (aGroupIter.Value() == theGroup)
2166f0fa 570 {
b64d84be 571 theGroup->Clear (Standard_False);
e276548b 572
b64d84be 573 if (((OpenGl_Group* )theGroup.operator->())->IsRaytracable())
e276548b 574 {
575 UpdateStateWithAncestorStructures();
576 UpdateRaytracableWithAncestorStructures();
577 }
e276548b 578
b64d84be 579 myGroups.Remove (aGroupIter);
2166f0fa
SK
580 return;
581 }
2166f0fa
SK
582 }
583}
584
4269bd1b 585// =======================================================================
586// function : Clear
587// purpose :
588// =======================================================================
63bcc448 589void OpenGl_Structure::Clear()
590{
591 Clear (GlDriver()->GetSharedContext());
592}
593
594// =======================================================================
595// function : Clear
596// purpose :
597// =======================================================================
5e27df78 598void OpenGl_Structure::Clear (const Handle(OpenGl_Context)& theGlCtx)
2166f0fa 599{
e276548b 600 Standard_Boolean aRaytracableGroupDeleted (Standard_False);
e276548b 601
5e27df78 602 // Release groups
b64d84be 603 for (OpenGl_Structure::GroupIterator aGroupIter (myGroups); aGroupIter.More(); aGroupIter.Next())
2166f0fa 604 {
b64d84be 605 aRaytracableGroupDeleted |= aGroupIter.Value()->IsRaytracable();
5322131b 606
2166f0fa 607 // Delete objects
b64d84be 608 aGroupIter.ChangeValue()->Release (theGlCtx);
2166f0fa
SK
609 }
610 myGroups.Clear();
e276548b 611
e276548b 612 if (aRaytracableGroupDeleted)
613 {
614 UpdateStateWithAncestorStructures();
615 UpdateRaytracableWithAncestorStructures();
616 }
b7cd4ba7 617
618 Is2dText = Standard_False;
619 IsForHighlight = Standard_False;
2166f0fa
SK
620}
621
4269bd1b 622// =======================================================================
0717ddc1 623// function : RenderGeometry
624// purpose :
625// =======================================================================
7d9e854b 626void OpenGl_Structure::RenderGeometry (const Handle(OpenGl_Workspace) &theWorkspace) const
0717ddc1 627{
628 // Render groups
629 const Graphic3d_SequenceOfGroup& aGroups = DrawGroups();
630 for (OpenGl_Structure::GroupIterator aGroupIter (aGroups); aGroupIter.More(); aGroupIter.Next())
631 {
7d9e854b 632 aGroupIter.Value()->Render (theWorkspace);
0717ddc1 633 }
634}
635
636// =======================================================================
4269bd1b 637// function : Render
638// purpose :
639// =======================================================================
7d9e854b 640void OpenGl_Structure::Render (const Handle(OpenGl_Workspace) &theWorkspace) const
2166f0fa
SK
641{
642 // Process the structure only if visible
7d9e854b 643 if (myNamedStatus & OPENGL_NS_HIDE)
644 {
2166f0fa 645 return;
7d9e854b 646 }
2166f0fa 647
7d9e854b 648 const Handle(OpenGl_Context)& aContext = theWorkspace->GetGlContext();
b7cd4ba7 649
2166f0fa 650 // Render named status
7d9e854b 651 const Standard_Integer aNamedStatus = theWorkspace->NamedStatus;
652 theWorkspace->NamedStatus |= myNamedStatus;
2166f0fa
SK
653
654 // Is rendering in ADD or IMMEDIATE mode?
7d9e854b 655 const Standard_Boolean isImmediate = (theWorkspace->NamedStatus & OPENGL_NS_IMMEDIATE) != 0;
656
657 // Do we need to restore GL_NORMALIZE?
658 Standard_Boolean anOldGlNormalize = aContext->IsGlNormalizeEnabled();
2166f0fa
SK
659
660 // Apply local transformation
7d9e854b 661 GLint aMatrixMode = 0;
662 const OpenGl_Matrix* aLocalTrsf = NULL;
2166f0fa
SK
663 if (myTransformation)
664 {
ca3c13d1 665 #if !defined(GL_ES_VERSION_2_0)
7d9e854b 666
667 Standard_ShortReal aScaleX = OpenGl_Vec3 (myTransformation->mat[0][0],
668 myTransformation->mat[0][1],
669 myTransformation->mat[0][2]).SquareModulus();
670 // Scale transform detected.
671 if (Abs (aScaleX - 1.f) > Precision::Confusion())
672 {
673 anOldGlNormalize = aContext->SetGlNormalizeEnabled (Standard_True);
674 }
675
2166f0fa
SK
676 if (isImmediate)
677 {
30f0ad28 678 Tmatrix3 aModelWorld;
679 call_util_transpose_mat (*aModelWorld, myTransformation->mat);
ca3c13d1 680
7d9e854b 681 glGetIntegerv (GL_MATRIX_MODE, &aMatrixMode);
30f0ad28 682
7d9e854b 683 if (!aContext->ShaderManager()->IsEmpty())
30f0ad28 684 {
685 Tmatrix3 aWorldView;
686 glGetFloatv (GL_MODELVIEW_MATRIX, *aWorldView);
687
688 Tmatrix3 aProjection;
689 glGetFloatv (GL_PROJECTION_MATRIX, *aProjection);
690
7d9e854b 691 aContext->ShaderManager()->UpdateModelWorldStateTo (&aModelWorld);
692 aContext->ShaderManager()->UpdateWorldViewStateTo (&aWorldView);
693 aContext->ShaderManager()->UpdateProjectionStateTo (&aProjection);
30f0ad28 694 }
695
2166f0fa
SK
696 glMatrixMode (GL_MODELVIEW);
697 glPushMatrix ();
7d9e854b 698 glScalef (1.0f, 1.0f, 1.0f);
30f0ad28 699 glMultMatrixf (*aModelWorld);
2166f0fa
SK
700 }
701 else
702 {
703 glMatrixMode (GL_MODELVIEW);
704 glPushMatrix();
705
7d9e854b 706 aLocalTrsf = theWorkspace->SetStructureMatrix (myTransformation);
2166f0fa 707 }
ca3c13d1 708 #endif
2166f0fa
SK
709 }
710
711 // Apply transform persistence
7d9e854b 712 const TEL_TRANSFORM_PERSISTENCE *aTransPersistence = NULL;
2166f0fa
SK
713 if ( myTransPers && myTransPers->mode != 0 )
714 {
7d9e854b 715 aTransPersistence = theWorkspace->ActiveView()->BeginTransformPersistence (aContext, myTransPers);
2166f0fa
SK
716 }
717
2166f0fa 718 // Apply aspects
7d9e854b 719 const OpenGl_AspectLine *anAspectLine = theWorkspace->AspectLine (Standard_False);
720 const OpenGl_AspectFace *anAspectFace = theWorkspace->AspectFace (Standard_False);
721 const OpenGl_AspectMarker *anAspectMarker = theWorkspace->AspectMarker (Standard_False);
722 const OpenGl_AspectText *anAspectText = theWorkspace->AspectText (Standard_False);
2166f0fa 723 if (myAspectLine)
7d9e854b 724 {
725 theWorkspace->SetAspectLine (myAspectLine);
726 }
2166f0fa 727 if (myAspectFace)
7d9e854b 728 {
729 theWorkspace->SetAspectFace (myAspectFace);
730 }
2166f0fa 731 if (myAspectMarker)
7d9e854b 732 {
733 theWorkspace->SetAspectMarker (myAspectMarker);
734 }
2166f0fa 735 if (myAspectText)
7d9e854b 736 {
737 theWorkspace->SetAspectText (myAspectText);
738 }
739
740 // Apply correction for mirror transform
741 if (myIsMirrored)
742 {
743 glFrontFace (GL_CW);
744 }
2166f0fa 745
2166f0fa 746 // Apply highlight color
7d9e854b 747 const TEL_COLOUR *aHighlightColor = theWorkspace->HighlightColor;
2166f0fa 748 if (myHighlightColor)
7d9e854b 749 theWorkspace->HighlightColor = myHighlightColor;
2166f0fa
SK
750
751 // Render connected structures
7d9e854b 752 OpenGl_ListOfStructure::Iterator anIter (myConnected);
753 while (anIter.More())
2166f0fa 754 {
7d9e854b 755 anIter.Value()->RenderGeometry (theWorkspace);
756 anIter.Next();
2166f0fa
SK
757 }
758
4269bd1b 759 // Set up plane equations for non-structure transformed global model-view matrix
b859a34d 760 // List of planes to be applied to context state
51b10cd4 761 Handle(Graphic3d_SequenceOfHClipPlane) aUserPlanes;
b859a34d 762
763 // Collect clipping planes of structure scope
764 if (!myClipPlanes.IsEmpty())
4269bd1b 765 {
7d9e854b 766 Graphic3d_SequenceOfHClipPlane::Iterator aClippingIter (myClipPlanes);
767 for (; aClippingIter.More(); aClippingIter.Next())
b859a34d 768 {
7d9e854b 769 const Handle(Graphic3d_ClipPlane)& aClipPlane = aClippingIter.Value();
b859a34d 770 if (!aClipPlane->IsOn())
771 {
772 continue;
773 }
774
775 if (aUserPlanes.IsNull())
776 {
51b10cd4 777 aUserPlanes = new Graphic3d_SequenceOfHClipPlane();
b859a34d 778 }
779
51b10cd4 780 aUserPlanes->Append (aClipPlane);
b859a34d 781 }
4269bd1b 782 }
783
b859a34d 784 if (!aUserPlanes.IsNull() && !aUserPlanes->IsEmpty())
4269bd1b 785 {
b859a34d 786 // add planes at loaded view matrix state
7d9e854b 787 aContext->ChangeClipping().AddWorld (*aUserPlanes, theWorkspace);
30f0ad28 788
789 // Set OCCT state uniform variables
790 if (!aContext->ShaderManager()->IsEmpty())
791 {
792 aContext->ShaderManager()->UpdateClippingState();
793 }
4269bd1b 794 }
795
2166f0fa 796 // Render groups
b64d84be 797 const Graphic3d_SequenceOfGroup& aGroups = DrawGroups();
798 for (OpenGl_Structure::GroupIterator aGroupIter (aGroups); aGroupIter.More(); aGroupIter.Next())
2166f0fa 799 {
7d9e854b 800 aGroupIter.Value()->Render (theWorkspace);
2166f0fa
SK
801 }
802
7d9e854b 803 // Reset correction for mirror transform
804 if (myIsMirrored)
805 glFrontFace (GL_CCW); // default
806
b859a34d 807 // Render capping for structure groups
808 if (!aContext->Clipping().Planes().IsEmpty())
809 {
7d9e854b 810 OpenGl_CappingAlgo::RenderCapping (theWorkspace, aGroups);
b859a34d 811 }
4269bd1b 812
b859a34d 813 // Revert structure clippings
814 if (!aUserPlanes.IsNull() && !aUserPlanes->IsEmpty())
4269bd1b 815 {
b859a34d 816 aContext->ChangeClipping().Remove (*aUserPlanes);
30f0ad28 817
818 // Set OCCT state uniform variables
819 if (!aContext->ShaderManager()->IsEmpty())
820 {
821 aContext->ShaderManager()->RevertClippingState();
822 }
4269bd1b 823 }
824
2166f0fa 825 // Restore highlight color
7d9e854b 826 theWorkspace->HighlightColor = aHighlightColor;
2166f0fa
SK
827
828 // Restore aspects
7d9e854b 829 theWorkspace->SetAspectLine (anAspectLine);
830 theWorkspace->SetAspectFace (anAspectFace);
831 theWorkspace->SetAspectMarker (anAspectMarker);
832 theWorkspace->SetAspectText (anAspectText);
2166f0fa
SK
833
834 // Restore transform persistence
835 if ( myTransPers && myTransPers->mode != 0 )
836 {
7d9e854b 837 theWorkspace->ActiveView()->BeginTransformPersistence (aContext, aTransPersistence);
2166f0fa
SK
838 }
839
840 // Restore local transformation
841 if (myTransformation)
842 {
ca3c13d1 843 #if !defined(GL_ES_VERSION_2_0)
7d9e854b 844
845 aContext->SetGlNormalizeEnabled (anOldGlNormalize);
846
2166f0fa
SK
847 if (isImmediate)
848 {
849 glPopMatrix ();
7d9e854b 850 glMatrixMode (aMatrixMode);
30f0ad28 851
852 Tmatrix3 aModelWorldState = { { 1.f, 0.f, 0.f, 0.f },
853 { 0.f, 1.f, 0.f, 0.f },
854 { 0.f, 0.f, 1.f, 0.f },
855 { 0.f, 0.f, 0.f, 1.f } };
856
b5ac8292 857 aContext->ShaderManager()->RevertModelWorldStateTo (&aModelWorldState);
2166f0fa
SK
858 }
859 else
860 {
7d9e854b 861 theWorkspace->SetStructureMatrix (aLocalTrsf, true);
2166f0fa
SK
862
863 glMatrixMode (GL_MODELVIEW);
864 glPopMatrix();
865 }
ca3c13d1 866 #endif
2166f0fa
SK
867 }
868
b7cd4ba7 869 // Apply highlight box
870 if (!myHighlightBox.IsNull())
871 {
7d9e854b 872 myHighlightBox->Render (theWorkspace);
b7cd4ba7 873 }
874
2166f0fa 875 // Restore named status
7d9e854b 876 theWorkspace->NamedStatus = aNamedStatus;
2166f0fa
SK
877}
878
5e27df78 879// =======================================================================
880// function : Release
881// purpose :
882// =======================================================================
883void OpenGl_Structure::Release (const Handle(OpenGl_Context)& theGlCtx)
884{
885 // Release groups
886 Clear (theGlCtx);
10b9c7df 887 OpenGl_Element::Destroy (theGlCtx.operator->(), myAspectLine);
888 OpenGl_Element::Destroy (theGlCtx.operator->(), myAspectFace);
889 OpenGl_Element::Destroy (theGlCtx.operator->(), myAspectMarker);
890 OpenGl_Element::Destroy (theGlCtx.operator->(), myAspectText);
b64d84be 891 clearHighlightColor (theGlCtx);
d5af8626 892
d5af8626 893 // Remove from connected list of ancestor
894 UnregisterFromAncestorStructure();
5e27df78 895}
896
dd8a4ce9 897// =======================================================================
898// function : ReleaseGlResources
899// purpose :
900// =======================================================================
901void OpenGl_Structure::ReleaseGlResources (const Handle(OpenGl_Context)& theGlCtx)
902{
b64d84be 903 for (OpenGl_Structure::GroupIterator aGroupIter (myGroups); aGroupIter.More(); aGroupIter.Next())
dd8a4ce9 904 {
b64d84be 905 aGroupIter.ChangeValue()->Release (theGlCtx);
dd8a4ce9 906 }
907 if (myAspectLine != NULL)
908 {
10b9c7df 909 myAspectLine->Release (theGlCtx.operator->());
dd8a4ce9 910 }
911 if (myAspectFace != NULL)
912 {
10b9c7df 913 myAspectFace->Release (theGlCtx.operator->());
dd8a4ce9 914 }
915 if (myAspectMarker != NULL)
916 {
10b9c7df 917 myAspectMarker->Release (theGlCtx.operator->());
dd8a4ce9 918 }
919 if (myAspectText != NULL)
920 {
10b9c7df 921 myAspectText->Release (theGlCtx.operator->());
dd8a4ce9 922 }
b64d84be 923 if (!myHighlightBox.IsNull())
dd8a4ce9 924 {
10b9c7df 925 myHighlightBox->Release (theGlCtx.operator->());
dd8a4ce9 926 }
927}
928
59f45b7c 929//=======================================================================
930//function : SetZLayer
bf75be98 931//purpose :
59f45b7c 932//=======================================================================
59f45b7c 933void OpenGl_Structure::SetZLayer (const Standard_Integer theLayerIndex)
934{
935 myZLayer = theLayerIndex;
936}
937
938//=======================================================================
939//function : GetZLayer
bf75be98 940//purpose :
59f45b7c 941//=======================================================================
59f45b7c 942Standard_Integer OpenGl_Structure::GetZLayer () const
943{
944 return myZLayer;
945}
679ecdee 946
679ecdee 947//=======================================================================
948//function : ShadowLink
949//purpose :
950//=======================================================================
951Handle(Graphic3d_CStructure) OpenGl_Structure::ShadowLink (const Handle(Graphic3d_StructureManager)& theManager) const
952{
953 return new OpenGl_StructureShadow (theManager, this);
954}