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