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 #ifndef __VCGLIB_MATERIAL
00053 #define __VCGLIB_MATERIAL
00054
00055 namespace vcg {
00056 namespace tri {
00057 namespace io {
00058
00059
00060
00061
00062 struct Material
00063 {
00064 unsigned int index;
00065 std::string materialName;
00066
00067 Point3f Ka;
00068 Point3f Kd;
00069 Point3f Ks;
00070
00071 float d;
00072 float Tr;
00073
00074 int illum;
00075 float Ns;
00076
00077 std::string map_Kd;
00078 };
00079
00080
00081 template <class SaveMeshType>
00082 class Materials
00083 {
00084 public:
00085 typedef typename SaveMeshType::FaceIterator FaceIterator;
00086 typedef typename SaveMeshType::VertexIterator VertexIterator;
00087 typedef typename SaveMeshType::VertexType VertexType;
00088
00089
00090
00091
00092 inline static int CreateNewMaterial(SaveMeshType &m, std::vector<Material> &materials, unsigned int index, FaceIterator &fi)
00093 {
00094 Point3f diffuse(1,1,1);
00095 float Transp = 1;
00096 if(HasPerFaceColor(m)){
00097 diffuse = Point3f((float)((*fi).C()[0])/255.0f,(float)((*fi).C()[1])/255.0f,(float)((*fi).C()[2])/255.0f);
00098 Transp = (float)((*fi).C()[3])/255.0f;
00099 }
00100
00101 int illum = 2;
00102 float ns = 0.0;
00103
00104 Material mtl;
00105
00106 mtl.index = index;
00107 mtl.Ka = Point3f(0.2f,0.2f,0.2f);
00108 mtl.Kd = diffuse;
00109 mtl.Ks = Point3f(1.0f,1.0f,1.0f);
00110 mtl.Tr = Transp;
00111 mtl.Ns = ns;
00112 mtl.illum = illum;
00113
00114 if(m.textures.size() && (*fi).WT(0).n() >=0 )
00115 mtl.map_Kd = m.textures[(*fi).WT(0).n()];
00116 else
00117 mtl.map_Kd = "";
00118
00119 int i = -1;
00120 if((i = MaterialsCompare(materials,mtl)) == -1)
00121 {
00122 materials.push_back(mtl);
00123 return materials.size();
00124 }
00125 return i;
00126 }
00127
00128
00129
00130
00131
00132 inline static int MaterialsCompare(std::vector<Material> &materials, Material mtl)
00133 {
00134 for(unsigned int i=0;i<materials.size();i++)
00135 {
00136 if(materials[i].Kd != mtl.Kd ) continue;
00137 if(materials[i].Ka != mtl.Ka ) continue;
00138 if(materials[i].Ks != mtl.Ks ) continue;
00139 if(materials[i].Tr != mtl.Tr ) continue;
00140 if(materials[i].illum != mtl.illum ) continue;
00141 if(materials[i].Ns != mtl.Ns ) continue;
00142 if(materials[i].map_Kd != mtl.map_Kd) continue;
00143 return i;
00144 }
00145 return -1;
00146 }
00147 };
00148 }
00149 }
00150 }
00151 #endif //__VCGLIB_MATERIAL