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"
27 #include <osg/TexEnvCombine>
29 #include <osg/Texture1D>
30 #include <osg/Texture2D>
31 #include <osg/Texture3D>
32 #include <osg/TextureRectangle>
33 #include <osg/TextureCubeMap>
34 #include <osgDB/FileUtils>
36 #include <boost/lexical_cast.hpp>
37 #include <boost/tuple/tuple.hpp>
38 #include <boost/tuple/tuple_comparison.hpp>
40 #include <simgear/scene/model/SGReaderWriterXMLOptions.hxx>
41 #include <simgear/scene/util/SGSceneFeatures.hxx>
42 #include <simgear/scene/util/StateAttributeFactory.hxx>
43 #include <simgear/math/SGMath.hxx>
44 #include <simgear/structure/OSGUtils.hxx>
53 using namespace effect;
55 TexEnvCombine* buildTexEnvCombine(Effect* effect,
56 const SGPropertyNode* envProp,
57 const SGReaderWriterXMLOptions* options);
58 TexGen* buildTexGen(Effect* Effect, const SGPropertyNode* tgenProp);
60 // Hack to force inclusion of TextureBuilder.cxx in library
61 osg::Texture* TextureBuilder::buildFromType(Effect* effect, const string& type,
62 const SGPropertyNode*props,
63 const SGReaderWriterXMLOptions*
66 return EffectBuilder<Texture>::buildFromType(effect, type, props, options);
69 typedef boost::tuple<string, Texture::FilterMode, Texture::FilterMode,
70 Texture::WrapMode, Texture::WrapMode, Texture::WrapMode,
71 string, MipMapTuple> TexTuple;
73 EffectNameValue<TexEnv::Mode> texEnvModesInit[] =
76 {"blend", TexEnv::BLEND},
77 {"decal", TexEnv::DECAL},
78 {"modulate", TexEnv::MODULATE},
79 {"replace", TexEnv::REPLACE}
81 EffectPropertyMap<TexEnv::Mode> texEnvModes(texEnvModesInit);
83 TexEnv* buildTexEnv(Effect* effect, const SGPropertyNode* prop)
85 const SGPropertyNode* modeProp = getEffectPropertyChild(effect, prop,
87 const SGPropertyNode* colorProp = getEffectPropertyChild(effect, prop,
91 TexEnv::Mode mode = TexEnv::MODULATE;
92 findAttr(texEnvModes, modeProp, mode);
93 if (mode == TexEnv::MODULATE) {
94 return StateAttributeFactory::instance()->getStandardTexEnv();
96 TexEnv* env = new TexEnv(mode);
98 env->setColor(toOsg(colorProp->getValue<SGVec4d>()));
103 void TextureUnitBuilder::buildAttribute(Effect* effect, Pass* pass,
104 const SGPropertyNode* prop,
105 const SGReaderWriterXMLOptions* options)
107 if (!isAttributeActive(effect, prop))
109 // Decode the texture unit
111 const SGPropertyNode* pUnit = prop->getChild("unit");
113 unit = pUnit->getValue<int>();
115 const SGPropertyNode* pName = prop->getChild("name");
118 unit = boost::lexical_cast<int>(pName->getStringValue());
119 } catch (boost::bad_lexical_cast& lex) {
120 SG_LOG(SG_INPUT, SG_ALERT, "can't decode name as texture unit "
124 const SGPropertyNode* pType = getEffectPropertyChild(effect, prop, "type");
129 type = pType->getStringValue();
130 Texture* texture = 0;
132 texture = TextureBuilder::buildFromType(effect, type, prop,
135 catch (BuilderException& ) {
136 SG_LOG(SG_INPUT, SG_ALERT, "No image file, "
137 << "maybe the reader did not set the filename attribute, "
138 << "using white for type '" << type << "' on '" << pass->getName() << "', in " << prop->getPath() );
139 texture = StateAttributeFactory::instance()->getWhiteTexture();
141 pass->setTextureAttributeAndModes(unit, texture);
142 const SGPropertyNode* envProp = prop->getChild("environment");
144 TexEnv* env = buildTexEnv(effect, envProp);
146 pass->setTextureAttributeAndModes(unit, env);
148 const SGPropertyNode* combineProp = prop->getChild("texenv-combine");
149 TexEnvCombine* combiner = 0;
150 if (combineProp && ((combiner = buildTexEnvCombine(effect, combineProp,
152 pass->setTextureAttributeAndModes(unit, combiner);
153 const SGPropertyNode* tgenProp = prop->getChild("texgen");
155 if (tgenProp && (tgen = buildTexGen(effect, tgenProp)))
156 pass->setTextureAttributeAndModes(unit, tgen);
159 // InstallAttributeBuilder call is in Effect.cxx to force this file to
164 EffectNameValue<Texture::FilterMode> filterModesInit[] =
166 { "linear", Texture::LINEAR },
167 { "linear-mipmap-linear", Texture::LINEAR_MIPMAP_LINEAR},
168 { "linear-mipmap-nearest", Texture::LINEAR_MIPMAP_NEAREST},
169 { "nearest", Texture::NEAREST},
170 { "nearest-mipmap-linear", Texture::NEAREST_MIPMAP_LINEAR},
171 { "nearest-mipmap-nearest", Texture::NEAREST_MIPMAP_NEAREST}
173 EffectPropertyMap<Texture::FilterMode> filterModes(filterModesInit);
175 EffectNameValue<Texture::WrapMode> wrapModesInit[] =
177 {"clamp", Texture::CLAMP},
178 {"clamp-to-border", Texture::CLAMP_TO_BORDER},
179 {"clamp-to-edge", Texture::CLAMP_TO_EDGE},
180 {"mirror", Texture::MIRROR},
181 {"repeat", Texture::REPEAT}
183 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);
218 const SGPropertyNode* pMipmapControl
219 = getEffectPropertyChild(effect, props, "mipmap-control");
220 MipMapTuple mipmapFunctions( AUTOMATIC, AUTOMATIC, AUTOMATIC, AUTOMATIC );
221 if ( pMipmapControl )
222 mipmapFunctions = makeMipMapTuple(effect, pMipmapControl, options);
224 return TexTuple(absFileName, minFilter, magFilter, sWrap, tWrap, rWrap,
225 texType, mipmapFunctions);
228 void setAttrs(const TexTuple& attrs, Texture* tex,
229 const SGReaderWriterXMLOptions* options)
231 const string& imageName = attrs.get<0>();
232 if (imageName.empty()) {
233 throw BuilderException("no image file");
235 osgDB::ReaderWriter::ReadResult result
236 = osgDB::Registry::instance()->readImage(imageName, options);
237 if (result.success()) {
238 osg::ref_ptr<osg::Image> image = result.getImage();
239 image = computeMipmap( image, attrs.get<7>() );
240 tex->setImage(GL_FRONT_AND_BACK, image);
243 if (s <= t && 32 <= s) {
244 SGSceneFeatures::instance()->setTextureCompression(tex);
245 } else if (t < s && 32 <= t) {
246 SGSceneFeatures::instance()->setTextureCompression(tex);
248 tex->setMaxAnisotropy(SGSceneFeatures::instance()
249 ->getTextureFilter());
251 SG_LOG(SG_INPUT, SG_ALERT, "failed to load effect texture file "
255 // texture->setDataVariance(osg::Object::STATIC);
256 tex->setFilter(Texture::MIN_FILTER, attrs.get<1>());
257 tex->setFilter(Texture::MAG_FILTER, attrs.get<2>());
258 tex->setWrap(Texture::WRAP_S, attrs.get<3>());
259 tex->setWrap(Texture::WRAP_T, attrs.get<4>());
260 tex->setWrap(Texture::WRAP_R, attrs.get<5>());
265 class TexBuilder : public TextureBuilder
268 TexBuilder(const string& texType) : _type(texType) {}
269 Texture* build(Effect* effect, const SGPropertyNode*,
270 const SGReaderWriterXMLOptions* options);
272 typedef map<TexTuple, ref_ptr<T> > TexMap;
278 Texture* TexBuilder<T>::build(Effect* effect, const SGPropertyNode* props,
279 const SGReaderWriterXMLOptions* options)
281 TexTuple attrs = makeTexTuple(effect, props, options, _type);
282 typename TexMap::iterator itr = texMap.find(attrs);
283 if (itr != texMap.end())
284 return itr->second.get();
286 setAttrs(attrs, tex, options);
287 texMap.insert(make_pair(attrs, tex));
294 TextureBuilder::Registrar install1D("1d", new TexBuilder<Texture1D>("1d"));
295 TextureBuilder::Registrar install2D("2d", new TexBuilder<Texture2D>("2d"));
296 TextureBuilder::Registrar install3D("3d", new TexBuilder<Texture3D>("3d"));
299 class WhiteTextureBuilder : public TextureBuilder
302 Texture* build(Effect* effect, const SGPropertyNode*,
303 const SGReaderWriterXMLOptions* options);
306 Texture* WhiteTextureBuilder::build(Effect* effect, const SGPropertyNode*,
307 const SGReaderWriterXMLOptions* options)
309 return StateAttributeFactory::instance()->getWhiteTexture();
314 TextureBuilder::Registrar installWhite("white", new WhiteTextureBuilder);
317 class TransparentTextureBuilder : public TextureBuilder
320 Texture* build(Effect* effect, const SGPropertyNode*,
321 const SGReaderWriterXMLOptions* options);
324 Texture* TransparentTextureBuilder::build(Effect* effect, const SGPropertyNode*,
325 const SGReaderWriterXMLOptions* options)
327 return StateAttributeFactory::instance()->getTransparentTexture();
332 TextureBuilder::Registrar installTransparent("transparent",
333 new TransparentTextureBuilder);
336 osg::Image* make3DNoiseImage(int texSize)
338 osg::Image* image = new osg::Image;
339 image->setImage(texSize, texSize, texSize,
340 4, GL_RGBA, GL_UNSIGNED_BYTE,
341 new unsigned char[4 * texSize * texSize * texSize],
342 osg::Image::USE_NEW_DELETE);
344 const int startFrequency = 4;
345 const int numOctaves = 4;
349 double inci, incj, inck;
350 int frequency = startFrequency;
354 osg::notify(osg::WARN) << "creating 3D noise texture... ";
356 for (f = 0, inc = 0; f < numOctaves; ++f, frequency *= 2, ++inc, amp *= 0.5)
358 SetNoiseFrequency(frequency);
360 ni[0] = ni[1] = ni[2] = 0;
362 inci = 1.0 / (texSize / frequency);
363 for (i = 0; i < texSize; ++i, ni[0] += inci)
365 incj = 1.0 / (texSize / frequency);
366 for (j = 0; j < texSize; ++j, ni[1] += incj)
368 inck = 1.0 / (texSize / frequency);
369 for (k = 0; k < texSize; ++k, ni[2] += inck, ptr += 4)
371 *(ptr+inc) = (GLubyte) (((noise3(ni) + 1.0) * amp) * 128.0);
377 osg::notify(osg::WARN) << "DONE" << std::endl;
381 class NoiseBuilder : public TextureBuilder
384 Texture* build(Effect* effect, const SGPropertyNode*,
385 const SGReaderWriterXMLOptions* options);
387 typedef map<int, ref_ptr<Texture3D> > NoiseMap;
391 Texture* NoiseBuilder::build(Effect* effect, const SGPropertyNode* props,
392 const SGReaderWriterXMLOptions* options)
395 const SGPropertyNode* sizeProp = getEffectPropertyChild(effect, props,
398 texSize = sizeProp->getValue<int>();
399 NoiseMap::iterator itr = _noises.find(texSize);
400 if (itr != _noises.end())
401 return itr->second.get();
402 Texture3D* noiseTexture = new osg::Texture3D;
403 noiseTexture->setFilter(osg::Texture3D::MIN_FILTER, osg::Texture3D::LINEAR);
404 noiseTexture->setFilter(osg::Texture3D::MAG_FILTER, osg::Texture3D::LINEAR);
405 noiseTexture->setWrap(osg::Texture3D::WRAP_S, osg::Texture3D::REPEAT);
406 noiseTexture->setWrap(osg::Texture3D::WRAP_T, osg::Texture3D::REPEAT);
407 noiseTexture->setWrap(osg::Texture3D::WRAP_R, osg::Texture3D::REPEAT);
408 noiseTexture->setImage( make3DNoiseImage(texSize) );
409 _noises.insert(make_pair(texSize, noiseTexture));
415 TextureBuilder::Registrar installNoise("noise", new NoiseBuilder);
420 // Image names for all sides
421 typedef boost::tuple<string, string, string, string, string, string> CubeMapTuple;
423 CubeMapTuple makeCubeMapTuple(Effect* effect, const SGPropertyNode* props)
425 const SGPropertyNode* ep = 0;
428 if ((ep = getEffectPropertyChild(effect, props, "positive-x")))
429 positive_x = ep->getStringValue();
431 if ((ep = getEffectPropertyChild(effect, props, "negative-x")))
432 negative_x = ep->getStringValue();
434 if ((ep = getEffectPropertyChild(effect, props, "positive-y")))
435 positive_y = ep->getStringValue();
437 if ((ep = getEffectPropertyChild(effect, props, "negative-y")))
438 negative_y = ep->getStringValue();
440 if ((ep = getEffectPropertyChild(effect, props, "positive-z")))
441 positive_z = ep->getStringValue();
443 if ((ep = getEffectPropertyChild(effect, props, "negative-z")))
444 negative_z = ep->getStringValue();
445 return CubeMapTuple(positive_x, negative_x, positive_y, negative_y, positive_z, negative_z);
449 class CubeMapBuilder : public TextureBuilder
452 Texture* build(Effect* effect, const SGPropertyNode*,
453 const SGReaderWriterXMLOptions* options);
455 typedef map<CubeMapTuple, ref_ptr<TextureCubeMap> > CubeMap;
456 typedef map<string, ref_ptr<TextureCubeMap> > CrossCubeMap;
458 CrossCubeMap _crossmaps;
461 // I use this until osg::CopyImage is fixed
462 // This one assumes images are the same format and sizes are correct
463 void copySubImage(const osg::Image* srcImage, int src_s, int src_t, int width, int height,
464 osg::Image* destImage, int dest_s, int dest_t)
466 for(int row = 0; row<height; ++row)
468 const unsigned char* srcData = srcImage->data(src_s, src_t+row, 0);
469 unsigned char* destData = destImage->data(dest_s, dest_t+row, 0);
470 memcpy(destData, srcData, (width*destImage->getPixelSizeInBits())/8);
475 Texture* CubeMapBuilder::build(Effect* effect, const SGPropertyNode* props,
476 const SGReaderWriterXMLOptions* options)
478 // First check that there is a <images> tag
479 const SGPropertyNode* texturesProp = getEffectPropertyChild(effect, props, "images");
480 const SGPropertyNode* crossProp = getEffectPropertyChild(effect, props, "image");
481 if (!texturesProp && !crossProp) {
482 throw BuilderException("no images defined for cube map");
483 return NULL; // This is redundant
486 // Using 6 separate images
489 SG_LOG(SG_INPUT, SG_DEBUG, "try 6 images ");
491 CubeMapTuple _tuple = makeCubeMapTuple(effect, texturesProp);
493 CubeMap::iterator itr = _cubemaps.find(_tuple);
494 if (itr != _cubemaps.end())
495 return itr->second.get();
497 TextureCubeMap* cubeTexture = new osg::TextureCubeMap;
499 // TODO: Read these from effect file? Maybe these are sane for all cuebmaps?
500 cubeTexture->setFilter(osg::Texture3D::MIN_FILTER, osg::Texture::LINEAR_MIPMAP_LINEAR);
501 cubeTexture->setFilter(osg::Texture3D::MAG_FILTER, osg::Texture::LINEAR);
502 cubeTexture->setWrap(osg::Texture3D::WRAP_S, osg::Texture::CLAMP_TO_EDGE);
503 cubeTexture->setWrap(osg::Texture3D::WRAP_T, osg::Texture::CLAMP_TO_EDGE);
504 cubeTexture->setWrap(osg::Texture3D::WRAP_R, osg::Texture::CLAMP_TO_EDGE);
506 osgDB::ReaderWriter::ReadResult result =
507 osgDB::Registry::instance()->readImage(_tuple.get<0>(), options);
508 if(result.success()) {
509 osg::Image* image = result.getImage();
510 cubeTexture->setImage(TextureCubeMap::POSITIVE_X, image);
512 result = osgDB::Registry::instance()->readImage(_tuple.get<1>(), options);
513 if(result.success()) {
514 osg::Image* image = result.getImage();
515 cubeTexture->setImage(TextureCubeMap::NEGATIVE_X, image);
517 result = osgDB::Registry::instance()->readImage(_tuple.get<2>(), options);
518 if(result.success()) {
519 osg::Image* image = result.getImage();
520 cubeTexture->setImage(TextureCubeMap::POSITIVE_Y, image);
522 result = osgDB::Registry::instance()->readImage(_tuple.get<3>(), options);
523 if(result.success()) {
524 osg::Image* image = result.getImage();
525 cubeTexture->setImage(TextureCubeMap::NEGATIVE_Y, image);
527 result = osgDB::Registry::instance()->readImage(_tuple.get<4>(), options);
528 if(result.success()) {
529 osg::Image* image = result.getImage();
530 cubeTexture->setImage(TextureCubeMap::POSITIVE_Z, image);
532 result = osgDB::Registry::instance()->readImage(_tuple.get<5>(), options);
533 if(result.success()) {
534 osg::Image* image = result.getImage();
535 cubeTexture->setImage(TextureCubeMap::NEGATIVE_Z, image);
538 _cubemaps[_tuple] = cubeTexture;
544 // Using 1 cross image
546 SG_LOG(SG_INPUT, SG_DEBUG, "try cross map ");
548 std::string texname = crossProp->getStringValue();
550 // Try to find existing cube map
551 CrossCubeMap::iterator itr = _crossmaps.find(texname);
552 if (itr != _crossmaps.end())
553 return itr->second.get();
555 osgDB::ReaderWriter::ReadResult result =
556 osgDB::Registry::instance()->readImage(texname, options);
557 if(result.success()) {
558 osg::Image* image = result.getImage();
559 image->flipVertical(); // Seems like the image coordinates are somewhat funny, flip to get better ones
561 //cubeTexture->setResizeNonPowerOfTwoHint(false);
563 // Size of a single image, 4 rows and 3 columns
564 int width = image->s() / 3;
565 int height = image->t() / 4;
566 int depth = image->r();
568 TextureCubeMap* cubeTexture = new osg::TextureCubeMap;
570 // Copy the 6 sub-images and push them
571 for(int n=0; n<6; n++) {
573 SG_LOG(SG_INPUT, SG_DEBUG, "Copying the " << n << "th sub-images and pushing it" );
575 osg::ref_ptr<osg::Image> subimg = new osg::Image();
576 subimg->allocateImage(width, height, depth, image->getPixelFormat(), image->getDataType()); // Copy attributes
578 // Choose correct image
581 copySubImage(image, width, 0, width, height, subimg.get(), 0, 0);
582 cubeTexture->setImage(TextureCubeMap::POSITIVE_Y, subimg.get());
583 cubeTexture->setWrap(osg::Texture3D::WRAP_S, osg::Texture::CLAMP_TO_EDGE);
584 cubeTexture->setWrap(osg::Texture3D::WRAP_T, osg::Texture::CLAMP_TO_EDGE);
585 cubeTexture->setWrap(osg::Texture3D::WRAP_R, osg::Texture::CLAMP_TO_EDGE);
588 copySubImage(image, 0, height, width, height, subimg.get(), 0, 0);
589 cubeTexture->setImage(TextureCubeMap::NEGATIVE_X, subimg.get());
590 cubeTexture->setWrap(osg::Texture2D::WRAP_S, osg::Texture::CLAMP_TO_EDGE);
591 cubeTexture->setWrap(osg::Texture3D::WRAP_T, osg::Texture::CLAMP_TO_EDGE);
592 cubeTexture->setWrap(osg::Texture3D::WRAP_R, osg::Texture::CLAMP_TO_EDGE);
595 copySubImage(image, width, height, width, height, subimg.get(), 0, 0);
596 cubeTexture->setImage(TextureCubeMap::POSITIVE_Z, subimg.get());
597 cubeTexture->setWrap(osg::Texture3D::WRAP_S, osg::Texture::CLAMP_TO_EDGE);
598 cubeTexture->setWrap(osg::Texture3D::WRAP_T, osg::Texture::CLAMP_TO_EDGE);
599 cubeTexture->setWrap(osg::Texture3D::WRAP_R, osg::Texture::CLAMP_TO_EDGE);
602 copySubImage(image, width*2, height, width, height, subimg.get(), 0, 0);
603 cubeTexture->setImage(TextureCubeMap::POSITIVE_X, subimg.get());
604 cubeTexture->setWrap(osg::Texture3D::WRAP_S, osg::Texture::CLAMP_TO_EDGE);
605 cubeTexture->setWrap(osg::Texture3D::WRAP_T, osg::Texture::CLAMP_TO_EDGE);
606 cubeTexture->setWrap(osg::Texture3D::WRAP_R, osg::Texture::CLAMP_TO_EDGE);
609 copySubImage(image, width, height*2, width, height, subimg.get(), 0, 0);
610 cubeTexture->setImage(TextureCubeMap::NEGATIVE_Y, subimg.get());
611 cubeTexture->setWrap(osg::Texture3D::WRAP_S, osg::Texture::CLAMP_TO_EDGE);
612 cubeTexture->setWrap(osg::Texture3D::WRAP_T, osg::Texture::CLAMP_TO_EDGE);
613 cubeTexture->setWrap(osg::Texture3D::WRAP_R, osg::Texture::CLAMP_TO_EDGE);
616 copySubImage(image, width, height*3, width, height, subimg.get(), 0, 0);
617 cubeTexture->setImage(TextureCubeMap::NEGATIVE_Z, subimg.get());
618 cubeTexture->setWrap(osg::Texture3D::WRAP_S, osg::Texture::CLAMP_TO_EDGE);
619 cubeTexture->setWrap(osg::Texture3D::WRAP_T, osg::Texture::CLAMP_TO_EDGE);
620 cubeTexture->setWrap(osg::Texture3D::WRAP_R, osg::Texture::CLAMP_TO_EDGE);
626 _crossmaps[texname] = cubeTexture;
631 throw BuilderException("Could not load cube cross");
639 TextureBuilder::Registrar installCubeMap("cubemap", new CubeMapBuilder);
642 EffectNameValue<TexEnvCombine::CombineParam> combineParamInit[] =
644 {"replace", TexEnvCombine::REPLACE},
645 {"modulate", TexEnvCombine::MODULATE},
646 {"add", TexEnvCombine::ADD},
647 {"add-signed", TexEnvCombine::ADD_SIGNED},
648 {"interpolate", TexEnvCombine::INTERPOLATE},
649 {"subtract", TexEnvCombine::SUBTRACT},
650 {"dot3-rgb", TexEnvCombine::DOT3_RGB},
651 {"dot3-rgba", TexEnvCombine::DOT3_RGBA}
654 EffectPropertyMap<TexEnvCombine::CombineParam> combineParams(combineParamInit);
656 EffectNameValue<TexEnvCombine::SourceParam> sourceParamInit[] =
658 {"constant", TexEnvCombine::CONSTANT},
659 {"primary_color", TexEnvCombine::PRIMARY_COLOR},
660 {"previous", TexEnvCombine::PREVIOUS},
661 {"texture", TexEnvCombine::TEXTURE},
662 {"texture0", TexEnvCombine::TEXTURE0},
663 {"texture1", TexEnvCombine::TEXTURE1},
664 {"texture2", TexEnvCombine::TEXTURE2},
665 {"texture3", TexEnvCombine::TEXTURE3},
666 {"texture4", TexEnvCombine::TEXTURE4},
667 {"texture5", TexEnvCombine::TEXTURE5},
668 {"texture6", TexEnvCombine::TEXTURE6},
669 {"texture7", TexEnvCombine::TEXTURE7}
672 EffectPropertyMap<TexEnvCombine::SourceParam> sourceParams(sourceParamInit);
674 EffectNameValue<TexEnvCombine::OperandParam> opParamInit[] =
676 {"src-color", TexEnvCombine::SRC_COLOR},
677 {"one-minus-src-color", TexEnvCombine::ONE_MINUS_SRC_COLOR},
678 {"src-alpha", TexEnvCombine::SRC_ALPHA},
679 {"one-minus-src-alpha", TexEnvCombine::ONE_MINUS_SRC_ALPHA}
682 EffectPropertyMap<TexEnvCombine::OperandParam> operandParams(opParamInit);
684 TexEnvCombine* buildTexEnvCombine(Effect* effect, const SGPropertyNode* envProp,
685 const SGReaderWriterXMLOptions* options)
687 if (!isAttributeActive(effect, envProp))
689 TexEnvCombine* result = new TexEnvCombine;
690 const SGPropertyNode* p = 0;
691 if ((p = getEffectPropertyChild(effect, envProp, "combine-rgb"))) {
692 TexEnvCombine::CombineParam crgb = TexEnvCombine::MODULATE;
693 findAttr(combineParams, p, crgb);
694 result->setCombine_RGB(crgb);
696 if ((p = getEffectPropertyChild(effect, envProp, "combine-alpha"))) {
697 TexEnvCombine::CombineParam calpha = TexEnvCombine::MODULATE;
698 findAttr(combineParams, p, calpha);
699 result->setCombine_Alpha(calpha);
701 if ((p = getEffectPropertyChild(effect, envProp, "source0-rgb"))) {
702 TexEnvCombine::SourceParam source = TexEnvCombine::TEXTURE;
703 findAttr(sourceParams, p, source);
704 result->setSource0_RGB(source);
706 if ((p = getEffectPropertyChild(effect, envProp, "source1-rgb"))) {
707 TexEnvCombine::SourceParam source = TexEnvCombine::PREVIOUS;
708 findAttr(sourceParams, p, source);
709 result->setSource1_RGB(source);
711 if ((p = getEffectPropertyChild(effect, envProp, "source2-rgb"))) {
712 TexEnvCombine::SourceParam source = TexEnvCombine::CONSTANT;
713 findAttr(sourceParams, p, source);
714 result->setSource2_RGB(source);
716 if ((p = getEffectPropertyChild(effect, envProp, "source0-alpha"))) {
717 TexEnvCombine::SourceParam source = TexEnvCombine::TEXTURE;
718 findAttr(sourceParams, p, source);
719 result->setSource0_Alpha(source);
721 if ((p = getEffectPropertyChild(effect, envProp, "source1-alpha"))) {
722 TexEnvCombine::SourceParam source = TexEnvCombine::PREVIOUS;
723 findAttr(sourceParams, p, source);
724 result->setSource1_Alpha(source);
726 if ((p = getEffectPropertyChild(effect, envProp, "source2-alpha"))) {
727 TexEnvCombine::SourceParam source = TexEnvCombine::CONSTANT;
728 findAttr(sourceParams, p, source);
729 result->setSource2_Alpha(source);
731 if ((p = getEffectPropertyChild(effect, envProp, "operand0-rgb"))) {
732 TexEnvCombine::OperandParam op = TexEnvCombine::SRC_COLOR;
733 findAttr(operandParams, p, op);
734 result->setOperand0_RGB(op);
736 if ((p = getEffectPropertyChild(effect, envProp, "operand1-rgb"))) {
737 TexEnvCombine::OperandParam op = TexEnvCombine::SRC_COLOR;
738 findAttr(operandParams, p, op);
739 result->setOperand1_RGB(op);
741 if ((p = getEffectPropertyChild(effect, envProp, "operand2-rgb"))) {
742 TexEnvCombine::OperandParam op = TexEnvCombine::SRC_ALPHA;
743 findAttr(operandParams, p, op);
744 result->setOperand2_RGB(op);
746 if ((p = getEffectPropertyChild(effect, envProp, "operand0-alpha"))) {
747 TexEnvCombine::OperandParam op = TexEnvCombine::SRC_ALPHA;
748 findAttr(operandParams, p, op);
749 result->setOperand0_Alpha(op);
751 if ((p = getEffectPropertyChild(effect, envProp, "operand1-alpha"))) {
752 TexEnvCombine::OperandParam op = TexEnvCombine::SRC_ALPHA;
753 findAttr(operandParams, p, op);
754 result->setOperand1_Alpha(op);
756 if ((p = getEffectPropertyChild(effect, envProp, "operand2-alpha"))) {
757 TexEnvCombine::OperandParam op = TexEnvCombine::SRC_ALPHA;
758 findAttr(operandParams, p, op);
759 result->setOperand2_Alpha(op);
761 if ((p = getEffectPropertyChild(effect, envProp, "scale-rgb"))) {
762 result->setScale_RGB(p->getValue<float>());
764 if ((p = getEffectPropertyChild(effect, envProp, "scale-alpha"))) {
765 result->setScale_Alpha(p->getValue<float>());
768 if ((p = getEffectPropertyChild(effect, envProp, "constant-color"))) {
769 SGVec4d color = p->getValue<SGVec4d>();
770 result->setConstantColor(toOsg(color));
771 } else if ((p = getEffectPropertyChild(effect, envProp,
772 "light-direction"))) {
773 SGVec3d direction = p->getValue<SGVec3d>();
774 result->setConstantColorAsLightDirection(toOsg(direction));
777 const SGPropertyNode* colorNode = envProp->getChild("constant-color");
779 initFromParameters(effect, colorNode, result,
780 &TexEnvCombine::setConstantColor, colorFields,
785 EffectNameValue<TexGen::Mode> tgenModeInit[] =
787 { "object-linear", TexGen::OBJECT_LINEAR},
788 { "eye-linear", TexGen::EYE_LINEAR},
789 { "sphere-map", TexGen::SPHERE_MAP},
790 { "normal-map", TexGen::NORMAL_MAP},
791 { "reflection-map", TexGen::REFLECTION_MAP}
794 EffectPropertyMap<TexGen::Mode> tgenModes(tgenModeInit);
796 EffectNameValue<TexGen::Coord> tgenCoordInit[] =
804 EffectPropertyMap<TexGen::Coord> tgenCoords(tgenCoordInit);
806 TexGen* buildTexGen(Effect* effect, const SGPropertyNode* tgenProp)
808 if (!isAttributeActive(effect, tgenProp))
810 TexGen* result = new TexGen;
811 TexGen::Mode mode = TexGen::OBJECT_LINEAR;
812 findAttr(tgenModes, getEffectPropertyChild(effect, tgenProp, "mode"), mode);
813 result->setMode(mode);
814 const SGPropertyNode* planesNode = tgenProp->getChild("planes");
816 for (int i = 0; i < planesNode->nChildren(); ++i) {
817 const SGPropertyNode* planeNode = planesNode->getChild(i);
819 findAttr(tgenCoords, planeNode->getName(), coord);
820 const SGPropertyNode* realNode
821 = getEffectPropertyNode(effect, planeNode);
822 SGVec4d plane = realNode->getValue<SGVec4d>();
823 result->setPlane(coord, toOsg(plane));
829 bool makeTextureParameters(SGPropertyNode* paramRoot, const StateSet* ss)
831 SGPropertyNode* texUnit = makeChild(paramRoot, "texture");
832 const Texture* tex = getStateAttribute<Texture>(0, ss);
833 const Texture2D* texture = dynamic_cast<const Texture2D*>(tex);
834 makeChild(texUnit, "unit")->setValue(0);
836 // The default shader-based technique ignores active
837 makeChild(texUnit, "active")->setValue(false);
840 const Image* image = texture->getImage();
843 imageName = image->getFileName();
845 makeChild(texUnit, "active")->setValue(false);
846 makeChild(texUnit, "type")->setValue("white");
849 makeChild(texUnit, "active")->setValue(true);
850 makeChild(texUnit, "type")->setValue("2d");
851 string filter = findName(filterModes,
852 texture->getFilter(Texture::MIN_FILTER));
853 string magFilter = findName(filterModes,
854 texture->getFilter(Texture::MAG_FILTER));
855 string wrapS = findName(wrapModes, texture->getWrap(Texture::WRAP_S));
856 string wrapT = findName(wrapModes, texture->getWrap(Texture::WRAP_T));
857 string wrapR = findName(wrapModes, texture->getWrap(Texture::WRAP_R));
858 makeChild(texUnit, "image")->setStringValue(imageName);
859 makeChild(texUnit, "filter")->setStringValue(filter);
860 makeChild(texUnit, "mag-filter")->setStringValue(magFilter);
861 makeChild(texUnit, "wrap-s")->setStringValue(wrapS);
862 makeChild(texUnit, "wrap-t")->setStringValue(wrapT);
863 makeChild(texUnit, "wrap-r")->setStringValue(wrapR);