]> git.mxchange.org Git - flightgear.git/blob - src/Objects/newmat.hxx
Connect to the electrical system.
[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    * FGNewMat uses this class to keep track of the model(s) and
78    * parameters for a single instance of a randomly-placeable object.
79    * The object can have more than one variant model (i.e. slightly
80    * different shapes of trees), but they are considered equivalent
81    * and interchangeable.
82    */
83   class Object
84   {
85   public:
86
87     /**
88      * The heading type for a randomly-placed object.
89      */
90     enum HeadingType {
91       HEADING_FIXED,
92       HEADING_BILLBOARD,
93       HEADING_RANDOM
94     };
95
96
97     /**
98      * Get the number of variant models available for the object.
99      *
100      * @return The number of variant models.
101      */
102     int get_model_count () const;
103
104
105     /**
106      * Get a specific variant model for the object.
107      *
108      * @param index The index of the model.
109      * @return The model.
110      */
111     ssgEntity * get_model (int index) const;
112
113
114     /**
115      * Get a randomly-selected variant model for the object.
116      *
117      * @return A randomly select model from the variants.
118      */
119     ssgEntity * get_random_model () const;
120
121
122     /**
123      * Get the average number of meters^2 occupied by each instance.
124      *
125      * @return The coverage in meters^2.
126      */
127     double get_coverage_m2 () const;
128
129
130     /**
131      * Get the heading type for the object.
132      *
133      * @return The heading type.
134      */
135     HeadingType get_heading_type () const;
136
137   protected:
138
139     friend class ObjectGroup;
140
141     Object (const SGPropertyNode * node, double range_m);
142
143     virtual ~Object ();
144
145   private:
146
147     /**
148      * Actually load the models.
149      *
150      * This class uses lazy loading so that models won't be held
151      * in memory for materials that are never referenced.
152      */
153     void load_models () const;
154
155     vector<string> _paths;
156     mutable vector<ssgEntity *> _models;
157     mutable bool _models_loaded;
158     double _coverage_m2;
159     double _range_m;
160     HeadingType _heading_type;
161   };
162
163
164   /**
165    * A collection of related objects with the same visual range.
166    *
167    * Grouping objects with the same range together significantly
168    * reduces the memory requirements of randomly-placed objects.
169    * Each FGNewMat instance keeps a (possibly-empty) list of
170    * object groups for placing randomly on the scenery.
171    */
172   class ObjectGroup
173   {
174   public:
175     virtual ~ObjectGroup ();
176
177
178     /**
179      * Get the visual range of the object in meters.
180      *
181      * @return The visual range.
182      */
183     double get_range_m () const;
184
185
186     /**
187      * Get the number of objects in the group.
188      *
189      * @return The number of objects.
190      */
191     int get_object_count () const;
192
193
194     /**
195      * Get a specific object.
196      *
197      * @param index The object's index, zero-based.
198      * @return The object selected.
199      */
200     Object * get_object (int index) const;
201
202   protected:
203
204     friend class FGNewMat;
205
206     ObjectGroup (SGPropertyNode * node);
207
208   private:
209
210     double _range_m;
211     vector<Object *> _objects;
212
213   };
214
215
216
217 \f
218   ////////////////////////////////////////////////////////////////////
219   // Public Constructors.
220   ////////////////////////////////////////////////////////////////////
221
222   /**
223    * Construct a material from a set of properties.
224    *
225    * @param props A property node containing subnodes with the
226    * state information for the material.  This node is usually
227    * loaded from the $FG_ROOT/materials.xml file.
228    */
229   FGNewMat (const SGPropertyNode * props);
230
231
232   /**
233    * Construct a material from an absolute texture path.
234    *
235    * @param texture_path A string containing an absolute path
236    * to a texture file (usually RGB).
237    */
238   FGNewMat (const string &texpath);
239
240
241   /**
242    * Construct a material around an existing SSG state.
243    *
244    * This constructor allows the application to create a custom,
245    * low-level state for the scene graph and wrap a material around
246    * it.  Note: the pointer ownership is transferred to the material.
247    *
248    * @param s The SSG state for this material.
249    */
250   FGNewMat (ssgSimpleState * s);
251
252   /**
253    * Destructor.
254    */
255   virtual ~FGNewMat ( void );
256
257
258 \f
259   ////////////////////////////////////////////////////////////////////
260   // Public methods.
261   ////////////////////////////////////////////////////////////////////
262
263   /**
264    * Force the texture to load if it hasn't already.
265    *
266    * @return true if the texture loaded, false if it was loaded
267    * already.
268    */
269   virtual bool load_texture ();
270
271
272   /**
273    * Get the textured state.
274    */
275   virtual inline ssgSimpleState *get_textured () { return textured; }
276
277
278   /**
279    * Get the xsize of the texture, in meters.
280    */
281   virtual inline double get_xsize() const { return xsize; }
282
283
284   /**
285    * Get the ysize of the texture, in meters.
286    */
287   virtual inline double get_ysize() const { return ysize; }
288
289
290   /**
291    * Get the light coverage.
292    *
293    * A smaller number means more generated night lighting.
294    *
295    * @return The area (m^2?) covered by each light.
296    */
297   virtual inline double get_light_coverage () const { return light_coverage; }
298
299
300   /**
301    * Get the number of randomly-placed objects defined for this material.
302    */
303   virtual int get_object_group_count () const { return object_groups.size(); }
304
305
306   /**
307    * Get a randomly-placed object for this material.
308    */
309   virtual ObjectGroup * get_object_group (int index) const {
310     return object_groups[index];
311   }
312
313
314   /**
315    * Get the current state.
316    */
317   virtual inline ssgStateSelector *get_state () const { return state; }
318
319
320   /**
321    * Increment the reference count for this material.
322    *
323    * A material with 0 references may be deleted by the
324    * material library.
325    */
326   virtual inline void ref () { refcount++; }
327
328
329   /**
330    * Decrement the reference count for this material.
331    */
332   virtual inline void deRef () { refcount--; }
333
334
335   /**
336    * Get the reference count for this material.
337    *
338    * @return The number of references (0 if none).
339    */
340   virtual inline int getRef () const { return refcount; }
341
342 protected:
343
344 \f
345   ////////////////////////////////////////////////////////////////////
346   // Protected methods.
347   ////////////////////////////////////////////////////////////////////
348
349   /**
350    * Initialization method, invoked by all public constructors.
351    */
352   virtual void init();
353
354
355 private:
356
357 \f
358   ////////////////////////////////////////////////////////////////////
359   // Internal state.
360   ////////////////////////////////////////////////////////////////////
361
362   // names
363   string texture_path;
364
365   // pointers to ssg states
366   ssgStateSelector *state;
367   ssgSimpleState *textured;
368   ssgSimpleState *nontextured;
369
370   // texture size
371   double xsize, ysize;
372
373   // wrap texture?
374   bool wrapu, wrapv;
375
376   // use mipmapping?
377   int mipmap;
378
379   // coverage of night lighting.
380   double light_coverage;
381
382   // material properties
383   sgVec4 ambient, diffuse, specular, emission;
384   double shininess;
385
386   // true if texture loading deferred, and not yet loaded
387   bool texture_loaded;
388
389   vector<ObjectGroup *> object_groups;
390
391   // ref count so we can properly delete if we have multiple
392   // pointers to this record
393   int refcount;
394
395
396 \f
397   ////////////////////////////////////////////////////////////////////
398   // Internal constructors and methods.
399   ////////////////////////////////////////////////////////////////////
400
401   FGNewMat (const FGNewMat &mat); // unimplemented
402
403   void read_properties (const SGPropertyNode * props);
404   void build_ssg_state(bool defer_tex_load = false);
405   void set_ssg_state( ssgSimpleState *s );
406
407
408 };
409
410 #endif // _NEWMAT_HXX