| 1 | /**************************************************************************** |
| 2 | * VCGLib o o * |
| 3 | * Visual and Computer Graphics Library o o * |
| 4 | * _ O _ * |
| 5 | * Copyright(C) 2004 \/)\/ * |
| 6 | * Visual Computing Lab /\/| * |
| 7 | * ISTI - Italian National Research Council | * |
| 8 | * \ * |
| 9 | * All rights reserved. * |
| 10 | * * |
| 11 | * This program is free software; you can redistribute it and/or modify * |
| 12 | * it under the terms of the GNU General Public License as published by * |
| 13 | * the Free Software Foundation; either version 2 of the License, or * |
| 14 | * (at your option) any later version. * |
| 15 | * * |
| 16 | * This program is distributed in the hope that it will be useful, * |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 19 | * GNU General Public License (http://www.gnu.org/licenses/gpl.txt) * |
| 20 | * for more details. * |
| 21 | * * |
| 22 | ****************************************************************************/ |
| 23 | |
| 24 | /**************************************************************************** |
| 25 | History |
| 26 | |
| 27 | $Log: io_material.h,v $ |
| 28 | Revision 1.6 2008/02/27 23:38:40 cignoni |
| 29 | added materialname field |
| 30 | |
| 31 | Revision 1.5 2008/02/27 00:34:43 cignoni |
| 32 | corrected various bugs in the export of texture coords |
| 33 | |
| 34 | Revision 1.4 2007/06/20 10:28:04 tarini |
| 35 | "newline at end of file" and "endif" warnings fixed |
| 36 | |
| 37 | Revision 1.3 2006/11/09 07:51:44 cignoni |
| 38 | bug due to wrong access to eventually unexistent FaceColor |
| 39 | |
| 40 | Revision 1.2 2006/10/09 19:58:08 cignoni |
| 41 | Added casts to remove warnings |
| 42 | |
| 43 | Revision 1.1 2006/03/07 13:19:29 cignoni |
| 44 | First Release with OBJ import support |
| 45 | |
| 46 | Revision 1.1 2006/02/16 19:28:36 fmazzant |
| 47 | transfer of Export_3ds.h, Export_obj.h, Io_3ds_obj_material.h from Meshlab to vcg |
| 48 | |
| 49 | Revision 1.1 2006/02/06 11:04:40 fmazzant |
| 50 | added file material.h. it include struct Material, CreateNewMaterial(...) and MaterialsCompare(...) |
| 51 | |
| 52 | |
| 53 | ****************************************************************************/ |
| 54 | |
| 55 | #ifndef __VCGLIB_MATERIAL |
| 56 | #define __VCGLIB_MATERIAL |
| 57 | |
| 58 | namespace vcg { |
| 59 | namespace tri { |
| 60 | namespace io { |
| 61 | |
| 62 | /* |
| 63 | structures material |
| 64 | */ |
| 65 | struct Material |
| 66 | { |
| 67 | unsigned int index;//index of material |
| 68 | std::string materialName; |
| 69 | |
| 70 | Point3f Ka;//ambient |
| 71 | Point3f Kd;//diffuse |
| 72 | Point3f Ks;//specular |
| 73 | |
| 74 | float d;//alpha |
| 75 | float Tr;//alpha |
| 76 | |
| 77 | int illum;//specular illumination |
| 78 | float Ns; |
| 79 | |
| 80 | std::string map_Kd; //filename texture |
| 81 | }; |
| 82 | |
| 83 | |
| 84 | template <class SaveMeshType> |
| 85 | class Materials |
| 86 | { |
| 87 | public: |
| 88 | typedef typename SaveMeshType::FaceIterator FaceIterator; |
| 89 | typedef typename SaveMeshType::VertexIterator VertexIterator; |
| 90 | typedef typename SaveMeshType::VertexType VertexType; |
| 91 | |
| 92 | /* |
| 93 | creates a new meterial |
| 94 | */ |
| 95 | inline static int CreateNewMaterial(SaveMeshType &m, std::vector<Material> &materials, unsigned int index, FaceIterator &fi) |
| 96 | { |
| 97 | Point3f diffuse(1,1,1); |
| 98 | float Tr = 1; |
| 99 | if(HasPerFaceColor(m)){ |
| 100 | diffuse = Point3f((float)((*fi).C()[0])/255.0f,(float)((*fi).C()[1])/255.0f,(float)((*fi).C()[2])/255.0f);//diffuse |
| 101 | (float)((*fi).C()[3])/255.0f;//alpha |
| 102 | } |
| 103 | |
| 104 | int illum = 2; //default not use Ks! |
| 105 | float ns = 0.0; //default |
| 106 | |
| 107 | Material mtl; |
| 108 | |
| 109 | mtl.index = index;//index of materials |
| 110 | mtl.Ka = Point3f(0.2f,0.2f,0.2f);//ambient |
| 111 | mtl.Kd = diffuse;//diffuse |
| 112 | mtl.Ks = Point3f(1.0f,1.0f,1.0f);//specular |
| 113 | mtl.Tr = Tr;//alpha |
| 114 | mtl.Ns = ns; |
| 115 | mtl.illum = illum;//illumination |
| 116 | |
| 117 | if(m.textures.size() && (*fi).WT(0).n() >=0 ) |
| 118 | mtl.map_Kd = m.textures[(*fi).WT(0).n()]; |
| 119 | else |
| 120 | mtl.map_Kd = ""; |
| 121 | |
| 122 | int i = -1; |
| 123 | if((i = MaterialsCompare(materials,mtl)) == -1) |
| 124 | { |
| 125 | materials.push_back(mtl); |
| 126 | return materials.size(); |
| 127 | } |
| 128 | return i; |
| 129 | } |
| 130 | |
| 131 | /* |
| 132 | returns the index of the material if it exists inside the list of the materials, |
| 133 | otherwise it returns -1. |
| 134 | */ |
| 135 | inline static int MaterialsCompare(std::vector<Material> &materials, Material mtl) |
| 136 | { |
| 137 | for(unsigned int i=0;i<materials.size();i++) |
| 138 | { |
| 139 | if(materials[i].Kd != mtl.Kd ) continue; |
| 140 | if(materials[i].Ka != mtl.Ka ) continue; |
| 141 | if(materials[i].Ks != mtl.Ks ) continue; |
| 142 | if(materials[i].Tr != mtl.Tr ) continue; |
| 143 | if(materials[i].illum != mtl.illum ) continue; |
| 144 | if(materials[i].Ns != mtl.Ns ) continue; |
| 145 | if(materials[i].map_Kd != mtl.map_Kd) continue; |
| 146 | return i; |
| 147 | } |
| 148 | return -1; |
| 149 | } |
| 150 | }; |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | #endif //__VCGLIB_MATERIAL |