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 * Return pointer to glyph class, or 0 if it doesn't exist.
174 virtual SGMaterialGlyph * get_glyph (const string& name) const {
175 map<string, SGMaterialGlyph *>::const_iterator it = glyphs.find(name);
176 return it != glyphs.end() ? it->second : 0;
182 ////////////////////////////////////////////////////////////////////
183 // Protected methods.
184 ////////////////////////////////////////////////////////////////////
187 * Initialization method, invoked by all public constructors.
193 struct _internal_state {
194 _internal_state( ssgSimpleState *s, const string &t, bool l )
195 : state(s), texture_path(t), texture_loaded(l) {}
196 ssgSharedPtr<ssgSimpleState> state;
204 ////////////////////////////////////////////////////////////////////
206 ////////////////////////////////////////////////////////////////////
209 vector<_internal_state> _status;
211 // Round-robin counter
212 unsigned int _current_ptr;
223 // coverage of night lighting.
224 double light_coverage;
226 // material properties
227 sgVec4 ambient, diffuse, specular, emission;
230 vector<SGSharedPtr<SGMatModelGroup> > object_groups;
232 // taxiway-/runway-sign texture elements
233 map<string, SGMaterialGlyph *> glyphs;
236 ////////////////////////////////////////////////////////////////////
237 // Internal constructors and methods.
238 ////////////////////////////////////////////////////////////////////
240 SGMaterial( const string &fg_root, const SGMaterial &mat ); // unimplemented
242 void read_properties( const string &fg_root, const SGPropertyNode *props, const char *season );
243 void build_ssg_state( bool defer_tex_load );
244 void set_ssg_state( ssgSimpleState *s );
250 class SGMaterialGlyph {
252 SGMaterialGlyph(SGPropertyNode *);
253 inline double get_left() const { return _left; }
254 inline double get_right() const { return _right; }
255 inline double get_width() const { return _right - _left; }
262 #endif // _SG_MAT_HXX