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