]> git.mxchange.org Git - simgear.git/blob - simgear/scene/material/mat.hxx
Fix effects code to work with OSG 2.8.2-rc4
[simgear.git] / simgear / scene / material / mat.hxx
1 // mat.hxx -- a material in the scene graph.
2 // TODO: this class needs to be renamed.
3 //
4 // Written by Curtis Olson, started May 1998.
5 // Overhauled by David Megginson, December 2001
6 //
7 // Copyright (C) 1998 - 2000  Curtis L. Olson  - http://www.flightgear.org/~curt
8 //
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.
13 //
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.
18 //
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.
22 //
23 // $Id$
24
25
26 #ifndef _SG_MAT_HXX
27 #define _SG_MAT_HXX
28
29 #ifndef __cplusplus
30 # error This library requires C++
31 #endif
32
33 #include <simgear/compiler.h>
34
35 #include <string>      // Standard C++ string library
36 #include <vector>
37 #include <map>
38
39 #include <simgear/math/SGMath.hxx>
40
41 #include <osg/ref_ptr>
42 #include <osgDB/ReaderWriter>
43
44 namespace osg
45 {
46 class StateSet;
47 }
48
49 #include <simgear/props/props.hxx>
50 #include <simgear/structure/SGSharedPtr.hxx>
51 #include <simgear/scene/util/SGSceneFeatures.hxx>
52
53 #include "matmodel.hxx"
54
55 namespace simgear
56 {
57 class Effect;
58 }
59
60 class SGMaterialGlyph;
61
62 /**
63  * A material in the scene graph.
64  *
65  * A material represents information about a single surface type
66  * in the 3D scene graph, including texture, colour, lighting,
67  * tiling, and so on; most of the materials in FlightGear are
68  * defined in the $FG_ROOT/materials.xml file, and can be changed
69  * at runtime.
70  */
71 class SGMaterial : public SGReferenced {
72
73 public:
74
75 \f
76   ////////////////////////////////////////////////////////////////////
77   // Public Constructors.
78   ////////////////////////////////////////////////////////////////////
79
80   /**
81    * Construct a material from a set of properties.
82    *
83    * @param props A property node containing subnodes with the
84    * state information for the material.  This node is usually
85    * loaded from the $FG_ROOT/materials.xml file.
86    */
87   SGMaterial( const osgDB::ReaderWriter::Options*, const SGPropertyNode *props);
88
89   /**
90    * Destructor.
91    */
92   ~SGMaterial( void );
93
94
95 \f
96   ////////////////////////////////////////////////////////////////////
97   // Public methods.
98   ////////////////////////////////////////////////////////////////////
99
100   /**
101    * Get the textured state.
102    */
103   simgear::Effect *get_effect(int n = -1);
104
105   /**
106    * Get the number of textures assigned to this material.
107    */
108   inline int get_num() const { return _status.size(); }
109
110
111   /**
112    * Get the xsize of the texture, in meters.
113    */
114   inline double get_xsize() const { return xsize; }
115
116
117   /**
118    * Get the ysize of the texture, in meters.
119    */
120   inline double get_ysize() const { return ysize; }
121
122
123   /**
124    * Get the light coverage.
125    *
126    * A smaller number means more generated night lighting.
127    *
128    * @return The area (m^2) covered by each light.
129    */
130   inline double get_light_coverage () const { return light_coverage; }
131
132   /**
133    * Get the forest coverage.
134    *
135    * A smaller number means more generated forest canopy.
136    *
137    * @return The area (m^2) covered by each canopy.
138    */
139   inline double get_tree_coverage () const { return tree_coverage; }
140
141   /**
142    * Get the forest height.
143    *
144    * @return The average height of the trees.
145    */
146   inline double get_tree_height () const { return tree_height; }
147
148   /**
149    * Get the forest width.
150    *
151    * @return The average width of the trees.
152    */
153   inline double get_tree_width () const { return tree_width; }
154
155   /**
156    * Get the forest LoD range.
157    *
158    * @return The LoD range for the trees.
159    */
160   inline double get_tree_range () const { return tree_range; }
161   
162   /**
163    * Get the number of tree varieties available
164    *
165    * @return the number of different trees defined in the texture strip
166    */
167   inline int get_tree_varieties () const { return tree_varieties; }
168   
169   /**
170    * Get the texture strip to use for trees
171    *
172    * @return the texture to use for trees.
173    */
174   inline std::string get_tree_texture () const { return  tree_texture; }
175
176   /**
177    * Return if the surface material is solid, if it is not solid, a fluid
178    * can be assumed, that is usually water.
179    */
180   bool get_solid () const { return solid; }
181
182   /**
183    * Get the friction factor for that material
184    */
185   double get_friction_factor () const { return friction_factor; }
186
187   /**
188    * Get the rolling friction for that material
189    */
190   double get_rolling_friction () const { return rolling_friction; }
191
192   /**
193    * Get the bumpines for that material
194    */
195   double get_bumpiness () const { return bumpiness; }
196
197   /**
198    * Get the load resistance
199    */
200   double get_load_resistance () const { return load_resistance; }
201
202   /**
203    * Get the list of names for this material
204    */
205   const std::vector<std::string>& get_names() const { return _names; }
206
207   /**
208    * add the given name to the list of names this material is known
209    */
210   void add_name(const std::string& name) { _names.push_back(name); }
211
212   /**
213    * Get the number of randomly-placed objects defined for this material.
214    */
215   int get_object_group_count () const { return object_groups.size(); }
216
217   /**
218    * Get a randomly-placed object for this material.
219    */
220   SGMatModelGroup * get_object_group (int index) const {
221     return object_groups[index];
222   }
223
224   /**
225    * Return pointer to glyph class, or 0 if it doesn't exist.
226    */
227   SGMaterialGlyph * get_glyph (const std::string& name) const;
228
229   void set_light_color(const SGVec4f& color)
230   { emission = color; }
231   const SGVec4f& get_light_color() const
232   { return emission; }
233
234   SGVec2f get_tex_coord_scale() const
235   {
236     float tex_width = get_xsize();
237     float tex_height = get_ysize();
238
239     return SGVec2f((0 < tex_width) ? 1000.0f/tex_width : 1.0f,
240                    (0 < tex_height) ? 1000.0f/tex_height : 1.0f);
241   }
242
243 protected:
244
245 \f
246   ////////////////////////////////////////////////////////////////////
247   // Protected methods.
248   ////////////////////////////////////////////////////////////////////
249
250   /**
251    * Initialization method, invoked by all public constructors.
252    */
253   void init();
254
255 protected:
256
257   struct _internal_state {
258       _internal_state(simgear::Effect *e, const std::string &t, bool l,
259                       const osgDB::ReaderWriter::Options *o);
260       osg::ref_ptr<simgear::Effect> effect;
261       std::string texture_path;
262       bool effect_realized;
263       osg::ref_ptr<const osgDB::ReaderWriter::Options> options;
264   };
265
266 private:
267
268 \f
269   ////////////////////////////////////////////////////////////////////
270   // Internal state.
271   ////////////////////////////////////////////////////////////////////
272
273   // texture status
274   std::vector<_internal_state> _status;
275
276   // Round-robin counter
277   mutable unsigned int _current_ptr;
278
279   // texture size
280   double xsize, ysize;
281
282   // wrap texture?
283   bool wrapu, wrapv;
284
285   // use mipmapping?
286   bool mipmap;
287
288   // coverage of night lighting.
289   double light_coverage;
290   
291   // coverage of trees
292   double tree_coverage;
293   
294   // Range at which trees become visible
295   double tree_range;
296
297   // Height of the tree
298   double tree_height;
299
300   // Width of the tree
301   double tree_width;
302
303   // Number of varieties of tree texture
304   int tree_varieties;
305
306   // True if the material is solid, false if it is a fluid
307   bool solid;
308
309   // the friction factor of that surface material
310   double friction_factor;
311
312   // the rolling friction of that surface material
313   double rolling_friction;
314
315   // the bumpiness of that surface material
316   double bumpiness;
317
318   // the load resistance of that surface material
319   double load_resistance;
320
321   // material properties
322   SGVec4f ambient, diffuse, specular, emission;
323   double shininess;
324
325   // effect for this material
326   std::string effect;
327
328   // the list of names for this material. May be empty.
329   std::vector<std::string> _names;
330
331   std::vector<SGSharedPtr<SGMatModelGroup> > object_groups;
332
333   // taxiway-/runway-sign texture elements
334   std::map<std::string, SGSharedPtr<SGMaterialGlyph> > glyphs;
335   
336   // Tree texture, typically a strip of applicable tree textures
337   std::string tree_texture;
338 \f
339   ////////////////////////////////////////////////////////////////////
340   // Internal constructors and methods.
341   ////////////////////////////////////////////////////////////////////
342
343   void read_properties(const osgDB::ReaderWriter::Options* options,
344                         const SGPropertyNode *props);
345   void buildEffectProperties(const osgDB::ReaderWriter::Options* options);
346 };
347
348
349 class SGMaterialGlyph : public SGReferenced {
350 public:
351   SGMaterialGlyph(SGPropertyNode *);
352   inline double get_left() const { return _left; }
353   inline double get_right() const { return _right; }
354   inline double get_width() const { return _right - _left; }
355
356 protected:
357   double _left;
358   double _right;
359 };
360
361 class SGMaterialUserData : public osg::Referenced {
362 public:
363   SGMaterialUserData(const SGMaterial* material) :
364     mMaterial(material)
365   {}
366   const SGMaterial* getMaterial() const
367   { return mMaterial; }
368 private:
369   // this cannot be an SGSharedPtr since that would create a cicrular reference
370   // making it impossible to ever free the space needed by SGMaterial
371   const SGMaterial* mMaterial;
372 };
373
374 void
375 SGSetTextureFilter( int max);
376
377 int
378 SGGetTextureFilter();
379
380 #endif // _SG_MAT_HXX