]> git.mxchange.org Git - simgear.git/blob - simgear/scene/material/TextureBuilder.cxx
Merge branch 'timoore/effects'
[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     const SGPropertyNode* ep = 0;
185     if ((ep = getEffectPropertyChild(effect, props, "filter")))
186         findAttr(filterModes, ep, minFilter);
187     Texture::FilterMode magFilter = Texture::LINEAR;
188     if ((ep = getEffectPropertyChild(effect, props, "mag-filter")))
189         findAttr(filterModes, ep, magFilter);
190     const SGPropertyNode* pWrapS
191         = getEffectPropertyChild(effect, props, "wrap-s");
192     Texture::WrapMode sWrap = Texture::CLAMP;
193     if (pWrapS)
194         findAttr(wrapModes, pWrapS, sWrap);
195     const SGPropertyNode* pWrapT
196         = getEffectPropertyChild(effect, props, "wrap-t");
197     Texture::WrapMode tWrap = Texture::CLAMP;
198     if (pWrapT)
199         findAttr(wrapModes, pWrapT, tWrap);
200     const SGPropertyNode* pWrapR
201         = getEffectPropertyChild(effect, props, "wrap-r");
202     Texture::WrapMode rWrap = Texture::CLAMP;
203     if (pWrapR)
204         findAttr(wrapModes, pWrapR, rWrap);
205     const SGPropertyNode* pImage
206         = getEffectPropertyChild(effect, props, "image");
207     string imageName;
208     if (pImage)
209         imageName = pImage->getStringValue();
210     string absFileName = osgDB::findDataFile(imageName, options);
211     return TexTuple(absFileName, minFilter, magFilter, sWrap, tWrap, rWrap,
212                     texType);
213 }
214
215 void setAttrs(const TexTuple& attrs, Texture* tex,
216               const osgDB::ReaderWriter::Options* options)
217 {
218     const string& imageName = attrs.get<0>();
219     if (imageName.empty()) {
220         throw BuilderException("no image file");
221     } else {
222         osgDB::ReaderWriter::ReadResult result
223             = osgDB::Registry::instance()->readImage(imageName, options);
224         if (result.success()) {
225             osg::Image* image = result.getImage();
226             tex->setImage(GL_FRONT_AND_BACK, image);
227             int s = image->s();
228             int t = image->t();
229             if (s <= t && 32 <= s) {
230                 SGSceneFeatures::instance()->setTextureCompression(tex);
231             } else if (t < s && 32 <= t) {
232                 SGSceneFeatures::instance()->setTextureCompression(tex);
233             }
234             tex->setMaxAnisotropy(SGSceneFeatures::instance()
235                                   ->getTextureFilter());
236         } else {
237             SG_LOG(SG_INPUT, SG_ALERT, "failed to load effect texture file "
238                    << imageName);
239         }
240     }
241     // texture->setDataVariance(osg::Object::STATIC);
242     tex->setFilter(Texture::MIN_FILTER, attrs.get<1>());
243     tex->setFilter(Texture::MAG_FILTER, attrs.get<2>());
244     tex->setWrap(Texture::WRAP_S, attrs.get<3>());
245     tex->setWrap(Texture::WRAP_T, attrs.get<4>());
246     tex->setWrap(Texture::WRAP_R, attrs.get<5>());
247 }
248 }
249
250 template<typename T>
251 class TexBuilder : public TextureBuilder
252 {
253 public:
254     TexBuilder(const string& texType) : _type(texType) {}
255     Texture* build(Effect* effect, const SGPropertyNode*,
256                    const osgDB::ReaderWriter::Options* options);
257 protected:
258     typedef map<TexTuple, ref_ptr<T> > TexMap;
259     TexMap texMap;
260     const string _type;
261 };
262
263 template<typename T>
264 Texture* TexBuilder<T>::build(Effect* effect, const SGPropertyNode* props,
265                               const osgDB::ReaderWriter::Options* options)
266 {
267     TexTuple attrs = makeTexTuple(effect, props, options, _type);
268     typename TexMap::iterator itr = texMap.find(attrs);
269     if (itr != texMap.end())
270         return itr->second.get();
271     T* tex = new T;
272     setAttrs(attrs, tex, options);
273     texMap.insert(make_pair(attrs, tex));
274     return tex;
275 }
276
277
278 namespace
279 {
280 TextureBuilder::Registrar install1D("1d", new TexBuilder<Texture1D>("1d"));
281 TextureBuilder::Registrar install2D("2d", new TexBuilder<Texture2D>("2d"));
282 TextureBuilder::Registrar install3D("3d", new TexBuilder<Texture3D>("3d"));
283 }
284
285 class WhiteTextureBuilder : public TextureBuilder
286 {
287 public:
288     Texture* build(Effect* effect, const SGPropertyNode*,
289                    const osgDB::ReaderWriter::Options* options);
290 };
291
292 Texture* WhiteTextureBuilder::build(Effect* effect, const SGPropertyNode*,
293                                     const osgDB::ReaderWriter::Options* options)
294 {
295     return StateAttributeFactory::instance()->getWhiteTexture();
296 }
297
298 namespace
299 {
300 TextureBuilder::Registrar installWhite("white", new WhiteTextureBuilder);
301 }
302
303 class TransparentTextureBuilder : public TextureBuilder
304 {
305 public:
306     Texture* build(Effect* effect, const SGPropertyNode*,
307                    const osgDB::ReaderWriter::Options* options);
308 };
309
310 Texture* TransparentTextureBuilder::build(Effect* effect, const SGPropertyNode*,
311                                     const osgDB::ReaderWriter::Options* options)
312 {
313     return StateAttributeFactory::instance()->getTransparentTexture();
314 }
315
316 namespace
317 {
318 TextureBuilder::Registrar installTransparent("transparent",
319                                              new TransparentTextureBuilder);
320 }
321
322 osg::Image* make3DNoiseImage(int texSize)
323 {
324     osg::Image* image = new osg::Image;
325     image->setImage(texSize, texSize, texSize,
326                     4, GL_RGBA, GL_UNSIGNED_BYTE,
327                     new unsigned char[4 * texSize * texSize * texSize],
328                     osg::Image::USE_NEW_DELETE);
329
330     const int startFrequency = 4;
331     const int numOctaves = 4;
332
333     int f, i, j, k, inc;
334     double ni[3];
335     double inci, incj, inck;
336     int frequency = startFrequency;
337     GLubyte *ptr;
338     double amp = 0.5;
339
340     osg::notify(osg::WARN) << "creating 3D noise texture... ";
341
342     for (f = 0, inc = 0; f < numOctaves; ++f, frequency *= 2, ++inc, amp *= 0.5)
343     {
344         SetNoiseFrequency(frequency);
345         ptr = image->data();
346         ni[0] = ni[1] = ni[2] = 0;
347
348         inci = 1.0 / (texSize / frequency);
349         for (i = 0; i < texSize; ++i, ni[0] += inci)
350         {
351             incj = 1.0 / (texSize / frequency);
352             for (j = 0; j < texSize; ++j, ni[1] += incj)
353             {
354                 inck = 1.0 / (texSize / frequency);
355                 for (k = 0; k < texSize; ++k, ni[2] += inck, ptr += 4)
356                 {
357                     *(ptr+inc) = (GLubyte) (((noise3(ni) + 1.0) * amp) * 128.0);
358                 }
359             }
360         }
361     }
362
363     osg::notify(osg::WARN) << "DONE" << std::endl;
364     return image;
365 }
366
367 class NoiseBuilder : public TextureBuilder
368 {
369 public:
370     Texture* build(Effect* effect, const SGPropertyNode*,
371                    const osgDB::ReaderWriter::Options* options);
372 protected:
373     typedef map<int, ref_ptr<Texture3D> > NoiseMap;
374     NoiseMap _noises;
375 };
376
377 Texture* NoiseBuilder::build(Effect* effect, const SGPropertyNode* props,
378                              const osgDB::ReaderWriter::Options* options)
379 {
380     int texSize = 64;
381     const SGPropertyNode* sizeProp = getEffectPropertyChild(effect, props,
382                                                             "size");
383     if (sizeProp)
384         texSize = sizeProp->getValue<int>();
385     NoiseMap::iterator itr = _noises.find(texSize);
386     if (itr != _noises.end())
387         return itr->second.get();
388     Texture3D* noiseTexture = new osg::Texture3D;
389     noiseTexture->setFilter(osg::Texture3D::MIN_FILTER, osg::Texture3D::LINEAR);
390     noiseTexture->setFilter(osg::Texture3D::MAG_FILTER, osg::Texture3D::LINEAR);
391     noiseTexture->setWrap(osg::Texture3D::WRAP_S, osg::Texture3D::REPEAT);
392     noiseTexture->setWrap(osg::Texture3D::WRAP_T, osg::Texture3D::REPEAT);
393     noiseTexture->setWrap(osg::Texture3D::WRAP_R, osg::Texture3D::REPEAT);
394     noiseTexture->setImage( make3DNoiseImage(texSize) );
395     _noises.insert(make_pair(texSize, noiseTexture));
396     return noiseTexture;
397 }
398
399 namespace
400 {
401 TextureBuilder::Registrar installNoise("noise", new NoiseBuilder);
402 }
403
404 EffectNameValue<TexEnvCombine::CombineParam> combineParamInit[] =
405 {
406     {"replace", TexEnvCombine::REPLACE},
407     {"modulate", TexEnvCombine::MODULATE},
408     {"add", TexEnvCombine::ADD},
409     {"add-signed", TexEnvCombine::ADD_SIGNED},
410     {"interpolate", TexEnvCombine::INTERPOLATE},
411     {"subtract", TexEnvCombine::SUBTRACT},
412     {"dot3-rgb", TexEnvCombine::DOT3_RGB},
413     {"dot3-rgba", TexEnvCombine::DOT3_RGBA}
414 };
415
416 EffectPropertyMap<TexEnvCombine::CombineParam> combineParams(combineParamInit);
417
418 EffectNameValue<TexEnvCombine::SourceParam> sourceParamInit[] =
419 {
420     {"constant", TexEnvCombine::CONSTANT},
421     {"primary_color", TexEnvCombine::PRIMARY_COLOR},
422     {"previous", TexEnvCombine::PREVIOUS},
423     {"texture", TexEnvCombine::TEXTURE},
424     {"texture0", TexEnvCombine::TEXTURE0},
425     {"texture1", TexEnvCombine::TEXTURE1},
426     {"texture2", TexEnvCombine::TEXTURE2},
427     {"texture3", TexEnvCombine::TEXTURE3},
428     {"texture4", TexEnvCombine::TEXTURE4},
429     {"texture5", TexEnvCombine::TEXTURE5},
430     {"texture6", TexEnvCombine::TEXTURE6},
431     {"texture7", TexEnvCombine::TEXTURE7}
432 };
433
434 EffectPropertyMap<TexEnvCombine::SourceParam> sourceParams(sourceParamInit);
435
436 EffectNameValue<TexEnvCombine::OperandParam> opParamInit[] =
437 {
438     {"src-color", TexEnvCombine::SRC_COLOR},
439     {"one-minus-src-color", TexEnvCombine::ONE_MINUS_SRC_COLOR},
440     {"src-alpha", TexEnvCombine::SRC_ALPHA},
441     {"one-minus-src-alpha", TexEnvCombine::ONE_MINUS_SRC_ALPHA}
442 };
443
444 EffectPropertyMap<TexEnvCombine::OperandParam> operandParams(opParamInit);
445
446 TexEnvCombine* buildTexEnvCombine(Effect* effect, const SGPropertyNode* envProp)
447 {
448     if (!isAttributeActive(effect, envProp))
449         return 0;
450     TexEnvCombine* result = new TexEnvCombine;
451     const SGPropertyNode* p = 0;
452     if ((p = getEffectPropertyChild(effect, envProp, "combine-rgb"))) {
453         TexEnvCombine::CombineParam crgb = TexEnvCombine::MODULATE;
454         findAttr(combineParams, p, crgb);
455         result->setCombine_RGB(crgb);
456     }
457     if ((p = getEffectPropertyChild(effect, envProp, "combine-alpha"))) {
458         TexEnvCombine::CombineParam calpha = TexEnvCombine::MODULATE;
459         findAttr(combineParams, p, calpha);
460         result->setCombine_Alpha(calpha);
461     }
462     if ((p = getEffectPropertyChild(effect, envProp, "source0-rgb"))) {
463         TexEnvCombine::SourceParam source = TexEnvCombine::TEXTURE;
464         findAttr(sourceParams, p, source);
465         result->setSource0_RGB(source);
466     }
467     if ((p = getEffectPropertyChild(effect, envProp, "source1-rgb"))) {
468         TexEnvCombine::SourceParam source = TexEnvCombine::PREVIOUS;
469         findAttr(sourceParams, p, source);
470         result->setSource1_RGB(source);
471     }
472     if ((p = getEffectPropertyChild(effect, envProp, "source2-rgb"))) {
473         TexEnvCombine::SourceParam source = TexEnvCombine::CONSTANT;
474         findAttr(sourceParams, p, source);
475         result->setSource2_RGB(source);
476     }
477     if ((p = getEffectPropertyChild(effect, envProp, "source0-alpha"))) {
478         TexEnvCombine::SourceParam source = TexEnvCombine::TEXTURE;
479         findAttr(sourceParams, p, source);
480         result->setSource0_Alpha(source);
481     }
482     if ((p = getEffectPropertyChild(effect, envProp, "source1-alpha"))) {
483         TexEnvCombine::SourceParam source = TexEnvCombine::PREVIOUS;
484         findAttr(sourceParams, p, source);
485         result->setSource1_Alpha(source);
486     }
487     if ((p = getEffectPropertyChild(effect, envProp, "source2-alpha"))) {
488         TexEnvCombine::SourceParam source = TexEnvCombine::CONSTANT;
489         findAttr(sourceParams, p, source);
490         result->setSource2_Alpha(source);
491     }
492     if ((p = getEffectPropertyChild(effect, envProp, "operand0-rgb"))) {
493         TexEnvCombine::OperandParam op = TexEnvCombine::SRC_COLOR;
494         findAttr(operandParams, p, op);
495         result->setOperand0_RGB(op);
496     }
497     if ((p = getEffectPropertyChild(effect, envProp, "operand1-rgb"))) {
498         TexEnvCombine::OperandParam op = TexEnvCombine::SRC_COLOR;
499         findAttr(operandParams, p, op);
500         result->setOperand1_RGB(op);
501     }
502     if ((p = getEffectPropertyChild(effect, envProp, "operand2-rgb"))) {
503         TexEnvCombine::OperandParam op = TexEnvCombine::SRC_ALPHA;
504         findAttr(operandParams, p, op);
505         result->setOperand2_RGB(op);
506     }
507     if ((p = getEffectPropertyChild(effect, envProp, "operand0-alpha"))) {
508         TexEnvCombine::OperandParam op = TexEnvCombine::SRC_ALPHA;
509         findAttr(operandParams, p, op);
510         result->setOperand0_Alpha(op);
511     }
512     if ((p = getEffectPropertyChild(effect, envProp, "operand1-alpha"))) {
513         TexEnvCombine::OperandParam op = TexEnvCombine::SRC_ALPHA;
514         findAttr(operandParams, p, op);
515         result->setOperand1_Alpha(op);
516     }
517     if ((p = getEffectPropertyChild(effect, envProp, "operand2-alpha"))) {
518         TexEnvCombine::OperandParam op = TexEnvCombine::SRC_ALPHA;
519         findAttr(operandParams, p, op);
520         result->setOperand2_Alpha(op);
521     }
522     if ((p = getEffectPropertyChild(effect, envProp, "scale-rgb"))) {
523         result->setScale_RGB(p->getValue<float>());
524     }
525     if ((p = getEffectPropertyChild(effect, envProp, "scale-alpha"))) {
526         result->setScale_Alpha(p->getValue<float>());
527     }
528 #if 0
529     if ((p = getEffectPropertyChild(effect, envProp, "constant-color"))) {
530         SGVec4d color = p->getValue<SGVec4d>();
531         result->setConstantColor(toOsg(color));
532     } else if ((p = getEffectPropertyChild(effect, envProp,
533                                            "light-direction"))) {
534         SGVec3d direction = p->getValue<SGVec3d>();
535         result->setConstantColorAsLightDirection(toOsg(direction));
536     }
537 #endif
538     const SGPropertyNode* colorNode = envProp->getChild("constant-color");
539     if (colorNode)
540         initFromParameters(effect, colorNode, result,
541                            &TexEnvCombine::setConstantColor, colorFields);
542     return result;
543 }
544
545 EffectNameValue<TexGen::Mode> tgenModeInit[] =
546 {
547     { "object-linear", TexGen::OBJECT_LINEAR},
548     { "eye-linear", TexGen::EYE_LINEAR},
549     { "sphere-map", TexGen::SPHERE_MAP},
550     { "normal-map", TexGen::NORMAL_MAP},
551     { "reflection-map", TexGen::REFLECTION_MAP}
552 };
553
554 EffectPropertyMap<TexGen::Mode> tgenModes(tgenModeInit);
555
556 EffectNameValue<TexGen::Coord> tgenCoordInit[] =
557 {
558     {"s", TexGen::S},
559     {"t", TexGen::T},
560     {"r", TexGen::R},
561     {"q", TexGen::Q}
562 };
563
564 EffectPropertyMap<TexGen::Coord> tgenCoords(tgenCoordInit);
565
566 TexGen* buildTexGen(Effect* effect, const SGPropertyNode* tgenProp)
567 {
568     if (!isAttributeActive(effect, tgenProp))
569         return 0;
570     TexGen* result = new TexGen;
571     const SGPropertyNode* p = 0;
572     TexGen::Mode mode = TexGen::OBJECT_LINEAR;
573     findAttr(tgenModes, getEffectPropertyChild(effect, tgenProp, "mode"), mode);
574     result->setMode(mode);
575     const SGPropertyNode* planesNode = tgenProp->getChild("planes");
576     if (planesNode) {
577         for (int i = 0; i < planesNode->nChildren(); ++i) {
578             const SGPropertyNode* planeNode = planesNode->getChild(i);
579             TexGen::Coord coord;
580             findAttr(tgenCoords, planeNode->getName(), coord);
581             const SGPropertyNode* realNode
582                 = getEffectPropertyNode(effect, planeNode);
583             SGVec4d plane = realNode->getValue<SGVec4d>();
584             result->setPlane(coord, toOsg(plane));
585         }
586     }
587     return result;
588 }
589
590 bool makeTextureParameters(SGPropertyNode* paramRoot, const StateSet* ss)
591 {
592     SGPropertyNode* texUnit = makeChild(paramRoot, "texture");
593     const Texture* tex = getStateAttribute<Texture>(0, ss);
594     const Texture2D* texture = dynamic_cast<const Texture2D*>(tex);
595     makeChild(texUnit, "unit")->setValue(0);
596     if (!tex) {
597         // The default shader-based technique ignores active
598         makeChild(texUnit, "active")->setValue(false);
599         return false;
600     }
601     const Image* image = texture->getImage();
602     string imageName;
603     if (image) {
604         imageName = image->getFileName();
605     } else {
606         makeChild(texUnit, "active")->setValue(false);
607         makeChild(texUnit, "type")->setValue("white");
608         return false;
609     }
610     makeChild(texUnit, "active")->setValue(true);
611     makeChild(texUnit, "type")->setValue("2d");
612     string filter = findName(filterModes,
613                              texture->getFilter(Texture::MIN_FILTER));
614     string magFilter = findName(filterModes,
615                              texture->getFilter(Texture::MAG_FILTER));
616     string wrapS = findName(wrapModes, texture->getWrap(Texture::WRAP_S));
617     string wrapT = findName(wrapModes, texture->getWrap(Texture::WRAP_T));
618     string wrapR = findName(wrapModes, texture->getWrap(Texture::WRAP_R));
619     makeChild(texUnit, "image")->setStringValue(imageName);
620     makeChild(texUnit, "filter")->setStringValue(filter);
621     makeChild(texUnit, "mag-filter")->setStringValue(magFilter);
622     makeChild(texUnit, "wrap-s")->setStringValue(wrapS);
623     makeChild(texUnit, "wrap-t")->setStringValue(wrapT);
624     makeChild(texUnit, "wrap-r")->setStringValue(wrapR);
625     return true;
626 }
627
628 }