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