[2769] | 1 | #include "RenderState.h"
|
---|
| 2 | #include "Geometry.h"
|
---|
| 3 | #include "Material.h"
|
---|
| 4 |
|
---|
[2861] | 5 | using namespace std;
|
---|
[2769] | 6 |
|
---|
[2776] | 7 | namespace CHCDemoEngine
|
---|
[2769] | 8 | {
|
---|
| 9 |
|
---|
[2818] | 10 | CGprofile RenderState::sCgFragmentProfile;
|
---|
| 11 | CGprofile RenderState::sCgVertexProfile;
|
---|
[2769] | 12 |
|
---|
[2819] | 13 | CGprogram RenderState::sCgMrtFragmentProgram = NULL;
|
---|
| 14 | CGprogram RenderState::sCgMrtFragmentTexProgram = NULL;
|
---|
[2818] | 15 |
|
---|
[2819] | 16 |
|
---|
[2825] | 17 | /////////////////
|
---|
| 18 |
|
---|
[2769] | 19 | RenderState::RenderState():
|
---|
[2773] | 20 | mAlphaTestEnabled(false),
|
---|
[2844] | 21 | mCullFaceEnabled(true),
|
---|
[2773] | 22 | mTexturesEnabled(false),
|
---|
[2801] | 23 | mMode(RENDER),
|
---|
[2825] | 24 | mRenderType(FIXED)
|
---|
[2769] | 25 | {
|
---|
| 26 | Reset();
|
---|
| 27 | }
|
---|
| 28 |
|
---|
| 29 |
|
---|
| 30 | bool RenderState::SetState(Mode mode)
|
---|
| 31 | {
|
---|
[2800] | 32 | ///////////
|
---|
[2769] | 33 | //-- just change the modewhen necessary
|
---|
[2800] | 34 |
|
---|
[2769] | 35 | if (mode == mMode) return false;
|
---|
| 36 |
|
---|
| 37 | mMode = mode;
|
---|
| 38 |
|
---|
| 39 | if (mode == QUERY)
|
---|
| 40 | {
|
---|
[2825] | 41 | if (mRenderType != DEPTH_PASS)
|
---|
[2801] | 42 | {
|
---|
| 43 | glDisable(GL_LIGHTING);
|
---|
| 44 | glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
|
---|
[2818] | 45 |
|
---|
[2825] | 46 | if (mRenderType == DEFERRED)
|
---|
| 47 | {
|
---|
| 48 | cgGLDisableProfile(sCgFragmentProfile);
|
---|
| 49 | cgGLDisableProfile(sCgVertexProfile);
|
---|
| 50 | }
|
---|
[2801] | 51 | }
|
---|
| 52 |
|
---|
[2769] | 53 | glDepthMask(GL_FALSE);
|
---|
| 54 |
|
---|
[2844] | 55 | SetState(false, false, false);
|
---|
[2769] | 56 | }
|
---|
| 57 | else // mode returns to render
|
---|
| 58 | {
|
---|
| 59 | /////////////
|
---|
| 60 | //-- restore render state
|
---|
| 61 |
|
---|
[2825] | 62 | if (mRenderType != DEPTH_PASS)
|
---|
[2801] | 63 | {
|
---|
| 64 | glEnable(GL_LIGHTING);
|
---|
| 65 | glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
|
---|
[2818] | 66 |
|
---|
[2825] | 67 | if (mRenderType == DEFERRED)
|
---|
| 68 | {
|
---|
| 69 | cgGLEnableProfile(sCgFragmentProfile);
|
---|
| 70 | cgGLEnableProfile(sCgVertexProfile);
|
---|
| 71 | }
|
---|
[2801] | 72 | }
|
---|
| 73 |
|
---|
[2769] | 74 | glDepthMask(GL_TRUE);
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | return true;
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 |
|
---|
[2844] | 81 | void RenderState::SetState(bool texturing, bool alphaTest, bool cullFace)
|
---|
[2769] | 82 | {
|
---|
[2844] | 83 | if (mCullFaceEnabled && !cullFace)
|
---|
| 84 | {
|
---|
| 85 | mCullFaceEnabled = false;
|
---|
| 86 | glDisable(GL_CULL_FACE);
|
---|
| 87 | }
|
---|
| 88 | else if (!mCullFaceEnabled && cullFace)
|
---|
| 89 | {
|
---|
| 90 | mCullFaceEnabled = true;
|
---|
[2851] | 91 | glEnable(GL_CULL_FACE);
|
---|
[2844] | 92 | }
|
---|
| 93 |
|
---|
[2769] | 94 | if (mAlphaTestEnabled && !alphaTest)
|
---|
| 95 | {
|
---|
| 96 | mAlphaTestEnabled = false;
|
---|
[2851] | 97 |
|
---|
| 98 | // not needed with GL_SAMPLE_ALPHA_TO_COVERAGE_ARB
|
---|
| 99 | //glDisable(GL_ALPHA_TEST);
|
---|
| 100 | glDisable(GL_SAMPLE_ALPHA_TO_COVERAGE_ARB);
|
---|
[2769] | 101 | }
|
---|
| 102 | else if (!mAlphaTestEnabled && alphaTest)
|
---|
| 103 | {
|
---|
| 104 | mAlphaTestEnabled = true;
|
---|
[2851] | 105 |
|
---|
| 106 | // not needed with GL_SAMPLE_ALPHA_TO_COVERAGE_ARB
|
---|
| 107 | //glEnable(GL_ALPHA_TEST);
|
---|
| 108 | glEnable(GL_SAMPLE_ALPHA_TO_COVERAGE_ARB);
|
---|
[2769] | 109 | }
|
---|
[2801] | 110 |
|
---|
| 111 | if (mTexturesEnabled &&
|
---|
[2825] | 112 | (!texturing || (mRenderType == DEPTH_PASS) && !mAlphaTestEnabled))
|
---|
[2801] | 113 | {
|
---|
| 114 | mTexturesEnabled = false;
|
---|
[2819] | 115 |
|
---|
[2825] | 116 | if (mRenderType == DEFERRED)
|
---|
| 117 | cgGLBindProgram(sCgMrtFragmentProgram);
|
---|
[2819] | 118 |
|
---|
[2801] | 119 | glDisable(GL_TEXTURE_2D);
|
---|
| 120 | glDisableClientState(GL_TEXTURE_COORD_ARRAY);
|
---|
| 121 | }
|
---|
| 122 | else if (!mTexturesEnabled && texturing)
|
---|
| 123 | {
|
---|
[2825] | 124 | // support only alpha textures in depth pass
|
---|
| 125 | if ((mRenderType != DEPTH_PASS) || mAlphaTestEnabled)
|
---|
[2801] | 126 | {
|
---|
| 127 | mTexturesEnabled = true;
|
---|
[2819] | 128 |
|
---|
[2825] | 129 | if (mRenderType == DEFERRED)
|
---|
| 130 | cgGLBindProgram(sCgMrtFragmentTexProgram);
|
---|
[2819] | 131 |
|
---|
[2801] | 132 | glEnable(GL_TEXTURE_2D);
|
---|
| 133 | glEnableClientState(GL_TEXTURE_COORD_ARRAY);
|
---|
| 134 | }
|
---|
| 135 | }
|
---|
[2769] | 136 | }
|
---|
| 137 |
|
---|
| 138 |
|
---|
| 139 | void RenderState::Reset()
|
---|
| 140 | {
|
---|
[2773] | 141 | mCurrentVboId = -1;
|
---|
[2825] | 142 |
|
---|
[2845] | 143 | SetState(false, false, true);
|
---|
[2769] | 144 | SetState(RENDER);
|
---|
| 145 | }
|
---|
| 146 |
|
---|
| 147 |
|
---|
| 148 | } |
---|