1 // mat.hxx -- a material in the scene graph.
2 // TODO: this class needs to be renamed.
4 // Written by Curtis Olson, started May 1998.
5 // Overhauled by David Megginson, December 2001
7 // Copyright (C) 1998 - 2000 Curtis L. Olson - http://www.flightgear.org/~curt
9 // This program is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU General Public License as
11 // published by the Free Software Foundation; either version 2 of the
12 // License, or (at your option) any later version.
14 // This program is distributed in the hope that it will be useful, but
15 // WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // General Public License for more details.
19 // You should have received a copy of the GNU General Public License
20 // along with this program; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
30 # error This library requires C++
33 #include <simgear/compiler.h>
35 #include STL_STRING // Standard C++ string library
39 #include <simgear/math/SGMath.hxx>
44 #include <simgear/props/props.hxx>
45 #include <simgear/structure/ssgSharedPtr.hxx>
46 #include <simgear/structure/SGSharedPtr.hxx>
48 #include "matmodel.hxx"
55 class SGMaterialGlyph;
59 * A material in the scene graph.
61 * A material represents information about a single surface type
62 * in the 3D scene graph, including texture, colour, lighting,
63 * tiling, and so on; most of the materials in FlightGear are
64 * defined in the $FG_ROOT/materials.xml file, and can be changed
67 class SGMaterial : public SGReferenced {
72 ////////////////////////////////////////////////////////////////////
73 // Public Constructors.
74 ////////////////////////////////////////////////////////////////////
77 * Construct a material from a set of properties.
79 * @param props A property node containing subnodes with the
80 * state information for the material. This node is usually
81 * loaded from the $FG_ROOT/materials.xml file.
83 SGMaterial( const string &fg_root, const SGPropertyNode *props, const char *season );
87 * Construct a material from an absolute texture path.
89 * @param texture_path A string containing an absolute path
90 * to a texture file (usually RGB).
92 SGMaterial( const string &texpath );
96 * Construct a material around an existing SSG state.
98 * This constructor allows the application to create a custom,
99 * low-level state for the scene graph and wrap a material around
100 * it. Note: the pointer ownership is transferred to the material.
102 * @param s The SSG state for this material.
104 SGMaterial( ssgSimpleState *s );
113 ////////////////////////////////////////////////////////////////////
115 ////////////////////////////////////////////////////////////////////
118 * Force the texture to load if it hasn't already.
120 * @return true if the texture loaded, false if it was loaded
123 bool load_texture (int n = -1);
127 * Get the textured state.
129 ssgSimpleState *get_state (int n = -1) const;
133 * Get the number of textures assigned to this material.
135 inline int get_num() const { return _status.size(); }
139 * Get the xsize of the texture, in meters.
141 inline double get_xsize() const { return xsize; }
145 * Get the ysize of the texture, in meters.
147 inline double get_ysize() const { return ysize; }
151 * Get the light coverage.
153 * A smaller number means more generated night lighting.
155 * @return The area (m^2?) covered by each light.
157 inline double get_light_coverage () const { return light_coverage; }
160 * Return if the surface material is solid, if it is not solid, a fluid
161 * can be assumed, that is usually water.
163 bool get_solid () const { return solid; }
166 * Get the friction factor for that material
168 double get_friction_factor () const { return friction_factor; }
171 * Get the rolling friction for that material
173 double get_rolling_friction () const { return rolling_friction; }
176 * Get the bumpines for that material
178 double get_bumpiness () const { return bumpiness; }
181 * Get the load resistence
183 double get_load_resistence () const { return load_resistence; }
186 * Get the list of names for this material
188 const vector<string>& get_names() const { return _names; }
191 * add the given name to the list of names this material is known
193 void add_name(const string& name) { _names.push_back(name); }
196 * Get the number of randomly-placed objects defined for this material.
198 int get_object_group_count () const { return object_groups.size(); }
201 * Get a randomly-placed object for this material.
203 SGMatModelGroup * get_object_group (int index) const {
204 return object_groups[index];
208 * Return pointer to glyph class, or 0 if it doesn't exist.
210 SGMaterialGlyph * get_glyph (const string& name) const {
211 map<string, SGSharedPtr<SGMaterialGlyph> >::const_iterator it = glyphs.find(name);
212 return it != glyphs.end() ? it->second : 0;
218 ////////////////////////////////////////////////////////////////////
219 // Protected methods.
220 ////////////////////////////////////////////////////////////////////
223 * Initialization method, invoked by all public constructors.
229 struct _internal_state {
230 _internal_state( ssgSimpleState *s, const string &t, bool l )
231 : state(s), texture_path(t), texture_loaded(l) {}
232 ssgSharedPtr<ssgSimpleState> state;
240 ////////////////////////////////////////////////////////////////////
242 ////////////////////////////////////////////////////////////////////
245 vector<_internal_state> _status;
247 // Round-robin counter
248 unsigned int _current_ptr;
259 // coverage of night lighting.
260 double light_coverage;
262 // True if the material is solid, false if it is a fluid
265 // the friction factor of that surface material
266 double friction_factor;
268 // the rolling friction of that surface material
269 double rolling_friction;
271 // the bumpiness of that surface material
274 // the load resistence of that surface material
275 double load_resistence;
277 // material properties
278 SGVec4f ambient, diffuse, specular, emission;
281 // the list of names for this material. May be empty.
282 vector<string> _names;
284 vector<SGSharedPtr<SGMatModelGroup> > object_groups;
286 // taxiway-/runway-sign texture elements
287 map<string, SGSharedPtr<SGMaterialGlyph> > glyphs;
290 ////////////////////////////////////////////////////////////////////
291 // Internal constructors and methods.
292 ////////////////////////////////////////////////////////////////////
294 SGMaterial( const string &fg_root, const SGMaterial &mat ); // unimplemented
296 void read_properties( const string &fg_root, const SGPropertyNode *props, const char *season );
297 void build_ssg_state( bool defer_tex_load );
298 void set_ssg_state( ssgSimpleState *s );
300 void assignTexture( ssgSimpleState *state, string &fname, int _wrapu = TRUE, int _wrapv = TRUE, int _mipmap = TRUE );
305 class SGMaterialGlyph : public SGReferenced {
307 SGMaterialGlyph(SGPropertyNode *);
308 inline double get_left() const { return _left; }
309 inline double get_right() const { return _right; }
310 inline double get_width() const { return _right - _left; }
317 class SGMaterialUserData : public ssgBase {
319 SGMaterialUserData(const SGMaterial* material) :
322 const SGMaterial* getMaterial() const
323 { return mMaterial; }
325 SGSharedPtr<const SGMaterial> mMaterial;
328 #endif // _SG_MAT_HXX