]> git.mxchange.org Git - simgear.git/blob - simgear/scene/material/mat.cxx
33162abe510d3cf03228bedd1ff339832cd67dd6
[simgear.git] / simgear / scene / material / mat.cxx
1 // mat.cxx -- class to handle material properties
2 //
3 // Written by Curtis Olson, started May 1998.
4 //
5 // Copyright (C) 1998 - 2000  Curtis L. Olson  - http://www.flightgear.org/~curt
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20 //
21 // $Id$
22
23
24 #ifdef HAVE_CONFIG_H
25 #  include <simgear_config.h>
26 #endif
27
28 #include <simgear/compiler.h>
29
30 #include <string.h>
31 #include <map>
32
33 #include <plib/ul.h>
34
35 #include <osg/CullFace>
36 #include <osg/Material>
37 #include <osg/ShadeModel>
38 #include <osg/TexEnv>
39 #include <osg/Texture2D>
40 #include <osgDB/ReadFile>
41
42 #include <simgear/debug/logstream.hxx>
43 #include <simgear/misc/sg_path.hxx>
44 #include <simgear/misc/sgstream.hxx>
45
46 #include <simgear/scene/model/model.hxx>
47 #include <simgear/scene/util/StateAttributeFactory.hxx>
48
49 #include "mat.hxx"
50
51 using std::map;
52 using namespace simgear;
53
54 \f
55 ////////////////////////////////////////////////////////////////////////
56 // Constructors and destructor.
57 ////////////////////////////////////////////////////////////////////////
58
59
60 SGMaterial::SGMaterial( const string &fg_root, const SGPropertyNode *props, const char *season )
61 {
62     init();
63     read_properties( fg_root, props, season );
64     build_state( false );
65 }
66
67 SGMaterial::SGMaterial( const string &texpath )
68 {
69     init();
70
71     _internal_state st( NULL, texpath, false );
72     _status.push_back( st );
73
74     build_state( true );
75 }
76
77 SGMaterial::SGMaterial( osg::StateSet *s )
78 {
79     init();
80     set_state( s );
81 }
82
83 SGMaterial::~SGMaterial (void)
84 {
85 }
86
87
88 \f
89 ////////////////////////////////////////////////////////////////////////
90 // Public methods.
91 ////////////////////////////////////////////////////////////////////////
92
93 void
94 SGMaterial::read_properties( const string &fg_root, const SGPropertyNode * props, const char *season )
95 {
96                                 // Gather the path(s) to the texture(s)
97   vector<SGPropertyNode_ptr> textures = props->getChildren("texture");
98   for (unsigned int i = 0; i < textures.size(); i++)
99   {
100     string tname = textures[i]->getStringValue();
101     string otname = tname;
102
103     if (tname == "") {
104         tname = "unknown.rgb";
105     }
106
107     SGPath tpath( fg_root );
108     tpath.append("Textures.high");
109     tpath.append(tname);
110     if ( !ulFileExists(tpath.c_str()) ) {
111       tpath = SGPath( fg_root );
112       tpath.append("Textures");
113       tpath.append(tname);
114     }
115
116     if ( ulFileExists(tpath.c_str()) ) {
117       _internal_state st( NULL, tpath.str(), false );
118       _status.push_back( st );
119     }
120   }
121
122   if (textures.size() == 0) {
123     string tname = "unknown.rgb";
124     SGPath tpath( fg_root );
125     tpath.append("Textures");
126     tpath.append("Terrain");
127     tpath.append(tname);
128     _internal_state st( NULL, tpath.str(), true );
129     _status.push_back( st );
130   }
131
132   xsize = props->getDoubleValue("xsize", 0.0);
133   ysize = props->getDoubleValue("ysize", 0.0);
134   wrapu = props->getBoolValue("wrapu", true);
135   wrapv = props->getBoolValue("wrapv", true);
136   mipmap = props->getBoolValue("mipmap", true);
137   light_coverage = props->getDoubleValue("light-coverage", 0.0);
138   tree_coverage = props->getDoubleValue("tree-coverage", 0.0);
139   tree_height = props->getDoubleValue("tree-height-m", 0.0);
140   tree_width = props->getDoubleValue("tree-width-m", 0.0);
141   tree_range = props->getDoubleValue("tree-range-m", 0.0);
142   tree_varieties = props->getIntValue("tree-varieties", 1);
143
144   SGPath tpath( fg_root );
145   tpath.append(props->getStringValue("tree-texture"));
146   tree_texture = tpath.str();
147
148   // surface values for use with ground reactions
149   solid = props->getBoolValue("solid", true);
150   friction_factor = props->getDoubleValue("friction-factor", 1.0);
151   rolling_friction = props->getDoubleValue("rolling-friction", 0.02);
152   bumpiness = props->getDoubleValue("bumpiness", 0.0);
153   load_resistance = props->getDoubleValue("load-resistance", 1e30);
154
155   // Taken from default values as used in ac3d
156   ambient[0] = props->getDoubleValue("ambient/r", 0.2);
157   ambient[1] = props->getDoubleValue("ambient/g", 0.2);
158   ambient[2] = props->getDoubleValue("ambient/b", 0.2);
159   ambient[3] = props->getDoubleValue("ambient/a", 1.0);
160
161   diffuse[0] = props->getDoubleValue("diffuse/r", 0.8);
162   diffuse[1] = props->getDoubleValue("diffuse/g", 0.8);
163   diffuse[2] = props->getDoubleValue("diffuse/b", 0.8);
164   diffuse[3] = props->getDoubleValue("diffuse/a", 1.0);
165
166   specular[0] = props->getDoubleValue("specular/r", 0.0);
167   specular[1] = props->getDoubleValue("specular/g", 0.0);
168   specular[2] = props->getDoubleValue("specular/b", 0.0);
169   specular[3] = props->getDoubleValue("specular/a", 1.0);
170
171   emission[0] = props->getDoubleValue("emissive/r", 0.0);
172   emission[1] = props->getDoubleValue("emissive/g", 0.0);
173   emission[2] = props->getDoubleValue("emissive/b", 0.0);
174   emission[3] = props->getDoubleValue("emissive/a", 1.0);
175
176   shininess = props->getDoubleValue("shininess", 1.0);
177
178   vector<SGPropertyNode_ptr> object_group_nodes =
179     ((SGPropertyNode *)props)->getChildren("object-group");
180   for (unsigned int i = 0; i < object_group_nodes.size(); i++)
181     object_groups.push_back(new SGMatModelGroup(object_group_nodes[i]));
182
183   // read glyph table for taxi-/runway-signs
184   vector<SGPropertyNode_ptr> glyph_nodes = props->getChildren("glyph");
185   for (unsigned int i = 0; i < glyph_nodes.size(); i++) {
186     const char *name = glyph_nodes[i]->getStringValue("name");
187     if (name)
188       glyphs[name] = new SGMaterialGlyph(glyph_nodes[i]);
189   }
190 }
191
192
193 \f
194 ////////////////////////////////////////////////////////////////////////
195 // Private methods.
196 ////////////////////////////////////////////////////////////////////////
197
198 void 
199 SGMaterial::init ()
200 {
201     _status.clear();
202     _current_ptr = 0;
203     xsize = 0;
204     ysize = 0;
205     wrapu = true;
206     wrapv = true;
207
208     mipmap = true;
209     light_coverage = 0.0;
210
211     solid = true;
212     friction_factor = 1;
213     rolling_friction = 0.02;
214     bumpiness = 0;
215     load_resistance = 1e30;
216
217     shininess = 1.0;
218     for (int i = 0; i < 4; i++) {
219         ambient[i]  = (i < 3) ? 0.2 : 1.0;
220         specular[i] = (i < 3) ? 0.0 : 1.0;
221         diffuse[i]  = (i < 3) ? 0.8 : 1.0;
222         emission[i] = (i < 3) ? 0.0 : 1.0;
223     }
224 }
225
226 osg::StateSet *
227 SGMaterial::get_state (int n)
228 {
229     if (_status.size() == 0) {
230         SG_LOG( SG_GENERAL, SG_WARN, "No state available.");
231         return NULL;
232     }
233     
234     int i = n >= 0 ? n : _current_ptr;
235
236     if(!_status[i].texture_loaded)
237     {
238         assignTexture(_status[i].state.get(), _status[i].texture_path,
239                       wrapu, wrapv, mipmap);
240         _status[i].texture_loaded = true;
241     }
242     osg::StateSet *st = _status[i].state.get();
243
244     _current_ptr += 1;
245     if (_current_ptr >= _status.size())
246         _current_ptr = 0;
247
248     return st;
249 }
250
251
252 void 
253 SGMaterial::build_state( bool defer_tex_load )
254 {
255     StateAttributeFactory *attrFact = StateAttributeFactory::instance();
256     for (unsigned int i = 0; i < _status.size(); i++)
257     {
258         osg::StateSet *stateSet = new osg::StateSet;
259         stateSet->setUserData(new SGMaterialUserData(this));
260
261         // Set up the textured state
262         stateSet->setAttribute(attrFact->getSmoothShadeModel());
263         stateSet->setAttributeAndModes(attrFact->getCullFaceBack());
264
265         stateSet->setMode(GL_LIGHTING, osg::StateAttribute::ON);
266
267         _status[i].texture_loaded = false;
268
269         osg::Material* material = new osg::Material;
270         material->setColorMode(osg::Material::AMBIENT_AND_DIFFUSE);
271         material->setAmbient(osg::Material::FRONT_AND_BACK, ambient.osg());
272         material->setDiffuse(osg::Material::FRONT_AND_BACK, diffuse.osg());
273         material->setSpecular(osg::Material::FRONT_AND_BACK, specular.osg());
274         material->setEmission(osg::Material::FRONT_AND_BACK, emission.osg());
275         material->setShininess(osg::Material::FRONT_AND_BACK, shininess );
276         stateSet->setAttribute(material);
277
278         if (ambient[3] < 1 || diffuse[3] < 1 ||
279             specular[3] < 1 || emission[3] < 1) {
280           stateSet->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
281           stateSet->setMode(GL_BLEND, osg::StateAttribute::ON);
282           stateSet->setMode(GL_ALPHA_TEST, osg::StateAttribute::ON);
283         } else {
284           stateSet->setRenderingHint(osg::StateSet::OPAQUE_BIN);
285           stateSet->setMode(GL_BLEND, osg::StateAttribute::OFF);
286           stateSet->setMode(GL_ALPHA_TEST, osg::StateAttribute::OFF);
287         }
288
289         _status[i].state = stateSet;
290     }
291 }
292
293
294 void SGMaterial::set_state( osg::StateSet *s )
295 {
296     _status.push_back( _internal_state( s, "", true ) );
297 }
298
299 void SGMaterial::assignTexture( osg::StateSet *state, const std::string &fname,
300                  bool _wrapu, bool _wrapv, bool _mipmap )
301 {
302    osg::Texture2D* texture = SGLoadTexture2D(fname, 0, _wrapu, _wrapv,
303                                              mipmap ? -1 : 0);
304    texture->setMaxAnisotropy( SGGetTextureFilter());
305    state->setTextureAttributeAndModes(0, texture);
306
307    osg::TexEnv* texEnv = new osg::TexEnv;
308    texEnv->setMode(osg::TexEnv::MODULATE);
309    state->setTextureAttributeAndModes(0, texEnv);
310 }
311
312 SGMaterialGlyph* SGMaterial::get_glyph (const string& name) const
313 {
314   map<string, SGSharedPtr<SGMaterialGlyph> >::const_iterator it;
315   it = glyphs.find(name);
316   if (it == glyphs.end())
317     return 0;
318
319   return it->second;
320 }
321
322 \f
323 ////////////////////////////////////////////////////////////////////////
324 // SGMaterialGlyph.
325 ////////////////////////////////////////////////////////////////////////
326
327 SGMaterialGlyph::SGMaterialGlyph(SGPropertyNode *p) :
328     _left(p->getDoubleValue("left", 0.0)),
329     _right(p->getDoubleValue("right", 1.0))
330 {
331 }
332
333 void
334 SGSetTextureFilter( int max) {
335         SGSceneFeatures::instance()->setTextureFilter( max);
336 }
337
338 int
339 SGGetTextureFilter() {
340         return SGSceneFeatures::instance()->getTextureFilter();
341 }