fa7506691c9d4d75920475d72aa0a76cdbc7efbc
[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 <OpenGl_ShaderStates.hxx>
17
18 // =======================================================================
19 // function : OpenGl_StateInterface
20 // purpose  :
21 // =======================================================================
22 OpenGl_StateInterface::OpenGl_StateInterface()
23 : myIndex (0)
24 {
25   //
26 }
27
28 // =======================================================================
29 // function : OpenGl_ProjectionState
30 // purpose  : Creates uninitialized projection state
31 // =======================================================================
32 OpenGl_ProjectionState::OpenGl_ProjectionState()
33 : myInverseNeedUpdate (false)
34 {
35   //
36 }
37
38 // =======================================================================
39 // function : Set
40 // purpose  : Sets new OCCT projection state
41 // =======================================================================
42 void OpenGl_ProjectionState::Set (const OpenGl_Mat4& theProjectionMatrix)
43 {
44   myProjectionMatrix = theProjectionMatrix;
45   myInverseNeedUpdate = true;
46 }
47
48 // =======================================================================
49 // function : ProjectionMatrix
50 // purpose  : Returns current projection matrix
51 // =======================================================================
52 const OpenGl_Mat4& OpenGl_ProjectionState::ProjectionMatrix() const
53 {
54   return myProjectionMatrix;
55 }
56
57 // =======================================================================
58 // function : ProjectionMatrixInverse
59 // purpose  : Returns inverse of current projection matrix
60 // =======================================================================
61 const OpenGl_Mat4& OpenGl_ProjectionState::ProjectionMatrixInverse() const
62 {
63   if (!myInverseNeedUpdate)
64   {
65     return myProjectionMatrixInverse;
66   }
67
68   myProjectionMatrix.Inverted (myProjectionMatrixInverse);
69
70   return myProjectionMatrixInverse;
71 }
72
73 // =======================================================================
74 // function : OpenGl_ModelWorldState
75 // purpose  : Creates uninitialized model-world state
76 // =======================================================================
77 OpenGl_ModelWorldState::OpenGl_ModelWorldState()
78 : myInverseNeedUpdate (false)
79 {
80   //
81 }
82
83 // =======================================================================
84 // function : Set
85 // purpose  : Sets new model-world matrix
86 // =======================================================================
87 void OpenGl_ModelWorldState::Set (const OpenGl_Mat4& theModelWorldMatrix)
88 {
89   myModelWorldMatrix = theModelWorldMatrix;
90   myInverseNeedUpdate = true;
91 }
92
93 // =======================================================================
94 // function : ModelWorldMatrix
95 // purpose  : Returns current model-world matrix
96 // =======================================================================
97 const OpenGl_Mat4& OpenGl_ModelWorldState::ModelWorldMatrix() const
98 {
99   return myModelWorldMatrix;
100 }
101
102 // =======================================================================
103 // function : ModelWorldMatrixInverse
104 // purpose  : Returns inverse of current model-world matrix
105 // =======================================================================
106 const OpenGl_Mat4& OpenGl_ModelWorldState::ModelWorldMatrixInverse() const
107 {
108   if (!myInverseNeedUpdate)
109   {
110     return myModelWorldMatrix;
111   }
112
113   myModelWorldMatrix.Inverted (myModelWorldMatrixInverse);
114
115   return myModelWorldMatrixInverse;
116 }
117
118 // =======================================================================
119 // function : OpenGl_WorldViewState
120 // purpose  : Creates uninitialized world-view state
121 // =======================================================================
122 OpenGl_WorldViewState::OpenGl_WorldViewState()
123 : myInverseNeedUpdate (false)
124 {
125   //
126 }
127
128 // =======================================================================
129 // function : Set
130 // purpose  : Sets new world-view matrix
131 // =======================================================================
132 void OpenGl_WorldViewState::Set (const OpenGl_Mat4& theWorldViewMatrix)
133 {
134   myWorldViewMatrix = theWorldViewMatrix;
135   myInverseNeedUpdate = true;
136 }
137
138 // =======================================================================
139 // function : WorldViewMatrix
140 // purpose  : Returns current world-view matrix
141 // =======================================================================
142 const OpenGl_Mat4& OpenGl_WorldViewState::WorldViewMatrix() const
143 {
144   return myWorldViewMatrix;
145 }
146
147 // =======================================================================
148 // function : WorldViewMatrixInverse
149 // purpose  : Returns inverse of current world-view matrix
150 // =======================================================================
151 const OpenGl_Mat4& OpenGl_WorldViewState::WorldViewMatrixInverse() const
152 {
153   if (!myInverseNeedUpdate)
154   {
155     return myWorldViewMatrix;
156   }
157
158   myWorldViewMatrix.Inverted (myWorldViewMatrixInverse);
159
160   return myWorldViewMatrixInverse;
161 }
162
163 // =======================================================================
164 // function : OpenGl_LightSourceState
165 // purpose  : Creates uninitialized state of light sources
166 // =======================================================================
167 OpenGl_LightSourceState::OpenGl_LightSourceState()
168 : myLightSources (NULL)
169 {
170   //
171 }
172
173 // =======================================================================
174 // function : Set
175 // purpose  : Sets new light sources
176 // =======================================================================
177 void OpenGl_LightSourceState::Set (const OpenGl_ListOfLight* theLightSources)
178 {
179   myLightSources = theLightSources;
180 }
181
182 // =======================================================================
183 // function : LightSources
184 // purpose  : Returns current list of light sources
185 // =======================================================================
186 const OpenGl_ListOfLight* OpenGl_LightSourceState::LightSources() const
187 {
188   return myLightSources;
189 }
190
191 // =======================================================================
192 // function : OpenGl_ClippingState
193 // purpose  : Creates new clipping state
194 // =======================================================================
195 OpenGl_ClippingState::OpenGl_ClippingState()
196 : myIndex (0),
197   myNextIndex (1)
198 {
199   //
200 }
201
202 // =======================================================================
203 // function : Update
204 // purpose  : Updates current state
205 // =======================================================================
206 void OpenGl_ClippingState::Update()
207 {
208   myStateStack.Prepend (myIndex);
209   myIndex = myNextIndex; // use myNextIndex here to handle properly Update() after Revert()
210   ++myNextIndex;
211 }
212
213 // =======================================================================
214 // function : Revert
215 // purpose  : Reverts current state
216 // =======================================================================
217 void OpenGl_ClippingState::Revert()
218 {
219   if (!myStateStack.IsEmpty())
220   {
221     myIndex = myStateStack.First();
222     myStateStack.RemoveFirst();
223   }
224   else
225   {
226     myIndex = 0;
227   }
228 }