1 // mat.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 - http://www.flightgear.org/~curt
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
30 # error This library requires C++
33 #include <simgear/compiler.h>
35 #include STL_STRING // Standard C++ string library
42 #include <simgear/props/props.hxx>
43 #include <simgear/structure/ssgSharedPtr.hxx>
44 #include <simgear/structure/SGSharedPtr.hxx>
46 #include "matmodel.hxx"
53 class SGMaterialGlyph;
57 * A material in the scene graph.
59 * A material represents information about a single surface type
60 * in the 3D scene graph, including texture, colour, lighting,
61 * tiling, and so on; most of the materials in FlightGear are
62 * defined in the $FG_ROOT/materials.xml file, and can be changed
65 class SGMaterial : public SGReferenced {
70 ////////////////////////////////////////////////////////////////////
71 // Public Constructors.
72 ////////////////////////////////////////////////////////////////////
75 * Construct a material from a set of properties.
77 * @param props A property node containing subnodes with the
78 * state information for the material. This node is usually
79 * loaded from the $FG_ROOT/materials.xml file.
81 SGMaterial( const string &fg_root, const SGPropertyNode *props, const char *season );
85 * Construct a material from an absolute texture path.
87 * @param texture_path A string containing an absolute path
88 * to a texture file (usually RGB).
90 SGMaterial( const string &texpath );
94 * Construct a material around an existing SSG state.
96 * This constructor allows the application to create a custom,
97 * low-level state for the scene graph and wrap a material around
98 * it. Note: the pointer ownership is transferred to the material.
100 * @param s The SSG state for this material.
102 SGMaterial( ssgSimpleState *s );
107 virtual ~SGMaterial( void );
111 ////////////////////////////////////////////////////////////////////
113 ////////////////////////////////////////////////////////////////////
116 * Force the texture to load if it hasn't already.
118 * @return true if the texture loaded, false if it was loaded
121 virtual bool load_texture (int n = -1);
125 * Get the textured state.
127 virtual ssgSimpleState *get_state (int n = -1) const;
131 * Get the number of textures assigned to this material.
133 virtual inline int get_num() const { return _status.size(); }
137 * Get the xsize of the texture, in meters.
139 virtual inline double get_xsize() const { return xsize; }
143 * Get the ysize of the texture, in meters.
145 virtual inline double get_ysize() const { return ysize; }
149 * Get the light coverage.
151 * A smaller number means more generated night lighting.
153 * @return The area (m^2?) covered by each light.
155 virtual inline double get_light_coverage () const { return light_coverage; }
159 * Get the number of randomly-placed objects defined for this material.
161 virtual int get_object_group_count () const { return object_groups.size(); }
165 * Get a randomly-placed object for this material.
167 virtual SGMatModelGroup * get_object_group (int index) const {
168 return object_groups[index];
172 * Get the horizontal scaling factor for runway/taxiway signs.
174 virtual inline double get_xscale() const { return xscale; }
177 * Return pointer to glyph class, or 0 if it doesn't exist.
179 virtual SGMaterialGlyph * get_glyph (const string& name) const {
180 map<string, SGMaterialGlyph *>::const_iterator it = glyphs.find(name);
181 return it != glyphs.end() ? it->second : 0;
187 ////////////////////////////////////////////////////////////////////
188 // Protected methods.
189 ////////////////////////////////////////////////////////////////////
192 * Initialization method, invoked by all public constructors.
198 struct _internal_state {
199 _internal_state( ssgSimpleState *s, const string &t, bool l )
200 : state(s), texture_path(t), texture_loaded(l) {}
201 ssgSharedPtr<ssgSimpleState> state;
209 ////////////////////////////////////////////////////////////////////
211 ////////////////////////////////////////////////////////////////////
214 vector<_internal_state> _status;
216 // Round-robin counter
217 unsigned int _current_ptr;
228 // coverage of night lighting.
229 double light_coverage;
231 // material properties
232 sgVec4 ambient, diffuse, specular, emission;
235 vector<SGSharedPtr<SGMatModelGroup> > object_groups;
237 // taxiway-/runway-sign elements
239 map<string, SGMaterialGlyph *> glyphs;
242 ////////////////////////////////////////////////////////////////////
243 // Internal constructors and methods.
244 ////////////////////////////////////////////////////////////////////
246 SGMaterial( const string &fg_root, const SGMaterial &mat ); // unimplemented
248 void read_properties( const string &fg_root, const SGPropertyNode *props, const char *season );
249 void build_ssg_state( bool defer_tex_load );
250 void set_ssg_state( ssgSimpleState *s );
256 class SGMaterialGlyph {
258 SGMaterialGlyph(SGPropertyNode *);
259 inline double get_left() const { return _left; }
260 inline double get_right() const { return _right; }
261 inline double get_width() const { return _right - _left; }
268 #endif // _SG_MAT_HXX