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.
29 #include <simgear/compiler.h>
31 #include <string> // Standard C++ string library
37 #include <osg/ref_ptr>
38 #include <osg/Texture2D>
45 #include <simgear/structure/SGSharedPtr.hxx>
46 #include <simgear/threads/SGThread.hxx> // for SGMutex
47 #include <simgear/math/SGLimits.hxx>
48 #include <simgear/math/SGMisc.hxx>
49 #include <simgear/math/SGMath.hxx>
50 #include <simgear/math/SGVec2.hxx>
51 #include <simgear/math/SGRect.hxx>
52 #include <simgear/bvh/BVHMaterial.hxx>
54 typedef osg::ref_ptr<osg::Texture2D> Texture2DRef;
55 typedef std::vector<SGRect <float> > AreaList;
60 void reload_shaders();
61 class SGReaderWriterOptions;
64 class SGMatModelGroup;
67 class SGMaterialGlyph;
68 class SGTexturedTriangleBin;
71 * A material in the scene graph.
73 * A material represents information about a single surface type
74 * in the 3D scene graph, including texture, colour, lighting,
75 * tiling, and so on; most of the materials in FlightGear are
76 * defined in the $FG_ROOT/materials.xml file, and can be changed
79 class SGMaterial : public simgear::BVHMaterial {
84 ////////////////////////////////////////////////////////////////////
85 // Public Constructors.
86 ////////////////////////////////////////////////////////////////////
89 * Construct a material from a set of properties.
91 * @param props A property node containing subnodes with the
92 * state information for the material. This node is usually
93 * loaded from the $FG_ROOT/materials.xml file.
95 SGMaterial(const osgDB::Options*,
96 const SGPropertyNode *props,
97 SGPropertyNode *prop_root,
99 SGSharedPtr<const SGCondition> c);
102 SGMaterial(const simgear::SGReaderWriterOptions*,
103 const SGPropertyNode *props,
104 SGPropertyNode *prop_root,
106 SGSharedPtr<const SGCondition> c);
115 ////////////////////////////////////////////////////////////////////
117 ////////////////////////////////////////////////////////////////////
120 * Get the textured state.
122 simgear::Effect* get_one_effect(int texIndex);
123 simgear::Effect* get_effect();
126 * Get the textured state.
128 osg::Texture2D* get_one_object_mask(int texIndex);
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 * Get the building coverage.
161 * A smaller number means more generated buildings.
163 * @return The area (m^2) covered by each light.
165 inline double get_building_coverage () const { return building_coverage; }
168 * Get the building spacing.
170 * This is the minimum spacing between buildings
172 * @return The minimum distance between buildings
174 inline double get_building_spacing () const { return building_spacing; }
177 * Get the building texture.
179 * This is the texture used for auto-generated buildings.
181 * @return The texture for auto-generated buildings.
183 inline std::string get_building_texture () const { return building_texture; }
186 * Get the building lightmap.
188 * This is the lightmap used for auto-generated buildings.
190 * @return The lightmap for auto-generated buildings.
192 inline std::string get_building_lightmap () const { return building_lightmap; }
194 // Ratio of the 3 random building sizes
195 inline double get_building_small_fraction () const { return building_small_ratio / (building_small_ratio + building_medium_ratio + building_large_ratio); }
196 inline double get_building_medium_fraction () const { return building_medium_ratio / (building_small_ratio + building_medium_ratio + building_large_ratio); }
197 inline double get_building_large_fraction () const { return building_large_ratio / (building_small_ratio + building_medium_ratio + building_large_ratio); }
199 // Proportion of buildings with pitched roofs
200 inline double get_building_small_pitch () const { return building_small_pitch; }
201 inline double get_building_medium_pitch () const { return building_medium_pitch; }
202 inline double get_building_large_pitch () const { return building_large_pitch; }
204 // Min/Max number of floors for each size
205 inline int get_building_small_min_floors () const { return building_small_min_floors; }
206 inline int get_building_small_max_floors () const { return building_small_max_floors; }
207 inline int get_building_medium_min_floors () const { return building_medium_min_floors; }
208 inline int get_building_medium_max_floors () const { return building_medium_max_floors; }
209 inline int get_building_large_min_floors () const { return building_large_min_floors; }
210 inline int get_building_large_max_floors () const { return building_large_max_floors; }
212 // Minimum width and depth for each size
213 inline double get_building_small_min_width () const { return building_small_min_width; }
214 inline double get_building_small_max_width () const { return building_small_max_width; }
215 inline double get_building_small_min_depth () const { return building_small_min_depth; }
216 inline double get_building_small_max_depth () const { return building_small_max_depth; }
218 inline double get_building_medium_min_width () const { return building_medium_min_width; }
219 inline double get_building_medium_max_width () const { return building_medium_max_width; }
220 inline double get_building_medium_min_depth () const { return building_medium_min_depth; }
221 inline double get_building_medium_max_depth () const { return building_medium_max_depth; }
223 inline double get_building_large_min_width () const { return building_large_min_width; }
224 inline double get_building_large_max_width () const { return building_large_max_width; }
225 inline double get_building_large_min_depth () const { return building_large_min_depth; }
226 inline double get_building_large_max_depth () const { return building_large_max_depth; }
228 inline double get_building_range () const { return building_range; }
230 inline double get_cos_object_max_density_slope_angle () const { return cos_object_max_density_slope_angle; }
231 inline double get_cos_object_zero_density_slope_angle () const { return cos_object_zero_density_slope_angle; }
234 * Get the wood coverage.
236 * A smaller number means more generated woods within the forest.
238 * @return The area (m^2) covered by each wood.
240 inline double get_wood_coverage () const { return wood_coverage; }
243 * Get the tree height.
245 * @return The average height of the trees.
247 inline double get_tree_height () const { return tree_height; }
250 * Get the tree width.
252 * @return The average width of the trees.
254 inline double get_tree_width () const { return tree_width; }
257 * Get the forest LoD range.
259 * @return The LoD range for the trees.
261 inline double get_tree_range () const { return tree_range; }
264 * Get the number of tree varieties available
266 * @return the number of different trees defined in the texture strip
268 inline int get_tree_varieties () const { return tree_varieties; }
271 * Get the texture strip to use for trees
273 * @return the texture to use for trees.
275 inline std::string get_tree_texture () const { return tree_texture; }
279 * Get the effect file name to use for trees
281 * @return the effect to use for this set of trees.
283 inline std::string get_tree_effect () const { return tree_effect; }
286 * Get the cosine of the maximum tree density slope angle. We
287 * use the cosine as it can be compared directly to the z component
288 * of a triangle normal.
290 * @return the cosine of the maximum tree density slope angle.
292 inline double get_cos_tree_max_density_slope_angle () const { return cos_tree_max_density_slope_angle; }
295 * Get the cosine of the maximum tree density slope angle. We
296 * use the cosine as it can be compared directly to the z component
297 * of a triangle normal.
299 * @return the cosine of the maximum tree density slope angle.
301 inline double get_cos_tree_zero_density_slope_angle () const { return cos_tree_zero_density_slope_angle; }
304 * Get the list of names for this material
306 const std::vector<std::string>& get_names() const { return _names; }
309 * add the given name to the list of names this material is known
311 void add_name(const std::string& name) { _names.push_back(name); }
314 * Get the number of randomly-placed objects defined for this material.
316 int get_object_group_count () const { return object_groups.size(); }
319 * Get a randomly-placed object for this material.
321 SGMatModelGroup * get_object_group (int index) const {
322 return object_groups[index];
326 * Evaluate whether this material is valid given the current global
327 * property state and the tile location.
329 bool valid(SGVec2f loc) const;
332 * Return pointer to glyph class, or 0 if it doesn't exist.
334 SGMaterialGlyph * get_glyph (const std::string& name) const;
336 void set_light_color(const SGVec4f& color)
337 { emission = color; }
338 const SGVec4f& get_light_color() const
341 SGVec2f get_tex_coord_scale() const
343 float tex_width = get_xsize();
344 float tex_height = get_ysize();
346 return SGVec2f((0 < tex_width) ? 1000.0f/tex_width : 1.0f,
347 (0 < tex_height) ? 1000.0f/tex_height : 1.0f);
353 ////////////////////////////////////////////////////////////////////
354 // Protected methods.
355 ////////////////////////////////////////////////////////////////////
358 * Initialization method, invoked by all public constructors.
364 struct _internal_state {
365 _internal_state(simgear::Effect *e, bool l,
366 const simgear::SGReaderWriterOptions *o);
367 _internal_state(simgear::Effect *e, const std::string &t, bool l,
368 const simgear::SGReaderWriterOptions *o);
369 void add_texture(const std::string &t, int i);
370 osg::ref_ptr<simgear::Effect> effect;
371 std::vector<std::pair<std::string,int> > texture_paths;
372 bool effect_realized;
373 osg::ref_ptr<const simgear::SGReaderWriterOptions> options;
379 ////////////////////////////////////////////////////////////////////
381 ////////////////////////////////////////////////////////////////////
384 std::vector<_internal_state> _status;
395 // coverage of night lighting.
396 double light_coverage;
398 // coverage of buildings
399 double building_coverage;
402 double building_spacing;
404 // building texture & lightmap
405 std::string building_texture;
406 std::string building_lightmap;
408 // Ratio of the 3 random building sizes
409 double building_small_ratio;
410 double building_medium_ratio;
411 double building_large_ratio;
413 // Proportion of buildings with pitched roofs
414 double building_small_pitch;
415 double building_medium_pitch;
416 double building_large_pitch;
418 // Min/Max number of floors for each size
419 int building_small_min_floors;
420 int building_small_max_floors;
421 int building_medium_min_floors;
422 int building_medium_max_floors;
423 int building_large_min_floors;
424 int building_large_max_floors;
426 // Minimum width and depth for each size
427 double building_small_min_width;
428 double building_small_max_width;
429 double building_small_min_depth;
430 double building_small_max_depth;
432 double building_medium_min_width;
433 double building_medium_max_width;
434 double building_medium_min_depth;
435 double building_medium_max_depth;
437 double building_large_min_width;
438 double building_large_max_width;
439 double building_large_min_depth;
440 double building_large_max_depth;
442 double building_range;
444 // Cosine of the angle of maximum and zero density,
445 // used to stop buildings and random objects from being
446 // created on too steep a slope.
447 double cos_object_max_density_slope_angle;
448 double cos_object_zero_density_slope_angle;
451 double wood_coverage;
453 // Range at which trees become visible
456 // Height of the tree
462 // Number of varieties of tree texture
465 // cosine of the tile angle of maximum and zero density,
466 // used to stop trees from being created on too steep a slope.
467 double cos_tree_max_density_slope_angle;
468 double cos_tree_zero_density_slope_angle;
470 // material properties
471 SGVec4f ambient, diffuse, specular, emission;
474 // effect for this material
477 // the list of names for this material. May be empty.
478 std::vector<std::string> _names;
480 std::vector<SGSharedPtr<SGMatModelGroup> > object_groups;
482 // taxiway-/runway-sign texture elements
483 std::map<std::string, SGSharedPtr<SGMaterialGlyph> > glyphs;
485 // Tree texture, typically a strip of applicable tree textures
486 std::string tree_texture;
488 // Tree effect to be used for a particular material
489 std::string tree_effect;
491 // Object mask, a simple RGB texture used as a mask when placing
492 // random vegetation, objects and buildings
493 std::vector<Texture2DRef> _masks;
495 // Condition, indicating when this material is active
496 SGSharedPtr<const SGCondition> condition;
498 // List of geographical rectangles for this material
501 // Parameters from the materials file
502 const SGPropertyNode* parameters;
504 // per-material lock for entrypoints called from multiple threads
507 ////////////////////////////////////////////////////////////////////
508 // Internal constructors and methods.
509 ////////////////////////////////////////////////////////////////////
511 void read_properties(const simgear::SGReaderWriterOptions* options,
512 const SGPropertyNode *props,
513 SGPropertyNode *prop_root);
514 void buildEffectProperties(const simgear::SGReaderWriterOptions* options);
515 simgear::Effect* get_effect(int i);
519 class SGMaterialGlyph : public SGReferenced {
521 SGMaterialGlyph(SGPropertyNode *);
522 inline double get_left() const { return _left; }
523 inline double get_right() const { return _right; }
524 inline double get_width() const { return _right - _left; }
531 class SGMaterialUserData : public osg::Referenced {
533 SGMaterialUserData(const SGMaterial* material) :
536 const SGMaterial* getMaterial() const
537 { return mMaterial; }
539 // this cannot be an SGSharedPtr since that would create a cicrular reference
540 // making it impossible to ever free the space needed by SGMaterial
541 const SGMaterial* mMaterial;
545 SGSetTextureFilter( int max);
548 SGGetTextureFilter();
550 #endif // _SG_MAT_HXX