]> 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
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    * Force the texture to load if it hasn't already.
118    *
119    * @return true if the texture loaded, false if it was loaded
120    * already.
121    */
122   bool load_texture (int n = -1);
123
124
125   /**
126    * Get the textured state.
127    */
128   osg::StateSet *get_state (int n = -1) const;
129
130
131   /**
132    * Get the number of textures assigned to this material.
133    */
134   inline int get_num() const { return _status.size(); }
135
136
137   /**
138    * Get the xsize of the texture, in meters.
139    */
140   inline double get_xsize() const { return xsize; }
141
142
143   /**
144    * Get the ysize of the texture, in meters.
145    */
146   inline double get_ysize() const { return ysize; }
147
148
149   /**
150    * Get the light coverage.
151    *
152    * A smaller number means more generated night lighting.
153    *
154    * @return The area (m^2?) covered by each light.
155    */
156   inline double get_light_coverage () const { return light_coverage; }
157
158   /**
159    * Return if the surface material is solid, if it is not solid, a fluid
160    * can be assumed, that is usually water.
161    */
162   bool get_solid () const { return solid; }
163
164   /**
165    * Get the friction factor for that material
166    */
167   double get_friction_factor () const { return friction_factor; }
168
169   /**
170    * Get the rolling friction for that material
171    */
172   double get_rolling_friction () const { return rolling_friction; }
173
174   /**
175    * Get the bumpines for that material
176    */
177   double get_bumpiness () const { return bumpiness; }
178
179   /**
180    * Get the load resistance
181    */
182   double get_load_resistance () const { return load_resistance; }
183
184   /**
185    * Get the list of names for this material
186    */
187   const vector<string>& get_names() const { return _names; }
188
189   /**
190    * add the given name to the list of names this material is known
191    */
192   void add_name(const string& name) { _names.push_back(name); }
193
194   /**
195    * Get the number of randomly-placed objects defined for this material.
196    */
197   int get_object_group_count () const { return object_groups.size(); }
198
199   /**
200    * Get a randomly-placed object for this material.
201    */
202   SGMatModelGroup * get_object_group (int index) const {
203     return object_groups[index];
204   }
205
206   /**
207    * Return pointer to glyph class, or 0 if it doesn't exist.
208    */
209   SGMaterialGlyph * get_glyph (const string& name) const;
210
211   void set_light_color(const SGVec4f& color)
212   { emission = color; }
213   const SGVec4f& get_light_color() const
214   { return emission; }
215
216   SGVec2f get_tex_coord_scale() const
217   {
218     float tex_width = get_xsize();
219     float tex_height = get_ysize();
220
221     return SGVec2f((0 < tex_width) ? 1000.0f/tex_width : 1.0f,
222                    (0 < tex_height) ? 1000.0f/tex_height : 1.0f);
223   }
224
225 protected:
226
227 \f
228   ////////////////////////////////////////////////////////////////////
229   // Protected methods.
230   ////////////////////////////////////////////////////////////////////
231
232   /**
233    * Initialization method, invoked by all public constructors.
234    */
235   void init();
236
237 protected:
238
239   struct _internal_state {
240       _internal_state( osg::StateSet *s, const string &t, bool l )
241                   : state(s), texture_path(t), texture_loaded(l) {}
242       osg::ref_ptr<osg::StateSet> state;
243       string texture_path;
244       bool texture_loaded;
245   };
246
247 private:
248
249 \f
250   ////////////////////////////////////////////////////////////////////
251   // Internal state.
252   ////////////////////////////////////////////////////////////////////
253
254   // texture status
255   vector<_internal_state> _status;
256
257   // Round-robin counter
258   mutable unsigned int _current_ptr;
259
260   // texture size
261   double xsize, ysize;
262
263   // wrap texture?
264   bool wrapu, wrapv;
265
266   // use mipmapping?
267   int mipmap;
268
269   // use anisotropic filtering
270   float filtering;
271
272   // coverage of night lighting.
273   double light_coverage;
274
275   // True if the material is solid, false if it is a fluid
276   bool solid;
277
278   // the friction factor of that surface material
279   double friction_factor;
280
281   // the rolling friction of that surface material
282   double rolling_friction;
283
284   // the bumpiness of that surface material
285   double bumpiness;
286
287   // the load resistance of that surface material
288   double load_resistance;
289
290   // material properties
291   SGVec4f ambient, diffuse, specular, emission;
292   double shininess;
293
294   // the list of names for this material. May be empty.
295   vector<string> _names;
296
297   vector<SGSharedPtr<SGMatModelGroup> > object_groups;
298
299   // taxiway-/runway-sign texture elements
300   map<string, SGSharedPtr<SGMaterialGlyph> > glyphs;
301
302 \f
303   ////////////////////////////////////////////////////////////////////
304   // Internal constructors and methods.
305   ////////////////////////////////////////////////////////////////////
306
307   SGMaterial( const string &fg_root, const SGMaterial &mat ); // unimplemented
308
309   void read_properties( const string &fg_root, const SGPropertyNode *props, const char *season );
310   void build_state( bool defer_tex_load );
311   void set_state( osg::StateSet *s );
312
313   void assignTexture( osg::StateSet *state, const std::string &fname, int _wrapu = TRUE, int _wrapv = TRUE, int _mipmap = TRUE, float filtering = 1.0f );
314
315 };
316
317
318 class SGMaterialGlyph : public SGReferenced {
319 public:
320   SGMaterialGlyph(SGPropertyNode *);
321   inline double get_left() const { return _left; }
322   inline double get_right() const { return _right; }
323   inline double get_width() const { return _right - _left; }
324
325 protected:
326   double _left;
327   double _right;
328 };
329
330 class SGMaterialUserData : public osg::Referenced {
331 public:
332   SGMaterialUserData(const SGMaterial* material) :
333     mMaterial(material)
334   {}
335   const SGMaterial* getMaterial() const
336   { return mMaterial; }
337 private:
338   SGSharedPtr<const SGMaterial> mMaterial;
339 };
340
341 #endif // _SG_MAT_HXX