]> git.mxchange.org Git - simgear.git/blobdiff - simgear/scene/material/mat.hxx
Race-condition fix in get_effect.
[simgear.git] / simgear / scene / material / mat.hxx
index 3ede53fd07fa8324e3886db43551ef927d7efe8c..7ed5958c962b17f7609ac572306cc45e96fe364c 100644 (file)
 #ifndef _SG_MAT_HXX
 #define _SG_MAT_HXX
 
-#ifndef __cplusplus
-# error This library requires C++
-#endif
-
 #include <simgear/compiler.h>
 
-#include STL_STRING      // Standard C++ string library
+#include <string>      // Standard C++ string library
 #include <vector>
 #include <map>
 
-#include <simgear/math/SGMath.hxx>
+#include "Effect.hxx"
 
 #include <osg/ref_ptr>
-#include <osg/StateSet>
-
-#include <simgear/props/props.hxx>
-#include <simgear/structure/SGSharedPtr.hxx>
-
-#include "matmodel.hxx"
-
-SG_USING_STD(string);
-SG_USING_STD(vector);
-SG_USING_STD(map);
+#include <osg/Texture2D>
 
+namespace osg
+{
+class StateSet;
+}
 
+#include <simgear/structure/SGSharedPtr.hxx>
+#include <simgear/threads/SGThread.hxx> // for SGMutex
+#include <simgear/math/SGMath.hxx>
+#include <simgear/bvh/BVHMaterial.hxx>
+
+namespace simgear
+{
+class Effect;
+void reload_shaders();
+class SGReaderWriterOptions;
+}
+
+class SGMatModelGroup;
+class SGCondition;
+class SGPropertyNode;
 class SGMaterialGlyph;
-
+class SGTexturedTriangleBin;
 
 /**
  * A material in the scene graph.
@@ -63,7 +69,7 @@ class SGMaterialGlyph;
  * defined in the $FG_ROOT/materials.xml file, and can be changed
  * at runtime.
  */
-class SGMaterial : public SGReferenced {
+class SGMaterial : public simgear::BVHMaterial {
 
 public:
 
@@ -79,29 +85,13 @@ public:
    * 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, const char *season );
-
-
-  /**
-   * Construct a material from an absolute texture path.
-   *
-   * @param texture_path A string containing an absolute path
-   * to a texture file (usually RGB).
-   */
-  SGMaterial( const string &texpath );
-
-
-  /**
-   * 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 state for this material.
-   */
-  SGMaterial( osg::StateSet *s );
+  SGMaterial( const osgDB::Options*, 
+              const SGPropertyNode *props, 
+              SGPropertyNode *prop_root);
 
+  SGMaterial(const simgear::SGReaderWriterOptions*,
+             const SGPropertyNode *props,
+             SGPropertyNode *prop_root);
   /**
    * Destructor.
    */
@@ -114,18 +104,15 @@ public:
   ////////////////////////////////////////////////////////////////////
 
   /**
-   * 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.
    */
-  bool load_texture (int n = -1);
-
+  simgear::Effect* get_effect(const SGTexturedTriangleBin& triangleBin);
+  simgear::Effect* get_effect();
 
   /**
    * Get the textured state.
    */
-  osg::StateSet *get_state (int n = -1) const;
+  osg::Texture2D* get_object_mask(const SGTexturedTriangleBin& triangleBin);
 
 
   /**
@@ -151,45 +138,156 @@ public:
    *
    * A smaller number means more generated night lighting.
    *
-   * @return The area (m^2?) covered by each light.
+   * @return The area (m^2) covered by each light.
    */
   inline double get_light_coverage () const { return light_coverage; }
+  
+  /**
+   * Get the building coverage.
+   *
+   * A smaller number means more generated buildings.
+   *
+   * @return The area (m^2) covered by each light.
+   */
+  inline double get_building_coverage () const { return building_coverage; }
 
   /**
-   * Return if the surface material is solid, if it is not solid, a fluid
-   * can be assumed, that is usually water.
+   * Get the building spacing.
+   *
+   * This is the minimum spacing between buildings
+   *
+   * @return The minimum distance between buildings
    */
-  bool get_solid () const { return solid; }
+  inline double get_building_spacing () const { return building_spacing; }
 
   /**
-   * Get the friction factor for that material
+   * Get the building texture.
+   *
+   * This is the texture used for auto-generated buildings.
+   *
+   * @return The texture for auto-generated buildings.
    */
-  double get_friction_factor () const { return friction_factor; }
+  inline std::string get_building_texture () const { return building_texture; }
 
   /**
-   * Get the rolling friction for that material
+   * Get the building lightmap.
+   *
+   * This is the lightmap used for auto-generated buildings.
+   *
+   * @return The lightmap for auto-generated buildings.
    */
-  double get_rolling_friction () const { return rolling_friction; }
+  inline std::string get_building_lightmap () const { return building_lightmap; }
+  
+  // Ratio of the 3 random building sizes
+  inline double get_building_small_fraction () const { return building_small_ratio / (building_small_ratio + building_medium_ratio + building_large_ratio); }
+  inline double get_building_medium_fraction () const { return building_medium_ratio / (building_small_ratio + building_medium_ratio + building_large_ratio); }
+  inline double get_building_large_fraction () const { return building_large_ratio / (building_small_ratio + building_medium_ratio + building_large_ratio); }
+  
+  // Proportion of buildings with pitched roofs
+  inline double get_building_small_pitch () const { return building_small_pitch; }
+  inline double get_building_medium_pitch () const { return building_medium_pitch; }
+  inline double get_building_large_pitch () const { return building_large_pitch; }
+
+  // Min/Max number of floors for each size
+  inline int get_building_small_min_floors () const { return  building_small_min_floors; }
+  inline int get_building_small_max_floors () const { return  building_small_max_floors; }
+  inline int get_building_medium_min_floors () const { return building_medium_min_floors; }
+  inline int get_building_medium_max_floors () const { return building_medium_max_floors; }
+  inline int get_building_large_min_floors () const { return building_large_min_floors; }
+  inline int get_building_large_max_floors () const { return building_large_max_floors; }
+  
+  // Minimum width and depth for each size
+  inline double get_building_small_min_width () const { return building_small_min_width; }
+  inline double get_building_small_max_width () const { return building_small_max_width; }
+  inline double get_building_small_min_depth () const { return building_small_min_depth; }
+  inline double get_building_small_max_depth () const { return building_small_max_depth; }
+  
+  inline double get_building_medium_min_width () const { return building_medium_min_width; }
+  inline double get_building_medium_max_width () const { return building_medium_max_width; }
+  inline double get_building_medium_min_depth () const { return building_medium_min_depth; }
+  inline double get_building_medium_max_depth () const { return building_medium_max_depth; }
+  
+  inline double get_building_large_min_width () const { return building_large_min_width; }
+  inline double get_building_large_max_width () const { return building_large_max_width; }
+  inline double get_building_large_min_depth () const { return building_large_min_depth; }
+  inline double get_building_large_max_depth () const { return building_large_max_depth; }
+  
+  inline double get_building_range () const { return building_range; }
+  
+  inline double get_cos_object_max_density_slope_angle () const { return cos_object_max_density_slope_angle; }
+  inline double get_cos_object_zero_density_slope_angle () const { return cos_object_zero_density_slope_angle; }
 
   /**
-   * Get the bumpines for that material
+   * Get the wood coverage.
+   *
+   * A smaller number means more generated woods within the forest.
+   *
+   * @return The area (m^2) covered by each wood.
    */
-  double get_bumpiness () const { return bumpiness; }
+  inline double get_wood_coverage () const { return wood_coverage; }
+  
+  /**
+   * Get the tree height.
+   *
+   * @return The average height of the trees.
+   */
+  inline double get_tree_height () const { return tree_height; }
 
   /**
-   * Get the load resistence
+   * Get the tree width.
+   *
+   * @return The average width of the trees.
    */
-  double get_load_resistence () const { return load_resistence; }
+  inline double get_tree_width () const { return tree_width; }
 
+  /**
+   * Get the forest LoD range.
+   *
+   * @return The LoD range for the trees.
+   */
+  inline double get_tree_range () const { return tree_range; }
+  
+  /**
+   * Get the number of tree varieties available
+   *
+   * @return the number of different trees defined in the texture strip
+   */
+  inline int get_tree_varieties () const { return tree_varieties; }
+  
+  /**
+   * Get the texture strip to use for trees
+   *
+   * @return the texture to use for trees.
+   */
+  inline std::string get_tree_texture () const { return  tree_texture; }
+  
+  /**
+   * Get the cosine of the maximum tree density slope angle. We
+   * use the cosine as it can be compared directly to the z component
+   * of a triangle normal.
+   * 
+   * @return the cosine of the maximum tree density slope angle.
+   */
+  inline double get_cos_tree_max_density_slope_angle () const { return cos_tree_max_density_slope_angle; }
+  
+  /**
+   * Get the cosine of the maximum tree density slope angle. We
+   * use the cosine as it can be compared directly to the z component
+   * of a triangle normal.
+   * 
+   * @return the cosine of the maximum tree density slope angle.
+   */
+  inline double get_cos_tree_zero_density_slope_angle () const { return cos_tree_zero_density_slope_angle; }
+  
   /**
    * Get the list of names for this material
    */
-  const vector<string>& get_names() const { return _names; }
+  const std::vector<std::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); }
+  void add_name(const std::string& name) { _names.push_back(name); }
 
   /**
    * Get the number of randomly-placed objects defined for this material.
@@ -202,12 +300,32 @@ public:
   SGMatModelGroup * get_object_group (int index) const {
     return object_groups[index];
   }
+  
+  /**
+   * Evaluate whether this material is valid given the current global
+   * property state.
+   */
+     bool valid() const;
 
   /**
    * Return pointer to glyph class, or 0 if it doesn't exist.
    */
-  SGMaterialGlyph * get_glyph (const string& name) const;
+  SGMaterialGlyph * get_glyph (const std::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:
 
 \f
@@ -223,11 +341,15 @@ protected:
 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;
+      _internal_state(simgear::Effect *e, bool l,
+                      const simgear::SGReaderWriterOptions *o);
+      _internal_state(simgear::Effect *e, const std::string &t, bool l,
+                      const simgear::SGReaderWriterOptions *o);
+      void add_texture(const std::string &t, int i);
+      osg::ref_ptr<simgear::Effect> effect;
+      std::vector<std::pair<std::string,int> > texture_paths;
+      bool effect_realized;
+      osg::ref_ptr<const simgear::SGReaderWriterOptions> options;
   };
 
 private:
@@ -238,10 +360,7 @@ private:
   ////////////////////////////////////////////////////////////////////
 
   // texture status
-  vector<_internal_state> _status;
-
-  // Round-robin counter
-  mutable unsigned int _current_ptr;
+  std::vector<_internal_state> _status;
 
   // texture size
   double xsize, ysize;
@@ -250,54 +369,123 @@ private:
   bool wrapu, wrapv;
 
   // use mipmapping?
-  int mipmap;
-
-  // use anisotropic filtering
-  float filtering;
+  bool mipmap;
 
   // 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 resistence of that surface material
-  double load_resistence;
+  
+  // coverage of buildings
+  double building_coverage;
+  
+  // building spacing
+  double building_spacing;
+  
+  // building texture & lightmap
+  std::string building_texture;
+  std::string building_lightmap;
+
+  // Ratio of the 3 random building sizes
+  double building_small_ratio;
+  double building_medium_ratio;
+  double building_large_ratio;
+  
+  // Proportion of buildings with pitched roofs
+  double building_small_pitch;
+  double building_medium_pitch;
+  double building_large_pitch;
+
+  // Min/Max number of floors for each size
+  int building_small_min_floors; 
+  int building_small_max_floors;
+  int building_medium_min_floors;
+  int building_medium_max_floors;
+  int building_large_min_floors;
+  int building_large_max_floors;
+  
+  // Minimum width and depth for each size
+  double building_small_min_width;
+  double building_small_max_width;
+  double building_small_min_depth;
+  double building_small_max_depth;
+  
+  double building_medium_min_width;
+  double building_medium_max_width;
+  double building_medium_min_depth;
+  double building_medium_max_depth;
+  
+  double building_large_min_width;
+  double building_large_max_width;
+  double building_large_min_depth;
+  double building_large_max_depth;
+  
+  double building_range;
+  
+  // Cosine of the angle of maximum and zero density, 
+  // used to stop buildings and random objects from being 
+  // created on too steep a slope.
+  double cos_object_max_density_slope_angle;
+  double cos_object_zero_density_slope_angle;
+  
+  // coverage of woods
+  double wood_coverage;
+
+  // Range at which trees become visible
+  double tree_range;
+
+  // Height of the tree
+  double tree_height;
+
+  // Width of the tree
+  double tree_width;
+
+  // Number of varieties of tree texture
+  int tree_varieties;
+  
+  // cosine of the tile angle of maximum and zero density,
+  // used to stop trees from being created on too steep a slope.
+  double cos_tree_max_density_slope_angle;
+  double cos_tree_zero_density_slope_angle;
 
   // material properties
   SGVec4f ambient, diffuse, specular, emission;
   double shininess;
 
+  // effect for this material
+  std::string effect;
+
   // the list of names for this material. May be empty.
-  vector<string> _names;
+  std::vector<std::string> _names;
 
-  vector<SGSharedPtr<SGMatModelGroup> > object_groups;
+  std::vector<SGSharedPtr<SGMatModelGroup> > object_groups;
 
   // taxiway-/runway-sign texture elements
-  map<string, SGSharedPtr<SGMaterialGlyph> > glyphs;
+  std::map<std::string, SGSharedPtr<SGMaterialGlyph> > glyphs;
+  
+  // Tree texture, typically a strip of applicable tree textures
+  std::string tree_texture;
+  
+  // Object mask, a simple RGB texture used as a mask when placing
+  // random vegetation, objects and buildings
+  std::vector<osg::Texture2D*> _masks;
+  
+  // Condition, indicating when this material is active
+  SGSharedPtr<const SGCondition> condition;
+  
+  // Parameters from the materials file
+  const SGPropertyNode* parameters;
+
+  // per-material lock for entrypoints called from multiple threads
+  SGMutex _lock;
 
-\f
   ////////////////////////////////////////////////////////////////////
   // Internal constructors and methods.
   ////////////////////////////////////////////////////////////////////
 
-  SGMaterial( const string &fg_root, const SGMaterial &mat ); // unimplemented
-
-  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, float filtering = 1.0f );
-
+  void read_properties(const simgear::SGReaderWriterOptions* options,
+                        const SGPropertyNode *props,
+                        SGPropertyNode *prop_root);
+  void buildEffectProperties(const simgear::SGReaderWriterOptions* options);
+  simgear::Effect* get_effect(int i);
 };
 
 
@@ -321,7 +509,15 @@ public:
   const SGMaterial* getMaterial() const
   { return mMaterial; }
 private:
-  SGSharedPtr<const SGMaterial> mMaterial;
+  // 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