]> git.mxchange.org Git - simgear.git/blob - simgear/scene/material/mat.hxx
scenery: Really, most people don't care for the noise.
[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 #ifndef __cplusplus
30 # error This library requires C++
31 #endif
32
33 #include <simgear/compiler.h>
34
35 #include <string>      // Standard C++ string library
36 #include <vector>
37 #include <map>
38
39 #include <simgear/math/SGMath.hxx>
40 #include "Effect.hxx"
41 #include <simgear/scene/tgdb/SGTexturedTriangleBin.hxx>
42
43 #include <osg/ref_ptr>
44 #include <osg/Texture2D>
45
46 namespace osg
47 {
48 class StateSet;
49 }
50
51 #include <simgear/scene/util/SGReaderWriterOptions.hxx>
52 #include <simgear/props/props.hxx>
53 #include <simgear/structure/SGSharedPtr.hxx>
54 #include <simgear/scene/util/SGSceneFeatures.hxx>
55 #include <simgear/props/condition.hxx>
56
57 #include "matmodel.hxx"
58
59 namespace simgear
60 {
61 class Effect;
62 void reload_shaders();
63 }
64
65 class SGMaterialGlyph;
66
67 /**
68  * A material in the scene graph.
69  *
70  * A material represents information about a single surface type
71  * in the 3D scene graph, including texture, colour, lighting,
72  * tiling, and so on; most of the materials in FlightGear are
73  * defined in the $FG_ROOT/materials.xml file, and can be changed
74  * at runtime.
75  */
76 class SGMaterial : public SGReferenced {
77
78 public:
79
80 \f
81   ////////////////////////////////////////////////////////////////////
82   // Public Constructors.
83   ////////////////////////////////////////////////////////////////////
84
85   /**
86    * Construct a material from a set of properties.
87    *
88    * @param props A property node containing subnodes with the
89    * state information for the material.  This node is usually
90    * loaded from the $FG_ROOT/materials.xml file.
91    */
92   SGMaterial( const osgDB::Options*, 
93               const SGPropertyNode *props, 
94               SGPropertyNode *prop_root);
95
96   SGMaterial(const simgear::SGReaderWriterOptions*,
97              const SGPropertyNode *props,
98              SGPropertyNode *prop_root);
99   /**
100    * Destructor.
101    */
102   ~SGMaterial( void );
103
104
105 \f
106   ////////////////////////////////////////////////////////////////////
107   // Public methods.
108   ////////////////////////////////////////////////////////////////////
109
110   /**
111    * Get the textured state.
112    */
113   simgear::Effect* get_effect(SGTexturedTriangleBin triangleBin);
114   simgear::Effect* get_effect();
115
116   /**
117    * Get the textured state.
118    */
119   osg::Texture2D* get_object_mask(SGTexturedTriangleBin triangleBin);
120
121
122   /**
123    * Get the number of textures assigned to this material.
124    */
125   inline int get_num() const { return _status.size(); }
126
127
128   /**
129    * Get the xsize of the texture, in meters.
130    */
131   inline double get_xsize() const { return xsize; }
132
133
134   /**
135    * Get the ysize of the texture, in meters.
136    */
137   inline double get_ysize() const { return ysize; }
138
139
140   /**
141    * Get the light coverage.
142    *
143    * A smaller number means more generated night lighting.
144    *
145    * @return The area (m^2) covered by each light.
146    */
147   inline double get_light_coverage () const { return light_coverage; }
148
149   /**
150    * Get the wood coverage.
151    *
152    * A smaller number means more generated woods within the forest.
153    *
154    * @return The area (m^2) covered by each wood.
155    */
156   inline double get_wood_coverage () const { return wood_coverage; }
157   
158   /**
159    * Get the tree height.
160    *
161    * @return The average height of the trees.
162    */
163   inline double get_tree_height () const { return tree_height; }
164
165   /**
166    * Get the tree width.
167    *
168    * @return The average width of the trees.
169    */
170   inline double get_tree_width () const { return tree_width; }
171
172   /**
173    * Get the forest LoD range.
174    *
175    * @return The LoD range for the trees.
176    */
177   inline double get_tree_range () const { return tree_range; }
178   
179   /**
180    * Get the number of tree varieties available
181    *
182    * @return the number of different trees defined in the texture strip
183    */
184   inline int get_tree_varieties () const { return tree_varieties; }
185   
186   /**
187    * Get the texture strip to use for trees
188    *
189    * @return the texture to use for trees.
190    */
191   inline std::string get_tree_texture () const { return  tree_texture; }
192   
193   /**
194    * Return if the surface material is solid, if it is not solid, a fluid
195    * can be assumed, that is usually water.
196    */
197   bool get_solid () const { return solid; }
198
199   /**
200    * Get the friction factor for that material
201    */
202   double get_friction_factor () const { return friction_factor; }
203
204   /**
205    * Get the rolling friction for that material
206    */
207   double get_rolling_friction () const { return rolling_friction; }
208
209   /**
210    * Get the bumpines for that material
211    */
212   double get_bumpiness () const { return bumpiness; }
213
214   /**
215    * Get the load resistance
216    */
217   double get_load_resistance () const { return load_resistance; }
218
219   /**
220    * Get the list of names for this material
221    */
222   const std::vector<std::string>& get_names() const { return _names; }
223
224   /**
225    * add the given name to the list of names this material is known
226    */
227   void add_name(const std::string& name) { _names.push_back(name); }
228
229   /**
230    * Get the number of randomly-placed objects defined for this material.
231    */
232   int get_object_group_count () const { return object_groups.size(); }
233
234   /**
235    * Get a randomly-placed object for this material.
236    */
237   SGMatModelGroup * get_object_group (int index) const {
238     return object_groups[index];
239   }
240   
241   /**
242    * Evaluate whether this material is valid given the current global
243    * property state.
244    */
245    bool valid() { 
246      if (condition) {
247        return condition->test();       
248      } else {
249        return true;
250      }
251    }
252
253   /**
254    * Return pointer to glyph class, or 0 if it doesn't exist.
255    */
256   SGMaterialGlyph * get_glyph (const std::string& name) const;
257
258   void set_light_color(const SGVec4f& color)
259   { emission = color; }
260   const SGVec4f& get_light_color() const
261   { return emission; }
262
263   SGVec2f get_tex_coord_scale() const
264   {
265     float tex_width = get_xsize();
266     float tex_height = get_ysize();
267
268     return SGVec2f((0 < tex_width) ? 1000.0f/tex_width : 1.0f,
269                    (0 < tex_height) ? 1000.0f/tex_height : 1.0f);
270   }
271
272 protected:
273
274 \f
275   ////////////////////////////////////////////////////////////////////
276   // Protected methods.
277   ////////////////////////////////////////////////////////////////////
278
279   /**
280    * Initialization method, invoked by all public constructors.
281    */
282   void init();
283
284 protected:
285
286   struct _internal_state {
287       _internal_state(simgear::Effect *e, bool l,
288                       const simgear::SGReaderWriterOptions *o);
289       _internal_state(simgear::Effect *e, const std::string &t, bool l,
290                       const simgear::SGReaderWriterOptions *o);
291       void add_texture(const std::string &t, int i);
292       osg::ref_ptr<simgear::Effect> effect;
293       std::vector<std::pair<std::string,int> > texture_paths;
294       bool effect_realized;
295       osg::ref_ptr<const simgear::SGReaderWriterOptions> options;
296   };
297
298 private:
299
300 \f
301   ////////////////////////////////////////////////////////////////////
302   // Internal state.
303   ////////////////////////////////////////////////////////////////////
304
305   // texture status
306   std::vector<_internal_state> _status;
307
308   // texture size
309   double xsize, ysize;
310
311   // wrap texture?
312   bool wrapu, wrapv;
313
314   // use mipmapping?
315   bool mipmap;
316
317   // coverage of night lighting.
318   double light_coverage;
319   
320   // coverage of woods
321   double wood_coverage;
322
323   // Range at which trees become visible
324   double tree_range;
325
326   // Height of the tree
327   double tree_height;
328
329   // Width of the tree
330   double tree_width;
331
332   // Number of varieties of tree texture
333   int tree_varieties;
334
335   // True if the material is solid, false if it is a fluid
336   bool solid;
337
338   // the friction factor of that surface material
339   double friction_factor;
340
341   // the rolling friction of that surface material
342   double rolling_friction;
343
344   // the bumpiness of that surface material
345   double bumpiness;
346
347   // the load resistance of that surface material
348   double load_resistance;
349
350   // material properties
351   SGVec4f ambient, diffuse, specular, emission;
352   double shininess;
353
354   // effect for this material
355   std::string effect;
356
357   // the list of names for this material. May be empty.
358   std::vector<std::string> _names;
359
360   std::vector<SGSharedPtr<SGMatModelGroup> > object_groups;
361
362   // taxiway-/runway-sign texture elements
363   std::map<std::string, SGSharedPtr<SGMaterialGlyph> > glyphs;
364   
365   // Tree texture, typically a strip of applicable tree textures
366   std::string tree_texture;
367   
368   // Object mask, a simple RGB texture used as a mask when placing
369   // random vegetation, objects and buildings
370   std::vector<osg::Texture2D*> _masks;
371   
372   // Condition, indicating when this material is active
373   SGSharedPtr<const SGCondition> condition;
374
375   ////////////////////////////////////////////////////////////////////
376   // Internal constructors and methods.
377   ////////////////////////////////////////////////////////////////////
378
379   void read_properties(const simgear::SGReaderWriterOptions* options,
380                         const SGPropertyNode *props,
381                         SGPropertyNode *prop_root);
382   void buildEffectProperties(const simgear::SGReaderWriterOptions* options);
383   simgear::Effect* get_effect(int i);
384 };
385
386
387 class SGMaterialGlyph : public SGReferenced {
388 public:
389   SGMaterialGlyph(SGPropertyNode *);
390   inline double get_left() const { return _left; }
391   inline double get_right() const { return _right; }
392   inline double get_width() const { return _right - _left; }
393
394 protected:
395   double _left;
396   double _right;
397 };
398
399 class SGMaterialUserData : public osg::Referenced {
400 public:
401   SGMaterialUserData(const SGMaterial* material) :
402     mMaterial(material)
403   {}
404   const SGMaterial* getMaterial() const
405   { return mMaterial; }
406 private:
407   // this cannot be an SGSharedPtr since that would create a cicrular reference
408   // making it impossible to ever free the space needed by SGMaterial
409   const SGMaterial* mMaterial;
410 };
411
412 void
413 SGSetTextureFilter( int max);
414
415 int
416 SGGetTextureFilter();
417
418 #endif // _SG_MAT_HXX