0025219: Visualization, TKOpenGl - disable code paths unavailable on OpenGL ES 2.0
[occt.git] / src / OpenGl / OpenGl_ShaderStates.cxx
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 #include <NCollection_Mat4.hxx>
17
18 #include <OpenGl_ShaderStates.hxx>
19
20 // =======================================================================
21 // function : OpenGl_StateInterface
22 // purpose  : Creates new OCCT state
23 // =======================================================================
24 OpenGl_StateInterface::OpenGl_StateInterface()
25 : myIndex (0),
26   myNextIndex (1)
27 {
28   //
29 }
30
31 // =======================================================================
32 // function : Index
33 // purpose  : Returns current state index
34 // =======================================================================
35 Standard_Size OpenGl_StateInterface::Index() const
36 {
37   return myIndex;
38 }
39
40 // =======================================================================
41 // function : Update
42 // purpose  : Updates current state
43 // =======================================================================
44 void OpenGl_StateInterface::Update()
45 {
46   myStateStack.Prepend (myIndex);
47   myIndex = myNextIndex;
48   ++myNextIndex;
49 }
50
51 // =======================================================================
52 // function : Revert
53 // purpose  : Reverts current state
54 // =======================================================================
55 void OpenGl_StateInterface::Revert()
56 {
57   if (!myStateStack.IsEmpty())
58   {
59     myIndex = myStateStack.First();
60     myStateStack.RemoveFirst();
61   }
62   else
63   {
64     myIndex = 0;
65   }
66 }
67
68 // =======================================================================
69 // function : OpenGl_ProjectionState
70 // purpose  : Creates uninitialized projection state
71 // =======================================================================
72 OpenGl_ProjectionState::OpenGl_ProjectionState()
73 : myInverseNeedUpdate (false)
74 {
75   //
76 }
77
78 // =======================================================================
79 // function : Set
80 // purpose  : Sets new OCCT projection state
81 // =======================================================================
82 void OpenGl_ProjectionState::Set (const Tmatrix3* theProjectionMatrix)
83 {
84   memcpy (myProjectionMatrix, theProjectionMatrix, sizeof (Tmatrix3));
85   myInverseNeedUpdate = true;
86 }
87
88 // =======================================================================
89 // function : ProjectionMatrix
90 // purpose  : Returns current projection matrix
91 // =======================================================================
92 const Tmatrix3& OpenGl_ProjectionState::ProjectionMatrix() const
93 {
94   return myProjectionMatrix;
95 }
96
97 // =======================================================================
98 // function : ProjectionMatrixInverse
99 // purpose  : Returns inverse of current projection matrix
100 // =======================================================================
101 const Tmatrix3& OpenGl_ProjectionState::ProjectionMatrixInverse() const
102 {
103   if (!myInverseNeedUpdate)
104   {
105     return myProjectionMatrixInverse;
106   }
107
108   reinterpret_cast<const NCollection_Mat4<float>*> (*myProjectionMatrix)->Inverted (
109                 *(reinterpret_cast<NCollection_Mat4<float>*> (*myProjectionMatrixInverse)));
110   return myProjectionMatrixInverse;
111 }
112
113 // =======================================================================
114 // function : OpenGl_ModelWorldState
115 // purpose  : Creates uninitialized model-world state
116 // =======================================================================
117 OpenGl_ModelWorldState::OpenGl_ModelWorldState()
118 : myInverseNeedUpdate (false)
119 {
120   //
121 }
122
123 // =======================================================================
124 // function : Set
125 // purpose  : Sets new model-world matrix
126 // =======================================================================
127 void OpenGl_ModelWorldState::Set (const Tmatrix3* theModelWorldMatrix)
128 {
129   memcpy (myModelWorldMatrix, theModelWorldMatrix, sizeof (Tmatrix3));
130   myInverseNeedUpdate = true;
131 }
132
133 // =======================================================================
134 // function : ModelWorldMatrix
135 // purpose  : Returns current model-world matrix
136 // =======================================================================
137 const Tmatrix3& OpenGl_ModelWorldState::ModelWorldMatrix() const
138 {
139   return myModelWorldMatrix;
140 }
141
142 // =======================================================================
143 // function : ModelWorldMatrixInverse
144 // purpose  : Returns inverse of current model-world matrix
145 // =======================================================================
146 const Tmatrix3& OpenGl_ModelWorldState::ModelWorldMatrixInverse() const
147 {
148   if (!myInverseNeedUpdate)
149   {
150     return myModelWorldMatrix;
151   }
152
153   reinterpret_cast<const NCollection_Mat4<float>*> (*myModelWorldMatrix)->Inverted (
154                 *(reinterpret_cast<NCollection_Mat4<float>*> (*myModelWorldMatrixInverse)));
155   return myModelWorldMatrixInverse;
156 }
157
158 // =======================================================================
159 // function : OpenGl_WorldViewState
160 // purpose  : Creates uninitialized world-view state
161 // =======================================================================
162 OpenGl_WorldViewState::OpenGl_WorldViewState()
163 : myInverseNeedUpdate (false)
164 {
165   //
166 }
167
168 // =======================================================================
169 // function : Set
170 // purpose  : Sets new world-view matrix
171 // =======================================================================
172 void OpenGl_WorldViewState::Set (const Tmatrix3* theWorldViewMatrix)
173 {
174   memcpy (myWorldViewMatrix, theWorldViewMatrix, sizeof (Tmatrix3));
175   myInverseNeedUpdate = true;
176 }
177
178 // =======================================================================
179 // function : WorldViewMatrix
180 // purpose  : Returns current world-view matrix
181 // =======================================================================
182 const Tmatrix3& OpenGl_WorldViewState::WorldViewMatrix() const
183 {
184   return myWorldViewMatrix;
185 }
186
187 // =======================================================================
188 // function : WorldViewMatrixInverse
189 // purpose  : Returns inverse of current world-view matrix
190 // =======================================================================
191 const Tmatrix3& OpenGl_WorldViewState::WorldViewMatrixInverse() const
192 {
193   if (!myInverseNeedUpdate)
194   {
195     return myWorldViewMatrix;
196   }
197
198   reinterpret_cast<const NCollection_Mat4<float>*> (*myWorldViewMatrix)->Inverted (
199                 *(reinterpret_cast<NCollection_Mat4<float>*> (*myWorldViewMatrixInverse)));
200   return myWorldViewMatrixInverse;
201 }
202
203 // =======================================================================
204 // function : OpenGl_LightSourceState
205 // purpose  : Creates uninitialized state of light sources
206 // =======================================================================
207 OpenGl_LightSourceState::OpenGl_LightSourceState()
208 : myLightSources (NULL)
209 {
210   //
211 }
212
213 // =======================================================================
214 // function : Set
215 // purpose  : Sets new light sources
216 // =======================================================================
217 void OpenGl_LightSourceState::Set (const OpenGl_ListOfLight* theLightSources)
218 {
219   myLightSources = theLightSources;
220 }
221
222 // =======================================================================
223 // function : LightSources
224 // purpose  : Returns current list of light sources
225 // =======================================================================
226 const OpenGl_ListOfLight* OpenGl_LightSourceState::LightSources() const
227 {
228   return myLightSources;
229 }
230
231 // =======================================================================
232 // function : OpenGl_MaterialState
233 // purpose  : Creates uninitialized material state
234 // =======================================================================
235 OpenGl_MaterialState::OpenGl_MaterialState (const OpenGl_Element* theAspect)
236 : myAspect (theAspect)
237 {
238   //
239 }
240
241 // =======================================================================
242 // function : Set
243 // purpose  : Sets new material aspect
244 // =======================================================================
245 void OpenGl_MaterialState::Set (const OpenGl_Element* theAspect)
246 {
247   myAspect = theAspect;
248 }
249
250 // =======================================================================
251 // function : Aspect
252 // purpose  : Returns material aspect
253 // =======================================================================
254 const OpenGl_Element* OpenGl_MaterialState::Aspect() const
255 {
256   return myAspect;
257 }
258
259 // =======================================================================
260 // function : OpenGl_ClippingState
261 // purpose  : Creates new clipping state
262 // =======================================================================
263 OpenGl_ClippingState::OpenGl_ClippingState()
264 {
265   //
266 }