// Written by Curtis Olson, started May 1998.
// Overhauled by David Megginson, December 2001
//
-// Copyright (C) 1998 - 2000 Curtis L. Olson - curt@flightgear.org
+// Copyright (C) 1998 - 2000 Curtis L. Olson - http://www.flightgear.org/~curt
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
-// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//
// $Id$
#ifndef _SG_MAT_HXX
#define _SG_MAT_HXX
-#ifndef __cplusplus
+#ifndef __cplusplus
# error This library requires C++
-#endif
+#endif
#include <simgear/compiler.h>
#include STL_STRING // Standard C++ string library
+#include <vector>
+#include <map>
-#include <plib/sg.h>
-#include <plib/ssg.h>
+#include <simgear/math/SGMath.hxx>
+
+#include <osg/ref_ptr>
+#include <osg/StateSet>
#include <simgear/props/props.hxx>
-#include <simgear/scene/model/loader.hxx>
+#include <simgear/structure/SGSharedPtr.hxx>
+#include <simgear/scene/util/SGSceneFeatures.hxx>
+
+#include "matmodel.hxx"
SG_USING_STD(string);
+SG_USING_STD(vector);
+SG_USING_STD(map);
+
+class SGMaterialGlyph;
/**
* A material in the scene graph.
* defined in the $FG_ROOT/materials.xml file, and can be changed
* at runtime.
*/
-class SGMaterial {
+class SGMaterial : public SGReferenced {
public:
-\f
- //////////////////////////////////////////////////////////////////////
- // Inner classes.
- //////////////////////////////////////////////////////////////////////
-
- class ObjectGroup;
-
- /**
- * A randomly-placeable object.
- *
- * SGMaterial uses this class to keep track of the model(s) and
- * parameters for a single instance of a randomly-placeable object.
- * The object can have more than one variant model (i.e. slightly
- * different shapes of trees), but they are considered equivalent
- * and interchangeable.
- */
- class Object
- {
- public:
-
- /**
- * The heading type for a randomly-placed object.
- */
- enum HeadingType {
- HEADING_FIXED,
- HEADING_BILLBOARD,
- HEADING_RANDOM
- };
-
-
- /**
- * Get the number of variant models available for the object.
- *
- * @return The number of variant models.
- */
- int get_model_count( SGModelLoader *loader,
- const string &fg_root,
- SGPropertyNode *prop_root,
- double sim_time_sec );
-
-
- /**
- * Get a specific variant model for the object.
- *
- * @param index The index of the model.
- * @return The model.
- */
- ssgEntity *get_model( int index,
- SGModelLoader *loader,
- const string &fg_root,
- SGPropertyNode *prop_root,
- double sim_time_sec );
-
-
- /**
- * Get a randomly-selected variant model for the object.
- *
- * @return A randomly select model from the variants.
- */
- ssgEntity *get_random_model( SGModelLoader *loader,
- const string &fg_root,
- SGPropertyNode *prop_root,
- double sim_time_sec );
-
-
- /**
- * Get the average number of meters^2 occupied by each instance.
- *
- * @return The coverage in meters^2.
- */
- double get_coverage_m2 () const;
-
-
- /**
- * Get the heading type for the object.
- *
- * @return The heading type.
- */
- HeadingType get_heading_type () const;
-
- protected:
-
- friend class ObjectGroup;
-
- Object (const SGPropertyNode * node, double range_m);
-
- virtual ~Object ();
-
- private:
-
- /**
- * Actually load the models.
- *
- * This class uses lazy loading so that models won't be held
- * in memory for materials that are never referenced.
- */
- void load_models( SGModelLoader *loader,
- const string &fg_root,
- SGPropertyNode *prop_root,
- double sim_time_sec );
-
- vector<string> _paths;
- mutable vector<ssgEntity *> _models;
- mutable bool _models_loaded;
- double _coverage_m2;
- double _range_m;
- HeadingType _heading_type;
- };
-
-
- /**
- * A collection of related objects with the same visual range.
- *
- * Grouping objects with the same range together significantly
- * reduces the memory requirements of randomly-placed objects.
- * Each SGMaterial instance keeps a (possibly-empty) list of
- * object groups for placing randomly on the scenery.
- */
- class ObjectGroup
- {
- public:
- virtual ~ObjectGroup ();
-
-
- /**
- * Get the visual range of the object in meters.
- *
- * @return The visual range.
- */
- double get_range_m () const;
-
-
- /**
- * Get the number of objects in the group.
- *
- * @return The number of objects.
- */
- int get_object_count () const;
-
-
- /**
- * Get a specific object.
- *
- * @param index The object's index, zero-based.
- * @return The object selected.
- */
- Object * get_object (int index) const;
-
- protected:
-
- friend class SGMaterial;
-
- ObjectGroup (SGPropertyNode * node);
-
- private:
-
- double _range_m;
- vector<Object *> _objects;
-
- };
-
-
-
\f
////////////////////////////////////////////////////////////////////
// Public Constructors.
* state information for the material. This node is usually
* loaded from the $FG_ROOT/materials.xml file.
*/
- SGMaterial( const string &fg_root, const SGPropertyNode *props );
+ SGMaterial( const string &fg_root, const SGPropertyNode *props, const char *season );
/**
/**
- * Construct a material around an existing SSG state.
+ * Construct a material around an existing state.
*
* This constructor allows the application to create a custom,
* low-level state for the scene graph and wrap a material around
* it. Note: the pointer ownership is transferred to the material.
*
- * @param s The SSG state for this material.
+ * @param s The state for this material.
*/
- SGMaterial( ssgSimpleState *s );
+ SGMaterial( osg::StateSet *s );
/**
* Destructor.
*/
- virtual ~SGMaterial( void );
+ ~SGMaterial( void );
\f
////////////////////////////////////////////////////////////////////
/**
- * Force the texture to load if it hasn't already.
- *
- * @return true if the texture loaded, false if it was loaded
- * already.
+ * Get the textured state.
*/
- virtual bool load_texture ();
+ osg::StateSet *get_state (int n = -1);
/**
- * Get the textured state.
+ * Get the number of textures assigned to this material.
*/
- virtual inline ssgSimpleState *get_state () const { return state; }
+ inline int get_num() const { return _status.size(); }
/**
* Get the xsize of the texture, in meters.
*/
- virtual inline double get_xsize() const { return xsize; }
+ inline double get_xsize() const { return xsize; }
/**
* Get the ysize of the texture, in meters.
*/
- virtual inline double get_ysize() const { return ysize; }
+ inline double get_ysize() const { return ysize; }
/**
*
* @return The area (m^2?) covered by each light.
*/
- virtual inline double get_light_coverage () const { return light_coverage; }
+ inline double get_light_coverage () const { return light_coverage; }
+ /**
+ * Return if the surface material is solid, if it is not solid, a fluid
+ * can be assumed, that is usually water.
+ */
+ bool get_solid () const { return solid; }
/**
- * Get the number of randomly-placed objects defined for this material.
+ * Get the friction factor for that material
*/
- virtual int get_object_group_count () const { return object_groups.size(); }
+ double get_friction_factor () const { return friction_factor; }
+ /**
+ * Get the rolling friction for that material
+ */
+ double get_rolling_friction () const { return rolling_friction; }
/**
- * Get a randomly-placed object for this material.
+ * Get the bumpines for that material
*/
- virtual ObjectGroup * get_object_group (int index) const {
- return object_groups[index];
- }
+ double get_bumpiness () const { return bumpiness; }
+ /**
+ * Get the load resistance
+ */
+ double get_load_resistance () const { return load_resistance; }
/**
- * Increment the reference count for this material.
- *
- * A material with 0 references may be deleted by the
- * material library.
+ * Get the list of names for this material
*/
- virtual inline void ref () { refcount++; }
+ const vector<string>& get_names() const { return _names; }
+ /**
+ * add the given name to the list of names this material is known
+ */
+ void add_name(const string& name) { _names.push_back(name); }
/**
- * Decrement the reference count for this material.
+ * Get the number of randomly-placed objects defined for this material.
*/
- virtual inline void deRef () { refcount--; }
+ int get_object_group_count () const { return object_groups.size(); }
+ /**
+ * Get a randomly-placed object for this material.
+ */
+ SGMatModelGroup * get_object_group (int index) const {
+ return object_groups[index];
+ }
/**
- * Get the reference count for this material.
- *
- * @return The number of references (0 if none).
+ * Return pointer to glyph class, or 0 if it doesn't exist.
*/
- virtual inline int getRef () const { return refcount; }
+ SGMaterialGlyph * get_glyph (const string& name) const;
+
+ void set_light_color(const SGVec4f& color)
+ { emission = color; }
+ const SGVec4f& get_light_color() const
+ { return emission; }
+
+ SGVec2f get_tex_coord_scale() const
+ {
+ float tex_width = get_xsize();
+ float tex_height = get_ysize();
+
+ return SGVec2f((0 < tex_width) ? 1000.0f/tex_width : 1.0f,
+ (0 < tex_height) ? 1000.0f/tex_height : 1.0f);
+ }
protected:
/**
* Initialization method, invoked by all public constructors.
*/
- virtual void init();
+ void init();
+protected:
+
+ struct _internal_state {
+ _internal_state( osg::StateSet *s, const string &t, bool l )
+ : state(s), texture_path(t), texture_loaded(l) {}
+ osg::ref_ptr<osg::StateSet> state;
+ string texture_path;
+ bool texture_loaded;
+ };
private:
// Internal state.
////////////////////////////////////////////////////////////////////
- // names
- string texture_path;
+ // texture status
+ vector<_internal_state> _status;
- // pointers to ssg states
- ssgSimpleState *state;
+ // Round-robin counter
+ mutable unsigned int _current_ptr;
// texture size
double xsize, ysize;
// coverage of night lighting.
double light_coverage;
+ // True if the material is solid, false if it is a fluid
+ bool solid;
+
+ // the friction factor of that surface material
+ double friction_factor;
+
+ // the rolling friction of that surface material
+ double rolling_friction;
+
+ // the bumpiness of that surface material
+ double bumpiness;
+
+ // the load resistance of that surface material
+ double load_resistance;
+
// material properties
- sgVec4 ambient, diffuse, specular, emission;
+ SGVec4f ambient, diffuse, specular, emission;
double shininess;
- // true if texture loading deferred, and not yet loaded
- bool texture_loaded;
-
- vector<ObjectGroup *> object_groups;
+ // the list of names for this material. May be empty.
+ vector<string> _names;
- // ref count so we can properly delete if we have multiple
- // pointers to this record
- int refcount;
+ vector<SGSharedPtr<SGMatModelGroup> > object_groups;
+ // taxiway-/runway-sign texture elements
+ map<string, SGSharedPtr<SGMaterialGlyph> > glyphs;
\f
////////////////////////////////////////////////////////////////////
SGMaterial( const string &fg_root, const SGMaterial &mat ); // unimplemented
- void read_properties( const string &fg_root, const SGPropertyNode *props );
- void build_ssg_state( bool defer_tex_load );
- void set_ssg_state( ssgSimpleState *s );
+ void read_properties( const string &fg_root, const SGPropertyNode *props, const char *season );
+ void build_state( bool defer_tex_load );
+ void set_state( osg::StateSet *s );
+
+ void assignTexture( osg::StateSet *state, const std::string &fname, int _wrapu = TRUE, int _wrapv = TRUE, int _mipmap = TRUE );
+
+};
+
+class SGMaterialGlyph : public SGReferenced {
+public:
+ SGMaterialGlyph(SGPropertyNode *);
+ inline double get_left() const { return _left; }
+ inline double get_right() const { return _right; }
+ inline double get_width() const { return _right - _left; }
+
+protected:
+ double _left;
+ double _right;
+};
+class SGMaterialUserData : public osg::Referenced {
+public:
+ SGMaterialUserData(const SGMaterial* material) :
+ mMaterial(material)
+ {}
+ const SGMaterial* getMaterial() const
+ { return mMaterial; }
+private:
+ // this cannot be an SGSharedPtr since that would create a cicrular reference
+ // making it impossible to ever free the space needed by SGMaterial
+ const SGMaterial* mMaterial;
};
+void
+SGSetTextureFilter( int max);
+
+int
+SGGetTextureFilter();
+
#endif // _SG_MAT_HXX