0024192: Adding support for shaders to OCCT visualization toolkit
[occt.git] / src / OpenGl / OpenGl_ShaderManager.hxx
1 // Created on: 2013-09-26
2 // Created by: Denis BOGOLEPOV
3 // Copyright (c) 2013 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_ShaderManager_HeaderFile
21 #define _OpenGl_ShaderManager_HeaderFile
22
23 #include <Graphic3d_ShaderProgram_Handle.hxx>
24
25 #include <NCollection_DataMap.hxx>
26 #include <NCollection_Sequence.hxx>
27
28 #include <Handle_OpenGl_ShaderManager.hxx>
29 #include <OpenGl_ShaderProgram.hxx>
30 #include <OpenGl_ShaderStates.hxx>
31
32 //! List of shader programs.
33 typedef NCollection_Sequence<Handle(OpenGl_ShaderProgram)> OpenGl_ShaderProgramList;
34
35 //! Map to declare per-program states of OCCT materials.
36 typedef NCollection_DataMap<Handle(OpenGl_ShaderProgram), OpenGl_MaterialState> OpenGl_MaterialStates;
37
38 //! This class is responsible for managing shader programs.
39 class OpenGl_ShaderManager : public Standard_Transient
40 {
41   friend class OpenGl_ShaderProgram;
42
43 public:
44
45   //! Creates new empty shader manager.
46   Standard_EXPORT OpenGl_ShaderManager (OpenGl_Context* theContext);
47
48   //! Releases resources of shader manager.
49   Standard_EXPORT virtual ~OpenGl_ShaderManager();
50
51   //! Creates new shader program.
52   Standard_EXPORT Handle(OpenGl_ShaderProgram) Create (const Handle(Graphic3d_ShaderProgram)& theProxyProgram = NULL);
53
54   //! Unregisters specified shader program.
55   Standard_EXPORT void Unregister (Handle(OpenGl_ShaderProgram)& theProgram);
56
57   //! Returns list of registered shader programs.
58   Standard_EXPORT const OpenGl_ShaderProgramList& ShaderPrograms() const;
59
60   //! Returns true if no program objects are registered in the manager.
61   Standard_EXPORT Standard_Boolean IsEmpty() const;
62
63   DEFINE_STANDARD_RTTI (OpenGl_ShaderManager)
64
65 protected:
66
67   OpenGl_MaterialStates   myMaterialStates;   //!< Per-program state of OCCT material
68   OpenGl_ProjectionState  myProjectionState;  //!< State of OCCT projection transformation
69   OpenGl_ModelWorldState  myModelWorldState;  //!< State of OCCT model-world transformation
70   OpenGl_WorldViewState   myWorldViewState;   //!< State of OCCT world-view transformation
71   OpenGl_LightSourceState myClippingState;    //!< State of OCCT clipping planes
72   OpenGl_LightSourceState myLightSourceState; //!< State of OCCT light sources
73
74 public:
75
76   //! Returns current state of OCCT light sources.
77   Standard_EXPORT const OpenGl_LightSourceState& LightSourceState() const;
78
79   //! Updates state of OCCT light sources.
80   Standard_EXPORT void UpdateLightSourceStateTo (const OpenGl_ListOfLight* theLights);
81
82   //! Pushes current state of OCCT light sources to specified program.
83   Standard_EXPORT void PushLightSourceState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
84
85 public:
86
87   //! Returns current state of OCCT projection transform.
88   Standard_EXPORT const OpenGl_ProjectionState& ProjectionState() const;
89
90   //! Updates state of OCCT projection transform.
91   Standard_EXPORT void UpdateProjectionStateTo (const Tmatrix3& theProjectionMatrix);
92
93   //! Reverts state of OCCT projection transform.
94   Standard_EXPORT void RevertProjectionStateTo (const Tmatrix3& theProjectionMatrix);
95
96   //! Pushes current state of OCCT projection transform to specified program.
97   Standard_EXPORT void PushProjectionState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
98
99 public:
100
101   //! Returns current state of OCCT model-world transform.
102   Standard_EXPORT const OpenGl_ModelWorldState& ModelWorldState() const;
103
104   //! Updates state of OCCT model-world transform.
105   Standard_EXPORT void UpdateModelWorldStateTo (const Tmatrix3& theModelWorldMatrix);
106
107   //! Reverts state of OCCT model-world transform.
108   Standard_EXPORT void RevertModelWorldStateTo (const Tmatrix3& theModelWorldMatrix);
109
110   //! Pushes current state of OCCT model-world transform to specified program.
111   Standard_EXPORT void PushModelWorldState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
112
113 public:
114
115   //! Returns current state of OCCT world-view transform.
116   Standard_EXPORT const OpenGl_WorldViewState& WorldViewState() const;
117
118   //! Updates state of OCCT world-view transform.
119   Standard_EXPORT void UpdateWorldViewStateTo (const Tmatrix3& theWorldViewMatrix);
120
121   //! Reverts state of OCCT world-view transform.
122   Standard_EXPORT void RevertWorldViewStateTo (const Tmatrix3& theWorldViewMatrix);
123
124   //! Pushes current state of OCCT world-view transform to specified program.
125   Standard_EXPORT void PushWorldViewState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
126
127 public:
128
129   //! Updates state of OCCT clipping planes.
130   Standard_EXPORT void UpdateClippingState();
131
132   //! Reverts state of OCCT clipping planes.
133   Standard_EXPORT void RevertClippingState();
134
135   //! Pushes current state of OCCT clipping planes to specified program.
136   Standard_EXPORT void PushClippingState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
137
138 public:
139
140   //! Resets state of OCCT material for all programs.
141   Standard_EXPORT void ResetMaterialStates();
142
143   //! Updates state of OCCT material for specified program.
144   Standard_EXPORT void UpdateMaterialStateTo (const Handle(OpenGl_ShaderProgram)& theProgram,
145                                               const OpenGl_Element*               theAspect);
146
147   //! Pushes current state of OCCT material to specified program.
148   Standard_EXPORT void PushMaterialState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
149
150   //! Returns current state of OCCT material for specified program.
151   Standard_EXPORT const OpenGl_MaterialState* MaterialState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
152
153 public:
154   
155   //! Pushes current state of OCCT graphics parameters to specified program.
156   Standard_EXPORT void PushState (const Handle(OpenGl_ShaderProgram)& theProgram) const;
157
158 protected:
159
160   OpenGl_ShaderProgramList myProgramList;  //!< The list of shader programs
161   OpenGl_Context*          myContext;      //!< The OpenGL context
162
163 private:
164
165   Standard_Boolean         myIsPP;         //!< Is any program object bound (programmable pipeline)?
166
167 };
168
169 #endif // _OpenGl_ShaderManager_HeaderFile