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/PointSprite>
28 #include <osg/TexEnvCombine>
30 #include <osg/Texture1D>
31 #include <osg/Texture2D>
32 #include <osg/Texture3D>
33 #include <osg/TextureRectangle>
34 #include <osg/TextureCubeMap>
35 #include <osgDB/FileUtils>
36 #include <osgDB/ReadFile>
38 #include <OpenThreads/Mutex>
39 #include <OpenThreads/ScopedLock>
41 #include <boost/lexical_cast.hpp>
42 #include <boost/tuple/tuple.hpp>
43 #include <boost/tuple/tuple_comparison.hpp>
45 #include <simgear/scene/util/OsgMath.hxx>
46 #include <simgear/scene/util/SGReaderWriterOptions.hxx>
47 #include <simgear/scene/util/SGSceneFeatures.hxx>
48 #include <simgear/scene/util/StateAttributeFactory.hxx>
49 #include <simgear/structure/OSGUtils.hxx>
50 #include <simgear/props/vectorPropTemplates.hxx>
55 using OpenThreads::Mutex;
56 using OpenThreads::ScopedLock;
61 using namespace effect;
63 TexEnvCombine* buildTexEnvCombine(Effect* effect,
64 const SGPropertyNode* envProp,
65 const SGReaderWriterOptions* options);
66 TexGen* buildTexGen(Effect* Effect, const SGPropertyNode* tgenProp);
68 // Hack to force inclusion of TextureBuilder.cxx in library
69 osg::Texture* TextureBuilder::buildFromType(Effect* effect, Pass* pass, const string& type,
70 const SGPropertyNode*props,
71 const SGReaderWriterOptions*
74 return EffectBuilder<Texture>::buildFromType(effect, pass, type, props, options);
77 typedef boost::tuple<string, Texture::FilterMode, Texture::FilterMode,
78 Texture::WrapMode, Texture::WrapMode, Texture::WrapMode,
79 string, MipMapTuple> TexTuple;
81 EffectNameValue<TexEnv::Mode> texEnvModesInit[] =
84 {"blend", TexEnv::BLEND},
85 {"decal", TexEnv::DECAL},
86 {"modulate", TexEnv::MODULATE},
87 {"replace", TexEnv::REPLACE}
89 EffectPropertyMap<TexEnv::Mode> texEnvModes(texEnvModesInit);
91 TexEnv* buildTexEnv(Effect* effect, const SGPropertyNode* prop)
93 const SGPropertyNode* modeProp = getEffectPropertyChild(effect, prop,
95 const SGPropertyNode* colorProp = getEffectPropertyChild(effect, prop,
99 TexEnv::Mode mode = TexEnv::MODULATE;
100 findAttr(texEnvModes, modeProp, mode);
101 if (mode == TexEnv::MODULATE) {
102 return StateAttributeFactory::instance()->getStandardTexEnv();
104 TexEnv* env = new TexEnv(mode);
106 env->setColor(toOsg(colorProp->getValue<SGVec4d>()));
111 void TextureUnitBuilder::buildAttribute(Effect* effect, Pass* pass,
112 const SGPropertyNode* prop,
113 const SGReaderWriterOptions* options)
115 if (!isAttributeActive(effect, prop))
117 // Decode the texture unit
119 const SGPropertyNode* pUnit = prop->getChild("unit");
121 unit = pUnit->getValue<int>();
123 const SGPropertyNode* pName = prop->getChild("name");
126 unit = boost::lexical_cast<int>(pName->getStringValue());
127 } catch (boost::bad_lexical_cast& lex) {
128 SG_LOG(SG_INPUT, SG_ALERT, "can't decode name as texture unit "
132 const SGPropertyNode* pType = getEffectPropertyChild(effect, prop, "type");
137 type = pType->getStringValue();
138 Texture* texture = 0;
140 texture = TextureBuilder::buildFromType(effect, pass, type, prop,
143 catch (BuilderException& e) {
144 SG_LOG(SG_INPUT, SG_DEBUG, e.getFormattedMessage() << ", "
145 << "maybe the reader did not set the filename attribute, "
146 << "using white for type '" << type << "' on '" << pass->getName() << "', in " << prop->getPath() );
147 texture = StateAttributeFactory::instance()->getWhiteTexture();
150 const SGPropertyNode* pPoint = getEffectPropertyChild(effect, prop, "point-sprite");
151 if (pPoint && pPoint->getBoolValue()) {
152 ref_ptr<PointSprite> pointSprite = new PointSprite;
153 pass->setTextureAttributeAndModes(unit, pointSprite.get(), osg::StateAttribute::ON);
156 pass->setTextureAttributeAndModes(unit, texture);
158 const SGPropertyNode* envProp = prop->getChild("environment");
160 TexEnv* env = buildTexEnv(effect, envProp);
162 pass->setTextureAttributeAndModes(unit, env);
164 const SGPropertyNode* combineProp = prop->getChild("texenv-combine");
165 TexEnvCombine* combiner = 0;
166 if (combineProp && ((combiner = buildTexEnvCombine(effect, combineProp,
168 pass->setTextureAttributeAndModes(unit, combiner);
169 const SGPropertyNode* tgenProp = prop->getChild("texgen");
171 if (tgenProp && (tgen = buildTexGen(effect, tgenProp)))
172 pass->setTextureAttributeAndModes(unit, tgen);
175 // InstallAttributeBuilder call is in Effect.cxx to force this file to
180 EffectNameValue<Texture::FilterMode> filterModesInit[] =
182 { "linear", Texture::LINEAR },
183 { "linear-mipmap-linear", Texture::LINEAR_MIPMAP_LINEAR},
184 { "linear-mipmap-nearest", Texture::LINEAR_MIPMAP_NEAREST},
185 { "nearest", Texture::NEAREST},
186 { "nearest-mipmap-linear", Texture::NEAREST_MIPMAP_LINEAR},
187 { "nearest-mipmap-nearest", Texture::NEAREST_MIPMAP_NEAREST}
189 EffectPropertyMap<Texture::FilterMode> filterModes(filterModesInit);
191 EffectNameValue<Texture::WrapMode> wrapModesInit[] =
193 {"clamp", Texture::CLAMP},
194 {"clamp-to-border", Texture::CLAMP_TO_BORDER},
195 {"clamp-to-edge", Texture::CLAMP_TO_EDGE},
196 {"mirror", Texture::MIRROR},
197 {"repeat", Texture::REPEAT}
199 EffectPropertyMap<Texture::WrapMode> wrapModes(wrapModesInit);
201 TexTuple makeTexTuple(Effect* effect, const SGPropertyNode* props,
202 const SGReaderWriterOptions* options,
203 const string& texType)
205 Texture::FilterMode minFilter = Texture::LINEAR_MIPMAP_LINEAR;
206 const SGPropertyNode* ep = 0;
207 if ((ep = getEffectPropertyChild(effect, props, "filter")))
208 findAttr(filterModes, ep, minFilter);
209 Texture::FilterMode magFilter = Texture::LINEAR;
210 if ((ep = getEffectPropertyChild(effect, props, "mag-filter")))
211 findAttr(filterModes, ep, magFilter);
212 const SGPropertyNode* pWrapS
213 = getEffectPropertyChild(effect, props, "wrap-s");
214 Texture::WrapMode sWrap = Texture::CLAMP;
216 findAttr(wrapModes, pWrapS, sWrap);
217 const SGPropertyNode* pWrapT
218 = getEffectPropertyChild(effect, props, "wrap-t");
219 Texture::WrapMode tWrap = Texture::CLAMP;
221 findAttr(wrapModes, pWrapT, tWrap);
222 const SGPropertyNode* pWrapR
223 = getEffectPropertyChild(effect, props, "wrap-r");
224 Texture::WrapMode rWrap = Texture::CLAMP;
226 findAttr(wrapModes, pWrapR, rWrap);
227 const SGPropertyNode* pImage
228 = getEffectPropertyChild(effect, props, "image");
233 imageName = pImage->getStringValue();
234 absFileName = SGModelLib::findDataFile(imageName, options);
235 if (absFileName.empty())
237 SG_LOG(SG_INPUT, SG_ALERT, "Texture file not found: '"
238 << imageName << "'");
242 const SGPropertyNode* pMipmapControl
243 = getEffectPropertyChild(effect, props, "mipmap-control");
244 MipMapTuple mipmapFunctions( AUTOMATIC, AUTOMATIC, AUTOMATIC, AUTOMATIC );
245 if ( pMipmapControl )
246 mipmapFunctions = makeMipMapTuple(effect, pMipmapControl, options);
248 return TexTuple(absFileName, minFilter, magFilter, sWrap, tWrap, rWrap,
249 texType, mipmapFunctions);
252 bool setAttrs(const TexTuple& attrs, Texture* tex,
253 const SGReaderWriterOptions* options)
255 const string& imageName = attrs.get<0>();
256 if (imageName.empty())
259 osgDB::ReaderWriter::ReadResult result;
260 result = osgDB::readImageFile(imageName, options);
261 osg::ref_ptr<osg::Image> image;
262 if (result.success())
263 image = result.getImage();
266 image = computeMipmap( image.get(), attrs.get<7>() );
267 tex->setImage(GL_FRONT_AND_BACK, image.get());
270 if (s <= t && 32 <= s) {
271 SGSceneFeatures::instance()->setTextureCompression(tex);
272 } else if (t < s && 32 <= t) {
273 SGSceneFeatures::instance()->setTextureCompression(tex);
275 tex->setMaxAnisotropy(SGSceneFeatures::instance()
276 ->getTextureFilter());
278 SG_LOG(SG_INPUT, SG_ALERT, "failed to load effect texture file "
283 // texture->setDataVariance(osg::Object::STATIC);
284 tex->setFilter(Texture::MIN_FILTER, attrs.get<1>());
285 tex->setFilter(Texture::MAG_FILTER, attrs.get<2>());
286 tex->setWrap(Texture::WRAP_S, attrs.get<3>());
287 tex->setWrap(Texture::WRAP_T, attrs.get<4>());
288 tex->setWrap(Texture::WRAP_R, attrs.get<5>());
292 } // of anonymous namespace
295 class TexBuilder : public TextureBuilder
298 TexBuilder(const string& texType) : _type(texType) {}
299 Texture* build(Effect* effect, Pass* pass, const SGPropertyNode*,
300 const SGReaderWriterOptions* options);
302 typedef map<TexTuple, observer_ptr<T> > TexMap;
308 Texture* TexBuilder<T>::build(Effect* effect, Pass* pass, const SGPropertyNode* props,
309 const SGReaderWriterOptions* options)
311 TexTuple attrs = makeTexTuple(effect, props, options, _type);
312 typename TexMap::iterator itr = texMap.find(attrs);
315 if ((itr != texMap.end())&&
316 (itr->second.lock(tex)))
318 return tex.release();
322 if (!setAttrs(attrs, tex, options))
325 if (itr == texMap.end())
326 texMap.insert(make_pair(attrs, tex));
328 itr->second = tex; // update existing, but empty observer
329 return tex.release();
335 TextureBuilder::Registrar install1D("1d", new TexBuilder<Texture1D>("1d"));
336 TextureBuilder::Registrar install2D("2d", new TexBuilder<Texture2D>("2d"));
337 TextureBuilder::Registrar install3D("3d", new TexBuilder<Texture3D>("3d"));
340 class WhiteTextureBuilder : public TextureBuilder
343 Texture* build(Effect* effect, Pass* pass, const SGPropertyNode*,
344 const SGReaderWriterOptions* options);
347 Texture* WhiteTextureBuilder::build(Effect* effect, Pass* pass, const SGPropertyNode*,
348 const SGReaderWriterOptions* options)
350 return StateAttributeFactory::instance()->getWhiteTexture();
355 TextureBuilder::Registrar installWhite("white", new WhiteTextureBuilder);
358 class TransparentTextureBuilder : public TextureBuilder
361 Texture* build(Effect* effect, Pass* pass, const SGPropertyNode*,
362 const SGReaderWriterOptions* options);
365 Texture* TransparentTextureBuilder::build(Effect* effect, Pass* pass, const SGPropertyNode*,
366 const SGReaderWriterOptions* options)
368 return StateAttributeFactory::instance()->getTransparentTexture();
373 TextureBuilder::Registrar installTransparent("transparent",
374 new TransparentTextureBuilder);
377 class NoiseBuilder : public TextureBuilder
380 Texture* build(Effect* effect, Pass* pass, const SGPropertyNode*,
381 const SGReaderWriterOptions* options);
383 typedef map<int, ref_ptr<Texture3D> > NoiseMap;
387 Texture* NoiseBuilder::build(Effect* effect, Pass* pass, const SGPropertyNode* props,
388 const SGReaderWriterOptions* options)
391 const SGPropertyNode* sizeProp = getEffectPropertyChild(effect, props,
394 texSize = sizeProp->getValue<int>();
396 return StateAttributeFactory::instance()->getNoiseTexture(texSize);
401 TextureBuilder::Registrar installNoise("noise", new NoiseBuilder);
404 class LightSpriteBuilder : public TextureBuilder
407 Texture* build(Effect* effect, Pass* pass, const SGPropertyNode*,
408 const SGReaderWriterOptions* options);
411 void setPointSpriteImage(unsigned char* data, unsigned log2resolution,
412 unsigned charsPerPixel);
413 osg::Image* getPointSpriteImage(int logResolution);
416 Texture* LightSpriteBuilder::build(Effect* effect, Pass* pass, const SGPropertyNode* props,
417 const SGReaderWriterOptions* options)
419 ScopedLock<Mutex> lock(lightMutex);
421 // Always called from when the lightMutex is already taken
422 static osg::ref_ptr<osg::Texture2D> texture;
425 return texture.get();
427 texture = new osg::Texture2D;
428 texture->setImage(getPointSpriteImage(6));
429 texture->setWrap(osg::Texture::WRAP_S, osg::Texture::CLAMP);
430 texture->setWrap(osg::Texture::WRAP_T, osg::Texture::CLAMP);
432 return texture.get();
436 LightSpriteBuilder::setPointSpriteImage(unsigned char* data, unsigned log2resolution,
437 unsigned charsPerPixel)
439 int env_tex_res = (1 << log2resolution);
440 for (int i = 0; i < env_tex_res; ++i) {
441 for (int j = 0; j < env_tex_res; ++j) {
442 int xi = 2*i + 1 - env_tex_res;
443 int yi = 2*j + 1 - env_tex_res;
457 float x = 1.5*xi/(float)(env_tex_res);
458 float y = 1.5*yi/(float)(env_tex_res);
459 // float x = 2*xi/(float)(env_tex_res);
460 // float y = 2*yi/(float)(env_tex_res);
461 float dist = sqrt(x*x + y*y);
462 float bright = SGMiscf::clip(255*(1-dist), 0, 255);
463 for (unsigned l = 0; l < charsPerPixel; ++l)
464 data[charsPerPixel*(i*env_tex_res + j) + l] = (unsigned char)bright;
470 LightSpriteBuilder::getPointSpriteImage(int logResolution)
472 osg::Image* image = new osg::Image;
474 osg::Image::MipmapDataType mipmapOffsets;
476 for (int i = logResolution; 0 <= i; --i) {
477 unsigned res = 1 << i;
479 mipmapOffsets.push_back(off);
482 int env_tex_res = (1 << logResolution);
484 unsigned char* imageData = new unsigned char[off];
485 image->setImage(env_tex_res, env_tex_res, 1,
486 GL_ALPHA, GL_ALPHA, GL_UNSIGNED_BYTE, imageData,
487 osg::Image::USE_NEW_DELETE);
488 image->setMipmapLevels(mipmapOffsets);
490 for (int k = logResolution; 0 <= k; --k) {
491 setPointSpriteImage(image->getMipmapData(logResolution - k), k, 1);
499 TextureBuilder::Registrar installLightSprite("light-sprite", new LightSpriteBuilder);
502 // Image names for all sides
503 typedef boost::tuple<string, string, string, string, string, string> CubeMapTuple;
505 CubeMapTuple makeCubeMapTuple(Effect* effect, const SGPropertyNode* props)
507 const SGPropertyNode* ep = 0;
510 if ((ep = getEffectPropertyChild(effect, props, "positive-x")))
511 positive_x = ep->getStringValue();
513 if ((ep = getEffectPropertyChild(effect, props, "negative-x")))
514 negative_x = ep->getStringValue();
516 if ((ep = getEffectPropertyChild(effect, props, "positive-y")))
517 positive_y = ep->getStringValue();
519 if ((ep = getEffectPropertyChild(effect, props, "negative-y")))
520 negative_y = ep->getStringValue();
522 if ((ep = getEffectPropertyChild(effect, props, "positive-z")))
523 positive_z = ep->getStringValue();
525 if ((ep = getEffectPropertyChild(effect, props, "negative-z")))
526 negative_z = ep->getStringValue();
527 return CubeMapTuple(positive_x, negative_x, positive_y, negative_y, positive_z, negative_z);
531 class CubeMapBuilder : public TextureBuilder
534 Texture* build(Effect* effect, Pass* pass, const SGPropertyNode*,
535 const SGReaderWriterOptions* options);
537 typedef map<CubeMapTuple, observer_ptr<TextureCubeMap> > CubeMap;
538 typedef map<string, observer_ptr<TextureCubeMap> > CrossCubeMap;
540 CrossCubeMap _crossmaps;
543 // I use this until osg::CopyImage is fixed
544 // This one assumes images are the same format and sizes are correct
545 void copySubImage(const osg::Image* srcImage, int src_s, int src_t, int width, int height,
546 osg::Image* destImage, int dest_s, int dest_t)
548 for(int row = 0; row<height; ++row)
550 const unsigned char* srcData = srcImage->data(src_s, src_t+row, 0);
551 unsigned char* destData = destImage->data(dest_s, dest_t+row, 0);
552 memcpy(destData, srcData, (width*destImage->getPixelSizeInBits())/8);
557 Texture* CubeMapBuilder::build(Effect* effect, Pass* pass, const SGPropertyNode* props,
558 const SGReaderWriterOptions* options)
560 // First check that there is a <images> tag
561 const SGPropertyNode* texturesProp = getEffectPropertyChild(effect, props, "images");
562 const SGPropertyNode* crossProp = getEffectPropertyChild(effect, props, "image");
563 if (!texturesProp && !crossProp) {
564 throw BuilderException("no images defined for cube map");
565 return NULL; // This is redundant
568 // Using 6 separate images
570 CubeMapTuple _tuple = makeCubeMapTuple(effect, texturesProp);
572 CubeMap::iterator itr = _cubemaps.find(_tuple);
573 ref_ptr<TextureCubeMap> cubeTexture;
575 if (itr != _cubemaps.end() && itr->second.lock(cubeTexture))
576 return cubeTexture.release();
578 cubeTexture = new osg::TextureCubeMap;
580 // TODO: Read these from effect file? Maybe these are sane for all cuebmaps?
581 cubeTexture->setFilter(osg::Texture3D::MIN_FILTER, osg::Texture::LINEAR_MIPMAP_LINEAR);
582 cubeTexture->setFilter(osg::Texture3D::MAG_FILTER, osg::Texture::LINEAR);
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);
587 osgDB::ReaderWriter::ReadResult result;
588 result = osgDB::readImageFile(_tuple.get<0>(), options);
589 if(result.success()) {
590 osg::Image* image = result.getImage();
591 cubeTexture->setImage(TextureCubeMap::POSITIVE_X, image);
593 result = osgDB::readImageFile(_tuple.get<1>(), options);
594 if(result.success()) {
595 osg::Image* image = result.getImage();
596 cubeTexture->setImage(TextureCubeMap::NEGATIVE_X, image);
598 result = osgDB::readImageFile(_tuple.get<2>(), options);
599 if(result.success()) {
600 osg::Image* image = result.getImage();
601 cubeTexture->setImage(TextureCubeMap::POSITIVE_Y, image);
603 result = osgDB::readImageFile(_tuple.get<3>(), options);
604 if(result.success()) {
605 osg::Image* image = result.getImage();
606 cubeTexture->setImage(TextureCubeMap::NEGATIVE_Y, image);
608 result = osgDB::readImageFile(_tuple.get<4>(), options);
609 if(result.success()) {
610 osg::Image* image = result.getImage();
611 cubeTexture->setImage(TextureCubeMap::POSITIVE_Z, image);
613 result = osgDB::readImageFile(_tuple.get<5>(), options);
614 if(result.success()) {
615 osg::Image* image = result.getImage();
616 cubeTexture->setImage(TextureCubeMap::NEGATIVE_Z, image);
619 if (itr == _cubemaps.end())
620 _cubemaps[_tuple] = cubeTexture;
622 itr->second = cubeTexture; // update existing
623 return cubeTexture.release();
626 // Using 1 cross image
628 std::string texname = crossProp->getStringValue();
630 // Try to find existing cube map
631 ref_ptr<TextureCubeMap> cubeTexture;
632 CrossCubeMap::iterator itr = _crossmaps.find(texname);
633 if ((itr != _crossmaps.end()) && itr->second.lock(cubeTexture))
634 return cubeTexture.release();
636 osgDB::ReaderWriter::ReadResult result;
637 result = osgDB::readImageFile(texname, options);
638 if(result.success()) {
639 osg::Image* image = result.getImage();
640 image->flipVertical(); // Seems like the image coordinates are somewhat funny, flip to get better ones
642 //cubeTexture->setResizeNonPowerOfTwoHint(false);
644 // Size of a single image, 4 rows and 3 columns
645 int width = image->s() / 3;
646 int height = image->t() / 4;
647 int depth = image->r();
649 cubeTexture = new osg::TextureCubeMap;
651 // Copy the 6 sub-images and push them
652 for(int n=0; n<6; n++) {
654 SG_LOG(SG_INPUT, SG_DEBUG, "Copying the " << n << "th sub-images and pushing it" );
656 osg::ref_ptr<osg::Image> subimg = new osg::Image();
657 subimg->allocateImage(width, height, depth, image->getPixelFormat(), image->getDataType()); // Copy attributes
659 // Choose correct image
662 copySubImage(image, width, 0, width, height, subimg.get(), 0, 0);
663 cubeTexture->setImage(TextureCubeMap::POSITIVE_Y, subimg.get());
664 cubeTexture->setWrap(osg::Texture3D::WRAP_S, osg::Texture::CLAMP_TO_EDGE);
665 cubeTexture->setWrap(osg::Texture3D::WRAP_T, osg::Texture::CLAMP_TO_EDGE);
666 cubeTexture->setWrap(osg::Texture3D::WRAP_R, osg::Texture::CLAMP_TO_EDGE);
669 copySubImage(image, 0, height, width, height, subimg.get(), 0, 0);
670 cubeTexture->setImage(TextureCubeMap::NEGATIVE_X, subimg.get());
671 cubeTexture->setWrap(osg::Texture2D::WRAP_S, osg::Texture::CLAMP_TO_EDGE);
672 cubeTexture->setWrap(osg::Texture3D::WRAP_T, osg::Texture::CLAMP_TO_EDGE);
673 cubeTexture->setWrap(osg::Texture3D::WRAP_R, osg::Texture::CLAMP_TO_EDGE);
676 copySubImage(image, width, height, width, height, subimg.get(), 0, 0);
677 cubeTexture->setImage(TextureCubeMap::POSITIVE_Z, subimg.get());
678 cubeTexture->setWrap(osg::Texture3D::WRAP_S, osg::Texture::CLAMP_TO_EDGE);
679 cubeTexture->setWrap(osg::Texture3D::WRAP_T, osg::Texture::CLAMP_TO_EDGE);
680 cubeTexture->setWrap(osg::Texture3D::WRAP_R, osg::Texture::CLAMP_TO_EDGE);
683 copySubImage(image, width*2, height, width, height, subimg.get(), 0, 0);
684 cubeTexture->setImage(TextureCubeMap::POSITIVE_X, subimg.get());
685 cubeTexture->setWrap(osg::Texture3D::WRAP_S, osg::Texture::CLAMP_TO_EDGE);
686 cubeTexture->setWrap(osg::Texture3D::WRAP_T, osg::Texture::CLAMP_TO_EDGE);
687 cubeTexture->setWrap(osg::Texture3D::WRAP_R, osg::Texture::CLAMP_TO_EDGE);
690 copySubImage(image, width, height*2, width, height, subimg.get(), 0, 0);
691 cubeTexture->setImage(TextureCubeMap::NEGATIVE_Y, subimg.get());
692 cubeTexture->setWrap(osg::Texture3D::WRAP_S, osg::Texture::CLAMP_TO_EDGE);
693 cubeTexture->setWrap(osg::Texture3D::WRAP_T, osg::Texture::CLAMP_TO_EDGE);
694 cubeTexture->setWrap(osg::Texture3D::WRAP_R, osg::Texture::CLAMP_TO_EDGE);
697 copySubImage(image, width, height*3, width, height, subimg.get(), 0, 0);
698 cubeTexture->setImage(TextureCubeMap::NEGATIVE_Z, subimg.get());
699 cubeTexture->setWrap(osg::Texture3D::WRAP_S, osg::Texture::CLAMP_TO_EDGE);
700 cubeTexture->setWrap(osg::Texture3D::WRAP_T, osg::Texture::CLAMP_TO_EDGE);
701 cubeTexture->setWrap(osg::Texture3D::WRAP_R, osg::Texture::CLAMP_TO_EDGE);
707 if (itr == _crossmaps.end())
708 _crossmaps[texname] = cubeTexture;
710 itr->second = cubeTexture; // update existing
711 return cubeTexture.release();
714 throw BuilderException("Could not load cube cross");
722 TextureBuilder::Registrar installCubeMap("cubemap", new CubeMapBuilder);
725 EffectNameValue<TexEnvCombine::CombineParam> combineParamInit[] =
727 {"replace", TexEnvCombine::REPLACE},
728 {"modulate", TexEnvCombine::MODULATE},
729 {"add", TexEnvCombine::ADD},
730 {"add-signed", TexEnvCombine::ADD_SIGNED},
731 {"interpolate", TexEnvCombine::INTERPOLATE},
732 {"subtract", TexEnvCombine::SUBTRACT},
733 {"dot3-rgb", TexEnvCombine::DOT3_RGB},
734 {"dot3-rgba", TexEnvCombine::DOT3_RGBA}
737 EffectPropertyMap<TexEnvCombine::CombineParam> combineParams(combineParamInit);
739 EffectNameValue<TexEnvCombine::SourceParam> sourceParamInit[] =
741 {"constant", TexEnvCombine::CONSTANT},
742 {"primary_color", TexEnvCombine::PRIMARY_COLOR},
743 {"previous", TexEnvCombine::PREVIOUS},
744 {"texture", TexEnvCombine::TEXTURE},
745 {"texture0", TexEnvCombine::TEXTURE0},
746 {"texture1", TexEnvCombine::TEXTURE1},
747 {"texture2", TexEnvCombine::TEXTURE2},
748 {"texture3", TexEnvCombine::TEXTURE3},
749 {"texture4", TexEnvCombine::TEXTURE4},
750 {"texture5", TexEnvCombine::TEXTURE5},
751 {"texture6", TexEnvCombine::TEXTURE6},
752 {"texture7", TexEnvCombine::TEXTURE7}
755 EffectPropertyMap<TexEnvCombine::SourceParam> sourceParams(sourceParamInit);
757 EffectNameValue<TexEnvCombine::OperandParam> opParamInit[] =
759 {"src-color", TexEnvCombine::SRC_COLOR},
760 {"one-minus-src-color", TexEnvCombine::ONE_MINUS_SRC_COLOR},
761 {"src-alpha", TexEnvCombine::SRC_ALPHA},
762 {"one-minus-src-alpha", TexEnvCombine::ONE_MINUS_SRC_ALPHA}
765 EffectPropertyMap<TexEnvCombine::OperandParam> operandParams(opParamInit);
767 TexEnvCombine* buildTexEnvCombine(Effect* effect, const SGPropertyNode* envProp,
768 const SGReaderWriterOptions* options)
770 if (!isAttributeActive(effect, envProp))
772 TexEnvCombine* result = new TexEnvCombine;
773 const SGPropertyNode* p = 0;
774 if ((p = getEffectPropertyChild(effect, envProp, "combine-rgb"))) {
775 TexEnvCombine::CombineParam crgb = TexEnvCombine::MODULATE;
776 findAttr(combineParams, p, crgb);
777 result->setCombine_RGB(crgb);
779 if ((p = getEffectPropertyChild(effect, envProp, "combine-alpha"))) {
780 TexEnvCombine::CombineParam calpha = TexEnvCombine::MODULATE;
781 findAttr(combineParams, p, calpha);
782 result->setCombine_Alpha(calpha);
784 if ((p = getEffectPropertyChild(effect, envProp, "source0-rgb"))) {
785 TexEnvCombine::SourceParam source = TexEnvCombine::TEXTURE;
786 findAttr(sourceParams, p, source);
787 result->setSource0_RGB(source);
789 if ((p = getEffectPropertyChild(effect, envProp, "source1-rgb"))) {
790 TexEnvCombine::SourceParam source = TexEnvCombine::PREVIOUS;
791 findAttr(sourceParams, p, source);
792 result->setSource1_RGB(source);
794 if ((p = getEffectPropertyChild(effect, envProp, "source2-rgb"))) {
795 TexEnvCombine::SourceParam source = TexEnvCombine::CONSTANT;
796 findAttr(sourceParams, p, source);
797 result->setSource2_RGB(source);
799 if ((p = getEffectPropertyChild(effect, envProp, "source0-alpha"))) {
800 TexEnvCombine::SourceParam source = TexEnvCombine::TEXTURE;
801 findAttr(sourceParams, p, source);
802 result->setSource0_Alpha(source);
804 if ((p = getEffectPropertyChild(effect, envProp, "source1-alpha"))) {
805 TexEnvCombine::SourceParam source = TexEnvCombine::PREVIOUS;
806 findAttr(sourceParams, p, source);
807 result->setSource1_Alpha(source);
809 if ((p = getEffectPropertyChild(effect, envProp, "source2-alpha"))) {
810 TexEnvCombine::SourceParam source = TexEnvCombine::CONSTANT;
811 findAttr(sourceParams, p, source);
812 result->setSource2_Alpha(source);
814 if ((p = getEffectPropertyChild(effect, envProp, "operand0-rgb"))) {
815 TexEnvCombine::OperandParam op = TexEnvCombine::SRC_COLOR;
816 findAttr(operandParams, p, op);
817 result->setOperand0_RGB(op);
819 if ((p = getEffectPropertyChild(effect, envProp, "operand1-rgb"))) {
820 TexEnvCombine::OperandParam op = TexEnvCombine::SRC_COLOR;
821 findAttr(operandParams, p, op);
822 result->setOperand1_RGB(op);
824 if ((p = getEffectPropertyChild(effect, envProp, "operand2-rgb"))) {
825 TexEnvCombine::OperandParam op = TexEnvCombine::SRC_ALPHA;
826 findAttr(operandParams, p, op);
827 result->setOperand2_RGB(op);
829 if ((p = getEffectPropertyChild(effect, envProp, "operand0-alpha"))) {
830 TexEnvCombine::OperandParam op = TexEnvCombine::SRC_ALPHA;
831 findAttr(operandParams, p, op);
832 result->setOperand0_Alpha(op);
834 if ((p = getEffectPropertyChild(effect, envProp, "operand1-alpha"))) {
835 TexEnvCombine::OperandParam op = TexEnvCombine::SRC_ALPHA;
836 findAttr(operandParams, p, op);
837 result->setOperand1_Alpha(op);
839 if ((p = getEffectPropertyChild(effect, envProp, "operand2-alpha"))) {
840 TexEnvCombine::OperandParam op = TexEnvCombine::SRC_ALPHA;
841 findAttr(operandParams, p, op);
842 result->setOperand2_Alpha(op);
844 if ((p = getEffectPropertyChild(effect, envProp, "scale-rgb"))) {
845 result->setScale_RGB(p->getValue<float>());
847 if ((p = getEffectPropertyChild(effect, envProp, "scale-alpha"))) {
848 result->setScale_Alpha(p->getValue<float>());
851 if ((p = getEffectPropertyChild(effect, envProp, "constant-color"))) {
852 SGVec4d color = p->getValue<SGVec4d>();
853 result->setConstantColor(toOsg(color));
854 } else if ((p = getEffectPropertyChild(effect, envProp,
855 "light-direction"))) {
856 SGVec3d direction = p->getValue<SGVec3d>();
857 result->setConstantColorAsLightDirection(toOsg(direction));
860 const SGPropertyNode* colorNode = envProp->getChild("constant-color");
862 initFromParameters(effect, colorNode, result,
863 &TexEnvCombine::setConstantColor, colorFields,
869 EffectNameValue<TexGen::Mode> tgenModeInit[] =
871 { "object-linear", TexGen::OBJECT_LINEAR},
872 { "eye-linear", TexGen::EYE_LINEAR},
873 { "sphere-map", TexGen::SPHERE_MAP},
874 { "normal-map", TexGen::NORMAL_MAP},
875 { "reflection-map", TexGen::REFLECTION_MAP}
878 EffectPropertyMap<TexGen::Mode> tgenModes(tgenModeInit);
880 EffectNameValue<TexGen::Coord> tgenCoordInit[] =
888 EffectPropertyMap<TexGen::Coord> tgenCoords(tgenCoordInit);
890 TexGen* buildTexGen(Effect* effect, const SGPropertyNode* tgenProp)
892 if (!isAttributeActive(effect, tgenProp))
894 TexGen* result = new TexGen;
895 TexGen::Mode mode = TexGen::OBJECT_LINEAR;
896 findAttr(tgenModes, getEffectPropertyChild(effect, tgenProp, "mode"), mode);
897 result->setMode(mode);
898 const SGPropertyNode* planesNode = tgenProp->getChild("planes");
900 for (int i = 0; i < planesNode->nChildren(); ++i) {
901 const SGPropertyNode* planeNode = planesNode->getChild(i);
903 findAttr(tgenCoords, planeNode->getName(), coord);
904 const SGPropertyNode* realNode
905 = getEffectPropertyNode(effect, planeNode);
906 SGVec4d plane = realNode->getValue<SGVec4d>();
907 result->setPlane(coord, toOsg(plane));
913 bool makeTextureParameters(SGPropertyNode* paramRoot, const StateSet* ss)
915 SGPropertyNode* texUnit = makeChild(paramRoot, "texture");
916 const Texture* tex = getStateAttribute<Texture>(0, ss);
917 const Texture2D* texture = dynamic_cast<const Texture2D*>(tex);
918 makeChild(texUnit, "unit")->setValue(0);
920 // The default shader-based technique ignores active
921 makeChild(texUnit, "active")->setValue(false);
924 const Image* image = texture->getImage();
927 imageName = image->getFileName();
929 makeChild(texUnit, "active")->setValue(false);
930 makeChild(texUnit, "type")->setValue("white");
933 makeChild(texUnit, "active")->setValue(true);
934 makeChild(texUnit, "type")->setValue("2d");
935 string filter = findName(filterModes,
936 texture->getFilter(Texture::MIN_FILTER));
937 string magFilter = findName(filterModes,
938 texture->getFilter(Texture::MAG_FILTER));
939 string wrapS = findName(wrapModes, texture->getWrap(Texture::WRAP_S));
940 string wrapT = findName(wrapModes, texture->getWrap(Texture::WRAP_T));
941 string wrapR = findName(wrapModes, texture->getWrap(Texture::WRAP_R));
942 makeChild(texUnit, "image")->setStringValue(imageName);
943 makeChild(texUnit, "filter")->setStringValue(filter);
944 makeChild(texUnit, "mag-filter")->setStringValue(magFilter);
945 makeChild(texUnit, "wrap-s")->setStringValue(wrapS);
946 makeChild(texUnit, "wrap-t")->setStringValue(wrapT);
947 makeChild(texUnit, "wrap-r")->setStringValue(wrapR);
951 class GBufferBuilder : public TextureBuilder
955 Texture* build(Effect* effect, Pass* pass, const SGPropertyNode*,
956 const SGReaderWriterOptions* options);
961 class BufferNameChangeListener : public SGPropertyChangeListener,
962 public DeferredPropertyListener {
964 BufferNameChangeListener(Pass* p, int u, const std::string& pn) : pass(p), unit(u)
966 propName = new std::string(pn);
968 ~BufferNameChangeListener()
973 void valueChanged(SGPropertyNode* node)
975 const char* buffer = node->getStringValue();
976 pass->setBufferUnit(unit, buffer);
978 void activate(SGPropertyNode* propRoot)
980 SGPropertyNode* listenProp = makeNode(propRoot, *propName);
984 listenProp->addChangeListener(this, true);
990 std::string* propName;
993 Texture* GBufferBuilder::build(Effect* effect, Pass* pass, const SGPropertyNode* prop,
994 const SGReaderWriterOptions* options)
997 const SGPropertyNode* pUnit = prop->getChild("unit");
999 unit = pUnit->getValue<int>();
1001 SG_LOG(SG_INPUT, SG_ALERT, "no texture unit");
1003 const SGPropertyNode* nameProp = getEffectPropertyChild(effect, prop,
1008 if (nameProp->nChildren() == 0) {
1009 buffer = nameProp->getStringValue();
1010 pass->setBufferUnit( unit, buffer );
1012 std::string propName = getGlobalProperty(nameProp, options);
1013 BufferNameChangeListener* listener = new BufferNameChangeListener(pass, unit, propName);
1014 effect->addDeferredPropertyListener(listener);
1017 // Return white for now. Would be overridden in Technique::ProcessDrawable
1018 return StateAttributeFactory::instance()->getWhiteTexture();
1023 TextureBuilder::Registrar installBuffer("buffer", new GBufferBuilder);