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 <NCollection_Mat4.hxx>
18 #include <OpenGl_ShaderStates.hxx>
20 // =======================================================================
21 // function : OpenGl_StateInterface
22 // purpose : Creates new OCCT state
23 // =======================================================================
24 OpenGl_StateInterface::OpenGl_StateInterface()
31 // =======================================================================
33 // purpose : Returns current state index
34 // =======================================================================
35 Standard_Size OpenGl_StateInterface::Index() const
40 // =======================================================================
42 // purpose : Updates current state
43 // =======================================================================
44 void OpenGl_StateInterface::Update()
46 myStateStack.Prepend (myIndex);
47 myIndex = myNextIndex;
51 // =======================================================================
53 // purpose : Reverts current state
54 // =======================================================================
55 void OpenGl_StateInterface::Revert()
57 if (!myStateStack.IsEmpty())
59 myIndex = myStateStack.First();
60 myStateStack.RemoveFirst();
68 // =======================================================================
69 // function : OpenGl_ProjectionState
70 // purpose : Creates uninitialized projection state
71 // =======================================================================
72 OpenGl_ProjectionState::OpenGl_ProjectionState()
73 : myInverseNeedUpdate (false)
78 // =======================================================================
80 // purpose : Sets new OCCT projection state
81 // =======================================================================
82 void OpenGl_ProjectionState::Set (const Tmatrix3* theProjectionMatrix)
84 memcpy (myProjectionMatrix, theProjectionMatrix, sizeof (Tmatrix3));
85 myInverseNeedUpdate = true;
88 // =======================================================================
89 // function : ProjectionMatrix
90 // purpose : Returns current projection matrix
91 // =======================================================================
92 const Tmatrix3& OpenGl_ProjectionState::ProjectionMatrix() const
94 return myProjectionMatrix;
97 // =======================================================================
98 // function : ProjectionMatrixInverse
99 // purpose : Returns inverse of current projection matrix
100 // =======================================================================
101 const Tmatrix3& OpenGl_ProjectionState::ProjectionMatrixInverse() const
103 if (!myInverseNeedUpdate)
105 return myProjectionMatrixInverse;
108 reinterpret_cast<const NCollection_Mat4<float>*> (*myProjectionMatrix)->Inverted (
109 *(reinterpret_cast<NCollection_Mat4<float>*> (*myProjectionMatrixInverse)));
110 return myProjectionMatrixInverse;
113 // =======================================================================
114 // function : OpenGl_ModelWorldState
115 // purpose : Creates uninitialized model-world state
116 // =======================================================================
117 OpenGl_ModelWorldState::OpenGl_ModelWorldState()
118 : myInverseNeedUpdate (false)
123 // =======================================================================
125 // purpose : Sets new model-world matrix
126 // =======================================================================
127 void OpenGl_ModelWorldState::Set (const Tmatrix3* theModelWorldMatrix)
129 memcpy (myModelWorldMatrix, theModelWorldMatrix, sizeof (Tmatrix3));
130 myInverseNeedUpdate = true;
133 // =======================================================================
134 // function : ModelWorldMatrix
135 // purpose : Returns current model-world matrix
136 // =======================================================================
137 const Tmatrix3& OpenGl_ModelWorldState::ModelWorldMatrix() const
139 return myModelWorldMatrix;
142 // =======================================================================
143 // function : ModelWorldMatrixInverse
144 // purpose : Returns inverse of current model-world matrix
145 // =======================================================================
146 const Tmatrix3& OpenGl_ModelWorldState::ModelWorldMatrixInverse() const
148 if (!myInverseNeedUpdate)
150 return myModelWorldMatrix;
153 reinterpret_cast<const NCollection_Mat4<float>*> (*myModelWorldMatrix)->Inverted (
154 *(reinterpret_cast<NCollection_Mat4<float>*> (*myModelWorldMatrixInverse)));
155 return myModelWorldMatrixInverse;
158 // =======================================================================
159 // function : OpenGl_WorldViewState
160 // purpose : Creates uninitialized world-view state
161 // =======================================================================
162 OpenGl_WorldViewState::OpenGl_WorldViewState()
163 : myInverseNeedUpdate (false)
168 // =======================================================================
170 // purpose : Sets new world-view matrix
171 // =======================================================================
172 void OpenGl_WorldViewState::Set (const Tmatrix3* theWorldViewMatrix)
174 memcpy (myWorldViewMatrix, theWorldViewMatrix, sizeof (Tmatrix3));
175 myInverseNeedUpdate = true;
178 // =======================================================================
179 // function : WorldViewMatrix
180 // purpose : Returns current world-view matrix
181 // =======================================================================
182 const Tmatrix3& OpenGl_WorldViewState::WorldViewMatrix() const
184 return myWorldViewMatrix;
187 // =======================================================================
188 // function : WorldViewMatrixInverse
189 // purpose : Returns inverse of current world-view matrix
190 // =======================================================================
191 const Tmatrix3& OpenGl_WorldViewState::WorldViewMatrixInverse() const
193 if (!myInverseNeedUpdate)
195 return myWorldViewMatrix;
198 reinterpret_cast<const NCollection_Mat4<float>*> (*myWorldViewMatrix)->Inverted (
199 *(reinterpret_cast<NCollection_Mat4<float>*> (*myWorldViewMatrixInverse)));
200 return myWorldViewMatrixInverse;
203 // =======================================================================
204 // function : OpenGl_LightSourceState
205 // purpose : Creates uninitialized state of light sources
206 // =======================================================================
207 OpenGl_LightSourceState::OpenGl_LightSourceState()
208 : myLightSources (NULL)
213 // =======================================================================
215 // purpose : Sets new light sources
216 // =======================================================================
217 void OpenGl_LightSourceState::Set (const OpenGl_ListOfLight* theLightSources)
219 myLightSources = theLightSources;
222 // =======================================================================
223 // function : LightSources
224 // purpose : Returns current list of light sources
225 // =======================================================================
226 const OpenGl_ListOfLight* OpenGl_LightSourceState::LightSources() const
228 return myLightSources;
231 // =======================================================================
232 // function : OpenGl_MaterialState
233 // purpose : Creates uninitialized material state
234 // =======================================================================
235 OpenGl_MaterialState::OpenGl_MaterialState (const OpenGl_Element* theAspect)
236 : myAspect (theAspect)
241 // =======================================================================
243 // purpose : Sets new material aspect
244 // =======================================================================
245 void OpenGl_MaterialState::Set (const OpenGl_Element* theAspect)
247 myAspect = theAspect;
250 // =======================================================================
252 // purpose : Returns material aspect
253 // =======================================================================
254 const OpenGl_Element* OpenGl_MaterialState::Aspect() const
259 // =======================================================================
260 // function : OpenGl_ClippingState
261 // purpose : Creates new clipping state
262 // =======================================================================
263 OpenGl_ClippingState::OpenGl_ClippingState()