]> git.mxchange.org Git - simgear.git/blob - simgear/scene/material/mat.hxx
materials.xml defined vegetation Effect.
[simgear.git] / simgear / scene / material / mat.hxx
1 // mat.hxx -- a material in the scene graph.
2 // TODO: this class needs to be renamed.
3 //
4 // Written by Curtis Olson, started May 1998.
5 // Overhauled by David Megginson, December 2001
6 //
7 // Copyright (C) 1998 - 2000  Curtis L. Olson  - http://www.flightgear.org/~curt
8 //
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.
13 //
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.
18 //
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.
22 //
23 // $Id$
24
25
26 #ifndef _SG_MAT_HXX
27 #define _SG_MAT_HXX
28
29 #include <simgear/compiler.h>
30
31 #include <string>      // Standard C++ string library
32 #include <vector>
33 #include <map>
34
35 #include "Effect.hxx"
36
37 #include <osg/ref_ptr>
38 #include <osg/Texture2D>
39
40 namespace osg
41 {
42 class StateSet;
43 }
44
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>
53
54 typedef osg::ref_ptr<osg::Texture2D> Texture2DRef;
55 typedef std::vector<SGRect <float> > AreaList;
56
57 namespace simgear
58 {
59 class Effect;
60 void reload_shaders();
61 class SGReaderWriterOptions;
62 }
63
64 class SGMatModelGroup;
65 class SGCondition;
66 class SGPropertyNode;
67 class SGMaterialGlyph;
68 class SGTexturedTriangleBin;
69
70 /**
71  * A material in the scene graph.
72  *
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
77  * at runtime.
78  */
79 class SGMaterial : public simgear::BVHMaterial {
80
81 public:
82
83 \f
84   ////////////////////////////////////////////////////////////////////
85   // Public Constructors.
86   ////////////////////////////////////////////////////////////////////
87
88   /**
89    * Construct a material from a set of properties.
90    *
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.
94    */
95   SGMaterial(const osgDB::Options*,
96              const SGPropertyNode *props,
97              SGPropertyNode *prop_root,
98              AreaList *a,
99                          SGSharedPtr<const SGCondition> c);
100
101
102   SGMaterial(const simgear::SGReaderWriterOptions*,
103              const SGPropertyNode *props,
104              SGPropertyNode *prop_root,
105              AreaList *a,
106                          SGSharedPtr<const SGCondition> c);
107
108   /**
109    * Destructor.
110    */
111   ~SGMaterial( void );
112
113
114 \f
115   ////////////////////////////////////////////////////////////////////
116   // Public methods.
117   ////////////////////////////////////////////////////////////////////
118
119   /**
120    * Get the textured state.
121    */
122   simgear::Effect* get_one_effect(int texIndex);
123   simgear::Effect* get_effect();
124
125   /**
126    * Get the textured state.
127    */
128   osg::Texture2D* get_one_object_mask(int texIndex);
129
130
131   /**
132    * Get the number of textures assigned to this material.
133    */
134   inline int get_num() const { return _status.size(); }
135
136
137   /**
138    * Get the xsize of the texture, in meters.
139    */
140   inline double get_xsize() const { return xsize; }
141
142
143   /**
144    * Get the ysize of the texture, in meters.
145    */
146   inline double get_ysize() const { return ysize; }
147
148
149   /**
150    * Get the light coverage.
151    *
152    * A smaller number means more generated night lighting.
153    *
154    * @return The area (m^2) covered by each light.
155    */
156   inline double get_light_coverage () const { return light_coverage; }
157   
158   /**
159    * Get the building coverage.
160    *
161    * A smaller number means more generated buildings.
162    *
163    * @return The area (m^2) covered by each light.
164    */
165   inline double get_building_coverage () const { return building_coverage; }
166
167   /**
168    * Get the building spacing.
169    *
170    * This is the minimum spacing between buildings
171    *
172    * @return The minimum distance between buildings
173    */
174   inline double get_building_spacing () const { return building_spacing; }
175
176   /**
177    * Get the building texture.
178    *
179    * This is the texture used for auto-generated buildings.
180    *
181    * @return The texture for auto-generated buildings.
182    */
183   inline std::string get_building_texture () const { return building_texture; }
184
185   /**
186    * Get the building lightmap.
187    *
188    * This is the lightmap used for auto-generated buildings.
189    *
190    * @return The lightmap for auto-generated buildings.
191    */
192   inline std::string get_building_lightmap () const { return building_lightmap; }
193   
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); }
198   
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; }
203
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; }
211   
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; }
217   
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; }
222   
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; }
227   
228   inline double get_building_range () const { return building_range; }
229   
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; }
232
233   /**
234    * Get the wood coverage.
235    *
236    * A smaller number means more generated woods within the forest.
237    *
238    * @return The area (m^2) covered by each wood.
239    */
240   inline double get_wood_coverage () const { return wood_coverage; }
241   
242   /**
243    * Get the tree height.
244    *
245    * @return The average height of the trees.
246    */
247   inline double get_tree_height () const { return tree_height; }
248
249   /**
250    * Get the tree width.
251    *
252    * @return The average width of the trees.
253    */
254   inline double get_tree_width () const { return tree_width; }
255
256   /**
257    * Get the forest LoD range.
258    *
259    * @return The LoD range for the trees.
260    */
261   inline double get_tree_range () const { return tree_range; }
262   
263   /**
264    * Get the number of tree varieties available
265    *
266    * @return the number of different trees defined in the texture strip
267    */
268   inline int get_tree_varieties () const { return tree_varieties; }
269   
270   /**
271    * Get the texture strip to use for trees
272    *
273    * @return the texture to use for trees.
274    */
275   inline std::string get_tree_texture () const { return  tree_texture; }
276
277
278   /**
279    * Get the effect file name to use for trees
280    *
281    * @return the effect to use for this set of trees.
282    */
283   inline std::string get_tree_effect () const { return  tree_effect; }
284   
285   /**
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.
289    * 
290    * @return the cosine of the maximum tree density slope angle.
291    */
292   inline double get_cos_tree_max_density_slope_angle () const { return cos_tree_max_density_slope_angle; }
293   
294   /**
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.
298    * 
299    * @return the cosine of the maximum tree density slope angle.
300    */
301   inline double get_cos_tree_zero_density_slope_angle () const { return cos_tree_zero_density_slope_angle; }
302   
303   /**
304    * Get the list of names for this material
305    */
306   const std::vector<std::string>& get_names() const { return _names; }
307
308   /**
309    * add the given name to the list of names this material is known
310    */
311   void add_name(const std::string& name) { _names.push_back(name); }
312
313   /**
314    * Get the number of randomly-placed objects defined for this material.
315    */
316   int get_object_group_count () const { return object_groups.size(); }
317
318   /**
319    * Get a randomly-placed object for this material.
320    */
321   SGMatModelGroup * get_object_group (int index) const {
322     return object_groups[index];
323   }
324   
325   /**
326    * Evaluate whether this material is valid given the current global
327    * property state and the tile location.
328    */
329      bool valid(SGVec2f loc) const;
330
331   /**
332    * Return pointer to glyph class, or 0 if it doesn't exist.
333    */
334   SGMaterialGlyph * get_glyph (const std::string& name) const;
335
336   void set_light_color(const SGVec4f& color)
337   { emission = color; }
338   const SGVec4f& get_light_color() const
339   { return emission; }
340
341   SGVec2f get_tex_coord_scale() const
342   {
343     float tex_width = get_xsize();
344     float tex_height = get_ysize();
345
346     return SGVec2f((0 < tex_width) ? 1000.0f/tex_width : 1.0f,
347                    (0 < tex_height) ? 1000.0f/tex_height : 1.0f);
348   }
349   
350 protected:
351
352 \f
353   ////////////////////////////////////////////////////////////////////
354   // Protected methods.
355   ////////////////////////////////////////////////////////////////////
356
357   /**
358    * Initialization method, invoked by all public constructors.
359    */
360   void init();
361
362 protected:
363
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;
374   };
375
376 private:
377
378 \f
379   ////////////////////////////////////////////////////////////////////
380   // Internal state.
381   ////////////////////////////////////////////////////////////////////
382
383   // texture status
384   std::vector<_internal_state> _status;
385
386   // texture size
387   double xsize, ysize;
388
389   // wrap texture?
390   bool wrapu, wrapv;
391
392   // use mipmapping?
393   bool mipmap;
394
395   // coverage of night lighting.
396   double light_coverage;
397   
398   // coverage of buildings
399   double building_coverage;
400   
401   // building spacing
402   double building_spacing;
403   
404   // building texture & lightmap
405   std::string building_texture;
406   std::string building_lightmap;
407
408   // Ratio of the 3 random building sizes
409   double building_small_ratio;
410   double building_medium_ratio;
411   double building_large_ratio;
412   
413   // Proportion of buildings with pitched roofs
414   double building_small_pitch;
415   double building_medium_pitch;
416   double building_large_pitch;
417
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;
425   
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;
431   
432   double building_medium_min_width;
433   double building_medium_max_width;
434   double building_medium_min_depth;
435   double building_medium_max_depth;
436   
437   double building_large_min_width;
438   double building_large_max_width;
439   double building_large_min_depth;
440   double building_large_max_depth;
441   
442   double building_range;
443   
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;
449   
450   // coverage of woods
451   double wood_coverage;
452
453   // Range at which trees become visible
454   double tree_range;
455
456   // Height of the tree
457   double tree_height;
458
459   // Width of the tree
460   double tree_width;
461
462   // Number of varieties of tree texture
463   int tree_varieties;
464   
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;
469
470   // material properties
471   SGVec4f ambient, diffuse, specular, emission;
472   double shininess;
473
474   // effect for this material
475   std::string effect;
476
477   // the list of names for this material. May be empty.
478   std::vector<std::string> _names;
479
480   std::vector<SGSharedPtr<SGMatModelGroup> > object_groups;
481
482   // taxiway-/runway-sign texture elements
483   std::map<std::string, SGSharedPtr<SGMaterialGlyph> > glyphs;
484   
485   // Tree texture, typically a strip of applicable tree textures
486   std::string tree_texture;
487
488   // Tree effect to be used for a particular material
489   std::string tree_effect;
490   
491   // Object mask, a simple RGB texture used as a mask when placing
492   // random vegetation, objects and buildings
493   std::vector<Texture2DRef> _masks;
494   
495   // Condition, indicating when this material is active
496   SGSharedPtr<const SGCondition> condition;
497   
498   // List of geographical rectangles for this material
499   AreaList* areas;
500
501   // Parameters from the materials file
502   const SGPropertyNode* parameters;
503
504   // per-material lock for entrypoints called from multiple threads
505   SGMutex _lock;
506
507   ////////////////////////////////////////////////////////////////////
508   // Internal constructors and methods.
509   ////////////////////////////////////////////////////////////////////
510
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);
516 };
517
518
519 class SGMaterialGlyph : public SGReferenced {
520 public:
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; }
525
526 protected:
527   double _left;
528   double _right;
529 };
530
531 class SGMaterialUserData : public osg::Referenced {
532 public:
533   SGMaterialUserData(const SGMaterial* material) :
534     mMaterial(material)
535   {}
536   const SGMaterial* getMaterial() const
537   { return mMaterial; }
538 private:
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;
542 };
543
544 void
545 SGSetTextureFilter( int max);
546
547 int
548 SGGetTextureFilter();
549
550 #endif // _SG_MAT_HXX