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/SGMath.hxx>
48 #include <simgear/bvh/BVHMaterial.hxx>
53 void reload_shaders();
54 class SGReaderWriterOptions;
57 class SGMatModelGroup;
60 class SGMaterialGlyph;
61 class SGTexturedTriangleBin;
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 simgear::BVHMaterial {
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::Options*,
89 const SGPropertyNode *props,
90 SGPropertyNode *prop_root);
92 SGMaterial(const simgear::SGReaderWriterOptions*,
93 const SGPropertyNode *props,
94 SGPropertyNode *prop_root);
102 ////////////////////////////////////////////////////////////////////
104 ////////////////////////////////////////////////////////////////////
107 * Get the textured state.
109 simgear::Effect* get_effect(const SGTexturedTriangleBin& triangleBin);
110 simgear::Effect* get_effect();
113 * Get the textured state.
115 osg::Texture2D* get_object_mask(const SGTexturedTriangleBin& triangleBin);
119 * Get the number of textures assigned to this material.
121 inline int get_num() const { return _status.size(); }
125 * Get the xsize of the texture, in meters.
127 inline double get_xsize() const { return xsize; }
131 * Get the ysize of the texture, in meters.
133 inline double get_ysize() const { return ysize; }
137 * Get the light coverage.
139 * A smaller number means more generated night lighting.
141 * @return The area (m^2) covered by each light.
143 inline double get_light_coverage () const { return light_coverage; }
146 * Get the building coverage.
148 * A smaller number means more generated buildings.
150 * @return The area (m^2) covered by each light.
152 inline double get_building_coverage () const { return building_coverage; }
155 * Get the building spacing.
157 * This is the minimum spacing between buildings
159 * @return The minimum distance between buildings
161 inline double get_building_spacing () const { return building_spacing; }
164 * Get the building texture.
166 * This is the texture used for auto-generated buildings.
168 * @return The texture for auto-generated buildings.
170 inline std::string get_building_texture () const { return building_texture; }
173 * Get the building lightmap.
175 * This is the lightmap used for auto-generated buildings.
177 * @return The lightmap for auto-generated buildings.
179 inline std::string get_building_lightmap () const { return building_lightmap; }
181 // Ratio of the 3 random building sizes
182 inline double get_building_small_fraction () const { return building_small_ratio / (building_small_ratio + building_medium_ratio + building_large_ratio); }
183 inline double get_building_medium_fraction () const { return building_medium_ratio / (building_small_ratio + building_medium_ratio + building_large_ratio); }
184 inline double get_building_large_fraction () const { return building_large_ratio / (building_small_ratio + building_medium_ratio + building_large_ratio); }
186 // Proportion of buildings with pitched roofs
187 inline double get_building_small_pitch () const { return building_small_pitch; }
188 inline double get_building_medium_pitch () const { return building_medium_pitch; }
189 inline double get_building_large_pitch () const { return building_large_pitch; }
191 // Min/Max number of floors for each size
192 inline int get_building_small_min_floors () const { return building_small_min_floors; }
193 inline int get_building_small_max_floors () const { return building_small_max_floors; }
194 inline int get_building_medium_min_floors () const { return building_medium_min_floors; }
195 inline int get_building_medium_max_floors () const { return building_medium_max_floors; }
196 inline int get_building_large_min_floors () const { return building_large_min_floors; }
197 inline int get_building_large_max_floors () const { return building_large_max_floors; }
199 // Minimum width and depth for each size
200 inline double get_building_small_min_width () const { return building_small_min_width; }
201 inline double get_building_small_max_width () const { return building_small_max_width; }
202 inline double get_building_small_min_depth () const { return building_small_min_depth; }
203 inline double get_building_small_max_depth () const { return building_small_max_depth; }
205 inline double get_building_medium_min_width () const { return building_medium_min_width; }
206 inline double get_building_medium_max_width () const { return building_medium_max_width; }
207 inline double get_building_medium_min_depth () const { return building_medium_min_depth; }
208 inline double get_building_medium_max_depth () const { return building_medium_max_depth; }
210 inline double get_building_large_min_width () const { return building_large_min_width; }
211 inline double get_building_large_max_width () const { return building_large_max_width; }
212 inline double get_building_large_min_depth () const { return building_large_min_depth; }
213 inline double get_building_large_max_depth () const { return building_large_max_depth; }
215 inline double get_building_range () const { return building_range; }
217 inline double get_cos_object_max_density_slope_angle () const { return cos_object_max_density_slope_angle; }
218 inline double get_cos_object_zero_density_slope_angle () const { return cos_object_zero_density_slope_angle; }
221 * Get the wood coverage.
223 * A smaller number means more generated woods within the forest.
225 * @return The area (m^2) covered by each wood.
227 inline double get_wood_coverage () const { return wood_coverage; }
230 * Get the tree height.
232 * @return The average height of the trees.
234 inline double get_tree_height () const { return tree_height; }
237 * Get the tree width.
239 * @return The average width of the trees.
241 inline double get_tree_width () const { return tree_width; }
244 * Get the forest LoD range.
246 * @return The LoD range for the trees.
248 inline double get_tree_range () const { return tree_range; }
251 * Get the number of tree varieties available
253 * @return the number of different trees defined in the texture strip
255 inline int get_tree_varieties () const { return tree_varieties; }
258 * Get the texture strip to use for trees
260 * @return the texture to use for trees.
262 inline std::string get_tree_texture () const { return tree_texture; }
265 * Get the cosine of the maximum tree density slope angle. We
266 * use the cosine as it can be compared directly to the z component
267 * of a triangle normal.
269 * @return the cosine of the maximum tree density slope angle.
271 inline double get_cos_tree_max_density_slope_angle () const { return cos_tree_max_density_slope_angle; }
274 * Get the cosine of the maximum tree density slope angle. We
275 * use the cosine as it can be compared directly to the z component
276 * of a triangle normal.
278 * @return the cosine of the maximum tree density slope angle.
280 inline double get_cos_tree_zero_density_slope_angle () const { return cos_tree_zero_density_slope_angle; }
283 * Get the list of names for this material
285 const std::vector<std::string>& get_names() const { return _names; }
288 * add the given name to the list of names this material is known
290 void add_name(const std::string& name) { _names.push_back(name); }
293 * Get the number of randomly-placed objects defined for this material.
295 int get_object_group_count () const { return object_groups.size(); }
298 * Get a randomly-placed object for this material.
300 SGMatModelGroup * get_object_group (int index) const {
301 return object_groups[index];
305 * Evaluate whether this material is valid given the current global
311 * Return pointer to glyph class, or 0 if it doesn't exist.
313 SGMaterialGlyph * get_glyph (const std::string& name) const;
315 void set_light_color(const SGVec4f& color)
316 { emission = color; }
317 const SGVec4f& get_light_color() const
320 SGVec2f get_tex_coord_scale() const
322 float tex_width = get_xsize();
323 float tex_height = get_ysize();
325 return SGVec2f((0 < tex_width) ? 1000.0f/tex_width : 1.0f,
326 (0 < tex_height) ? 1000.0f/tex_height : 1.0f);
332 ////////////////////////////////////////////////////////////////////
333 // Protected methods.
334 ////////////////////////////////////////////////////////////////////
337 * Initialization method, invoked by all public constructors.
343 struct _internal_state {
344 _internal_state(simgear::Effect *e, bool l,
345 const simgear::SGReaderWriterOptions *o);
346 _internal_state(simgear::Effect *e, const std::string &t, bool l,
347 const simgear::SGReaderWriterOptions *o);
348 void add_texture(const std::string &t, int i);
349 osg::ref_ptr<simgear::Effect> effect;
350 std::vector<std::pair<std::string,int> > texture_paths;
351 bool effect_realized;
352 osg::ref_ptr<const simgear::SGReaderWriterOptions> options;
358 ////////////////////////////////////////////////////////////////////
360 ////////////////////////////////////////////////////////////////////
363 std::vector<_internal_state> _status;
374 // coverage of night lighting.
375 double light_coverage;
377 // coverage of buildings
378 double building_coverage;
381 double building_spacing;
383 // building texture & lightmap
384 std::string building_texture;
385 std::string building_lightmap;
387 // Ratio of the 3 random building sizes
388 double building_small_ratio;
389 double building_medium_ratio;
390 double building_large_ratio;
392 // Proportion of buildings with pitched roofs
393 double building_small_pitch;
394 double building_medium_pitch;
395 double building_large_pitch;
397 // Min/Max number of floors for each size
398 int building_small_min_floors;
399 int building_small_max_floors;
400 int building_medium_min_floors;
401 int building_medium_max_floors;
402 int building_large_min_floors;
403 int building_large_max_floors;
405 // Minimum width and depth for each size
406 double building_small_min_width;
407 double building_small_max_width;
408 double building_small_min_depth;
409 double building_small_max_depth;
411 double building_medium_min_width;
412 double building_medium_max_width;
413 double building_medium_min_depth;
414 double building_medium_max_depth;
416 double building_large_min_width;
417 double building_large_max_width;
418 double building_large_min_depth;
419 double building_large_max_depth;
421 double building_range;
423 // Cosine of the angle of maximum and zero density,
424 // used to stop buildings and random objects from being
425 // created on too steep a slope.
426 double cos_object_max_density_slope_angle;
427 double cos_object_zero_density_slope_angle;
430 double wood_coverage;
432 // Range at which trees become visible
435 // Height of the tree
441 // Number of varieties of tree texture
444 // cosine of the tile angle of maximum and zero density,
445 // used to stop trees from being created on too steep a slope.
446 double cos_tree_max_density_slope_angle;
447 double cos_tree_zero_density_slope_angle;
449 // material properties
450 SGVec4f ambient, diffuse, specular, emission;
453 // effect for this material
456 // the list of names for this material. May be empty.
457 std::vector<std::string> _names;
459 std::vector<SGSharedPtr<SGMatModelGroup> > object_groups;
461 // taxiway-/runway-sign texture elements
462 std::map<std::string, SGSharedPtr<SGMaterialGlyph> > glyphs;
464 // Tree texture, typically a strip of applicable tree textures
465 std::string tree_texture;
467 // Object mask, a simple RGB texture used as a mask when placing
468 // random vegetation, objects and buildings
469 std::vector<osg::Texture2D*> _masks;
471 // Condition, indicating when this material is active
472 SGSharedPtr<const SGCondition> condition;
474 // Parameters from the materials file
475 const SGPropertyNode* parameters;
477 // per-material lock for entrypoints called from multiple threads
480 ////////////////////////////////////////////////////////////////////
481 // Internal constructors and methods.
482 ////////////////////////////////////////////////////////////////////
484 void read_properties(const simgear::SGReaderWriterOptions* options,
485 const SGPropertyNode *props,
486 SGPropertyNode *prop_root);
487 void buildEffectProperties(const simgear::SGReaderWriterOptions* options);
488 simgear::Effect* get_effect(int i);
492 class SGMaterialGlyph : public SGReferenced {
494 SGMaterialGlyph(SGPropertyNode *);
495 inline double get_left() const { return _left; }
496 inline double get_right() const { return _right; }
497 inline double get_width() const { return _right - _left; }
504 class SGMaterialUserData : public osg::Referenced {
506 SGMaterialUserData(const SGMaterial* material) :
509 const SGMaterial* getMaterial() const
510 { return mMaterial; }
512 // this cannot be an SGSharedPtr since that would create a cicrular reference
513 // making it impossible to ever free the space needed by SGMaterial
514 const SGMaterial* mMaterial;
518 SGSetTextureFilter( int max);
521 SGGetTextureFilter();
523 #endif // _SG_MAT_HXX