]> git.mxchange.org Git - simgear.git/blob - simgear/scene/material/TextureBuilder.cxx
c1f353ce3fe832da153af59782f5f3faf6cc7635
[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 class TransparentTextureBuilder : public TextureBuilder
301 {
302 public:
303     Texture* build(Effect* effect, const SGPropertyNode*,
304                    const osgDB::ReaderWriter::Options* options);
305 };
306
307 Texture* TransparentTextureBuilder::build(Effect* effect, const SGPropertyNode*,
308                                     const osgDB::ReaderWriter::Options* options)
309 {
310     return StateAttributeFactory::instance()->getTransparentTexture();
311 }
312
313 namespace
314 {
315 TextureBuilder::Registrar installTransparent("transparent",
316                                              new TransparentTextureBuilder);
317 }
318
319 osg::Image* make3DNoiseImage(int texSize)
320 {
321     osg::Image* image = new osg::Image;
322     image->setImage(texSize, texSize, texSize,
323                     4, GL_RGBA, GL_UNSIGNED_BYTE,
324                     new unsigned char[4 * texSize * texSize * texSize],
325                     osg::Image::USE_NEW_DELETE);
326
327     const int startFrequency = 4;
328     const int numOctaves = 4;
329
330     int f, i, j, k, inc;
331     double ni[3];
332     double inci, incj, inck;
333     int frequency = startFrequency;
334     GLubyte *ptr;
335     double amp = 0.5;
336
337     osg::notify(osg::WARN) << "creating 3D noise texture... ";
338
339     for (f = 0, inc = 0; f < numOctaves; ++f, frequency *= 2, ++inc, amp *= 0.5)
340     {
341         SetNoiseFrequency(frequency);
342         ptr = image->data();
343         ni[0] = ni[1] = ni[2] = 0;
344
345         inci = 1.0 / (texSize / frequency);
346         for (i = 0; i < texSize; ++i, ni[0] += inci)
347         {
348             incj = 1.0 / (texSize / frequency);
349             for (j = 0; j < texSize; ++j, ni[1] += incj)
350             {
351                 inck = 1.0 / (texSize / frequency);
352                 for (k = 0; k < texSize; ++k, ni[2] += inck, ptr += 4)
353                 {
354                     *(ptr+inc) = (GLubyte) (((noise3(ni) + 1.0) * amp) * 128.0);
355                 }
356             }
357         }
358     }
359
360     osg::notify(osg::WARN) << "DONE" << std::endl;
361     return image;
362 }
363
364 class NoiseBuilder : public TextureBuilder
365 {
366 public:
367     Texture* build(Effect* effect, const SGPropertyNode*,
368                    const osgDB::ReaderWriter::Options* options);
369 protected:
370     typedef map<int, ref_ptr<Texture3D> > NoiseMap;
371     NoiseMap _noises;
372 };
373
374 Texture* NoiseBuilder::build(Effect* effect, const SGPropertyNode* props,
375                              const osgDB::ReaderWriter::Options* options)
376 {
377     int texSize = 64;
378     const SGPropertyNode* sizeProp = getEffectPropertyChild(effect, props,
379                                                             "size");
380     if (sizeProp)
381         texSize = sizeProp->getValue<int>();
382     NoiseMap::iterator itr = _noises.find(texSize);
383     if (itr != _noises.end())
384         return itr->second.get();
385     Texture3D* noiseTexture = new osg::Texture3D;
386     noiseTexture->setFilter(osg::Texture3D::MIN_FILTER, osg::Texture3D::LINEAR);
387     noiseTexture->setFilter(osg::Texture3D::MAG_FILTER, osg::Texture3D::LINEAR);
388     noiseTexture->setWrap(osg::Texture3D::WRAP_S, osg::Texture3D::REPEAT);
389     noiseTexture->setWrap(osg::Texture3D::WRAP_T, osg::Texture3D::REPEAT);
390     noiseTexture->setWrap(osg::Texture3D::WRAP_R, osg::Texture3D::REPEAT);
391     noiseTexture->setImage( make3DNoiseImage(texSize) );
392     _noises.insert(make_pair(texSize, noiseTexture));
393     return noiseTexture;
394 }
395
396 namespace
397 {
398 TextureBuilder::Registrar installNoise("noise", new NoiseBuilder);
399 }
400
401 EffectNameValue<TexEnvCombine::CombineParam> combineParamInit[] =
402 {
403     {"replace", TexEnvCombine::REPLACE},
404     {"modulate", TexEnvCombine::MODULATE},
405     {"add", TexEnvCombine::ADD},
406     {"add-signed", TexEnvCombine::ADD_SIGNED},
407     {"interpolate", TexEnvCombine::INTERPOLATE},
408     {"subtract", TexEnvCombine::SUBTRACT},
409     {"dot3-rgb", TexEnvCombine::DOT3_RGB},
410     {"dot3-rgba", TexEnvCombine::DOT3_RGBA}
411 };
412
413 EffectPropertyMap<TexEnvCombine::CombineParam> combineParams(combineParamInit);
414
415 EffectNameValue<TexEnvCombine::SourceParam> sourceParamInit[] =
416 {
417     {"constant", TexEnvCombine::CONSTANT},
418     {"primary_color", TexEnvCombine::PRIMARY_COLOR},
419     {"previous", TexEnvCombine::PREVIOUS},
420     {"texture", TexEnvCombine::TEXTURE},
421     {"texture0", TexEnvCombine::TEXTURE0},
422     {"texture1", TexEnvCombine::TEXTURE1},
423     {"texture2", TexEnvCombine::TEXTURE2},
424     {"texture3", TexEnvCombine::TEXTURE3},
425     {"texture4", TexEnvCombine::TEXTURE4},
426     {"texture5", TexEnvCombine::TEXTURE5},
427     {"texture6", TexEnvCombine::TEXTURE6},
428     {"texture7", TexEnvCombine::TEXTURE7}
429 };
430
431 EffectPropertyMap<TexEnvCombine::SourceParam> sourceParams(sourceParamInit);
432
433 EffectNameValue<TexEnvCombine::OperandParam> opParamInit[] =
434 {
435     {"src_color", TexEnvCombine::SRC_COLOR},
436     {"one_minus_src_color", TexEnvCombine::ONE_MINUS_SRC_COLOR},
437     {"src_alpha", TexEnvCombine::SRC_ALPHA},
438     {"one_minus_src_alpha", TexEnvCombine::ONE_MINUS_SRC_ALPHA}
439 };
440
441 EffectPropertyMap<TexEnvCombine::OperandParam> operandParams(opParamInit);
442
443 TexEnvCombine* buildTexEnvCombine(Effect* effect, const SGPropertyNode* envProp)
444 {
445     if (!isAttributeActive(effect, envProp))
446         return 0;
447     TexEnvCombine* result = new TexEnvCombine;
448     const SGPropertyNode* p = 0;
449     if ((p = getEffectPropertyChild(effect, envProp, "combine-rgb"))) {
450         TexEnvCombine::CombineParam crgb = TexEnvCombine::MODULATE;
451         findAttr(combineParams, p, crgb);
452         result->setCombine_RGB(crgb);
453     }
454     if ((p = getEffectPropertyChild(effect, envProp, "combine-alpha"))) {
455         TexEnvCombine::CombineParam calpha = TexEnvCombine::MODULATE;
456         findAttr(combineParams, p, calpha);
457         result->setCombine_Alpha(calpha);
458     }
459     if ((p = getEffectPropertyChild(effect, envProp, "source0-rgb"))) {
460         TexEnvCombine::SourceParam source = TexEnvCombine::TEXTURE;
461         findAttr(sourceParams, p, source);
462         result->setSource0_RGB(source);
463     }
464     if ((p = getEffectPropertyChild(effect, envProp, "source1-rgb"))) {
465         TexEnvCombine::SourceParam source = TexEnvCombine::PREVIOUS;
466         findAttr(sourceParams, p, source);
467         result->setSource1_RGB(source);
468     }
469     if ((p = getEffectPropertyChild(effect, envProp, "source2-rgb"))) {
470         TexEnvCombine::SourceParam source = TexEnvCombine::CONSTANT;
471         findAttr(sourceParams, p, source);
472         result->setSource2_RGB(source);
473     }
474     if ((p = getEffectPropertyChild(effect, envProp, "source0-alpha"))) {
475         TexEnvCombine::SourceParam source = TexEnvCombine::TEXTURE;
476         findAttr(sourceParams, p, source);
477         result->setSource0_Alpha(source);
478     }
479     if ((p = getEffectPropertyChild(effect, envProp, "source1-alpha"))) {
480         TexEnvCombine::SourceParam source = TexEnvCombine::PREVIOUS;
481         findAttr(sourceParams, p, source);
482         result->setSource1_Alpha(source);
483     }
484     if ((p = getEffectPropertyChild(effect, envProp, "source2-alpha"))) {
485         TexEnvCombine::SourceParam source = TexEnvCombine::CONSTANT;
486         findAttr(sourceParams, p, source);
487         result->setSource2_Alpha(source);
488     }
489     if ((p = getEffectPropertyChild(effect, envProp, "operand0-rgb"))) {
490         TexEnvCombine::OperandParam op = TexEnvCombine::SRC_COLOR;
491         findAttr(operandParams, p, op);
492         result->setOperand0_RGB(op);
493     }
494     if ((p = getEffectPropertyChild(effect, envProp, "operand1-rgb"))) {
495         TexEnvCombine::OperandParam op = TexEnvCombine::SRC_COLOR;
496         findAttr(operandParams, p, op);
497         result->setOperand1_RGB(op);
498     }
499     if ((p = getEffectPropertyChild(effect, envProp, "operand2-rgb"))) {
500         TexEnvCombine::OperandParam op = TexEnvCombine::SRC_ALPHA;
501         findAttr(operandParams, p, op);
502         result->setOperand2_RGB(op);
503     }
504     if ((p = getEffectPropertyChild(effect, envProp, "operand0-alpha"))) {
505         TexEnvCombine::OperandParam op = TexEnvCombine::SRC_ALPHA;
506         findAttr(operandParams, p, op);
507         result->setOperand0_Alpha(op);
508     }
509     if ((p = getEffectPropertyChild(effect, envProp, "operand1-alpha"))) {
510         TexEnvCombine::OperandParam op = TexEnvCombine::SRC_ALPHA;
511         findAttr(operandParams, p, op);
512         result->setOperand1_Alpha(op);
513     }
514     if ((p = getEffectPropertyChild(effect, envProp, "operand2-alpha"))) {
515         TexEnvCombine::OperandParam op = TexEnvCombine::SRC_ALPHA;
516         findAttr(operandParams, p, op);
517         result->setOperand2_Alpha(op);
518     }
519     if ((p = getEffectPropertyChild(effect, envProp, "scale-rgb"))) {
520         result->setScale_RGB(p->getValue<float>());
521     }
522     if ((p = getEffectPropertyChild(effect, envProp, "scale-alpha"))) {
523         result->setScale_Alpha(p->getValue<float>());
524     }
525 #if 0
526     if ((p = getEffectPropertyChild(effect, envProp, "constant-color"))) {
527         SGVec4d color = p->getValue<SGVec4d>();
528         result->setConstantColor(toOsg(color));
529     } else if ((p = getEffectPropertyChild(effect, envProp,
530                                            "light-direction"))) {
531         SGVec3d direction = p->getValue<SGVec3d>();
532         result->setConstantColorAsLightDirection(toOsg(direction));
533     }
534 #endif
535     const SGPropertyNode* colorNode = envProp->getChild("constant-color");
536     if (colorNode)
537         initFromParameters(effect, colorNode, result,
538                            &TexEnvCombine::setConstantColor, colorFields);
539     return result;
540 }
541
542 EffectNameValue<TexGen::Mode> tgenModeInit[] =
543 {
544     { "object-linear", TexGen::OBJECT_LINEAR},
545     { "eye-linear", TexGen::EYE_LINEAR},
546     { "sphere-map", TexGen::SPHERE_MAP},
547     { "normal-map", TexGen::NORMAL_MAP},
548     { "reflection-map", TexGen::REFLECTION_MAP}
549 };
550
551 EffectPropertyMap<TexGen::Mode> tgenModes(tgenModeInit);
552
553 EffectNameValue<TexGen::Coord> tgenCoordInit[] =
554 {
555     {"s", TexGen::S},
556     {"t", TexGen::T},
557     {"r", TexGen::R},
558     {"q", TexGen::Q}
559 };
560
561 EffectPropertyMap<TexGen::Coord> tgenCoords(tgenCoordInit);
562
563 TexGen* buildTexGen(Effect* effect, const SGPropertyNode* tgenProp)
564 {
565     if (!isAttributeActive(effect, tgenProp))
566         return 0;
567     TexGen* result = new TexGen;
568     const SGPropertyNode* p = 0;
569     TexGen::Mode mode = TexGen::OBJECT_LINEAR;
570     if (findAttr(tgenModes, getEffectPropertyChild(effect, tgenProp, "mode"),
571                  mode))
572         result->setMode(mode);
573     const SGPropertyNode* planesNode = tgenProp->getChild("planes");
574     if (planesNode) {
575         for (int i = 0; i < planesNode->nChildren(); ++i) {
576             const SGPropertyNode* planeNode = planesNode->getChild(i);
577             TexGen::Coord coord;
578             if (!findAttr(tgenCoords, planeNode->getName(), coord)) {
579                 SG_LOG(SG_INPUT, SG_ALERT, "Unknown TexGen plane "
580                        << planeNode->getName());
581             } else {
582                 const SGPropertyNode* realNode
583                     = getEffectPropertyNode(effect, planeNode);
584                 SGVec4d plane = realNode->getValue<SGVec4d>();
585                 result->setPlane(coord, toOsg(plane));
586             }
587         }
588     }
589     return result;
590 }
591
592 bool makeTextureParameters(SGPropertyNode* paramRoot, const StateSet* ss)
593 {
594     SGPropertyNode* texUnit = makeChild(paramRoot, "texture");
595     const Texture* tex = getStateAttribute<Texture>(0, ss);
596     const Texture2D* texture = dynamic_cast<const Texture2D*>(tex);
597     makeChild(texUnit, "unit")->setValue(0);
598     if (!tex) {
599         // The default shader-based technique ignores active
600         makeChild(texUnit, "active")->setValue(false);
601         return false;
602     }
603     const Image* image = texture->getImage();
604     string imageName;
605     if (image) {
606         imageName = image->getFileName();
607     } else {
608         makeChild(texUnit, "active")->setValue(false);
609         makeChild(texUnit, "type")->setValue("white");
610         return false;
611     }
612     makeChild(texUnit, "active")->setValue(true);
613     makeChild(texUnit, "type")->setValue("2d");
614     string wrapS = findName(wrapModes, texture->getWrap(Texture::WRAP_S));
615     string wrapT = findName(wrapModes, texture->getWrap(Texture::WRAP_T));
616     string wrapR = findName(wrapModes, texture->getWrap(Texture::WRAP_R));
617     makeChild(texUnit, "image")->setStringValue(imageName);
618     makeChild(texUnit, "wrap-s")->setStringValue(wrapS);
619     makeChild(texUnit, "wrap-t")->setStringValue(wrapT);
620     makeChild(texUnit, "wrap-r")->setStringValue(wrapR);
621     return true;
622 }
623
624 }