0024437: Visualization - silhouette edges based on OpenGL
[occt.git] / src / OpenGl / OpenGl_AspectMarker.hxx
1 // Created on: 2011-07-13
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_AspectMarker_Header
17 #define OpenGl_AspectMarker_Header
18
19 #include <Aspect_TypeOfMarker.hxx>
20 #include <Graphic3d_AspectMarker3d.hxx>
21 #include <TCollection_AsciiString.hxx>
22
23 #include <OpenGl_Element.hxx>
24 #include <OpenGl_TextureSet.hxx>
25
26 class OpenGl_PointSprite;
27 class OpenGl_ShaderProgram;
28
29 //! The element holding Graphic3d_AspectMarker3d.
30 class OpenGl_AspectMarker : public OpenGl_Element
31 {
32 public:
33
34   //! Empty constructor.
35   Standard_EXPORT OpenGl_AspectMarker();
36
37   //! Create and assign parameters.
38   Standard_EXPORT OpenGl_AspectMarker (const Handle(Graphic3d_AspectMarker3d)& theAspect);
39
40   //! Return the aspect.
41   const Handle(Graphic3d_AspectMarker3d)& Aspect() const { return myAspect; }
42
43   //! Assign new aspect.
44   Standard_EXPORT void SetAspect (const Handle(Graphic3d_AspectMarker3d)& theAspect);
45
46   //! @return marker size
47   Standard_ShortReal MarkerSize() const { return myMarkerSize; }
48
49   //! Init and return OpenGl point sprite resource.
50   //! @return point sprite texture.
51   const Handle(OpenGl_TextureSet)& SpriteRes (const Handle(OpenGl_Context)& theCtx) const
52   {
53     if (!myResources.IsSpriteReady())
54     {
55       myResources.BuildSprites (theCtx,
56                                 myAspect->GetMarkerImage(),
57                                 myAspect->Type(),
58                                 myAspect->Scale(),
59                                 myAspect->ColorRGBA(),
60                                 myMarkerSize);
61       myResources.SetSpriteReady();
62     }
63
64     return myResources.Sprite();
65   }
66
67   //! Init and return OpenGl highlight point sprite resource.
68   //! @return point sprite texture for highlight.
69   const Handle(OpenGl_TextureSet)& SpriteHighlightRes (const Handle(OpenGl_Context)& theCtx) const
70   {
71     if (!myResources.IsSpriteReady())
72     {
73       myResources.BuildSprites (theCtx,
74                                 myAspect->GetMarkerImage(),
75                                 myAspect->Type(),
76                                 myAspect->Scale(),
77                                 myAspect->ColorRGBA(),
78                                 myMarkerSize);
79       myResources.SetSpriteReady();
80     }
81
82     return myResources.SpriteA();
83   }
84
85   //! Init and return OpenGl shader program resource.
86   //! @return shader program resource.
87   const Handle(OpenGl_ShaderProgram)& ShaderProgramRes (const Handle(OpenGl_Context)& theCtx) const
88   {
89     if (!myResources.IsShaderReady())
90     {
91       myResources.BuildShader (theCtx, myAspect->ShaderProgram());
92       myResources.SetShaderReady();
93     }
94
95     return myResources.ShaderProgram();
96   }
97
98   Standard_EXPORT virtual void Render  (const Handle(OpenGl_Workspace)& theWorkspace) const Standard_OVERRIDE;
99   Standard_EXPORT virtual void Release (OpenGl_Context* theContext) Standard_OVERRIDE;
100
101   //! Update presentation aspects parameters after their modification.
102   virtual void SynchronizeAspects() Standard_OVERRIDE { SetAspect (myAspect); }
103
104 protected:
105
106   //! OpenGl resources
107   mutable struct Resources
108   {
109   public:
110
111     //! Empty constructor.
112     Resources()
113     : myIsSpriteReady (Standard_False),
114       myIsShaderReady (Standard_False) {}
115
116     const Handle(OpenGl_TextureSet)&    Sprite()  const { return mySprite; }
117     const Handle(OpenGl_TextureSet)&    SpriteA() const { return mySpriteA; }
118     const Handle(OpenGl_ShaderProgram)& ShaderProgram() const { return myShaderProgram; }
119
120     Standard_Boolean IsSpriteReady() const { return myIsSpriteReady; }
121     Standard_Boolean IsShaderReady() const { return myIsShaderReady; }
122     void SetSpriteReady() { myIsSpriteReady = Standard_True; }
123     void SetShaderReady() { myIsShaderReady = Standard_True; }
124
125     //! Update texture resource up-to-date state.
126     Standard_EXPORT void UpdateTexturesRediness (const Handle(Graphic3d_AspectMarker3d)& theAspect,
127                                                  Standard_ShortReal& theMarkerSize);
128
129     //! Update shader resource up-to-date state.
130     Standard_EXPORT void UpdateShaderRediness (const Handle(Graphic3d_AspectMarker3d)& theAspect);
131
132     //! Release texture resource.
133     Standard_EXPORT void ReleaseTextures (OpenGl_Context* theCtx);
134
135     //! Release shader resource.
136     Standard_EXPORT void ReleaseShaders (OpenGl_Context* theCtx);
137
138     //! Build texture resources.
139     Standard_EXPORT void BuildSprites (const Handle(OpenGl_Context)&        theCtx,
140                                        const Handle(Graphic3d_MarkerImage)& theMarkerImage,
141                                        const Aspect_TypeOfMarker            theType,
142                                        const Standard_ShortReal             theScale,
143                                        const Graphic3d_Vec4&                theColor,
144                                        Standard_ShortReal&                  theMarkerSize);
145
146     //! Build shader resources.
147     Standard_EXPORT void BuildShader (const Handle(OpenGl_Context)&          theCtx,
148                                       const Handle(Graphic3d_ShaderProgram)& theShader);
149
150   private:
151
152     //! Generate resource keys for a sprite.
153     static void spriteKeys (const Handle(Graphic3d_MarkerImage)& theMarkerImage,
154                             const Aspect_TypeOfMarker            theType,
155                             const Standard_ShortReal             theScale,
156                             const Graphic3d_Vec4&                theColor,
157                             TCollection_AsciiString&             theKey,
158                             TCollection_AsciiString&             theKeyA);
159
160   private:
161
162     Handle(OpenGl_TextureSet)      mySprite;
163     Handle(OpenGl_TextureSet)      mySpriteA;
164     Handle(OpenGl_ShaderProgram)   myShaderProgram;
165     TCollection_AsciiString        myShaderProgramId;
166     Standard_Boolean               myIsSpriteReady;
167     Standard_Boolean               myIsShaderReady;
168
169   } myResources;
170
171   Handle(Graphic3d_AspectMarker3d) myAspect;
172   mutable Standard_ShortReal       myMarkerSize;
173
174 public:
175
176   DEFINE_STANDARD_ALLOC
177
178 };
179
180 #endif // OpenGl_AspectMarker_Header