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