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