1 // Copyright (C) 2009 Tim Moore timoore@redhat.com
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.
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.
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.
18 # include <simgear_config.h>
21 #include "TextureBuilder.hxx"
26 #include <osg/TexEnvCombine>
28 #include <osg/Texture1D>
29 #include <osg/Texture2D>
30 #include <osg/Texture3D>
31 #include <osg/TextureRectangle>
32 #include <osg/TextureCubeMap>
33 #include <osgDB/FileUtils>
35 #include <boost/lexical_cast.hpp>
36 #include <boost/tuple/tuple.hpp>
37 #include <boost/tuple/tuple_comparison.hpp>
39 #include <simgear/scene/model/SGReaderWriterXMLOptions.hxx>
40 #include <simgear/scene/util/SGSceneFeatures.hxx>
41 #include <simgear/scene/util/StateAttributeFactory.hxx>
42 #include <simgear/math/SGMath.hxx>
43 #include <simgear/structure/OSGUtils.hxx>
52 using namespace effect;
54 TexEnvCombine* buildTexEnvCombine(Effect* effect,
55 const SGPropertyNode* envProp,
56 const SGReaderWriterXMLOptions* options);
57 TexGen* buildTexGen(Effect* Effect, const SGPropertyNode* tgenProp);
59 // Hack to force inclusion of TextureBuilder.cxx in library
60 osg::Texture* TextureBuilder::buildFromType(Effect* effect, const string& type,
61 const SGPropertyNode*props,
62 const SGReaderWriterXMLOptions*
65 return EffectBuilder<Texture>::buildFromType(effect, type, props, options);
68 typedef boost::tuple<string, Texture::FilterMode, Texture::FilterMode,
69 Texture::WrapMode, Texture::WrapMode, Texture::WrapMode,
72 EffectNameValue<TexEnv::Mode> texEnvModesInit[] =
75 {"blend", TexEnv::BLEND},
76 {"decal", TexEnv::DECAL},
77 {"modulate", TexEnv::MODULATE},
78 {"replace", TexEnv::REPLACE}
80 EffectPropertyMap<TexEnv::Mode> texEnvModes(texEnvModesInit);
82 TexEnv* buildTexEnv(Effect* effect, const SGPropertyNode* prop)
84 const SGPropertyNode* modeProp = getEffectPropertyChild(effect, prop,
86 const SGPropertyNode* colorProp = getEffectPropertyChild(effect, prop,
90 TexEnv::Mode mode = TexEnv::MODULATE;
91 findAttr(texEnvModes, modeProp, mode);
92 if (mode == TexEnv::MODULATE) {
93 return StateAttributeFactory::instance()->getStandardTexEnv();
95 TexEnv* env = new TexEnv(mode);
97 env->setColor(toOsg(colorProp->getValue<SGVec4d>()));
102 void TextureUnitBuilder::buildAttribute(Effect* effect, Pass* pass,
103 const SGPropertyNode* prop,
104 const SGReaderWriterXMLOptions* options)
106 if (!isAttributeActive(effect, prop))
108 // Decode the texture unit
110 const SGPropertyNode* pUnit = prop->getChild("unit");
112 unit = pUnit->getValue<int>();
114 const SGPropertyNode* pName = prop->getChild("name");
117 unit = boost::lexical_cast<int>(pName->getStringValue());
118 } catch (boost::bad_lexical_cast& lex) {
119 SG_LOG(SG_INPUT, SG_ALERT, "can't decode name as texture unit "
123 const SGPropertyNode* pType = getEffectPropertyChild(effect, prop, "type");
128 type = pType->getStringValue();
129 Texture* texture = 0;
131 texture = TextureBuilder::buildFromType(effect, type, prop,
134 catch (BuilderException& ) {
135 SG_LOG(SG_INPUT, SG_ALERT, "No image file, "
136 << "maybe the reader did not set the filename attribute, "
137 << "using white on " << pass->getName());
138 texture = StateAttributeFactory::instance()->getWhiteTexture();
140 pass->setTextureAttributeAndModes(unit, texture);
141 const SGPropertyNode* envProp = prop->getChild("environment");
143 TexEnv* env = buildTexEnv(effect, envProp);
145 pass->setTextureAttributeAndModes(unit, env);
147 const SGPropertyNode* combineProp = prop->getChild("texenv-combine");
148 TexEnvCombine* combiner = 0;
149 if (combineProp && ((combiner = buildTexEnvCombine(effect, combineProp,
151 pass->setTextureAttributeAndModes(unit, combiner);
152 const SGPropertyNode* tgenProp = prop->getChild("texgen");
154 if (tgenProp && (tgen = buildTexGen(effect, tgenProp)))
155 pass->setTextureAttributeAndModes(unit, tgen);
158 // InstallAttributeBuilder call is in Effect.cxx to force this file to
163 EffectNameValue<Texture::FilterMode> filterModesInit[] =
165 { "linear", Texture::LINEAR },
166 { "linear-mipmap-linear", Texture::LINEAR_MIPMAP_LINEAR},
167 { "linear-mipmap-nearest", Texture::LINEAR_MIPMAP_NEAREST},
168 { "nearest", Texture::NEAREST},
169 { "nearest-mipmap-linear", Texture::NEAREST_MIPMAP_LINEAR},
170 { "nearest-mipmap-nearest", Texture::NEAREST_MIPMAP_NEAREST}
172 EffectPropertyMap<Texture::FilterMode> filterModes(filterModesInit);
174 EffectNameValue<Texture::WrapMode> wrapModesInit[] =
176 {"clamp", Texture::CLAMP},
177 {"clamp-to-border", Texture::CLAMP_TO_BORDER},
178 {"clamp-to-edge", Texture::CLAMP_TO_EDGE},
179 {"mirror", Texture::MIRROR},
180 {"repeat", Texture::REPEAT}
182 EffectPropertyMap<Texture::WrapMode> wrapModes(wrapModesInit);
185 TexTuple makeTexTuple(Effect* effect, const SGPropertyNode* props,
186 const SGReaderWriterXMLOptions* options,
187 const string& texType)
189 Texture::FilterMode minFilter = Texture::LINEAR_MIPMAP_LINEAR;
190 const SGPropertyNode* ep = 0;
191 if ((ep = getEffectPropertyChild(effect, props, "filter")))
192 findAttr(filterModes, ep, minFilter);
193 Texture::FilterMode magFilter = Texture::LINEAR;
194 if ((ep = getEffectPropertyChild(effect, props, "mag-filter")))
195 findAttr(filterModes, ep, magFilter);
196 const SGPropertyNode* pWrapS
197 = getEffectPropertyChild(effect, props, "wrap-s");
198 Texture::WrapMode sWrap = Texture::CLAMP;
200 findAttr(wrapModes, pWrapS, sWrap);
201 const SGPropertyNode* pWrapT
202 = getEffectPropertyChild(effect, props, "wrap-t");
203 Texture::WrapMode tWrap = Texture::CLAMP;
205 findAttr(wrapModes, pWrapT, tWrap);
206 const SGPropertyNode* pWrapR
207 = getEffectPropertyChild(effect, props, "wrap-r");
208 Texture::WrapMode rWrap = Texture::CLAMP;
210 findAttr(wrapModes, pWrapR, rWrap);
211 const SGPropertyNode* pImage
212 = getEffectPropertyChild(effect, props, "image");
215 imageName = pImage->getStringValue();
216 string absFileName = osgDB::findDataFile(imageName, options);
217 return TexTuple(absFileName, minFilter, magFilter, sWrap, tWrap, rWrap,
221 void setAttrs(const TexTuple& attrs, Texture* tex,
222 const SGReaderWriterXMLOptions* options)
224 const string& imageName = attrs.get<0>();
225 if (imageName.empty()) {
226 throw BuilderException("no image file");
228 osgDB::ReaderWriter::ReadResult result
229 = osgDB::Registry::instance()->readImage(imageName, options);
230 if (result.success()) {
231 osg::Image* image = result.getImage();
232 tex->setImage(GL_FRONT_AND_BACK, image);
235 if (s <= t && 32 <= s) {
236 SGSceneFeatures::instance()->setTextureCompression(tex);
237 } else if (t < s && 32 <= t) {
238 SGSceneFeatures::instance()->setTextureCompression(tex);
240 tex->setMaxAnisotropy(SGSceneFeatures::instance()
241 ->getTextureFilter());
243 SG_LOG(SG_INPUT, SG_ALERT, "failed to load effect texture file "
247 // texture->setDataVariance(osg::Object::STATIC);
248 tex->setFilter(Texture::MIN_FILTER, attrs.get<1>());
249 tex->setFilter(Texture::MAG_FILTER, attrs.get<2>());
250 tex->setWrap(Texture::WRAP_S, attrs.get<3>());
251 tex->setWrap(Texture::WRAP_T, attrs.get<4>());
252 tex->setWrap(Texture::WRAP_R, attrs.get<5>());
257 class TexBuilder : public TextureBuilder
260 TexBuilder(const string& texType) : _type(texType) {}
261 Texture* build(Effect* effect, const SGPropertyNode*,
262 const SGReaderWriterXMLOptions* options);
264 typedef map<TexTuple, ref_ptr<T> > TexMap;
270 Texture* TexBuilder<T>::build(Effect* effect, const SGPropertyNode* props,
271 const SGReaderWriterXMLOptions* options)
273 TexTuple attrs = makeTexTuple(effect, props, options, _type);
274 typename TexMap::iterator itr = texMap.find(attrs);
275 if (itr != texMap.end())
276 return itr->second.get();
278 setAttrs(attrs, tex, options);
279 texMap.insert(make_pair(attrs, tex));
286 TextureBuilder::Registrar install1D("1d", new TexBuilder<Texture1D>("1d"));
287 TextureBuilder::Registrar install2D("2d", new TexBuilder<Texture2D>("2d"));
288 TextureBuilder::Registrar install3D("3d", new TexBuilder<Texture3D>("3d"));
291 class WhiteTextureBuilder : public TextureBuilder
294 Texture* build(Effect* effect, const SGPropertyNode*,
295 const SGReaderWriterXMLOptions* options);
298 Texture* WhiteTextureBuilder::build(Effect* effect, const SGPropertyNode*,
299 const SGReaderWriterXMLOptions* options)
301 return StateAttributeFactory::instance()->getWhiteTexture();
306 TextureBuilder::Registrar installWhite("white", new WhiteTextureBuilder);
309 class TransparentTextureBuilder : public TextureBuilder
312 Texture* build(Effect* effect, const SGPropertyNode*,
313 const SGReaderWriterXMLOptions* options);
316 Texture* TransparentTextureBuilder::build(Effect* effect, const SGPropertyNode*,
317 const SGReaderWriterXMLOptions* options)
319 return StateAttributeFactory::instance()->getTransparentTexture();
324 TextureBuilder::Registrar installTransparent("transparent",
325 new TransparentTextureBuilder);
328 osg::Image* make3DNoiseImage(int texSize)
330 osg::Image* image = new osg::Image;
331 image->setImage(texSize, texSize, texSize,
332 4, GL_RGBA, GL_UNSIGNED_BYTE,
333 new unsigned char[4 * texSize * texSize * texSize],
334 osg::Image::USE_NEW_DELETE);
336 const int startFrequency = 4;
337 const int numOctaves = 4;
341 double inci, incj, inck;
342 int frequency = startFrequency;
346 osg::notify(osg::WARN) << "creating 3D noise texture... ";
348 for (f = 0, inc = 0; f < numOctaves; ++f, frequency *= 2, ++inc, amp *= 0.5)
350 SetNoiseFrequency(frequency);
352 ni[0] = ni[1] = ni[2] = 0;
354 inci = 1.0 / (texSize / frequency);
355 for (i = 0; i < texSize; ++i, ni[0] += inci)
357 incj = 1.0 / (texSize / frequency);
358 for (j = 0; j < texSize; ++j, ni[1] += incj)
360 inck = 1.0 / (texSize / frequency);
361 for (k = 0; k < texSize; ++k, ni[2] += inck, ptr += 4)
363 *(ptr+inc) = (GLubyte) (((noise3(ni) + 1.0) * amp) * 128.0);
369 osg::notify(osg::WARN) << "DONE" << std::endl;
373 class NoiseBuilder : public TextureBuilder
376 Texture* build(Effect* effect, const SGPropertyNode*,
377 const SGReaderWriterXMLOptions* options);
379 typedef map<int, ref_ptr<Texture3D> > NoiseMap;
383 Texture* NoiseBuilder::build(Effect* effect, const SGPropertyNode* props,
384 const SGReaderWriterXMLOptions* options)
387 const SGPropertyNode* sizeProp = getEffectPropertyChild(effect, props,
390 texSize = sizeProp->getValue<int>();
391 NoiseMap::iterator itr = _noises.find(texSize);
392 if (itr != _noises.end())
393 return itr->second.get();
394 Texture3D* noiseTexture = new osg::Texture3D;
395 noiseTexture->setFilter(osg::Texture3D::MIN_FILTER, osg::Texture3D::LINEAR);
396 noiseTexture->setFilter(osg::Texture3D::MAG_FILTER, osg::Texture3D::LINEAR);
397 noiseTexture->setWrap(osg::Texture3D::WRAP_S, osg::Texture3D::REPEAT);
398 noiseTexture->setWrap(osg::Texture3D::WRAP_T, osg::Texture3D::REPEAT);
399 noiseTexture->setWrap(osg::Texture3D::WRAP_R, osg::Texture3D::REPEAT);
400 noiseTexture->setImage( make3DNoiseImage(texSize) );
401 _noises.insert(make_pair(texSize, noiseTexture));
407 TextureBuilder::Registrar installNoise("noise", new NoiseBuilder);
411 // Image names for all sides
412 typedef boost::tuple<string, string, string, string, string, string> CubeMapTuple;
414 CubeMapTuple makeCubeMapTuple(Effect* effect, const SGPropertyNode* props)
416 const SGPropertyNode* ep = 0;
419 if ((ep = getEffectPropertyChild(effect, props, "positive-x")))
420 positive_x = ep->getStringValue();
422 if ((ep = getEffectPropertyChild(effect, props, "negative-x")))
423 negative_x = ep->getStringValue();
425 if ((ep = getEffectPropertyChild(effect, props, "positive-y")))
426 positive_y = ep->getStringValue();
428 if ((ep = getEffectPropertyChild(effect, props, "negative-y")))
429 negative_y = ep->getStringValue();
431 if ((ep = getEffectPropertyChild(effect, props, "positive-z")))
432 positive_z = ep->getStringValue();
434 if ((ep = getEffectPropertyChild(effect, props, "negative-z")))
435 negative_z = ep->getStringValue();
436 return CubeMapTuple(positive_x, negative_x, positive_y, negative_y, positive_z, negative_z);
440 class CubeMapBuilder : public TextureBuilder
443 Texture* build(Effect* effect, const SGPropertyNode*,
444 const SGReaderWriterXMLOptions* options);
446 typedef map<CubeMapTuple, ref_ptr<TextureCubeMap> > CubeMap;
450 Texture* CubeMapBuilder::build(Effect* effect, const SGPropertyNode* props,
451 const SGReaderWriterXMLOptions* options)
453 // First check that there is a <images> tag
454 const SGPropertyNode* texturesProp = getEffectPropertyChild(effect, props, "images");
456 throw BuilderException("no <images> for cube map");
457 return NULL; // This is redundant
460 CubeMapTuple _tuple = makeCubeMapTuple(effect, texturesProp);
462 CubeMap::iterator itr = _cubemaps.find(_tuple);
463 if (itr != _cubemaps.end())
464 return itr->second.get();
466 TextureCubeMap* cubeTexture = new osg::TextureCubeMap;
468 // TODO: Read these from effect file? Maybe these are sane for all cuebmaps?
469 cubeTexture->setFilter(osg::Texture3D::MIN_FILTER, osg::Texture::LINEAR_MIPMAP_LINEAR);
470 cubeTexture->setFilter(osg::Texture3D::MAG_FILTER, osg::Texture::LINEAR);
471 cubeTexture->setWrap(osg::Texture3D::WRAP_S, osg::Texture::CLAMP_TO_EDGE);
472 cubeTexture->setWrap(osg::Texture3D::WRAP_T, osg::Texture::CLAMP_TO_EDGE);
473 cubeTexture->setWrap(osg::Texture3D::WRAP_R, osg::Texture::CLAMP_TO_EDGE);
475 osgDB::ReaderWriter::ReadResult result =
476 osgDB::Registry::instance()->readImage(_tuple.get<0>(), options);
477 if(result.success()) {
478 osg::Image* image = result.getImage();
479 cubeTexture->setImage(TextureCubeMap::POSITIVE_X, image);
481 result = osgDB::Registry::instance()->readImage(_tuple.get<1>(), options);
482 if(result.success()) {
483 osg::Image* image = result.getImage();
484 cubeTexture->setImage(TextureCubeMap::NEGATIVE_X, image);
486 result = osgDB::Registry::instance()->readImage(_tuple.get<2>(), options);
487 if(result.success()) {
488 osg::Image* image = result.getImage();
489 cubeTexture->setImage(TextureCubeMap::POSITIVE_Y, image);
491 result = osgDB::Registry::instance()->readImage(_tuple.get<3>(), options);
492 if(result.success()) {
493 osg::Image* image = result.getImage();
494 cubeTexture->setImage(TextureCubeMap::NEGATIVE_Y, image);
496 result = osgDB::Registry::instance()->readImage(_tuple.get<4>(), options);
497 if(result.success()) {
498 osg::Image* image = result.getImage();
499 cubeTexture->setImage(TextureCubeMap::POSITIVE_Z, image);
501 result = osgDB::Registry::instance()->readImage(_tuple.get<5>(), options);
502 if(result.success()) {
503 osg::Image* image = result.getImage();
504 cubeTexture->setImage(TextureCubeMap::NEGATIVE_Z, image);
507 _cubemaps[_tuple] = cubeTexture;
513 TextureBuilder::Registrar installCubeMap("cubemap", new CubeMapBuilder);
518 EffectNameValue<TexEnvCombine::CombineParam> combineParamInit[] =
520 {"replace", TexEnvCombine::REPLACE},
521 {"modulate", TexEnvCombine::MODULATE},
522 {"add", TexEnvCombine::ADD},
523 {"add-signed", TexEnvCombine::ADD_SIGNED},
524 {"interpolate", TexEnvCombine::INTERPOLATE},
525 {"subtract", TexEnvCombine::SUBTRACT},
526 {"dot3-rgb", TexEnvCombine::DOT3_RGB},
527 {"dot3-rgba", TexEnvCombine::DOT3_RGBA}
530 EffectPropertyMap<TexEnvCombine::CombineParam> combineParams(combineParamInit);
532 EffectNameValue<TexEnvCombine::SourceParam> sourceParamInit[] =
534 {"constant", TexEnvCombine::CONSTANT},
535 {"primary_color", TexEnvCombine::PRIMARY_COLOR},
536 {"previous", TexEnvCombine::PREVIOUS},
537 {"texture", TexEnvCombine::TEXTURE},
538 {"texture0", TexEnvCombine::TEXTURE0},
539 {"texture1", TexEnvCombine::TEXTURE1},
540 {"texture2", TexEnvCombine::TEXTURE2},
541 {"texture3", TexEnvCombine::TEXTURE3},
542 {"texture4", TexEnvCombine::TEXTURE4},
543 {"texture5", TexEnvCombine::TEXTURE5},
544 {"texture6", TexEnvCombine::TEXTURE6},
545 {"texture7", TexEnvCombine::TEXTURE7}
548 EffectPropertyMap<TexEnvCombine::SourceParam> sourceParams(sourceParamInit);
550 EffectNameValue<TexEnvCombine::OperandParam> opParamInit[] =
552 {"src-color", TexEnvCombine::SRC_COLOR},
553 {"one-minus-src-color", TexEnvCombine::ONE_MINUS_SRC_COLOR},
554 {"src-alpha", TexEnvCombine::SRC_ALPHA},
555 {"one-minus-src-alpha", TexEnvCombine::ONE_MINUS_SRC_ALPHA}
558 EffectPropertyMap<TexEnvCombine::OperandParam> operandParams(opParamInit);
560 TexEnvCombine* buildTexEnvCombine(Effect* effect, const SGPropertyNode* envProp,
561 const SGReaderWriterXMLOptions* options)
563 if (!isAttributeActive(effect, envProp))
565 TexEnvCombine* result = new TexEnvCombine;
566 const SGPropertyNode* p = 0;
567 if ((p = getEffectPropertyChild(effect, envProp, "combine-rgb"))) {
568 TexEnvCombine::CombineParam crgb = TexEnvCombine::MODULATE;
569 findAttr(combineParams, p, crgb);
570 result->setCombine_RGB(crgb);
572 if ((p = getEffectPropertyChild(effect, envProp, "combine-alpha"))) {
573 TexEnvCombine::CombineParam calpha = TexEnvCombine::MODULATE;
574 findAttr(combineParams, p, calpha);
575 result->setCombine_Alpha(calpha);
577 if ((p = getEffectPropertyChild(effect, envProp, "source0-rgb"))) {
578 TexEnvCombine::SourceParam source = TexEnvCombine::TEXTURE;
579 findAttr(sourceParams, p, source);
580 result->setSource0_RGB(source);
582 if ((p = getEffectPropertyChild(effect, envProp, "source1-rgb"))) {
583 TexEnvCombine::SourceParam source = TexEnvCombine::PREVIOUS;
584 findAttr(sourceParams, p, source);
585 result->setSource1_RGB(source);
587 if ((p = getEffectPropertyChild(effect, envProp, "source2-rgb"))) {
588 TexEnvCombine::SourceParam source = TexEnvCombine::CONSTANT;
589 findAttr(sourceParams, p, source);
590 result->setSource2_RGB(source);
592 if ((p = getEffectPropertyChild(effect, envProp, "source0-alpha"))) {
593 TexEnvCombine::SourceParam source = TexEnvCombine::TEXTURE;
594 findAttr(sourceParams, p, source);
595 result->setSource0_Alpha(source);
597 if ((p = getEffectPropertyChild(effect, envProp, "source1-alpha"))) {
598 TexEnvCombine::SourceParam source = TexEnvCombine::PREVIOUS;
599 findAttr(sourceParams, p, source);
600 result->setSource1_Alpha(source);
602 if ((p = getEffectPropertyChild(effect, envProp, "source2-alpha"))) {
603 TexEnvCombine::SourceParam source = TexEnvCombine::CONSTANT;
604 findAttr(sourceParams, p, source);
605 result->setSource2_Alpha(source);
607 if ((p = getEffectPropertyChild(effect, envProp, "operand0-rgb"))) {
608 TexEnvCombine::OperandParam op = TexEnvCombine::SRC_COLOR;
609 findAttr(operandParams, p, op);
610 result->setOperand0_RGB(op);
612 if ((p = getEffectPropertyChild(effect, envProp, "operand1-rgb"))) {
613 TexEnvCombine::OperandParam op = TexEnvCombine::SRC_COLOR;
614 findAttr(operandParams, p, op);
615 result->setOperand1_RGB(op);
617 if ((p = getEffectPropertyChild(effect, envProp, "operand2-rgb"))) {
618 TexEnvCombine::OperandParam op = TexEnvCombine::SRC_ALPHA;
619 findAttr(operandParams, p, op);
620 result->setOperand2_RGB(op);
622 if ((p = getEffectPropertyChild(effect, envProp, "operand0-alpha"))) {
623 TexEnvCombine::OperandParam op = TexEnvCombine::SRC_ALPHA;
624 findAttr(operandParams, p, op);
625 result->setOperand0_Alpha(op);
627 if ((p = getEffectPropertyChild(effect, envProp, "operand1-alpha"))) {
628 TexEnvCombine::OperandParam op = TexEnvCombine::SRC_ALPHA;
629 findAttr(operandParams, p, op);
630 result->setOperand1_Alpha(op);
632 if ((p = getEffectPropertyChild(effect, envProp, "operand2-alpha"))) {
633 TexEnvCombine::OperandParam op = TexEnvCombine::SRC_ALPHA;
634 findAttr(operandParams, p, op);
635 result->setOperand2_Alpha(op);
637 if ((p = getEffectPropertyChild(effect, envProp, "scale-rgb"))) {
638 result->setScale_RGB(p->getValue<float>());
640 if ((p = getEffectPropertyChild(effect, envProp, "scale-alpha"))) {
641 result->setScale_Alpha(p->getValue<float>());
644 if ((p = getEffectPropertyChild(effect, envProp, "constant-color"))) {
645 SGVec4d color = p->getValue<SGVec4d>();
646 result->setConstantColor(toOsg(color));
647 } else if ((p = getEffectPropertyChild(effect, envProp,
648 "light-direction"))) {
649 SGVec3d direction = p->getValue<SGVec3d>();
650 result->setConstantColorAsLightDirection(toOsg(direction));
653 const SGPropertyNode* colorNode = envProp->getChild("constant-color");
655 initFromParameters(effect, colorNode, result,
656 &TexEnvCombine::setConstantColor, colorFields,
661 EffectNameValue<TexGen::Mode> tgenModeInit[] =
663 { "object-linear", TexGen::OBJECT_LINEAR},
664 { "eye-linear", TexGen::EYE_LINEAR},
665 { "sphere-map", TexGen::SPHERE_MAP},
666 { "normal-map", TexGen::NORMAL_MAP},
667 { "reflection-map", TexGen::REFLECTION_MAP}
670 EffectPropertyMap<TexGen::Mode> tgenModes(tgenModeInit);
672 EffectNameValue<TexGen::Coord> tgenCoordInit[] =
680 EffectPropertyMap<TexGen::Coord> tgenCoords(tgenCoordInit);
682 TexGen* buildTexGen(Effect* effect, const SGPropertyNode* tgenProp)
684 if (!isAttributeActive(effect, tgenProp))
686 TexGen* result = new TexGen;
687 TexGen::Mode mode = TexGen::OBJECT_LINEAR;
688 findAttr(tgenModes, getEffectPropertyChild(effect, tgenProp, "mode"), mode);
689 result->setMode(mode);
690 const SGPropertyNode* planesNode = tgenProp->getChild("planes");
692 for (int i = 0; i < planesNode->nChildren(); ++i) {
693 const SGPropertyNode* planeNode = planesNode->getChild(i);
695 findAttr(tgenCoords, planeNode->getName(), coord);
696 const SGPropertyNode* realNode
697 = getEffectPropertyNode(effect, planeNode);
698 SGVec4d plane = realNode->getValue<SGVec4d>();
699 result->setPlane(coord, toOsg(plane));
705 bool makeTextureParameters(SGPropertyNode* paramRoot, const StateSet* ss)
707 SGPropertyNode* texUnit = makeChild(paramRoot, "texture");
708 const Texture* tex = getStateAttribute<Texture>(0, ss);
709 const Texture2D* texture = dynamic_cast<const Texture2D*>(tex);
710 makeChild(texUnit, "unit")->setValue(0);
712 // The default shader-based technique ignores active
713 makeChild(texUnit, "active")->setValue(false);
716 const Image* image = texture->getImage();
719 imageName = image->getFileName();
721 makeChild(texUnit, "active")->setValue(false);
722 makeChild(texUnit, "type")->setValue("white");
725 makeChild(texUnit, "active")->setValue(true);
726 makeChild(texUnit, "type")->setValue("2d");
727 string filter = findName(filterModes,
728 texture->getFilter(Texture::MIN_FILTER));
729 string magFilter = findName(filterModes,
730 texture->getFilter(Texture::MAG_FILTER));
731 string wrapS = findName(wrapModes, texture->getWrap(Texture::WRAP_S));
732 string wrapT = findName(wrapModes, texture->getWrap(Texture::WRAP_T));
733 string wrapR = findName(wrapModes, texture->getWrap(Texture::WRAP_R));
734 makeChild(texUnit, "image")->setStringValue(imageName);
735 makeChild(texUnit, "filter")->setStringValue(filter);
736 makeChild(texUnit, "mag-filter")->setStringValue(magFilter);
737 makeChild(texUnit, "wrap-s")->setStringValue(wrapS);
738 makeChild(texUnit, "wrap-t")->setStringValue(wrapT);
739 makeChild(texUnit, "wrap-r")->setStringValue(wrapR);