GLState.h
Go to the documentation of this file.00001 /**<!--------------------------------------------------------------------> 00002 @class GLState 00003 @author Travis Fischer (fisch0920@gmail.com) 00004 @date Summer 2008 00005 00006 @brief 00007 Utility wrapper classes for accessing and synchronizing OpenGL state 00008 in a cleaner, more object-oriented manner, grouping similar state items 00009 with each other. 00010 00011 @note 00012 Where applicable, acceptable parameters are listed along with default 00013 values marked between '<' and '>'. For example, the frontFace property of 00014 GLPrimitiveStateItem is preceded by the following comment: 00015 /// glFrontFace; GL_CW or <GL_CCW> 00016 Where GL_CCW is the OpenGL-defined default value for this property. 00017 <!-------------------------------------------------------------------->**/ 00018 00019 #ifndef GL_STATE_H_ 00020 #define GL_STATE_H_ 00021 00022 #include <utils/IFeatures.h> 00023 #include <common/common.h> 00024 00025 #include <GL/gl.h> 00026 #include <map> 00027 00028 #define GL_CHECK_ERROR() GLState::glCheckError(__FILE__, __LINE__) 00029 00030 union GLcolor { 00031 GLclampf data[4]; 00032 00033 struct { 00034 GLclampf r; 00035 GLclampf g; 00036 GLclampf b; 00037 GLclampf a; 00038 }; 00039 }; 00040 00041 class GLState { 00042 public: 00043 /** 00044 * @returns the next lowest light index which is not currently enabled, 00045 * or -1 if all light slots within the current GL context are taken 00046 */ 00047 static int getFreeLight(); 00048 00049 static bool glCheckError(const char *file, int line); 00050 00051 protected: 00052 #if 0 00053 GLBlendStateItem m_blendState; 00054 GLDepthStateItem m_depthState; 00055 GLStencilStateItem m_stencilState; 00056 GLPrimitiveStateItem m_primitiveState; 00057 GLAlphaStateItem m_alphaState; 00058 GLColorStateItem m_colorState; 00059 GLFogStateItem m_fogState; 00060 GLPointStateItem m_pointState; 00061 #endif 00062 }; 00063 00064 class GLStateItem : public IFeatures { 00065 public: 00066 virtual ~GLStateItem() 00067 { } 00068 00069 //template <typename T> 00070 //T get(const std::string &name) = 0; 00071 00072 virtual bool sync() = 0; 00073 virtual bool apply() = 0; 00074 }; 00075 00076 class GLBlendStateItem : public GLStateItem { 00077 public: 00078 virtual ~GLBlendStateItem() 00079 { } 00080 00081 virtual bool sync(); 00082 virtual bool apply(); 00083 00084 ///@name State variables 00085 //@{----------------------------------------------------------------- 00086 00087 /// glEnable; GL_TRUE or <GL_FALSE> 00088 GLboolean enable; 00089 00090 /// glBlendFunc; GL_ZERO, <GL_ONE>, 00091 /// GL_SRC_COLOR, GL_ONE_MINUS_SRC_COLOR, 00092 /// GL_DST_COLOR, GL_ONE_MINUS_DST_COLOR, 00093 /// GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, 00094 /// GL_DST_ALPHA, GL_ONE_MINUS_DST_ALPHA, 00095 /// GL_CONSTANT_COLOR, GL_ONE_MINUS_CONSTANT_COLOR, 00096 /// GL_CONSTANT_ALPHA, GL_ONE_MINUS_CONSTANT_ALPHA, 00097 /// GL_SRC_ALPHA_SATURATE 00098 GLenum blendSrc; 00099 00100 /// glBlendFunc; <GL_ZERO>, GL_ONE, 00101 /// GL_SRC_COLOR, GL_ONE_MINUS_SRC_COLOR, 00102 /// GL_DST_COLOR, GL_ONE_MINUS_DST_COLOR, 00103 /// GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, 00104 /// GL_DST_ALPHA, GL_ONE_MINUS_DST_ALPHA, 00105 /// GL_CONSTANT_COLOR, GL_ONE_MINUS_CONSTANT_COLOR, 00106 /// GL_CONSTANT_ALPHA, GL_ONE_MINUS_CONSTANT_ALPHA 00107 GLenum blendDst; 00108 00109 /// glBlendEquation; <GL_FUNC_ADD>, GL_FUNC_SUBTRACT, 00110 /// GL_FUNC_REVERSE_SUBTRACT, GL_MIN, GL_MAX 00111 GLenum blendEquation; 00112 00113 /// glBlendColor; <0, 0, 0, 0> 00114 GLcolor blendColor; 00115 00116 //@}----------------------------------------------------------------- 00117 }; 00118 00119 class GLDepthStateItem : public GLStateItem { 00120 public: 00121 virtual ~GLDepthStateItem() 00122 { } 00123 00124 virtual bool sync(); 00125 virtual bool apply(); 00126 00127 ///@name State variables 00128 //@{----------------------------------------------------------------- 00129 00130 /// glEnable; GL_TRUE or <GL_FALSE> 00131 GLboolean enable; 00132 00133 /// glDepthFunc; GL_NEVER, <GL_LESS>, GL_EQUAL, GL_GREATER, GL_NOTEQUAL, 00134 /// GL_GEQUAL, GL_ALWAYS 00135 GLenum depthFunc; 00136 00137 /// glDepthMask; <GL_TRUE> or GL_FALSE 00138 GLboolean depthMask; 00139 00140 /// glDepthRange; <0, 1> 00141 GLclampf depthRange[2]; 00142 00143 //@}----------------------------------------------------------------- 00144 }; 00145 00146 class GLStencilStateItem : public GLStateItem { 00147 public: 00148 virtual ~GLStencilStateItem() 00149 { } 00150 00151 virtual bool sync(); 00152 virtual bool apply(); 00153 00154 ///@name State variables 00155 //@{----------------------------------------------------------------- 00156 00157 /// glEnable; GL_TRUE or <GL_FALSE> 00158 GLboolean enable; 00159 00160 /// glStencilFunc; GL_NEVER, <GL_LESS>, GL_EQUAL, GL_GREATER, 00161 /// GL_NOTEQUAL, GL_GEQUAL, GL_ALWAYS 00162 GLenum stencilFunc; 00163 /// glStencilFunc; <0> to 2^n-1, where n is # of bitplanes in stencil buf 00164 GLint stencilRef; 00165 /// glStencilFunc; <1> 00166 GLuint stencilMask; 00167 00168 /// glStencilOp; <GL_KEEP>, GL_ZERO, GL_REPLACE, GL_INCR, GL_INCR_WRAP, 00169 /// GL_DECR, GL_DECR_WRAP, GL_INVERT 00170 GLenum stencilFail; 00171 /// glStencilOp; <GL_KEEP>, GL_ZERO, GL_REPLACE, GL_INCR, GL_INCR_WRAP, 00172 /// GL_DECR, GL_DECR_WRAP, GL_INVERT 00173 GLenum stencilPassDepthFail; 00174 /// glStencilOp; <GL_KEEP>, GL_ZERO, GL_REPLACE, GL_INCR, GL_INCR_WRAP, 00175 /// GL_DECR, GL_DECR_WRAP, GL_INVERT 00176 GLenum stencilPassDepthPass; 00177 00178 // TODO: incorporate glStencil{Op,Func,Mask}Separate? 00179 /* 00180 GLenum stencilBackFail; 00181 GLenum stencilBackFunc; 00182 GLenum stencilBackPassDepthFail; 00183 GLenum stencilBackPassDepthPass; 00184 00185 GLuint stencilBackRef; 00186 GLuint stencilBackValueMask; 00187 GLuint stencilBackWriteMask; 00188 00189 GLenum stencilPassDepthPass; 00190 GLuint stencilValueMask; 00191 GLuint stencilWriteMask; 00192 */ 00193 00194 //@}----------------------------------------------------------------- 00195 }; 00196 00197 class GLPrimitiveStateItem : public GLStateItem { 00198 public: 00199 virtual ~GLPrimitiveStateItem() 00200 { } 00201 00202 virtual bool sync(); 00203 virtual bool apply(); 00204 00205 ///@name State variables 00206 //@{----------------------------------------------------------------- 00207 00208 /// glPolygonMode; indexed by GL_POINT, GL_LINE, and GL_FILL 00209 std::map<GLenum, GLenum> polygonMode; 00210 00211 /// glEnable; GL_TRUE or <GL_FALSE> 00212 GLboolean enableCullFace; 00213 /// glCullFace; GL_FRONT, <GL_BACK>, GL_FRONT_AND_BACK 00214 GLenum cullFace; 00215 00216 /// glFrontFace; GL_CW or <GL_CCW> 00217 GLenum frontFace; 00218 00219 /// glEnable; indexed by GL_POLYGON_OFFSET_{FILL|LINE|POINT} 00220 std::map<GLenum, GLboolean> enablePolygonOffset; 00221 00222 /// glPolygonOffset 00223 GLfloat polygonOffsetFactor; 00224 /// glPolygonOffset 00225 GLfloat polygonOffsetUnits; 00226 00227 //@}----------------------------------------------------------------- 00228 }; 00229 00230 class GLAlphaStateItem : public GLStateItem { 00231 public: 00232 virtual ~GLAlphaStateItem() 00233 { } 00234 00235 virtual bool sync(); 00236 virtual bool apply(); 00237 00238 ///@name State variables 00239 //@{----------------------------------------------------------------- 00240 00241 /// glEnable; GL_TRUE or <GL_FALSE> 00242 GLboolean enable; 00243 00244 /// glAlphaFunc; GL_NEVER, GL_LESS, GL_EQUAL, GL_GREATER, GL_NOTEQUAL, 00245 /// GL_GEQUAL, <GL_ALWAYS> 00246 GLenum alphaTestFunc; 00247 /// glAlphaFunc; <0> (lowest alpha) to 1 (highest alpha) 00248 GLfloat alphaTestRef; 00249 00250 //@}----------------------------------------------------------------- 00251 }; 00252 00253 class GLColorStateItem : public GLStateItem { 00254 public: 00255 virtual ~GLColorStateItem() 00256 { } 00257 00258 virtual bool sync(); 00259 virtual bool apply(); 00260 00261 ///@name State variables 00262 //@{----------------------------------------------------------------- 00263 00264 /// glEnable; <GL_TRUE> or GL_FALSE 00265 GLboolean enableDither; 00266 00267 /// glColorMask; <GL_TRUE> or GL_FALSE 00268 GLboolean colorMask[4]; 00269 00270 //@}----------------------------------------------------------------- 00271 }; 00272 00273 class GLFogStateItem : public GLStateItem { 00274 public: 00275 virtual ~GLFogStateItem() 00276 { } 00277 00278 virtual bool sync(); 00279 virtual bool apply(); 00280 00281 ///@name State variables 00282 //@{----------------------------------------------------------------- 00283 00284 /// glEnable; GL_TRUE or <GL_FALSE> 00285 GLboolean enable; 00286 00287 /// glFogi; GL_LINEAR, <GL_EXP>, GL_EXP2 00288 GLenum fogMode; 00289 00290 /// glFogf; <1> (must be non-negative) 00291 GLfloat fogDensity; 00292 /// glFogf; <0> 00293 GLfloat fogStart; 00294 /// glFogf; <1> 00295 GLfloat fogEnd; 00296 00297 /// glFogi; GL_FOG_COORD or <GL_FRAGMENT_DEPTH> -- TODO ? initial value 00298 GLenum fogIndex; 00299 00300 /// glFogfv; 00301 GLcolor fogColor; 00302 00303 /// glHint; GL_FASTEST, GL_NICEST, or <GL_DONT_CARE> 00304 GLenum fogHint; 00305 00306 // TODO: add GL_FOG_COORD_ARRAY* 00307 00308 //@}----------------------------------------------------------------- 00309 }; 00310 00311 class GLPointStateItem : public GLStateItem { 00312 public: 00313 virtual ~GLPointStateItem() 00314 { } 00315 00316 virtual bool sync(); 00317 virtual bool apply(); 00318 00319 ///@name State variables 00320 //@{----------------------------------------------------------------- 00321 00322 /// glPointSize; <1> (must be positive) 00323 GLfloat pointSize; 00324 00325 /// glPointParameterf; <0> 00326 GLfloat pointSizeMin; 00327 /// glPointParameterf; <1> 00328 GLfloat pointSizeMax; 00329 /// glPointParameterf; <1> 00330 GLfloat pointFadeThresholdSize; 00331 00332 /// glPointParameteri; <GL_LOWER_LEFT> or GL_UPPER_LEFT 00333 GLenum pointSpriteCoordOrigin; 00334 00335 /// glPointParameterfv; <1, 0, 0> 00336 GLenum pointDistanceAttenuation[3]; 00337 00338 /// glEnable; GL_TRUE or <GL_FALSE> 00339 GLboolean pointSmooth; 00340 /// glHint; GL_FASTEST, GL_NICEST, or <GL_DONT_CARE> 00341 GLenum pointSmoothHint; 00342 00343 /// glEnable; GL_TRUE or <GL_FALSE> 00344 GLboolean pointSprite; 00345 00346 /// glGetFloat; read-only 00347 GLfloat pointSizeGranularity; 00348 GLfloat pointSizeRange[2]; 00349 00350 //@}----------------------------------------------------------------- 00351 }; 00352 00353 #endif // GL_STATE_H_ 00354
Generated on 28 Feb 2009 for Milton by
1.5.6