6b142a93694a3b14348c1dff2b8909fa313103f3
[occt.git] / src / OpenGl / OpenGl_Structure.hxx
1 // Created on: 2011-08-01
2 // Created by: Sergey ZERCHANINOV
3 // Copyright (c) 2011-2014 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
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
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.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15
16 #ifndef OpenGl_Structure_Header
17 #define OpenGl_Structure_Header
18
19 #include <Graphic3d_CStructure.hxx>
20 #include <Graphic3d_SequenceOfHClipPlane.hxx>
21
22 #include <OpenGl_AspectLine.hxx>
23 #include <OpenGl_AspectFace.hxx>
24 #include <OpenGl_AspectMarker.hxx>
25 #include <OpenGl_AspectText.hxx>
26
27 #include <OpenGl_Group.hxx>
28 #include <OpenGl_Matrix.hxx>
29 #include <OpenGl_NamedStatus.hxx>
30
31 #include <NCollection_List.hxx>
32 #include <InterfaceGraphic_Graphic3d.hxx>
33
34 class OpenGl_Structure;
35 class OpenGl_GraphicDriver;
36
37 typedef NCollection_List<const OpenGl_Structure* > OpenGl_ListOfStructure;
38
39 //! Implementation of low-level graphic structure.
40 class OpenGl_Structure : public Graphic3d_CStructure
41 {
42   friend class OpenGl_Group;
43
44 public:
45
46   //! Auxiliary wrapper to iterate OpenGl_Group sequence.
47   class GroupIterator
48   {
49
50   public:
51     GroupIterator (const Graphic3d_SequenceOfGroup& theGroups) : myIter (theGroups) {}
52     Standard_Boolean More() const     { return myIter.More(); }
53     void Next()                       { myIter.Next(); }
54     const OpenGl_Group* Value() const { return (const OpenGl_Group* )(myIter.Value().operator->()); }
55     OpenGl_Group*       ChangeValue() { return (OpenGl_Group* )(myIter.ChangeValue().operator->()); }
56
57   private:
58     Graphic3d_SequenceOfGroup::Iterator myIter;
59
60   };
61
62 public:
63
64   //! Create empty structure
65   Standard_EXPORT OpenGl_Structure (const Handle(Graphic3d_StructureManager)& theManager);
66
67   //! Setup structure graphic state
68   Standard_EXPORT virtual void UpdateNamedStatus();
69
70   //! Clear graphic data
71   Standard_EXPORT virtual void Clear();
72
73   //! Connect other structure to this one
74   Standard_EXPORT virtual void Connect    (Graphic3d_CStructure& theStructure);
75
76   //! Disconnect other structure to this one
77   Standard_EXPORT virtual void Disconnect (Graphic3d_CStructure& theStructure);
78
79   //! Synchronize structure aspects
80   Standard_EXPORT virtual void UpdateAspects();
81
82   //! Synchronize structure transformation
83   Standard_EXPORT virtual void UpdateTransformation();
84
85   //! Highlight entire structure with color
86   Standard_EXPORT virtual void HighlightWithColor (const Graphic3d_Vec3&  theColor,
87                                                    const Standard_Boolean theToCreate);
88
89   //! Highlight structure using boundary box
90   Standard_EXPORT virtual void HighlightWithBndBox (const Handle(Graphic3d_Structure)& theStruct,
91                                                     const Standard_Boolean             theToCreate);
92
93   //! Create shadow link to this structure
94   Standard_EXPORT virtual Handle(Graphic3d_CStructure) ShadowLink (const Handle(Graphic3d_StructureManager)& theManager) const;
95
96   //! Create new group within this structure
97   Standard_EXPORT virtual Handle(Graphic3d_Group) NewGroup (const Handle(Graphic3d_Structure)& theStruct);
98
99   //! Remove group from this structure
100   Standard_EXPORT virtual void RemoveGroup (const Handle(Graphic3d_Group)& theGroup);
101
102 public:
103
104   //! @return graphic groups
105   virtual const Graphic3d_SequenceOfGroup& DrawGroups() const
106   {
107     return myGroups;
108   }
109
110   //! Access graphic driver
111   OpenGl_GraphicDriver* GlDriver() const
112   {
113     return (OpenGl_GraphicDriver* )myGraphicDriver.operator->();
114   }
115
116   void SetTransformPersistence (const CALL_DEF_TRANSFORM_PERSISTENCE &ATransPers);
117
118   void SetAspectLine   (const CALL_DEF_CONTEXTLINE &theAspect);
119   void SetAspectFace   (const CALL_DEF_CONTEXTFILLAREA& theAspect);
120   void SetAspectMarker (const CALL_DEF_CONTEXTMARKER& theAspect);
121   void SetAspectText   (const CALL_DEF_CONTEXTTEXT &theAspect);
122
123   void clearHighlightBox (const Handle(OpenGl_Context)& theGlCtx);
124
125   void setHighlightColor (const Handle(OpenGl_Context)& theGlCtx,
126                           const Graphic3d_Vec3&         theColor);
127
128   void clearHighlightColor (const Handle(OpenGl_Context)& theGlCtx);
129
130   Standard_Boolean IsVisible() const { return !(myNamedStatus & OPENGL_NS_HIDE); }
131
132   Standard_EXPORT void Clear (const Handle(OpenGl_Context)& theGlCtx);
133
134   //! Set z layer ID to display the structure in specified layer
135   Standard_EXPORT void SetZLayer (const Standard_Integer theLayerIndex);
136
137   //! Get z layer ID
138   Standard_EXPORT Standard_Integer GetZLayer() const;
139
140   virtual void Render  (const Handle(OpenGl_Workspace)& theWorkspace) const;
141   virtual void Release (const Handle(OpenGl_Context)&   theGlCtx);
142
143   //! This method releases GL resources without actual elements destruction.
144   //! As result structure could be correctly destroyed layer without GL context
145   //! (after last window was closed for example).
146   //!
147   //! Notice however that reusage of this structure after calling this method is incorrect
148   //! and will lead to broken visualization due to loosed data.
149   Standard_EXPORT void ReleaseGlResources (const Handle(OpenGl_Context)& theGlCtx);
150
151   //! Returns list of connected OpenGL structures.
152   const OpenGl_ListOfStructure& ConnectedStructures() const { return myConnected; }
153
154   //! Returns OpenGL face aspect.
155   const OpenGl_AspectFace* AspectFace() const { return myAspectFace; }
156
157   //! Returns OpenGL transformation matrix.
158   const OpenGl_Matrix* Transformation() const { return myTransformation; }
159   
160   //! Returns OpenGL persistent translation.
161   const TEL_TRANSFORM_PERSISTENCE* PersistentTranslation() const { return myTransPers; }
162
163 #ifdef HAVE_OPENCL
164
165   //! Returns structure modification state (for ray-tracing).
166   Standard_Size ModificationState() const { return myModificationState; }
167
168   //! Resets structure modification state (for ray-tracing)
169   void ResetModificationState() const { myModificationState = 0; }
170
171   //! Is the structure ray-tracable (contains ray-tracable elements)?
172   Standard_Boolean IsRaytracable() const { return myIsRaytracable; }
173
174 #endif
175
176 protected:
177
178   Standard_EXPORT virtual ~OpenGl_Structure();
179
180 #ifdef HAVE_OPENCL
181
182   //! Registers ancestor connected structure (for updating ray-tracing state).
183   void RegisterAncestorStructure (const OpenGl_Structure* theStructure) const;
184
185   //! Unregisters ancestor connected structure (for updating ray-tracing state).
186   void UnregisterAncestorStructure (const OpenGl_Structure* theStructure) const;
187
188   //! Unregisters structure from ancestor structure (for updating ray-tracing state).
189   void UnregisterFromAncestorStructure() const;
190
191   //! Updates modification state for structure and its parents.
192   void UpdateStateWithAncestorStructures() const;
193
194   //! Updates ray-tracable status for structure and its parents.
195   void UpdateRaytracableWithAncestorStructures() const;
196
197   //! Sets ray-tracable status for structure and its parents.
198   void SetRaytracableWithAncestorStructures() const;
199
200 #endif
201
202 protected:
203
204   OpenGl_Matrix*             myTransformation;
205   TEL_TRANSFORM_PERSISTENCE* myTransPers;
206   OpenGl_AspectLine*         myAspectLine;
207   OpenGl_AspectFace*         myAspectFace;
208   OpenGl_AspectMarker*       myAspectMarker;
209   OpenGl_AspectText*         myAspectText;
210
211   Handle(OpenGl_Group)       myHighlightBox;
212   TEL_COLOUR*                myHighlightColor;
213
214   int                        myNamedStatus;
215   int                        myZLayer;
216
217   OpenGl_ListOfStructure           myConnected;
218
219 #ifdef HAVE_OPENCL
220   mutable OpenGl_ListOfStructure   myAncestorStructures;
221   mutable Standard_Boolean         myIsRaytracable;
222   mutable Standard_Size            myModificationState;
223 #endif
224
225 public:
226
227   DEFINE_STANDARD_RTTI(OpenGl_Structure) // Type definition
228
229 };
230
231 DEFINE_STANDARD_HANDLE(OpenGl_Structure, Graphic3d_CStructure)
232
233 #endif // OpenGl_Structure_Header