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