1 | #include "RenderState.h"
|
---|
2 | #include "Geometry.h"
|
---|
3 | #include "Material.h"
|
---|
4 | #include "ShaderProgram.h"
|
---|
5 | #include "Texture.h"
|
---|
6 | #include "ShaderManager.h"
|
---|
7 |
|
---|
8 |
|
---|
9 | using namespace std;
|
---|
10 |
|
---|
11 |
|
---|
12 | namespace CHCDemoEngine
|
---|
13 | {
|
---|
14 |
|
---|
15 |
|
---|
16 | /////////////////
|
---|
17 |
|
---|
18 | RenderState::RenderState():
|
---|
19 | mRenderTechnique(0),
|
---|
20 | mUseAlphaToCoverage(true),
|
---|
21 | mLockCullFaceEnabled(false),
|
---|
22 | mCurrentVboId(-1),
|
---|
23 | mCurrentTexId(0),
|
---|
24 | mCullFaceEnabled(true),
|
---|
25 | mAlphaTestEnabled(false),
|
---|
26 | mTexturesEnabled(false),
|
---|
27 | mCurrentVertexProgram(NULL),
|
---|
28 | mCurrentFragmentProgram(NULL),
|
---|
29 | mLightingEnabled(true),
|
---|
30 | mColorWriteEnabled(true),
|
---|
31 | mDepthWriteEnabled(true),
|
---|
32 | mCurrentTechnique(NULL)
|
---|
33 | {
|
---|
34 | SetMode(RENDER);
|
---|
35 |
|
---|
36 | glEnable(GL_CULL_FACE);
|
---|
37 | glDisable(GL_SAMPLE_ALPHA_TO_COVERAGE_ARB);
|
---|
38 | glDisable(GL_ALPHA_TEST);
|
---|
39 | glDisable(GL_TEXTURE_2D);
|
---|
40 | glDisableClientState(GL_TEXTURE_COORD_ARRAY);
|
---|
41 | glEnable(GL_LIGHTING);
|
---|
42 | glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
|
---|
43 | glDepthMask(GL_TRUE);
|
---|
44 | }
|
---|
45 |
|
---|
46 |
|
---|
47 | void RenderState::Reset()
|
---|
48 | {
|
---|
49 | SetMode(RENDER);
|
---|
50 |
|
---|
51 | mCurrentVboId = -1;
|
---|
52 | mCurrentTexId = 0;
|
---|
53 |
|
---|
54 | mCullFaceEnabled = true;
|
---|
55 | mAlphaTestEnabled = false;
|
---|
56 | mTexturesEnabled = false;
|
---|
57 |
|
---|
58 | mCurrentVertexProgram = NULL;
|
---|
59 | mCurrentFragmentProgram = NULL;
|
---|
60 |
|
---|
61 | glEnable(GL_CULL_FACE);
|
---|
62 |
|
---|
63 | glDisable(GL_SAMPLE_ALPHA_TO_COVERAGE_ARB);
|
---|
64 | glDisable(GL_ALPHA_TEST);
|
---|
65 |
|
---|
66 | glDisable(GL_TEXTURE_2D);
|
---|
67 | glDisableClientState(GL_TEXTURE_COORD_ARRAY);
|
---|
68 |
|
---|
69 | ShaderManager::GetSingleton()->DisableFragmentProfile();
|
---|
70 | ShaderManager::GetSingleton()->DisableVertexProfile();
|
---|
71 |
|
---|
72 | mFragmentProgramEnabled = false;
|
---|
73 | mVertexProgramEnabled = false;
|
---|
74 |
|
---|
75 | mLightingEnabled = true;
|
---|
76 | glEnable(GL_LIGHTING);
|
---|
77 |
|
---|
78 | mColorWriteEnabled = true;
|
---|
79 | glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
|
---|
80 |
|
---|
81 | mDepthWriteEnabled = true;
|
---|
82 | glDepthMask(GL_TRUE);
|
---|
83 |
|
---|
84 | mCurrentTechnique = NULL;
|
---|
85 | }
|
---|
86 |
|
---|
87 |
|
---|
88 | static Technique GetDummyTechnique()
|
---|
89 | {
|
---|
90 | Technique tech;
|
---|
91 | tech.Init();
|
---|
92 |
|
---|
93 | tech.SetColorWriteEnabled(true);
|
---|
94 | tech.SetLightingEnabled(true);
|
---|
95 | tech.SetDepthWriteEnabled(false);
|
---|
96 | //tech.SetCullFaceEnabled(false);
|
---|
97 |
|
---|
98 | return tech;
|
---|
99 | }
|
---|
100 |
|
---|
101 | /** A special technique for occlusion queries. This material
|
---|
102 | has color write, depth write, and lighting turned off
|
---|
103 | */
|
---|
104 | static Technique GetQueryTechnique()
|
---|
105 | {
|
---|
106 | Technique tech;
|
---|
107 | tech.Init();
|
---|
108 |
|
---|
109 | tech.SetColorWriteEnabled(false);
|
---|
110 | tech.SetLightingEnabled(false);
|
---|
111 | tech.SetDepthWriteEnabled(false);
|
---|
112 | //tech.SetCullFaceEnabled(false);
|
---|
113 |
|
---|
114 | return tech;
|
---|
115 | }
|
---|
116 |
|
---|
117 |
|
---|
118 | bool RenderState::SetMode(Mode mode)
|
---|
119 | {
|
---|
120 | ///////////
|
---|
121 | //-- just change the mode when necessary
|
---|
122 |
|
---|
123 | if (mode == mMode) return false;
|
---|
124 |
|
---|
125 | mMode = mode;
|
---|
126 |
|
---|
127 | if (mode == QUERY)
|
---|
128 | {
|
---|
129 | /// the query box material
|
---|
130 | static Technique queryTech = GetQueryTechnique();
|
---|
131 | SetState(&queryTech);
|
---|
132 |
|
---|
133 | glDisableClientState(GL_NORMAL_ARRAY);
|
---|
134 | }
|
---|
135 | else
|
---|
136 | {
|
---|
137 | glEnableClientState(GL_NORMAL_ARRAY);
|
---|
138 | }
|
---|
139 |
|
---|
140 | return true;
|
---|
141 | }
|
---|
142 |
|
---|
143 |
|
---|
144 | void RenderState::SetState(Technique *tech, SceneEntity *ent)
|
---|
145 | {
|
---|
146 | //if (mCurrentTechnique == tech) return;
|
---|
147 | mCurrentTechnique = tech;
|
---|
148 |
|
---|
149 | Texture * const tex = tech->GetTexture();
|
---|
150 |
|
---|
151 | const bool texturing = (tex != NULL);
|
---|
152 | const bool alphaTest = tech->IsAlphaTestEnabled();
|
---|
153 | const bool cullFace = tech->IsCullFaceEnabled();
|
---|
154 | const bool lighting = tech->IsLightingEnabled();
|
---|
155 | const bool colorWrite = tech->IsColorWriteEnabled();
|
---|
156 | const bool depthWrite = tech->IsDepthWriteEnabled();
|
---|
157 |
|
---|
158 |
|
---|
159 | if (mDepthWriteEnabled && !depthWrite)
|
---|
160 | {
|
---|
161 | mDepthWriteEnabled = false;
|
---|
162 | glDepthMask(GL_FALSE);
|
---|
163 | }
|
---|
164 | else if (!mDepthWriteEnabled && depthWrite)
|
---|
165 | {
|
---|
166 | mDepthWriteEnabled = true;
|
---|
167 | glDepthMask(GL_TRUE);
|
---|
168 | }
|
---|
169 |
|
---|
170 | if (mLightingEnabled && !lighting)
|
---|
171 | {
|
---|
172 | mLightingEnabled = false;
|
---|
173 | glDisable(GL_LIGHTING);
|
---|
174 | }
|
---|
175 | else if (!mLightingEnabled && lighting)
|
---|
176 | {
|
---|
177 | mLightingEnabled = true;
|
---|
178 | glEnable(GL_LIGHTING);
|
---|
179 | }
|
---|
180 |
|
---|
181 | if (mColorWriteEnabled && !colorWrite)
|
---|
182 | {
|
---|
183 | mColorWriteEnabled = false;
|
---|
184 | glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
|
---|
185 | }
|
---|
186 | else if (!mColorWriteEnabled && colorWrite)
|
---|
187 | {
|
---|
188 | mColorWriteEnabled = true;
|
---|
189 | glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
|
---|
190 | }
|
---|
191 |
|
---|
192 | if (!mLockCullFaceEnabled)
|
---|
193 | {
|
---|
194 | if (mCullFaceEnabled && !cullFace)
|
---|
195 | {
|
---|
196 | mCullFaceEnabled = false;
|
---|
197 | glDisable(GL_CULL_FACE);
|
---|
198 | }
|
---|
199 | else if (!mCullFaceEnabled && cullFace)
|
---|
200 | {
|
---|
201 | mCullFaceEnabled = true;
|
---|
202 | glEnable(GL_CULL_FACE);
|
---|
203 | }
|
---|
204 | }
|
---|
205 | if (mAlphaTestEnabled && !alphaTest)
|
---|
206 | {
|
---|
207 | mAlphaTestEnabled = false;
|
---|
208 |
|
---|
209 | if (mUseAlphaToCoverage)
|
---|
210 | glDisable(GL_SAMPLE_ALPHA_TO_COVERAGE_ARB);
|
---|
211 | else
|
---|
212 | glDisable(GL_ALPHA_TEST);
|
---|
213 | }
|
---|
214 | else if (!mAlphaTestEnabled && alphaTest)
|
---|
215 | {
|
---|
216 | mAlphaTestEnabled = true;
|
---|
217 |
|
---|
218 | if (mUseAlphaToCoverage)
|
---|
219 | glEnable(GL_SAMPLE_ALPHA_TO_COVERAGE_ARB);
|
---|
220 | else
|
---|
221 | glEnable(GL_ALPHA_TEST);
|
---|
222 | }
|
---|
223 |
|
---|
224 | if (mTexturesEnabled && !texturing)
|
---|
225 | {
|
---|
226 | mTexturesEnabled = false;
|
---|
227 | glDisable(GL_TEXTURE_2D);
|
---|
228 |
|
---|
229 | glDisableClientState(GL_TEXTURE_COORD_ARRAY);
|
---|
230 | }
|
---|
231 | else if (!mTexturesEnabled && texturing)
|
---|
232 | {
|
---|
233 | mTexturesEnabled = true;
|
---|
234 |
|
---|
235 | glEnable(GL_TEXTURE_2D);
|
---|
236 | glEnableClientState(GL_TEXTURE_COORD_ARRAY);
|
---|
237 | }
|
---|
238 |
|
---|
239 |
|
---|
240 | // set texture for fixed function rendering
|
---|
241 | if (tex)
|
---|
242 | {
|
---|
243 | if (mCurrentTexId != tex->GetId())
|
---|
244 | {
|
---|
245 | tex->Bind();
|
---|
246 | mCurrentTexId = tex->GetId();
|
---|
247 | }
|
---|
248 | }
|
---|
249 | else
|
---|
250 | {
|
---|
251 | mCurrentTexId = -1;
|
---|
252 | glBindTexture(GL_TEXTURE_2D, 0);
|
---|
253 | }
|
---|
254 |
|
---|
255 |
|
---|
256 | //////////////////
|
---|
257 | //-- fragment and vertex programs
|
---|
258 |
|
---|
259 | ShaderProgram *frag = tech->GetFragmentProgram();
|
---|
260 |
|
---|
261 | if (frag)
|
---|
262 | {
|
---|
263 | if (!mFragmentProgramEnabled)
|
---|
264 | {
|
---|
265 | mFragmentProgramEnabled = true;
|
---|
266 | ShaderManager::GetSingleton()->EnableFragmentProfile();
|
---|
267 | }
|
---|
268 |
|
---|
269 | if (frag != mCurrentFragmentProgram)
|
---|
270 | {
|
---|
271 | mCurrentFragmentProgram = frag;
|
---|
272 | mCurrentFragmentProgram->Bind();
|
---|
273 | }
|
---|
274 |
|
---|
275 | tech->GetFragmentProgramParameters()->UpdateParameters(ent);
|
---|
276 | }
|
---|
277 | else
|
---|
278 | {
|
---|
279 | if (mFragmentProgramEnabled)
|
---|
280 | {
|
---|
281 | mFragmentProgramEnabled = false;
|
---|
282 | ShaderManager::GetSingleton()->DisableFragmentProfile();
|
---|
283 | }
|
---|
284 | }
|
---|
285 |
|
---|
286 | ShaderProgram *vtx = tech->GetVertexProgram();
|
---|
287 |
|
---|
288 | if (vtx)
|
---|
289 | {
|
---|
290 | if (!mVertexProgramEnabled)
|
---|
291 | {
|
---|
292 | mVertexProgramEnabled = true;
|
---|
293 | ShaderManager::GetSingleton()->EnableVertexProfile();
|
---|
294 | }
|
---|
295 |
|
---|
296 | if (vtx != mCurrentVertexProgram)
|
---|
297 | {
|
---|
298 | mCurrentVertexProgram = vtx;
|
---|
299 | mCurrentVertexProgram->Bind();
|
---|
300 | }
|
---|
301 |
|
---|
302 | tech->GetVertexProgramParameters()->UpdateParameters(ent);
|
---|
303 | }
|
---|
304 | else
|
---|
305 | {
|
---|
306 | if (mVertexProgramEnabled)
|
---|
307 | {
|
---|
308 | mVertexProgramEnabled = false;
|
---|
309 | ShaderManager::GetSingleton()->DisableVertexProfile();
|
---|
310 | }
|
---|
311 | }
|
---|
312 | }
|
---|
313 |
|
---|
314 |
|
---|
315 | void RenderState::SetUseAlphaToCoverage(bool useAlphaToCoverage)
|
---|
316 | {
|
---|
317 | mUseAlphaToCoverage = useAlphaToCoverage;
|
---|
318 | }
|
---|
319 |
|
---|
320 |
|
---|
321 | void RenderState::SetRenderTechnique(int t)
|
---|
322 | {
|
---|
323 | mRenderTechnique = t;
|
---|
324 | }
|
---|
325 |
|
---|
326 |
|
---|
327 | int RenderState::GetRenderTechnique() const
|
---|
328 | {
|
---|
329 | return mRenderTechnique;
|
---|
330 | }
|
---|
331 |
|
---|
332 |
|
---|
333 | void RenderState::LockCullFaceEnabled(bool lockCull)
|
---|
334 | {
|
---|
335 | mLockCullFaceEnabled = lockCull;
|
---|
336 | }
|
---|
337 |
|
---|
338 |
|
---|
339 |
|
---|
340 | } |
---|