]> git.mxchange.org Git - flightgear.git/blob - src/Objects/newmat.hxx
Randomly-place object overhaul and enhancements
[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   // Inner classes.
70   //////////////////////////////////////////////////////////////////////
71
72   class ObjectGroup;
73
74   /**
75    * A randomly-placeable object.
76    */
77   class Object
78   {
79   public:
80
81     enum HeadingType {
82       HEADING_FIXED,
83       HEADING_BILLBOARD,
84       HEADING_RANDOM
85     };
86
87     int get_model_count () const;
88     ssgEntity * get_model (int index) const;
89     ssgEntity * get_random_model () const;
90     double get_coverage_m2 () const;
91     HeadingType get_heading_type () const;
92   protected:
93     friend class ObjectGroup;
94     Object (const SGPropertyNode * node, double range_m);
95     virtual ~Object ();
96   private:
97     void load_models () const;
98     vector<string> _paths;
99     mutable vector<ssgEntity *> _models;
100     mutable bool _models_loaded;
101     double _coverage_m2;
102     double _range_m;
103     HeadingType _heading_type;
104   };
105
106
107   /**
108    * A collection of related objects with the same visual range.
109    */
110   class ObjectGroup
111   {
112   public:
113     virtual ~ObjectGroup ();
114     double get_range_m () const;
115     int get_object_count () const;
116     Object * get_object (int index) const;
117   protected:
118     friend class FGNewMat;
119     ObjectGroup (SGPropertyNode * node);
120   private:
121     double _range_m;
122     vector<Object *> _objects;
123   };
124
125
126
127 \f
128   ////////////////////////////////////////////////////////////////////
129   // Public Constructors.
130   ////////////////////////////////////////////////////////////////////
131
132   /**
133    * Construct a material from a set of properties.
134    *
135    * @param props A property node containing subnodes with the
136    * state information for the material.  This node is usually
137    * loaded from the $FG_ROOT/materials.xml file.
138    */
139   FGNewMat (const SGPropertyNode * props);
140
141
142   /**
143    * Construct a material from an absolute texture path.
144    *
145    * @param texture_path A string containing an absolute path
146    * to a texture file (usually RGB).
147    */
148   FGNewMat (const string &texpath);
149
150
151   /**
152    * Construct a material around an existing SSG state.
153    *
154    * This constructor allows the application to create a custom,
155    * low-level state for the scene graph and wrap a material around
156    * it.  Note: the pointer ownership is transferred to the material.
157    *
158    * @param s The SSG state for this material.
159    */
160   FGNewMat (ssgSimpleState * s);
161
162   /**
163    * Destructor.
164    */
165   virtual ~FGNewMat ( void );
166
167
168 \f
169   ////////////////////////////////////////////////////////////////////
170   // Public methods.
171   ////////////////////////////////////////////////////////////////////
172
173   /**
174    * Force the texture to load if it hasn't already.
175    *
176    * @return true if the texture loaded, false if it was loaded
177    * already.
178    */
179   virtual bool load_texture ();
180
181
182   /**
183    * Get the textured state.
184    */
185   virtual inline ssgSimpleState *get_textured () { return textured; }
186
187
188   /**
189    * Get the xsize of the texture, in meters.
190    */
191   virtual inline double get_xsize() const { return xsize; }
192
193
194   /**
195    * Get the ysize of the texture, in meters.
196    */
197   virtual inline double get_ysize() const { return ysize; }
198
199
200   /**
201    * Get the light coverage.
202    *
203    * A smaller number means more generated night lighting.
204    *
205    * @return The area (m^2?) covered by each light.
206    */
207   virtual inline double get_light_coverage () const { return light_coverage; }
208
209
210   /**
211    * Get the number of randomly-placed objects defined for this material.
212    */
213   virtual int get_object_group_count () const { return object_groups.size(); }
214
215
216   /**
217    * Get a randomly-placed object for this material.
218    */
219   virtual ObjectGroup * get_object_group (int index) const {
220     return object_groups[index];
221   }
222
223
224   /**
225    * Get the current state.
226    */
227   virtual inline ssgStateSelector *get_state () const { return state; }
228
229
230   /**
231    * Increment the reference count for this material.
232    *
233    * A material with 0 references may be deleted by the
234    * material library.
235    */
236   virtual inline void ref () { refcount++; }
237
238
239   /**
240    * Decrement the reference count for this material.
241    */
242   virtual inline void deRef () { refcount--; }
243
244
245   /**
246    * Get the reference count for this material.
247    *
248    * @return The number of references (0 if none).
249    */
250   virtual inline int getRef () const { return refcount; }
251
252 protected:
253
254 \f
255   ////////////////////////////////////////////////////////////////////
256   // Protected methods.
257   ////////////////////////////////////////////////////////////////////
258
259   /**
260    * Initialization method, invoked by all public constructors.
261    */
262   virtual void init();
263
264
265 private:
266
267 \f
268   ////////////////////////////////////////////////////////////////////
269   // Internal state.
270   ////////////////////////////////////////////////////////////////////
271
272   // names
273   string texture_path;
274
275   // pointers to ssg states
276   ssgStateSelector *state;
277   ssgSimpleState *textured;
278   ssgSimpleState *nontextured;
279
280   // texture size
281   double xsize, ysize;
282
283   // wrap texture?
284   bool wrapu, wrapv;
285
286   // use mipmapping?
287   int mipmap;
288
289   // coverage of night lighting.
290   double light_coverage;
291
292   // material properties
293   sgVec4 ambient, diffuse, specular, emission;
294
295   // true if texture loading deferred, and not yet loaded
296   bool texture_loaded;
297
298   vector<ObjectGroup *> object_groups;
299
300   // ref count so we can properly delete if we have multiple
301   // pointers to this record
302   int refcount;
303
304
305 \f
306   ////////////////////////////////////////////////////////////////////
307   // Internal constructors and methods.
308   ////////////////////////////////////////////////////////////////////
309
310   FGNewMat (const FGNewMat &mat); // unimplemented
311
312   void read_properties (const SGPropertyNode * props);
313   void build_ssg_state(bool defer_tex_load = false);
314   void set_ssg_state( ssgSimpleState *s );
315
316
317 };
318
319 #endif // _NEWMAT_HXX