]> git.mxchange.org Git - simgear.git/blob - simgear/scene/material/mat.hxx
Allow (ab)using findDataFile to also search directories again.
[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
56 #include "matmodel.hxx"
57
58 namespace simgear
59 {
60 class Effect;
61 void reload_shaders();
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::ReaderWriter::Options*, const SGPropertyNode *props);
92
93   SGMaterial(const simgear::SGReaderWriterOptions*,
94              const SGPropertyNode *props);
95   /**
96    * Destructor.
97    */
98   ~SGMaterial( void );
99
100
101 \f
102   ////////////////////////////////////////////////////////////////////
103   // Public methods.
104   ////////////////////////////////////////////////////////////////////
105
106   /**
107    * Get the textured state.
108    */
109   simgear::Effect* get_effect(SGTexturedTriangleBin triangleBin);
110   simgear::Effect* get_effect();
111
112   /**
113    * Get the textured state.
114    */
115   osg::Texture2D* get_object_mask(SGTexturedTriangleBin triangleBin);
116
117
118   /**
119    * Get the number of textures assigned to this material.
120    */
121   inline int get_num() const { return _status.size(); }
122
123
124   /**
125    * Get the xsize of the texture, in meters.
126    */
127   inline double get_xsize() const { return xsize; }
128
129
130   /**
131    * Get the ysize of the texture, in meters.
132    */
133   inline double get_ysize() const { return ysize; }
134
135
136   /**
137    * Get the light coverage.
138    *
139    * A smaller number means more generated night lighting.
140    *
141    * @return The area (m^2) covered by each light.
142    */
143   inline double get_light_coverage () const { return light_coverage; }
144
145   /**
146    * Get the wood coverage.
147    *
148    * A smaller number means more generated woods within the forest.
149    *
150    * @return The area (m^2) covered by each wood.
151    */
152   inline double get_wood_coverage () const { return wood_coverage; }
153   
154   /**
155    * Get the tree height.
156    *
157    * @return The average height of the trees.
158    */
159   inline double get_tree_height () const { return tree_height; }
160
161   /**
162    * Get the tree width.
163    *
164    * @return The average width of the trees.
165    */
166   inline double get_tree_width () const { return tree_width; }
167
168   /**
169    * Get the forest LoD range.
170    *
171    * @return The LoD range for the trees.
172    */
173   inline double get_tree_range () const { return tree_range; }
174   
175   /**
176    * Get the number of tree varieties available
177    *
178    * @return the number of different trees defined in the texture strip
179    */
180   inline int get_tree_varieties () const { return tree_varieties; }
181   
182   /**
183    * Get the texture strip to use for trees
184    *
185    * @return the texture to use for trees.
186    */
187   inline std::string get_tree_texture () const { return  tree_texture; }
188   
189   /**
190    * Return if the surface material is solid, if it is not solid, a fluid
191    * can be assumed, that is usually water.
192    */
193   bool get_solid () const { return solid; }
194
195   /**
196    * Get the friction factor for that material
197    */
198   double get_friction_factor () const { return friction_factor; }
199
200   /**
201    * Get the rolling friction for that material
202    */
203   double get_rolling_friction () const { return rolling_friction; }
204
205   /**
206    * Get the bumpines for that material
207    */
208   double get_bumpiness () const { return bumpiness; }
209
210   /**
211    * Get the load resistance
212    */
213   double get_load_resistance () const { return load_resistance; }
214
215   /**
216    * Get the list of names for this material
217    */
218   const std::vector<std::string>& get_names() const { return _names; }
219
220   /**
221    * add the given name to the list of names this material is known
222    */
223   void add_name(const std::string& name) { _names.push_back(name); }
224
225   /**
226    * Get the number of randomly-placed objects defined for this material.
227    */
228   int get_object_group_count () const { return object_groups.size(); }
229
230   /**
231    * Get a randomly-placed object for this material.
232    */
233   SGMatModelGroup * get_object_group (int index) const {
234     return object_groups[index];
235   }
236
237   /**
238    * Return pointer to glyph class, or 0 if it doesn't exist.
239    */
240   SGMaterialGlyph * get_glyph (const std::string& name) const;
241
242   void set_light_color(const SGVec4f& color)
243   { emission = color; }
244   const SGVec4f& get_light_color() const
245   { return emission; }
246
247   SGVec2f get_tex_coord_scale() const
248   {
249     float tex_width = get_xsize();
250     float tex_height = get_ysize();
251
252     return SGVec2f((0 < tex_width) ? 1000.0f/tex_width : 1.0f,
253                    (0 < tex_height) ? 1000.0f/tex_height : 1.0f);
254   }
255
256 protected:
257
258 \f
259   ////////////////////////////////////////////////////////////////////
260   // Protected methods.
261   ////////////////////////////////////////////////////////////////////
262
263   /**
264    * Initialization method, invoked by all public constructors.
265    */
266   void init();
267
268 protected:
269
270   struct _internal_state {
271       _internal_state(simgear::Effect *e, bool l,
272                       const simgear::SGReaderWriterOptions *o);
273       _internal_state(simgear::Effect *e, const std::string &t, bool l,
274                       const simgear::SGReaderWriterOptions *o);
275       void add_texture(const std::string &t, int i);
276       osg::ref_ptr<simgear::Effect> effect;
277       std::vector<std::pair<std::string,int> > texture_paths;
278       bool effect_realized;
279       osg::ref_ptr<const simgear::SGReaderWriterOptions> options;
280   };
281
282 private:
283
284 \f
285   ////////////////////////////////////////////////////////////////////
286   // Internal state.
287   ////////////////////////////////////////////////////////////////////
288
289   // texture status
290   std::vector<_internal_state> _status;
291
292   // texture size
293   double xsize, ysize;
294
295   // wrap texture?
296   bool wrapu, wrapv;
297
298   // use mipmapping?
299   bool mipmap;
300
301   // coverage of night lighting.
302   double light_coverage;
303   
304   // coverage of woods
305   double wood_coverage;
306
307   // Range at which trees become visible
308   double tree_range;
309
310   // Height of the tree
311   double tree_height;
312
313   // Width of the tree
314   double tree_width;
315
316   // Number of varieties of tree texture
317   int tree_varieties;
318
319   // True if the material is solid, false if it is a fluid
320   bool solid;
321
322   // the friction factor of that surface material
323   double friction_factor;
324
325   // the rolling friction of that surface material
326   double rolling_friction;
327
328   // the bumpiness of that surface material
329   double bumpiness;
330
331   // the load resistance of that surface material
332   double load_resistance;
333
334   // material properties
335   SGVec4f ambient, diffuse, specular, emission;
336   double shininess;
337
338   // effect for this material
339   std::string effect;
340
341   // the list of names for this material. May be empty.
342   std::vector<std::string> _names;
343
344   std::vector<SGSharedPtr<SGMatModelGroup> > object_groups;
345
346   // taxiway-/runway-sign texture elements
347   std::map<std::string, SGSharedPtr<SGMaterialGlyph> > glyphs;
348   
349   // Tree texture, typically a strip of applicable tree textures
350   std::string tree_texture;
351   
352   // Object mask, a simple RGB texture used as a mask when placing
353   // random vegetation, objects and buildings
354   std::vector<osg::Texture2D*> _masks;
355 \f
356   ////////////////////////////////////////////////////////////////////
357   // Internal constructors and methods.
358   ////////////////////////////////////////////////////////////////////
359
360   void read_properties(const simgear::SGReaderWriterOptions* options,
361                         const SGPropertyNode *props);
362   void buildEffectProperties(const simgear::SGReaderWriterOptions* options);
363   simgear::Effect* get_effect(int i);
364 };
365
366
367 class SGMaterialGlyph : public SGReferenced {
368 public:
369   SGMaterialGlyph(SGPropertyNode *);
370   inline double get_left() const { return _left; }
371   inline double get_right() const { return _right; }
372   inline double get_width() const { return _right - _left; }
373
374 protected:
375   double _left;
376   double _right;
377 };
378
379 class SGMaterialUserData : public osg::Referenced {
380 public:
381   SGMaterialUserData(const SGMaterial* material) :
382     mMaterial(material)
383   {}
384   const SGMaterial* getMaterial() const
385   { return mMaterial; }
386 private:
387   // this cannot be an SGSharedPtr since that would create a cicrular reference
388   // making it impossible to ever free the space needed by SGMaterial
389   const SGMaterial* mMaterial;
390 };
391
392 void
393 SGSetTextureFilter( int max);
394
395 int
396 SGGetTextureFilter();
397
398 #endif // _SG_MAT_HXX