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>
46 typedef osg::ref_ptr<osg::Texture2D> Texture2DRef;
48 #include <simgear/structure/SGSharedPtr.hxx>
49 #include <simgear/threads/SGThread.hxx> // for SGMutex
50 #include <simgear/math/SGMath.hxx>
51 #include <simgear/bvh/BVHMaterial.hxx>
56 void reload_shaders();
57 class SGReaderWriterOptions;
60 class SGMatModelGroup;
63 class SGMaterialGlyph;
64 class SGTexturedTriangleBin;
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 simgear::BVHMaterial {
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(const SGTexturedTriangleBin& triangleBin);
113 simgear::Effect* get_effect();
116 * Get the textured state.
118 osg::Texture2D* get_object_mask(const 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; }
218 inline double get_building_range () const { return building_range; }
220 inline double get_cos_object_max_density_slope_angle () const { return cos_object_max_density_slope_angle; }
221 inline double get_cos_object_zero_density_slope_angle () const { return cos_object_zero_density_slope_angle; }
224 * Get the wood coverage.
226 * A smaller number means more generated woods within the forest.
228 * @return The area (m^2) covered by each wood.
230 inline double get_wood_coverage () const { return wood_coverage; }
233 * Get the tree height.
235 * @return The average height of the trees.
237 inline double get_tree_height () const { return tree_height; }
240 * Get the tree width.
242 * @return The average width of the trees.
244 inline double get_tree_width () const { return tree_width; }
247 * Get the forest LoD range.
249 * @return The LoD range for the trees.
251 inline double get_tree_range () const { return tree_range; }
254 * Get the number of tree varieties available
256 * @return the number of different trees defined in the texture strip
258 inline int get_tree_varieties () const { return tree_varieties; }
261 * Get the texture strip to use for trees
263 * @return the texture to use for trees.
265 inline std::string get_tree_texture () const { return tree_texture; }
268 * Get the cosine of the maximum tree density slope angle. We
269 * use the cosine as it can be compared directly to the z component
270 * of a triangle normal.
272 * @return the cosine of the maximum tree density slope angle.
274 inline double get_cos_tree_max_density_slope_angle () const { return cos_tree_max_density_slope_angle; }
277 * Get the cosine of the maximum tree density slope angle. We
278 * use the cosine as it can be compared directly to the z component
279 * of a triangle normal.
281 * @return the cosine of the maximum tree density slope angle.
283 inline double get_cos_tree_zero_density_slope_angle () const { return cos_tree_zero_density_slope_angle; }
286 * Get the list of names for this material
288 const std::vector<std::string>& get_names() const { return _names; }
291 * add the given name to the list of names this material is known
293 void add_name(const std::string& name) { _names.push_back(name); }
296 * Get the number of randomly-placed objects defined for this material.
298 int get_object_group_count () const { return object_groups.size(); }
301 * Get a randomly-placed object for this material.
303 SGMatModelGroup * get_object_group (int index) const {
304 return object_groups[index];
308 * Evaluate whether this material is valid given the current global
314 * Return pointer to glyph class, or 0 if it doesn't exist.
316 SGMaterialGlyph * get_glyph (const std::string& name) const;
318 void set_light_color(const SGVec4f& color)
319 { emission = color; }
320 const SGVec4f& get_light_color() const
323 SGVec2f get_tex_coord_scale() const
325 float tex_width = get_xsize();
326 float tex_height = get_ysize();
328 return SGVec2f((0 < tex_width) ? 1000.0f/tex_width : 1.0f,
329 (0 < tex_height) ? 1000.0f/tex_height : 1.0f);
335 ////////////////////////////////////////////////////////////////////
336 // Protected methods.
337 ////////////////////////////////////////////////////////////////////
340 * Initialization method, invoked by all public constructors.
346 struct _internal_state {
347 _internal_state(simgear::Effect *e, bool l,
348 const simgear::SGReaderWriterOptions *o);
349 _internal_state(simgear::Effect *e, const std::string &t, bool l,
350 const simgear::SGReaderWriterOptions *o);
351 void add_texture(const std::string &t, int i);
352 osg::ref_ptr<simgear::Effect> effect;
353 std::vector<std::pair<std::string,int> > texture_paths;
354 bool effect_realized;
355 osg::ref_ptr<const simgear::SGReaderWriterOptions> options;
361 ////////////////////////////////////////////////////////////////////
363 ////////////////////////////////////////////////////////////////////
366 std::vector<_internal_state> _status;
377 // coverage of night lighting.
378 double light_coverage;
380 // coverage of buildings
381 double building_coverage;
384 double building_spacing;
386 // building texture & lightmap
387 std::string building_texture;
388 std::string building_lightmap;
390 // Ratio of the 3 random building sizes
391 double building_small_ratio;
392 double building_medium_ratio;
393 double building_large_ratio;
395 // Proportion of buildings with pitched roofs
396 double building_small_pitch;
397 double building_medium_pitch;
398 double building_large_pitch;
400 // Min/Max number of floors for each size
401 int building_small_min_floors;
402 int building_small_max_floors;
403 int building_medium_min_floors;
404 int building_medium_max_floors;
405 int building_large_min_floors;
406 int building_large_max_floors;
408 // Minimum width and depth for each size
409 double building_small_min_width;
410 double building_small_max_width;
411 double building_small_min_depth;
412 double building_small_max_depth;
414 double building_medium_min_width;
415 double building_medium_max_width;
416 double building_medium_min_depth;
417 double building_medium_max_depth;
419 double building_large_min_width;
420 double building_large_max_width;
421 double building_large_min_depth;
422 double building_large_max_depth;
424 double building_range;
426 // Cosine of the angle of maximum and zero density,
427 // used to stop buildings and random objects from being
428 // created on too steep a slope.
429 double cos_object_max_density_slope_angle;
430 double cos_object_zero_density_slope_angle;
433 double wood_coverage;
435 // Range at which trees become visible
438 // Height of the tree
444 // Number of varieties of tree texture
447 // cosine of the tile angle of maximum and zero density,
448 // used to stop trees from being created on too steep a slope.
449 double cos_tree_max_density_slope_angle;
450 double cos_tree_zero_density_slope_angle;
452 // material properties
453 SGVec4f ambient, diffuse, specular, emission;
456 // effect for this material
459 // the list of names for this material. May be empty.
460 std::vector<std::string> _names;
462 std::vector<SGSharedPtr<SGMatModelGroup> > object_groups;
464 // taxiway-/runway-sign texture elements
465 std::map<std::string, SGSharedPtr<SGMaterialGlyph> > glyphs;
467 // Tree texture, typically a strip of applicable tree textures
468 std::string tree_texture;
470 // Object mask, a simple RGB texture used as a mask when placing
471 // random vegetation, objects and buildings
472 std::vector<Texture2DRef> _masks;
474 // Condition, indicating when this material is active
475 SGSharedPtr<const SGCondition> condition;
477 // Parameters from the materials file
478 const SGPropertyNode* parameters;
480 // per-material lock for entrypoints called from multiple threads
483 ////////////////////////////////////////////////////////////////////
484 // Internal constructors and methods.
485 ////////////////////////////////////////////////////////////////////
487 void read_properties(const simgear::SGReaderWriterOptions* options,
488 const SGPropertyNode *props,
489 SGPropertyNode *prop_root);
490 void buildEffectProperties(const simgear::SGReaderWriterOptions* options);
491 simgear::Effect* get_effect(int i);
495 class SGMaterialGlyph : public SGReferenced {
497 SGMaterialGlyph(SGPropertyNode *);
498 inline double get_left() const { return _left; }
499 inline double get_right() const { return _right; }
500 inline double get_width() const { return _right - _left; }
507 class SGMaterialUserData : public osg::Referenced {
509 SGMaterialUserData(const SGMaterial* material) :
512 const SGMaterial* getMaterial() const
513 { return mMaterial; }
515 // this cannot be an SGSharedPtr since that would create a cicrular reference
516 // making it impossible to ever free the space needed by SGMaterial
517 const SGMaterial* mMaterial;
521 SGSetTextureFilter( int max);
524 SGGetTextureFilter();
526 #endif // _SG_MAT_HXX