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 |
|
---|
134 | return true;
|
---|
135 | }
|
---|
136 |
|
---|
137 |
|
---|
138 | void RenderState::SetState(Technique *tech)
|
---|
139 | {
|
---|
140 | if (mCurrentTechnique == tech)
|
---|
141 | return;
|
---|
142 |
|
---|
143 | mCurrentTechnique = tech;
|
---|
144 |
|
---|
145 | Texture * const tex = tech->GetTexture();
|
---|
146 |
|
---|
147 | const bool texturing = (tex != NULL);
|
---|
148 | const bool alphaTest = tech->IsAlphaTestEnabled();
|
---|
149 | const bool cullFace = tech->IsCullFaceEnabled();
|
---|
150 | const bool lighting = tech->IsLightingEnabled();
|
---|
151 | const bool colorWrite = tech->IsColorWriteEnabled();
|
---|
152 | const bool depthWrite = tech->IsDepthWriteEnabled();
|
---|
153 |
|
---|
154 | if (mDepthWriteEnabled && !depthWrite)
|
---|
155 | {
|
---|
156 | mDepthWriteEnabled = false;
|
---|
157 | glDepthMask(GL_FALSE);
|
---|
158 | }
|
---|
159 | else if (!mDepthWriteEnabled && depthWrite)
|
---|
160 | {
|
---|
161 | mDepthWriteEnabled = true;
|
---|
162 | glDepthMask(GL_TRUE);
|
---|
163 | }
|
---|
164 |
|
---|
165 | if (mLightingEnabled && !lighting)
|
---|
166 | {
|
---|
167 | mLightingEnabled = false;
|
---|
168 | glDisable(GL_LIGHTING);
|
---|
169 | }
|
---|
170 | else if (!mLightingEnabled && lighting)
|
---|
171 | {
|
---|
172 | mLightingEnabled = true;
|
---|
173 | glEnable(GL_LIGHTING);
|
---|
174 | }
|
---|
175 |
|
---|
176 | if (mColorWriteEnabled && !colorWrite)
|
---|
177 | {
|
---|
178 | mColorWriteEnabled = false;
|
---|
179 | glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
|
---|
180 | }
|
---|
181 | else if (!mColorWriteEnabled && colorWrite)
|
---|
182 | {
|
---|
183 | mColorWriteEnabled = true;
|
---|
184 | glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
|
---|
185 | }
|
---|
186 |
|
---|
187 | if (!mLockCullFaceEnabled)
|
---|
188 | {
|
---|
189 | if (mCullFaceEnabled && !cullFace)
|
---|
190 | {
|
---|
191 | mCullFaceEnabled = false;
|
---|
192 | glDisable(GL_CULL_FACE);
|
---|
193 | }
|
---|
194 | else if (!mCullFaceEnabled && cullFace)
|
---|
195 | {
|
---|
196 | mCullFaceEnabled = true;
|
---|
197 | glEnable(GL_CULL_FACE);
|
---|
198 | }
|
---|
199 | }
|
---|
200 | if (mAlphaTestEnabled && !alphaTest)
|
---|
201 | {
|
---|
202 | mAlphaTestEnabled = false;
|
---|
203 |
|
---|
204 | if (mUseAlphaToCoverage)
|
---|
205 | glDisable(GL_SAMPLE_ALPHA_TO_COVERAGE_ARB);
|
---|
206 | else
|
---|
207 | glDisable(GL_ALPHA_TEST);
|
---|
208 | }
|
---|
209 | else if (!mAlphaTestEnabled && alphaTest)
|
---|
210 | {
|
---|
211 | mAlphaTestEnabled = true;
|
---|
212 |
|
---|
213 | if (mUseAlphaToCoverage)
|
---|
214 | glEnable(GL_SAMPLE_ALPHA_TO_COVERAGE_ARB);
|
---|
215 | else
|
---|
216 | glEnable(GL_ALPHA_TEST);
|
---|
217 | }
|
---|
218 |
|
---|
219 | if (mTexturesEnabled && !texturing)
|
---|
220 | {
|
---|
221 | mTexturesEnabled = false;
|
---|
222 |
|
---|
223 | //if (!tech->GetFragmentProgram())
|
---|
224 | glDisable(GL_TEXTURE_2D);
|
---|
225 |
|
---|
226 | glDisableClientState(GL_TEXTURE_COORD_ARRAY);
|
---|
227 | }
|
---|
228 | else if (!mTexturesEnabled && texturing)
|
---|
229 | {
|
---|
230 | mTexturesEnabled = true;
|
---|
231 |
|
---|
232 | //if (!tech->GetFragmentProgram())
|
---|
233 | glEnable(GL_TEXTURE_2D);
|
---|
234 |
|
---|
235 | glEnableClientState(GL_TEXTURE_COORD_ARRAY);
|
---|
236 | }
|
---|
237 |
|
---|
238 |
|
---|
239 | //////////////////
|
---|
240 | //-- fragment and vertex programs
|
---|
241 |
|
---|
242 | ShaderProgram *frag = tech->GetFragmentProgram();
|
---|
243 |
|
---|
244 | if (frag)
|
---|
245 | {
|
---|
246 | if (!mFragmentProgramEnabled)
|
---|
247 | {
|
---|
248 | mFragmentProgramEnabled = true;
|
---|
249 | ShaderManager::GetSingleton()->EnableFragmentProfile();
|
---|
250 | }
|
---|
251 |
|
---|
252 | if (frag != mCurrentFragmentProgram)
|
---|
253 | {
|
---|
254 | mCurrentFragmentProgram = frag;
|
---|
255 | mCurrentFragmentProgram->Bind();
|
---|
256 | }
|
---|
257 |
|
---|
258 | tech->GetFragmentProgramParameters()->UpdateParameters();
|
---|
259 | }
|
---|
260 | else
|
---|
261 | {
|
---|
262 | if (mFragmentProgramEnabled)
|
---|
263 | {
|
---|
264 | mFragmentProgramEnabled = false;
|
---|
265 | ShaderManager::GetSingleton()->DisableFragmentProfile();
|
---|
266 | }
|
---|
267 | }
|
---|
268 |
|
---|
269 | ShaderProgram *vert = tech->GetVertexProgram();
|
---|
270 |
|
---|
271 | if (vert)
|
---|
272 | {
|
---|
273 | if (!mVertexProgramEnabled)
|
---|
274 | {
|
---|
275 | mVertexProgramEnabled = true;
|
---|
276 | ShaderManager::GetSingleton()->EnableVertexProfile();
|
---|
277 | }
|
---|
278 |
|
---|
279 | if (vert != mCurrentVertexProgram)
|
---|
280 | {
|
---|
281 | mCurrentVertexProgram = vert;
|
---|
282 | mCurrentVertexProgram->Bind();
|
---|
283 | }
|
---|
284 |
|
---|
285 | tech->GetVertexProgramParameters()->UpdateParameters();
|
---|
286 | }
|
---|
287 | else
|
---|
288 | {
|
---|
289 | if (mVertexProgramEnabled)
|
---|
290 | {
|
---|
291 | mVertexProgramEnabled = false;
|
---|
292 | ShaderManager::GetSingleton()->DisableVertexProfile();
|
---|
293 | }
|
---|
294 | }
|
---|
295 |
|
---|
296 | if (!mFragmentProgramEnabled)
|
---|
297 | {
|
---|
298 | // set texture for fixed function rendering
|
---|
299 | if (tex)
|
---|
300 | {
|
---|
301 | if (mCurrentTexId != tex->GetId())
|
---|
302 | {
|
---|
303 | tex->Bind();
|
---|
304 | mCurrentTexId = tex->GetId();
|
---|
305 | }
|
---|
306 | }
|
---|
307 | else
|
---|
308 | {
|
---|
309 | mCurrentTexId = -1;
|
---|
310 | glBindTexture(GL_TEXTURE_2D, 0);
|
---|
311 | }
|
---|
312 | }
|
---|
313 | }
|
---|
314 |
|
---|
315 |
|
---|
316 | void RenderState::SetUseAlphaToCoverage(bool useAlphaToCoverage)
|
---|
317 | {
|
---|
318 | mUseAlphaToCoverage = useAlphaToCoverage;
|
---|
319 | }
|
---|
320 |
|
---|
321 |
|
---|
322 | void RenderState::SetRenderTechnique(int t)
|
---|
323 | {
|
---|
324 | mRenderTechnique = t;
|
---|
325 | }
|
---|
326 |
|
---|
327 |
|
---|
328 | int RenderState::GetRenderTechnique() const
|
---|
329 | {
|
---|
330 | return mRenderTechnique;
|
---|
331 | }
|
---|
332 |
|
---|
333 |
|
---|
334 | void RenderState::LockCullFaceEnabled(bool lockCull)
|
---|
335 | {
|
---|
336 | mLockCullFaceEnabled = lockCull;
|
---|
337 | }
|
---|
338 |
|
---|
339 |
|
---|
340 |
|
---|
341 | } |
---|