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