00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043 #include <stdlib.h>
00044 #include <stdio.h>
00045
00046 #include "OSGConfig.h"
00047
00048 #include <OSGGL.h>
00049 #include <OSGGLU.h>
00050
00051 #include "OSGMaterialChunk.h"
00052
00053 OSG_USING_NAMESPACE
00054
00055
00056
00057
00058
00072
00073
00074
00075
00076 StateChunkClass MaterialChunk::_class("Material");
00077
00078
00079
00080
00081
00082 void MaterialChunk::initMethod (void)
00083 {
00084 }
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094 MaterialChunk::MaterialChunk(void) :
00095 Inherited()
00096 {
00097 }
00098
00099 MaterialChunk::MaterialChunk(const MaterialChunk &source) :
00100 Inherited(source)
00101 {
00102 }
00103
00104 MaterialChunk::~MaterialChunk(void)
00105 {
00106 }
00107
00108
00109
00110 const StateChunkClass *MaterialChunk::getClass(void) const
00111 {
00112 return &_class;
00113 }
00114
00115
00116
00117 void MaterialChunk::changed(BitVector whichField, UInt32 origin)
00118 {
00119 Inherited::changed(whichField, origin);
00120 }
00121
00122
00123
00124 void MaterialChunk::dump( UInt32 OSG_CHECK_ARG(uiIndent),
00125 const BitVector OSG_CHECK_ARG(bvFlags )) const
00126 {
00127 SLOG << "Dump MaterialChunk NI" << std::endl;
00128 }
00129
00130
00131
00132 void MaterialChunk::activate(DrawActionBase *, UInt32)
00133 {
00134 glErr("material:activate:precheck");
00135
00136 GLenum target;
00137
00138 if(getBackMaterial())
00139 {
00140 target = GL_FRONT;
00141 if(getColorMaterial() == GL_NONE && getBackColorMaterial() == GL_NONE)
00142 {
00143 glDisable(GL_COLOR_MATERIAL);
00144 }
00145 else if(getColorMaterial() == GL_NONE)
00146 {
00147 glColorMaterial(GL_BACK, getBackColorMaterial());
00148 glEnable(GL_COLOR_MATERIAL);
00149 }
00150 else if(getBackColorMaterial() == GL_NONE)
00151 {
00152 glColorMaterial(GL_FRONT, getColorMaterial());
00153 glEnable(GL_COLOR_MATERIAL);
00154 }
00155 else if(getColorMaterial() != getBackColorMaterial())
00156 {
00157 FWARNING(("MaterialChunk::activate: can't have different "
00158 "back and front colorMaterial types!\n"));
00159 glEnable(GL_COLOR_MATERIAL);
00160 }
00161 else
00162 {
00163 glColorMaterial(GL_FRONT_AND_BACK, getColorMaterial());
00164 glEnable(GL_COLOR_MATERIAL);
00165 }
00166 }
00167 else
00168 {
00169 target = GL_FRONT_AND_BACK;
00170 if(getColorMaterial() == GL_NONE)
00171 {
00172 glDisable(GL_COLOR_MATERIAL);
00173 }
00174 else
00175 {
00176 glColorMaterial(GL_FRONT_AND_BACK, getColorMaterial());
00177 glEnable(GL_COLOR_MATERIAL);
00178 }
00179 }
00180
00181 glColor4fv(_sfDiffuse.getValue().getValuesRGBA());
00182
00183 if(getLit())
00184 {
00185 glMaterialfv(target, GL_DIFFUSE,
00186 _sfDiffuse.getValue().getValuesRGBA());
00187 glMaterialfv(target, GL_AMBIENT,
00188 _sfAmbient.getValue().getValuesRGBA());
00189 glMaterialfv(target, GL_SPECULAR,
00190 _sfSpecular.getValue().getValuesRGBA());
00191 glMaterialfv(target, GL_EMISSION,
00192 _sfEmission.getValue().getValuesRGBA());
00193 glMaterialf (target, GL_SHININESS, _sfShininess.getValue());
00194
00195 if(getBackMaterial())
00196 {
00197 glMaterialfv(GL_BACK, GL_DIFFUSE,
00198 _sfBackDiffuse.getValue().getValuesRGBA());
00199 glMaterialfv(GL_BACK, GL_AMBIENT,
00200 _sfBackAmbient.getValue().getValuesRGBA());
00201 glMaterialfv(GL_BACK, GL_SPECULAR,
00202 _sfBackSpecular.getValue().getValuesRGBA());
00203 glMaterialfv(GL_BACK, GL_EMISSION,
00204 _sfBackEmission.getValue().getValuesRGBA());
00205 glMaterialf (GL_BACK, GL_SHININESS, _sfBackShininess.getValue());
00206 }
00207
00208 glEnable(GL_LIGHTING);
00209 }
00210 else
00211 {
00212 glDisable(GL_LIGHTING);
00213 }
00214
00215 glErr("material:activate:postcheck");
00216 }
00217
00218 void MaterialChunk::changeFrom(DrawActionBase *, StateChunk * old_chunk, UInt32)
00219 {
00220 glErr("material:changed:precheck");
00221
00222 MaterialChunk const *old = dynamic_cast<MaterialChunk const*>(old_chunk);
00223
00224
00225
00226 if(old == this)
00227 {
00228
00229 if(getColorMaterial() != GL_NONE ||
00230 (getBackMaterial() && getBackColorMaterial() != GL_NONE)
00231 )
00232 glColor4fv(_sfDiffuse.getValue().getValuesRGBA());
00233
00234 return;
00235 }
00236
00237 if(getColorMaterial() != old->getColorMaterial() ||
00238 getBackColorMaterial() != old->getBackColorMaterial()
00239 )
00240 {
00241 if(getBackMaterial())
00242 {
00243 if(getColorMaterial() == GL_NONE && getBackColorMaterial() == GL_NONE)
00244 {
00245 glDisable(GL_COLOR_MATERIAL);
00246 }
00247 else if(getColorMaterial() == GL_NONE)
00248 {
00249 glColorMaterial(GL_BACK, getBackColorMaterial());
00250 glEnable(GL_COLOR_MATERIAL);
00251 }
00252 else if(getBackColorMaterial() == GL_NONE)
00253 {
00254 glColorMaterial(GL_FRONT, getColorMaterial());
00255 glEnable(GL_COLOR_MATERIAL);
00256 }
00257 else if(getColorMaterial() != getBackColorMaterial())
00258 {
00259 FWARNING(("MaterialChunk::changeFrom: can't have different "
00260 "back and front colorMaterial types!\n"));
00261 glEnable(GL_COLOR_MATERIAL);
00262 }
00263 else
00264 {
00265 glColorMaterial(GL_FRONT_AND_BACK, getColorMaterial());
00266 glEnable(GL_COLOR_MATERIAL);
00267 }
00268 }
00269 else
00270 {
00271 if(getColorMaterial() == GL_NONE)
00272 {
00273 glDisable(GL_COLOR_MATERIAL);
00274 }
00275 else
00276 {
00277 glColorMaterial(GL_FRONT_AND_BACK, getColorMaterial());
00278 glEnable(GL_COLOR_MATERIAL);
00279 }
00280 }
00281 }
00282
00283 if(getColorMaterial() != GL_NONE || getBackColorMaterial() != GL_NONE)
00284 glColor4fv(_sfDiffuse.getValue().getValuesRGBA());
00285
00286 if(getLit() && ! old->getLit())
00287 glEnable(GL_LIGHTING);
00288 else if(! getLit() && old->getLit())
00289 glDisable(GL_LIGHTING);
00290
00291 if(getLit())
00292 {
00293 GLenum target;
00294
00295 if(getBackMaterial())
00296 {
00297 target = GL_FRONT;
00298 }
00299 else
00300 {
00301 target = GL_FRONT_AND_BACK;
00302 }
00303
00304 glMaterialfv(target, GL_DIFFUSE,
00305 _sfDiffuse.getValue().getValuesRGBA());
00306 glMaterialfv(target, GL_AMBIENT,
00307 _sfAmbient.getValue().getValuesRGBA());
00308 glMaterialfv(target, GL_SPECULAR,
00309 _sfSpecular.getValue().getValuesRGBA());
00310 glMaterialfv(target, GL_EMISSION,
00311 _sfEmission.getValue().getValuesRGBA());
00312
00313 if(osgabs(_sfShininess.getValue() - old->getShininess()) > 1e-4)
00314 glMaterialf( target, GL_SHININESS, _sfShininess.getValue());
00315
00316 if(getBackMaterial())
00317 {
00318 glMaterialfv(GL_BACK, GL_DIFFUSE,
00319 _sfBackDiffuse.getValue().getValuesRGBA());
00320 glMaterialfv(GL_BACK, GL_AMBIENT,
00321 _sfBackAmbient.getValue().getValuesRGBA());
00322 glMaterialfv(GL_BACK, GL_SPECULAR,
00323 _sfBackSpecular.getValue().getValuesRGBA());
00324 glMaterialfv(GL_BACK, GL_EMISSION,
00325 _sfBackEmission.getValue().getValuesRGBA());
00326
00327 if(osgabs(_sfBackShininess.getValue() -
00328 old->getBackShininess()
00329 ) > 1e-4)
00330 glMaterialf( GL_BACK, GL_SHININESS, _sfBackShininess.getValue());
00331 }
00332 }
00333
00334 glErr("material:changed:precheck");
00335 }
00336
00337 void MaterialChunk::deactivate(DrawActionBase *, UInt32)
00338 {
00339 if(getLit())
00340 glDisable(GL_LIGHTING);
00341
00342 if(getColorMaterial() != GL_NONE)
00343 glDisable(GL_COLOR_MATERIAL);
00344 }
00345
00346
00347
00348
00349 Real32 MaterialChunk::switchCost(StateChunk *OSG_CHECK_ARG(chunk))
00350 {
00351 return 0;
00352 }
00353
00354 bool MaterialChunk::isTransparent(void) const
00355 {
00356 return(getDiffuse()[3] <(1. - Eps));
00357 }
00358
00359
00360 bool MaterialChunk::operator <(const StateChunk &other) const
00361 {
00362 return this < &other;
00363 }
00364
00365 bool MaterialChunk::operator ==(const StateChunk &other) const
00366 {
00367 MaterialChunk const *tother = dynamic_cast<MaterialChunk const*>(&other);
00368
00369 if(!tother)
00370 return false;
00371
00372 if(tother == this)
00373 return true;
00374
00375 if(!getAmbient ().equals(tother->getAmbient (), Eps) ||
00376 !getDiffuse ().equals(tother->getDiffuse (), Eps) ||
00377 !getSpecular ().equals(tother->getSpecular (), Eps) ||
00378 !getEmission ().equals(tother->getEmission (), Eps) ||
00379 getShininess() != tother->getShininess() ||
00380 getLit() != tother->getLit() ||
00381 getColorMaterial() != tother->getColorMaterial()
00382 )
00383 return false;
00384
00385 return true;
00386 }
00387
00388 bool MaterialChunk::operator !=(const StateChunk &other) const
00389 {
00390 return !(*this == other);
00391 }
00392
00393
00394
00395
00396
00397 #ifdef OSG_SGI_CC
00398 #pragma set woff 1174
00399 #endif
00400
00401 #ifdef OSG_LINUX_ICC
00402 #pragma warning( disable : 177 )
00403 #endif
00404
00405 namespace
00406 {
00407 static Char8 cvsid_cpp [] = "@(#)$Id: FCTemplate_cpp.h,v 1.13 2002/06/01 10:37:25 vossg Exp $";
00408 static Char8 cvsid_hpp [] = OSGMATERIALCHUNK_HEADER_CVSID;
00409 static Char8 cvsid_inl [] = OSGMATERIALCHUNK_INLINE_CVSID;
00410
00411 static Char8 cvsid_fields_hpp[] = OSGMATERIALCHUNKFIELDS_HEADER_CVSID;
00412 }
00413
00414 #ifdef __sgi
00415 #pragma reset woff 1174
00416 #endif
00417
00418