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
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056 #include "MaterialNode.hh"
00057 #include "DrawModes.hh"
00058
00059
00060
00061
00062 namespace ACG {
00063 namespace SceneGraph {
00064
00065
00066
00067
00068
00069 MaterialNode::MaterialNode( BaseNode* _parent,
00070 const std::string& _name,
00071 unsigned int _applyProperties )
00072 : BaseNode(_parent, _name),
00073 applyProperties_(_applyProperties),
00074 base_color_(GLState::default_base_color),
00075 ambient_color_(GLState::default_ambient_color),
00076 diffuse_color_(GLState::default_diffuse_color),
00077 specular_color_(GLState::default_specular_color),
00078 shininess_(GLState::default_shininess),
00079 point_size_(1.0),
00080 line_width_(1.0),
00081 round_points_(false),
00082 lines_smooth_(false),
00083 alpha_test_(false),
00084 alpha_clip_(0),
00085 blending_(false),
00086 blend_param1_(GL_ONE),
00087 blend_param2_(GL_ZERO),
00088 color_material_(true),
00089 backface_culling_(false),
00090 multiSampling_(true)
00091 {}
00092
00093
00094
00095
00096
00097 void MaterialNode::enter(GLState& _state, unsigned int _drawmode )
00098 {
00099 if (applyProperties_ & BaseColor)
00100 {
00101 base_color_backup_ = _state.base_color();
00102 _state.set_base_color(base_color_);
00103 }
00104
00105 if (applyProperties_ & Material)
00106 {
00107 ambient_color_backup_ = _state.ambient_color();
00108 diffuse_color_backup_ = _state.diffuse_color();
00109 specular_color_backup_ = _state.specular_color();
00110 shininess_backup_ = _state.shininess();
00111
00112 _state.set_ambient_color(ambient_color_);
00113 _state.set_diffuse_color(diffuse_color_);
00114 _state.set_specular_color(specular_color_);
00115 _state.set_shininess(shininess_);
00116 }
00117
00118 if (applyProperties_ & PointSize)
00119 {
00120 point_size_backup_ = _state.point_size();
00121 _state.set_point_size(point_size_);
00122 }
00123
00124 if (applyProperties_ & LineWidth)
00125 {
00126 line_width_backup_ = _state.line_width();
00127 _state.set_line_width(line_width_);
00128 }
00129
00130 if (applyProperties_ & RoundPoints)
00131 {
00132 round_points_backup_ = glIsEnabled(GL_POINT_SMOOTH) &&
00133 glIsEnabled(GL_ALPHA_TEST);
00134
00135 if( round_points_) {
00136 glHint(GL_POINT_SMOOTH_HINT,GL_NICEST);
00137 glEnable(GL_POINT_SMOOTH);
00138 } else
00139 glDisable(GL_POINT_SMOOTH);
00140 }
00141
00142 if (applyProperties_ & LineSmooth)
00143 {
00144 lines_smooth_backup_ = glIsEnabled(GL_LINE_SMOOTH) &&
00145 glIsEnabled(GL_ALPHA_TEST);
00146
00147 if( lines_smooth_) {
00148 glHint(GL_LINE_SMOOTH_HINT,GL_NICEST);
00149 glEnable(GL_LINE_SMOOTH);
00150 } else
00151 glDisable(GL_LINE_SMOOTH);
00152 }
00153
00154 if (applyProperties_ & MultiSampling)
00155 {
00156 multiSampling_backup_ = _state.multisampling();
00157 _state.set_multisampling( multiSampling_ );
00158 }
00159
00160 if (applyProperties_ & AlphaTest)
00161 {
00162 alpha_test_backup_ = glIsEnabled(GL_ALPHA_TEST);
00163 glGetFloatv(GL_ALPHA_TEST_REF, &alpha_clip_backup_);
00164
00165 if(alpha_test_)
00166 {
00167 glAlphaFunc(GL_GREATER, alpha_clip_);
00168 glEnable(GL_ALPHA_TEST);
00169 }
00170 else
00171 {
00172 glDisable(GL_ALPHA_TEST);
00173 }
00174 }
00175
00176
00177 if (applyProperties_ & Blending)
00178 {
00179 blending_backup_ = _state.blending();
00180 glGetIntegerv( GL_BLEND_SRC, (GLint*) &blend_param1_backup_);
00181 glGetIntegerv( GL_BLEND_DST, (GLint*) &blend_param2_backup_);
00182
00183 _state.set_blending(blending_);
00184
00185 if (blending_)
00186 {
00187 glDepthFunc(GL_LEQUAL);
00188 glBlendFunc(blend_param1_, blend_param2_);
00189 glEnable(GL_BLEND);
00190 }
00191 else
00192 {
00193 glDepthFunc(GL_LESS);
00194 glDisable(GL_BLEND);
00195 }
00196 }
00197
00198
00199 if (applyProperties_ & BackFaceCulling)
00200 {
00201 backface_culling_backup_ = glIsEnabled(GL_CULL_FACE);
00202
00203 if ( backface_culling_ )
00204 glEnable( GL_CULL_FACE );
00205 else
00206 glDisable( GL_CULL_FACE );
00207
00208 }
00209
00210 if ( ( applyProperties_ & ColorMaterial ) && ( (_drawmode & DrawModes::SOLID_FACES_COLORED_FLAT_SHADED) ||
00211 (_drawmode & DrawModes::SOLID_2DTEXTURED_FACE_SHADED) ) )
00212 {
00213 color_material_backup_ = glIsEnabled(GL_COLOR_MATERIAL);
00214
00215 if (color_material_ ) {
00216 glDisable( GL_COLOR_MATERIAL );
00217 glColorMaterial( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE );
00218 glEnable( GL_COLOR_MATERIAL );
00219 } else
00220 glDisable( GL_COLOR_MATERIAL );
00221 }
00222
00223 }
00224
00225
00226
00227 void MaterialNode::enterPick(GLState& ,
00228 PickTarget ,
00229 unsigned int ) {
00230
00231 }
00232
00233 void MaterialNode::leavePick(GLState& ,
00234 PickTarget ,
00235 unsigned int ) {
00236 }
00237
00238
00239
00240
00241
00242 void MaterialNode::leave(GLState& _state, unsigned int _drawmode )
00243 {
00244 if (applyProperties_ & BaseColor)
00245 {
00246 _state.set_base_color(base_color_backup_);
00247 }
00248
00249
00250 if (applyProperties_ & Material)
00251 {
00252 _state.set_ambient_color(ambient_color_backup_);
00253 _state.set_diffuse_color(diffuse_color_backup_);
00254 _state.set_specular_color(specular_color_backup_);
00255 _state.set_shininess(shininess_backup_);
00256 }
00257
00258
00259 if (applyProperties_ & PointSize)
00260 {
00261 _state.set_point_size(point_size_backup_);
00262 }
00263
00264
00265 if (applyProperties_ & LineWidth)
00266 {
00267 _state.set_line_width(line_width_backup_);
00268 }
00269
00270
00271 if (applyProperties_ & RoundPoints)
00272 {
00273 if( round_points_backup_)
00274 glEnable(GL_POINT_SMOOTH);
00275 else
00276 glDisable(GL_POINT_SMOOTH);
00277 }
00278
00279 if (applyProperties_ & LineSmooth)
00280 {
00281 if( lines_smooth_backup_)
00282 glEnable(GL_LINE_SMOOTH);
00283 else
00284 glDisable(GL_LINE_SMOOTH);
00285 }
00286
00287 if (applyProperties_ & MultiSampling)
00288 _state.set_multisampling( multiSampling_backup_ );
00289
00290 if (applyProperties_ & AlphaTest)
00291 {
00292 if (alpha_test_backup_)
00293 {
00294 glAlphaFunc(GL_GREATER, alpha_clip_backup_);
00295 glEnable(GL_ALPHA_TEST);
00296 }
00297 else
00298 {
00299 glDisable(GL_ALPHA_TEST);
00300 }
00301 }
00302
00303
00304 if (applyProperties_ & Blending)
00305 {
00306 _state.set_blending(blending_backup_);
00307
00308 if (blending_backup_)
00309 {
00310 glDepthFunc(GL_LEQUAL);
00311 glBlendFunc(blend_param1_backup_, blend_param2_backup_);
00312 glEnable(GL_BLEND);
00313 }
00314 else
00315 {
00316 glDepthFunc(GL_LESS);
00317 glDisable(GL_BLEND);
00318 }
00319 }
00320
00321
00322 if (applyProperties_ & BackFaceCulling)
00323 {
00324 if (backface_culling_backup_)
00325 glEnable( GL_CULL_FACE );
00326 else
00327 glDisable( GL_CULL_FACE );
00328 }
00329
00330 if ( ( applyProperties_ & ColorMaterial ) && ( (_drawmode & DrawModes::SOLID_FACES_COLORED_FLAT_SHADED) ||
00331 (_drawmode & DrawModes::SOLID_2DTEXTURED_FACE_SHADED) ) )
00332 {
00333 if (color_material_backup_ ) {
00334 glEnable( GL_COLOR_MATERIAL );
00335 } else
00336 glDisable( GL_COLOR_MATERIAL );
00337 }
00338
00339 }
00340
00341
00342
00343
00344
00345 void
00346 MaterialNode::read(std::istream& _is)
00347 {
00348
00349 char s[200];
00350 float x, y, z, u;
00351
00352
00353 while(_is && (!_is.eof()) && _is.getline(s, 200))
00354 {
00355
00356 if (s[0] == '#') continue;
00357
00358
00359
00360 else if (strncmp(s, "BaseColor ", 10) == 0)
00361 {
00362 if (sscanf(s, "BaseColor %f %f %f %f", &x, &y, &z, &u))
00363 {
00364 base_color_ = Vec4f(x,y,z,u);
00365 }
00366 }
00367
00368 else if (strncmp(s, "AmbientColor ", 13) == 0)
00369 {
00370 if (sscanf(s, "AmbientColor %f %f %f %f", &x, &y, &z, &u))
00371 {
00372 ambient_color_ = Vec4f(x,y,z,u);
00373 }
00374 }
00375
00376 else if (strncmp(s, "DiffuseColor ", 13) == 0)
00377 {
00378 if (sscanf(s, "DiffuseColor %f %f %f %f", &x, &y, &z, &u))
00379 {
00380 diffuse_color_ = Vec4f(x,y,z,u);
00381 }
00382 }
00383
00384 else if (strncmp(s, "SpecularColor ", 14) == 0)
00385 {
00386 if (sscanf(s, "SpecularColor %f %f %f %f", &x, &y, &z, &u))
00387 {
00388 specular_color_ = Vec4f(x,y,z,u);
00389 }
00390 }
00391
00392 else if (strncmp(s, "Shininess ", 10) == 0)
00393 {
00394 if (sscanf(s, "Shininess %f", &x))
00395 {
00396 shininess_ = x;
00397 }
00398 }
00399
00400 else if (strncmp(s, "PointSize ", 10) == 0)
00401 {
00402 if (sscanf(s, "PointSize %f", &x))
00403 {
00404 point_size_ = x;
00405 }
00406 }
00407
00408 else if (strncmp(s, "LineWidth ", 10) == 0)
00409 {
00410 if (sscanf(s, "LineWidth %f", &x))
00411 {
00412 line_width_ = x;
00413 }
00414 }
00415
00416
00417 s[0]=' ';
00418 }
00419 }
00420
00421
00422 }
00423 }
00424