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/math/SGMath.hxx>
47 #include <simgear/bvh/BVHMaterial.hxx>
52 void reload_shaders();
53 class SGReaderWriterOptions;
56 class SGMatModelGroup;
59 class SGMaterialGlyph;
60 class SGTexturedTriangleBin;
63 * A material in the scene graph.
65 * A material represents information about a single surface type
66 * in the 3D scene graph, including texture, colour, lighting,
67 * tiling, and so on; most of the materials in FlightGear are
68 * defined in the $FG_ROOT/materials.xml file, and can be changed
71 class SGMaterial : public simgear::BVHMaterial {
76 ////////////////////////////////////////////////////////////////////
77 // Public Constructors.
78 ////////////////////////////////////////////////////////////////////
81 * Construct a material from a set of properties.
83 * @param props A property node containing subnodes with the
84 * state information for the material. This node is usually
85 * loaded from the $FG_ROOT/materials.xml file.
87 SGMaterial( const osgDB::Options*,
88 const SGPropertyNode *props,
89 SGPropertyNode *prop_root);
91 SGMaterial(const simgear::SGReaderWriterOptions*,
92 const SGPropertyNode *props,
93 SGPropertyNode *prop_root);
101 ////////////////////////////////////////////////////////////////////
103 ////////////////////////////////////////////////////////////////////
106 * Get the textured state.
108 simgear::Effect* get_effect(const SGTexturedTriangleBin& triangleBin);
109 simgear::Effect* get_effect();
112 * Get the textured state.
114 osg::Texture2D* get_object_mask(const SGTexturedTriangleBin& triangleBin);
118 * Get the number of textures assigned to this material.
120 inline int get_num() const { return _status.size(); }
124 * Get the xsize of the texture, in meters.
126 inline double get_xsize() const { return xsize; }
130 * Get the ysize of the texture, in meters.
132 inline double get_ysize() const { return ysize; }
136 * Get the light coverage.
138 * A smaller number means more generated night lighting.
140 * @return The area (m^2) covered by each light.
142 inline double get_light_coverage () const { return light_coverage; }
145 * Get the building coverage.
147 * A smaller number means more generated buildings.
149 * @return The area (m^2) covered by each light.
151 inline double get_building_coverage () const { return building_coverage; }
154 * Get the building spacing.
156 * This is the minimum spacing between buildings
158 * @return The minimum distance between buildings
160 inline double get_building_spacing () const { return building_spacing; }
163 * Get the building texture.
165 * This is the texture used for auto-generated buildings.
167 * @return The texture for auto-generated buildings.
169 inline std::string get_building_texture () const { return building_texture; }
172 * Get the building lightmap.
174 * This is the lightmap used for auto-generated buildings.
176 * @return The lightmap for auto-generated buildings.
178 inline std::string get_building_lightmap () const { return building_lightmap; }
180 // Ratio of the 3 random building sizes
181 inline double get_building_small_fraction () const { return building_small_ratio / (building_small_ratio + building_medium_ratio + building_large_ratio); }
182 inline double get_building_medium_fraction () const { return building_medium_ratio / (building_small_ratio + building_medium_ratio + building_large_ratio); }
183 inline double get_building_large_fraction () const { return building_large_ratio / (building_small_ratio + building_medium_ratio + building_large_ratio); }
185 // Proportion of buildings with pitched roofs
186 inline double get_building_small_pitch () const { return building_small_pitch; }
187 inline double get_building_medium_pitch () const { return building_medium_pitch; }
188 inline double get_building_large_pitch () const { return building_large_pitch; }
190 // Min/Max number of floors for each size
191 inline int get_building_small_min_floors () const { return building_small_min_floors; }
192 inline int get_building_small_max_floors () const { return building_small_max_floors; }
193 inline int get_building_medium_min_floors () const { return building_medium_min_floors; }
194 inline int get_building_medium_max_floors () const { return building_medium_max_floors; }
195 inline int get_building_large_min_floors () const { return building_large_min_floors; }
196 inline int get_building_large_max_floors () const { return building_large_max_floors; }
198 // Minimum width and depth for each size
199 inline double get_building_small_min_width () const { return building_small_min_width; }
200 inline double get_building_small_max_width () const { return building_small_max_width; }
201 inline double get_building_small_min_depth () const { return building_small_min_depth; }
202 inline double get_building_small_max_depth () const { return building_small_max_depth; }
204 inline double get_building_medium_min_width () const { return building_medium_min_width; }
205 inline double get_building_medium_max_width () const { return building_medium_max_width; }
206 inline double get_building_medium_min_depth () const { return building_medium_min_depth; }
207 inline double get_building_medium_max_depth () const { return building_medium_max_depth; }
209 inline double get_building_large_min_width () const { return building_large_min_width; }
210 inline double get_building_large_max_width () const { return building_large_max_width; }
211 inline double get_building_large_min_depth () const { return building_large_min_depth; }
212 inline double get_building_large_max_depth () const { return building_large_max_depth; }
214 inline double get_building_range () const { return building_range; }
216 inline double get_cos_object_max_density_slope_angle () const { return cos_object_max_density_slope_angle; }
217 inline double get_cos_object_zero_density_slope_angle () const { return cos_object_zero_density_slope_angle; }
220 * Get the wood coverage.
222 * A smaller number means more generated woods within the forest.
224 * @return The area (m^2) covered by each wood.
226 inline double get_wood_coverage () const { return wood_coverage; }
229 * Get the tree height.
231 * @return The average height of the trees.
233 inline double get_tree_height () const { return tree_height; }
236 * Get the tree width.
238 * @return The average width of the trees.
240 inline double get_tree_width () const { return tree_width; }
243 * Get the forest LoD range.
245 * @return The LoD range for the trees.
247 inline double get_tree_range () const { return tree_range; }
250 * Get the number of tree varieties available
252 * @return the number of different trees defined in the texture strip
254 inline int get_tree_varieties () const { return tree_varieties; }
257 * Get the texture strip to use for trees
259 * @return the texture to use for trees.
261 inline std::string get_tree_texture () const { return tree_texture; }
264 * Get the cosine of the maximum tree density slope angle. We
265 * use the cosine as it can be compared directly to the z component
266 * of a triangle normal.
268 * @return the cosine of the maximum tree density slope angle.
270 inline double get_cos_tree_max_density_slope_angle () const { return cos_tree_max_density_slope_angle; }
273 * Get the cosine of the maximum tree density slope angle. We
274 * use the cosine as it can be compared directly to the z component
275 * of a triangle normal.
277 * @return the cosine of the maximum tree density slope angle.
279 inline double get_cos_tree_zero_density_slope_angle () const { return cos_tree_zero_density_slope_angle; }
282 * Get the list of names for this material
284 const std::vector<std::string>& get_names() const { return _names; }
287 * add the given name to the list of names this material is known
289 void add_name(const std::string& name) { _names.push_back(name); }
292 * Get the number of randomly-placed objects defined for this material.
294 int get_object_group_count () const { return object_groups.size(); }
297 * Get a randomly-placed object for this material.
299 SGMatModelGroup * get_object_group (int index) const {
300 return object_groups[index];
304 * Evaluate whether this material is valid given the current global
310 * Return pointer to glyph class, or 0 if it doesn't exist.
312 SGMaterialGlyph * get_glyph (const std::string& name) const;
314 void set_light_color(const SGVec4f& color)
315 { emission = color; }
316 const SGVec4f& get_light_color() const
319 SGVec2f get_tex_coord_scale() const
321 float tex_width = get_xsize();
322 float tex_height = get_ysize();
324 return SGVec2f((0 < tex_width) ? 1000.0f/tex_width : 1.0f,
325 (0 < tex_height) ? 1000.0f/tex_height : 1.0f);
331 ////////////////////////////////////////////////////////////////////
332 // Protected methods.
333 ////////////////////////////////////////////////////////////////////
336 * Initialization method, invoked by all public constructors.
342 struct _internal_state {
343 _internal_state(simgear::Effect *e, bool l,
344 const simgear::SGReaderWriterOptions *o);
345 _internal_state(simgear::Effect *e, const std::string &t, bool l,
346 const simgear::SGReaderWriterOptions *o);
347 void add_texture(const std::string &t, int i);
348 osg::ref_ptr<simgear::Effect> effect;
349 std::vector<std::pair<std::string,int> > texture_paths;
350 bool effect_realized;
351 osg::ref_ptr<const simgear::SGReaderWriterOptions> options;
357 ////////////////////////////////////////////////////////////////////
359 ////////////////////////////////////////////////////////////////////
362 std::vector<_internal_state> _status;
373 // coverage of night lighting.
374 double light_coverage;
376 // coverage of buildings
377 double building_coverage;
380 double building_spacing;
382 // building texture & lightmap
383 std::string building_texture;
384 std::string building_lightmap;
386 // Ratio of the 3 random building sizes
387 double building_small_ratio;
388 double building_medium_ratio;
389 double building_large_ratio;
391 // Proportion of buildings with pitched roofs
392 double building_small_pitch;
393 double building_medium_pitch;
394 double building_large_pitch;
396 // Min/Max number of floors for each size
397 int building_small_min_floors;
398 int building_small_max_floors;
399 int building_medium_min_floors;
400 int building_medium_max_floors;
401 int building_large_min_floors;
402 int building_large_max_floors;
404 // Minimum width and depth for each size
405 double building_small_min_width;
406 double building_small_max_width;
407 double building_small_min_depth;
408 double building_small_max_depth;
410 double building_medium_min_width;
411 double building_medium_max_width;
412 double building_medium_min_depth;
413 double building_medium_max_depth;
415 double building_large_min_width;
416 double building_large_max_width;
417 double building_large_min_depth;
418 double building_large_max_depth;
420 double building_range;
422 // Cosine of the angle of maximum and zero density,
423 // used to stop buildings and random objects from being
424 // created on too steep a slope.
425 double cos_object_max_density_slope_angle;
426 double cos_object_zero_density_slope_angle;
429 double wood_coverage;
431 // Range at which trees become visible
434 // Height of the tree
440 // Number of varieties of tree texture
443 // cosine of the tile angle of maximum and zero density,
444 // used to stop trees from being created on too steep a slope.
445 double cos_tree_max_density_slope_angle;
446 double cos_tree_zero_density_slope_angle;
448 // material properties
449 SGVec4f ambient, diffuse, specular, emission;
452 // effect for this material
455 // the list of names for this material. May be empty.
456 std::vector<std::string> _names;
458 std::vector<SGSharedPtr<SGMatModelGroup> > object_groups;
460 // taxiway-/runway-sign texture elements
461 std::map<std::string, SGSharedPtr<SGMaterialGlyph> > glyphs;
463 // Tree texture, typically a strip of applicable tree textures
464 std::string tree_texture;
466 // Object mask, a simple RGB texture used as a mask when placing
467 // random vegetation, objects and buildings
468 std::vector<osg::Texture2D*> _masks;
470 // Condition, indicating when this material is active
471 SGSharedPtr<const SGCondition> condition;
473 // Parameters from the materials file
474 const SGPropertyNode* parameters;
476 ////////////////////////////////////////////////////////////////////
477 // Internal constructors and methods.
478 ////////////////////////////////////////////////////////////////////
480 void read_properties(const simgear::SGReaderWriterOptions* options,
481 const SGPropertyNode *props,
482 SGPropertyNode *prop_root);
483 void buildEffectProperties(const simgear::SGReaderWriterOptions* options);
484 simgear::Effect* get_effect(int i);
488 class SGMaterialGlyph : public SGReferenced {
490 SGMaterialGlyph(SGPropertyNode *);
491 inline double get_left() const { return _left; }
492 inline double get_right() const { return _right; }
493 inline double get_width() const { return _right - _left; }
500 class SGMaterialUserData : public osg::Referenced {
502 SGMaterialUserData(const SGMaterial* material) :
505 const SGMaterial* getMaterial() const
506 { return mMaterial; }
508 // this cannot be an SGSharedPtr since that would create a cicrular reference
509 // making it impossible to ever free the space needed by SGMaterial
510 const SGMaterial* mMaterial;
514 SGSetTextureFilter( int max);
517 SGGetTextureFilter();
519 #endif // _SG_MAT_HXX