]> git.mxchange.org Git - simgear.git/blob - simgear/scene/material/mat.hxx
7ed5958c962b17f7609ac572306cc45e96fe364c
[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/SGMath.hxx>
48 #include <simgear/bvh/BVHMaterial.hxx>
49
50 namespace simgear
51 {
52 class Effect;
53 void reload_shaders();
54 class SGReaderWriterOptions;
55 }
56
57 class SGMatModelGroup;
58 class SGCondition;
59 class SGPropertyNode;
60 class SGMaterialGlyph;
61 class SGTexturedTriangleBin;
62
63 /**
64  * A material in the scene graph.
65  *
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
70  * at runtime.
71  */
72 class SGMaterial : public simgear::BVHMaterial {
73
74 public:
75
76 \f
77   ////////////////////////////////////////////////////////////////////
78   // Public Constructors.
79   ////////////////////////////////////////////////////////////////////
80
81   /**
82    * Construct a material from a set of properties.
83    *
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.
87    */
88   SGMaterial( const osgDB::Options*, 
89               const SGPropertyNode *props, 
90               SGPropertyNode *prop_root);
91
92   SGMaterial(const simgear::SGReaderWriterOptions*,
93              const SGPropertyNode *props,
94              SGPropertyNode *prop_root);
95   /**
96    * Destructor.
97    */
98   ~SGMaterial( void );
99
100
101 \f
102   ////////////////////////////////////////////////////////////////////
103   // Public methods.
104   ////////////////////////////////////////////////////////////////////
105
106   /**
107    * Get the textured state.
108    */
109   simgear::Effect* get_effect(const SGTexturedTriangleBin& triangleBin);
110   simgear::Effect* get_effect();
111
112   /**
113    * Get the textured state.
114    */
115   osg::Texture2D* get_object_mask(const SGTexturedTriangleBin& triangleBin);
116
117
118   /**
119    * Get the number of textures assigned to this material.
120    */
121   inline int get_num() const { return _status.size(); }
122
123
124   /**
125    * Get the xsize of the texture, in meters.
126    */
127   inline double get_xsize() const { return xsize; }
128
129
130   /**
131    * Get the ysize of the texture, in meters.
132    */
133   inline double get_ysize() const { return ysize; }
134
135
136   /**
137    * Get the light coverage.
138    *
139    * A smaller number means more generated night lighting.
140    *
141    * @return The area (m^2) covered by each light.
142    */
143   inline double get_light_coverage () const { return light_coverage; }
144   
145   /**
146    * Get the building coverage.
147    *
148    * A smaller number means more generated buildings.
149    *
150    * @return The area (m^2) covered by each light.
151    */
152   inline double get_building_coverage () const { return building_coverage; }
153
154   /**
155    * Get the building spacing.
156    *
157    * This is the minimum spacing between buildings
158    *
159    * @return The minimum distance between buildings
160    */
161   inline double get_building_spacing () const { return building_spacing; }
162
163   /**
164    * Get the building texture.
165    *
166    * This is the texture used for auto-generated buildings.
167    *
168    * @return The texture for auto-generated buildings.
169    */
170   inline std::string get_building_texture () const { return building_texture; }
171
172   /**
173    * Get the building lightmap.
174    *
175    * This is the lightmap used for auto-generated buildings.
176    *
177    * @return The lightmap for auto-generated buildings.
178    */
179   inline std::string get_building_lightmap () const { return building_lightmap; }
180   
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); }
185   
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; }
190
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; }
198   
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; }
204   
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; }
209   
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; }
214   
215   inline double get_building_range () const { return building_range; }
216   
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; }
219
220   /**
221    * Get the wood coverage.
222    *
223    * A smaller number means more generated woods within the forest.
224    *
225    * @return The area (m^2) covered by each wood.
226    */
227   inline double get_wood_coverage () const { return wood_coverage; }
228   
229   /**
230    * Get the tree height.
231    *
232    * @return The average height of the trees.
233    */
234   inline double get_tree_height () const { return tree_height; }
235
236   /**
237    * Get the tree width.
238    *
239    * @return The average width of the trees.
240    */
241   inline double get_tree_width () const { return tree_width; }
242
243   /**
244    * Get the forest LoD range.
245    *
246    * @return The LoD range for the trees.
247    */
248   inline double get_tree_range () const { return tree_range; }
249   
250   /**
251    * Get the number of tree varieties available
252    *
253    * @return the number of different trees defined in the texture strip
254    */
255   inline int get_tree_varieties () const { return tree_varieties; }
256   
257   /**
258    * Get the texture strip to use for trees
259    *
260    * @return the texture to use for trees.
261    */
262   inline std::string get_tree_texture () const { return  tree_texture; }
263   
264   /**
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.
268    * 
269    * @return the cosine of the maximum tree density slope angle.
270    */
271   inline double get_cos_tree_max_density_slope_angle () const { return cos_tree_max_density_slope_angle; }
272   
273   /**
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.
277    * 
278    * @return the cosine of the maximum tree density slope angle.
279    */
280   inline double get_cos_tree_zero_density_slope_angle () const { return cos_tree_zero_density_slope_angle; }
281   
282   /**
283    * Get the list of names for this material
284    */
285   const std::vector<std::string>& get_names() const { return _names; }
286
287   /**
288    * add the given name to the list of names this material is known
289    */
290   void add_name(const std::string& name) { _names.push_back(name); }
291
292   /**
293    * Get the number of randomly-placed objects defined for this material.
294    */
295   int get_object_group_count () const { return object_groups.size(); }
296
297   /**
298    * Get a randomly-placed object for this material.
299    */
300   SGMatModelGroup * get_object_group (int index) const {
301     return object_groups[index];
302   }
303   
304   /**
305    * Evaluate whether this material is valid given the current global
306    * property state.
307    */
308      bool valid() const;
309
310   /**
311    * Return pointer to glyph class, or 0 if it doesn't exist.
312    */
313   SGMaterialGlyph * get_glyph (const std::string& name) const;
314
315   void set_light_color(const SGVec4f& color)
316   { emission = color; }
317   const SGVec4f& get_light_color() const
318   { return emission; }
319
320   SGVec2f get_tex_coord_scale() const
321   {
322     float tex_width = get_xsize();
323     float tex_height = get_ysize();
324
325     return SGVec2f((0 < tex_width) ? 1000.0f/tex_width : 1.0f,
326                    (0 < tex_height) ? 1000.0f/tex_height : 1.0f);
327   }
328   
329 protected:
330
331 \f
332   ////////////////////////////////////////////////////////////////////
333   // Protected methods.
334   ////////////////////////////////////////////////////////////////////
335
336   /**
337    * Initialization method, invoked by all public constructors.
338    */
339   void init();
340
341 protected:
342
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;
353   };
354
355 private:
356
357 \f
358   ////////////////////////////////////////////////////////////////////
359   // Internal state.
360   ////////////////////////////////////////////////////////////////////
361
362   // texture status
363   std::vector<_internal_state> _status;
364
365   // texture size
366   double xsize, ysize;
367
368   // wrap texture?
369   bool wrapu, wrapv;
370
371   // use mipmapping?
372   bool mipmap;
373
374   // coverage of night lighting.
375   double light_coverage;
376   
377   // coverage of buildings
378   double building_coverage;
379   
380   // building spacing
381   double building_spacing;
382   
383   // building texture & lightmap
384   std::string building_texture;
385   std::string building_lightmap;
386
387   // Ratio of the 3 random building sizes
388   double building_small_ratio;
389   double building_medium_ratio;
390   double building_large_ratio;
391   
392   // Proportion of buildings with pitched roofs
393   double building_small_pitch;
394   double building_medium_pitch;
395   double building_large_pitch;
396
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;
404   
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;
410   
411   double building_medium_min_width;
412   double building_medium_max_width;
413   double building_medium_min_depth;
414   double building_medium_max_depth;
415   
416   double building_large_min_width;
417   double building_large_max_width;
418   double building_large_min_depth;
419   double building_large_max_depth;
420   
421   double building_range;
422   
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;
428   
429   // coverage of woods
430   double wood_coverage;
431
432   // Range at which trees become visible
433   double tree_range;
434
435   // Height of the tree
436   double tree_height;
437
438   // Width of the tree
439   double tree_width;
440
441   // Number of varieties of tree texture
442   int tree_varieties;
443   
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;
448
449   // material properties
450   SGVec4f ambient, diffuse, specular, emission;
451   double shininess;
452
453   // effect for this material
454   std::string effect;
455
456   // the list of names for this material. May be empty.
457   std::vector<std::string> _names;
458
459   std::vector<SGSharedPtr<SGMatModelGroup> > object_groups;
460
461   // taxiway-/runway-sign texture elements
462   std::map<std::string, SGSharedPtr<SGMaterialGlyph> > glyphs;
463   
464   // Tree texture, typically a strip of applicable tree textures
465   std::string tree_texture;
466   
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;
470   
471   // Condition, indicating when this material is active
472   SGSharedPtr<const SGCondition> condition;
473   
474   // Parameters from the materials file
475   const SGPropertyNode* parameters;
476
477   // per-material lock for entrypoints called from multiple threads
478   SGMutex _lock;
479
480   ////////////////////////////////////////////////////////////////////
481   // Internal constructors and methods.
482   ////////////////////////////////////////////////////////////////////
483
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);
489 };
490
491
492 class SGMaterialGlyph : public SGReferenced {
493 public:
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; }
498
499 protected:
500   double _left;
501   double _right;
502 };
503
504 class SGMaterialUserData : public osg::Referenced {
505 public:
506   SGMaterialUserData(const SGMaterial* material) :
507     mMaterial(material)
508   {}
509   const SGMaterial* getMaterial() const
510   { return mMaterial; }
511 private:
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;
515 };
516
517 void
518 SGSetTextureFilter( int max);
519
520 int
521 SGGetTextureFilter();
522
523 #endif // _SG_MAT_HXX