[2769] | 1 | #include "RenderState.h"
|
---|
| 2 | #include "Geometry.h"
|
---|
| 3 | #include "Material.h"
|
---|
[3028] | 4 | #include "ShaderProgram.h"
|
---|
| 5 | #include "Texture.h"
|
---|
[3057] | 6 | #include "ShaderManager.h"
|
---|
[2769] | 7 |
|
---|
[3038] | 8 |
|
---|
[2861] | 9 | using namespace std;
|
---|
[2769] | 10 |
|
---|
[3038] | 11 |
|
---|
[2776] | 12 | namespace CHCDemoEngine
|
---|
[2769] | 13 | {
|
---|
| 14 |
|
---|
| 15 |
|
---|
[2825] | 16 | /////////////////
|
---|
| 17 |
|
---|
[2769] | 18 | RenderState::RenderState():
|
---|
[3068] | 19 | mRenderTechnique(0),
|
---|
[2955] | 20 | mUseAlphaToCoverage(true),
|
---|
[3050] | 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)
|
---|
[2769] | 33 | {
|
---|
[3038] | 34 | SetMode(RENDER);
|
---|
[3050] | 35 |
|
---|
[3038] | 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);
|
---|
[3050] | 41 | glEnable(GL_LIGHTING);
|
---|
| 42 | glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
|
---|
| 43 | glDepthMask(GL_TRUE);
|
---|
[2769] | 44 | }
|
---|
| 45 |
|
---|
[3038] | 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 |
|
---|
[3057] | 69 | ShaderManager::GetSingleton()->DisableFragmentProfile();
|
---|
| 70 | ShaderManager::GetSingleton()->DisableVertexProfile();
|
---|
[3043] | 71 |
|
---|
| 72 | mFragmentProgramEnabled = false;
|
---|
| 73 | mVertexProgramEnabled = false;
|
---|
[3050] | 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;
|
---|
[3038] | 85 | }
|
---|
| 86 |
|
---|
[3050] | 87 |
|
---|
[3065] | 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 |
|
---|
[3031] | 118 | bool RenderState::SetMode(Mode mode)
|
---|
[2769] | 119 | {
|
---|
[2800] | 120 | ///////////
|
---|
[3050] | 121 | //-- just change the mode when necessary
|
---|
[2800] | 122 |
|
---|
[2769] | 123 | if (mode == mMode) return false;
|
---|
| 124 |
|
---|
| 125 | mMode = mode;
|
---|
| 126 |
|
---|
| 127 | if (mode == QUERY)
|
---|
| 128 | {
|
---|
[3048] | 129 | /// the query box material
|
---|
[3050] | 130 | static Technique queryTech = GetQueryTechnique();
|
---|
[3066] | 131 | SetState(&queryTech);
|
---|
[2769] | 132 | }
|
---|
| 133 |
|
---|
| 134 | return true;
|
---|
| 135 | }
|
---|
| 136 |
|
---|
| 137 |
|
---|
[3114] | 138 | void RenderState::SetState(Technique *tech, SceneEntity *ent)
|
---|
[2769] | 139 | {
|
---|
[3114] | 140 | //if (mCurrentTechnique == tech) return;
|
---|
[3034] | 141 |
|
---|
[3050] | 142 | mCurrentTechnique = tech;
|
---|
| 143 |
|
---|
| 144 | Texture * const tex = tech->GetTexture();
|
---|
| 145 |
|
---|
[3031] | 146 | const bool texturing = (tex != NULL);
|
---|
[3050] | 147 | const bool alphaTest = tech->IsAlphaTestEnabled();
|
---|
| 148 | const bool cullFace = tech->IsCullFaceEnabled();
|
---|
| 149 | const bool lighting = tech->IsLightingEnabled();
|
---|
| 150 | const bool colorWrite = tech->IsColorWriteEnabled();
|
---|
| 151 | const bool depthWrite = tech->IsDepthWriteEnabled();
|
---|
[3031] | 152 |
|
---|
[3114] | 153 |
|
---|
[3050] | 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 | }
|
---|
[3034] | 164 |
|
---|
[3050] | 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 |
|
---|
[2955] | 187 | if (!mLockCullFaceEnabled)
|
---|
[2844] | 188 | {
|
---|
[2955] | 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 | }
|
---|
[2844] | 199 | }
|
---|
[2769] | 200 | if (mAlphaTestEnabled && !alphaTest)
|
---|
| 201 | {
|
---|
| 202 | mAlphaTestEnabled = false;
|
---|
[2851] | 203 |
|
---|
[2950] | 204 | if (mUseAlphaToCoverage)
|
---|
[2943] | 205 | glDisable(GL_SAMPLE_ALPHA_TO_COVERAGE_ARB);
|
---|
[2950] | 206 | else
|
---|
[2943] | 207 | glDisable(GL_ALPHA_TEST);
|
---|
[2769] | 208 | }
|
---|
| 209 | else if (!mAlphaTestEnabled && alphaTest)
|
---|
| 210 | {
|
---|
| 211 | mAlphaTestEnabled = true;
|
---|
[2851] | 212 |
|
---|
[2950] | 213 | if (mUseAlphaToCoverage)
|
---|
[2943] | 214 | glEnable(GL_SAMPLE_ALPHA_TO_COVERAGE_ARB);
|
---|
| 215 | else
|
---|
| 216 | glEnable(GL_ALPHA_TEST);
|
---|
[2769] | 217 | }
|
---|
[2801] | 218 |
|
---|
[3048] | 219 | if (mTexturesEnabled && !texturing)
|
---|
[2801] | 220 | {
|
---|
| 221 | mTexturesEnabled = false;
|
---|
[2819] | 222 |
|
---|
[3050] | 223 | //if (!tech->GetFragmentProgram())
|
---|
[3028] | 224 | glDisable(GL_TEXTURE_2D);
|
---|
[2819] | 225 |
|
---|
[2801] | 226 | glDisableClientState(GL_TEXTURE_COORD_ARRAY);
|
---|
| 227 | }
|
---|
| 228 | else if (!mTexturesEnabled && texturing)
|
---|
| 229 | {
|
---|
[3050] | 230 | mTexturesEnabled = true;
|
---|
[2819] | 231 |
|
---|
[3050] | 232 | //if (!tech->GetFragmentProgram())
|
---|
| 233 | glEnable(GL_TEXTURE_2D);
|
---|
[2819] | 234 |
|
---|
[3050] | 235 | glEnableClientState(GL_TEXTURE_COORD_ARRAY);
|
---|
[2801] | 236 | }
|
---|
[3031] | 237 |
|
---|
[3050] | 238 |
|
---|
| 239 | //////////////////
|
---|
[3043] | 240 | //-- fragment and vertex programs
|
---|
| 241 |
|
---|
| 242 | ShaderProgram *frag = tech->GetFragmentProgram();
|
---|
| 243 |
|
---|
| 244 | if (frag)
|
---|
[3034] | 245 | {
|
---|
[3043] | 246 | if (!mFragmentProgramEnabled)
|
---|
[3034] | 247 | {
|
---|
[3043] | 248 | mFragmentProgramEnabled = true;
|
---|
[3057] | 249 | ShaderManager::GetSingleton()->EnableFragmentProfile();
|
---|
[3043] | 250 | }
|
---|
[3038] | 251 |
|
---|
[3043] | 252 | if (frag != mCurrentFragmentProgram)
|
---|
| 253 | {
|
---|
| 254 | mCurrentFragmentProgram = frag;
|
---|
| 255 | mCurrentFragmentProgram->Bind();
|
---|
[3034] | 256 | }
|
---|
[3043] | 257 |
|
---|
[3114] | 258 | tech->GetFragmentProgramParameters()->UpdateParameters(ent);
|
---|
[3043] | 259 | }
|
---|
| 260 | else
|
---|
| 261 | {
|
---|
| 262 | if (mFragmentProgramEnabled)
|
---|
| 263 | {
|
---|
| 264 | mFragmentProgramEnabled = false;
|
---|
[3057] | 265 | ShaderManager::GetSingleton()->DisableFragmentProfile();
|
---|
[3043] | 266 | }
|
---|
| 267 | }
|
---|
[3042] | 268 |
|
---|
[3114] | 269 | ShaderProgram *vtx = tech->GetVertexProgram();
|
---|
[3042] | 270 |
|
---|
[3114] | 271 | if (vtx)
|
---|
[3043] | 272 | {
|
---|
| 273 | if (!mVertexProgramEnabled)
|
---|
[3034] | 274 | {
|
---|
[3043] | 275 | mVertexProgramEnabled = true;
|
---|
[3057] | 276 | ShaderManager::GetSingleton()->EnableVertexProfile();
|
---|
[3043] | 277 | }
|
---|
| 278 |
|
---|
[3114] | 279 | if (vtx != mCurrentVertexProgram)
|
---|
[3043] | 280 | {
|
---|
[3114] | 281 | mCurrentVertexProgram = vtx;
|
---|
[3043] | 282 | mCurrentVertexProgram->Bind();
|
---|
| 283 | }
|
---|
[3039] | 284 |
|
---|
[3114] | 285 | tech->GetVertexProgramParameters()->UpdateParameters(ent);
|
---|
[3034] | 286 | }
|
---|
| 287 | else
|
---|
| 288 | {
|
---|
[3043] | 289 | if (mVertexProgramEnabled)
|
---|
| 290 | {
|
---|
| 291 | mVertexProgramEnabled = false;
|
---|
[3057] | 292 | ShaderManager::GetSingleton()->DisableVertexProfile();
|
---|
[3043] | 293 | }
|
---|
| 294 | }
|
---|
| 295 |
|
---|
| 296 | if (!mFragmentProgramEnabled)
|
---|
| 297 | {
|
---|
[3050] | 298 | // set texture for fixed function rendering
|
---|
[3034] | 299 | if (tex)
|
---|
| 300 | {
|
---|
| 301 | if (mCurrentTexId != tex->GetId())
|
---|
| 302 | {
|
---|
| 303 | tex->Bind();
|
---|
| 304 | mCurrentTexId = tex->GetId();
|
---|
| 305 | }
|
---|
| 306 | }
|
---|
| 307 | else
|
---|
| 308 | {
|
---|
[3050] | 309 | mCurrentTexId = -1;
|
---|
[3034] | 310 | glBindTexture(GL_TEXTURE_2D, 0);
|
---|
| 311 | }
|
---|
| 312 | }
|
---|
[2769] | 313 | }
|
---|
| 314 |
|
---|
| 315 |
|
---|
[2943] | 316 | void RenderState::SetUseAlphaToCoverage(bool useAlphaToCoverage)
|
---|
| 317 | {
|
---|
| 318 | mUseAlphaToCoverage = useAlphaToCoverage;
|
---|
| 319 | }
|
---|
| 320 |
|
---|
| 321 |
|
---|
[3068] | 322 | void RenderState::SetRenderTechnique(int t)
|
---|
[2955] | 323 | {
|
---|
[3048] | 324 | mRenderTechnique = t;
|
---|
[2955] | 325 | }
|
---|
| 326 |
|
---|
| 327 |
|
---|
[3068] | 328 | int RenderState::GetRenderTechnique() const
|
---|
[2955] | 329 | {
|
---|
[3048] | 330 | return mRenderTechnique;
|
---|
[2955] | 331 | }
|
---|
| 332 |
|
---|
| 333 |
|
---|
| 334 | void RenderState::LockCullFaceEnabled(bool lockCull)
|
---|
| 335 | {
|
---|
| 336 | mLockCullFaceEnabled = lockCull;
|
---|
| 337 | }
|
---|
| 338 |
|
---|
| 339 |
|
---|
[3074] | 340 |
|
---|
[2769] | 341 | } |
---|