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