]> git.mxchange.org Git - simgear.git/blob - simgear/scene/material/mat.hxx
8631fdc1dc612923b2f8e2495f33e60e672d58db
[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    * Return pointer to glyph class, or 0 if it doesn't exist.
173    */
174   virtual SGMaterialGlyph * get_glyph (const string& name) const {
175     map<string, SGMaterialGlyph *>::const_iterator it = glyphs.find(name);
176     return it != glyphs.end() ? it->second : 0;
177   }
178
179 protected:
180
181 \f
182   ////////////////////////////////////////////////////////////////////
183   // Protected methods.
184   ////////////////////////////////////////////////////////////////////
185
186   /**
187    * Initialization method, invoked by all public constructors.
188    */
189   virtual void init();
190
191 protected:
192
193   struct _internal_state {
194      _internal_state( ssgSimpleState *s, const string &t, bool l )
195                   : state(s), texture_path(t), texture_loaded(l) {}
196       ssgSharedPtr<ssgSimpleState> state;
197       string texture_path;
198       bool texture_loaded;
199   };
200
201 private:
202
203 \f
204   ////////////////////////////////////////////////////////////////////
205   // Internal state.
206   ////////////////////////////////////////////////////////////////////
207
208   // texture status
209   vector<_internal_state> _status;
210
211   // Round-robin counter
212   unsigned int _current_ptr;
213
214   // texture size
215   double xsize, ysize;
216
217   // wrap texture?
218   bool wrapu, wrapv;
219
220   // use mipmapping?
221   int mipmap;
222
223   // coverage of night lighting.
224   double light_coverage;
225
226   // material properties
227   sgVec4 ambient, diffuse, specular, emission;
228   double shininess;
229
230   vector<SGSharedPtr<SGMatModelGroup> > object_groups;
231
232   // taxiway-/runway-sign texture elements
233   map<string, SGMaterialGlyph *> glyphs;
234
235 \f
236   ////////////////////////////////////////////////////////////////////
237   // Internal constructors and methods.
238   ////////////////////////////////////////////////////////////////////
239
240   SGMaterial( const string &fg_root, const SGMaterial &mat ); // unimplemented
241
242   void read_properties( const string &fg_root, const SGPropertyNode *props, const char *season );
243   void build_ssg_state( bool defer_tex_load );
244   void set_ssg_state( ssgSimpleState *s );
245
246
247 };
248
249
250 class SGMaterialGlyph {
251 public:
252   SGMaterialGlyph(SGPropertyNode *);
253   inline double get_left() const { return _left; }
254   inline double get_right() const { return _right; }
255   inline double get_width() const { return _right - _left; }
256
257 protected:
258   double _left;
259   double _right;
260 };
261
262 #endif // _SG_MAT_HXX