0030640: Visualization, Graphic3d_Camera - add option creating Projection matrix...
[occt.git] / src / OpenGl / OpenGl_ShaderStates.hxx
1 // Created on: 2013-10-02
2 // Created by: Denis BOGOLEPOV
3 // Copyright (c) 2013-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_State_HeaderFile
17 #define _OpenGl_State_HeaderFile
18
19 #include <Graphic3d_RenderTransparentMethod.hxx>
20 #include <NCollection_List.hxx>
21 #include <Graphic3d_LightSet.hxx>
22 #include <OpenGl_Element.hxx>
23 #include <OpenGl_Vec.hxx>
24
25 class OpenGl_ShadowMapArray;
26
27 //! Defines interface for OpenGL state.
28 class OpenGl_StateInterface
29 {
30 public:
31
32   //! Creates new state.
33   Standard_EXPORT OpenGl_StateInterface();
34
35   //! Returns current state index.
36   Standard_Size Index() const { return myIndex; }
37
38   //! Increment current state.
39   void Update() { ++myIndex; }
40
41 protected:
42
43   Standard_Size myIndex; //!< current state index
44
45 };
46
47 //! Defines state of OCCT projection transformation.
48 class OpenGl_ProjectionState : public OpenGl_StateInterface
49 {
50 public:
51
52   //! Creates uninitialized projection state.
53   Standard_EXPORT OpenGl_ProjectionState();
54
55   //! Sets new projection matrix.
56   Standard_EXPORT void Set (const OpenGl_Mat4& theProjectionMatrix);
57
58   //! Returns current projection matrix.
59   const OpenGl_Mat4& ProjectionMatrix() const { return myProjectionMatrix; }
60
61   //! Returns inverse of current projection matrix.
62   Standard_EXPORT const OpenGl_Mat4& ProjectionMatrixInverse() const;
63
64 private:
65
66   OpenGl_Mat4         myProjectionMatrix;        //!< OCCT projection matrix
67   mutable OpenGl_Mat4 myProjectionMatrixInverse; //!< Inverse of OCCT projection matrix
68   mutable bool        myInverseNeedUpdate;       //!< Is inversed matrix outdated?
69
70 };
71
72 //! Defines state of OCCT model-world transformation.
73 class OpenGl_ModelWorldState : public OpenGl_StateInterface
74 {
75 public:
76
77   //! Creates uninitialized model-world state.
78   Standard_EXPORT OpenGl_ModelWorldState();
79
80   //! Sets new model-world matrix.
81   Standard_EXPORT void Set (const OpenGl_Mat4& theModelWorldMatrix);
82
83   //! Returns current model-world matrix.
84   const OpenGl_Mat4& ModelWorldMatrix() const { return myModelWorldMatrix; }
85
86   //! Returns inverse of current model-world matrix.
87   Standard_EXPORT const OpenGl_Mat4& ModelWorldMatrixInverse() const;
88
89 private:
90
91   OpenGl_Mat4         myModelWorldMatrix;        //!< OCCT model-world matrix
92   mutable OpenGl_Mat4 myModelWorldMatrixInverse; //!< Inverse of OCCT model-world matrix
93   mutable bool        myInverseNeedUpdate;       //!< Is inversed matrix outdated?
94   
95 };
96
97 //! Defines state of OCCT world-view transformation.
98 class OpenGl_WorldViewState : public OpenGl_StateInterface
99 {
100 public:
101
102   //! Creates uninitialized world-view state.
103   Standard_EXPORT OpenGl_WorldViewState();
104
105   //! Sets new world-view matrix.
106   Standard_EXPORT void Set (const OpenGl_Mat4& theWorldViewMatrix);
107
108   //! Returns current world-view matrix.
109   const OpenGl_Mat4& WorldViewMatrix() const { return myWorldViewMatrix; }
110
111   //! Returns inverse of current world-view matrix.
112   Standard_EXPORT const OpenGl_Mat4& WorldViewMatrixInverse() const;
113
114 private:
115
116   OpenGl_Mat4         myWorldViewMatrix;        //!< OCCT world-view matrix
117   mutable OpenGl_Mat4 myWorldViewMatrixInverse; //!< Inverse of OCCT world-view matrix
118   mutable bool        myInverseNeedUpdate;      //!< Is inversed matrix outdated?
119
120 };
121
122 //! Defines state of OCCT light sources.
123 class OpenGl_LightSourceState : public OpenGl_StateInterface
124 {
125 public:
126
127   //! Creates uninitialized state of light sources.
128   OpenGl_LightSourceState() : mySpecIBLMapLevels (0), myToCastShadows (Standard_True) {}
129
130   //! Sets new light sources.
131   void Set (const Handle(Graphic3d_LightSet)& theLightSources) { myLightSources = theLightSources; }
132
133   //! Returns current list of light sources.
134   const Handle(Graphic3d_LightSet)& LightSources() const { return myLightSources; }
135
136   //! Returns number of mipmap levels used in specular IBL map.
137   //! 0 by default or in case of using non-PBR shading model.
138   Standard_Integer SpecIBLMapLevels() const { return mySpecIBLMapLevels; }
139
140   //! Sets number of mipmap levels used in specular IBL map.
141   void SetSpecIBLMapLevels(Standard_Integer theSpecIBLMapLevels) { mySpecIBLMapLevels = theSpecIBLMapLevels; }
142
143   //! Returns TRUE if shadowmap is set.
144   bool HasShadowMaps() const { return myToCastShadows && !myShadowMaps.IsNull(); }
145
146   //! Returns shadowmap.
147   const Handle(OpenGl_ShadowMapArray)& ShadowMaps() const { return myShadowMaps; }
148
149   //! Sets shadowmap.
150   void SetShadowMaps (const Handle(OpenGl_ShadowMapArray)& theMap) { myShadowMaps = theMap; }
151
152   //! Returns TRUE if shadowmap should be enabled when available; TRUE by default.
153   bool ToCastShadows() const { return myToCastShadows; }
154
155   //! Set if shadowmap should be enabled when available.
156   void SetCastShadows (bool theToCast) { myToCastShadows = theToCast; }
157
158 private:
159
160   Handle(Graphic3d_LightSet) myLightSources;     //!< List of OCCT light sources
161   Standard_Integer           mySpecIBLMapLevels; //!< Number of mipmap levels used in specular IBL map (0 by default or in case of using non-PBR shading model)
162   Handle(OpenGl_ShadowMapArray) myShadowMaps;    //!< active shadowmap
163   Standard_Boolean           myToCastShadows;    //!< enable/disable shadowmap
164
165 };
166
167 //! Defines generic state of OCCT clipping state.
168 class OpenGl_ClippingState
169 {
170 public:
171
172   //! Creates new clipping state.
173   Standard_EXPORT OpenGl_ClippingState();
174
175   //! Returns current state index.
176   Standard_Size Index() const { return myIndex; }
177
178   //! Updates current state.
179   Standard_EXPORT void Update();
180
181   //! Reverts current state.
182   Standard_EXPORT void Revert();
183
184 protected:
185
186   Standard_Size                   myIndex;      //!< Current state index
187   Standard_Size                   myNextIndex;  //!< Next    state index
188   NCollection_List<Standard_Size> myStateStack; //!< Stack of previous states
189
190 };
191
192 //! Defines generic state of order-independent transparency rendering properties.
193 class OpenGl_OitState : public OpenGl_StateInterface
194 {
195 public:
196
197   //! Creates new uniform state.
198   OpenGl_OitState() : myOitMode (Graphic3d_RTM_BLEND_UNORDERED), myDepthFactor (0.5f) {}
199
200   //! Sets the uniform values.
201   //! @param theToEnableWrite [in] flag indicating whether color and coverage
202   //!  values for OIT processing should be written by shader program.
203   //! @param theDepthFactor [in] scalar factor [0-1] defining influence of depth
204   //!  component of a fragment to its final coverage coefficient.
205   void Set (Graphic3d_RenderTransparentMethod theMode,
206             const float theDepthFactor)
207   {
208     myOitMode = theMode;
209     myDepthFactor = static_cast<float> (Max (0.f, Min (1.f, theDepthFactor)));
210   }
211
212   //! Returns flag indicating whether writing of output for OIT processing
213   //! should be enabled/disabled.
214   Graphic3d_RenderTransparentMethod ActiveMode() const { return myOitMode; }
215
216   //! Returns factor defining influence of depth component of a fragment
217   //! to its final coverage coefficient.
218   float DepthFactor() const { return myDepthFactor; }
219
220 private:
221
222   Graphic3d_RenderTransparentMethod myOitMode;     //!< active OIT method for the main GLSL program
223   float                             myDepthFactor; //!< factor of depth influence to coverage
224 };
225
226 #endif // _OpenGl_State_HeaderFile