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 <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>
46 #include <simgear/scene/util/SGSceneFeatures.hxx>
48 #include "matmodel.hxx"
55 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);
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 * Get the textured state.
119 osg::StateSet *get_state (int n = -1);
123 * Get the number of textures assigned to this material.
125 inline int get_num() const { return _status.size(); }
129 * Get the xsize of the texture, in meters.
131 inline double get_xsize() const { return xsize; }
135 * Get the ysize of the texture, in meters.
137 inline double get_ysize() const { return ysize; }
141 * Get the light coverage.
143 * A smaller number means more generated night lighting.
145 * @return The area (m^2) covered by each light.
147 inline double get_light_coverage () const { return light_coverage; }
150 * Get the forest coverage.
152 * A smaller number means more generated forest canopy.
154 * @return The area (m^2) covered by each canopy.
156 inline double get_tree_coverage () const { return tree_coverage; }
159 * Get the forest height.
161 * @return The average height of the trees.
163 inline double get_tree_height () const { return tree_height; }
166 * Get the forest width.
168 * @return The average width of the trees.
170 inline double get_tree_width () const { return tree_width; }
173 * Get the forest LoD range.
175 * @return The LoD range for the trees.
177 inline double get_tree_range () const { return tree_range; }
180 * Get the number of tree varieties available
182 * @return the number of different trees defined in the texture strip
184 inline int get_tree_varieties () const { return tree_varieties; }
187 * Get the texture strip to use for trees
189 * @return the texture to use for trees.
191 inline string get_tree_texture () const { return tree_texture; }
194 * Return if the surface material is solid, if it is not solid, a fluid
195 * can be assumed, that is usually water.
197 bool get_solid () const { return solid; }
200 * Get the friction factor for that material
202 double get_friction_factor () const { return friction_factor; }
205 * Get the rolling friction for that material
207 double get_rolling_friction () const { return rolling_friction; }
210 * Get the bumpines for that material
212 double get_bumpiness () const { return bumpiness; }
215 * Get the load resistance
217 double get_load_resistance () const { return load_resistance; }
220 * Get the list of names for this material
222 const vector<string>& get_names() const { return _names; }
225 * add the given name to the list of names this material is known
227 void add_name(const string& name) { _names.push_back(name); }
230 * Get the number of randomly-placed objects defined for this material.
232 int get_object_group_count () const { return object_groups.size(); }
235 * Get a randomly-placed object for this material.
237 SGMatModelGroup * get_object_group (int index) const {
238 return object_groups[index];
242 * Return pointer to glyph class, or 0 if it doesn't exist.
244 SGMaterialGlyph * get_glyph (const string& name) const;
246 void set_light_color(const SGVec4f& color)
247 { emission = color; }
248 const SGVec4f& get_light_color() const
251 SGVec2f get_tex_coord_scale() const
253 float tex_width = get_xsize();
254 float tex_height = get_ysize();
256 return SGVec2f((0 < tex_width) ? 1000.0f/tex_width : 1.0f,
257 (0 < tex_height) ? 1000.0f/tex_height : 1.0f);
263 ////////////////////////////////////////////////////////////////////
264 // Protected methods.
265 ////////////////////////////////////////////////////////////////////
268 * Initialization method, invoked by all public constructors.
274 struct _internal_state {
275 _internal_state( osg::StateSet *s, const string &t, bool l )
276 : state(s), texture_path(t), texture_loaded(l) {}
277 osg::ref_ptr<osg::StateSet> state;
285 ////////////////////////////////////////////////////////////////////
287 ////////////////////////////////////////////////////////////////////
290 vector<_internal_state> _status;
292 // Round-robin counter
293 mutable unsigned int _current_ptr;
304 // coverage of night lighting.
305 double light_coverage;
308 double tree_coverage;
310 // Range at which trees become visible
313 // Height of the tree
319 // Number of varieties of tree texture
322 // True if the material is solid, false if it is a fluid
325 // the friction factor of that surface material
326 double friction_factor;
328 // the rolling friction of that surface material
329 double rolling_friction;
331 // the bumpiness of that surface material
334 // the load resistance of that surface material
335 double load_resistance;
337 // material properties
338 SGVec4f ambient, diffuse, specular, emission;
341 // the list of names for this material. May be empty.
342 vector<string> _names;
344 vector<SGSharedPtr<SGMatModelGroup> > object_groups;
346 // taxiway-/runway-sign texture elements
347 map<string, SGSharedPtr<SGMaterialGlyph> > glyphs;
349 // Tree texture, typically a strip of applicable tree textures
352 ////////////////////////////////////////////////////////////////////
353 // Internal constructors and methods.
354 ////////////////////////////////////////////////////////////////////
356 SGMaterial( const string &fg_root, const SGMaterial &mat ); // unimplemented
358 void read_properties( const string &fg_root, const SGPropertyNode *props );
359 void build_state( bool defer_tex_load );
360 void set_state( osg::StateSet *s );
362 void assignTexture( osg::StateSet *state, const std::string &fname, bool _wrapu = true, bool _wrapv = true, bool _mipmap = true );
367 class SGMaterialGlyph : public SGReferenced {
369 SGMaterialGlyph(SGPropertyNode *);
370 inline double get_left() const { return _left; }
371 inline double get_right() const { return _right; }
372 inline double get_width() const { return _right - _left; }
379 class SGMaterialUserData : public osg::Referenced {
381 SGMaterialUserData(const SGMaterial* material) :
384 const SGMaterial* getMaterial() const
385 { return mMaterial; }
387 // this cannot be an SGSharedPtr since that would create a cicrular reference
388 // making it impossible to ever free the space needed by SGMaterial
389 const SGMaterial* mMaterial;
393 SGSetTextureFilter( int max);
396 SGGetTextureFilter();
398 #endif // _SG_MAT_HXX