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