File: io_material.h

package info (click to toggle)
meshlab 2020.09%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 45,132 kB
  • sloc: cpp: 400,238; ansic: 31,952; javascript: 1,578; sh: 387; yacc: 238; lex: 139; python: 86; makefile: 30
file content (143 lines) | stat: -rw-r--r-- 4,977 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/****************************************************************************
* VCGLib                                                            o o     *
* Visual and Computer Graphics Library                            o     o   *
*                                                                _   O  _   *
* Copyright(C) 2004-2016                                           \/)\/    *
* Visual Computing Lab                                            /\/|      *
* ISTI - Italian National Research Council                           |      *
*                                                                    \      *
* All rights reserved.                                                      *
*                                                                           *
* This program is free software; you can redistribute it and/or modify      *   
* it under the terms of the GNU General Public License as published by      *
* the Free Software Foundation; either version 2 of the License, or         *
* (at your option) any later version.                                       *
*                                                                           *
* This program is distributed in the hope that it will be useful,           *
* but WITHOUT ANY WARRANTY; without even the implied warranty of            *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
* GNU General Public License (http://www.gnu.org/licenses/gpl.txt)          *
* for more details.                                                         *
*                                                                           *
****************************************************************************/

/****************************************************************************
  History
  
 $Log: not supported by cvs2svn $
 Revision 1.5  2008/02/27 00:34:43  cignoni
 corrected various bugs in the export of texture coords
 
 Revision 1.4  2007/06/20 10:28:04  tarini
 "newline at end of file" and  "endif" warnings fixed
 
 Revision 1.3  2006/11/09 07:51:44  cignoni
 bug due to wrong access to eventually unexistent FaceColor
 
 Revision 1.2  2006/10/09 19:58:08  cignoni
 Added casts to remove warnings
 
 Revision 1.1  2006/03/07 13:19:29  cignoni
 First Release with OBJ import support
 
 Revision 1.1  2006/02/16 19:28:36  fmazzant
 transfer of Export_3ds.h, Export_obj.h, Io_3ds_obj_material.h from Meshlab to vcg
 
 Revision 1.1  2006/02/06 11:04:40  fmazzant
 added file material.h. it include struct Material, CreateNewMaterial(...) and MaterialsCompare(...)
 
 
 ****************************************************************************/

#ifndef __VCGLIB_MATERIAL
#define __VCGLIB_MATERIAL

#include <string>
#include <vector>
#include <vcg/space/point3.h>

namespace vcg {
namespace tri {
namespace io {

/*
    structures material
  */
struct Material
{
  unsigned int index=-1;//index of material
  std::string materialName;
  
  Point3f Ka=Point3f(0.2f, 0.2f, 0.2f);//ambient
  Point3f Kd=Point3f(1.0f, 1.0f, 1.0f);//diffuse
  Point3f Ks=Point3f(1.0f, 1.0f, 1.0f);//specular
  
  float d;//alpha
  float Tr=1.0f;//alpha
  
  int illum=2;//specular illumination
  float Ns=0.f;
  
  std::string map_Kd; //filename texture
};


template <class SaveMeshType>
class Materials
{
public:	
  typedef typename SaveMeshType::FaceIterator FaceIterator;
  typedef typename SaveMeshType::VertexIterator VertexIterator;
  typedef typename SaveMeshType::VertexType VertexType;
  
  /*
      creates a new meterial
    */
  inline static int CreateNewMaterial(SaveMeshType &m, std::vector<Material> &materials, FaceIterator &fi)
  {			
    Material mtl;
            
    if(HasPerFaceColor(m)){
      mtl.Kd = Point3f((float)((*fi).C()[0])/255.0f,(float)((*fi).C()[1])/255.0f,(float)((*fi).C()[2])/255.0f);//diffuse
      mtl.Tr = (float)((*fi).C()[3])/255.0f;//alpha
    }
    
    if(m.textures.size() && (*fi).WT(0).n() >=0 ) 
      mtl.map_Kd = m.textures[(*fi).WT(0).n()];
    else
      mtl.map_Kd = "";
    
    int matInd = MaterialsCompare(materials,mtl);
    if(matInd == -1)
    {
      mtl.index = int(materials.size());
      materials.push_back(mtl);
      return mtl.index;
    }
    return matInd;
  }
  
  /*
      returns the index of the material if it exists inside the list of the materials, 
      otherwise it returns -1.
    */
  inline static int MaterialsCompare(std::vector<Material> &materials, Material mtl)
  {
    for(unsigned int i=0;i<materials.size();i++)
    {
      if(materials[i].Kd     != mtl.Kd    ) continue;
      if(materials[i].Ka     != mtl.Ka    ) continue;
      if(materials[i].Ks     != mtl.Ks    ) continue;
      if(materials[i].Tr     != mtl.Tr    ) continue;
      if(materials[i].illum  != mtl.illum ) continue;
      if(materials[i].Ns     != mtl.Ns    ) continue;
      if(materials[i].map_Kd != mtl.map_Kd) continue;
      return i;
    }
    return -1;
  }
};
}
}
}
#endif //__VCGLIB_MATERIAL