]> git.mxchange.org Git - simgear.git/blob - simgear/scene/material/mat.hxx
Modified Files:
[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
47 #include "matmodel.hxx"
48
49 SG_USING_STD(string);
50 SG_USING_STD(vector);
51 SG_USING_STD(map);
52
53
54 class SGMaterialGlyph;
55
56 class SGTextureFilterListener  {
57 private:
58   static int filter;
59
60   SGTextureFilterListener() {
61   }
62   
63 public:
64   static int getFilter();
65   static void setFilter(int filt);
66  };
67
68 /**
69  * A material in the scene graph.
70  *
71  * A material represents information about a single surface type
72  * in the 3D scene graph, including texture, colour, lighting,
73  * tiling, and so on; most of the materials in FlightGear are
74  * defined in the $FG_ROOT/materials.xml file, and can be changed
75  * at runtime.
76  */
77 class SGMaterial : public SGReferenced {
78
79 public:
80
81 \f
82   ////////////////////////////////////////////////////////////////////
83   // Public Constructors.
84   ////////////////////////////////////////////////////////////////////
85
86   /**
87    * Construct a material from a set of properties.
88    *
89    * @param props A property node containing subnodes with the
90    * state information for the material.  This node is usually
91    * loaded from the $FG_ROOT/materials.xml file.
92    */
93   SGMaterial( const string &fg_root, const SGPropertyNode *props, const char *season );
94
95
96   /**
97    * Construct a material from an absolute texture path.
98    *
99    * @param texture_path A string containing an absolute path
100    * to a texture file (usually RGB).
101    */
102   SGMaterial( const string &texpath );
103
104
105   /**
106    * Construct a material around an existing state.
107    *
108    * This constructor allows the application to create a custom,
109    * low-level state for the scene graph and wrap a material around
110    * it.  Note: the pointer ownership is transferred to the material.
111    *
112    * @param s The state for this material.
113    */
114   SGMaterial( osg::StateSet *s );
115
116   /**
117    * Destructor.
118    */
119   ~SGMaterial( void );
120
121
122 \f
123   ////////////////////////////////////////////////////////////////////
124   // Public methods.
125   ////////////////////////////////////////////////////////////////////
126
127   /**
128    * Force the texture to load if it hasn't already.
129    *
130    * @return true if the texture loaded, false if it was loaded
131    * already.
132    */
133   bool load_texture (int n = -1);
134
135   /**
136    * Get the textured state.
137    */
138   osg::StateSet *get_state (int n = -1) const;
139
140
141   /**
142    * Get the number of textures assigned to this material.
143    */
144   inline int get_num() const { return _status.size(); }
145
146
147   /**
148    * Get the xsize of the texture, in meters.
149    */
150   inline double get_xsize() const { return xsize; }
151
152
153   /**
154    * Get the ysize of the texture, in meters.
155    */
156   inline double get_ysize() const { return ysize; }
157
158
159   /**
160    * Get the light coverage.
161    *
162    * A smaller number means more generated night lighting.
163    *
164    * @return The area (m^2?) covered by each light.
165    */
166   inline double get_light_coverage () const { return light_coverage; }
167
168   /**
169    * Return if the surface material is solid, if it is not solid, a fluid
170    * can be assumed, that is usually water.
171    */
172   bool get_solid () const { return solid; }
173
174   /**
175    * Get the friction factor for that material
176    */
177   double get_friction_factor () const { return friction_factor; }
178
179   /**
180    * Get the rolling friction for that material
181    */
182   double get_rolling_friction () const { return rolling_friction; }
183
184   /**
185    * Get the bumpines for that material
186    */
187   double get_bumpiness () const { return bumpiness; }
188
189   /**
190    * Get the load resistance
191    */
192   double get_load_resistance () const { return load_resistance; }
193
194   /**
195    * Get the list of names for this material
196    */
197   const vector<string>& get_names() const { return _names; }
198
199   /**
200    * add the given name to the list of names this material is known
201    */
202   void add_name(const string& name) { _names.push_back(name); }
203
204   /**
205    * Get the number of randomly-placed objects defined for this material.
206    */
207   int get_object_group_count () const { return object_groups.size(); }
208
209   /**
210    * Get a randomly-placed object for this material.
211    */
212   SGMatModelGroup * get_object_group (int index) const {
213     return object_groups[index];
214   }
215
216   /**
217    * Return pointer to glyph class, or 0 if it doesn't exist.
218    */
219   SGMaterialGlyph * get_glyph (const string& name) const;
220
221   void set_light_color(const SGVec4f& color)
222   { emission = color; }
223   const SGVec4f& get_light_color() const
224   { return emission; }
225
226   SGVec2f get_tex_coord_scale() const
227   {
228     float tex_width = get_xsize();
229     float tex_height = get_ysize();
230
231     return SGVec2f((0 < tex_width) ? 1000.0f/tex_width : 1.0f,
232                    (0 < tex_height) ? 1000.0f/tex_height : 1.0f);
233   }
234
235 protected:
236
237 \f
238   ////////////////////////////////////////////////////////////////////
239   // Protected methods.
240   ////////////////////////////////////////////////////////////////////
241
242   /**
243    * Initialization method, invoked by all public constructors.
244    */
245   void init();
246
247 protected:
248
249   struct _internal_state {
250       _internal_state( osg::StateSet *s, const string &t, bool l )
251                   : state(s), texture_path(t), texture_loaded(l) {}
252       osg::ref_ptr<osg::StateSet> state;
253       string texture_path;
254       bool texture_loaded;
255   };
256
257 private:
258
259 \f
260   ////////////////////////////////////////////////////////////////////
261   // Internal state.
262   ////////////////////////////////////////////////////////////////////
263
264   // texture status
265   vector<_internal_state> _status;
266
267   // Round-robin counter
268   mutable unsigned int _current_ptr;
269
270   // texture size
271   double xsize, ysize;
272
273   // wrap texture?
274   bool wrapu, wrapv;
275
276   // use mipmapping?
277   int mipmap;
278
279   // coverage of night lighting.
280   double light_coverage;
281
282   // True if the material is solid, false if it is a fluid
283   bool solid;
284
285   // the friction factor of that surface material
286   double friction_factor;
287
288   // the rolling friction of that surface material
289   double rolling_friction;
290
291   // the bumpiness of that surface material
292   double bumpiness;
293
294   // the load resistance of that surface material
295   double load_resistance;
296
297   // material properties
298   SGVec4f ambient, diffuse, specular, emission;
299   double shininess;
300
301   // the list of names for this material. May be empty.
302   vector<string> _names;
303
304   vector<SGSharedPtr<SGMatModelGroup> > object_groups;
305
306   // taxiway-/runway-sign texture elements
307   map<string, SGSharedPtr<SGMaterialGlyph> > glyphs;
308
309 \f
310   ////////////////////////////////////////////////////////////////////
311   // Internal constructors and methods.
312   ////////////////////////////////////////////////////////////////////
313
314   SGMaterial( const string &fg_root, const SGMaterial &mat ); // unimplemented
315
316   void read_properties( const string &fg_root, const SGPropertyNode *props, const char *season );
317   void build_state( bool defer_tex_load );
318   void set_state( osg::StateSet *s );
319
320   void assignTexture( osg::StateSet *state, const std::string &fname, int _wrapu = TRUE, int _wrapv = TRUE, int _mipmap = TRUE );
321
322 };
323
324
325 class SGMaterialGlyph : public SGReferenced {
326 public:
327   SGMaterialGlyph(SGPropertyNode *);
328   inline double get_left() const { return _left; }
329   inline double get_right() const { return _right; }
330   inline double get_width() const { return _right - _left; }
331
332 protected:
333   double _left;
334   double _right;
335 };
336
337 class SGMaterialUserData : public osg::Referenced {
338 public:
339   SGMaterialUserData(const SGMaterial* material) :
340     mMaterial(material)
341   {}
342   const SGMaterial* getMaterial() const
343   { return mMaterial; }
344 private:
345   SGSharedPtr<const SGMaterial> mMaterial;
346 };
347
348 #endif // _SG_MAT_HXX