Developer Documentation

Material.hh
1 /*===========================================================================*\
2 * *
3 * OpenFlipper *
4 * Copyright (c) 2001-2015, RWTH-Aachen University *
5 * Department of Computer Graphics and Multimedia *
6 * All rights reserved. *
7 * www.openflipper.org *
8 * *
9 *---------------------------------------------------------------------------*
10 * This file is part of OpenFlipper. *
11 *---------------------------------------------------------------------------*
12 * *
13 * Redistribution and use in source and binary forms, with or without *
14 * modification, are permitted provided that the following conditions *
15 * are met: *
16 * *
17 * 1. Redistributions of source code must retain the above copyright notice, *
18 * this list of conditions and the following disclaimer. *
19 * *
20 * 2. Redistributions in binary form must reproduce the above copyright *
21 * notice, this list of conditions and the following disclaimer in the *
22 * documentation and/or other materials provided with the distribution. *
23 * *
24 * 3. Neither the name of the copyright holder nor the names of its *
25 * contributors may be used to endorse or promote products derived from *
26 * this software without specific prior written permission. *
27 * *
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39 * *
40 \*===========================================================================*/
41
42 #pragma once
43
44 #ifndef DOXY_IGNORE_THIS
45
46 #include <iomanip>
47 #include <map>
48 #include <string>
49
55 class Material
56 {
57 public:
58
59 Material() :
60 localNum_(0),
61 Kd_is_set_(false),
62 Ka_is_set_(false),
63 Ks_is_set_(false),
64 Ke_is_set_(false),
65 Ni_is_set_(false),
66 Ns_is_set_(false),
67 illum_is_set_(false),
68 Tr_(1.0), // fully opaque
69 Tr_is_set_(false),
70 map_Kd_(""),
71 index_Kd_(0),
72 map_Kd_is_set_(false)
73 {
74 }
75
76
77
78 void cleanup() {
79 localNum_ = 0;
80 Kd_is_set_ = false;
81 Ka_is_set_ = false;
82 Ks_is_set_ = false;
83 Ke_is_set_ = false;
84 Ni_is_set_ = false;
85 Ns_is_set_ = false;
86 illum_is_set_ = false;
87 Tr_ = 0.0;
88 Tr_is_set_ = false;
89 map_Kd_ = "";
90 index_Kd_ = 0;
91 map_Kd_is_set_ = false;
92 }
93
94 bool is_valid(void) const
95 { return Kd_is_set_ || Ka_is_set_ || Ks_is_set_ || Tr_is_set_; }
96
98 bool has_Kd(void) { return Kd_is_set_; }
99
101 bool has_Ka(void) { return Ka_is_set_; }
102
104 bool has_Ks(void) { return Ks_is_set_; }
105
107 bool has_Ke(void) { return Ke_is_set_; }
108
110 bool has_Ni(void) { return Ni_is_set_; }
111
113 bool has_Ns(void) { return Ns_is_set_; }
114
116 bool has_illum(void) { return illum_is_set_; }
117
119 bool has_Tr(void) { return Tr_is_set_; }
120
122 bool has_Texture() const { return map_Kd_is_set_; }
123
125 bool operator== (Material& _mat) {
126 if( _mat.Kd_ != Kd_ ||
127 _mat.Ka_ != Ka_ ||
128 _mat.Ks_ != Ks_ ||
129 _mat.Ke_ != Ke_ ||
130 _mat.Ni_ != Ni_ ||
131 _mat.Ns_ != Ns_ ||
132 _mat.Tr_ != Tr_ ||
133 _mat.map_Kd_ != map_Kd_ ||
134 _mat.index_Kd_ != index_Kd_ )
135 return false;
136 return true;
137 }
138
140 bool operator!= (Material& _mat) { return !(*this == _mat); }
141
143 int material_number() const { return localNum_; }
144 void material_number(unsigned int _n) { localNum_ = _n; }
145
147 void set_Kd( float r, float g, float b )
148 { Kd_=OpenMesh::Vec3f(r,g,b); Kd_is_set_=true; }
149
151 void set_Ka( float r, float g, float b )
152 { Ka_=OpenMesh::Vec3f(r,g,b); Ka_is_set_=true; }
153
155 void set_Ks( float r, float g, float b )
156 { Ks_=OpenMesh::Vec3f(r,g,b); Ks_is_set_=true; }
157
159 void set_Ke( float r, float g, float b )
160 { Ke_=OpenMesh::Vec3f(r,g,b); Ke_is_set_=true; }
161
163 void set_Ni( float t )
164 { Ni_=t; Ni_is_set_=true; }
165
167 void set_Ns( float t )
168 { Ns_=t; Ns_is_set_=true; }
169
171 void set_illum( int t )
172 { illum_=t; illum_is_set_=true; }
173
175 void set_Tr( float t )
176 { Tr_=t; Tr_is_set_=true; }
177
179 void set_map_Kd( std::string _name, int _index_Kd )
180 { map_Kd_ = _name, index_Kd_ = _index_Kd; map_Kd_is_set_ = true; }
181
183 const OpenMesh::Vec3f& Kd( void ) const { return Kd_; }
184
186 const OpenMesh::Vec3f& Ka( void ) const { return Ka_; }
187
189 const OpenMesh::Vec3f& Ks( void ) const { return Ks_; }
190
192 const OpenMesh::Vec3f& Ke( void ) const { return Ke_; }
193
195 float Ni( void ) const { return Ni_; }
196
198 float Ns( void ) const { return Ns_; }
199
201 int illum( void ) const { return illum_; }
202
204 bool isRefractive(void) const {return (illum_ == 4 || illum_ == 6 || illum_ == 7 || illum_ == 9);}
205
207 float Tr( void ) const { return Tr_; }
208
210 const std::string& map_Kd( void ) { return map_Kd_ ; }
211
213 const int& map_Kd_index( void ) { return index_Kd_ ; }
214
215 private:
216
217 unsigned int localNum_;
218 OpenMesh::Vec3f Kd_; bool Kd_is_set_; // diffuse
219 OpenMesh::Vec3f Ka_; bool Ka_is_set_; // ambient
220 OpenMesh::Vec3f Ks_; bool Ks_is_set_; // specular
221 OpenMesh::Vec3f Ke_; bool Ke_is_set_; // emission
222 float Ni_; bool Ni_is_set_; // index of refraction
223 float Ns_; bool Ns_is_set_; // shininess
224 int illum_; bool illum_is_set_; // illumination model
225 float Tr_; bool Tr_is_set_; // transperency
226
227 std::string map_Kd_; int index_Kd_; bool map_Kd_is_set_; // Texture
228 };
229
230 inline std::ostream&
231 operator<< (std::ostream& _os, const Material& _mat) {
232 _os << "mat" << std::setfill('0') << std::setw(3) << _mat.material_number();
233
234 return _os;
235 }
236
237 typedef std::map<std::string, Material> MaterialList;
238
239 #endif
240
OpenMesh::Vec3f
VectorT< float, 3 > Vec3f
Definition: Vector11T.hh:811
OpenMesh::VectorT< float, 3 >