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