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