00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <GLEXT/GLState.h>
00022 #include <common/DefinesString.h>
00023 #include <common/DefinesAssert.h>
00024 #include <string.h>
00025 #include <string>
00026
00027 unsigned GLState::currentState_ =
00028 GLState::TEXTURE_OFF | GLState::BLEND_OFF |
00029 GLState::DEPTH_OFF | GLState::CUBEMAP_OFF |
00030 GLState::LIGHTING_OFF | GLState::NORMALIZE_OFF |
00031 GLState::LIGHT1_OFF | GLState::ALPHATEST_OFF;
00032 unsigned int GLState::stateSwitches_ = 0;
00033
00034 GLState::GLState(unsigned wantedState)
00035 {
00036 returnState_ = currentState_;
00037
00038 if (wantedState == 0) return;
00039 setState(wantedState);
00040 }
00041
00042 GLState::~GLState()
00043 {
00044 setState(returnState_);
00045 }
00046
00047 void GLState::setState(unsigned wanted)
00048 {
00049 if (wanted == currentState_) return;
00050
00051 GL_ASSERT();
00052
00053 if ((wanted & ALPHATEST_ON) && (currentState_ & ALPHATEST_OFF))
00054 {
00055 currentState_ ^= ALPHATEST_OFF;
00056 currentState_ |= ALPHATEST_ON;
00057 glEnable(GL_ALPHA_TEST);
00058 stateSwitches_++;
00059 }
00060 else if ((wanted & ALPHATEST_OFF) && (currentState_ & ALPHATEST_ON))
00061 {
00062 currentState_ ^= ALPHATEST_ON;
00063 currentState_ |= ALPHATEST_OFF;
00064 glDisable(GL_ALPHA_TEST);
00065 stateSwitches_++;
00066 }
00067
00068 if ((wanted & LIGHT1_ON) && (currentState_ & LIGHT1_OFF))
00069 {
00070 currentState_ ^= LIGHT1_OFF;
00071 currentState_ |= LIGHT1_ON;
00072 glEnable(GL_LIGHT1);
00073 stateSwitches_++;
00074 }
00075 else if ((wanted & LIGHT1_OFF) && (currentState_ & LIGHT1_ON))
00076 {
00077 currentState_ ^= LIGHT1_ON;
00078 currentState_ |= LIGHT1_OFF;
00079 glDisable(GL_LIGHT1);
00080 stateSwitches_++;
00081 }
00082
00083 if ((wanted & LIGHTING_ON) && (currentState_ & LIGHTING_OFF))
00084 {
00085 currentState_ ^= LIGHTING_OFF;
00086 currentState_ |= LIGHTING_ON;
00087 glEnable(GL_LIGHTING);
00088 stateSwitches_++;
00089 }
00090 else if ((wanted & LIGHTING_OFF) && (currentState_ & LIGHTING_ON))
00091 {
00092 currentState_ ^= LIGHTING_ON;
00093 currentState_ |= LIGHTING_OFF;
00094 glDisable(GL_LIGHTING);
00095 stateSwitches_++;
00096 }
00097
00098 if ((wanted & NORMALIZE_ON) && (currentState_ & NORMALIZE_OFF))
00099 {
00100 currentState_ ^= NORMALIZE_OFF;
00101 currentState_ |= NORMALIZE_ON;
00102 glEnable(GL_NORMALIZE);
00103 stateSwitches_++;
00104 }
00105 else if ((wanted & NORMALIZE_OFF) && (currentState_ & NORMALIZE_ON))
00106 {
00107 currentState_ ^= NORMALIZE_ON;
00108 currentState_ |= NORMALIZE_OFF;
00109 glDisable(GL_NORMALIZE);
00110 stateSwitches_++;
00111 }
00112
00113 if ((wanted & TEXTURE_ON) && (currentState_ & TEXTURE_OFF))
00114 {
00115 currentState_ ^= TEXTURE_OFF;
00116 currentState_ |= TEXTURE_ON;
00117 glEnable(GL_TEXTURE_2D);
00118 stateSwitches_++;
00119 }
00120 else if ((wanted & TEXTURE_OFF) && (currentState_ & TEXTURE_ON))
00121 {
00122 currentState_ ^= TEXTURE_ON;
00123 currentState_ |= TEXTURE_OFF;
00124 glDisable(GL_TEXTURE_2D);
00125 stateSwitches_++;
00126 }
00127
00128 if ((wanted & DEPTH_ON) && (currentState_ & DEPTH_OFF))
00129 {
00130 currentState_ ^= DEPTH_OFF;
00131 currentState_ |= DEPTH_ON;
00132 glEnable(GL_DEPTH_TEST);
00133 stateSwitches_++;
00134 }
00135 else if ((wanted & DEPTH_OFF) && (currentState_ & DEPTH_ON))
00136 {
00137 currentState_ ^= DEPTH_ON;
00138 currentState_ |= DEPTH_OFF;
00139 glDisable(GL_DEPTH_TEST);
00140 stateSwitches_++;
00141 }
00142
00143 if ((wanted & BLEND_ON) && (currentState_ & BLEND_OFF))
00144 {
00145 currentState_ ^= BLEND_OFF;
00146 currentState_ |= BLEND_ON;
00147 glEnable(GL_BLEND);
00148 stateSwitches_++;
00149 }
00150 else if ((wanted & BLEND_OFF) && (currentState_ & BLEND_ON))
00151 {
00152 currentState_ ^= BLEND_ON;
00153 currentState_ |= BLEND_OFF;
00154 glDisable(GL_BLEND);
00155 stateSwitches_++;
00156 }
00157
00158 if ((wanted & CUBEMAP_ON) && (currentState_ & CUBEMAP_OFF))
00159 {
00160 currentState_ ^= CUBEMAP_OFF;
00161 currentState_ |= CUBEMAP_ON;
00162 glEnable(GL_TEXTURE_CUBE_MAP_EXT);
00163 stateSwitches_++;
00164 }
00165 else if ((wanted & CUBEMAP_OFF) && (currentState_ & CUBEMAP_ON))
00166 {
00167 currentState_ ^= CUBEMAP_ON;
00168 currentState_ |= CUBEMAP_OFF;
00169 glDisable(GL_TEXTURE_CUBE_MAP_EXT);
00170 stateSwitches_++;
00171 }
00172
00173 GL_ASSERT();
00174 }
00175
00176 static void addToBuffer(std::string &buffer, unsigned int value, const char *name)
00177 {
00178 if (GLState::getState() & value) buffer.append(name).append("\n");
00179 }
00180
00181 const char *GLState::getStateString()
00182 {
00183 static std::string buffer;
00184 buffer = "";
00185 addToBuffer(buffer, NORMALIZE_ON, "NORMALIZE_ON");
00186 addToBuffer(buffer, NORMALIZE_OFF, "NORMALIZE_OFF");
00187 addToBuffer(buffer, LIGHTING_ON, "LIGHTING_ON");
00188 addToBuffer(buffer, LIGHTING_OFF, "LIGHTING_OFF");
00189 addToBuffer(buffer, LIGHT1_ON, "LIGHT1_ON");
00190 addToBuffer(buffer, LIGHT1_OFF, "LIGHT1_OFF");
00191 addToBuffer(buffer, TEXTURE_ON, "TEXTURE_ON");
00192 addToBuffer(buffer, TEXTURE_OFF, "TEXTURE_OFF");
00193 addToBuffer(buffer, DEPTH_ON, "DEPTH_ON");
00194 addToBuffer(buffer, DEPTH_OFF, "DEPTH_OFF");
00195 addToBuffer(buffer, BLEND_ON, "BLEND_ON");
00196 addToBuffer(buffer, BLEND_OFF, "BLEND_OFF");
00197 addToBuffer(buffer, CUBEMAP_ON, "CUBEMAP_ON");
00198 addToBuffer(buffer, CUBEMAP_OFF, "CUBEMAP_OFF");
00199
00200 return buffer.c_str();
00201 }
00202
00203 void GLState::setBaseState(unsigned bs)
00204 {
00205 setState(bs);
00206 }