Material2Node.cc

00001 /*===========================================================================*\
00002  *                                                                           *
00003  *                              OpenFlipper                                  *
00004  *      Copyright (C) 2001-2009 by Computer Graphics Group, RWTH Aachen      *
00005  *                           www.openflipper.org                             *
00006  *                                                                           *
00007  *---------------------------------------------------------------------------*
00008  *  This file is part of OpenFlipper.                                        *
00009  *                                                                           *
00010  *  OpenFlipper is free software: you can redistribute it and/or modify      *
00011  *  it under the terms of the GNU Lesser General Public License as           *
00012  *  published by the Free Software Foundation, either version 3 of           *
00013  *  the License, or (at your option) any later version with the              *
00014  *  following exceptions:                                                    *
00015  *                                                                           *
00016  *  If other files instantiate templates or use macros                       *
00017  *  or inline functions from this file, or you compile this file and         *
00018  *  link it with other files to produce an executable, this file does        *
00019  *  not by itself cause the resulting executable to be covered by the        *
00020  *  GNU Lesser General Public License. This exception does not however       *
00021  *  invalidate any other reasons why the executable file might be            *
00022  *  covered by the GNU Lesser General Public License.                        *
00023  *                                                                           *
00024  *  OpenFlipper is distributed in the hope that it will be useful,           *
00025  *  but WITHOUT ANY WARRANTY; without even the implied warranty of           *
00026  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            *
00027  *  GNU Lesser General Public License for more details.                      *
00028  *                                                                           *
00029  *  You should have received a copy of the GNU LesserGeneral Public          *
00030  *  License along with OpenFlipper. If not,                                  *
00031  *  see <http://www.gnu.org/licenses/>.                                      *
00032  *                                                                           *
00033 \*===========================================================================*/
00034 
00035 /*===========================================================================*\
00036  *                                                                           *
00037  *   $Revision: 6743 $                                                       *
00038  *   $Author: moebius $                                                      *
00039  *   $Date: 2009-08-05 11:03:10 +0200 (Mi, 05. Aug 2009) $                   *
00040  *                                                                           *
00041 \*===========================================================================*/
00042 
00043 
00044 
00045 
00046 //=============================================================================
00047 //
00048 //  CLASS Material2Node - IMPLEMENTATION
00049 //
00050 //=============================================================================
00051 
00052 
00053 //== INCLUDES =================================================================
00054 
00055 
00056 #include "Material2Node.hh"
00057 
00058 
00059 //== NAMESPACES ===============================================================
00060 
00061 namespace ACG {
00062 namespace SceneGraph {
00063 
00064   
00065 //== IMPLEMENTATION ========================================================== 
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 /* _drawmode */ ) 
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 /* _drawmode*/ )
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& /* _is */ )
00293 {
00294 
00295 //   char                                  s[200];
00296 //   float                                 x, y, z, u;
00297 
00298 
00299 //   while(_is && (!_is.eof()) && _is.getline(s, 200))
00300 //   {
00301 //     // comment
00302 //     if (s[0] == '#') continue;
00303 
00304     
00305 //     // BaseColor
00306 //     else if (strncmp(s, "BaseColor ", 10) == 0)
00307 //     {
00308 //       if (sscanf(s, "BaseColor %f %f %f %f", &x, &y, &z, &u))
00309 //       {
00310 //         base_color_ = Vec4f(x,y,z,u);
00311 //       }
00312 //     }
00313 //     // AmbientColor
00314 //     else if (strncmp(s, "AmbientColor ", 13) == 0)
00315 //     {
00316 //       if (sscanf(s, "AmbientColor %f %f %f %f", &x, &y, &z, &u))
00317 //       {
00318 //         ambient_color_ = Vec4f(x,y,z,u);
00319 //       }
00320 //     }
00321 //     // DiffuseColor
00322 //     else if (strncmp(s, "DiffuseColor ", 13) == 0)
00323 //     {
00324 //       if (sscanf(s, "DiffuseColor %f %f %f %f", &x, &y, &z, &u))
00325 //       {
00326 //         diffuse_color_ = Vec4f(x,y,z,u);
00327 //       }
00328 //     }
00329 //     // SpecularColor
00330 //     else if (strncmp(s, "SpecularColor ", 14) == 0)
00331 //     {
00332 //       if (sscanf(s, "SpecularColor %f %f %f %f", &x, &y, &z, &u))
00333 //       {
00334 //         specular_color_ = Vec4f(x,y,z,u);
00335 //       }
00336 //     }
00337 //     // Shininess
00338 //     else if (strncmp(s, "Shininess ", 10) == 0)
00339 //     {
00340 //       if (sscanf(s, "Shininess %f", &x))
00341 //       {
00342 //         shininess_ = x;
00343 //       }
00344 //     }
00345 //     // PointSize
00346 //     else if (strncmp(s, "PointSize ", 10) == 0)
00347 //     {
00348 //       if (sscanf(s, "PointSize %f", &x))
00349 //       {
00350 //         point_size_ = x;
00351 //       }
00352 //     }
00353 //     // LineWidth
00354 //     else if (strncmp(s, "LineWidth ", 10) == 0)
00355 //     {
00356 //       if (sscanf(s, "LineWidth %f", &x))
00357 //       {
00358 //         line_width_ = x;
00359 //       }
00360 //     }
00361 
00362 
00363 //     s[0]=' ';
00364 //   }
00365 }
00366  
00367 //=============================================================================
00368 } // namespace SceneGraph
00369 } // namespace ACG
00370 //=============================================================================

acg pic Project OpenFlipper, ©  Computer Graphics Group, RWTH Aachen. Documentation generated using doxygen .