]> git.mxchange.org Git - simgear.git/blob - simgear/scene/material/mat.cxx
b306391bcd40262772ea17893f343fd533690a99
[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 )
61 {
62     init();
63     read_properties( fg_root, props );
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 \f
88 ////////////////////////////////////////////////////////////////////////
89 // Public methods.
90 ////////////////////////////////////////////////////////////////////////
91
92 void
93 SGMaterial::read_properties( const string &fg_root, const SGPropertyNode *props)
94 {
95                                 // Gather the path(s) to the texture(s)
96   vector<SGPropertyNode_ptr> textures = props->getChildren("texture");
97   for (unsigned int i = 0; i < textures.size(); i++)
98   {
99     string tname = textures[i]->getStringValue();
100     if (tname.empty()) {
101         tname = "unknown.rgb";
102     }
103
104     SGPath tpath( fg_root );
105     tpath.append("Textures.high");
106     tpath.append(tname);
107     if ( !ulFileExists(tpath.c_str()) ) {
108       tpath = SGPath( fg_root );
109       tpath.append("Textures");
110       tpath.append(tname);
111     }
112
113     if ( ulFileExists(tpath.c_str()) ) {
114       _internal_state st( NULL, tpath.str(), false );
115       _status.push_back( st );
116     }
117   }
118
119   if (textures.size() == 0) {
120     string tname = "unknown.rgb";
121     SGPath tpath( fg_root );
122     tpath.append("Textures");
123     tpath.append("Terrain");
124     tpath.append(tname);
125     _internal_state st( NULL, tpath.str(), true );
126     _status.push_back( st );
127   }
128
129   xsize = props->getDoubleValue("xsize", 0.0);
130   ysize = props->getDoubleValue("ysize", 0.0);
131   wrapu = props->getBoolValue("wrapu", true);
132   wrapv = props->getBoolValue("wrapv", true);
133   mipmap = props->getBoolValue("mipmap", true);
134   light_coverage = props->getDoubleValue("light-coverage", 0.0);
135   tree_coverage = props->getDoubleValue("tree-coverage", 0.0);
136   tree_height = props->getDoubleValue("tree-height-m", 0.0);
137   tree_width = props->getDoubleValue("tree-width-m", 0.0);
138   tree_range = props->getDoubleValue("tree-range-m", 0.0);
139   tree_varieties = props->getIntValue("tree-varieties", 1);
140
141   SGPath tpath( fg_root );
142   tpath.append(props->getStringValue("tree-texture"));
143   tree_texture = tpath.str();
144
145   // surface values for use with ground reactions
146   solid = props->getBoolValue("solid", true);
147   friction_factor = props->getDoubleValue("friction-factor", 1.0);
148   rolling_friction = props->getDoubleValue("rolling-friction", 0.02);
149   bumpiness = props->getDoubleValue("bumpiness", 0.0);
150   load_resistance = props->getDoubleValue("load-resistance", 1e30);
151
152   // Taken from default values as used in ac3d
153   ambient[0] = props->getDoubleValue("ambient/r", 0.2);
154   ambient[1] = props->getDoubleValue("ambient/g", 0.2);
155   ambient[2] = props->getDoubleValue("ambient/b", 0.2);
156   ambient[3] = props->getDoubleValue("ambient/a", 1.0);
157
158   diffuse[0] = props->getDoubleValue("diffuse/r", 0.8);
159   diffuse[1] = props->getDoubleValue("diffuse/g", 0.8);
160   diffuse[2] = props->getDoubleValue("diffuse/b", 0.8);
161   diffuse[3] = props->getDoubleValue("diffuse/a", 1.0);
162
163   specular[0] = props->getDoubleValue("specular/r", 0.0);
164   specular[1] = props->getDoubleValue("specular/g", 0.0);
165   specular[2] = props->getDoubleValue("specular/b", 0.0);
166   specular[3] = props->getDoubleValue("specular/a", 1.0);
167
168   emission[0] = props->getDoubleValue("emissive/r", 0.0);
169   emission[1] = props->getDoubleValue("emissive/g", 0.0);
170   emission[2] = props->getDoubleValue("emissive/b", 0.0);
171   emission[3] = props->getDoubleValue("emissive/a", 1.0);
172
173   shininess = props->getDoubleValue("shininess", 1.0);
174
175   vector<SGPropertyNode_ptr> object_group_nodes =
176     ((SGPropertyNode *)props)->getChildren("object-group");
177   for (unsigned int i = 0; i < object_group_nodes.size(); i++)
178     object_groups.push_back(new SGMatModelGroup(object_group_nodes[i]));
179
180   // read glyph table for taxi-/runway-signs
181   vector<SGPropertyNode_ptr> glyph_nodes = props->getChildren("glyph");
182   for (unsigned int i = 0; i < glyph_nodes.size(); i++) {
183     const char *name = glyph_nodes[i]->getStringValue("name");
184     if (name)
185       glyphs[name] = new SGMaterialGlyph(glyph_nodes[i]);
186   }
187 }
188
189
190 \f
191 ////////////////////////////////////////////////////////////////////////
192 // Private methods.
193 ////////////////////////////////////////////////////////////////////////
194
195 void 
196 SGMaterial::init ()
197 {
198     _status.clear();
199     _current_ptr = 0;
200     xsize = 0;
201     ysize = 0;
202     wrapu = true;
203     wrapv = true;
204
205     mipmap = true;
206     light_coverage = 0.0;
207
208     solid = true;
209     friction_factor = 1;
210     rolling_friction = 0.02;
211     bumpiness = 0;
212     load_resistance = 1e30;
213
214     shininess = 1.0;
215     for (int i = 0; i < 4; i++) {
216         ambient[i]  = (i < 3) ? 0.2 : 1.0;
217         specular[i] = (i < 3) ? 0.0 : 1.0;
218         diffuse[i]  = (i < 3) ? 0.8 : 1.0;
219         emission[i] = (i < 3) ? 0.0 : 1.0;
220     }
221 }
222
223 osg::StateSet *
224 SGMaterial::get_state (int n)
225 {
226     if (_status.size() == 0) {
227         SG_LOG( SG_GENERAL, SG_WARN, "No state available.");
228         return NULL;
229     }
230     
231     int i = n >= 0 ? n : _current_ptr;
232
233     if(!_status[i].texture_loaded)
234     {
235         assignTexture(_status[i].state.get(), _status[i].texture_path,
236                       wrapu, wrapv, mipmap);
237         _status[i].texture_loaded = true;
238     }
239     osg::StateSet *st = _status[i].state.get();
240
241     _current_ptr += 1;
242     if (_current_ptr >= _status.size())
243         _current_ptr = 0;
244
245     return st;
246 }
247
248
249 void 
250 SGMaterial::build_state( bool defer_tex_load )
251 {
252     StateAttributeFactory *attrFact = StateAttributeFactory::instance();
253     for (unsigned int i = 0; i < _status.size(); i++)
254     {
255         osg::StateSet *stateSet = new osg::StateSet;
256         stateSet->setUserData(new SGMaterialUserData(this));
257
258         // Set up the textured state
259         stateSet->setAttribute(attrFact->getSmoothShadeModel());
260         stateSet->setAttributeAndModes(attrFact->getCullFaceBack());
261
262         stateSet->setMode(GL_LIGHTING, osg::StateAttribute::ON);
263
264         _status[i].texture_loaded = false;
265
266         osg::Material* material = new osg::Material;
267         material->setColorMode(osg::Material::AMBIENT_AND_DIFFUSE);
268         material->setAmbient(osg::Material::FRONT_AND_BACK, ambient.osg());
269         material->setDiffuse(osg::Material::FRONT_AND_BACK, diffuse.osg());
270         material->setSpecular(osg::Material::FRONT_AND_BACK, specular.osg());
271         material->setEmission(osg::Material::FRONT_AND_BACK, emission.osg());
272         material->setShininess(osg::Material::FRONT_AND_BACK, shininess );
273         stateSet->setAttribute(material);
274
275         if (ambient[3] < 1 || diffuse[3] < 1 ||
276             specular[3] < 1 || emission[3] < 1) {
277           stateSet->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
278           stateSet->setMode(GL_BLEND, osg::StateAttribute::ON);
279           stateSet->setMode(GL_ALPHA_TEST, osg::StateAttribute::ON);
280         } else {
281           stateSet->setRenderingHint(osg::StateSet::OPAQUE_BIN);
282           stateSet->setMode(GL_BLEND, osg::StateAttribute::OFF);
283           stateSet->setMode(GL_ALPHA_TEST, osg::StateAttribute::OFF);
284         }
285
286         _status[i].state = stateSet;
287     }
288 }
289
290
291 void SGMaterial::set_state( osg::StateSet *s )
292 {
293     _status.push_back( _internal_state( s, "", true ) );
294 }
295
296 void SGMaterial::assignTexture( osg::StateSet *state, const std::string &fname,
297                  bool _wrapu, bool _wrapv, bool _mipmap )
298 {
299    osg::Texture2D* texture = SGLoadTexture2D(fname, 0, _wrapu, _wrapv,
300                                              mipmap ? -1 : 0);
301    texture->setMaxAnisotropy( SGGetTextureFilter());
302    state->setTextureAttributeAndModes(0, texture);
303
304    StateAttributeFactory *attrFact = StateAttributeFactory::instance();
305    state->setTextureAttributeAndModes(0, attrFact->getStandardTexEnv());
306 }
307
308 SGMaterialGlyph* SGMaterial::get_glyph (const string& name) const
309 {
310   map<string, SGSharedPtr<SGMaterialGlyph> >::const_iterator it;
311   it = glyphs.find(name);
312   if (it == glyphs.end())
313     return 0;
314
315   return it->second;
316 }
317
318 \f
319 ////////////////////////////////////////////////////////////////////////
320 // SGMaterialGlyph.
321 ////////////////////////////////////////////////////////////////////////
322
323 SGMaterialGlyph::SGMaterialGlyph(SGPropertyNode *p) :
324     _left(p->getDoubleValue("left", 0.0)),
325     _right(p->getDoubleValue("right", 1.0))
326 {
327 }
328
329 void
330 SGSetTextureFilter( int max) {
331         SGSceneFeatures::instance()->setTextureFilter( max);
332 }
333
334 int
335 SGGetTextureFilter() {
336         return SGSceneFeatures::instance()->getTextureFilter();
337 }