]> git.mxchange.org Git - flightgear.git/blob - src/Objects/newmat.hxx
Added support for dynamically-generated scenery objects. Set the
[flightgear.git] / src / Objects / newmat.hxx
1 // newmat.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 _NEWMAT_HXX
27 #define _NEWMAT_HXX
28
29 #ifndef __cplusplus                                                          
30 # error This library requires C++
31 #endif                                   
32
33 #ifdef HAVE_CONFIG_H
34 #  include <config.h>
35 #endif
36
37 #ifdef HAVE_WINDOWS_H
38 #  include <windows.h>
39 #endif
40
41 #include <plib/sg.h>
42 #include <plib/ssg.h>
43
44 #include <simgear/compiler.h>
45 #include <simgear/misc/props.hxx>
46
47 #include <GL/glut.h>
48
49 #include STL_STRING      // Standard C++ string library
50
51 SG_USING_STD(string);
52
53
54 /**
55  * A material in the scene graph.
56  *
57  * A material represents information about a single surface type
58  * in the 3D scene graph, including texture, colour, lighting,
59  * tiling, and so on; most of the materials in FlightGear are
60  * defined in the $FG_ROOT/materials.xml file, and can be changed
61  * at runtime.
62  */
63 class FGNewMat {
64
65 public:
66
67 \f
68   ////////////////////////////////////////////////////////////////////
69   // Public Constructors.
70   ////////////////////////////////////////////////////////////////////
71
72   /**
73    * Construct a material from a set of properties.
74    *
75    * @param props A property node containing subnodes with the
76    * state information for the material.  This node is usually
77    * loaded from the $FG_ROOT/materials.xml file.
78    */
79   FGNewMat (const SGPropertyNode * props);
80
81
82   /**
83    * Construct a material from an absolute texture path.
84    *
85    * @param texture_path A string containing an absolute path
86    * to a texture file (usually RGB).
87    */
88   FGNewMat (const string &texpath);
89
90
91   /**
92    * Construct a material around an existing SSG state.
93    *
94    * This constructor allows the application to create a custom,
95    * low-level state for the scene graph and wrap a material around
96    * it.  Note: the pointer ownership is transferred to the material.
97    *
98    * @param s The SSG state for this material.
99    */
100   FGNewMat (ssgSimpleState * s);
101
102   /**
103    * Destructor.
104    */
105   virtual ~FGNewMat ( void );
106
107
108 \f
109   ////////////////////////////////////////////////////////////////////
110   // Public methods.
111   ////////////////////////////////////////////////////////////////////
112
113   /**
114    * Force the texture to load if it hasn't already.
115    *
116    * @return true if the texture loaded, false if it was loaded
117    * already.
118    */
119   virtual bool load_texture ();
120
121
122   /**
123    * Get the textured state.
124    */
125   virtual inline ssgSimpleState *get_textured () { return textured; }
126
127
128   /**
129    * Get the xsize of the texture, in meters.
130    */
131   virtual inline double get_xsize() const { return xsize; }
132
133
134   /**
135    * Get the ysize of the texture, in meters.
136    */
137   virtual inline double get_ysize() const { return ysize; }
138
139
140   /**
141    * Get the light coverage.
142    *
143    * A smaller number means more generated night lighting.
144    *
145    * @return The area (m^2?) covered by each light.
146    */
147   virtual inline double get_light_coverage () const { return light_coverage; }
148
149
150   /**
151    * Get the number of dynamic objects defined for this material.
152    */
153   virtual int get_object_count () const { return objects.size(); }
154
155
156   /**
157    * Get a dynamic object for this material.
158    */
159   virtual ssgEntity * get_object (int i) const { return objects[i].model; }
160
161
162   /**
163    * Get the average space for a dynamic object for this material.
164    */
165   virtual double get_object_coverage (int i) const {
166     return objects[i].coverage;
167   }
168
169
170   /**
171    * Get the group LOD range for a dynamic object for this material.
172    */
173   virtual double get_object_group_lod (int i) const {
174     return objects[i].group_lod;
175   }
176
177
178   /**
179    * Get the current state.
180    */
181   virtual inline ssgStateSelector *get_state () const { return state; }
182
183
184   /**
185    * Increment the reference count for this material.
186    *
187    * A material with 0 references may be deleted by the
188    * material library.
189    */
190   virtual inline void ref () { refcount++; }
191
192
193   /**
194    * Decrement the reference count for this material.
195    */
196   virtual inline void deRef () { refcount--; }
197
198
199   /**
200    * Get the reference count for this material.
201    *
202    * @return The number of references (0 if none).
203    */
204   virtual inline int getRef () const { return refcount; }
205
206 protected:
207
208 \f
209   ////////////////////////////////////////////////////////////////////
210   // Protected methods.
211   ////////////////////////////////////////////////////////////////////
212
213   /**
214    * Initialization method, invoked by all public constructors.
215    */
216   virtual void init();
217
218
219 private:
220
221 \f
222   ////////////////////////////////////////////////////////////////////
223   // Internal state.
224   ////////////////////////////////////////////////////////////////////
225
226   // names
227   string texture_path;
228
229   // pointers to ssg states
230   ssgStateSelector *state;
231   ssgSimpleState *textured;
232   ssgSimpleState *nontextured;
233
234   // texture size
235   double xsize, ysize;
236
237   // wrap texture?
238   bool wrapu, wrapv;
239
240   // use mipmapping?
241   int mipmap;
242
243   // coverage of night lighting.
244   double light_coverage;
245
246   // material properties
247   sgVec4 ambient, diffuse, specular, emission;
248
249   // true if texture loading deferred, and not yet loaded
250   bool texture_loaded;
251
252   struct Object
253   {
254     ssgEntity * model;
255     double coverage;
256     double group_lod;
257   };
258
259   vector<Object> objects;
260
261   // ref count so we can properly delete if we have multiple
262   // pointers to this record
263   int refcount;
264
265
266 \f
267   ////////////////////////////////////////////////////////////////////
268   // Internal constructors and methods.
269   ////////////////////////////////////////////////////////////////////
270
271   FGNewMat (const FGNewMat &mat); // unimplemented
272
273   void read_properties (const SGPropertyNode * props);
274   void build_ssg_state(bool defer_tex_load = false);
275   void set_ssg_state( ssgSimpleState *s );
276
277 };
278
279 #endif // _NEWMAT_HXX