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_effect(const SGTexturedTriangleBin& triangleBin);
123 simgear::Effect* get_effect();
126 * Get the textured state.
128 osg::Texture2D* get_object_mask(const SGTexturedTriangleBin& triangleBin);
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; }
278 * Get the cosine of the maximum tree density slope angle. We
279 * use the cosine as it can be compared directly to the z component
280 * of a triangle normal.
282 * @return the cosine of the maximum tree density slope angle.
284 inline double get_cos_tree_max_density_slope_angle () const { return cos_tree_max_density_slope_angle; }
287 * Get the cosine of the maximum tree density slope angle. We
288 * use the cosine as it can be compared directly to the z component
289 * of a triangle normal.
291 * @return the cosine of the maximum tree density slope angle.
293 inline double get_cos_tree_zero_density_slope_angle () const { return cos_tree_zero_density_slope_angle; }
296 * Get the list of names for this material
298 const std::vector<std::string>& get_names() const { return _names; }
301 * add the given name to the list of names this material is known
303 void add_name(const std::string& name) { _names.push_back(name); }
306 * Get the number of randomly-placed objects defined for this material.
308 int get_object_group_count () const { return object_groups.size(); }
311 * Get a randomly-placed object for this material.
313 SGMatModelGroup * get_object_group (int index) const {
314 return object_groups[index];
318 * Evaluate whether this material is valid given the current global
319 * property state and the tile location.
321 bool valid(SGVec2f loc) const;
324 * Return pointer to glyph class, or 0 if it doesn't exist.
326 SGMaterialGlyph * get_glyph (const std::string& name) const;
328 void set_light_color(const SGVec4f& color)
329 { emission = color; }
330 const SGVec4f& get_light_color() const
333 SGVec2f get_tex_coord_scale() const
335 float tex_width = get_xsize();
336 float tex_height = get_ysize();
338 return SGVec2f((0 < tex_width) ? 1000.0f/tex_width : 1.0f,
339 (0 < tex_height) ? 1000.0f/tex_height : 1.0f);
345 ////////////////////////////////////////////////////////////////////
346 // Protected methods.
347 ////////////////////////////////////////////////////////////////////
350 * Initialization method, invoked by all public constructors.
356 struct _internal_state {
357 _internal_state(simgear::Effect *e, bool l,
358 const simgear::SGReaderWriterOptions *o);
359 _internal_state(simgear::Effect *e, const std::string &t, bool l,
360 const simgear::SGReaderWriterOptions *o);
361 void add_texture(const std::string &t, int i);
362 osg::ref_ptr<simgear::Effect> effect;
363 std::vector<std::pair<std::string,int> > texture_paths;
364 bool effect_realized;
365 osg::ref_ptr<const simgear::SGReaderWriterOptions> options;
371 ////////////////////////////////////////////////////////////////////
373 ////////////////////////////////////////////////////////////////////
376 std::vector<_internal_state> _status;
387 // coverage of night lighting.
388 double light_coverage;
390 // coverage of buildings
391 double building_coverage;
394 double building_spacing;
396 // building texture & lightmap
397 std::string building_texture;
398 std::string building_lightmap;
400 // Ratio of the 3 random building sizes
401 double building_small_ratio;
402 double building_medium_ratio;
403 double building_large_ratio;
405 // Proportion of buildings with pitched roofs
406 double building_small_pitch;
407 double building_medium_pitch;
408 double building_large_pitch;
410 // Min/Max number of floors for each size
411 int building_small_min_floors;
412 int building_small_max_floors;
413 int building_medium_min_floors;
414 int building_medium_max_floors;
415 int building_large_min_floors;
416 int building_large_max_floors;
418 // Minimum width and depth for each size
419 double building_small_min_width;
420 double building_small_max_width;
421 double building_small_min_depth;
422 double building_small_max_depth;
424 double building_medium_min_width;
425 double building_medium_max_width;
426 double building_medium_min_depth;
427 double building_medium_max_depth;
429 double building_large_min_width;
430 double building_large_max_width;
431 double building_large_min_depth;
432 double building_large_max_depth;
434 double building_range;
436 // Cosine of the angle of maximum and zero density,
437 // used to stop buildings and random objects from being
438 // created on too steep a slope.
439 double cos_object_max_density_slope_angle;
440 double cos_object_zero_density_slope_angle;
443 double wood_coverage;
445 // Range at which trees become visible
448 // Height of the tree
454 // Number of varieties of tree texture
457 // cosine of the tile angle of maximum and zero density,
458 // used to stop trees from being created on too steep a slope.
459 double cos_tree_max_density_slope_angle;
460 double cos_tree_zero_density_slope_angle;
462 // material properties
463 SGVec4f ambient, diffuse, specular, emission;
466 // effect for this material
469 // the list of names for this material. May be empty.
470 std::vector<std::string> _names;
472 std::vector<SGSharedPtr<SGMatModelGroup> > object_groups;
474 // taxiway-/runway-sign texture elements
475 std::map<std::string, SGSharedPtr<SGMaterialGlyph> > glyphs;
477 // Tree texture, typically a strip of applicable tree textures
478 std::string tree_texture;
480 // Object mask, a simple RGB texture used as a mask when placing
481 // random vegetation, objects and buildings
482 std::vector<Texture2DRef> _masks;
484 // Condition, indicating when this material is active
485 SGSharedPtr<const SGCondition> condition;
487 // List of geographical rectangles for this material
490 // Parameters from the materials file
491 const SGPropertyNode* parameters;
493 // per-material lock for entrypoints called from multiple threads
496 ////////////////////////////////////////////////////////////////////
497 // Internal constructors and methods.
498 ////////////////////////////////////////////////////////////////////
500 void read_properties(const simgear::SGReaderWriterOptions* options,
501 const SGPropertyNode *props,
502 SGPropertyNode *prop_root);
503 void buildEffectProperties(const simgear::SGReaderWriterOptions* options);
504 simgear::Effect* get_effect(int i);
508 class SGMaterialGlyph : public SGReferenced {
510 SGMaterialGlyph(SGPropertyNode *);
511 inline double get_left() const { return _left; }
512 inline double get_right() const { return _right; }
513 inline double get_width() const { return _right - _left; }
520 class SGMaterialUserData : public osg::Referenced {
522 SGMaterialUserData(const SGMaterial* material) :
525 const SGMaterial* getMaterial() const
526 { return mMaterial; }
528 // this cannot be an SGSharedPtr since that would create a cicrular reference
529 // making it impossible to ever free the space needed by SGMaterial
530 const SGMaterial* mMaterial;
534 SGSetTextureFilter( int max);
537 SGGetTextureFilter();
539 #endif // _SG_MAT_HXX