]> git.mxchange.org Git - simgear.git/blob - simgear/scene/material/mat.hxx
Allow a <parameters> section of a material definition, to pass parameters straight...
[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 #ifndef __cplusplus
30 # error This library requires C++
31 #endif
32
33 #include <simgear/compiler.h>
34
35 #include <string>      // Standard C++ string library
36 #include <vector>
37 #include <map>
38
39 #include "Effect.hxx"
40 #include <simgear/scene/tgdb/SGTexturedTriangleBin.hxx>
41
42 #include <osg/ref_ptr>
43 #include <osg/Texture2D>
44
45 namespace osg
46 {
47 class StateSet;
48 }
49
50 #include <simgear/scene/util/SGReaderWriterOptions.hxx>
51 #include <simgear/props/props.hxx>
52 #include <simgear/structure/SGSharedPtr.hxx>
53 #include <simgear/scene/util/SGSceneFeatures.hxx>
54 #include <simgear/props/condition.hxx>
55
56 #include "matmodel.hxx"
57
58 namespace simgear
59 {
60 class Effect;
61 void reload_shaders();
62 }
63
64 class SGMaterialGlyph;
65
66 /**
67  * A material in the scene graph.
68  *
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
73  * at runtime.
74  */
75 class SGMaterial : public SGReferenced {
76
77 public:
78
79 \f
80   ////////////////////////////////////////////////////////////////////
81   // Public Constructors.
82   ////////////////////////////////////////////////////////////////////
83
84   /**
85    * Construct a material from a set of properties.
86    *
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.
90    */
91   SGMaterial( const osgDB::Options*, 
92               const SGPropertyNode *props, 
93               SGPropertyNode *prop_root);
94
95   SGMaterial(const simgear::SGReaderWriterOptions*,
96              const SGPropertyNode *props,
97              SGPropertyNode *prop_root);
98   /**
99    * Destructor.
100    */
101   ~SGMaterial( void );
102
103
104 \f
105   ////////////////////////////////////////////////////////////////////
106   // Public methods.
107   ////////////////////////////////////////////////////////////////////
108
109   /**
110    * Get the textured state.
111    */
112   simgear::Effect* get_effect(SGTexturedTriangleBin triangleBin);
113   simgear::Effect* get_effect();
114
115   /**
116    * Get the textured state.
117    */
118   osg::Texture2D* get_object_mask(SGTexturedTriangleBin triangleBin);
119
120
121   /**
122    * Get the number of textures assigned to this material.
123    */
124   inline int get_num() const { return _status.size(); }
125
126
127   /**
128    * Get the xsize of the texture, in meters.
129    */
130   inline double get_xsize() const { return xsize; }
131
132
133   /**
134    * Get the ysize of the texture, in meters.
135    */
136   inline double get_ysize() const { return ysize; }
137
138
139   /**
140    * Get the light coverage.
141    *
142    * A smaller number means more generated night lighting.
143    *
144    * @return The area (m^2) covered by each light.
145    */
146   inline double get_light_coverage () const { return light_coverage; }
147   
148   /**
149    * Get the building coverage.
150    *
151    * A smaller number means more generated buildings.
152    *
153    * @return The area (m^2) covered by each light.
154    */
155   inline double get_building_coverage () const { return building_coverage; }
156
157   /**
158    * Get the building spacing.
159    *
160    * This is the minimum spacing between buildings
161    *
162    * @return The minimum distance between buildings
163    */
164   inline double get_building_spacing () const { return building_spacing; }
165
166   /**
167    * Get the building texture.
168    *
169    * This is the texture used for auto-generated buildings.
170    *
171    * @return The texture for auto-generated buildings.
172    */
173   inline std::string get_building_texture () const { return building_texture; }
174
175   /**
176    * Get the building lightmap.
177    *
178    * This is the lightmap used for auto-generated buildings.
179    *
180    * @return The lightmap for auto-generated buildings.
181    */
182   inline std::string get_building_lightmap () const { return building_lightmap; }
183   
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); }
188   
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; }
193
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; }
201   
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; }
207   
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; }
212   
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; }
217
218   /**
219    * Get the wood coverage.
220    *
221    * A smaller number means more generated woods within the forest.
222    *
223    * @return The area (m^2) covered by each wood.
224    */
225   inline double get_wood_coverage () const { return wood_coverage; }
226   
227   /**
228    * Get the tree height.
229    *
230    * @return The average height of the trees.
231    */
232   inline double get_tree_height () const { return tree_height; }
233
234   /**
235    * Get the tree width.
236    *
237    * @return The average width of the trees.
238    */
239   inline double get_tree_width () const { return tree_width; }
240
241   /**
242    * Get the forest LoD range.
243    *
244    * @return The LoD range for the trees.
245    */
246   inline double get_tree_range () const { return tree_range; }
247   
248   /**
249    * Get the number of tree varieties available
250    *
251    * @return the number of different trees defined in the texture strip
252    */
253   inline int get_tree_varieties () const { return tree_varieties; }
254   
255   /**
256    * Get the texture strip to use for trees
257    *
258    * @return the texture to use for trees.
259    */
260   inline std::string get_tree_texture () const { return  tree_texture; }
261   
262   /**
263    * Return if the surface material is solid, if it is not solid, a fluid
264    * can be assumed, that is usually water.
265    */
266   bool get_solid () const { return solid; }
267
268   /**
269    * Get the friction factor for that material
270    */
271   double get_friction_factor () const { return friction_factor; }
272
273   /**
274    * Get the rolling friction for that material
275    */
276   double get_rolling_friction () const { return rolling_friction; }
277
278   /**
279    * Get the bumpines for that material
280    */
281   double get_bumpiness () const { return bumpiness; }
282
283   /**
284    * Get the load resistance
285    */
286   double get_load_resistance () const { return load_resistance; }
287
288   /**
289    * Get the list of names for this material
290    */
291   const std::vector<std::string>& get_names() const { return _names; }
292
293   /**
294    * add the given name to the list of names this material is known
295    */
296   void add_name(const std::string& name) { _names.push_back(name); }
297
298   /**
299    * Get the number of randomly-placed objects defined for this material.
300    */
301   int get_object_group_count () const { return object_groups.size(); }
302
303   /**
304    * Get a randomly-placed object for this material.
305    */
306   SGMatModelGroup * get_object_group (int index) const {
307     return object_groups[index];
308   }
309   
310   /**
311    * Evaluate whether this material is valid given the current global
312    * property state.
313    */
314    bool valid() { 
315      if (condition) {
316        return condition->test();       
317      } else {
318        return true;
319      }
320    }
321
322   /**
323    * Return pointer to glyph class, or 0 if it doesn't exist.
324    */
325   SGMaterialGlyph * get_glyph (const std::string& name) const;
326
327   void set_light_color(const SGVec4f& color)
328   { emission = color; }
329   const SGVec4f& get_light_color() const
330   { return emission; }
331
332   SGVec2f get_tex_coord_scale() const
333   {
334     float tex_width = get_xsize();
335     float tex_height = get_ysize();
336
337     return SGVec2f((0 < tex_width) ? 1000.0f/tex_width : 1.0f,
338                    (0 < tex_height) ? 1000.0f/tex_height : 1.0f);
339   }
340
341 protected:
342
343 \f
344   ////////////////////////////////////////////////////////////////////
345   // Protected methods.
346   ////////////////////////////////////////////////////////////////////
347
348   /**
349    * Initialization method, invoked by all public constructors.
350    */
351   void init();
352
353 protected:
354
355   struct _internal_state {
356       _internal_state(simgear::Effect *e, bool l,
357                       const simgear::SGReaderWriterOptions *o);
358       _internal_state(simgear::Effect *e, const std::string &t, bool l,
359                       const simgear::SGReaderWriterOptions *o);
360       void add_texture(const std::string &t, int i);
361       osg::ref_ptr<simgear::Effect> effect;
362       std::vector<std::pair<std::string,int> > texture_paths;
363       bool effect_realized;
364       osg::ref_ptr<const simgear::SGReaderWriterOptions> options;
365   };
366
367 private:
368
369 \f
370   ////////////////////////////////////////////////////////////////////
371   // Internal state.
372   ////////////////////////////////////////////////////////////////////
373
374   // texture status
375   std::vector<_internal_state> _status;
376
377   // texture size
378   double xsize, ysize;
379
380   // wrap texture?
381   bool wrapu, wrapv;
382
383   // use mipmapping?
384   bool mipmap;
385
386   // coverage of night lighting.
387   double light_coverage;
388   
389   // coverage of buildings
390   double building_coverage;
391   
392   // building spacing
393   double building_spacing;
394   
395   // building texture & lightmap
396   std::string building_texture;
397   std::string building_lightmap;
398
399   // Ratio of the 3 random building sizes
400   double building_small_ratio;
401   double building_medium_ratio;
402   double building_large_ratio;
403   
404   // Proportion of buildings with pitched roofs
405   double building_small_pitch;
406   double building_medium_pitch;
407   double building_large_pitch;
408
409   // Min/Max number of floors for each size
410   int building_small_min_floors; 
411   int building_small_max_floors;
412   int building_medium_min_floors;
413   int building_medium_max_floors;
414   int building_large_min_floors;
415   int building_large_max_floors;
416   
417   // Minimum width and depth for each size
418   double building_small_min_width;
419   double building_small_max_width;
420   double building_small_min_depth;
421   double building_small_max_depth;
422   
423   double building_medium_min_width;
424   double building_medium_max_width;
425   double building_medium_min_depth;
426   double building_medium_max_depth;
427   
428   double building_large_min_width;
429   double building_large_max_width;
430   double building_large_min_depth;
431   double building_large_max_depth;
432   
433   // coverage of woods
434   double wood_coverage;
435
436   // Range at which trees become visible
437   double tree_range;
438
439   // Height of the tree
440   double tree_height;
441
442   // Width of the tree
443   double tree_width;
444
445   // Number of varieties of tree texture
446   int tree_varieties;
447
448   // True if the material is solid, false if it is a fluid
449   bool solid;
450
451   // the friction factor of that surface material
452   double friction_factor;
453
454   // the rolling friction of that surface material
455   double rolling_friction;
456
457   // the bumpiness of that surface material
458   double bumpiness;
459
460   // the load resistance of that surface material
461   double load_resistance;
462
463   // material properties
464   SGVec4f ambient, diffuse, specular, emission;
465   double shininess;
466
467   // effect for this material
468   std::string effect;
469
470   // the list of names for this material. May be empty.
471   std::vector<std::string> _names;
472
473   std::vector<SGSharedPtr<SGMatModelGroup> > object_groups;
474
475   // taxiway-/runway-sign texture elements
476   std::map<std::string, SGSharedPtr<SGMaterialGlyph> > glyphs;
477   
478   // Tree texture, typically a strip of applicable tree textures
479   std::string tree_texture;
480   
481   // Object mask, a simple RGB texture used as a mask when placing
482   // random vegetation, objects and buildings
483   std::vector<osg::Texture2D*> _masks;
484   
485   // Condition, indicating when this material is active
486   SGSharedPtr<const SGCondition> condition;
487   
488   // Parameters from the materials file
489   const SGPropertyNode* parameters;
490
491   ////////////////////////////////////////////////////////////////////
492   // Internal constructors and methods.
493   ////////////////////////////////////////////////////////////////////
494
495   void read_properties(const simgear::SGReaderWriterOptions* options,
496                         const SGPropertyNode *props,
497                         SGPropertyNode *prop_root);
498   void buildEffectProperties(const simgear::SGReaderWriterOptions* options);
499   simgear::Effect* get_effect(int i);
500 };
501
502
503 class SGMaterialGlyph : public SGReferenced {
504 public:
505   SGMaterialGlyph(SGPropertyNode *);
506   inline double get_left() const { return _left; }
507   inline double get_right() const { return _right; }
508   inline double get_width() const { return _right - _left; }
509
510 protected:
511   double _left;
512   double _right;
513 };
514
515 class SGMaterialUserData : public osg::Referenced {
516 public:
517   SGMaterialUserData(const SGMaterial* material) :
518     mMaterial(material)
519   {}
520   const SGMaterial* getMaterial() const
521   { return mMaterial; }
522 private:
523   // this cannot be an SGSharedPtr since that would create a cicrular reference
524   // making it impossible to ever free the space needed by SGMaterial
525   const SGMaterial* mMaterial;
526 };
527
528 void
529 SGSetTextureFilter( int max);
530
531 int
532 SGGetTextureFilter();
533
534 #endif // _SG_MAT_HXX