Material2Node.cc
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 "Material2Node.hh"
00057
00058
00059
00060
00061 namespace ACG {
00062 namespace SceneGraph {
00063
00064
00065
00066
00067
00068 Material2Node::Material2Node( BaseNode* _parent,
00069 const std::string& _name,
00070 unsigned int _applyProperties )
00071 : BaseNode(_parent, _name),
00072 applyProperties_(_applyProperties),
00073 shininess_(GLState::default_shininess),
00074 point_size_(1.0),
00075 line_width_(1.0),
00076 round_points_(false),
00077 alpha_test_(false),
00078 alpha_clip_(0),
00079 blending_(false),
00080 blend_param1_(GL_ONE),
00081 blend_param2_(GL_ZERO),
00082 backface_culling_(false),
00083 twosided_(false)
00084 {
00085 base_color_[0] = GLState::default_base_color;
00086 ambient_color_[0] = GLState::default_ambient_color;
00087 diffuse_color_[0] = GLState::default_diffuse_color;
00088 specular_color_[0] = GLState::default_specular_color;
00089 }
00090
00091
00092
00093
00094
00095 void Material2Node::enter(GLState& _state, unsigned int )
00096 {
00097 if (applyProperties_ & BaseColor)
00098 {
00099 base_color_backup_[0] = _state.base_color();
00100 _state.set_base_color(base_color_[0]);
00101 }
00102
00103 if (applyProperties_ & Material)
00104 {
00105 ambient_color_backup_[0] = _state.ambient_color();
00106 diffuse_color_backup_[0] = _state.diffuse_color();
00107 specular_color_backup_[0] = _state.specular_color();
00108 shininess_backup_ = _state.shininess();
00109
00110 _state.set_ambient_color(ambient_color_[0]);
00111 _state.set_diffuse_color(diffuse_color_[0]);
00112 _state.set_specular_color(specular_color_[0]);
00113 _state.set_shininess(shininess_);
00114 }
00115
00116 if (applyProperties_ & PointSize)
00117 {
00118 point_size_backup_ = _state.point_size();
00119 _state.set_point_size(point_size_);
00120 }
00121
00122 if (applyProperties_ & LineWidth)
00123 {
00124 line_width_backup_ = _state.line_width();
00125 _state.set_line_width(line_width_);
00126 }
00127
00128 if (applyProperties_ & RoundPoints)
00129 {
00130 round_points_backup_ = glIsEnabled(GL_POINT_SMOOTH) &&
00131 glIsEnabled(GL_ALPHA_TEST);
00132
00133 if( round_points_)
00134 glEnable(GL_POINT_SMOOTH);
00135 else
00136 glDisable(GL_POINT_SMOOTH);
00137 }
00138
00139
00140 if (applyProperties_ & AlphaTest)
00141 {
00142 alpha_test_backup_ = glIsEnabled(GL_ALPHA_TEST);
00143 glGetFloatv(GL_ALPHA_TEST_REF, &alpha_clip_backup_);
00144
00145 if(alpha_test_)
00146 {
00147 glAlphaFunc(GL_GREATER, alpha_clip_);
00148 glEnable(GL_ALPHA_TEST);
00149 }
00150 else
00151 {
00152 glDisable(GL_ALPHA_TEST);
00153 }
00154 }
00155
00156
00157 if (applyProperties_ & Blending)
00158 {
00159 blending_backup_ = _state.blending();
00160 glGetIntegerv( GL_BLEND_SRC, (GLint*) &blend_param1_backup_);
00161 glGetIntegerv( GL_BLEND_DST, (GLint*) &blend_param2_backup_);
00162
00163 _state.set_blending(blending_);
00164
00165 if (blending_)
00166 {
00167 glDepthFunc(GL_LEQUAL);
00168 glBlendFunc(blend_param1_, blend_param2_);
00169 glEnable(GL_BLEND);
00170 }
00171 else
00172 {
00173 glDepthFunc(GL_LESS);
00174 glDisable(GL_BLEND);
00175 }
00176 }
00177
00178
00179 if (applyProperties_ & BackFaceCulling)
00180 {
00181 backface_culling_backup_ = glIsEnabled(GL_CULL_FACE);
00182
00183 if (backface_culling_)
00184 glEnable( GL_CULL_FACE );
00185 else
00186 glDisable( GL_CULL_FACE );
00187 }
00188
00189
00190 if ( twosided_ )
00191 {
00192 glMaterialfv( GL_FRONT, GL_AMBIENT, ambient_color_[0].data() );
00193 glMaterialfv( GL_FRONT, GL_DIFFUSE, diffuse_color_[0].data() );
00194 glMaterialfv( GL_FRONT, GL_SPECULAR, specular_color_[0].data() );
00195
00196 glMaterialfv( GL_BACK, GL_AMBIENT, ambient_color_[1].data() );
00197 glMaterialfv( GL_BACK, GL_DIFFUSE, diffuse_color_[1].data() );
00198 glMaterialfv( GL_BACK, GL_SPECULAR, specular_color_[1].data() );
00199
00200 glColor3f( 0.5, 0.5, 0.5 );
00201 }
00202
00203 }
00204
00205
00206
00207
00208
00209 void Material2Node::leave(GLState& _state, unsigned int )
00210 {
00211 if (applyProperties_ & BaseColor)
00212 {
00213 _state.set_base_color(base_color_backup_[0]);
00214 }
00215
00216
00217 if (applyProperties_ & Material)
00218 {
00219 _state.set_ambient_color(ambient_color_backup_[0]);
00220 _state.set_diffuse_color(diffuse_color_backup_[0]);
00221 _state.set_specular_color(specular_color_backup_[0]);
00222 _state.set_shininess(shininess_backup_);
00223 }
00224
00225
00226 if (applyProperties_ & PointSize)
00227 {
00228 _state.set_point_size(point_size_backup_);
00229 }
00230
00231
00232 if (applyProperties_ & LineWidth)
00233 {
00234 _state.set_line_width(line_width_backup_);
00235 }
00236
00237
00238 if (applyProperties_ & RoundPoints)
00239 {
00240 if( round_points_backup_)
00241 glEnable(GL_POINT_SMOOTH);
00242 else
00243 glDisable(GL_POINT_SMOOTH);
00244 }
00245
00246 if (applyProperties_ & AlphaTest)
00247 {
00248 if (alpha_test_backup_)
00249 {
00250 glAlphaFunc(GL_GREATER, alpha_clip_backup_);
00251 glEnable(GL_ALPHA_TEST);
00252 }
00253 else
00254 {
00255 glDisable(GL_ALPHA_TEST);
00256 }
00257 }
00258
00259
00260 if (applyProperties_ & Blending)
00261 {
00262 _state.set_blending(blending_backup_);
00263
00264 if (blending_backup_)
00265 {
00266 glDepthFunc(GL_LEQUAL);
00267 glBlendFunc(blend_param1_backup_, blend_param2_backup_);
00268 glEnable(GL_BLEND);
00269 }
00270 else
00271 {
00272 glDepthFunc(GL_LESS);
00273 glDisable(GL_BLEND);
00274 }
00275 }
00276
00277
00278 if (applyProperties_ & BackFaceCulling)
00279 {
00280 if (backface_culling_backup_)
00281 glEnable( GL_CULL_FACE );
00282 else
00283 glDisable( GL_CULL_FACE );
00284 }
00285 }
00286
00287
00288
00289
00290
00291 void
00292 Material2Node::read(std::istream& )
00293 {
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365 }
00366
00367
00368 }
00369 }
00370