1 // Created on: 2013-10-02
2 // Created by: Denis BOGOLEPOV
3 // Copyright (c) 2013-2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
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.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 #include <OpenGl_ShaderStates.hxx>
18 #include <NCollection_Mat4.hxx>
20 // =======================================================================
21 // function : OpenGl_StateInterface
23 // =======================================================================
24 OpenGl_StateInterface::OpenGl_StateInterface()
30 // =======================================================================
31 // function : OpenGl_ProjectionState
32 // purpose : Creates uninitialized projection state
33 // =======================================================================
34 OpenGl_ProjectionState::OpenGl_ProjectionState()
35 : myInverseNeedUpdate (false)
40 // =======================================================================
42 // purpose : Sets new OCCT projection state
43 // =======================================================================
44 void OpenGl_ProjectionState::Set (const OpenGl_Mat4& theProjectionMatrix)
46 myProjectionMatrix = theProjectionMatrix;
47 myInverseNeedUpdate = true;
50 // =======================================================================
51 // function : ProjectionMatrix
52 // purpose : Returns current projection matrix
53 // =======================================================================
54 const OpenGl_Mat4& OpenGl_ProjectionState::ProjectionMatrix() const
56 return myProjectionMatrix;
59 // =======================================================================
60 // function : ProjectionMatrixInverse
61 // purpose : Returns inverse of current projection matrix
62 // =======================================================================
63 const OpenGl_Mat4& OpenGl_ProjectionState::ProjectionMatrixInverse() const
65 if (!myInverseNeedUpdate)
67 return myProjectionMatrixInverse;
70 myProjectionMatrix.Inverted (myProjectionMatrixInverse);
72 return myProjectionMatrixInverse;
75 // =======================================================================
76 // function : OpenGl_ModelWorldState
77 // purpose : Creates uninitialized model-world state
78 // =======================================================================
79 OpenGl_ModelWorldState::OpenGl_ModelWorldState()
80 : myInverseNeedUpdate (false)
85 // =======================================================================
87 // purpose : Sets new model-world matrix
88 // =======================================================================
89 void OpenGl_ModelWorldState::Set (const OpenGl_Mat4& theModelWorldMatrix)
91 myModelWorldMatrix = theModelWorldMatrix;
92 myInverseNeedUpdate = true;
95 // =======================================================================
96 // function : ModelWorldMatrix
97 // purpose : Returns current model-world matrix
98 // =======================================================================
99 const OpenGl_Mat4& OpenGl_ModelWorldState::ModelWorldMatrix() const
101 return myModelWorldMatrix;
104 // =======================================================================
105 // function : ModelWorldMatrixInverse
106 // purpose : Returns inverse of current model-world matrix
107 // =======================================================================
108 const OpenGl_Mat4& OpenGl_ModelWorldState::ModelWorldMatrixInverse() const
110 if (!myInverseNeedUpdate)
112 return myModelWorldMatrix;
115 myModelWorldMatrix.Inverted (myModelWorldMatrixInverse);
117 return myModelWorldMatrixInverse;
120 // =======================================================================
121 // function : OpenGl_WorldViewState
122 // purpose : Creates uninitialized world-view state
123 // =======================================================================
124 OpenGl_WorldViewState::OpenGl_WorldViewState()
125 : myInverseNeedUpdate (false)
130 // =======================================================================
132 // purpose : Sets new world-view matrix
133 // =======================================================================
134 void OpenGl_WorldViewState::Set (const OpenGl_Mat4& theWorldViewMatrix)
136 myWorldViewMatrix = theWorldViewMatrix;
137 myInverseNeedUpdate = true;
140 // =======================================================================
141 // function : WorldViewMatrix
142 // purpose : Returns current world-view matrix
143 // =======================================================================
144 const OpenGl_Mat4& OpenGl_WorldViewState::WorldViewMatrix() const
146 return myWorldViewMatrix;
149 // =======================================================================
150 // function : WorldViewMatrixInverse
151 // purpose : Returns inverse of current world-view matrix
152 // =======================================================================
153 const OpenGl_Mat4& OpenGl_WorldViewState::WorldViewMatrixInverse() const
155 if (!myInverseNeedUpdate)
157 return myWorldViewMatrix;
160 myWorldViewMatrix.Inverted (myWorldViewMatrixInverse);
162 return myWorldViewMatrixInverse;
165 // =======================================================================
166 // function : OpenGl_LightSourceState
167 // purpose : Creates uninitialized state of light sources
168 // =======================================================================
169 OpenGl_LightSourceState::OpenGl_LightSourceState()
170 : myLightSources (NULL)
175 // =======================================================================
177 // purpose : Sets new light sources
178 // =======================================================================
179 void OpenGl_LightSourceState::Set (const OpenGl_ListOfLight* theLightSources)
181 myLightSources = theLightSources;
184 // =======================================================================
185 // function : LightSources
186 // purpose : Returns current list of light sources
187 // =======================================================================
188 const OpenGl_ListOfLight* OpenGl_LightSourceState::LightSources() const
190 return myLightSources;
193 // =======================================================================
194 // function : OpenGl_MaterialState
195 // purpose : Creates uninitialized material state
196 // =======================================================================
197 OpenGl_MaterialState::OpenGl_MaterialState (const OpenGl_Element* theAspect)
198 : myAspect (theAspect)
203 // =======================================================================
205 // purpose : Sets new material aspect
206 // =======================================================================
207 void OpenGl_MaterialState::Set (const OpenGl_Element* theAspect)
209 myAspect = theAspect;
212 // =======================================================================
214 // purpose : Returns material aspect
215 // =======================================================================
216 const OpenGl_Element* OpenGl_MaterialState::Aspect() const
221 // =======================================================================
222 // function : OpenGl_ClippingState
223 // purpose : Creates new clipping state
224 // =======================================================================
225 OpenGl_ClippingState::OpenGl_ClippingState()
232 // =======================================================================
234 // purpose : Updates current state
235 // =======================================================================
236 void OpenGl_ClippingState::Update()
238 myStateStack.Prepend (myIndex);
239 myIndex = myNextIndex; // use myNextIndex here to handle properly Update() after Revert()
243 // =======================================================================
245 // purpose : Reverts current state
246 // =======================================================================
247 void OpenGl_ClippingState::Revert()
249 if (!myStateStack.IsEmpty())
251 myIndex = myStateStack.First();
252 myStateStack.RemoveFirst();