1 // newmat.hxx -- a material in the scene graph.
2 // TODO: this class needs to be renamed.
4 // Written by Curtis Olson, started May 1998.
5 // Overhauled by David Megginson, December 2001
7 // Copyright (C) 1998 - 2000 Curtis L. Olson - curt@flightgear.org
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.
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.
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.
30 # error This library requires C++
33 #include <simgear/compiler.h>
35 #include STL_STRING // Standard C++ string library
40 #include <simgear/props/props.hxx>
46 * A material in the scene graph.
48 * A material represents information about a single surface type
49 * in the 3D scene graph, including texture, colour, lighting,
50 * tiling, and so on; most of the materials in FlightGear are
51 * defined in the $FG_ROOT/materials.xml file, and can be changed
59 //////////////////////////////////////////////////////////////////////
61 //////////////////////////////////////////////////////////////////////
66 * A randomly-placeable object.
68 * FGNewMat uses this class to keep track of the model(s) and
69 * parameters for a single instance of a randomly-placeable object.
70 * The object can have more than one variant model (i.e. slightly
71 * different shapes of trees), but they are considered equivalent
72 * and interchangeable.
79 * The heading type for a randomly-placed object.
89 * Get the number of variant models available for the object.
91 * @return The number of variant models.
93 int get_model_count () const;
97 * Get a specific variant model for the object.
99 * @param index The index of the model.
102 ssgEntity * get_model (int index) const;
106 * Get a randomly-selected variant model for the object.
108 * @return A randomly select model from the variants.
110 ssgEntity * get_random_model () const;
114 * Get the average number of meters^2 occupied by each instance.
116 * @return The coverage in meters^2.
118 double get_coverage_m2 () const;
122 * Get the heading type for the object.
124 * @return The heading type.
126 HeadingType get_heading_type () const;
130 friend class ObjectGroup;
132 Object (const SGPropertyNode * node, double range_m);
139 * Actually load the models.
141 * This class uses lazy loading so that models won't be held
142 * in memory for materials that are never referenced.
144 void load_models () const;
146 vector<string> _paths;
147 mutable vector<ssgEntity *> _models;
148 mutable bool _models_loaded;
151 HeadingType _heading_type;
156 * A collection of related objects with the same visual range.
158 * Grouping objects with the same range together significantly
159 * reduces the memory requirements of randomly-placed objects.
160 * Each FGNewMat instance keeps a (possibly-empty) list of
161 * object groups for placing randomly on the scenery.
166 virtual ~ObjectGroup ();
170 * Get the visual range of the object in meters.
172 * @return The visual range.
174 double get_range_m () const;
178 * Get the number of objects in the group.
180 * @return The number of objects.
182 int get_object_count () const;
186 * Get a specific object.
188 * @param index The object's index, zero-based.
189 * @return The object selected.
191 Object * get_object (int index) const;
195 friend class FGNewMat;
197 ObjectGroup (SGPropertyNode * node);
202 vector<Object *> _objects;
209 ////////////////////////////////////////////////////////////////////
210 // Public Constructors.
211 ////////////////////////////////////////////////////////////////////
214 * Construct a material from a set of properties.
216 * @param props A property node containing subnodes with the
217 * state information for the material. This node is usually
218 * loaded from the $FG_ROOT/materials.xml file.
220 FGNewMat (const SGPropertyNode * props);
224 * Construct a material from an absolute texture path.
226 * @param texture_path A string containing an absolute path
227 * to a texture file (usually RGB).
229 FGNewMat (const string &texpath);
233 * Construct a material around an existing SSG state.
235 * This constructor allows the application to create a custom,
236 * low-level state for the scene graph and wrap a material around
237 * it. Note: the pointer ownership is transferred to the material.
239 * @param s The SSG state for this material.
241 FGNewMat (ssgSimpleState * s);
246 virtual ~FGNewMat ( void );
250 ////////////////////////////////////////////////////////////////////
252 ////////////////////////////////////////////////////////////////////
255 * Force the texture to load if it hasn't already.
257 * @return true if the texture loaded, false if it was loaded
260 virtual bool load_texture ();
264 * Get the textured state.
266 virtual inline ssgSimpleState *get_textured () { return textured; }
270 * Get the xsize of the texture, in meters.
272 virtual inline double get_xsize() const { return xsize; }
276 * Get the ysize of the texture, in meters.
278 virtual inline double get_ysize() const { return ysize; }
282 * Get the light coverage.
284 * A smaller number means more generated night lighting.
286 * @return The area (m^2?) covered by each light.
288 virtual inline double get_light_coverage () const { return light_coverage; }
292 * Get the number of randomly-placed objects defined for this material.
294 virtual int get_object_group_count () const { return object_groups.size(); }
298 * Get a randomly-placed object for this material.
300 virtual ObjectGroup * get_object_group (int index) const {
301 return object_groups[index];
306 * Get the current state.
308 virtual inline ssgStateSelector *get_state () const { return state; }
312 * Increment the reference count for this material.
314 * A material with 0 references may be deleted by the
317 virtual inline void ref () { refcount++; }
321 * Decrement the reference count for this material.
323 virtual inline void deRef () { refcount--; }
327 * Get the reference count for this material.
329 * @return The number of references (0 if none).
331 virtual inline int getRef () const { return refcount; }
336 ////////////////////////////////////////////////////////////////////
337 // Protected methods.
338 ////////////////////////////////////////////////////////////////////
341 * Initialization method, invoked by all public constructors.
349 ////////////////////////////////////////////////////////////////////
351 ////////////////////////////////////////////////////////////////////
356 // pointers to ssg states
357 ssgStateSelector *state;
358 ssgSimpleState *textured;
359 ssgSimpleState *nontextured;
370 // coverage of night lighting.
371 double light_coverage;
373 // material properties
374 sgVec4 ambient, diffuse, specular, emission;
377 // true if texture loading deferred, and not yet loaded
380 vector<ObjectGroup *> object_groups;
382 // ref count so we can properly delete if we have multiple
383 // pointers to this record
388 ////////////////////////////////////////////////////////////////////
389 // Internal constructors and methods.
390 ////////////////////////////////////////////////////////////////////
392 FGNewMat (const FGNewMat &mat); // unimplemented
394 void read_properties (const SGPropertyNode * props);
395 void build_ssg_state(bool defer_tex_load = false);
396 void set_ssg_state( ssgSimpleState *s );
401 #endif // _NEWMAT_HXX