2f83701ee3b82b1df8757dc39741086f3ee53bbe
[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;
99   Standard_EXPORT virtual void Release (OpenGl_Context* theContext);
100
101 protected:
102
103   //! OpenGl resources
104   mutable struct Resources
105   {
106   public:
107
108     //! Empty constructor.
109     Resources()
110     : myIsSpriteReady (Standard_False),
111       myIsShaderReady (Standard_False) {}
112
113     const Handle(OpenGl_TextureSet)&    Sprite()  const { return mySprite; }
114     const Handle(OpenGl_TextureSet)&    SpriteA() const { return mySpriteA; }
115     const Handle(OpenGl_ShaderProgram)& ShaderProgram() const { return myShaderProgram; }
116
117     Standard_Boolean IsSpriteReady() const { return myIsSpriteReady; }
118     Standard_Boolean IsShaderReady() const { return myIsShaderReady; }
119     void SetSpriteReady() { myIsSpriteReady = Standard_True; }
120     void SetShaderReady() { myIsShaderReady = Standard_True; }
121
122     //! Update texture resource up-to-date state.
123     Standard_EXPORT void UpdateTexturesRediness (const Handle(Graphic3d_AspectMarker3d)& theAspect,
124                                                  Standard_ShortReal& theMarkerSize);
125
126     //! Update shader resource up-to-date state.
127     Standard_EXPORT void UpdateShaderRediness (const Handle(Graphic3d_AspectMarker3d)& theAspect);
128
129     //! Release texture resource.
130     Standard_EXPORT void ReleaseTextures (OpenGl_Context* theCtx);
131
132     //! Release shader resource.
133     Standard_EXPORT void ReleaseShaders (OpenGl_Context* theCtx);
134
135     //! Build texture resources.
136     Standard_EXPORT void BuildSprites (const Handle(OpenGl_Context)&        theCtx,
137                                        const Handle(Graphic3d_MarkerImage)& theMarkerImage,
138                                        const Aspect_TypeOfMarker            theType,
139                                        const Standard_ShortReal             theScale,
140                                        const Graphic3d_Vec4&                theColor,
141                                        Standard_ShortReal&                  theMarkerSize);
142
143     //! Build shader resources.
144     Standard_EXPORT void BuildShader (const Handle(OpenGl_Context)&          theCtx,
145                                       const Handle(Graphic3d_ShaderProgram)& theShader);
146
147   private:
148
149     //! Generate resource keys for a sprite.
150     static void spriteKeys (const Handle(Graphic3d_MarkerImage)& theMarkerImage,
151                             const Aspect_TypeOfMarker            theType,
152                             const Standard_ShortReal             theScale,
153                             const Graphic3d_Vec4&                theColor,
154                             TCollection_AsciiString&             theKey,
155                             TCollection_AsciiString&             theKeyA);
156
157   private:
158
159     Handle(OpenGl_TextureSet)      mySprite;
160     Handle(OpenGl_TextureSet)      mySpriteA;
161     Handle(OpenGl_ShaderProgram)   myShaderProgram;
162     TCollection_AsciiString        myShaderProgramId;
163     Standard_Boolean               myIsSpriteReady;
164     Standard_Boolean               myIsShaderReady;
165
166   } myResources;
167
168   Handle(Graphic3d_AspectMarker3d) myAspect;
169   mutable Standard_ShortReal       myMarkerSize;
170
171 public:
172
173   DEFINE_STANDARD_ALLOC
174
175 };
176
177 #endif // OpenGl_AspectMarker_Header