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 | /** A special technique for occlusion queries. This material
|
---|
89 | has color write, depth write, and lighting turned off
|
---|
90 | */
|
---|
91 | static Technique GetQueryTechnique()
|
---|
92 | {
|
---|
93 | Technique tech;
|
---|
94 | tech.Init();
|
---|
95 |
|
---|
96 | tech.SetColorWriteEnabled(false);
|
---|
97 | tech.SetLightingEnabled(false);
|
---|
98 | tech.SetDepthWriteEnabled(false);
|
---|
99 |
|
---|
100 | return tech;
|
---|
101 | }
|
---|
102 |
|
---|
103 |
|
---|
104 | bool RenderState::SetMode(Mode mode)
|
---|
105 | {
|
---|
106 | ///////////
|
---|
107 | //-- just change the mode when necessary
|
---|
108 |
|
---|
109 | if (mode == mMode) return false;
|
---|
110 |
|
---|
111 | mMode = mode;
|
---|
112 |
|
---|
113 | if (mode == QUERY)
|
---|
114 | {
|
---|
115 | /// the query box material
|
---|
116 | static Technique queryTech = GetQueryTechnique();
|
---|
117 | SetState(&queryTech);
|
---|
118 |
|
---|
119 | glDisableClientState(GL_NORMAL_ARRAY);
|
---|
120 | }
|
---|
121 | else
|
---|
122 | {
|
---|
123 | glEnableClientState(GL_NORMAL_ARRAY);
|
---|
124 | }
|
---|
125 |
|
---|
126 | return true;
|
---|
127 | }
|
---|
128 |
|
---|
129 |
|
---|
130 | void RenderState::SetState(Technique *tech, SceneEntity *ent)
|
---|
131 | {
|
---|
132 | //if (mCurrentTechnique == tech) return;
|
---|
133 | mCurrentTechnique = tech;
|
---|
134 |
|
---|
135 | Texture * const tex = tech->GetTexture();
|
---|
136 |
|
---|
137 | const bool texturing = (tex != NULL);
|
---|
138 | const bool alphaTest = tech->IsAlphaTestEnabled();
|
---|
139 | const bool cullFace = tech->IsCullFaceEnabled();
|
---|
140 | const bool lighting = tech->IsLightingEnabled();
|
---|
141 | const bool colorWrite = tech->IsColorWriteEnabled();
|
---|
142 | const bool depthWrite = tech->IsDepthWriteEnabled();
|
---|
143 |
|
---|
144 |
|
---|
145 | if (mDepthWriteEnabled && !depthWrite)
|
---|
146 | {
|
---|
147 | mDepthWriteEnabled = false;
|
---|
148 | glDepthMask(GL_FALSE);
|
---|
149 | }
|
---|
150 | else if (!mDepthWriteEnabled && depthWrite)
|
---|
151 | {
|
---|
152 | mDepthWriteEnabled = true;
|
---|
153 | glDepthMask(GL_TRUE);
|
---|
154 | }
|
---|
155 |
|
---|
156 | if (mLightingEnabled && !lighting)
|
---|
157 | {
|
---|
158 | mLightingEnabled = false;
|
---|
159 | glDisable(GL_LIGHTING);
|
---|
160 | }
|
---|
161 | else if (!mLightingEnabled && lighting)
|
---|
162 | {
|
---|
163 | mLightingEnabled = true;
|
---|
164 | glEnable(GL_LIGHTING);
|
---|
165 | }
|
---|
166 |
|
---|
167 | if (mColorWriteEnabled && !colorWrite)
|
---|
168 | {
|
---|
169 | mColorWriteEnabled = false;
|
---|
170 | glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
|
---|
171 | }
|
---|
172 | else if (!mColorWriteEnabled && colorWrite)
|
---|
173 | {
|
---|
174 | mColorWriteEnabled = true;
|
---|
175 | glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
|
---|
176 | }
|
---|
177 |
|
---|
178 | if (!mLockCullFaceEnabled)
|
---|
179 | {
|
---|
180 | if (mCullFaceEnabled && !cullFace)
|
---|
181 | {
|
---|
182 | mCullFaceEnabled = false;
|
---|
183 | glDisable(GL_CULL_FACE);
|
---|
184 | }
|
---|
185 | else if (!mCullFaceEnabled && cullFace)
|
---|
186 | {
|
---|
187 | mCullFaceEnabled = true;
|
---|
188 | glEnable(GL_CULL_FACE);
|
---|
189 | }
|
---|
190 | }
|
---|
191 | if (mAlphaTestEnabled && !alphaTest)
|
---|
192 | {
|
---|
193 | mAlphaTestEnabled = false;
|
---|
194 |
|
---|
195 | if (mUseAlphaToCoverage)
|
---|
196 | glDisable(GL_SAMPLE_ALPHA_TO_COVERAGE_ARB);
|
---|
197 | else
|
---|
198 | glDisable(GL_ALPHA_TEST);
|
---|
199 | }
|
---|
200 | else if (!mAlphaTestEnabled && alphaTest)
|
---|
201 | {
|
---|
202 | mAlphaTestEnabled = true;
|
---|
203 |
|
---|
204 | if (mUseAlphaToCoverage)
|
---|
205 | glEnable(GL_SAMPLE_ALPHA_TO_COVERAGE_ARB);
|
---|
206 | else
|
---|
207 | glEnable(GL_ALPHA_TEST);
|
---|
208 | }
|
---|
209 |
|
---|
210 | if (mTexturesEnabled && !texturing)
|
---|
211 | {
|
---|
212 | mTexturesEnabled = false;
|
---|
213 | glDisable(GL_TEXTURE_2D);
|
---|
214 |
|
---|
215 | glDisableClientState(GL_TEXTURE_COORD_ARRAY);
|
---|
216 | }
|
---|
217 | else if (!mTexturesEnabled && texturing)
|
---|
218 | {
|
---|
219 | mTexturesEnabled = true;
|
---|
220 |
|
---|
221 | glEnable(GL_TEXTURE_2D);
|
---|
222 | glEnableClientState(GL_TEXTURE_COORD_ARRAY);
|
---|
223 | }
|
---|
224 |
|
---|
225 |
|
---|
226 | // set texture for fixed function rendering
|
---|
227 | if (tex)
|
---|
228 | {
|
---|
229 | if (mCurrentTexId != tex->GetId())
|
---|
230 | {
|
---|
231 | tex->Bind();
|
---|
232 | mCurrentTexId = tex->GetId();
|
---|
233 | }
|
---|
234 | }
|
---|
235 | else
|
---|
236 | {
|
---|
237 | mCurrentTexId = -1;
|
---|
238 | glBindTexture(GL_TEXTURE_2D, 0);
|
---|
239 | }
|
---|
240 |
|
---|
241 |
|
---|
242 | //////////////////
|
---|
243 | //-- fragment and vertex programs
|
---|
244 |
|
---|
245 | ShaderProgram *frag = tech->GetFragmentProgram();
|
---|
246 |
|
---|
247 | if (frag)
|
---|
248 | {
|
---|
249 | if (!mFragmentProgramEnabled)
|
---|
250 | {
|
---|
251 | mFragmentProgramEnabled = true;
|
---|
252 | ShaderManager::GetSingleton()->EnableFragmentProfile();
|
---|
253 | }
|
---|
254 |
|
---|
255 | if (frag != mCurrentFragmentProgram)
|
---|
256 | {
|
---|
257 | mCurrentFragmentProgram = frag;
|
---|
258 | mCurrentFragmentProgram->Bind();
|
---|
259 | }
|
---|
260 |
|
---|
261 | tech->GetFragmentProgramParameters()->UpdateParameters(ent);
|
---|
262 | }
|
---|
263 | else
|
---|
264 | {
|
---|
265 | if (mFragmentProgramEnabled)
|
---|
266 | {
|
---|
267 | mFragmentProgramEnabled = false;
|
---|
268 | ShaderManager::GetSingleton()->DisableFragmentProfile();
|
---|
269 | }
|
---|
270 | }
|
---|
271 |
|
---|
272 | ShaderProgram *vtx = tech->GetVertexProgram();
|
---|
273 |
|
---|
274 | if (vtx)
|
---|
275 | {
|
---|
276 | if (!mVertexProgramEnabled)
|
---|
277 | {
|
---|
278 | mVertexProgramEnabled = true;
|
---|
279 | ShaderManager::GetSingleton()->EnableVertexProfile();
|
---|
280 | }
|
---|
281 |
|
---|
282 | if (vtx != mCurrentVertexProgram)
|
---|
283 | {
|
---|
284 | mCurrentVertexProgram = vtx;
|
---|
285 | mCurrentVertexProgram->Bind();
|
---|
286 | }
|
---|
287 |
|
---|
288 | tech->GetVertexProgramParameters()->UpdateParameters(ent);
|
---|
289 | }
|
---|
290 | else
|
---|
291 | {
|
---|
292 | if (mVertexProgramEnabled)
|
---|
293 | {
|
---|
294 | mVertexProgramEnabled = false;
|
---|
295 | ShaderManager::GetSingleton()->DisableVertexProfile();
|
---|
296 | }
|
---|
297 | }
|
---|
298 | }
|
---|
299 |
|
---|
300 |
|
---|
301 | void RenderState::SetUseAlphaToCoverage(bool useAlphaToCoverage)
|
---|
302 | {
|
---|
303 | mUseAlphaToCoverage = useAlphaToCoverage;
|
---|
304 | }
|
---|
305 |
|
---|
306 |
|
---|
307 | void RenderState::SetRenderTechnique(int t)
|
---|
308 | {
|
---|
309 | mRenderTechnique = t;
|
---|
310 | }
|
---|
311 |
|
---|
312 |
|
---|
313 | int RenderState::GetRenderTechnique() const
|
---|
314 | {
|
---|
315 | return mRenderTechnique;
|
---|
316 | }
|
---|
317 |
|
---|
318 |
|
---|
319 | void RenderState::LockCullFaceEnabled(bool lockCull)
|
---|
320 | {
|
---|
321 | mLockCullFaceEnabled = lockCull;
|
---|
322 | }
|
---|
323 |
|
---|
324 |
|
---|
325 |
|
---|
326 | } |
---|