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, ref_ptr<TextureCubeMap> > CubeMap;
538 typedef map<string, ref_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 if (itr != _cubemaps.end())
574 return itr->second.get();
576 TextureCubeMap* cubeTexture = new osg::TextureCubeMap;
578 // TODO: Read these from effect file? Maybe these are sane for all cuebmaps?
579 cubeTexture->setFilter(osg::Texture3D::MIN_FILTER, osg::Texture::LINEAR_MIPMAP_LINEAR);
580 cubeTexture->setFilter(osg::Texture3D::MAG_FILTER, osg::Texture::LINEAR);
581 cubeTexture->setWrap(osg::Texture3D::WRAP_S, osg::Texture::CLAMP_TO_EDGE);
582 cubeTexture->setWrap(osg::Texture3D::WRAP_T, osg::Texture::CLAMP_TO_EDGE);
583 cubeTexture->setWrap(osg::Texture3D::WRAP_R, osg::Texture::CLAMP_TO_EDGE);
585 osgDB::ReaderWriter::ReadResult result;
586 result = osgDB::readImageFile(_tuple.get<0>(), options);
587 if(result.success()) {
588 osg::Image* image = result.getImage();
589 cubeTexture->setImage(TextureCubeMap::POSITIVE_X, image);
591 result = osgDB::readImageFile(_tuple.get<1>(), options);
592 if(result.success()) {
593 osg::Image* image = result.getImage();
594 cubeTexture->setImage(TextureCubeMap::NEGATIVE_X, image);
596 result = osgDB::readImageFile(_tuple.get<2>(), options);
597 if(result.success()) {
598 osg::Image* image = result.getImage();
599 cubeTexture->setImage(TextureCubeMap::POSITIVE_Y, image);
601 result = osgDB::readImageFile(_tuple.get<3>(), options);
602 if(result.success()) {
603 osg::Image* image = result.getImage();
604 cubeTexture->setImage(TextureCubeMap::NEGATIVE_Y, image);
606 result = osgDB::readImageFile(_tuple.get<4>(), options);
607 if(result.success()) {
608 osg::Image* image = result.getImage();
609 cubeTexture->setImage(TextureCubeMap::POSITIVE_Z, image);
611 result = osgDB::readImageFile(_tuple.get<5>(), options);
612 if(result.success()) {
613 osg::Image* image = result.getImage();
614 cubeTexture->setImage(TextureCubeMap::NEGATIVE_Z, image);
617 _cubemaps[_tuple] = cubeTexture;
623 // Using 1 cross image
625 std::string texname = crossProp->getStringValue();
627 // Try to find existing cube map
628 CrossCubeMap::iterator itr = _crossmaps.find(texname);
629 if (itr != _crossmaps.end())
630 return itr->second.get();
632 osgDB::ReaderWriter::ReadResult result;
633 result = osgDB::readImageFile(texname, options);
634 if(result.success()) {
635 osg::Image* image = result.getImage();
636 image->flipVertical(); // Seems like the image coordinates are somewhat funny, flip to get better ones
638 //cubeTexture->setResizeNonPowerOfTwoHint(false);
640 // Size of a single image, 4 rows and 3 columns
641 int width = image->s() / 3;
642 int height = image->t() / 4;
643 int depth = image->r();
645 TextureCubeMap* cubeTexture = new osg::TextureCubeMap;
647 // Copy the 6 sub-images and push them
648 for(int n=0; n<6; n++) {
650 SG_LOG(SG_INPUT, SG_DEBUG, "Copying the " << n << "th sub-images and pushing it" );
652 osg::ref_ptr<osg::Image> subimg = new osg::Image();
653 subimg->allocateImage(width, height, depth, image->getPixelFormat(), image->getDataType()); // Copy attributes
655 // Choose correct image
658 copySubImage(image, width, 0, width, height, subimg.get(), 0, 0);
659 cubeTexture->setImage(TextureCubeMap::POSITIVE_Y, subimg.get());
660 cubeTexture->setWrap(osg::Texture3D::WRAP_S, osg::Texture::CLAMP_TO_EDGE);
661 cubeTexture->setWrap(osg::Texture3D::WRAP_T, osg::Texture::CLAMP_TO_EDGE);
662 cubeTexture->setWrap(osg::Texture3D::WRAP_R, osg::Texture::CLAMP_TO_EDGE);
665 copySubImage(image, 0, height, width, height, subimg.get(), 0, 0);
666 cubeTexture->setImage(TextureCubeMap::NEGATIVE_X, subimg.get());
667 cubeTexture->setWrap(osg::Texture2D::WRAP_S, osg::Texture::CLAMP_TO_EDGE);
668 cubeTexture->setWrap(osg::Texture3D::WRAP_T, osg::Texture::CLAMP_TO_EDGE);
669 cubeTexture->setWrap(osg::Texture3D::WRAP_R, osg::Texture::CLAMP_TO_EDGE);
672 copySubImage(image, width, height, width, height, subimg.get(), 0, 0);
673 cubeTexture->setImage(TextureCubeMap::POSITIVE_Z, subimg.get());
674 cubeTexture->setWrap(osg::Texture3D::WRAP_S, osg::Texture::CLAMP_TO_EDGE);
675 cubeTexture->setWrap(osg::Texture3D::WRAP_T, osg::Texture::CLAMP_TO_EDGE);
676 cubeTexture->setWrap(osg::Texture3D::WRAP_R, osg::Texture::CLAMP_TO_EDGE);
679 copySubImage(image, width*2, height, width, height, subimg.get(), 0, 0);
680 cubeTexture->setImage(TextureCubeMap::POSITIVE_X, subimg.get());
681 cubeTexture->setWrap(osg::Texture3D::WRAP_S, osg::Texture::CLAMP_TO_EDGE);
682 cubeTexture->setWrap(osg::Texture3D::WRAP_T, osg::Texture::CLAMP_TO_EDGE);
683 cubeTexture->setWrap(osg::Texture3D::WRAP_R, osg::Texture::CLAMP_TO_EDGE);
686 copySubImage(image, width, height*2, width, height, subimg.get(), 0, 0);
687 cubeTexture->setImage(TextureCubeMap::NEGATIVE_Y, subimg.get());
688 cubeTexture->setWrap(osg::Texture3D::WRAP_S, osg::Texture::CLAMP_TO_EDGE);
689 cubeTexture->setWrap(osg::Texture3D::WRAP_T, osg::Texture::CLAMP_TO_EDGE);
690 cubeTexture->setWrap(osg::Texture3D::WRAP_R, osg::Texture::CLAMP_TO_EDGE);
693 copySubImage(image, width, height*3, width, height, subimg.get(), 0, 0);
694 cubeTexture->setImage(TextureCubeMap::NEGATIVE_Z, subimg.get());
695 cubeTexture->setWrap(osg::Texture3D::WRAP_S, osg::Texture::CLAMP_TO_EDGE);
696 cubeTexture->setWrap(osg::Texture3D::WRAP_T, osg::Texture::CLAMP_TO_EDGE);
697 cubeTexture->setWrap(osg::Texture3D::WRAP_R, osg::Texture::CLAMP_TO_EDGE);
703 _crossmaps[texname] = cubeTexture;
708 throw BuilderException("Could not load cube cross");
716 TextureBuilder::Registrar installCubeMap("cubemap", new CubeMapBuilder);
719 EffectNameValue<TexEnvCombine::CombineParam> combineParamInit[] =
721 {"replace", TexEnvCombine::REPLACE},
722 {"modulate", TexEnvCombine::MODULATE},
723 {"add", TexEnvCombine::ADD},
724 {"add-signed", TexEnvCombine::ADD_SIGNED},
725 {"interpolate", TexEnvCombine::INTERPOLATE},
726 {"subtract", TexEnvCombine::SUBTRACT},
727 {"dot3-rgb", TexEnvCombine::DOT3_RGB},
728 {"dot3-rgba", TexEnvCombine::DOT3_RGBA}
731 EffectPropertyMap<TexEnvCombine::CombineParam> combineParams(combineParamInit);
733 EffectNameValue<TexEnvCombine::SourceParam> sourceParamInit[] =
735 {"constant", TexEnvCombine::CONSTANT},
736 {"primary_color", TexEnvCombine::PRIMARY_COLOR},
737 {"previous", TexEnvCombine::PREVIOUS},
738 {"texture", TexEnvCombine::TEXTURE},
739 {"texture0", TexEnvCombine::TEXTURE0},
740 {"texture1", TexEnvCombine::TEXTURE1},
741 {"texture2", TexEnvCombine::TEXTURE2},
742 {"texture3", TexEnvCombine::TEXTURE3},
743 {"texture4", TexEnvCombine::TEXTURE4},
744 {"texture5", TexEnvCombine::TEXTURE5},
745 {"texture6", TexEnvCombine::TEXTURE6},
746 {"texture7", TexEnvCombine::TEXTURE7}
749 EffectPropertyMap<TexEnvCombine::SourceParam> sourceParams(sourceParamInit);
751 EffectNameValue<TexEnvCombine::OperandParam> opParamInit[] =
753 {"src-color", TexEnvCombine::SRC_COLOR},
754 {"one-minus-src-color", TexEnvCombine::ONE_MINUS_SRC_COLOR},
755 {"src-alpha", TexEnvCombine::SRC_ALPHA},
756 {"one-minus-src-alpha", TexEnvCombine::ONE_MINUS_SRC_ALPHA}
759 EffectPropertyMap<TexEnvCombine::OperandParam> operandParams(opParamInit);
761 TexEnvCombine* buildTexEnvCombine(Effect* effect, const SGPropertyNode* envProp,
762 const SGReaderWriterOptions* options)
764 if (!isAttributeActive(effect, envProp))
766 TexEnvCombine* result = new TexEnvCombine;
767 const SGPropertyNode* p = 0;
768 if ((p = getEffectPropertyChild(effect, envProp, "combine-rgb"))) {
769 TexEnvCombine::CombineParam crgb = TexEnvCombine::MODULATE;
770 findAttr(combineParams, p, crgb);
771 result->setCombine_RGB(crgb);
773 if ((p = getEffectPropertyChild(effect, envProp, "combine-alpha"))) {
774 TexEnvCombine::CombineParam calpha = TexEnvCombine::MODULATE;
775 findAttr(combineParams, p, calpha);
776 result->setCombine_Alpha(calpha);
778 if ((p = getEffectPropertyChild(effect, envProp, "source0-rgb"))) {
779 TexEnvCombine::SourceParam source = TexEnvCombine::TEXTURE;
780 findAttr(sourceParams, p, source);
781 result->setSource0_RGB(source);
783 if ((p = getEffectPropertyChild(effect, envProp, "source1-rgb"))) {
784 TexEnvCombine::SourceParam source = TexEnvCombine::PREVIOUS;
785 findAttr(sourceParams, p, source);
786 result->setSource1_RGB(source);
788 if ((p = getEffectPropertyChild(effect, envProp, "source2-rgb"))) {
789 TexEnvCombine::SourceParam source = TexEnvCombine::CONSTANT;
790 findAttr(sourceParams, p, source);
791 result->setSource2_RGB(source);
793 if ((p = getEffectPropertyChild(effect, envProp, "source0-alpha"))) {
794 TexEnvCombine::SourceParam source = TexEnvCombine::TEXTURE;
795 findAttr(sourceParams, p, source);
796 result->setSource0_Alpha(source);
798 if ((p = getEffectPropertyChild(effect, envProp, "source1-alpha"))) {
799 TexEnvCombine::SourceParam source = TexEnvCombine::PREVIOUS;
800 findAttr(sourceParams, p, source);
801 result->setSource1_Alpha(source);
803 if ((p = getEffectPropertyChild(effect, envProp, "source2-alpha"))) {
804 TexEnvCombine::SourceParam source = TexEnvCombine::CONSTANT;
805 findAttr(sourceParams, p, source);
806 result->setSource2_Alpha(source);
808 if ((p = getEffectPropertyChild(effect, envProp, "operand0-rgb"))) {
809 TexEnvCombine::OperandParam op = TexEnvCombine::SRC_COLOR;
810 findAttr(operandParams, p, op);
811 result->setOperand0_RGB(op);
813 if ((p = getEffectPropertyChild(effect, envProp, "operand1-rgb"))) {
814 TexEnvCombine::OperandParam op = TexEnvCombine::SRC_COLOR;
815 findAttr(operandParams, p, op);
816 result->setOperand1_RGB(op);
818 if ((p = getEffectPropertyChild(effect, envProp, "operand2-rgb"))) {
819 TexEnvCombine::OperandParam op = TexEnvCombine::SRC_ALPHA;
820 findAttr(operandParams, p, op);
821 result->setOperand2_RGB(op);
823 if ((p = getEffectPropertyChild(effect, envProp, "operand0-alpha"))) {
824 TexEnvCombine::OperandParam op = TexEnvCombine::SRC_ALPHA;
825 findAttr(operandParams, p, op);
826 result->setOperand0_Alpha(op);
828 if ((p = getEffectPropertyChild(effect, envProp, "operand1-alpha"))) {
829 TexEnvCombine::OperandParam op = TexEnvCombine::SRC_ALPHA;
830 findAttr(operandParams, p, op);
831 result->setOperand1_Alpha(op);
833 if ((p = getEffectPropertyChild(effect, envProp, "operand2-alpha"))) {
834 TexEnvCombine::OperandParam op = TexEnvCombine::SRC_ALPHA;
835 findAttr(operandParams, p, op);
836 result->setOperand2_Alpha(op);
838 if ((p = getEffectPropertyChild(effect, envProp, "scale-rgb"))) {
839 result->setScale_RGB(p->getValue<float>());
841 if ((p = getEffectPropertyChild(effect, envProp, "scale-alpha"))) {
842 result->setScale_Alpha(p->getValue<float>());
845 if ((p = getEffectPropertyChild(effect, envProp, "constant-color"))) {
846 SGVec4d color = p->getValue<SGVec4d>();
847 result->setConstantColor(toOsg(color));
848 } else if ((p = getEffectPropertyChild(effect, envProp,
849 "light-direction"))) {
850 SGVec3d direction = p->getValue<SGVec3d>();
851 result->setConstantColorAsLightDirection(toOsg(direction));
854 const SGPropertyNode* colorNode = envProp->getChild("constant-color");
856 initFromParameters(effect, colorNode, result,
857 &TexEnvCombine::setConstantColor, colorFields,
862 EffectNameValue<TexGen::Mode> tgenModeInit[] =
864 { "object-linear", TexGen::OBJECT_LINEAR},
865 { "eye-linear", TexGen::EYE_LINEAR},
866 { "sphere-map", TexGen::SPHERE_MAP},
867 { "normal-map", TexGen::NORMAL_MAP},
868 { "reflection-map", TexGen::REFLECTION_MAP}
871 EffectPropertyMap<TexGen::Mode> tgenModes(tgenModeInit);
873 EffectNameValue<TexGen::Coord> tgenCoordInit[] =
881 EffectPropertyMap<TexGen::Coord> tgenCoords(tgenCoordInit);
883 TexGen* buildTexGen(Effect* effect, const SGPropertyNode* tgenProp)
885 if (!isAttributeActive(effect, tgenProp))
887 TexGen* result = new TexGen;
888 TexGen::Mode mode = TexGen::OBJECT_LINEAR;
889 findAttr(tgenModes, getEffectPropertyChild(effect, tgenProp, "mode"), mode);
890 result->setMode(mode);
891 const SGPropertyNode* planesNode = tgenProp->getChild("planes");
893 for (int i = 0; i < planesNode->nChildren(); ++i) {
894 const SGPropertyNode* planeNode = planesNode->getChild(i);
896 findAttr(tgenCoords, planeNode->getName(), coord);
897 const SGPropertyNode* realNode
898 = getEffectPropertyNode(effect, planeNode);
899 SGVec4d plane = realNode->getValue<SGVec4d>();
900 result->setPlane(coord, toOsg(plane));
906 bool makeTextureParameters(SGPropertyNode* paramRoot, const StateSet* ss)
908 SGPropertyNode* texUnit = makeChild(paramRoot, "texture");
909 const Texture* tex = getStateAttribute<Texture>(0, ss);
910 const Texture2D* texture = dynamic_cast<const Texture2D*>(tex);
911 makeChild(texUnit, "unit")->setValue(0);
913 // The default shader-based technique ignores active
914 makeChild(texUnit, "active")->setValue(false);
917 const Image* image = texture->getImage();
920 imageName = image->getFileName();
922 makeChild(texUnit, "active")->setValue(false);
923 makeChild(texUnit, "type")->setValue("white");
926 makeChild(texUnit, "active")->setValue(true);
927 makeChild(texUnit, "type")->setValue("2d");
928 string filter = findName(filterModes,
929 texture->getFilter(Texture::MIN_FILTER));
930 string magFilter = findName(filterModes,
931 texture->getFilter(Texture::MAG_FILTER));
932 string wrapS = findName(wrapModes, texture->getWrap(Texture::WRAP_S));
933 string wrapT = findName(wrapModes, texture->getWrap(Texture::WRAP_T));
934 string wrapR = findName(wrapModes, texture->getWrap(Texture::WRAP_R));
935 makeChild(texUnit, "image")->setStringValue(imageName);
936 makeChild(texUnit, "filter")->setStringValue(filter);
937 makeChild(texUnit, "mag-filter")->setStringValue(magFilter);
938 makeChild(texUnit, "wrap-s")->setStringValue(wrapS);
939 makeChild(texUnit, "wrap-t")->setStringValue(wrapT);
940 makeChild(texUnit, "wrap-r")->setStringValue(wrapR);
944 class GBufferBuilder : public TextureBuilder
948 Texture* build(Effect* effect, Pass* pass, const SGPropertyNode*,
949 const SGReaderWriterOptions* options);
954 class BufferNameChangeListener : public SGPropertyChangeListener, public InitializeWhenAdded,
955 public Effect::Updater {
957 BufferNameChangeListener(Pass* p, int u, const std::string& pn) : pass(p), unit(u)
959 propName = new std::string(pn);
961 ~BufferNameChangeListener()
966 void valueChanged(SGPropertyNode* node)
968 const char* buffer = node->getStringValue();
969 pass->setBufferUnit(unit, buffer);
971 void initOnAddImpl(Effect* effect, SGPropertyNode* propRoot)
973 SGPropertyNode* listenProp = makeNode(propRoot, *propName);
977 listenProp->addChangeListener(this, true);
983 std::string* propName;
986 Texture* GBufferBuilder::build(Effect* effect, Pass* pass, const SGPropertyNode* prop,
987 const SGReaderWriterOptions* options)
990 const SGPropertyNode* pUnit = prop->getChild("unit");
992 unit = pUnit->getValue<int>();
994 SG_LOG(SG_INPUT, SG_ALERT, "no texture unit");
996 const SGPropertyNode* nameProp = getEffectPropertyChild(effect, prop,
1001 if (nameProp->nChildren() == 0) {
1002 buffer = nameProp->getStringValue();
1003 pass->setBufferUnit( unit, buffer );
1005 std::string propName = getGlobalProperty(nameProp, options);
1006 BufferNameChangeListener* listener = new BufferNameChangeListener(pass, unit, propName);
1007 effect->addUpdater(listener);
1010 // Return white for now. Would be overridden in Technique::ProcessDrawable
1011 return StateAttributeFactory::instance()->getWhiteTexture();
1016 TextureBuilder::Registrar installBuffer("buffer", new GBufferBuilder);