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++
40 #include <simgear/compiler.h>
41 #include <simgear/misc/props.hxx>
43 #include STL_STRING // Standard C++ string library
49 * A material in the scene graph.
51 * A material represents information about a single surface type
52 * in the 3D scene graph, including texture, colour, lighting,
53 * tiling, and so on; most of the materials in FlightGear are
54 * defined in the $FG_ROOT/materials.xml file, and can be changed
62 //////////////////////////////////////////////////////////////////////
64 //////////////////////////////////////////////////////////////////////
69 * A randomly-placeable object.
71 * FGNewMat uses this class to keep track of the model(s) and
72 * parameters for a single instance of a randomly-placeable object.
73 * The object can have more than one variant model (i.e. slightly
74 * different shapes of trees), but they are considered equivalent
75 * and interchangeable.
82 * The heading type for a randomly-placed object.
92 * Get the number of variant models available for the object.
94 * @return The number of variant models.
96 int get_model_count () const;
100 * Get a specific variant model for the object.
102 * @param index The index of the model.
105 ssgEntity * get_model (int index) const;
109 * Get a randomly-selected variant model for the object.
111 * @return A randomly select model from the variants.
113 ssgEntity * get_random_model () const;
117 * Get the average number of meters^2 occupied by each instance.
119 * @return The coverage in meters^2.
121 double get_coverage_m2 () const;
125 * Get the heading type for the object.
127 * @return The heading type.
129 HeadingType get_heading_type () const;
133 friend class ObjectGroup;
135 Object (const SGPropertyNode * node, double range_m);
142 * Actually load the models.
144 * This class uses lazy loading so that models won't be held
145 * in memory for materials that are never referenced.
147 void load_models () const;
149 vector<string> _paths;
150 mutable vector<ssgEntity *> _models;
151 mutable bool _models_loaded;
154 HeadingType _heading_type;
159 * A collection of related objects with the same visual range.
161 * Grouping objects with the same range together significantly
162 * reduces the memory requirements of randomly-placed objects.
163 * Each FGNewMat instance keeps a (possibly-empty) list of
164 * object groups for placing randomly on the scenery.
169 virtual ~ObjectGroup ();
173 * Get the visual range of the object in meters.
175 * @return The visual range.
177 double get_range_m () const;
181 * Get the number of objects in the group.
183 * @return The number of objects.
185 int get_object_count () const;
189 * Get a specific object.
191 * @param index The object's index, zero-based.
192 * @return The object selected.
194 Object * get_object (int index) const;
198 friend class FGNewMat;
200 ObjectGroup (SGPropertyNode * node);
205 vector<Object *> _objects;
212 ////////////////////////////////////////////////////////////////////
213 // Public Constructors.
214 ////////////////////////////////////////////////////////////////////
217 * Construct a material from a set of properties.
219 * @param props A property node containing subnodes with the
220 * state information for the material. This node is usually
221 * loaded from the $FG_ROOT/materials.xml file.
223 FGNewMat (const SGPropertyNode * props);
227 * Construct a material from an absolute texture path.
229 * @param texture_path A string containing an absolute path
230 * to a texture file (usually RGB).
232 FGNewMat (const string &texpath);
236 * Construct a material around an existing SSG state.
238 * This constructor allows the application to create a custom,
239 * low-level state for the scene graph and wrap a material around
240 * it. Note: the pointer ownership is transferred to the material.
242 * @param s The SSG state for this material.
244 FGNewMat (ssgSimpleState * s);
249 virtual ~FGNewMat ( void );
253 ////////////////////////////////////////////////////////////////////
255 ////////////////////////////////////////////////////////////////////
258 * Force the texture to load if it hasn't already.
260 * @return true if the texture loaded, false if it was loaded
263 virtual bool load_texture ();
267 * Get the textured state.
269 virtual inline ssgSimpleState *get_textured () { return textured; }
273 * Get the xsize of the texture, in meters.
275 virtual inline double get_xsize() const { return xsize; }
279 * Get the ysize of the texture, in meters.
281 virtual inline double get_ysize() const { return ysize; }
285 * Get the light coverage.
287 * A smaller number means more generated night lighting.
289 * @return The area (m^2?) covered by each light.
291 virtual inline double get_light_coverage () const { return light_coverage; }
295 * Get the number of randomly-placed objects defined for this material.
297 virtual int get_object_group_count () const { return object_groups.size(); }
301 * Get a randomly-placed object for this material.
303 virtual ObjectGroup * get_object_group (int index) const {
304 return object_groups[index];
309 * Get the current state.
311 virtual inline ssgStateSelector *get_state () const { return state; }
315 * Increment the reference count for this material.
317 * A material with 0 references may be deleted by the
320 virtual inline void ref () { refcount++; }
324 * Decrement the reference count for this material.
326 virtual inline void deRef () { refcount--; }
330 * Get the reference count for this material.
332 * @return The number of references (0 if none).
334 virtual inline int getRef () const { return refcount; }
339 ////////////////////////////////////////////////////////////////////
340 // Protected methods.
341 ////////////////////////////////////////////////////////////////////
344 * Initialization method, invoked by all public constructors.
352 ////////////////////////////////////////////////////////////////////
354 ////////////////////////////////////////////////////////////////////
359 // pointers to ssg states
360 ssgStateSelector *state;
361 ssgSimpleState *textured;
362 ssgSimpleState *nontextured;
373 // coverage of night lighting.
374 double light_coverage;
376 // material properties
377 sgVec4 ambient, diffuse, specular, emission;
380 // true if texture loading deferred, and not yet loaded
383 vector<ObjectGroup *> object_groups;
385 // ref count so we can properly delete if we have multiple
386 // pointers to this record
391 ////////////////////////////////////////////////////////////////////
392 // Internal constructors and methods.
393 ////////////////////////////////////////////////////////////////////
395 FGNewMat (const FGNewMat &mat); // unimplemented
397 void read_properties (const SGPropertyNode * props);
398 void build_ssg_state(bool defer_tex_load = false);
399 void set_ssg_state( ssgSimpleState *s );
404 #endif // _NEWMAT_HXX