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