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 <osgDB/ReaderWriter>
49 #include <simgear/props/props.hxx>
50 #include <simgear/structure/SGSharedPtr.hxx>
51 #include <simgear/scene/util/SGSceneFeatures.hxx>
53 #include "matmodel.hxx"
58 void reload_shaders();
61 class SGMaterialGlyph;
64 * A material in the scene graph.
66 * A material represents information about a single surface type
67 * in the 3D scene graph, including texture, colour, lighting,
68 * tiling, and so on; most of the materials in FlightGear are
69 * defined in the $FG_ROOT/materials.xml file, and can be changed
72 class SGMaterial : public SGReferenced {
77 ////////////////////////////////////////////////////////////////////
78 // Public Constructors.
79 ////////////////////////////////////////////////////////////////////
82 * Construct a material from a set of properties.
84 * @param props A property node containing subnodes with the
85 * state information for the material. This node is usually
86 * loaded from the $FG_ROOT/materials.xml file.
88 SGMaterial( const osgDB::ReaderWriter::Options*, const SGPropertyNode *props);
97 ////////////////////////////////////////////////////////////////////
99 ////////////////////////////////////////////////////////////////////
102 * Get the textured state.
104 simgear::Effect *get_effect(int n = -1);
107 * Get the number of textures assigned to this material.
109 inline int get_num() const { return _status.size(); }
113 * Get the xsize of the texture, in meters.
115 inline double get_xsize() const { return xsize; }
119 * Get the ysize of the texture, in meters.
121 inline double get_ysize() const { return ysize; }
125 * Get the light coverage.
127 * A smaller number means more generated night lighting.
129 * @return The area (m^2) covered by each light.
131 inline double get_light_coverage () const { return light_coverage; }
134 * Get the wood coverage.
136 * A smaller number means more generated woods within the forest.
138 * @return The area (m^2) covered by each wood.
140 inline double get_wood_coverage () const { return wood_coverage; }
143 * Get the density of the wood
145 * @return The area (m^2) covered by each tree in the wood.
147 inline double get_tree_density () const { return tree_density; }
150 * Get the size of each wood
152 * @return the average area (m^2) of each wood
154 inline double get_wood_size () const { return wood_size; }
157 * Get the tree height.
159 * @return The average height of the trees.
161 inline double get_tree_height () const { return tree_height; }
164 * Get the tree width.
166 * @return The average width of the trees.
168 inline double get_tree_width () const { return tree_width; }
171 * Get the forest LoD range.
173 * @return The LoD range for the trees.
175 inline double get_tree_range () const { return tree_range; }
178 * Get the number of tree varieties available
180 * @return the number of different trees defined in the texture strip
182 inline int get_tree_varieties () const { return tree_varieties; }
185 * Get the texture strip to use for trees
187 * @return the texture to use for trees.
189 inline std::string get_tree_texture () const { return tree_texture; }
192 * Return if the surface material is solid, if it is not solid, a fluid
193 * can be assumed, that is usually water.
195 bool get_solid () const { return solid; }
198 * Get the friction factor for that material
200 double get_friction_factor () const { return friction_factor; }
203 * Get the rolling friction for that material
205 double get_rolling_friction () const { return rolling_friction; }
208 * Get the bumpines for that material
210 double get_bumpiness () const { return bumpiness; }
213 * Get the load resistance
215 double get_load_resistance () const { return load_resistance; }
218 * Get the list of names for this material
220 const std::vector<std::string>& get_names() const { return _names; }
223 * add the given name to the list of names this material is known
225 void add_name(const std::string& name) { _names.push_back(name); }
228 * Get the number of randomly-placed objects defined for this material.
230 int get_object_group_count () const { return object_groups.size(); }
233 * Get a randomly-placed object for this material.
235 SGMatModelGroup * get_object_group (int index) const {
236 return object_groups[index];
240 * Return pointer to glyph class, or 0 if it doesn't exist.
242 SGMaterialGlyph * get_glyph (const std::string& name) const;
244 void set_light_color(const SGVec4f& color)
245 { emission = color; }
246 const SGVec4f& get_light_color() const
249 SGVec2f get_tex_coord_scale() const
251 float tex_width = get_xsize();
252 float tex_height = get_ysize();
254 return SGVec2f((0 < tex_width) ? 1000.0f/tex_width : 1.0f,
255 (0 < tex_height) ? 1000.0f/tex_height : 1.0f);
261 ////////////////////////////////////////////////////////////////////
262 // Protected methods.
263 ////////////////////////////////////////////////////////////////////
266 * Initialization method, invoked by all public constructors.
272 struct _internal_state {
273 _internal_state(simgear::Effect *e, const std::string &t, bool l,
274 const osgDB::ReaderWriter::Options *o);
275 osg::ref_ptr<simgear::Effect> effect;
276 std::string texture_path;
277 bool effect_realized;
278 osg::ref_ptr<const osgDB::ReaderWriter::Options> options;
284 ////////////////////////////////////////////////////////////////////
286 ////////////////////////////////////////////////////////////////////
289 std::vector<_internal_state> _status;
291 // Round-robin counter
292 mutable unsigned int _current_ptr;
303 // coverage of night lighting.
304 double light_coverage;
307 double wood_coverage;
309 // The size of each wood
312 // Tree density within the wood
315 // Range at which trees become visible
318 // Height of the tree
324 // Number of varieties of tree texture
327 // True if the material is solid, false if it is a fluid
330 // the friction factor of that surface material
331 double friction_factor;
333 // the rolling friction of that surface material
334 double rolling_friction;
336 // the bumpiness of that surface material
339 // the load resistance of that surface material
340 double load_resistance;
342 // material properties
343 SGVec4f ambient, diffuse, specular, emission;
346 // effect for this material
349 // the list of names for this material. May be empty.
350 std::vector<std::string> _names;
352 std::vector<SGSharedPtr<SGMatModelGroup> > object_groups;
354 // taxiway-/runway-sign texture elements
355 std::map<std::string, SGSharedPtr<SGMaterialGlyph> > glyphs;
357 // Tree texture, typically a strip of applicable tree textures
358 std::string tree_texture;
360 ////////////////////////////////////////////////////////////////////
361 // Internal constructors and methods.
362 ////////////////////////////////////////////////////////////////////
364 void read_properties(const osgDB::ReaderWriter::Options* options,
365 const SGPropertyNode *props);
366 void buildEffectProperties(const osgDB::ReaderWriter::Options* options);
370 class SGMaterialGlyph : public SGReferenced {
372 SGMaterialGlyph(SGPropertyNode *);
373 inline double get_left() const { return _left; }
374 inline double get_right() const { return _right; }
375 inline double get_width() const { return _right - _left; }
382 class SGMaterialUserData : public osg::Referenced {
384 SGMaterialUserData(const SGMaterial* material) :
387 const SGMaterial* getMaterial() const
388 { return mMaterial; }
390 // this cannot be an SGSharedPtr since that would create a cicrular reference
391 // making it impossible to ever free the space needed by SGMaterial
392 const SGMaterial* mMaterial;
396 SGSetTextureFilter( int max);
399 SGGetTextureFilter();
401 #endif // _SG_MAT_HXX