]> git.mxchange.org Git - simgear.git/blob - simgear/scene/material/mat.hxx
pass SGReaderWriterXMLOptions to effects
[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 #include <simgear/scene/model/SGReaderWriterXMLOptions.hxx>
49 #include <simgear/props/props.hxx>
50 #include <simgear/structure/SGSharedPtr.hxx>
51 #include <simgear/scene/util/SGSceneFeatures.hxx>
52
53 #include "matmodel.hxx"
54
55 namespace simgear
56 {
57 class Effect;
58 void reload_shaders();
59 }
60
61 class SGMaterialGlyph;
62
63 /**
64  * A material in the scene graph.
65  *
66  * A material represents information about a single surface type
67  * in the 3D scene graph, including texture, colour, lighting,
68  * tiling, and so on; most of the materials in FlightGear are
69  * defined in the $FG_ROOT/materials.xml file, and can be changed
70  * at runtime.
71  */
72 class SGMaterial : public SGReferenced {
73
74 public:
75
76 \f
77   ////////////////////////////////////////////////////////////////////
78   // Public Constructors.
79   ////////////////////////////////////////////////////////////////////
80
81   /**
82    * Construct a material from a set of properties.
83    *
84    * @param props A property node containing subnodes with the
85    * state information for the material.  This node is usually
86    * loaded from the $FG_ROOT/materials.xml file.
87    */
88   SGMaterial( const osgDB::ReaderWriter::Options*, const SGPropertyNode *props);
89
90   SGMaterial(const simgear::SGReaderWriterXMLOptions*,
91              const SGPropertyNode *props);
92   /**
93    * Destructor.
94    */
95   ~SGMaterial( void );
96
97
98 \f
99   ////////////////////////////////////////////////////////////////////
100   // Public methods.
101   ////////////////////////////////////////////////////////////////////
102
103   /**
104    * Get the textured state.
105    */
106   simgear::Effect *get_effect(int n = -1);
107
108   /**
109    * Get the number of textures assigned to this material.
110    */
111   inline int get_num() const { return _status.size(); }
112
113
114   /**
115    * Get the xsize of the texture, in meters.
116    */
117   inline double get_xsize() const { return xsize; }
118
119
120   /**
121    * Get the ysize of the texture, in meters.
122    */
123   inline double get_ysize() const { return ysize; }
124
125
126   /**
127    * Get the light coverage.
128    *
129    * A smaller number means more generated night lighting.
130    *
131    * @return The area (m^2) covered by each light.
132    */
133   inline double get_light_coverage () const { return light_coverage; }
134
135   /**
136    * Get the wood coverage.
137    *
138    * A smaller number means more generated woods within the forest.
139    *
140    * @return The area (m^2) covered by each wood.
141    */
142   inline double get_wood_coverage () const { return wood_coverage; }
143
144   /**
145    * Get the density of the wood
146    *
147    * @return The area (m^2) covered by each tree in the wood.
148    */
149   inline double get_tree_density () const { return tree_density; }
150   
151   /**
152    * Get the size of each wood
153    *
154    * @return the average area (m^2) of each wood
155    */
156   inline double get_wood_size () const { return wood_size; }
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    * Return pointer to glyph class, or 0 if it doesn't exist.
243    */
244   SGMaterialGlyph * get_glyph (const std::string& name) const;
245
246   void set_light_color(const SGVec4f& color)
247   { emission = color; }
248   const SGVec4f& get_light_color() const
249   { return emission; }
250
251   SGVec2f get_tex_coord_scale() const
252   {
253     float tex_width = get_xsize();
254     float tex_height = get_ysize();
255
256     return SGVec2f((0 < tex_width) ? 1000.0f/tex_width : 1.0f,
257                    (0 < tex_height) ? 1000.0f/tex_height : 1.0f);
258   }
259
260 protected:
261
262 \f
263   ////////////////////////////////////////////////////////////////////
264   // Protected methods.
265   ////////////////////////////////////////////////////////////////////
266
267   /**
268    * Initialization method, invoked by all public constructors.
269    */
270   void init();
271
272 protected:
273
274   struct _internal_state {
275       _internal_state(simgear::Effect *e, const std::string &t, bool l,
276                       const simgear::SGReaderWriterXMLOptions *o);
277       osg::ref_ptr<simgear::Effect> effect;
278       std::string texture_path;
279       bool effect_realized;
280       osg::ref_ptr<const simgear::SGReaderWriterXMLOptions> options;
281   };
282
283 private:
284
285 \f
286   ////////////////////////////////////////////////////////////////////
287   // Internal state.
288   ////////////////////////////////////////////////////////////////////
289
290   // texture status
291   std::vector<_internal_state> _status;
292
293   // Round-robin counter
294   mutable unsigned int _current_ptr;
295
296   // texture size
297   double xsize, ysize;
298
299   // wrap texture?
300   bool wrapu, wrapv;
301
302   // use mipmapping?
303   bool mipmap;
304
305   // coverage of night lighting.
306   double light_coverage;
307   
308   // coverage of woods
309   double wood_coverage;
310
311   // The size of each wood
312   double wood_size;
313
314   // Tree density within the wood
315   double tree_density;
316
317   // Range at which trees become visible
318   double tree_range;
319
320   // Height of the tree
321   double tree_height;
322
323   // Width of the tree
324   double tree_width;
325
326   // Number of varieties of tree texture
327   int tree_varieties;
328
329   // True if the material is solid, false if it is a fluid
330   bool solid;
331
332   // the friction factor of that surface material
333   double friction_factor;
334
335   // the rolling friction of that surface material
336   double rolling_friction;
337
338   // the bumpiness of that surface material
339   double bumpiness;
340
341   // the load resistance of that surface material
342   double load_resistance;
343
344   // material properties
345   SGVec4f ambient, diffuse, specular, emission;
346   double shininess;
347
348   // effect for this material
349   std::string effect;
350
351   // the list of names for this material. May be empty.
352   std::vector<std::string> _names;
353
354   std::vector<SGSharedPtr<SGMatModelGroup> > object_groups;
355
356   // taxiway-/runway-sign texture elements
357   std::map<std::string, SGSharedPtr<SGMaterialGlyph> > glyphs;
358   
359   // Tree texture, typically a strip of applicable tree textures
360   std::string tree_texture;
361 \f
362   ////////////////////////////////////////////////////////////////////
363   // Internal constructors and methods.
364   ////////////////////////////////////////////////////////////////////
365
366   void read_properties(const simgear::SGReaderWriterXMLOptions* options,
367                         const SGPropertyNode *props);
368   void buildEffectProperties(const simgear::SGReaderWriterXMLOptions* options);
369 };
370
371
372 class SGMaterialGlyph : public SGReferenced {
373 public:
374   SGMaterialGlyph(SGPropertyNode *);
375   inline double get_left() const { return _left; }
376   inline double get_right() const { return _right; }
377   inline double get_width() const { return _right - _left; }
378
379 protected:
380   double _left;
381   double _right;
382 };
383
384 class SGMaterialUserData : public osg::Referenced {
385 public:
386   SGMaterialUserData(const SGMaterial* material) :
387     mMaterial(material)
388   {}
389   const SGMaterial* getMaterial() const
390   { return mMaterial; }
391 private:
392   // this cannot be an SGSharedPtr since that would create a cicrular reference
393   // making it impossible to ever free the space needed by SGMaterial
394   const SGMaterial* mMaterial;
395 };
396
397 void
398 SGSetTextureFilter( int max);
399
400 int
401 SGGetTextureFilter();
402
403 #endif // _SG_MAT_HXX