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++
44 #include <simgear/compiler.h>
45 #include <simgear/misc/props.hxx>
49 #include STL_STRING // Standard C++ string library
55 * A material in the scene graph.
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
68 //////////////////////////////////////////////////////////////////////
70 //////////////////////////////////////////////////////////////////////
75 * A randomly-placeable object.
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;
93 friend class ObjectGroup;
94 Object (const SGPropertyNode * node, double range_m);
97 void load_models () const;
98 vector<string> _paths;
99 mutable vector<ssgEntity *> _models;
100 mutable bool _models_loaded;
103 HeadingType _heading_type;
108 * A collection of related objects with the same visual range.
113 virtual ~ObjectGroup ();
114 double get_range_m () const;
115 int get_object_count () const;
116 Object * get_object (int index) const;
118 friend class FGNewMat;
119 ObjectGroup (SGPropertyNode * node);
122 vector<Object *> _objects;
128 ////////////////////////////////////////////////////////////////////
129 // Public Constructors.
130 ////////////////////////////////////////////////////////////////////
133 * Construct a material from a set of properties.
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.
139 FGNewMat (const SGPropertyNode * props);
143 * Construct a material from an absolute texture path.
145 * @param texture_path A string containing an absolute path
146 * to a texture file (usually RGB).
148 FGNewMat (const string &texpath);
152 * Construct a material around an existing SSG state.
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.
158 * @param s The SSG state for this material.
160 FGNewMat (ssgSimpleState * s);
165 virtual ~FGNewMat ( void );
169 ////////////////////////////////////////////////////////////////////
171 ////////////////////////////////////////////////////////////////////
174 * Force the texture to load if it hasn't already.
176 * @return true if the texture loaded, false if it was loaded
179 virtual bool load_texture ();
183 * Get the textured state.
185 virtual inline ssgSimpleState *get_textured () { return textured; }
189 * Get the xsize of the texture, in meters.
191 virtual inline double get_xsize() const { return xsize; }
195 * Get the ysize of the texture, in meters.
197 virtual inline double get_ysize() const { return ysize; }
201 * Get the light coverage.
203 * A smaller number means more generated night lighting.
205 * @return The area (m^2?) covered by each light.
207 virtual inline double get_light_coverage () const { return light_coverage; }
211 * Get the number of randomly-placed objects defined for this material.
213 virtual int get_object_group_count () const { return object_groups.size(); }
217 * Get a randomly-placed object for this material.
219 virtual ObjectGroup * get_object_group (int index) const {
220 return object_groups[index];
225 * Get the current state.
227 virtual inline ssgStateSelector *get_state () const { return state; }
231 * Increment the reference count for this material.
233 * A material with 0 references may be deleted by the
236 virtual inline void ref () { refcount++; }
240 * Decrement the reference count for this material.
242 virtual inline void deRef () { refcount--; }
246 * Get the reference count for this material.
248 * @return The number of references (0 if none).
250 virtual inline int getRef () const { return refcount; }
255 ////////////////////////////////////////////////////////////////////
256 // Protected methods.
257 ////////////////////////////////////////////////////////////////////
260 * Initialization method, invoked by all public constructors.
268 ////////////////////////////////////////////////////////////////////
270 ////////////////////////////////////////////////////////////////////
275 // pointers to ssg states
276 ssgStateSelector *state;
277 ssgSimpleState *textured;
278 ssgSimpleState *nontextured;
289 // coverage of night lighting.
290 double light_coverage;
292 // material properties
293 sgVec4 ambient, diffuse, specular, emission;
295 // true if texture loading deferred, and not yet loaded
298 vector<ObjectGroup *> object_groups;
300 // ref count so we can properly delete if we have multiple
301 // pointers to this record
306 ////////////////////////////////////////////////////////////////////
307 // Internal constructors and methods.
308 ////////////////////////////////////////////////////////////////////
310 FGNewMat (const FGNewMat &mat); // unimplemented
312 void read_properties (const SGPropertyNode * props);
313 void build_ssg_state(bool defer_tex_load = false);
314 void set_ssg_state( ssgSimpleState *s );
319 #endif // _NEWMAT_HXX