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