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