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
40 #include <simgear/scene/tgdb/SGTexturedTriangleBin.hxx>
42 #include <osg/ref_ptr>
43 #include <osg/Texture2D>
50 #include <simgear/scene/util/SGReaderWriterOptions.hxx>
51 #include <simgear/props/props.hxx>
52 #include <simgear/structure/SGSharedPtr.hxx>
53 #include <simgear/scene/util/SGSceneFeatures.hxx>
54 #include <simgear/props/condition.hxx>
56 #include "matmodel.hxx"
61 void reload_shaders();
64 class SGMaterialGlyph;
67 * A material in the scene graph.
69 * A material represents information about a single surface type
70 * in the 3D scene graph, including texture, colour, lighting,
71 * tiling, and so on; most of the materials in FlightGear are
72 * defined in the $FG_ROOT/materials.xml file, and can be changed
75 class SGMaterial : public SGReferenced {
80 ////////////////////////////////////////////////////////////////////
81 // Public Constructors.
82 ////////////////////////////////////////////////////////////////////
85 * Construct a material from a set of properties.
87 * @param props A property node containing subnodes with the
88 * state information for the material. This node is usually
89 * loaded from the $FG_ROOT/materials.xml file.
91 SGMaterial( const osgDB::Options*,
92 const SGPropertyNode *props,
93 SGPropertyNode *prop_root);
95 SGMaterial(const simgear::SGReaderWriterOptions*,
96 const SGPropertyNode *props,
97 SGPropertyNode *prop_root);
105 ////////////////////////////////////////////////////////////////////
107 ////////////////////////////////////////////////////////////////////
110 * Get the textured state.
112 simgear::Effect* get_effect(SGTexturedTriangleBin triangleBin);
113 simgear::Effect* get_effect();
116 * Get the textured state.
118 osg::Texture2D* get_object_mask(SGTexturedTriangleBin triangleBin);
122 * Get the number of textures assigned to this material.
124 inline int get_num() const { return _status.size(); }
128 * Get the xsize of the texture, in meters.
130 inline double get_xsize() const { return xsize; }
134 * Get the ysize of the texture, in meters.
136 inline double get_ysize() const { return ysize; }
140 * Get the light coverage.
142 * A smaller number means more generated night lighting.
144 * @return The area (m^2) covered by each light.
146 inline double get_light_coverage () const { return light_coverage; }
149 * Get the building coverage.
151 * A smaller number means more generated buildings.
153 * @return The area (m^2) covered by each light.
155 inline double get_building_coverage () const { return building_coverage; }
158 * Get the building spacing.
160 * This is the minimum spacing between buildings
162 * @return The minimum distance between buildings
164 inline double get_building_spacing () const { return building_spacing; }
167 * Get the building texture.
169 * This is the texture used for auto-generated buildings.
171 * @return The texture for auto-generated buildings.
173 inline std::string get_building_texture () const { return building_texture; }
176 * Get the building lightmap.
178 * This is the lightmap used for auto-generated buildings.
180 * @return The lightmap for auto-generated buildings.
182 inline std::string get_building_lightmap () const { return building_lightmap; }
184 // Ratio of the 3 random building sizes
185 inline double get_building_small_fraction () const { return building_small_ratio / (building_small_ratio + building_medium_ratio + building_large_ratio); }
186 inline double get_building_medium_fraction () const { return building_medium_ratio / (building_small_ratio + building_medium_ratio + building_large_ratio); }
187 inline double get_building_large_fraction () const { return building_large_ratio / (building_small_ratio + building_medium_ratio + building_large_ratio); }
189 // Proportion of buildings with pitched roofs
190 inline double get_building_small_pitch () const { return building_small_pitch; }
191 inline double get_building_medium_pitch () const { return building_medium_pitch; }
192 inline double get_building_large_pitch () const { return building_large_pitch; }
194 // Min/Max number of floors for each size
195 inline int get_building_small_min_floors () const { return building_small_min_floors; }
196 inline int get_building_small_max_floors () const { return building_small_max_floors; }
197 inline int get_building_medium_min_floors () const { return building_medium_min_floors; }
198 inline int get_building_medium_max_floors () const { return building_medium_max_floors; }
199 inline int get_building_large_min_floors () const { return building_large_min_floors; }
200 inline int get_building_large_max_floors () const { return building_large_max_floors; }
202 // Minimum width and depth for each size
203 inline double get_building_small_min_width () const { return building_small_min_width; }
204 inline double get_building_small_max_width () const { return building_small_max_width; }
205 inline double get_building_small_min_depth () const { return building_small_min_depth; }
206 inline double get_building_small_max_depth () const { return building_small_max_depth; }
208 inline double get_building_medium_min_width () const { return building_medium_min_width; }
209 inline double get_building_medium_max_width () const { return building_medium_max_width; }
210 inline double get_building_medium_min_depth () const { return building_medium_min_depth; }
211 inline double get_building_medium_max_depth () const { return building_medium_max_depth; }
213 inline double get_building_large_min_width () const { return building_large_min_width; }
214 inline double get_building_large_max_width () const { return building_large_max_width; }
215 inline double get_building_large_min_depth () const { return building_large_min_depth; }
216 inline double get_building_large_max_depth () const { return building_large_max_depth; }
219 * Get the wood coverage.
221 * A smaller number means more generated woods within the forest.
223 * @return The area (m^2) covered by each wood.
225 inline double get_wood_coverage () const { return wood_coverage; }
228 * Get the tree height.
230 * @return The average height of the trees.
232 inline double get_tree_height () const { return tree_height; }
235 * Get the tree width.
237 * @return The average width of the trees.
239 inline double get_tree_width () const { return tree_width; }
242 * Get the forest LoD range.
244 * @return The LoD range for the trees.
246 inline double get_tree_range () const { return tree_range; }
249 * Get the number of tree varieties available
251 * @return the number of different trees defined in the texture strip
253 inline int get_tree_varieties () const { return tree_varieties; }
256 * Get the texture strip to use for trees
258 * @return the texture to use for trees.
260 inline std::string get_tree_texture () const { return tree_texture; }
263 * Return if the surface material is solid, if it is not solid, a fluid
264 * can be assumed, that is usually water.
266 bool get_solid () const { return solid; }
269 * Get the friction factor for that material
271 double get_friction_factor () const { return friction_factor; }
274 * Get the rolling friction for that material
276 double get_rolling_friction () const { return rolling_friction; }
279 * Get the bumpines for that material
281 double get_bumpiness () const { return bumpiness; }
284 * Get the load resistance
286 double get_load_resistance () const { return load_resistance; }
289 * Get the list of names for this material
291 const std::vector<std::string>& get_names() const { return _names; }
294 * add the given name to the list of names this material is known
296 void add_name(const std::string& name) { _names.push_back(name); }
299 * Get the number of randomly-placed objects defined for this material.
301 int get_object_group_count () const { return object_groups.size(); }
304 * Get a randomly-placed object for this material.
306 SGMatModelGroup * get_object_group (int index) const {
307 return object_groups[index];
311 * Evaluate whether this material is valid given the current global
316 return condition->test();
323 * Return pointer to glyph class, or 0 if it doesn't exist.
325 SGMaterialGlyph * get_glyph (const std::string& name) const;
327 void set_light_color(const SGVec4f& color)
328 { emission = color; }
329 const SGVec4f& get_light_color() const
332 SGVec2f get_tex_coord_scale() const
334 float tex_width = get_xsize();
335 float tex_height = get_ysize();
337 return SGVec2f((0 < tex_width) ? 1000.0f/tex_width : 1.0f,
338 (0 < tex_height) ? 1000.0f/tex_height : 1.0f);
344 ////////////////////////////////////////////////////////////////////
345 // Protected methods.
346 ////////////////////////////////////////////////////////////////////
349 * Initialization method, invoked by all public constructors.
355 struct _internal_state {
356 _internal_state(simgear::Effect *e, bool l,
357 const simgear::SGReaderWriterOptions *o);
358 _internal_state(simgear::Effect *e, const std::string &t, bool l,
359 const simgear::SGReaderWriterOptions *o);
360 void add_texture(const std::string &t, int i);
361 osg::ref_ptr<simgear::Effect> effect;
362 std::vector<std::pair<std::string,int> > texture_paths;
363 bool effect_realized;
364 osg::ref_ptr<const simgear::SGReaderWriterOptions> options;
370 ////////////////////////////////////////////////////////////////////
372 ////////////////////////////////////////////////////////////////////
375 std::vector<_internal_state> _status;
386 // coverage of night lighting.
387 double light_coverage;
389 // coverage of buildings
390 double building_coverage;
393 double building_spacing;
395 // building texture & lightmap
396 std::string building_texture;
397 std::string building_lightmap;
399 // Ratio of the 3 random building sizes
400 double building_small_ratio;
401 double building_medium_ratio;
402 double building_large_ratio;
404 // Proportion of buildings with pitched roofs
405 double building_small_pitch;
406 double building_medium_pitch;
407 double building_large_pitch;
409 // Min/Max number of floors for each size
410 int building_small_min_floors;
411 int building_small_max_floors;
412 int building_medium_min_floors;
413 int building_medium_max_floors;
414 int building_large_min_floors;
415 int building_large_max_floors;
417 // Minimum width and depth for each size
418 double building_small_min_width;
419 double building_small_max_width;
420 double building_small_min_depth;
421 double building_small_max_depth;
423 double building_medium_min_width;
424 double building_medium_max_width;
425 double building_medium_min_depth;
426 double building_medium_max_depth;
428 double building_large_min_width;
429 double building_large_max_width;
430 double building_large_min_depth;
431 double building_large_max_depth;
434 double wood_coverage;
436 // Range at which trees become visible
439 // Height of the tree
445 // Number of varieties of tree texture
448 // True if the material is solid, false if it is a fluid
451 // the friction factor of that surface material
452 double friction_factor;
454 // the rolling friction of that surface material
455 double rolling_friction;
457 // the bumpiness of that surface material
460 // the load resistance of that surface material
461 double load_resistance;
463 // material properties
464 SGVec4f ambient, diffuse, specular, emission;
467 // effect for this material
470 // the list of names for this material. May be empty.
471 std::vector<std::string> _names;
473 std::vector<SGSharedPtr<SGMatModelGroup> > object_groups;
475 // taxiway-/runway-sign texture elements
476 std::map<std::string, SGSharedPtr<SGMaterialGlyph> > glyphs;
478 // Tree texture, typically a strip of applicable tree textures
479 std::string tree_texture;
481 // Object mask, a simple RGB texture used as a mask when placing
482 // random vegetation, objects and buildings
483 std::vector<osg::Texture2D*> _masks;
485 // Condition, indicating when this material is active
486 SGSharedPtr<const SGCondition> condition;
488 // Parameters from the materials file
489 const SGPropertyNode* parameters;
491 ////////////////////////////////////////////////////////////////////
492 // Internal constructors and methods.
493 ////////////////////////////////////////////////////////////////////
495 void read_properties(const simgear::SGReaderWriterOptions* options,
496 const SGPropertyNode *props,
497 SGPropertyNode *prop_root);
498 void buildEffectProperties(const simgear::SGReaderWriterOptions* options);
499 simgear::Effect* get_effect(int i);
503 class SGMaterialGlyph : public SGReferenced {
505 SGMaterialGlyph(SGPropertyNode *);
506 inline double get_left() const { return _left; }
507 inline double get_right() const { return _right; }
508 inline double get_width() const { return _right - _left; }
515 class SGMaterialUserData : public osg::Referenced {
517 SGMaterialUserData(const SGMaterial* material) :
520 const SGMaterial* getMaterial() const
521 { return mMaterial; }
523 // this cannot be an SGSharedPtr since that would create a cicrular reference
524 // making it impossible to ever free the space needed by SGMaterial
525 const SGMaterial* mMaterial;
529 SGSetTextureFilter( int max);
532 SGGetTextureFilter();
534 #endif // _SG_MAT_HXX