io_material.h
Go to the documentation of this file.
00001 /****************************************************************************
00002 * VCGLib                                                            o o     *
00003 * Visual and Computer Graphics Library                            o     o   *
00004 *                                                                _   O  _   *
00005 * Copyright(C) 2004                                                \/)\/    *
00006 * Visual Computing Lab                                            /\/|      *
00007 * ISTI - Italian National Research Council                           |      *
00008 *                                                                    \      *
00009 * All rights reserved.                                                      *
00010 *                                                                           *
00011 * This program is free software; you can redistribute it and/or modify      *   
00012 * it under the terms of the GNU General Public License as published by      *
00013 * the Free Software Foundation; either version 2 of the License, or         *
00014 * (at your option) any later version.                                       *
00015 *                                                                           *
00016 * This program is distributed in the hope that it will be useful,           *
00017 * but WITHOUT ANY WARRANTY; without even the implied warranty of            *
00018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
00019 * GNU General Public License (http://www.gnu.org/licenses/gpl.txt)          *
00020 * for more details.                                                         *
00021 *                                                                           *
00022 ****************************************************************************/
00023 
00024 /****************************************************************************
00025   History
00026 
00027  $Log: not supported by cvs2svn $
00028  Revision 1.5  2008/02/27 00:34:43  cignoni
00029  corrected various bugs in the export of texture coords
00030 
00031  Revision 1.4  2007/06/20 10:28:04  tarini
00032  "newline at end of file" and  "endif" warnings fixed
00033 
00034  Revision 1.3  2006/11/09 07:51:44  cignoni
00035  bug due to wrong access to eventually unexistent FaceColor
00036 
00037  Revision 1.2  2006/10/09 19:58:08  cignoni
00038  Added casts to remove warnings
00039 
00040  Revision 1.1  2006/03/07 13:19:29  cignoni
00041  First Release with OBJ import support
00042 
00043  Revision 1.1  2006/02/16 19:28:36  fmazzant
00044  transfer of Export_3ds.h, Export_obj.h, Io_3ds_obj_material.h from Meshlab to vcg
00045 
00046  Revision 1.1  2006/02/06 11:04:40  fmazzant
00047  added file material.h. it include struct Material, CreateNewMaterial(...) and MaterialsCompare(...)
00048 
00049 
00050  ****************************************************************************/
00051 
00052 #ifndef __VCGLIB_MATERIAL
00053 #define __VCGLIB_MATERIAL
00054 
00055 #include <string>
00056 #include <vector>
00057 #include <vcg/space/point3.h>
00058 
00059 namespace vcg {
00060 namespace tri {
00061 namespace io {
00062         
00063         /*
00064                 structures material
00065         */
00066         struct Material
00067         {
00068                 unsigned int index;//index of material
00069                 std::string materialName;
00070 
00071                 Point3f Ka;//ambient
00072                 Point3f Kd;//diffuse
00073                 Point3f Ks;//specular
00074                 
00075                 float d;//alpha
00076                 float Tr;//alpha
00077                 
00078                 int illum;//specular illumination
00079                 float Ns;
00080 
00081                 std::string map_Kd; //filename texture
00082         };
00083 
00084         
00085         template <class SaveMeshType>
00086         class Materials
00087         {
00088         public: 
00089                 typedef typename SaveMeshType::FaceIterator FaceIterator;
00090                 typedef typename SaveMeshType::VertexIterator VertexIterator;
00091                 typedef typename SaveMeshType::VertexType VertexType;
00092         
00093                 /*
00094                         creates a new meterial
00095                 */
00096                 inline static int CreateNewMaterial(SaveMeshType &m, std::vector<Material> &materials, unsigned int index, FaceIterator &fi)
00097                 {                       
00098                         Point3f diffuse(1,1,1);
00099       float Transp = 1;
00100       if(HasPerFaceColor(m)){
00101         diffuse = Point3f((float)((*fi).C()[0])/255.0f,(float)((*fi).C()[1])/255.0f,(float)((*fi).C()[2])/255.0f);//diffuse
00102                           Transp = (float)((*fi).C()[3])/255.0f;//alpha
00103       }
00104                         
00105                         int illum = 2; //default not use Ks!
00106                         float ns = 0.0; //default
00107 
00108                         Material mtl;
00109 
00110                         mtl.index = index;//index of materials
00111                         mtl.Ka = Point3f(0.2f,0.2f,0.2f);//ambient
00112                         mtl.Kd = diffuse;//diffuse
00113                         mtl.Ks = Point3f(1.0f,1.0f,1.0f);//specular
00114                         mtl.Tr = Transp;//alpha
00115                         mtl.Ns = ns;
00116                         mtl.illum = illum;//illumination
00117                         
00118                         if(m.textures.size() && (*fi).WT(0).n() >=0 ) 
00119                                 mtl.map_Kd = m.textures[(*fi).WT(0).n()];
00120                         else
00121                                 mtl.map_Kd = "";
00122                         
00123                         int i = -1;
00124                         if((i = MaterialsCompare(materials,mtl)) == -1)
00125                         {
00126                                 materials.push_back(mtl);
00127                                 return materials.size();
00128                         }
00129                         return i;
00130                 }
00131 
00132                 /*
00133                         returns the index of the material if it exists inside the list of the materials, 
00134                         otherwise it returns -1.
00135                 */
00136                 inline static int MaterialsCompare(std::vector<Material> &materials, Material mtl)
00137                 {
00138                         for(unsigned int i=0;i<materials.size();i++)
00139                         {
00140                                 if(materials[i].Kd     != mtl.Kd    ) continue;
00141                                 if(materials[i].Ka     != mtl.Ka    ) continue;
00142                                 if(materials[i].Ks     != mtl.Ks    ) continue;
00143                                 if(materials[i].Tr     != mtl.Tr    ) continue;
00144                                 if(materials[i].illum  != mtl.illum ) continue;
00145                                 if(materials[i].Ns     != mtl.Ns    ) continue;
00146                                 if(materials[i].map_Kd != mtl.map_Kd) continue;
00147                                 return i;
00148                         }
00149                         return -1;
00150                 }
00151         };
00152 }
00153 }
00154 }
00155 #endif //__VCGLIB_MATERIAL


shape_reconstruction
Author(s): Roberto Martín-Martín
autogenerated on Sat Jun 8 2019 18:32:10