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