]> git.mxchange.org Git - simgear.git/blob - simgear/scene/material/mat.hxx
Ugly hack to please Boost 1.51.0
[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/math/SGMath.hxx>
47 #include <simgear/bvh/BVHMaterial.hxx>
48
49 namespace simgear
50 {
51 class Effect;
52 void reload_shaders();
53 class SGReaderWriterOptions;
54 }
55
56 class SGMatModelGroup;
57 class SGCondition;
58 class SGPropertyNode;
59 class SGMaterialGlyph;
60 class SGTexturedTriangleBin;
61
62 /**
63  * A material in the scene graph.
64  *
65  * A material represents information about a single surface type
66  * in the 3D scene graph, including texture, colour, lighting,
67  * tiling, and so on; most of the materials in FlightGear are
68  * defined in the $FG_ROOT/materials.xml file, and can be changed
69  * at runtime.
70  */
71 class SGMaterial : public simgear::BVHMaterial {
72
73 public:
74
75 \f
76   ////////////////////////////////////////////////////////////////////
77   // Public Constructors.
78   ////////////////////////////////////////////////////////////////////
79
80   /**
81    * Construct a material from a set of properties.
82    *
83    * @param props A property node containing subnodes with the
84    * state information for the material.  This node is usually
85    * loaded from the $FG_ROOT/materials.xml file.
86    */
87   SGMaterial( const osgDB::Options*, 
88               const SGPropertyNode *props, 
89               SGPropertyNode *prop_root);
90
91   SGMaterial(const simgear::SGReaderWriterOptions*,
92              const SGPropertyNode *props,
93              SGPropertyNode *prop_root);
94   /**
95    * Destructor.
96    */
97   ~SGMaterial( void );
98
99
100 \f
101   ////////////////////////////////////////////////////////////////////
102   // Public methods.
103   ////////////////////////////////////////////////////////////////////
104
105   /**
106    * Get the textured state.
107    */
108   simgear::Effect* get_effect(const SGTexturedTriangleBin& triangleBin);
109   simgear::Effect* get_effect();
110
111   /**
112    * Get the textured state.
113    */
114   osg::Texture2D* get_object_mask(const SGTexturedTriangleBin& triangleBin);
115
116
117   /**
118    * Get the number of textures assigned to this material.
119    */
120   inline int get_num() const { return _status.size(); }
121
122
123   /**
124    * Get the xsize of the texture, in meters.
125    */
126   inline double get_xsize() const { return xsize; }
127
128
129   /**
130    * Get the ysize of the texture, in meters.
131    */
132   inline double get_ysize() const { return ysize; }
133
134
135   /**
136    * Get the light coverage.
137    *
138    * A smaller number means more generated night lighting.
139    *
140    * @return The area (m^2) covered by each light.
141    */
142   inline double get_light_coverage () const { return light_coverage; }
143   
144   /**
145    * Get the building coverage.
146    *
147    * A smaller number means more generated buildings.
148    *
149    * @return The area (m^2) covered by each light.
150    */
151   inline double get_building_coverage () const { return building_coverage; }
152
153   /**
154    * Get the building spacing.
155    *
156    * This is the minimum spacing between buildings
157    *
158    * @return The minimum distance between buildings
159    */
160   inline double get_building_spacing () const { return building_spacing; }
161
162   /**
163    * Get the building texture.
164    *
165    * This is the texture used for auto-generated buildings.
166    *
167    * @return The texture for auto-generated buildings.
168    */
169   inline std::string get_building_texture () const { return building_texture; }
170
171   /**
172    * Get the building lightmap.
173    *
174    * This is the lightmap used for auto-generated buildings.
175    *
176    * @return The lightmap for auto-generated buildings.
177    */
178   inline std::string get_building_lightmap () const { return building_lightmap; }
179   
180   // Ratio of the 3 random building sizes
181   inline double get_building_small_fraction () const { return building_small_ratio / (building_small_ratio + building_medium_ratio + building_large_ratio); }
182   inline double get_building_medium_fraction () const { return building_medium_ratio / (building_small_ratio + building_medium_ratio + building_large_ratio); }
183   inline double get_building_large_fraction () const { return building_large_ratio / (building_small_ratio + building_medium_ratio + building_large_ratio); }
184   
185   // Proportion of buildings with pitched roofs
186   inline double get_building_small_pitch () const { return building_small_pitch; }
187   inline double get_building_medium_pitch () const { return building_medium_pitch; }
188   inline double get_building_large_pitch () const { return building_large_pitch; }
189
190   // Min/Max number of floors for each size
191   inline int get_building_small_min_floors () const { return  building_small_min_floors; }
192   inline int get_building_small_max_floors () const { return  building_small_max_floors; }
193   inline int get_building_medium_min_floors () const { return building_medium_min_floors; }
194   inline int get_building_medium_max_floors () const { return building_medium_max_floors; }
195   inline int get_building_large_min_floors () const { return building_large_min_floors; }
196   inline int get_building_large_max_floors () const { return building_large_max_floors; }
197   
198   // Minimum width and depth for each size
199   inline double get_building_small_min_width () const { return building_small_min_width; }
200   inline double get_building_small_max_width () const { return building_small_max_width; }
201   inline double get_building_small_min_depth () const { return building_small_min_depth; }
202   inline double get_building_small_max_depth () const { return building_small_max_depth; }
203   
204   inline double get_building_medium_min_width () const { return building_medium_min_width; }
205   inline double get_building_medium_max_width () const { return building_medium_max_width; }
206   inline double get_building_medium_min_depth () const { return building_medium_min_depth; }
207   inline double get_building_medium_max_depth () const { return building_medium_max_depth; }
208   
209   inline double get_building_large_min_width () const { return building_large_min_width; }
210   inline double get_building_large_max_width () const { return building_large_max_width; }
211   inline double get_building_large_min_depth () const { return building_large_min_depth; }
212   inline double get_building_large_max_depth () const { return building_large_max_depth; }
213
214   /**
215    * Get the wood coverage.
216    *
217    * A smaller number means more generated woods within the forest.
218    *
219    * @return The area (m^2) covered by each wood.
220    */
221   inline double get_wood_coverage () const { return wood_coverage; }
222   
223   /**
224    * Get the tree height.
225    *
226    * @return The average height of the trees.
227    */
228   inline double get_tree_height () const { return tree_height; }
229
230   /**
231    * Get the tree width.
232    *
233    * @return The average width of the trees.
234    */
235   inline double get_tree_width () const { return tree_width; }
236
237   /**
238    * Get the forest LoD range.
239    *
240    * @return The LoD range for the trees.
241    */
242   inline double get_tree_range () const { return tree_range; }
243   
244   /**
245    * Get the number of tree varieties available
246    *
247    * @return the number of different trees defined in the texture strip
248    */
249   inline int get_tree_varieties () const { return tree_varieties; }
250   
251   /**
252    * Get the texture strip to use for trees
253    *
254    * @return the texture to use for trees.
255    */
256   inline std::string get_tree_texture () const { return  tree_texture; }
257   
258   /**
259    * Get the list of names for this material
260    */
261   const std::vector<std::string>& get_names() const { return _names; }
262
263   /**
264    * add the given name to the list of names this material is known
265    */
266   void add_name(const std::string& name) { _names.push_back(name); }
267
268   /**
269    * Get the number of randomly-placed objects defined for this material.
270    */
271   int get_object_group_count () const { return object_groups.size(); }
272
273   /**
274    * Get a randomly-placed object for this material.
275    */
276   SGMatModelGroup * get_object_group (int index) const {
277     return object_groups[index];
278   }
279   
280   /**
281    * Evaluate whether this material is valid given the current global
282    * property state.
283    */
284      bool valid() const;
285
286   /**
287    * Return pointer to glyph class, or 0 if it doesn't exist.
288    */
289   SGMaterialGlyph * get_glyph (const std::string& name) const;
290
291   void set_light_color(const SGVec4f& color)
292   { emission = color; }
293   const SGVec4f& get_light_color() const
294   { return emission; }
295
296   SGVec2f get_tex_coord_scale() const
297   {
298     float tex_width = get_xsize();
299     float tex_height = get_ysize();
300
301     return SGVec2f((0 < tex_width) ? 1000.0f/tex_width : 1.0f,
302                    (0 < tex_height) ? 1000.0f/tex_height : 1.0f);
303   }
304
305 protected:
306
307 \f
308   ////////////////////////////////////////////////////////////////////
309   // Protected methods.
310   ////////////////////////////////////////////////////////////////////
311
312   /**
313    * Initialization method, invoked by all public constructors.
314    */
315   void init();
316
317 protected:
318
319   struct _internal_state {
320       _internal_state(simgear::Effect *e, bool l,
321                       const simgear::SGReaderWriterOptions *o);
322       _internal_state(simgear::Effect *e, const std::string &t, bool l,
323                       const simgear::SGReaderWriterOptions *o);
324       void add_texture(const std::string &t, int i);
325       osg::ref_ptr<simgear::Effect> effect;
326       std::vector<std::pair<std::string,int> > texture_paths;
327       bool effect_realized;
328       osg::ref_ptr<const simgear::SGReaderWriterOptions> options;
329   };
330
331 private:
332
333 \f
334   ////////////////////////////////////////////////////////////////////
335   // Internal state.
336   ////////////////////////////////////////////////////////////////////
337
338   // texture status
339   std::vector<_internal_state> _status;
340
341   // texture size
342   double xsize, ysize;
343
344   // wrap texture?
345   bool wrapu, wrapv;
346
347   // use mipmapping?
348   bool mipmap;
349
350   // coverage of night lighting.
351   double light_coverage;
352   
353   // coverage of buildings
354   double building_coverage;
355   
356   // building spacing
357   double building_spacing;
358   
359   // building texture & lightmap
360   std::string building_texture;
361   std::string building_lightmap;
362
363   // Ratio of the 3 random building sizes
364   double building_small_ratio;
365   double building_medium_ratio;
366   double building_large_ratio;
367   
368   // Proportion of buildings with pitched roofs
369   double building_small_pitch;
370   double building_medium_pitch;
371   double building_large_pitch;
372
373   // Min/Max number of floors for each size
374   int building_small_min_floors; 
375   int building_small_max_floors;
376   int building_medium_min_floors;
377   int building_medium_max_floors;
378   int building_large_min_floors;
379   int building_large_max_floors;
380   
381   // Minimum width and depth for each size
382   double building_small_min_width;
383   double building_small_max_width;
384   double building_small_min_depth;
385   double building_small_max_depth;
386   
387   double building_medium_min_width;
388   double building_medium_max_width;
389   double building_medium_min_depth;
390   double building_medium_max_depth;
391   
392   double building_large_min_width;
393   double building_large_max_width;
394   double building_large_min_depth;
395   double building_large_max_depth;
396   
397   // coverage of woods
398   double wood_coverage;
399
400   // Range at which trees become visible
401   double tree_range;
402
403   // Height of the tree
404   double tree_height;
405
406   // Width of the tree
407   double tree_width;
408
409   // Number of varieties of tree texture
410   int tree_varieties;
411
412   // material properties
413   SGVec4f ambient, diffuse, specular, emission;
414   double shininess;
415
416   // effect for this material
417   std::string effect;
418
419   // the list of names for this material. May be empty.
420   std::vector<std::string> _names;
421
422   std::vector<SGSharedPtr<SGMatModelGroup> > object_groups;
423
424   // taxiway-/runway-sign texture elements
425   std::map<std::string, SGSharedPtr<SGMaterialGlyph> > glyphs;
426   
427   // Tree texture, typically a strip of applicable tree textures
428   std::string tree_texture;
429   
430   // Object mask, a simple RGB texture used as a mask when placing
431   // random vegetation, objects and buildings
432   std::vector<osg::Texture2D*> _masks;
433   
434   // Condition, indicating when this material is active
435   SGSharedPtr<const SGCondition> condition;
436   
437   // Parameters from the materials file
438   const SGPropertyNode* parameters;
439
440   ////////////////////////////////////////////////////////////////////
441   // Internal constructors and methods.
442   ////////////////////////////////////////////////////////////////////
443
444   void read_properties(const simgear::SGReaderWriterOptions* options,
445                         const SGPropertyNode *props,
446                         SGPropertyNode *prop_root);
447   void buildEffectProperties(const simgear::SGReaderWriterOptions* options);
448   simgear::Effect* get_effect(int i);
449 };
450
451
452 class SGMaterialGlyph : public SGReferenced {
453 public:
454   SGMaterialGlyph(SGPropertyNode *);
455   inline double get_left() const { return _left; }
456   inline double get_right() const { return _right; }
457   inline double get_width() const { return _right - _left; }
458
459 protected:
460   double _left;
461   double _right;
462 };
463
464 class SGMaterialUserData : public osg::Referenced {
465 public:
466   SGMaterialUserData(const SGMaterial* material) :
467     mMaterial(material)
468   {}
469   const SGMaterial* getMaterial() const
470   { return mMaterial; }
471 private:
472   // this cannot be an SGSharedPtr since that would create a cicrular reference
473   // making it impossible to ever free the space needed by SGMaterial
474   const SGMaterial* mMaterial;
475 };
476
477 void
478 SGSetTextureFilter( int max);
479
480 int
481 SGGetTextureFilter();
482
483 #endif // _SG_MAT_HXX