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