00001 #include "StateSet.h"
00002 #include "Program.h"
00003 #include "Error.h"
00004
00005 namespace RenderTools {
00006
00007 Stateset::Stateset( void ):
00008 AbstractPropertyContainer(),
00009 mask( NONE ),
00010 currentColor( 0.0f, 0.0f, 0.0f, 0.0f ),
00011 clearColor( 0.0f, 0.0f, 0.0f, 0.0f ),
00012 texture( GL_FALSE ),
00013 blend( GL_TRUE ),
00014 #ifdef GL_BLEND_SRC
00015 blendSrc( GL_SRC_ALPHA ),
00016 blendDst( GL_ONE_MINUS_SRC_ALPHA ),
00017 #endif
00018 redMask( GL_TRUE ),
00019 greenMask( GL_TRUE ),
00020 blueMask( GL_TRUE ),
00021 alphaMask( GL_TRUE ),
00022 alphaTest( GL_FALSE ),
00023 #ifdef GL_ALPHA_TEST_FUNC
00024 alphaFunc( GL_ALWAYS ),
00025 alphaRef( 1.0f ),
00026 #endif
00027 depthTest( GL_TRUE ),
00028 #ifdef GL_DEPTH_FUNC
00029 depthFunc( GL_LEQUAL ),
00030 #endif
00031 depthMask( GL_TRUE ),
00032 #ifdef GL_CULL_FACE
00033 cullFace( GL_FALSE ),
00034 cullMode( GL_BACK ),
00035 #endif
00036 #ifdef GL_POLYGON_MODE
00037 polygonFrontMode( GL_FILL ),
00038 polygonBackMode( GL_FILL ),
00039 #endif
00040 lineWidth( 1.0f ),
00041 pointSize( 1.0f ){
00042
00043 }
00044
00045 PropertyPtr Stateset::create( const XMLNodePtr & xml ){
00046 StatesetPtr p( new Stateset() );
00047 p->setName( xml );
00048 p->createProperties();
00049 p->setProperties( xml, false );
00050 return( dynamic_pointer_cast< AbstractProperty, Stateset >( p ) );
00051 }
00052
00053 void Stateset::createProperties( void ){
00054 AbstractPropertyContainer::createProperties();
00055 createProperty( this, "componentMask", StateComponentMaskStruct( & mask ), getStateComponentsEnums(), AbstractProperty::MASK );
00056 createProperty( this, "currentColor", & currentColor );
00057 createProperty( this, "clearColor", & clearColor );
00058 createProperty( this, "texture", GLbooleanStruct( & texture ) );
00059 createProperty( this, "blend", GLbooleanStruct( & blend ) );
00060
00061 #ifdef GL_BLEND_SRC
00062 EnumList blendEnum;
00063 blendEnum << GL_ZERO;
00064 blendEnum << GL_ONE;
00065 blendEnum << GL_SRC_COLOR;
00066 blendEnum << GL_ONE_MINUS_SRC_COLOR;
00067 blendEnum << GL_SRC_ALPHA;
00068 blendEnum << GL_ONE_MINUS_SRC_ALPHA;
00069 blendEnum << GL_DST_ALPHA;
00070 blendEnum << GL_ONE_MINUS_DST_ALPHA;
00071 createProperty( this, "blendSource", GLenumStruct( & blendSrc ), blendEnum );
00072 createProperty( this, "blendDestination", GLenumStruct( & blendDst ), blendEnum );
00073 #endif
00074
00075 createProperty( this, "redMask", GLbooleanStruct( & redMask ) );
00076 createProperty( this, "greenMask", GLbooleanStruct( & greenMask ) );
00077 createProperty( this, "blueMask", GLbooleanStruct( & blueMask ) );
00078 createProperty( this, "alphaMask", GLbooleanStruct( & alphaMask ) );
00079 createProperty( this, "alphaTest", GLbooleanStruct( & alphaTest ) );
00080
00081 EnumList funcEnum;
00082 #ifdef GL_NEVER
00083 funcEnum << GL_NEVER;
00084 #endif
00085 #ifdef GL_LESS
00086 funcEnum << GL_LESS;
00087 #endif
00088 #ifdef GL_EQUAL
00089 funcEnum << GL_EQUAL;
00090 #endif
00091 #ifdef GL_LEQUAL
00092 funcEnum << GL_LEQUAL;
00093 #endif
00094 #ifdef GL_GREATER
00095 funcEnum << GL_GREATER;
00096 #endif
00097 #ifdef GL_NOTEQUAL
00098 funcEnum << GL_NOTEQUAL;
00099 #endif
00100 #ifdef GL_GEQUAL
00101 funcEnum << GL_GEQUAL;
00102 #endif
00103 #ifdef GL_ALWAYS
00104 funcEnum << GL_ALWAYS;
00105 #endif
00106
00107 #ifdef GL_ALPHA_TEST_FUNC
00108 createProperty( this, "alphaFunction", GLenumStruct( & alphaFunc ), funcEnum );
00109 createProperty( this, "alphaReference", & alphaRef );
00110 #endif
00111
00112 createProperty( this, "depthTest", GLbooleanStruct( & depthTest ) );
00113 #ifdef GL_DEPTH_FUNC
00114 createProperty( this, "depthFunction", GLenumStruct( & depthFunc ), funcEnum );
00115 #endif
00116 createProperty( this, "depthMask", GLbooleanStruct( & depthMask ) );
00117
00118 #ifdef GL_CULL_FACE
00119 createProperty( this, "faceCulling", GLbooleanStruct( & cullFace ) );
00120
00121 EnumList cullEnum;
00122 cullEnum << GL_FRONT;
00123 cullEnum << GL_BACK;
00124 cullEnum << GL_FRONT_AND_BACK;
00125 createProperty( this, "cullMode", GLenumStruct( & cullMode ), cullEnum );
00126 #endif
00127
00128 #ifdef GL_POLYGON_MODE
00129 EnumList polyEnum;
00130 polyEnum << GL_POINT;
00131 polyEnum << GL_LINE;
00132 polyEnum << GL_FILL;
00133 createProperty( this, "polygonFrontMode", GLenumStruct( & polygonFrontMode ), polyEnum );
00134 createProperty( this, "polygonBackFace", GLenumStruct( & polygonBackMode ), polyEnum );
00135 #endif
00136
00137 createProperty( this, "lineWidth", & lineWidth );
00138 createProperty( this, "pointSize", & pointSize );
00139 }
00140
00141 const string Stateset::getTypeName( bool ofComponent ) const {
00142 return( "Stateset" );
00143 }
00144
00145 void Stateset::onBind( int unit ){
00146
00147 #if glColor4f
00148 if( mask & CURRENT_COLOR ){
00149 glColor4f( currentColor[0], currentColor[1], currentColor[2], currentColor[3] );
00150 }
00151 #else
00152 # ifndef RT_GLES1
00153 Error::assertNoErrors( __FILE__, __LINE__ );
00154 ProgramPtr current = Program::getCurrent();
00155 if( current ){
00156 current->bind( "currentColor", toString( currentColor ) );
00157 }
00158 # endif
00159 #endif
00160 if( mask & CLEAR_COLOR ){
00161 glClearColor( clearColor[0], clearColor[1], clearColor[2], clearColor[3] );
00162 if( mask & DEPTH_TEST ){
00163 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
00164 }
00165 else{
00166 glClear( GL_COLOR_BUFFER_BIT );
00167 }
00168 }
00169 if( mask & COLOR_MASK ){
00170 glColorMask( redMask, greenMask, blueMask, alphaMask );
00171 Error::assertNoErrors( __FILE__, __LINE__ );
00172 }
00173 if( mask & TEXTURE ){
00174 if( texture ){
00175 glEnable( GL_TEXTURE_2D );
00176 }
00177 else{
00178 glDisable( GL_TEXTURE_2D );
00179 }
00180 }
00181 #ifdef GL_BLEND
00182 if( mask & BLEND ){
00183 if( blend ){
00184 glEnable( GL_BLEND );
00185 #ifdef GL_BLEND_SRC
00186 glBlendFunc( blendSrc, blendDst );
00187 Error::assertNoErrors( __FILE__, __LINE__ );
00188 #endif
00189 }
00190 else{
00191 glDisable( GL_BLEND );
00192 }
00193 }
00194 Error::assertNoErrors( __FILE__, __LINE__ );
00195 #endif
00196
00197 #ifdef GL_ALPHA_TEST
00198 if( mask & ALPHA_TEST ){
00199 if( alphaTest ){
00200 glEnable( GL_ALPHA_TEST );
00201 }
00202 else{
00203 glDisable( GL_ALPHA_TEST );
00204 }
00205 }
00206 #endif
00207
00208 #ifdef GL_ALPHA_TEST_FUNC
00209 if( mask & ALPHA_FUNCTION ){
00210 glAlphaFunc( alphaFunc, alphaRef );
00211 Error::assertNoErrors( __FILE__, __LINE__ );
00212 }
00213 #endif
00214
00215 if( mask & DEPTH_TEST ){
00216 if( depthTest ){
00217 glEnable( GL_DEPTH_TEST );
00218 }
00219 else{
00220 glDisable( GL_DEPTH_TEST );
00221 }
00222 }
00223
00224 #ifdef GL_DEPTH_FUNC
00225 if( mask & DEPTH_FUNCTION ){
00226 glDepthFunc( depthFunc );
00227 Error::assertNoErrors( __FILE__, __LINE__ );
00228 }
00229 #endif
00230
00231 if( mask & DEPTH_MASK ){
00232 glDepthMask( depthMask );
00233 Error::assertNoErrors( __FILE__, __LINE__ );
00234 }
00235
00236 if( mask & CULL_FACE ){
00237 if( cullFace ){
00238 glEnable( GL_CULL_FACE );
00239 }
00240 else{
00241 glDisable( GL_CULL_FACE );
00242 }
00243 }
00244
00245 if( mask & CULL_MODE ){
00246 glCullFace( cullMode );
00247 Error::assertNoErrors( __FILE__, __LINE__ );
00248 }
00249 #ifdef GL_POLYGON_MODE
00250 if( mask & POLYGON_MODE ){
00251 if( polygonFrontMode == polygonBackMode ){
00252 glPolygonMode( GL_FRONT_AND_BACK, polygonFrontMode );
00253 }
00254 else{
00255 glPolygonMode( GL_FRONT, polygonFrontMode );
00256 glPolygonMode( GL_BACK, polygonBackMode );
00257 }
00258 Error::assertNoErrors( __FILE__, __LINE__ );
00259 }
00260 #endif
00261
00262 #ifdef GL_LINE_WIDTH
00263 if( mask & LINE_WIDTH ){
00264 glLineWidth( lineWidth );
00265 Error::assertNoErrors( __FILE__, __LINE__ );
00266 }
00267 #endif
00268
00269 #ifdef GL_POINT_SIZE
00270 if( mask & POINT_SIZE ){
00271 glPointSize( pointSize );
00272 Error::assertNoErrors( __FILE__, __LINE__ );
00273 }
00274 #endif
00275 }
00276
00277 void Stateset::onInitialize( void ){
00278
00279 }
00280
00281 void Stateset::onUnbind( int unit ){
00282
00283 }
00284
00285 };