Go to the documentation of this file.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 #include <string>
00056 #include <vector>
00057 #include <vcg/space/point3.h>
00058
00059 namespace vcg {
00060 namespace tri {
00061 namespace io {
00062
00063
00064
00065
00066 struct Material
00067 {
00068 unsigned int index;
00069 std::string materialName;
00070
00071 Point3f Ka;
00072 Point3f Kd;
00073 Point3f Ks;
00074
00075 float d;
00076 float Tr;
00077
00078 int illum;
00079 float Ns;
00080
00081 std::string map_Kd;
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
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);
00102 Transp = (float)((*fi).C()[3])/255.0f;
00103 }
00104
00105 int illum = 2;
00106 float ns = 0.0;
00107
00108 Material mtl;
00109
00110 mtl.index = index;
00111 mtl.Ka = Point3f(0.2f,0.2f,0.2f);
00112 mtl.Kd = diffuse;
00113 mtl.Ks = Point3f(1.0f,1.0f,1.0f);
00114 mtl.Tr = Transp;
00115 mtl.Ns = ns;
00116 mtl.illum = illum;
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
00134
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