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>
41 #include <osg/ref_ptr>
42 #include <osg/StateSet>
44 #include <simgear/props/props.hxx>
45 #include <simgear/structure/SGSharedPtr.hxx>
47 #include "matmodel.hxx"
54 class SGMaterialGlyph;
58 * A material in the scene graph.
60 * A material represents information about a single surface type
61 * in the 3D scene graph, including texture, colour, lighting,
62 * tiling, and so on; most of the materials in FlightGear are
63 * defined in the $FG_ROOT/materials.xml file, and can be changed
66 class SGMaterial : public SGReferenced {
71 ////////////////////////////////////////////////////////////////////
72 // Public Constructors.
73 ////////////////////////////////////////////////////////////////////
76 * Construct a material from a set of properties.
78 * @param props A property node containing subnodes with the
79 * state information for the material. This node is usually
80 * loaded from the $FG_ROOT/materials.xml file.
82 SGMaterial( const string &fg_root, const SGPropertyNode *props, const char *season );
86 * Construct a material from an absolute texture path.
88 * @param texture_path A string containing an absolute path
89 * to a texture file (usually RGB).
91 SGMaterial( const string &texpath );
95 * Construct a material around an existing state.
97 * This constructor allows the application to create a custom,
98 * low-level state for the scene graph and wrap a material around
99 * it. Note: the pointer ownership is transferred to the material.
101 * @param s The state for this material.
103 SGMaterial( osg::StateSet *s );
112 ////////////////////////////////////////////////////////////////////
114 ////////////////////////////////////////////////////////////////////
117 * Force the texture to load if it hasn't already.
119 * @return true if the texture loaded, false if it was loaded
122 bool load_texture (int n = -1);
126 * Get the textured state.
128 osg::StateSet *get_state (int n = -1) const;
132 * Get the number of textures assigned to this material.
134 inline int get_num() const { return _status.size(); }
138 * Get the xsize of the texture, in meters.
140 inline double get_xsize() const { return xsize; }
144 * Get the ysize of the texture, in meters.
146 inline double get_ysize() const { return ysize; }
150 * Get the light coverage.
152 * A smaller number means more generated night lighting.
154 * @return The area (m^2?) covered by each light.
156 inline double get_light_coverage () const { return light_coverage; }
159 * Return if the surface material is solid, if it is not solid, a fluid
160 * can be assumed, that is usually water.
162 bool get_solid () const { return solid; }
165 * Get the friction factor for that material
167 double get_friction_factor () const { return friction_factor; }
170 * Get the rolling friction for that material
172 double get_rolling_friction () const { return rolling_friction; }
175 * Get the bumpines for that material
177 double get_bumpiness () const { return bumpiness; }
180 * Get the load resistence
182 double get_load_resistence () const { return load_resistence; }
185 * Get the list of names for this material
187 const vector<string>& get_names() const { return _names; }
190 * add the given name to the list of names this material is known
192 void add_name(const string& name) { _names.push_back(name); }
195 * Get the number of randomly-placed objects defined for this material.
197 int get_object_group_count () const { return object_groups.size(); }
200 * Get a randomly-placed object for this material.
202 SGMatModelGroup * get_object_group (int index) const {
203 return object_groups[index];
207 * Return pointer to glyph class, or 0 if it doesn't exist.
209 SGMaterialGlyph * get_glyph (const string& name) const;
214 ////////////////////////////////////////////////////////////////////
215 // Protected methods.
216 ////////////////////////////////////////////////////////////////////
219 * Initialization method, invoked by all public constructors.
225 struct _internal_state {
226 _internal_state( osg::StateSet *s, const string &t, bool l )
227 : state(s), texture_path(t), texture_loaded(l) {}
228 osg::ref_ptr<osg::StateSet> state;
236 ////////////////////////////////////////////////////////////////////
238 ////////////////////////////////////////////////////////////////////
241 vector<_internal_state> _status;
243 // Round-robin counter
244 mutable unsigned int _current_ptr;
255 // use anisotropic filtering
258 // coverage of night lighting.
259 double light_coverage;
261 // True if the material is solid, false if it is a fluid
264 // the friction factor of that surface material
265 double friction_factor;
267 // the rolling friction of that surface material
268 double rolling_friction;
270 // the bumpiness of that surface material
273 // the load resistence of that surface material
274 double load_resistence;
276 // material properties
277 SGVec4f ambient, diffuse, specular, emission;
280 // the list of names for this material. May be empty.
281 vector<string> _names;
283 vector<SGSharedPtr<SGMatModelGroup> > object_groups;
285 // taxiway-/runway-sign texture elements
286 map<string, SGSharedPtr<SGMaterialGlyph> > glyphs;
289 ////////////////////////////////////////////////////////////////////
290 // Internal constructors and methods.
291 ////////////////////////////////////////////////////////////////////
293 SGMaterial( const string &fg_root, const SGMaterial &mat ); // unimplemented
295 void read_properties( const string &fg_root, const SGPropertyNode *props, const char *season );
296 void build_state( bool defer_tex_load );
297 void set_state( osg::StateSet *s );
299 void assignTexture( osg::StateSet *state, const std::string &fname, int _wrapu = TRUE, int _wrapv = TRUE, int _mipmap = TRUE, float filtering = 1.0f );
304 class SGMaterialGlyph : public SGReferenced {
306 SGMaterialGlyph(SGPropertyNode *);
307 inline double get_left() const { return _left; }
308 inline double get_right() const { return _right; }
309 inline double get_width() const { return _right - _left; }
316 class SGMaterialUserData : public osg::Referenced {
318 SGMaterialUserData(const SGMaterial* material) :
321 const SGMaterial* getMaterial() const
322 { return mMaterial; }
324 SGSharedPtr<const SGMaterial> mMaterial;
327 #endif // _SG_MAT_HXX