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