]> git.mxchange.org Git - simgear.git/blob - simgear/scene/material/mat.hxx
Fix leak of object-mask textures
[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
46 typedef osg::ref_ptr<osg::Texture2D> Texture2DRef;
47     
48 #include <simgear/structure/SGSharedPtr.hxx>
49 #include <simgear/threads/SGThread.hxx> // for SGMutex
50 #include <simgear/math/SGMath.hxx>
51 #include <simgear/bvh/BVHMaterial.hxx>
52
53 namespace simgear
54 {
55 class Effect;
56 void reload_shaders();
57 class SGReaderWriterOptions;
58 }
59
60 class SGMatModelGroup;
61 class SGCondition;
62 class SGPropertyNode;
63 class SGMaterialGlyph;
64 class SGTexturedTriangleBin;
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 simgear::BVHMaterial {
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(const SGTexturedTriangleBin& triangleBin);
113   simgear::Effect* get_effect();
114
115   /**
116    * Get the textured state.
117    */
118   osg::Texture2D* get_object_mask(const 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   inline double get_building_range () const { return building_range; }
219   
220   inline double get_cos_object_max_density_slope_angle () const { return cos_object_max_density_slope_angle; }
221   inline double get_cos_object_zero_density_slope_angle () const { return cos_object_zero_density_slope_angle; }
222
223   /**
224    * Get the wood coverage.
225    *
226    * A smaller number means more generated woods within the forest.
227    *
228    * @return The area (m^2) covered by each wood.
229    */
230   inline double get_wood_coverage () const { return wood_coverage; }
231   
232   /**
233    * Get the tree height.
234    *
235    * @return The average height of the trees.
236    */
237   inline double get_tree_height () const { return tree_height; }
238
239   /**
240    * Get the tree width.
241    *
242    * @return The average width of the trees.
243    */
244   inline double get_tree_width () const { return tree_width; }
245
246   /**
247    * Get the forest LoD range.
248    *
249    * @return The LoD range for the trees.
250    */
251   inline double get_tree_range () const { return tree_range; }
252   
253   /**
254    * Get the number of tree varieties available
255    *
256    * @return the number of different trees defined in the texture strip
257    */
258   inline int get_tree_varieties () const { return tree_varieties; }
259   
260   /**
261    * Get the texture strip to use for trees
262    *
263    * @return the texture to use for trees.
264    */
265   inline std::string get_tree_texture () const { return  tree_texture; }
266   
267   /**
268    * Get the cosine of the maximum tree density slope angle. We
269    * use the cosine as it can be compared directly to the z component
270    * of a triangle normal.
271    * 
272    * @return the cosine of the maximum tree density slope angle.
273    */
274   inline double get_cos_tree_max_density_slope_angle () const { return cos_tree_max_density_slope_angle; }
275   
276   /**
277    * Get the cosine of the maximum tree density slope angle. We
278    * use the cosine as it can be compared directly to the z component
279    * of a triangle normal.
280    * 
281    * @return the cosine of the maximum tree density slope angle.
282    */
283   inline double get_cos_tree_zero_density_slope_angle () const { return cos_tree_zero_density_slope_angle; }
284   
285   /**
286    * Get the list of names for this material
287    */
288   const std::vector<std::string>& get_names() const { return _names; }
289
290   /**
291    * add the given name to the list of names this material is known
292    */
293   void add_name(const std::string& name) { _names.push_back(name); }
294
295   /**
296    * Get the number of randomly-placed objects defined for this material.
297    */
298   int get_object_group_count () const { return object_groups.size(); }
299
300   /**
301    * Get a randomly-placed object for this material.
302    */
303   SGMatModelGroup * get_object_group (int index) const {
304     return object_groups[index];
305   }
306   
307   /**
308    * Evaluate whether this material is valid given the current global
309    * property state.
310    */
311      bool valid() const;
312
313   /**
314    * Return pointer to glyph class, or 0 if it doesn't exist.
315    */
316   SGMaterialGlyph * get_glyph (const std::string& name) const;
317
318   void set_light_color(const SGVec4f& color)
319   { emission = color; }
320   const SGVec4f& get_light_color() const
321   { return emission; }
322
323   SGVec2f get_tex_coord_scale() const
324   {
325     float tex_width = get_xsize();
326     float tex_height = get_ysize();
327
328     return SGVec2f((0 < tex_width) ? 1000.0f/tex_width : 1.0f,
329                    (0 < tex_height) ? 1000.0f/tex_height : 1.0f);
330   }
331   
332 protected:
333
334 \f
335   ////////////////////////////////////////////////////////////////////
336   // Protected methods.
337   ////////////////////////////////////////////////////////////////////
338
339   /**
340    * Initialization method, invoked by all public constructors.
341    */
342   void init();
343
344 protected:
345
346   struct _internal_state {
347       _internal_state(simgear::Effect *e, bool l,
348                       const simgear::SGReaderWriterOptions *o);
349       _internal_state(simgear::Effect *e, const std::string &t, bool l,
350                       const simgear::SGReaderWriterOptions *o);
351       void add_texture(const std::string &t, int i);
352       osg::ref_ptr<simgear::Effect> effect;
353       std::vector<std::pair<std::string,int> > texture_paths;
354       bool effect_realized;
355       osg::ref_ptr<const simgear::SGReaderWriterOptions> options;
356   };
357
358 private:
359
360 \f
361   ////////////////////////////////////////////////////////////////////
362   // Internal state.
363   ////////////////////////////////////////////////////////////////////
364
365   // texture status
366   std::vector<_internal_state> _status;
367
368   // texture size
369   double xsize, ysize;
370
371   // wrap texture?
372   bool wrapu, wrapv;
373
374   // use mipmapping?
375   bool mipmap;
376
377   // coverage of night lighting.
378   double light_coverage;
379   
380   // coverage of buildings
381   double building_coverage;
382   
383   // building spacing
384   double building_spacing;
385   
386   // building texture & lightmap
387   std::string building_texture;
388   std::string building_lightmap;
389
390   // Ratio of the 3 random building sizes
391   double building_small_ratio;
392   double building_medium_ratio;
393   double building_large_ratio;
394   
395   // Proportion of buildings with pitched roofs
396   double building_small_pitch;
397   double building_medium_pitch;
398   double building_large_pitch;
399
400   // Min/Max number of floors for each size
401   int building_small_min_floors; 
402   int building_small_max_floors;
403   int building_medium_min_floors;
404   int building_medium_max_floors;
405   int building_large_min_floors;
406   int building_large_max_floors;
407   
408   // Minimum width and depth for each size
409   double building_small_min_width;
410   double building_small_max_width;
411   double building_small_min_depth;
412   double building_small_max_depth;
413   
414   double building_medium_min_width;
415   double building_medium_max_width;
416   double building_medium_min_depth;
417   double building_medium_max_depth;
418   
419   double building_large_min_width;
420   double building_large_max_width;
421   double building_large_min_depth;
422   double building_large_max_depth;
423   
424   double building_range;
425   
426   // Cosine of the angle of maximum and zero density, 
427   // used to stop buildings and random objects from being 
428   // created on too steep a slope.
429   double cos_object_max_density_slope_angle;
430   double cos_object_zero_density_slope_angle;
431   
432   // coverage of woods
433   double wood_coverage;
434
435   // Range at which trees become visible
436   double tree_range;
437
438   // Height of the tree
439   double tree_height;
440
441   // Width of the tree
442   double tree_width;
443
444   // Number of varieties of tree texture
445   int tree_varieties;
446   
447   // cosine of the tile angle of maximum and zero density,
448   // used to stop trees from being created on too steep a slope.
449   double cos_tree_max_density_slope_angle;
450   double cos_tree_zero_density_slope_angle;
451
452   // material properties
453   SGVec4f ambient, diffuse, specular, emission;
454   double shininess;
455
456   // effect for this material
457   std::string effect;
458
459   // the list of names for this material. May be empty.
460   std::vector<std::string> _names;
461
462   std::vector<SGSharedPtr<SGMatModelGroup> > object_groups;
463
464   // taxiway-/runway-sign texture elements
465   std::map<std::string, SGSharedPtr<SGMaterialGlyph> > glyphs;
466   
467   // Tree texture, typically a strip of applicable tree textures
468   std::string tree_texture;
469   
470   // Object mask, a simple RGB texture used as a mask when placing
471   // random vegetation, objects and buildings
472   std::vector<Texture2DRef> _masks;
473   
474   // Condition, indicating when this material is active
475   SGSharedPtr<const SGCondition> condition;
476   
477   // Parameters from the materials file
478   const SGPropertyNode* parameters;
479
480   // per-material lock for entrypoints called from multiple threads
481   SGMutex _lock;
482
483   ////////////////////////////////////////////////////////////////////
484   // Internal constructors and methods.
485   ////////////////////////////////////////////////////////////////////
486
487   void read_properties(const simgear::SGReaderWriterOptions* options,
488                         const SGPropertyNode *props,
489                         SGPropertyNode *prop_root);
490   void buildEffectProperties(const simgear::SGReaderWriterOptions* options);
491   simgear::Effect* get_effect(int i);
492 };
493
494
495 class SGMaterialGlyph : public SGReferenced {
496 public:
497   SGMaterialGlyph(SGPropertyNode *);
498   inline double get_left() const { return _left; }
499   inline double get_right() const { return _right; }
500   inline double get_width() const { return _right - _left; }
501
502 protected:
503   double _left;
504   double _right;
505 };
506
507 class SGMaterialUserData : public osg::Referenced {
508 public:
509   SGMaterialUserData(const SGMaterial* material) :
510     mMaterial(material)
511   {}
512   const SGMaterial* getMaterial() const
513   { return mMaterial; }
514 private:
515   // this cannot be an SGSharedPtr since that would create a cicrular reference
516   // making it impossible to ever free the space needed by SGMaterial
517   const SGMaterial* mMaterial;
518 };
519
520 void
521 SGSetTextureFilter( int max);
522
523 int
524 SGGetTextureFilter();
525
526 #endif // _SG_MAT_HXX