0024391: Erased AIS object can not be displayed in AIS_InteractiveContext after AIS_I...
[occt.git] / src / OpenGl / OpenGl_AspectMarker.hxx
1 // Created on: 2011-07-13
2 // Created by: Sergey ZERCHANINOV
3 // Copyright (c) 2011-2012 OPEN CASCADE SAS
4 //
5 // The content of this file is subject to the Open CASCADE Technology Public
6 // License Version 6.5 (the "License"). You may not use the content of this file
7 // except in compliance with the License. Please obtain a copy of the License
8 // at http://www.opencascade.org and read it completely before using this file.
9 //
10 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
11 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
12 //
13 // The Original Code and all software distributed under the License is
14 // distributed on an "AS IS" basis, without warranty of any kind, and the
15 // Initial Developer hereby disclaims all such warranties, including without
16 // limitation, any warranties of merchantability, fitness for a particular
17 // purpose or non-infringement. Please see the License for the specific terms
18 // and conditions governing the rights and limitations under the License.
19
20 #ifndef OpenGl_AspectMarker_Header
21 #define OpenGl_AspectMarker_Header
22
23 #include <InterfaceGraphic_Graphic3d.hxx>
24 #include <Aspect_TypeOfMarker.hxx>
25 #include <Graphic3d_CGroup.hxx>
26 #include <TCollection_AsciiString.hxx>
27
28 #include <OpenGl_Element.hxx>
29 #include <Handle_OpenGl_PointSprite.hxx>
30 #include <Handle_OpenGl_ShaderProgram.hxx>
31
32 #include <Image_PixMap_Handle.hxx>
33
34 class OpenGl_AspectMarker : public OpenGl_Element
35 {
36 public:
37
38   OpenGl_AspectMarker();
39
40   //! Copy parameters
41   void SetAspect (const CALL_DEF_CONTEXTMARKER& theAspect);
42
43   //! @return marker color
44   const TEL_COLOUR& Color() const
45   {
46     return myColor;
47   }
48
49   //! @return maker type
50   Aspect_TypeOfMarker Type() const
51   {
52     return myType;
53   }
54
55   //! @return marker scale
56   Standard_ShortReal Scale() const
57   {
58     return myScale;
59   }
60
61   //! @return marker size
62   Standard_ShortReal MarkerSize() const
63   {
64     return myMarkerSize;
65   }
66
67   //! Init and return OpenGl point sprite resource.
68   //! @return point sprite texture.
69   const Handle(OpenGl_PointSprite)& SpriteRes (const Handle(OpenGl_Workspace)& theWorkspace) const
70   {
71     if (!myResources.IsSpriteReady())
72     {
73       myResources.BuildSprites (theWorkspace, myMarkerImage, myType, myScale, myColor, myMarkerSize);
74       myResources.SetSpriteReady();
75     }
76
77     return myResources.Sprite;
78   }
79
80   //! Init and return OpenGl highlight point sprite resource.
81   //! @return point sprite texture for highlight.
82   const Handle(OpenGl_PointSprite)& SpriteHighlightRes (const Handle(OpenGl_Workspace)& theWorkspace) const
83   {
84     if (!myResources.IsSpriteReady())
85     {
86       myResources.BuildSprites (theWorkspace, myMarkerImage, myType, myScale, myColor, myMarkerSize);
87       myResources.SetSpriteReady();
88     }
89
90     return myResources.SpriteA;
91   }
92
93   //! Init and return OpenGl shader program resource.
94   //! @return shader program resource.
95   const Handle(OpenGl_ShaderProgram)& ShaderProgramRes (const Handle(OpenGl_Workspace)& theWorkspace) const
96   {
97     if (!myResources.IsShaderReady())
98     {
99       myResources.BuildShader (theWorkspace, myShaderProgram);
100       myResources.SetShaderReady();
101     }
102
103     return myResources.ShaderProgram;
104   }
105
106   virtual void Render  (const Handle(OpenGl_Workspace)& theWorkspace) const;
107   virtual void Release (const Handle(OpenGl_Context)&   theContext);
108
109 protected: //! @name ordinary aspect properties
110
111   TEL_COLOUR                      myColor;
112   Aspect_TypeOfMarker             myType;
113   Standard_ShortReal              myScale;
114   mutable Standard_ShortReal      myMarkerSize;
115   Handle(Graphic3d_MarkerImage)   myMarkerImage;
116   Handle(Graphic3d_ShaderProgram) myShaderProgram;
117
118 protected: //! @name OpenGl resources
119
120   //! OpenGl resources
121   mutable struct Resources
122   {
123   public:
124
125     Resources() :
126         SpriteKey (""),
127         SpriteAKey (""),
128         myIsSpriteReady (Standard_False),
129         myIsShaderReady (Standard_False) {}
130
131     Standard_Boolean IsSpriteReady() const { return myIsSpriteReady; }
132     Standard_Boolean IsShaderReady() const { return myIsShaderReady; }
133     void SetSpriteReady() { myIsSpriteReady = Standard_True; }
134     void SetShaderReady() { myIsShaderReady = Standard_True; }
135     void ResetSpriteReadiness() { myIsSpriteReady = Standard_False; }
136     void ResetShaderReadiness() { myIsShaderReady = Standard_False; }
137
138     void BuildSprites (const Handle(OpenGl_Workspace)& theWS,
139                        const Handle(Graphic3d_MarkerImage)& theMarkerImage,
140                        const Aspect_TypeOfMarker theType,
141                        const Standard_ShortReal theScale,
142                        const TEL_COLOUR& theColor,
143                        Standard_ShortReal& theMarkerSize);
144
145     void BuildShader (const Handle(OpenGl_Workspace)& theWS, const Handle(Graphic3d_ShaderProgram)& theShader);
146
147     void SpriteKeys (const Handle(Graphic3d_MarkerImage)& theMarkerImage,
148                      const Aspect_TypeOfMarker theType,
149                      const Standard_ShortReal theScale,
150                      const TEL_COLOUR& theColor,
151                      TCollection_AsciiString& theKey,
152                      TCollection_AsciiString& theKeyA);
153
154     Handle(OpenGl_PointSprite)   Sprite;
155     TCollection_AsciiString      SpriteKey;
156
157     Handle(OpenGl_PointSprite)   SpriteA;
158     TCollection_AsciiString      SpriteAKey;
159
160     Handle(OpenGl_ShaderProgram) ShaderProgram;
161     TCollection_AsciiString      ShaderProgramId;
162
163   private:
164
165     Standard_Boolean myIsSpriteReady;
166     Standard_Boolean myIsShaderReady;
167
168   } myResources;
169
170 public:
171
172   DEFINE_STANDARD_ALLOC
173
174 };
175
176 #endif // OpenGl_AspectMarker_Header