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