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>
41 #include <simgear/scene/model/loader.hxx>
47 * A material in the scene graph.
49 * A material represents information about a single surface type
50 * in the 3D scene graph, including texture, colour, lighting,
51 * tiling, and so on; most of the materials in FlightGear are
52 * defined in the $FG_ROOT/materials.xml file, and can be changed
60 //////////////////////////////////////////////////////////////////////
62 //////////////////////////////////////////////////////////////////////
67 * A randomly-placeable object.
69 * FGNewMat uses this class to keep track of the model(s) and
70 * parameters for a single instance of a randomly-placeable object.
71 * The object can have more than one variant model (i.e. slightly
72 * different shapes of trees), but they are considered equivalent
73 * and interchangeable.
80 * The heading type for a randomly-placed object.
90 * Get the number of variant models available for the object.
92 * @return The number of variant models.
94 int get_model_count( FGModelLoader *loader,
95 const string &fg_root,
96 SGPropertyNode *prop_root,
97 double sim_time_sec );
101 * Get a specific variant model for the object.
103 * @param index The index of the model.
106 ssgEntity *get_model( int index,
107 FGModelLoader *loader,
108 const string &fg_root,
109 SGPropertyNode *prop_root,
110 double sim_time_sec );
114 * Get a randomly-selected variant model for the object.
116 * @return A randomly select model from the variants.
118 ssgEntity *get_random_model( FGModelLoader *loader,
119 const string &fg_root,
120 SGPropertyNode *prop_root,
121 double sim_time_sec );
125 * Get the average number of meters^2 occupied by each instance.
127 * @return The coverage in meters^2.
129 double get_coverage_m2 () const;
133 * Get the heading type for the object.
135 * @return The heading type.
137 HeadingType get_heading_type () const;
141 friend class ObjectGroup;
143 Object (const SGPropertyNode * node, double range_m);
150 * Actually load the models.
152 * This class uses lazy loading so that models won't be held
153 * in memory for materials that are never referenced.
155 void load_models( FGModelLoader *loader,
156 const string &fg_root,
157 SGPropertyNode *prop_root,
158 double sim_time_sec );
160 vector<string> _paths;
161 mutable vector<ssgEntity *> _models;
162 mutable bool _models_loaded;
165 HeadingType _heading_type;
170 * A collection of related objects with the same visual range.
172 * Grouping objects with the same range together significantly
173 * reduces the memory requirements of randomly-placed objects.
174 * Each FGNewMat instance keeps a (possibly-empty) list of
175 * object groups for placing randomly on the scenery.
180 virtual ~ObjectGroup ();
184 * Get the visual range of the object in meters.
186 * @return The visual range.
188 double get_range_m () const;
192 * Get the number of objects in the group.
194 * @return The number of objects.
196 int get_object_count () const;
200 * Get a specific object.
202 * @param index The object's index, zero-based.
203 * @return The object selected.
205 Object * get_object (int index) const;
209 friend class FGNewMat;
211 ObjectGroup (SGPropertyNode * node);
216 vector<Object *> _objects;
223 ////////////////////////////////////////////////////////////////////
224 // Public Constructors.
225 ////////////////////////////////////////////////////////////////////
228 * Construct a material from a set of properties.
230 * @param props A property node containing subnodes with the
231 * state information for the material. This node is usually
232 * loaded from the $FG_ROOT/materials.xml file.
234 FGNewMat( const string &fg_root, const SGPropertyNode *props,
235 bool smooth_shading, bool use_textures );
239 * Construct a material from an absolute texture path.
241 * @param texture_path A string containing an absolute path
242 * to a texture file (usually RGB).
244 FGNewMat( const string &texpath, bool smooth_shading, bool use_textures );
248 * Construct a material around an existing SSG state.
250 * This constructor allows the application to create a custom,
251 * low-level state for the scene graph and wrap a material around
252 * it. Note: the pointer ownership is transferred to the material.
254 * @param s The SSG state for this material.
256 FGNewMat( ssgSimpleState *s, bool smooth_shading, bool use_textures );
261 virtual ~FGNewMat( void );
265 ////////////////////////////////////////////////////////////////////
267 ////////////////////////////////////////////////////////////////////
270 * Force the texture to load if it hasn't already.
272 * @return true if the texture loaded, false if it was loaded
275 virtual bool load_texture ();
279 * Get the textured state.
281 virtual inline ssgSimpleState *get_textured () { return textured; }
285 * Get the xsize of the texture, in meters.
287 virtual inline double get_xsize() const { return xsize; }
291 * Get the ysize of the texture, in meters.
293 virtual inline double get_ysize() const { return ysize; }
297 * Get the light coverage.
299 * A smaller number means more generated night lighting.
301 * @return The area (m^2?) covered by each light.
303 virtual inline double get_light_coverage () const { return light_coverage; }
307 * Get the number of randomly-placed objects defined for this material.
309 virtual int get_object_group_count () const { return object_groups.size(); }
313 * Get a randomly-placed object for this material.
315 virtual ObjectGroup * get_object_group (int index) const {
316 return object_groups[index];
321 * Get the current state.
323 virtual inline ssgStateSelector *get_state () const { return state; }
327 * Increment the reference count for this material.
329 * A material with 0 references may be deleted by the
332 virtual inline void ref () { refcount++; }
336 * Decrement the reference count for this material.
338 virtual inline void deRef () { refcount--; }
342 * Get the reference count for this material.
344 * @return The number of references (0 if none).
346 virtual inline int getRef () const { return refcount; }
351 ////////////////////////////////////////////////////////////////////
352 // Protected methods.
353 ////////////////////////////////////////////////////////////////////
356 * Initialization method, invoked by all public constructors.
364 ////////////////////////////////////////////////////////////////////
366 ////////////////////////////////////////////////////////////////////
371 // pointers to ssg states
372 ssgStateSelector *state;
373 ssgSimpleState *textured;
374 ssgSimpleState *nontextured;
385 // coverage of night lighting.
386 double light_coverage;
388 // material properties
389 sgVec4 ambient, diffuse, specular, emission;
392 // true if texture loading deferred, and not yet loaded
395 vector<ObjectGroup *> object_groups;
397 // ref count so we can properly delete if we have multiple
398 // pointers to this record
403 ////////////////////////////////////////////////////////////////////
404 // Internal constructors and methods.
405 ////////////////////////////////////////////////////////////////////
407 FGNewMat( const string &fg_root, const FGNewMat &mat ); // unimplemented
409 void read_properties( const string &fg_root, const SGPropertyNode *props );
410 void build_ssg_state( bool defer_tex_load,
413 void set_ssg_state( ssgSimpleState *s,
414 bool smooth_shading, bool use_textures );
419 #endif // _NEWMAT_HXX