]> git.mxchange.org Git - simgear.git/blob - simgear/scene/material/mat.hxx
Random trees from Stuart Buchanan
[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 STL_STRING      // Standard C++ string library
36 #include <vector>
37 #include <map>
38
39 #include <simgear/math/SGMath.hxx>
40
41 #include <osg/ref_ptr>
42 #include <osg/StateSet>
43
44 #include <simgear/props/props.hxx>
45 #include <simgear/structure/SGSharedPtr.hxx>
46 #include <simgear/scene/util/SGSceneFeatures.hxx>
47
48 #include "matmodel.hxx"
49
50 SG_USING_STD(string);
51 SG_USING_STD(vector);
52 SG_USING_STD(map);
53
54
55 class SGMaterialGlyph;
56
57 /**
58  * A material in the scene graph.
59  *
60  * A material represents information about a single surface type
61  * in the 3D scene graph, including texture, colour, lighting,
62  * tiling, and so on; most of the materials in FlightGear are
63  * defined in the $FG_ROOT/materials.xml file, and can be changed
64  * at runtime.
65  */
66 class SGMaterial : public SGReferenced {
67
68 public:
69
70 \f
71   ////////////////////////////////////////////////////////////////////
72   // Public Constructors.
73   ////////////////////////////////////////////////////////////////////
74
75   /**
76    * Construct a material from a set of properties.
77    *
78    * @param props A property node containing subnodes with the
79    * state information for the material.  This node is usually
80    * loaded from the $FG_ROOT/materials.xml file.
81    */
82   SGMaterial( const string &fg_root, const SGPropertyNode *props, const char *season );
83
84
85   /**
86    * Construct a material from an absolute texture path.
87    *
88    * @param texture_path A string containing an absolute path
89    * to a texture file (usually RGB).
90    */
91   SGMaterial( const string &texpath );
92
93
94   /**
95    * Construct a material around an existing state.
96    *
97    * This constructor allows the application to create a custom,
98    * low-level state for the scene graph and wrap a material around
99    * it.  Note: the pointer ownership is transferred to the material.
100    *
101    * @param s The state for this material.
102    */
103   SGMaterial( osg::StateSet *s );
104
105   /**
106    * Destructor.
107    */
108   ~SGMaterial( void );
109
110
111 \f
112   ////////////////////////////////////////////////////////////////////
113   // Public methods.
114   ////////////////////////////////////////////////////////////////////
115
116   /**
117    * Get the textured state.
118    */
119   osg::StateSet *get_state (int n = -1);
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 forest coverage.
151    *
152    * A smaller number means more generated forest canopy.
153    *
154    * @return The area (m^2) covered by each canopy.
155    */
156   inline double get_tree_coverage () const { return tree_coverage; }
157
158   /**
159    * Get the forest 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 forest 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 list of textures to use for trees in the forest
181    *
182    * @return the vector of forest textures to use.
183    */
184   inline vector<string> get_tree_textures () const { return  tree_textures; }
185
186   /**
187    * Return if the surface material is solid, if it is not solid, a fluid
188    * can be assumed, that is usually water.
189    */
190   bool get_solid () const { return solid; }
191
192   /**
193    * Get the friction factor for that material
194    */
195   double get_friction_factor () const { return friction_factor; }
196
197   /**
198    * Get the rolling friction for that material
199    */
200   double get_rolling_friction () const { return rolling_friction; }
201
202   /**
203    * Get the bumpines for that material
204    */
205   double get_bumpiness () const { return bumpiness; }
206
207   /**
208    * Get the load resistance
209    */
210   double get_load_resistance () const { return load_resistance; }
211
212   /**
213    * Get the list of names for this material
214    */
215   const vector<string>& get_names() const { return _names; }
216
217   /**
218    * add the given name to the list of names this material is known
219    */
220   void add_name(const string& name) { _names.push_back(name); }
221
222   /**
223    * Get the number of randomly-placed objects defined for this material.
224    */
225   int get_object_group_count () const { return object_groups.size(); }
226
227   /**
228    * Get a randomly-placed object for this material.
229    */
230   SGMatModelGroup * get_object_group (int index) const {
231     return object_groups[index];
232   }
233
234   /**
235    * Return pointer to glyph class, or 0 if it doesn't exist.
236    */
237   SGMaterialGlyph * get_glyph (const string& name) const;
238
239   void set_light_color(const SGVec4f& color)
240   { emission = color; }
241   const SGVec4f& get_light_color() const
242   { return emission; }
243
244   SGVec2f get_tex_coord_scale() const
245   {
246     float tex_width = get_xsize();
247     float tex_height = get_ysize();
248
249     return SGVec2f((0 < tex_width) ? 1000.0f/tex_width : 1.0f,
250                    (0 < tex_height) ? 1000.0f/tex_height : 1.0f);
251   }
252
253 protected:
254
255 \f
256   ////////////////////////////////////////////////////////////////////
257   // Protected methods.
258   ////////////////////////////////////////////////////////////////////
259
260   /**
261    * Initialization method, invoked by all public constructors.
262    */
263   void init();
264
265 protected:
266
267   struct _internal_state {
268       _internal_state( osg::StateSet *s, const string &t, bool l )
269                   : state(s), texture_path(t), texture_loaded(l) {}
270       osg::ref_ptr<osg::StateSet> state;
271       string texture_path;
272       bool texture_loaded;
273   };
274
275 private:
276
277 \f
278   ////////////////////////////////////////////////////////////////////
279   // Internal state.
280   ////////////////////////////////////////////////////////////////////
281
282   // texture status
283   vector<_internal_state> _status;
284
285   // Round-robin counter
286   mutable unsigned int _current_ptr;
287
288   // texture size
289   double xsize, ysize;
290
291   // wrap texture?
292   bool wrapu, wrapv;
293
294   // use mipmapping?
295   int mipmap;
296
297   // coverage of night lighting.
298   double light_coverage;
299   
300   // coverage of trees
301   double tree_coverage;
302   
303   // Range at which trees become visible
304   double tree_range;
305
306   // Height of the tree
307   double tree_height;
308
309   // Width of the tree
310   double tree_width;
311
312   // True if the material is solid, false if it is a fluid
313   bool solid;
314
315   // the friction factor of that surface material
316   double friction_factor;
317
318   // the rolling friction of that surface material
319   double rolling_friction;
320
321   // the bumpiness of that surface material
322   double bumpiness;
323
324   // the load resistance of that surface material
325   double load_resistance;
326
327   // material properties
328   SGVec4f ambient, diffuse, specular, emission;
329   double shininess;
330
331   // the list of names for this material. May be empty.
332   vector<string> _names;
333
334   vector<SGSharedPtr<SGMatModelGroup> > object_groups;
335
336   // taxiway-/runway-sign texture elements
337   map<string, SGSharedPtr<SGMaterialGlyph> > glyphs;
338   
339   // The list of forest textures, used when creating trees
340   vector<string> tree_textures;
341 \f
342   ////////////////////////////////////////////////////////////////////
343   // Internal constructors and methods.
344   ////////////////////////////////////////////////////////////////////
345
346   SGMaterial( const string &fg_root, const SGMaterial &mat ); // unimplemented
347
348   void read_properties( const string &fg_root, const SGPropertyNode *props, const char *season );
349   void build_state( bool defer_tex_load );
350   void set_state( osg::StateSet *s );
351
352   void assignTexture( osg::StateSet *state, const std::string &fname, int _wrapu = TRUE, int _wrapv = TRUE, int _mipmap = TRUE );
353
354 };
355
356
357 class SGMaterialGlyph : public SGReferenced {
358 public:
359   SGMaterialGlyph(SGPropertyNode *);
360   inline double get_left() const { return _left; }
361   inline double get_right() const { return _right; }
362   inline double get_width() const { return _right - _left; }
363
364 protected:
365   double _left;
366   double _right;
367 };
368
369 class SGMaterialUserData : public osg::Referenced {
370 public:
371   SGMaterialUserData(const SGMaterial* material) :
372     mMaterial(material)
373   {}
374   const SGMaterial* getMaterial() const
375   { return mMaterial; }
376 private:
377   // this cannot be an SGSharedPtr since that would create a cicrular reference
378   // making it impossible to ever free the space needed by SGMaterial
379   const SGMaterial* mMaterial;
380 };
381
382 void
383 SGSetTextureFilter( int max);
384
385 int
386 SGGetTextureFilter();
387
388 #endif // _SG_MAT_HXX