]> git.mxchange.org Git - simgear.git/blob - simgear/scene/material/mat.cxx
8230107a1b3bb2cc9d68e7ee19f72581046b8ebf
[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 "mat.hxx"
34
35 #include <osg/CullFace>
36 #include <osg/Material>
37 #include <osg/ShadeModel>
38 #include <osg/StateSet>
39 #include <osg/TexEnv>
40 #include <osg/Texture2D>
41 #include <osgDB/ReadFile>
42 #include <osgDB/FileUtils>
43
44 #include <simgear/debug/logstream.hxx>
45 #include <simgear/misc/sg_path.hxx>
46 #include <simgear/misc/sgstream.hxx>
47
48 #include <simgear/scene/model/model.hxx>
49 #include <simgear/scene/util/StateAttributeFactory.hxx>
50
51 #include "Effect.hxx"
52 #include "Technique.hxx"
53 #include "Pass.hxx"
54
55 using std::map;
56 using namespace simgear;
57
58 \f
59 ////////////////////////////////////////////////////////////////////////
60 // Constructors and destructor.
61 ////////////////////////////////////////////////////////////////////////
62
63 SGMaterial::_internal_state::_internal_state(osg::StateSet *s,
64                                              const std::string &t, bool l ) :
65     state(s), texture_path(t), texture_loaded(l)
66 {
67 }
68
69 SGMaterial::SGMaterial( const string &fg_root, const SGPropertyNode *props )
70 {
71     init();
72     read_properties( fg_root, props );
73     build_state( false );
74 }
75
76 SGMaterial::SGMaterial( const string &texpath )
77 {
78     init();
79
80     _internal_state st( NULL, texpath, false );
81     _status.push_back( st );
82
83     build_state( true );
84 }
85
86 SGMaterial::SGMaterial( osg::StateSet *s )
87 {
88     init();
89     set_state( s );
90 }
91
92 SGMaterial::~SGMaterial (void)
93 {
94 }
95
96 \f
97 ////////////////////////////////////////////////////////////////////////
98 // Public methods.
99 ////////////////////////////////////////////////////////////////////////
100
101 void
102 SGMaterial::read_properties( const string &fg_root, const SGPropertyNode *props)
103 {
104                                 // Gather the path(s) to the texture(s)
105   vector<SGPropertyNode_ptr> textures = props->getChildren("texture");
106   for (unsigned int i = 0; i < textures.size(); i++)
107   {
108     string tname = textures[i]->getStringValue();
109     if (tname.empty()) {
110         tname = "unknown.rgb";
111     }
112
113     SGPath tpath( fg_root );
114     tpath.append("Textures.high");
115     tpath.append(tname);
116     if ( !osgDB::fileExists(tpath.str()) ) {
117       tpath = SGPath( fg_root );
118       tpath.append("Textures");
119       tpath.append(tname);
120     }
121
122     if ( osgDB::fileExists(tpath.str()) ) {
123       _internal_state st( NULL, tpath.str(), false );
124       _status.push_back( st );
125     }
126   }
127
128   if (textures.size() == 0) {
129     string tname = "unknown.rgb";
130     SGPath tpath( fg_root );
131     tpath.append("Textures");
132     tpath.append("Terrain");
133     tpath.append(tname);
134     _internal_state st( NULL, tpath.str(), true );
135     _status.push_back( st );
136   }
137
138   xsize = props->getDoubleValue("xsize", 0.0);
139   ysize = props->getDoubleValue("ysize", 0.0);
140   wrapu = props->getBoolValue("wrapu", true);
141   wrapv = props->getBoolValue("wrapv", true);
142   mipmap = props->getBoolValue("mipmap", true);
143   light_coverage = props->getDoubleValue("light-coverage", 0.0);
144   tree_coverage = props->getDoubleValue("tree-coverage", 0.0);
145   tree_height = props->getDoubleValue("tree-height-m", 0.0);
146   tree_width = props->getDoubleValue("tree-width-m", 0.0);
147   tree_range = props->getDoubleValue("tree-range-m", 0.0);
148   tree_varieties = props->getIntValue("tree-varieties", 1);
149
150   SGPath tpath( fg_root );
151   tpath.append(props->getStringValue("tree-texture"));
152   tree_texture = tpath.str();
153
154   // surface values for use with ground reactions
155   solid = props->getBoolValue("solid", true);
156   friction_factor = props->getDoubleValue("friction-factor", 1.0);
157   rolling_friction = props->getDoubleValue("rolling-friction", 0.02);
158   bumpiness = props->getDoubleValue("bumpiness", 0.0);
159   load_resistance = props->getDoubleValue("load-resistance", 1e30);
160
161   // Taken from default values as used in ac3d
162   ambient[0] = props->getDoubleValue("ambient/r", 0.2);
163   ambient[1] = props->getDoubleValue("ambient/g", 0.2);
164   ambient[2] = props->getDoubleValue("ambient/b", 0.2);
165   ambient[3] = props->getDoubleValue("ambient/a", 1.0);
166
167   diffuse[0] = props->getDoubleValue("diffuse/r", 0.8);
168   diffuse[1] = props->getDoubleValue("diffuse/g", 0.8);
169   diffuse[2] = props->getDoubleValue("diffuse/b", 0.8);
170   diffuse[3] = props->getDoubleValue("diffuse/a", 1.0);
171
172   specular[0] = props->getDoubleValue("specular/r", 0.0);
173   specular[1] = props->getDoubleValue("specular/g", 0.0);
174   specular[2] = props->getDoubleValue("specular/b", 0.0);
175   specular[3] = props->getDoubleValue("specular/a", 1.0);
176
177   emission[0] = props->getDoubleValue("emissive/r", 0.0);
178   emission[1] = props->getDoubleValue("emissive/g", 0.0);
179   emission[2] = props->getDoubleValue("emissive/b", 0.0);
180   emission[3] = props->getDoubleValue("emissive/a", 1.0);
181
182   shininess = props->getDoubleValue("shininess", 1.0);
183
184   vector<SGPropertyNode_ptr> object_group_nodes =
185     ((SGPropertyNode *)props)->getChildren("object-group");
186   for (unsigned int i = 0; i < object_group_nodes.size(); i++)
187     object_groups.push_back(new SGMatModelGroup(object_group_nodes[i]));
188
189   // read glyph table for taxi-/runway-signs
190   vector<SGPropertyNode_ptr> glyph_nodes = props->getChildren("glyph");
191   for (unsigned int i = 0; i < glyph_nodes.size(); i++) {
192     const char *name = glyph_nodes[i]->getStringValue("name");
193     if (name)
194       glyphs[name] = new SGMaterialGlyph(glyph_nodes[i]);
195   }
196 }
197
198
199 \f
200 ////////////////////////////////////////////////////////////////////////
201 // Private methods.
202 ////////////////////////////////////////////////////////////////////////
203
204 void 
205 SGMaterial::init ()
206 {
207     _status.clear();
208     _current_ptr = 0;
209     xsize = 0;
210     ysize = 0;
211     wrapu = true;
212     wrapv = true;
213
214     mipmap = true;
215     light_coverage = 0.0;
216
217     solid = true;
218     friction_factor = 1;
219     rolling_friction = 0.02;
220     bumpiness = 0;
221     load_resistance = 1e30;
222
223     shininess = 1.0;
224     for (int i = 0; i < 4; i++) {
225         ambient[i]  = (i < 3) ? 0.2 : 1.0;
226         specular[i] = (i < 3) ? 0.0 : 1.0;
227         diffuse[i]  = (i < 3) ? 0.8 : 1.0;
228         emission[i] = (i < 3) ? 0.0 : 1.0;
229     }
230 }
231
232 osg::StateSet *
233 SGMaterial::get_state (int n)
234 {
235     if (_status.size() == 0) {
236         SG_LOG( SG_GENERAL, SG_WARN, "No state available.");
237         return NULL;
238     }
239     
240     int i = n >= 0 ? n : _current_ptr;
241
242     if(!_status[i].texture_loaded)
243     {
244         assignTexture(_status[i].state.get(), _status[i].texture_path,
245                       wrapu, wrapv, mipmap);
246         _status[i].texture_loaded = true;
247     }
248     osg::StateSet *st = _status[i].state.get();
249
250     _current_ptr += 1;
251     if (_current_ptr >= _status.size())
252         _current_ptr = 0;
253
254     return st;
255 }
256
257 Effect* SGMaterial::get_effect(int n)
258 {
259     if (_status.size() == 0) {
260         SG_LOG( SG_GENERAL, SG_WARN, "No effect available.");
261         return 0;
262     }
263     int i = n >= 0 ? n : _current_ptr;
264     if(!_status[i].texture_loaded) {
265         assignTexture(_status[i].state.get(), _status[i].texture_path,
266                       wrapu, wrapv, mipmap);
267         _status[i].texture_loaded = true;
268     }
269     // XXX This business of returning a "random" alternate texture is
270     // really bogus. It means that the appearance of the terrain
271     // depends on the order in which it is paged in!
272     _current_ptr = (_current_ptr + 1) % _status.size();
273     return _status[i].effect.get();
274 }
275
276 void 
277 SGMaterial::build_state( bool defer_tex_load )
278 {
279     StateAttributeFactory *attrFact = StateAttributeFactory::instance();
280     SGMaterialUserData* user = new SGMaterialUserData(this);
281     for (unsigned int i = 0; i < _status.size(); i++)
282     {
283         osg::StateSet *stateSet = new osg::StateSet;
284         stateSet->setUserData(user);
285
286         // Set up the textured state
287         stateSet->setAttribute(attrFact->getSmoothShadeModel());
288         stateSet->setAttributeAndModes(attrFact->getCullFaceBack());
289
290         stateSet->setMode(GL_LIGHTING, osg::StateAttribute::ON);
291
292         _status[i].texture_loaded = false;
293
294         osg::Material* material = new osg::Material;
295         material->setColorMode(osg::Material::AMBIENT_AND_DIFFUSE);
296         material->setAmbient(osg::Material::FRONT_AND_BACK, ambient.osg());
297         material->setDiffuse(osg::Material::FRONT_AND_BACK, diffuse.osg());
298         material->setSpecular(osg::Material::FRONT_AND_BACK, specular.osg());
299         material->setEmission(osg::Material::FRONT_AND_BACK, emission.osg());
300         material->setShininess(osg::Material::FRONT_AND_BACK, shininess );
301         stateSet->setAttribute(material);
302
303         if (ambient[3] < 1 || diffuse[3] < 1 ||
304             specular[3] < 1 || emission[3] < 1) {
305           stateSet->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
306           stateSet->setMode(GL_BLEND, osg::StateAttribute::ON);
307           stateSet->setMode(GL_ALPHA_TEST, osg::StateAttribute::ON);
308         } else {
309           stateSet->setRenderingHint(osg::StateSet::OPAQUE_BIN);
310           stateSet->setMode(GL_BLEND, osg::StateAttribute::OFF);
311           stateSet->setMode(GL_ALPHA_TEST, osg::StateAttribute::OFF);
312         }
313
314         _status[i].state = stateSet;
315         Pass* pass = new Pass;
316         pass->setStateSet(_status[i].state.get());
317         Technique* tniq = new Technique(true);
318         tniq->passes.push_back(pass);
319         Effect* effect = new Effect;
320         effect->techniques.push_back(tniq);
321         effect->setUserData(user);
322         _status[i].effect = effect;
323     }
324 }
325
326
327 void SGMaterial::set_state( osg::StateSet *s )
328 {
329     _status.push_back( _internal_state( s, "", true ) );
330 }
331
332 void SGMaterial::assignTexture( osg::StateSet *state, const std::string &fname,
333                  bool _wrapu, bool _wrapv, bool _mipmap )
334 {
335    osg::Texture2D* texture = SGLoadTexture2D(fname, 0, _wrapu, _wrapv,
336                                              mipmap ? -1 : 0);
337    texture->setMaxAnisotropy( SGGetTextureFilter());
338    state->setTextureAttributeAndModes(0, texture);
339
340    StateAttributeFactory *attrFact = StateAttributeFactory::instance();
341    state->setTextureAttributeAndModes(0, attrFact->getStandardTexEnv());
342 }
343
344 SGMaterialGlyph* SGMaterial::get_glyph (const string& name) const
345 {
346   map<string, SGSharedPtr<SGMaterialGlyph> >::const_iterator it;
347   it = glyphs.find(name);
348   if (it == glyphs.end())
349     return 0;
350
351   return it->second;
352 }
353
354 \f
355 ////////////////////////////////////////////////////////////////////////
356 // SGMaterialGlyph.
357 ////////////////////////////////////////////////////////////////////////
358
359 SGMaterialGlyph::SGMaterialGlyph(SGPropertyNode *p) :
360     _left(p->getDoubleValue("left", 0.0)),
361     _right(p->getDoubleValue("right", 1.0))
362 {
363 }
364
365 void
366 SGSetTextureFilter( int max) {
367         SGSceneFeatures::instance()->setTextureFilter( max);
368 }
369
370 int
371 SGGetTextureFilter() {
372         return SGSceneFeatures::instance()->getTextureFilter();
373 }