]> git.mxchange.org Git - simgear.git/blob - simgear/scene/material/TextureBuilder.cxx
Effects in models working for transparent materials and chrome animation
[simgear.git] / simgear / scene / material / TextureBuilder.cxx
1 // Copyright (C) 2009  Tim Moore timoore@redhat.com
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Library General Public
5 // License as published by the Free Software Foundation; either
6 // version 2 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Library General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with this program; if not, write to the Free Software
15 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
16
17 #ifdef HAVE_CONFIG_H
18 #  include <simgear_config.h>
19 #endif
20
21 #include "TextureBuilder.hxx"
22
23 #include "Pass.hxx"
24
25 #include <osg/TexEnv>
26 #include <osg/TexEnvCombine>
27 #include <osg/TexGen>
28 #include <osg/Texture1D>
29 #include <osg/Texture2D>
30 #include <osg/Texture3D>
31 #include <osg/TextureRectangle>
32 #include <osgDB/FileUtils>
33
34 #include <boost/lexical_cast.hpp>
35 #include <boost/tuple/tuple.hpp>
36 #include <boost/tuple/tuple_comparison.hpp>
37
38 #include <simgear/scene/util/SGSceneFeatures.hxx>
39 #include <simgear/scene/util/StateAttributeFactory.hxx>
40 #include <simgear/math/SGMath.hxx>
41 #include <simgear/structure/OSGUtils.hxx>
42
43 #include "Noise.hxx"
44
45 namespace simgear
46 {
47 using namespace std;
48 using namespace osg;
49
50 using namespace effect;
51
52 TexEnvCombine* buildTexEnvCombine(Effect* effect,
53                                   const SGPropertyNode* envProp);
54 TexGen* buildTexGen(Effect* Effect, const SGPropertyNode* tgenProp);
55
56 // Hack to force inclusion of TextureBuilder.cxx in library
57 osg::Texture* TextureBuilder::buildFromType(Effect* effect, const string& type,
58                                             const SGPropertyNode*props,
59                                             const osgDB::ReaderWriter::Options*
60                                             options)
61 {
62     return EffectBuilder<Texture>::buildFromType(effect, type, props, options);
63 }
64
65 typedef boost::tuple<string, Texture::FilterMode, Texture::FilterMode,
66                      Texture::WrapMode, Texture::WrapMode, Texture::WrapMode,
67                      string> TexTuple;
68
69 EffectNameValue<TexEnv::Mode> texEnvModesInit[] =
70 {
71     {"add", TexEnv::ADD},
72     {"blend", TexEnv::BLEND},
73     {"decal", TexEnv::DECAL},
74     {"modulate", TexEnv::MODULATE},
75     {"replace", TexEnv::REPLACE}
76 };
77 EffectPropertyMap<TexEnv::Mode> texEnvModes(texEnvModesInit);
78
79 TexEnv* buildTexEnv(Effect* effect, const SGPropertyNode* prop)
80 {
81     const SGPropertyNode* modeProp = getEffectPropertyChild(effect, prop,
82                                                             "mode");
83     const SGPropertyNode* colorProp = getEffectPropertyChild(effect, prop,
84                                                              "color");
85     if (!modeProp)
86         return 0;
87     TexEnv::Mode mode = TexEnv::MODULATE;
88     findAttr(texEnvModes, modeProp, mode);
89     if (mode == TexEnv::MODULATE) {
90         return StateAttributeFactory::instance()->getStandardTexEnv();
91     }
92     TexEnv* env = new TexEnv(mode);
93     if (colorProp)
94         env->setColor(toOsg(colorProp->getValue<SGVec4d>()));
95     return env;
96  }
97
98
99 void TextureUnitBuilder::buildAttribute(Effect* effect, Pass* pass,
100                                         const SGPropertyNode* prop,
101                                         const osgDB::ReaderWriter::Options* options)
102 {
103     if (!isAttributeActive(effect, prop))
104         return;
105     // Decode the texture unit
106     int unit = 0;
107     const SGPropertyNode* pUnit = prop->getChild("unit");
108     if (pUnit) {
109         unit = pUnit->getValue<int>();
110     } else {
111         const SGPropertyNode* pName = prop->getChild("name");
112         if (pName)
113             try {
114                 unit = boost::lexical_cast<int>(pName->getStringValue());
115             } catch (boost::bad_lexical_cast& lex) {
116                 SG_LOG(SG_INPUT, SG_ALERT, "can't decode name as texture unit "
117                        << lex.what());
118             }
119     }
120     const SGPropertyNode* pType = getEffectPropertyChild(effect, prop, "type");
121     string type;
122     if (!pType)
123         type = "2d";
124     else
125         type = pType->getStringValue();
126     Texture* texture = 0;
127     try {
128         texture = TextureBuilder::buildFromType(effect, type, prop,
129                                                 options);
130     }
131     catch (BuilderException& e) {
132         SG_LOG(SG_INPUT, SG_ALERT, "No image file for texture, using white ");
133         texture = StateAttributeFactory::instance()->getWhiteTexture();
134     }
135     pass->setTextureAttributeAndModes(unit, texture);
136     const SGPropertyNode* envProp = prop->getChild("environment");
137     if (envProp) {
138         TexEnv* env = buildTexEnv(effect, envProp);
139         if (env)
140             pass->setTextureAttributeAndModes(unit, env);
141     }
142     const SGPropertyNode* combineProp = prop->getChild("texenv-combine");
143     TexEnvCombine* combiner = 0;
144     if (combineProp && ((combiner = buildTexEnvCombine(effect, combineProp))))
145         pass->setTextureAttributeAndModes(unit, combiner);
146     const SGPropertyNode* tgenProp = prop->getChild("texgen");
147     TexGen* tgen = 0;
148     if (tgenProp && (tgen = buildTexGen(effect, tgenProp)))
149         pass->setTextureAttributeAndModes(unit, tgen);
150 }
151
152 // InstallAttributeBuilder call is in Effect.cxx to force this file to
153 // be linked in.
154
155 namespace
156 {
157 EffectNameValue<Texture::FilterMode> filterModesInit[] =
158 {
159     { "linear", Texture::LINEAR },
160     { "linear-mipmap-linear", Texture::LINEAR_MIPMAP_LINEAR},
161     { "linear-mipmap-nearest", Texture::LINEAR_MIPMAP_NEAREST},
162     { "nearest", Texture::NEAREST},
163     { "nearest-mipmap-linear", Texture::NEAREST_MIPMAP_LINEAR},
164     { "nearest-mipmap-nearest", Texture::NEAREST_MIPMAP_NEAREST}
165 };
166 EffectPropertyMap<Texture::FilterMode> filterModes(filterModesInit);
167
168 EffectNameValue<Texture::WrapMode> wrapModesInit[] =
169 {
170     {"clamp", Texture::CLAMP},
171     {"clamp-to-border", Texture::CLAMP_TO_BORDER},
172     {"clamp-to-edge", Texture::CLAMP_TO_EDGE},
173     {"mirror", Texture::MIRROR},
174     {"repeat", Texture::REPEAT}
175 };
176 EffectPropertyMap<Texture::WrapMode> wrapModes(wrapModesInit);
177
178
179 TexTuple makeTexTuple(Effect* effect, const SGPropertyNode* props,
180                       const osgDB::ReaderWriter::Options* options,
181                       const string& texType)
182 {
183     Texture::FilterMode minFilter = Texture::LINEAR_MIPMAP_LINEAR;
184     findAttr(filterModes, getEffectPropertyChild(effect, props, "filter"),
185              minFilter);
186     Texture::FilterMode magFilter = Texture::LINEAR;
187     findAttr(filterModes, getEffectPropertyChild(effect, props,
188                                                  "mag-filter"),
189              magFilter);
190     const SGPropertyNode* pWrapS
191         = getEffectPropertyChild(effect, props, "wrap-s");
192     Texture::WrapMode sWrap = Texture::CLAMP;
193     findAttr(wrapModes, pWrapS, sWrap);
194     const SGPropertyNode* pWrapT
195         = getEffectPropertyChild(effect, props, "wrap-t");
196     Texture::WrapMode tWrap = Texture::CLAMP;
197     findAttr(wrapModes, pWrapT, tWrap);
198     const SGPropertyNode* pWrapR
199         = getEffectPropertyChild(effect, props, "wrap-r");
200     Texture::WrapMode rWrap = Texture::CLAMP;
201     findAttr(wrapModes, pWrapR, rWrap);
202     const SGPropertyNode* pImage
203         = getEffectPropertyChild(effect, props, "image");
204     string imageName;
205     if (pImage)
206         imageName = pImage->getStringValue();
207     string absFileName = osgDB::findDataFile(imageName, options);
208     return TexTuple(absFileName, minFilter, magFilter, sWrap, tWrap, rWrap,
209                     texType);
210 }
211
212 void setAttrs(const TexTuple& attrs, Texture* tex,
213               const osgDB::ReaderWriter::Options* options)
214 {
215     const string& imageName = attrs.get<0>();
216     if (imageName.empty()) {
217         throw BuilderException("no image file");
218     } else {
219         osgDB::ReaderWriter::ReadResult result
220             = osgDB::Registry::instance()->readImage(imageName, options);
221         if (result.success()) {
222             osg::Image* image = result.getImage();
223             tex->setImage(GL_FRONT_AND_BACK, image);
224             int s = image->s();
225             int t = image->t();
226             if (s <= t && 32 <= s) {
227                 SGSceneFeatures::instance()->setTextureCompression(tex);
228             } else if (t < s && 32 <= t) {
229                 SGSceneFeatures::instance()->setTextureCompression(tex);
230             }
231             tex->setMaxAnisotropy(SGSceneFeatures::instance()
232                                   ->getTextureFilter());
233         } else {
234             SG_LOG(SG_INPUT, SG_ALERT, "failed to load effect texture file "
235                    << imageName);
236         }
237     }
238     // texture->setDataVariance(osg::Object::STATIC);
239     tex->setFilter(Texture::MIN_FILTER, attrs.get<1>());
240     tex->setFilter(Texture::MAG_FILTER, attrs.get<2>());
241     tex->setWrap(Texture::WRAP_S, attrs.get<3>());
242     tex->setWrap(Texture::WRAP_T, attrs.get<4>());
243     tex->setWrap(Texture::WRAP_R, attrs.get<5>());
244 }
245 }
246
247 template<typename T>
248 class TexBuilder : public TextureBuilder
249 {
250 public:
251     TexBuilder(const string& texType) : _type(texType) {}
252     Texture* build(Effect* effect, const SGPropertyNode*,
253                    const osgDB::ReaderWriter::Options* options);
254 protected:
255     typedef map<TexTuple, ref_ptr<T> > TexMap;
256     TexMap texMap;
257     const string _type;
258 };
259
260 template<typename T>
261 Texture* TexBuilder<T>::build(Effect* effect, const SGPropertyNode* props,
262                               const osgDB::ReaderWriter::Options* options)
263 {
264     TexTuple attrs = makeTexTuple(effect, props, options, _type);
265     typename TexMap::iterator itr = texMap.find(attrs);
266     if (itr != texMap.end())
267         return itr->second.get();
268     T* tex = new T;
269     setAttrs(attrs, tex, options);
270     texMap.insert(make_pair(attrs, tex));
271     return tex;
272 }
273
274
275 namespace
276 {
277 TextureBuilder::Registrar install1D("1d", new TexBuilder<Texture1D>("1d"));
278 TextureBuilder::Registrar install2D("2d", new TexBuilder<Texture2D>("2d"));
279 TextureBuilder::Registrar install3D("3d", new TexBuilder<Texture3D>("3d"));
280 }
281
282 class WhiteTextureBuilder : public TextureBuilder
283 {
284 public:
285     Texture* build(Effect* effect, const SGPropertyNode*,
286                    const osgDB::ReaderWriter::Options* options);
287 };
288
289 Texture* WhiteTextureBuilder::build(Effect* effect, const SGPropertyNode*,
290                                     const osgDB::ReaderWriter::Options* options)
291 {
292     return StateAttributeFactory::instance()->getWhiteTexture();
293 }
294
295 namespace
296 {
297 TextureBuilder::Registrar installWhite("white", new WhiteTextureBuilder);
298 }
299
300 osg::Image* make3DNoiseImage(int texSize)
301 {
302     osg::Image* image = new osg::Image;
303     image->setImage(texSize, texSize, texSize,
304                     4, GL_RGBA, GL_UNSIGNED_BYTE,
305                     new unsigned char[4 * texSize * texSize * texSize],
306                     osg::Image::USE_NEW_DELETE);
307
308     const int startFrequency = 4;
309     const int numOctaves = 4;
310
311     int f, i, j, k, inc;
312     double ni[3];
313     double inci, incj, inck;
314     int frequency = startFrequency;
315     GLubyte *ptr;
316     double amp = 0.5;
317
318     osg::notify(osg::WARN) << "creating 3D noise texture... ";
319
320     for (f = 0, inc = 0; f < numOctaves; ++f, frequency *= 2, ++inc, amp *= 0.5)
321     {
322         SetNoiseFrequency(frequency);
323         ptr = image->data();
324         ni[0] = ni[1] = ni[2] = 0;
325
326         inci = 1.0 / (texSize / frequency);
327         for (i = 0; i < texSize; ++i, ni[0] += inci)
328         {
329             incj = 1.0 / (texSize / frequency);
330             for (j = 0; j < texSize; ++j, ni[1] += incj)
331             {
332                 inck = 1.0 / (texSize / frequency);
333                 for (k = 0; k < texSize; ++k, ni[2] += inck, ptr += 4)
334                 {
335                     *(ptr+inc) = (GLubyte) (((noise3(ni) + 1.0) * amp) * 128.0);
336                 }
337             }
338         }
339     }
340
341     osg::notify(osg::WARN) << "DONE" << std::endl;
342     return image;
343 }
344
345 class NoiseBuilder : public TextureBuilder
346 {
347 public:
348     Texture* build(Effect* effect, const SGPropertyNode*,
349                    const osgDB::ReaderWriter::Options* options);
350 protected:
351     typedef map<int, ref_ptr<Texture3D> > NoiseMap;
352     NoiseMap _noises;
353 };
354
355 Texture* NoiseBuilder::build(Effect* effect, const SGPropertyNode* props,
356                              const osgDB::ReaderWriter::Options* options)
357 {
358     int texSize = 64;
359     const SGPropertyNode* sizeProp = getEffectPropertyChild(effect, props,
360                                                             "size");
361     if (sizeProp)
362         texSize = sizeProp->getValue<int>();
363     NoiseMap::iterator itr = _noises.find(texSize);
364     if (itr != _noises.end())
365         return itr->second.get();
366     Texture3D* noiseTexture = new osg::Texture3D;
367     noiseTexture->setFilter(osg::Texture3D::MIN_FILTER, osg::Texture3D::LINEAR);
368     noiseTexture->setFilter(osg::Texture3D::MAG_FILTER, osg::Texture3D::LINEAR);
369     noiseTexture->setWrap(osg::Texture3D::WRAP_S, osg::Texture3D::REPEAT);
370     noiseTexture->setWrap(osg::Texture3D::WRAP_T, osg::Texture3D::REPEAT);
371     noiseTexture->setWrap(osg::Texture3D::WRAP_R, osg::Texture3D::REPEAT);
372     noiseTexture->setImage( make3DNoiseImage(texSize) );
373     _noises.insert(make_pair(texSize, noiseTexture));
374     return noiseTexture;
375 }
376
377 namespace
378 {
379 TextureBuilder::Registrar installNoise("noise", new NoiseBuilder);
380 }
381
382 EffectNameValue<TexEnvCombine::CombineParam> combineParamInit[] =
383 {
384     {"replace", TexEnvCombine::REPLACE},
385     {"modulate", TexEnvCombine::MODULATE},
386     {"add", TexEnvCombine::ADD},
387     {"add-signed", TexEnvCombine::ADD_SIGNED},
388     {"interpolate", TexEnvCombine::INTERPOLATE},
389     {"subtract", TexEnvCombine::SUBTRACT},
390     {"dot3-rgb", TexEnvCombine::DOT3_RGB},
391     {"dot3-rgba", TexEnvCombine::DOT3_RGBA}
392 };
393
394 EffectPropertyMap<TexEnvCombine::CombineParam> combineParams(combineParamInit);
395
396 EffectNameValue<TexEnvCombine::SourceParam> sourceParamInit[] =
397 {
398     {"constant", TexEnvCombine::CONSTANT},
399     {"primary_color", TexEnvCombine::PRIMARY_COLOR},
400     {"previous", TexEnvCombine::PREVIOUS},
401     {"texture", TexEnvCombine::TEXTURE},
402     {"texture0", TexEnvCombine::TEXTURE0},
403     {"texture1", TexEnvCombine::TEXTURE1},
404     {"texture2", TexEnvCombine::TEXTURE2},
405     {"texture3", TexEnvCombine::TEXTURE3},
406     {"texture4", TexEnvCombine::TEXTURE4},
407     {"texture5", TexEnvCombine::TEXTURE5},
408     {"texture6", TexEnvCombine::TEXTURE6},
409     {"texture7", TexEnvCombine::TEXTURE7}
410 };
411
412 EffectPropertyMap<TexEnvCombine::SourceParam> sourceParams(sourceParamInit);
413
414 EffectNameValue<TexEnvCombine::OperandParam> opParamInit[] =
415 {
416     {"src_color", TexEnvCombine::SRC_COLOR},
417     {"one_minus_src_color", TexEnvCombine::ONE_MINUS_SRC_COLOR},
418     {"src_alpha", TexEnvCombine::SRC_ALPHA},
419     {"one_minus_src_alpha", TexEnvCombine::ONE_MINUS_SRC_ALPHA}
420 };
421
422 EffectPropertyMap<TexEnvCombine::OperandParam> operandParams(opParamInit);
423
424 TexEnvCombine* buildTexEnvCombine(Effect* effect, const SGPropertyNode* envProp)
425 {
426     if (!isAttributeActive(effect, envProp))
427         return 0;
428     TexEnvCombine* result = new TexEnvCombine;
429     const SGPropertyNode* p = 0;
430     if ((p = getEffectPropertyChild(effect, envProp, "combine-rgb"))) {
431         TexEnvCombine::CombineParam crgb = TexEnvCombine::MODULATE;
432         findAttr(combineParams, p, crgb);
433         result->setCombine_RGB(crgb);
434     }
435     if ((p = getEffectPropertyChild(effect, envProp, "combine-alpha"))) {
436         TexEnvCombine::CombineParam calpha = TexEnvCombine::MODULATE;
437         findAttr(combineParams, p, calpha);
438         result->setCombine_RGB(calpha);
439     }
440     if ((p = getEffectPropertyChild(effect, envProp, "source0-rgb"))) {
441         TexEnvCombine::SourceParam source = TexEnvCombine::TEXTURE;
442         findAttr(sourceParams, p, source);
443         result->setSource0_RGB(source);
444     }
445     if ((p = getEffectPropertyChild(effect, envProp, "source1-rgb"))) {
446         TexEnvCombine::SourceParam source = TexEnvCombine::PREVIOUS;
447         findAttr(sourceParams, p, source);
448         result->setSource1_RGB(source);
449     }
450     if ((p = getEffectPropertyChild(effect, envProp, "source2-rgb"))) {
451         TexEnvCombine::SourceParam source = TexEnvCombine::CONSTANT;
452         findAttr(sourceParams, p, source);
453         result->setSource2_RGB(source);
454     }
455     if ((p = getEffectPropertyChild(effect, envProp, "source0-alpha"))) {
456         TexEnvCombine::SourceParam source = TexEnvCombine::TEXTURE;
457         findAttr(sourceParams, p, source);
458         result->setSource0_Alpha(source);
459     }
460     if ((p = getEffectPropertyChild(effect, envProp, "source1-alpha"))) {
461         TexEnvCombine::SourceParam source = TexEnvCombine::PREVIOUS;
462         findAttr(sourceParams, p, source);
463         result->setSource1_Alpha(source);
464     }
465     if ((p = getEffectPropertyChild(effect, envProp, "source2-alpha"))) {
466         TexEnvCombine::SourceParam source = TexEnvCombine::CONSTANT;
467         findAttr(sourceParams, p, source);
468         result->setSource2_Alpha(source);
469     }
470     if ((p = getEffectPropertyChild(effect, envProp, "operand0-rgb"))) {
471         TexEnvCombine::OperandParam op = TexEnvCombine::SRC_COLOR;
472         findAttr(operandParams, p, op);
473         result->setOperand0_RGB(op);
474     }
475     if ((p = getEffectPropertyChild(effect, envProp, "operand1-rgb"))) {
476         TexEnvCombine::OperandParam op = TexEnvCombine::SRC_COLOR;
477         findAttr(operandParams, p, op);
478         result->setOperand1_RGB(op);
479     }
480     if ((p = getEffectPropertyChild(effect, envProp, "operand2-rgb"))) {
481         TexEnvCombine::OperandParam op = TexEnvCombine::SRC_ALPHA;
482         findAttr(operandParams, p, op);
483         result->setOperand2_RGB(op);
484     }
485     if ((p = getEffectPropertyChild(effect, envProp, "operand0-alpha"))) {
486         TexEnvCombine::OperandParam op = TexEnvCombine::SRC_ALPHA;
487         findAttr(operandParams, p, op);
488         result->setOperand0_Alpha(op);
489     }
490     if ((p = getEffectPropertyChild(effect, envProp, "operand1-alpha"))) {
491         TexEnvCombine::OperandParam op = TexEnvCombine::SRC_ALPHA;
492         findAttr(operandParams, p, op);
493         result->setOperand1_Alpha(op);
494     }
495     if ((p = getEffectPropertyChild(effect, envProp, "operand2-alpha"))) {
496         TexEnvCombine::OperandParam op = TexEnvCombine::SRC_ALPHA;
497         findAttr(operandParams, p, op);
498         result->setOperand2_Alpha(op);
499     }
500     if ((p = getEffectPropertyChild(effect, envProp, "scale-rgb"))) {
501         result->setScale_RGB(p->getValue<float>());
502     }
503     if ((p = getEffectPropertyChild(effect, envProp, "scale-alpha"))) {
504         result->setScale_Alpha(p->getValue<float>());
505     }
506 #if 0
507     if ((p = getEffectPropertyChild(effect, envProp, "constant-color"))) {
508         SGVec4d color = p->getValue<SGVec4d>();
509         result->setConstantColor(toOsg(color));
510     } else if ((p = getEffectPropertyChild(effect, envProp,
511                                            "light-direction"))) {
512         SGVec3d direction = p->getValue<SGVec3d>();
513         result->setConstantColorAsLightDirection(toOsg(direction));
514     }
515 #endif
516     const SGPropertyNode* colorNode = envProp->getChild("constant-color");
517     if (colorNode)
518         initFromParameters(effect, colorNode, result,
519                            &TexEnvCombine::setConstantColor, colorFields);
520     return result;
521 }
522
523 EffectNameValue<TexGen::Mode> tgenModeInit[] =
524 {
525     { "object-linear", TexGen::OBJECT_LINEAR},
526     { "eye-linear", TexGen::EYE_LINEAR},
527     { "sphere-map", TexGen::SPHERE_MAP},
528     { "normal-map", TexGen::NORMAL_MAP},
529     { "reflection-map", TexGen::REFLECTION_MAP}
530 };
531
532 EffectPropertyMap<TexGen::Mode> tgenModes(tgenModeInit);
533
534 EffectNameValue<TexGen::Coord> tgenCoordInit[] =
535 {
536     {"s", TexGen::S},
537     {"t", TexGen::T},
538     {"r", TexGen::R},
539     {"q", TexGen::Q}
540 };
541
542 EffectPropertyMap<TexGen::Coord> tgenCoords(tgenCoordInit);
543
544 TexGen* buildTexGen(Effect* effect, const SGPropertyNode* tgenProp)
545 {
546     if (!isAttributeActive(effect, tgenProp))
547         return 0;
548     TexGen* result = new TexGen;
549     const SGPropertyNode* p = 0;
550     TexGen::Mode mode = TexGen::OBJECT_LINEAR;
551     if (findAttr(tgenModes, getEffectPropertyChild(effect, tgenProp, "mode"),
552                  mode))
553         result->setMode(mode);
554     const SGPropertyNode* planesNode = tgenProp->getChild("planes");
555     if (planesNode) {
556         for (int i = 0; i < planesNode->nChildren(); ++i) {
557             const SGPropertyNode* planeNode = planesNode->getChild(i);
558             TexGen::Coord coord;
559             if (!findAttr(tgenCoords, planeNode->getName(), coord)) {
560                 SG_LOG(SG_INPUT, SG_ALERT, "Unknown TexGen plane "
561                        << planeNode->getName());
562             } else {
563                 const SGPropertyNode* realNode
564                     = getEffectPropertyNode(effect, planeNode);
565                 SGVec4d plane = realNode->getValue<SGVec4d>();
566                 result->setPlane(coord, toOsg(plane));
567             }
568         }
569     }
570     return result;
571 }
572
573 bool makeTextureParameters(SGPropertyNode* paramRoot, const StateSet* ss)
574 {
575     SGPropertyNode* texUnit = makeChild(paramRoot, "texture");
576     const Texture* tex = getStateAttribute<Texture>(0, ss);
577     const Texture2D* texture = dynamic_cast<const Texture2D*>(tex);
578     makeChild(texUnit, "unit")->setValue(0);
579     if (!tex) {
580         makeChild(texUnit, "active")->setValue(false);
581         makeChild(texUnit, "type")->setValue("white");
582         return false;
583     }
584     const Image* image = texture->getImage();
585     string imageName;
586     if (image) {
587         imageName = image->getFileName();
588     } else {
589         makeChild(texUnit, "active")->setValue(false);
590         makeChild(texUnit, "type")->setValue("white");
591         return false;
592     }
593     makeChild(texUnit, "active")->setValue(true);
594     makeChild(texUnit, "type")->setValue("2d");
595     string wrapS = findName(wrapModes, texture->getWrap(Texture::WRAP_S));
596     string wrapT = findName(wrapModes, texture->getWrap(Texture::WRAP_T));
597     string wrapR = findName(wrapModes, texture->getWrap(Texture::WRAP_R));
598     makeChild(texUnit, "image")->setStringValue(imageName);
599     makeChild(texUnit, "wrap-s")->setStringValue(wrapS);
600     makeChild(texUnit, "wrap-t")->setStringValue(wrapT);
601     makeChild(texUnit, "wrap-r")->setStringValue(wrapR);
602     return true;
603 }
604
605 }