[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 | /** 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 |
|
---|
[3031] | 104 | bool RenderState::SetMode(Mode mode)
|
---|
[2769] | 105 | {
|
---|
[2800] | 106 | ///////////
|
---|
[3050] | 107 | //-- just change the mode when necessary
|
---|
[2800] | 108 |
|
---|
[2769] | 109 | if (mode == mMode) return false;
|
---|
| 110 |
|
---|
| 111 | mMode = mode;
|
---|
| 112 |
|
---|
| 113 | if (mode == QUERY)
|
---|
| 114 | {
|
---|
[3048] | 115 | /// the query box material
|
---|
[3050] | 116 | static Technique queryTech = GetQueryTechnique();
|
---|
[3066] | 117 | SetState(&queryTech);
|
---|
[3127] | 118 |
|
---|
| 119 | glDisableClientState(GL_NORMAL_ARRAY);
|
---|
[2769] | 120 | }
|
---|
[3127] | 121 | else
|
---|
| 122 | {
|
---|
| 123 | glEnableClientState(GL_NORMAL_ARRAY);
|
---|
| 124 | }
|
---|
[2769] | 125 |
|
---|
| 126 | return true;
|
---|
| 127 | }
|
---|
| 128 |
|
---|
| 129 |
|
---|
[3114] | 130 | void RenderState::SetState(Technique *tech, SceneEntity *ent)
|
---|
[2769] | 131 | {
|
---|
[3114] | 132 | //if (mCurrentTechnique == tech) return;
|
---|
[3050] | 133 | mCurrentTechnique = tech;
|
---|
| 134 |
|
---|
| 135 | Texture * const tex = tech->GetTexture();
|
---|
| 136 |
|
---|
[3031] | 137 | const bool texturing = (tex != NULL);
|
---|
[3050] | 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();
|
---|
[3031] | 143 |
|
---|
[3114] | 144 |
|
---|
[3050] | 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 | }
|
---|
[3034] | 155 |
|
---|
[3050] | 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 |
|
---|
[2955] | 178 | if (!mLockCullFaceEnabled)
|
---|
[2844] | 179 | {
|
---|
[2955] | 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 | }
|
---|
[2844] | 190 | }
|
---|
[2769] | 191 | if (mAlphaTestEnabled && !alphaTest)
|
---|
| 192 | {
|
---|
| 193 | mAlphaTestEnabled = false;
|
---|
[2851] | 194 |
|
---|
[2950] | 195 | if (mUseAlphaToCoverage)
|
---|
[2943] | 196 | glDisable(GL_SAMPLE_ALPHA_TO_COVERAGE_ARB);
|
---|
[2950] | 197 | else
|
---|
[2943] | 198 | glDisable(GL_ALPHA_TEST);
|
---|
[2769] | 199 | }
|
---|
| 200 | else if (!mAlphaTestEnabled && alphaTest)
|
---|
| 201 | {
|
---|
| 202 | mAlphaTestEnabled = true;
|
---|
[2851] | 203 |
|
---|
[2950] | 204 | if (mUseAlphaToCoverage)
|
---|
[2943] | 205 | glEnable(GL_SAMPLE_ALPHA_TO_COVERAGE_ARB);
|
---|
| 206 | else
|
---|
| 207 | glEnable(GL_ALPHA_TEST);
|
---|
[2769] | 208 | }
|
---|
[2801] | 209 |
|
---|
[3048] | 210 | if (mTexturesEnabled && !texturing)
|
---|
[2801] | 211 | {
|
---|
| 212 | mTexturesEnabled = false;
|
---|
[3127] | 213 | glDisable(GL_TEXTURE_2D);
|
---|
[2819] | 214 |
|
---|
[2801] | 215 | glDisableClientState(GL_TEXTURE_COORD_ARRAY);
|
---|
| 216 | }
|
---|
| 217 | else if (!mTexturesEnabled && texturing)
|
---|
| 218 | {
|
---|
[3050] | 219 | mTexturesEnabled = true;
|
---|
[2819] | 220 |
|
---|
[3127] | 221 | glEnable(GL_TEXTURE_2D);
|
---|
[3050] | 222 | glEnableClientState(GL_TEXTURE_COORD_ARRAY);
|
---|
[2801] | 223 | }
|
---|
[3031] | 224 |
|
---|
[3050] | 225 |
|
---|
[3126] | 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 |
|
---|
[3050] | 242 | //////////////////
|
---|
[3043] | 243 | //-- fragment and vertex programs
|
---|
| 244 |
|
---|
| 245 | ShaderProgram *frag = tech->GetFragmentProgram();
|
---|
| 246 |
|
---|
| 247 | if (frag)
|
---|
[3034] | 248 | {
|
---|
[3043] | 249 | if (!mFragmentProgramEnabled)
|
---|
[3034] | 250 | {
|
---|
[3043] | 251 | mFragmentProgramEnabled = true;
|
---|
[3057] | 252 | ShaderManager::GetSingleton()->EnableFragmentProfile();
|
---|
[3043] | 253 | }
|
---|
[3038] | 254 |
|
---|
[3153] | 255 | if (frag != mCurrentFragmentProgram)
|
---|
[3043] | 256 | {
|
---|
| 257 | mCurrentFragmentProgram = frag;
|
---|
| 258 | mCurrentFragmentProgram->Bind();
|
---|
[3034] | 259 | }
|
---|
[3043] | 260 |
|
---|
[3114] | 261 | tech->GetFragmentProgramParameters()->UpdateParameters(ent);
|
---|
[3043] | 262 | }
|
---|
| 263 | else
|
---|
| 264 | {
|
---|
| 265 | if (mFragmentProgramEnabled)
|
---|
| 266 | {
|
---|
| 267 | mFragmentProgramEnabled = false;
|
---|
[3057] | 268 | ShaderManager::GetSingleton()->DisableFragmentProfile();
|
---|
[3043] | 269 | }
|
---|
| 270 | }
|
---|
[3042] | 271 |
|
---|
[3114] | 272 | ShaderProgram *vtx = tech->GetVertexProgram();
|
---|
[3042] | 273 |
|
---|
[3114] | 274 | if (vtx)
|
---|
[3043] | 275 | {
|
---|
| 276 | if (!mVertexProgramEnabled)
|
---|
[3034] | 277 | {
|
---|
[3043] | 278 | mVertexProgramEnabled = true;
|
---|
[3057] | 279 | ShaderManager::GetSingleton()->EnableVertexProfile();
|
---|
[3043] | 280 | }
|
---|
| 281 |
|
---|
[3114] | 282 | if (vtx != mCurrentVertexProgram)
|
---|
[3043] | 283 | {
|
---|
[3114] | 284 | mCurrentVertexProgram = vtx;
|
---|
[3043] | 285 | mCurrentVertexProgram->Bind();
|
---|
| 286 | }
|
---|
[3039] | 287 |
|
---|
[3114] | 288 | tech->GetVertexProgramParameters()->UpdateParameters(ent);
|
---|
[3034] | 289 | }
|
---|
| 290 | else
|
---|
| 291 | {
|
---|
[3043] | 292 | if (mVertexProgramEnabled)
|
---|
| 293 | {
|
---|
| 294 | mVertexProgramEnabled = false;
|
---|
[3057] | 295 | ShaderManager::GetSingleton()->DisableVertexProfile();
|
---|
[3043] | 296 | }
|
---|
| 297 | }
|
---|
[2769] | 298 | }
|
---|
| 299 |
|
---|
| 300 |
|
---|
[2943] | 301 | void RenderState::SetUseAlphaToCoverage(bool useAlphaToCoverage)
|
---|
| 302 | {
|
---|
| 303 | mUseAlphaToCoverage = useAlphaToCoverage;
|
---|
| 304 | }
|
---|
| 305 |
|
---|
| 306 |
|
---|
[3068] | 307 | void RenderState::SetRenderTechnique(int t)
|
---|
[2955] | 308 | {
|
---|
[3048] | 309 | mRenderTechnique = t;
|
---|
[2955] | 310 | }
|
---|
| 311 |
|
---|
| 312 |
|
---|
[3068] | 313 | int RenderState::GetRenderTechnique() const
|
---|
[2955] | 314 | {
|
---|
[3048] | 315 | return mRenderTechnique;
|
---|
[2955] | 316 | }
|
---|
| 317 |
|
---|
| 318 |
|
---|
| 319 | void RenderState::LockCullFaceEnabled(bool lockCull)
|
---|
| 320 | {
|
---|
| 321 | mLockCullFaceEnabled = lockCull;
|
---|
| 322 | }
|
---|
| 323 |
|
---|
| 324 |
|
---|
[3074] | 325 |
|
---|
[2769] | 326 | } |
---|