]> git.mxchange.org Git - simgear.git/blobdiff - simgear/scene/material/TextureBuilder.cxx
Random buildings - initial commit.
[simgear.git] / simgear / scene / material / TextureBuilder.cxx
index dccc47dea18273bf5b21fa1c1bdc7f02fb0ce784..c06883aab0f5b0eda332e3269eb4ec472c44cf78 100644 (file)
@@ -19,6 +19,7 @@
 #endif
 
 #include "TextureBuilder.hxx"
+#include "mipmap.hxx"
 
 #include "Pass.hxx"
 
 #include <osg/TextureRectangle>
 #include <osg/TextureCubeMap>
 #include <osgDB/FileUtils>
+#include <osgDB/ReadFile>
 
 #include <boost/lexical_cast.hpp>
 #include <boost/tuple/tuple.hpp>
 #include <boost/tuple/tuple_comparison.hpp>
 
-#include <simgear/scene/model/SGReaderWriterXMLOptions.hxx>
+#include <simgear/scene/util/OsgMath.hxx>
+#include <simgear/scene/util/SGReaderWriterOptions.hxx>
 #include <simgear/scene/util/SGSceneFeatures.hxx>
 #include <simgear/scene/util/StateAttributeFactory.hxx>
-#include <simgear/math/SGMath.hxx>
 #include <simgear/structure/OSGUtils.hxx>
 
-#include "Noise.hxx"
-
 namespace simgear
 {
 using namespace std;
@@ -53,21 +53,21 @@ using namespace effect;
 
 TexEnvCombine* buildTexEnvCombine(Effect* effect,
                                   const SGPropertyNode* envProp,
-                                  const SGReaderWriterXMLOptions* options);
+                                  const SGReaderWriterOptions* options);
 TexGen* buildTexGen(Effect* Effect, const SGPropertyNode* tgenProp);
 
 // Hack to force inclusion of TextureBuilder.cxx in library
-osg::Texture* TextureBuilder::buildFromType(Effect* effect, const string& type,
+osg::Texture* TextureBuilder::buildFromType(Effect* effect, Pass* pass, const string& type,
                                             const SGPropertyNode*props,
-                                            const SGReaderWriterXMLOptions*
+                                            const SGReaderWriterOptions*
                                             options)
 {
-    return EffectBuilder<Texture>::buildFromType(effect, type, props, options);
+    return EffectBuilder<Texture>::buildFromType(effect, pass, type, props, options);
 }
 
 typedef boost::tuple<string, Texture::FilterMode, Texture::FilterMode,
                      Texture::WrapMode, Texture::WrapMode, Texture::WrapMode,
-                     string> TexTuple;
+                     string, MipMapTuple> TexTuple;
 
 EffectNameValue<TexEnv::Mode> texEnvModesInit[] =
 {
@@ -101,7 +101,7 @@ TexEnv* buildTexEnv(Effect* effect, const SGPropertyNode* prop)
 
 void TextureUnitBuilder::buildAttribute(Effect* effect, Pass* pass,
                                         const SGPropertyNode* prop,
-                                        const SGReaderWriterXMLOptions* options)
+                                        const SGReaderWriterOptions* options)
 {
     if (!isAttributeActive(effect, prop))
         return;
@@ -128,11 +128,11 @@ void TextureUnitBuilder::buildAttribute(Effect* effect, Pass* pass,
         type = pType->getStringValue();
     Texture* texture = 0;
     try {
-        texture = TextureBuilder::buildFromType(effect, type, prop,
+        texture = TextureBuilder::buildFromType(effect, pass, type, prop,
                                                 options);
     }
-    catch (BuilderException& ) {
-        SG_LOG(SG_INPUT, SG_ALERT, "No image file, "
+    catch (BuilderException& e) {
+        SG_LOG(SG_INPUT, SG_DEBUG, e.getFormattedMessage() << ", "
             << "maybe the reader did not set the filename attribute, "
             << "using white for type '" << type << "' on '" << pass->getName() << "', in " << prop->getPath() );
         texture = StateAttributeFactory::instance()->getWhiteTexture();
@@ -181,9 +181,8 @@ EffectNameValue<Texture::WrapMode> wrapModesInit[] =
 };
 EffectPropertyMap<Texture::WrapMode> wrapModes(wrapModesInit);
 
-
 TexTuple makeTexTuple(Effect* effect, const SGPropertyNode* props,
-                      const SGReaderWriterXMLOptions* options,
+                      const SGReaderWriterOptions* options,
                       const string& texType)
 {
     Texture::FilterMode minFilter = Texture::LINEAR_MIPMAP_LINEAR;
@@ -211,25 +210,44 @@ TexTuple makeTexTuple(Effect* effect, const SGPropertyNode* props,
     const SGPropertyNode* pImage
         = getEffectPropertyChild(effect, props, "image");
     string imageName;
+    string absFileName;
     if (pImage)
+    {
         imageName = pImage->getStringValue();
-    string absFileName = osgDB::findDataFile(imageName, options);
+        absFileName = SGModelLib::findDataFile(imageName, options);
+        if (absFileName.empty())
+        {
+            SG_LOG(SG_INPUT, SG_ALERT, "Texture file not found: '"
+                   << imageName << "'");
+        }
+    }
+
+    const SGPropertyNode* pMipmapControl
+        = getEffectPropertyChild(effect, props, "mipmap-control");
+    MipMapTuple mipmapFunctions( AUTOMATIC, AUTOMATIC, AUTOMATIC, AUTOMATIC ); 
+    if ( pMipmapControl )
+        mipmapFunctions = makeMipMapTuple(effect, pMipmapControl, options);
+
     return TexTuple(absFileName, minFilter, magFilter, sWrap, tWrap, rWrap,
-                    texType);
+                    texType, mipmapFunctions);
 }
 
 void setAttrs(const TexTuple& attrs, Texture* tex,
-              const SGReaderWriterXMLOptions* options)
+              const SGReaderWriterOptions* options)
 {
     const string& imageName = attrs.get<0>();
     if (imageName.empty()) {
         throw BuilderException("no image file");
     } else {
-        osgDB::ReaderWriter::ReadResult result
-            = osgDB::Registry::instance()->readImage(imageName, options);
-        if (result.success()) {
-            osg::Image* image = result.getImage();
-            tex->setImage(GL_FRONT_AND_BACK, image);
+        osgDB::ReaderWriter::ReadResult result;
+        result = osgDB::readImageFile(imageName, options);
+        osg::ref_ptr<osg::Image> image;
+        if (result.success())
+            image = result.getImage();
+        if (image.valid())
+        {
+            image = computeMipmap( image.get(), attrs.get<7>() );
+            tex->setImage(GL_FRONT_AND_BACK, image.get());
             int s = image->s();
             int t = image->t();
             if (s <= t && 32 <= s) {
@@ -258,26 +276,35 @@ class TexBuilder : public TextureBuilder
 {
 public:
     TexBuilder(const string& texType) : _type(texType) {}
-    Texture* build(Effect* effect, const SGPropertyNode*,
-                   const SGReaderWriterXMLOptions* options);
+    Texture* build(Effect* effect, Pass* pass, const SGPropertyNode*,
+                   const SGReaderWriterOptions* options);
 protected:
-    typedef map<TexTuple, ref_ptr<T> > TexMap;
+    typedef map<TexTuple, observer_ptr<T> > TexMap;
     TexMap texMap;
     const string _type;
 };
 
 template<typename T>
-Texture* TexBuilder<T>::build(Effect* effect, const SGPropertyNode* props,
-                              const SGReaderWriterXMLOptions* options)
+Texture* TexBuilder<T>::build(Effect* effect, Pass* pass, const SGPropertyNode* props,
+                              const SGReaderWriterOptions* options)
 {
     TexTuple attrs = makeTexTuple(effect, props, options, _type);
     typename TexMap::iterator itr = texMap.find(attrs);
-    if (itr != texMap.end())
-        return itr->second.get();
-    T* tex = new T;
+
+    ref_ptr<T> tex;
+    if ((itr != texMap.end())&&
+        (itr->second.lock(tex)))
+    {
+        return tex.release();
+    }
+
+    tex = new T;
     setAttrs(attrs, tex, options);
-    texMap.insert(make_pair(attrs, tex));
-    return tex;
+    if (itr == texMap.end())
+        texMap.insert(make_pair(attrs, tex));
+    else
+        itr->second = tex; // update existing, but empty observer
+    return tex.release();
 }
 
 
@@ -291,12 +318,12 @@ TextureBuilder::Registrar install3D("3d", new TexBuilder<Texture3D>("3d"));
 class WhiteTextureBuilder : public TextureBuilder
 {
 public:
-    Texture* build(Effect* effect, const SGPropertyNode*,
-                   const SGReaderWriterXMLOptions* options);
+    Texture* build(Effect* effect, Pass* pass, const SGPropertyNode*,
+                   const SGReaderWriterOptions* options);
 };
 
-Texture* WhiteTextureBuilder::build(Effect* effect, const SGPropertyNode*,
-                                    const SGReaderWriterXMLOptions* options)
+Texture* WhiteTextureBuilder::build(Effect* effect, Pass* pass, const SGPropertyNode*,
+                                    const SGReaderWriterOptions* options)
 {
     return StateAttributeFactory::instance()->getWhiteTexture();
 }
@@ -309,12 +336,12 @@ TextureBuilder::Registrar installWhite("white", new WhiteTextureBuilder);
 class TransparentTextureBuilder : public TextureBuilder
 {
 public:
-    Texture* build(Effect* effect, const SGPropertyNode*,
-                   const SGReaderWriterXMLOptions* options);
+    Texture* build(Effect* effect, Pass* pass, const SGPropertyNode*,
+                   const SGReaderWriterOptions* options);
 };
 
-Texture* TransparentTextureBuilder::build(Effect* effect, const SGPropertyNode*,
-                                    const SGReaderWriterXMLOptions* options)
+Texture* TransparentTextureBuilder::build(Effect* effect, Pass* pass, const SGPropertyNode*,
+                                    const SGReaderWriterOptions* options)
 {
     return StateAttributeFactory::instance()->getTransparentTexture();
 }
@@ -325,81 +352,26 @@ TextureBuilder::Registrar installTransparent("transparent",
                                              new TransparentTextureBuilder);
 }
 
-osg::Image* make3DNoiseImage(int texSize)
-{
-    osg::Image* image = new osg::Image;
-    image->setImage(texSize, texSize, texSize,
-                    4, GL_RGBA, GL_UNSIGNED_BYTE,
-                    new unsigned char[4 * texSize * texSize * texSize],
-                    osg::Image::USE_NEW_DELETE);
-
-    const int startFrequency = 4;
-    const int numOctaves = 4;
-
-    int f, i, j, k, inc;
-    double ni[3];
-    double inci, incj, inck;
-    int frequency = startFrequency;
-    GLubyte *ptr;
-    double amp = 0.5;
-
-    osg::notify(osg::WARN) << "creating 3D noise texture... ";
-
-    for (f = 0, inc = 0; f < numOctaves; ++f, frequency *= 2, ++inc, amp *= 0.5)
-    {
-        SetNoiseFrequency(frequency);
-        ptr = image->data();
-        ni[0] = ni[1] = ni[2] = 0;
-
-        inci = 1.0 / (texSize / frequency);
-        for (i = 0; i < texSize; ++i, ni[0] += inci)
-        {
-            incj = 1.0 / (texSize / frequency);
-            for (j = 0; j < texSize; ++j, ni[1] += incj)
-            {
-                inck = 1.0 / (texSize / frequency);
-                for (k = 0; k < texSize; ++k, ni[2] += inck, ptr += 4)
-                {
-                    *(ptr+inc) = (GLubyte) (((noise3(ni) + 1.0) * amp) * 128.0);
-                }
-            }
-        }
-    }
-
-    osg::notify(osg::WARN) << "DONE" << std::endl;
-    return image;
-}
-
 class NoiseBuilder : public TextureBuilder
 {
 public:
-    Texture* build(Effect* effect, const SGPropertyNode*,
-                   const SGReaderWriterXMLOptions* options);
+    Texture* build(Effect* effect, Pass* pass, const SGPropertyNode*,
+                   const SGReaderWriterOptions* options);
 protected:
     typedef map<int, ref_ptr<Texture3D> > NoiseMap;
     NoiseMap _noises;
 };
 
-Texture* NoiseBuilder::build(Effect* effect, const SGPropertyNode* props,
-                             const SGReaderWriterXMLOptions* options)
+Texture* NoiseBuilder::build(Effect* effect, Pass* pass, const SGPropertyNode* props,
+                             const SGReaderWriterOptions* options)
 {
     int texSize = 64;
     const SGPropertyNode* sizeProp = getEffectPropertyChild(effect, props,
                                                             "size");
     if (sizeProp)
         texSize = sizeProp->getValue<int>();
-    NoiseMap::iterator itr = _noises.find(texSize);
-    if (itr != _noises.end())
-        return itr->second.get();
-    Texture3D* noiseTexture = new osg::Texture3D;
-    noiseTexture->setFilter(osg::Texture3D::MIN_FILTER, osg::Texture3D::LINEAR);
-    noiseTexture->setFilter(osg::Texture3D::MAG_FILTER, osg::Texture3D::LINEAR);
-    noiseTexture->setWrap(osg::Texture3D::WRAP_S, osg::Texture3D::REPEAT);
-    noiseTexture->setWrap(osg::Texture3D::WRAP_T, osg::Texture3D::REPEAT);
-    noiseTexture->setWrap(osg::Texture3D::WRAP_R, osg::Texture3D::REPEAT);
-    noiseTexture->setImage( make3DNoiseImage(texSize) );
-    _noises.insert(make_pair(texSize, noiseTexture));
-    return noiseTexture;
+
+    return StateAttributeFactory::instance()->getNoiseTexture(texSize);
 }
 
 namespace
@@ -441,8 +413,8 @@ CubeMapTuple makeCubeMapTuple(Effect* effect, const SGPropertyNode* props)
 class CubeMapBuilder : public TextureBuilder
 {
 public:
-    Texture* build(Effect* effect, const SGPropertyNode*,
-                   const SGReaderWriterXMLOptions* options);
+    Texture* build(Effect* effect, Pass* pass, const SGPropertyNode*,
+                   const SGReaderWriterOptions* options);
 protected:
     typedef map<CubeMapTuple, ref_ptr<TextureCubeMap> > CubeMap;
     typedef map<string, ref_ptr<TextureCubeMap> > CrossCubeMap;
@@ -464,167 +436,167 @@ void copySubImage(const osg::Image* srcImage, int src_s, int src_t, int width, i
 }
 
 
-Texture* CubeMapBuilder::build(Effect* effect, const SGPropertyNode* props,
-                                                          const SGReaderWriterXMLOptions* options)
-{
-       // First check that there is a <images> tag
-       const SGPropertyNode* texturesProp = getEffectPropertyChild(effect, props, "images");
-       const SGPropertyNode* crossProp = getEffectPropertyChild(effect, props, "image");
-       if (!texturesProp && !crossProp) {
-               throw BuilderException("no images defined for cube map");
-               return NULL; // This is redundant
-       }
-
-       // Using 6 separate images
-       if(texturesProp) {
-
-               SG_LOG(SG_INPUT, SG_DEBUG, "try 6 images ");
-
-               CubeMapTuple _tuple = makeCubeMapTuple(effect, texturesProp);
-
-               CubeMap::iterator itr = _cubemaps.find(_tuple);
-               if (itr != _cubemaps.end())
-                       return itr->second.get();
-
-               TextureCubeMap* cubeTexture = new osg::TextureCubeMap;
-
-               // TODO: Read these from effect file? Maybe these are sane for all cuebmaps?
-               cubeTexture->setFilter(osg::Texture3D::MIN_FILTER, osg::Texture::LINEAR_MIPMAP_LINEAR);
-               cubeTexture->setFilter(osg::Texture3D::MAG_FILTER, osg::Texture::LINEAR);
-               cubeTexture->setWrap(osg::Texture3D::WRAP_S, osg::Texture::CLAMP_TO_EDGE);
-               cubeTexture->setWrap(osg::Texture3D::WRAP_T, osg::Texture::CLAMP_TO_EDGE);
-               cubeTexture->setWrap(osg::Texture3D::WRAP_R, osg::Texture::CLAMP_TO_EDGE);
-
-               osgDB::ReaderWriter::ReadResult result =
-                       osgDB::Registry::instance()->readImage(_tuple.get<0>(), options);
-               if(result.success()) {
-                       osg::Image* image = result.getImage();
-                       cubeTexture->setImage(TextureCubeMap::POSITIVE_X, image);
-               }
-               result = osgDB::Registry::instance()->readImage(_tuple.get<1>(), options);
-               if(result.success()) {
-                       osg::Image* image = result.getImage();
-                       cubeTexture->setImage(TextureCubeMap::NEGATIVE_X, image);
-               }
-               result = osgDB::Registry::instance()->readImage(_tuple.get<2>(), options);
-               if(result.success()) {
-                       osg::Image* image = result.getImage();
-                       cubeTexture->setImage(TextureCubeMap::POSITIVE_Y, image);
-               }
-               result = osgDB::Registry::instance()->readImage(_tuple.get<3>(), options);
-               if(result.success()) {
-                       osg::Image* image = result.getImage();
-                       cubeTexture->setImage(TextureCubeMap::NEGATIVE_Y, image);
-               }
-               result = osgDB::Registry::instance()->readImage(_tuple.get<4>(), options);
-               if(result.success()) {
-                       osg::Image* image = result.getImage();
-                       cubeTexture->setImage(TextureCubeMap::POSITIVE_Z, image);
-               }
-               result = osgDB::Registry::instance()->readImage(_tuple.get<5>(), options);
-               if(result.success()) {
-                       osg::Image* image = result.getImage();
-                       cubeTexture->setImage(TextureCubeMap::NEGATIVE_Z, image);
-               }
-
-               _cubemaps[_tuple] = cubeTexture;
-
-               return cubeTexture;
-       }
-
-
-       // Using 1 cross image
-       else if(crossProp) {
-               SG_LOG(SG_INPUT, SG_DEBUG, "try cross map ");
-
-               std::string texname = crossProp->getStringValue();
-
-               // Try to find existing cube map
-               CrossCubeMap::iterator itr = _crossmaps.find(texname);
-               if (itr != _crossmaps.end())
-                       return itr->second.get();
-
-               osgDB::ReaderWriter::ReadResult result =
-                       osgDB::Registry::instance()->readImage(texname, options);
-               if(result.success()) {
-                       osg::Image* image = result.getImage();
-                       image->flipVertical();   // Seems like the image coordinates are somewhat funny, flip to get better ones
-
-                       //cubeTexture->setResizeNonPowerOfTwoHint(false);
-
-                       // Size of a single image, 4 rows and 3 columns
-                       int width = image->s() / 3;
-                       int height = image->t() / 4;
-                       int depth = image->r();
-
-                       TextureCubeMap* cubeTexture = new osg::TextureCubeMap;
-
-                       // Copy the 6 sub-images and push them
-                       for(int n=0; n<6; n++) {
-
-                               SG_LOG(SG_INPUT, SG_DEBUG, "Copying the " << n << "th sub-images and pushing it" );
-
-                               osg::ref_ptr<osg::Image> subimg = new osg::Image();
-                               subimg->allocateImage(width, height, depth, image->getPixelFormat(), image->getDataType());  // Copy attributes
-
-                               // Choose correct image
-                               switch(n) {
-                               case 0:  // Front
-                                       copySubImage(image, width, 0, width, height, subimg.get(), 0, 0);
-                                       cubeTexture->setImage(TextureCubeMap::POSITIVE_Y, subimg.get());
-                                       cubeTexture->setWrap(osg::Texture3D::WRAP_S, osg::Texture::CLAMP_TO_EDGE);
-                                       cubeTexture->setWrap(osg::Texture3D::WRAP_T, osg::Texture::CLAMP_TO_EDGE);
-                                       cubeTexture->setWrap(osg::Texture3D::WRAP_R, osg::Texture::CLAMP_TO_EDGE);
-                                       break;
-                               case 1:  // Left
-                                       copySubImage(image, 0, height, width, height, subimg.get(), 0, 0);
-                                       cubeTexture->setImage(TextureCubeMap::NEGATIVE_X, subimg.get());
-                                       cubeTexture->setWrap(osg::Texture2D::WRAP_S, osg::Texture::CLAMP_TO_EDGE);
-                                       cubeTexture->setWrap(osg::Texture3D::WRAP_T, osg::Texture::CLAMP_TO_EDGE);
-                                       cubeTexture->setWrap(osg::Texture3D::WRAP_R, osg::Texture::CLAMP_TO_EDGE);
-                                       break;
-                               case 2:  // Top
-                                       copySubImage(image, width, height, width, height, subimg.get(), 0, 0);
-                                       cubeTexture->setImage(TextureCubeMap::POSITIVE_Z, subimg.get());
-                                       cubeTexture->setWrap(osg::Texture3D::WRAP_S, osg::Texture::CLAMP_TO_EDGE);
-                                       cubeTexture->setWrap(osg::Texture3D::WRAP_T, osg::Texture::CLAMP_TO_EDGE);
-                                       cubeTexture->setWrap(osg::Texture3D::WRAP_R, osg::Texture::CLAMP_TO_EDGE);
-                                       break;
-                               case 3:  // Right
-                                       copySubImage(image, width*2, height, width, height, subimg.get(), 0, 0);
-                                       cubeTexture->setImage(TextureCubeMap::POSITIVE_X, subimg.get());
-                                       cubeTexture->setWrap(osg::Texture3D::WRAP_S, osg::Texture::CLAMP_TO_EDGE);
-                                       cubeTexture->setWrap(osg::Texture3D::WRAP_T, osg::Texture::CLAMP_TO_EDGE);
-                                       cubeTexture->setWrap(osg::Texture3D::WRAP_R, osg::Texture::CLAMP_TO_EDGE);
-                                       break;
-                               case 4:  // Back
-                                       copySubImage(image, width, height*2, width, height, subimg.get(), 0, 0);
-                                       cubeTexture->setImage(TextureCubeMap::NEGATIVE_Y, subimg.get());
-                                       cubeTexture->setWrap(osg::Texture3D::WRAP_S, osg::Texture::CLAMP_TO_EDGE);
-                                       cubeTexture->setWrap(osg::Texture3D::WRAP_T, osg::Texture::CLAMP_TO_EDGE);
-                                       cubeTexture->setWrap(osg::Texture3D::WRAP_R, osg::Texture::CLAMP_TO_EDGE);
-                                       break;
-                               case 5:  // Bottom
-                                       copySubImage(image, width, height*3, width, height, subimg.get(), 0, 0);
-                                       cubeTexture->setImage(TextureCubeMap::NEGATIVE_Z, subimg.get());
-                                       cubeTexture->setWrap(osg::Texture3D::WRAP_S, osg::Texture::CLAMP_TO_EDGE);
-                                       cubeTexture->setWrap(osg::Texture3D::WRAP_T, osg::Texture::CLAMP_TO_EDGE);
-                                       cubeTexture->setWrap(osg::Texture3D::WRAP_R, osg::Texture::CLAMP_TO_EDGE);
-                                       break;
-                               };
-
-                       }
-
-                       _crossmaps[texname] = cubeTexture;
-
-                       return cubeTexture;
-
-               } else {
-                       throw BuilderException("Could not load cube cross");
-               }
-       }
-
-       return NULL;
+Texture* CubeMapBuilder::build(Effect* effect, Pass* pass, const SGPropertyNode* props,
+                               const SGReaderWriterOptions* options)
+{
+    // First check that there is a <images> tag
+    const SGPropertyNode* texturesProp = getEffectPropertyChild(effect, props, "images");
+    const SGPropertyNode* crossProp = getEffectPropertyChild(effect, props, "image");
+    if (!texturesProp && !crossProp) {
+        throw BuilderException("no images defined for cube map");
+        return NULL; // This is redundant
+    }
+
+    // Using 6 separate images
+    if(texturesProp) {
+
+        SG_LOG(SG_INPUT, SG_DEBUG, "try 6 images ");
+
+        CubeMapTuple _tuple = makeCubeMapTuple(effect, texturesProp);
+
+        CubeMap::iterator itr = _cubemaps.find(_tuple);
+        if (itr != _cubemaps.end())
+            return itr->second.get();
+
+        TextureCubeMap* cubeTexture = new osg::TextureCubeMap;
+
+        // TODO: Read these from effect file? Maybe these are sane for all cuebmaps?
+        cubeTexture->setFilter(osg::Texture3D::MIN_FILTER, osg::Texture::LINEAR_MIPMAP_LINEAR);
+        cubeTexture->setFilter(osg::Texture3D::MAG_FILTER, osg::Texture::LINEAR);
+        cubeTexture->setWrap(osg::Texture3D::WRAP_S, osg::Texture::CLAMP_TO_EDGE);
+        cubeTexture->setWrap(osg::Texture3D::WRAP_T, osg::Texture::CLAMP_TO_EDGE);
+        cubeTexture->setWrap(osg::Texture3D::WRAP_R, osg::Texture::CLAMP_TO_EDGE);
+
+        osgDB::ReaderWriter::ReadResult result;
+        result = osgDB::readImageFile(_tuple.get<0>(), options);
+        if(result.success()) {
+            osg::Image* image = result.getImage();
+            cubeTexture->setImage(TextureCubeMap::POSITIVE_X, image);
+        }
+        result = osgDB::readImageFile(_tuple.get<1>(), options);
+        if(result.success()) {
+            osg::Image* image = result.getImage();
+            cubeTexture->setImage(TextureCubeMap::NEGATIVE_X, image);
+        }
+        result = osgDB::readImageFile(_tuple.get<2>(), options);
+        if(result.success()) {
+            osg::Image* image = result.getImage();
+            cubeTexture->setImage(TextureCubeMap::POSITIVE_Y, image);
+        }
+        result = osgDB::readImageFile(_tuple.get<3>(), options);
+        if(result.success()) {
+            osg::Image* image = result.getImage();
+            cubeTexture->setImage(TextureCubeMap::NEGATIVE_Y, image);
+        }
+        result = osgDB::readImageFile(_tuple.get<4>(), options);
+        if(result.success()) {
+            osg::Image* image = result.getImage();
+            cubeTexture->setImage(TextureCubeMap::POSITIVE_Z, image);
+        }
+        result = osgDB::readImageFile(_tuple.get<5>(), options);
+        if(result.success()) {
+            osg::Image* image = result.getImage();
+            cubeTexture->setImage(TextureCubeMap::NEGATIVE_Z, image);
+        }
+
+        _cubemaps[_tuple] = cubeTexture;
+
+        return cubeTexture;
+    }
+
+
+    // Using 1 cross image
+    else if(crossProp) {
+        SG_LOG(SG_INPUT, SG_DEBUG, "try cross map ");
+
+        std::string texname = crossProp->getStringValue();
+
+        // Try to find existing cube map
+        CrossCubeMap::iterator itr = _crossmaps.find(texname);
+        if (itr != _crossmaps.end())
+            return itr->second.get();
+
+        osgDB::ReaderWriter::ReadResult result;
+        result = osgDB::readImageFile(texname, options);
+        if(result.success()) {
+            osg::Image* image = result.getImage();
+            image->flipVertical();   // Seems like the image coordinates are somewhat funny, flip to get better ones
+
+            //cubeTexture->setResizeNonPowerOfTwoHint(false);
+
+            // Size of a single image, 4 rows and 3 columns
+            int width = image->s() / 3;
+            int height = image->t() / 4;
+            int depth = image->r();
+
+            TextureCubeMap* cubeTexture = new osg::TextureCubeMap;
+
+            // Copy the 6 sub-images and push them
+            for(int n=0; n<6; n++) {
+
+                SG_LOG(SG_INPUT, SG_DEBUG, "Copying the " << n << "th sub-images and pushing it" );
+
+                osg::ref_ptr<osg::Image> subimg = new osg::Image();
+                subimg->allocateImage(width, height, depth, image->getPixelFormat(), image->getDataType());  // Copy attributes
+
+                // Choose correct image
+                switch(n) {
+                case 0:  // Front
+                    copySubImage(image, width, 0, width, height, subimg.get(), 0, 0);
+                    cubeTexture->setImage(TextureCubeMap::POSITIVE_Y, subimg.get());
+                    cubeTexture->setWrap(osg::Texture3D::WRAP_S, osg::Texture::CLAMP_TO_EDGE);
+                    cubeTexture->setWrap(osg::Texture3D::WRAP_T, osg::Texture::CLAMP_TO_EDGE);
+                    cubeTexture->setWrap(osg::Texture3D::WRAP_R, osg::Texture::CLAMP_TO_EDGE);
+                    break;
+                case 1:  // Left
+                    copySubImage(image, 0, height, width, height, subimg.get(), 0, 0);
+                    cubeTexture->setImage(TextureCubeMap::NEGATIVE_X, subimg.get());
+                    cubeTexture->setWrap(osg::Texture2D::WRAP_S, osg::Texture::CLAMP_TO_EDGE);
+                    cubeTexture->setWrap(osg::Texture3D::WRAP_T, osg::Texture::CLAMP_TO_EDGE);
+                    cubeTexture->setWrap(osg::Texture3D::WRAP_R, osg::Texture::CLAMP_TO_EDGE);
+                    break;
+                case 2:  // Top
+                    copySubImage(image, width, height, width, height, subimg.get(), 0, 0);
+                    cubeTexture->setImage(TextureCubeMap::POSITIVE_Z, subimg.get());
+                    cubeTexture->setWrap(osg::Texture3D::WRAP_S, osg::Texture::CLAMP_TO_EDGE);
+                    cubeTexture->setWrap(osg::Texture3D::WRAP_T, osg::Texture::CLAMP_TO_EDGE);
+                    cubeTexture->setWrap(osg::Texture3D::WRAP_R, osg::Texture::CLAMP_TO_EDGE);
+                    break;
+                case 3:  // Right
+                    copySubImage(image, width*2, height, width, height, subimg.get(), 0, 0);
+                    cubeTexture->setImage(TextureCubeMap::POSITIVE_X, subimg.get());
+                    cubeTexture->setWrap(osg::Texture3D::WRAP_S, osg::Texture::CLAMP_TO_EDGE);
+                    cubeTexture->setWrap(osg::Texture3D::WRAP_T, osg::Texture::CLAMP_TO_EDGE);
+                    cubeTexture->setWrap(osg::Texture3D::WRAP_R, osg::Texture::CLAMP_TO_EDGE);
+                    break;
+                case 4:  // Back
+                    copySubImage(image, width, height*2, width, height, subimg.get(), 0, 0);
+                    cubeTexture->setImage(TextureCubeMap::NEGATIVE_Y, subimg.get());
+                    cubeTexture->setWrap(osg::Texture3D::WRAP_S, osg::Texture::CLAMP_TO_EDGE);
+                    cubeTexture->setWrap(osg::Texture3D::WRAP_T, osg::Texture::CLAMP_TO_EDGE);
+                    cubeTexture->setWrap(osg::Texture3D::WRAP_R, osg::Texture::CLAMP_TO_EDGE);
+                    break;
+                case 5:  // Bottom
+                    copySubImage(image, width, height*3, width, height, subimg.get(), 0, 0);
+                    cubeTexture->setImage(TextureCubeMap::NEGATIVE_Z, subimg.get());
+                    cubeTexture->setWrap(osg::Texture3D::WRAP_S, osg::Texture::CLAMP_TO_EDGE);
+                    cubeTexture->setWrap(osg::Texture3D::WRAP_T, osg::Texture::CLAMP_TO_EDGE);
+                    cubeTexture->setWrap(osg::Texture3D::WRAP_R, osg::Texture::CLAMP_TO_EDGE);
+                    break;
+                };
+
+            }
+
+            _crossmaps[texname] = cubeTexture;
+
+            return cubeTexture;
+
+        } else {
+            throw BuilderException("Could not load cube cross");
+        }
+    }
+
+    return NULL;
 }
 
 namespace {
@@ -674,7 +646,7 @@ EffectNameValue<TexEnvCombine::OperandParam> opParamInit[] =
 EffectPropertyMap<TexEnvCombine::OperandParam> operandParams(opParamInit);
 
 TexEnvCombine* buildTexEnvCombine(Effect* effect, const SGPropertyNode* envProp,
-                                  const SGReaderWriterXMLOptions* options)
+                                  const SGReaderWriterOptions* options)
 {
     if (!isAttributeActive(effect, envProp))
         return 0;
@@ -856,4 +828,50 @@ bool makeTextureParameters(SGPropertyNode* paramRoot, const StateSet* ss)
     return true;
 }
 
+class GBufferBuilder : public TextureBuilder
+{
+public:
+    GBufferBuilder(int b) : buffer(b) {}
+    Texture* build(Effect* effect, Pass* pass, const SGPropertyNode*,
+                   const SGReaderWriterOptions* options);
+private:
+    int buffer;
+};
+
+Texture* GBufferBuilder::build(Effect* effect, Pass* pass, const SGPropertyNode* prop,
+                                    const SGReaderWriterOptions* options)
+{
+    int unit = 0;
+    const SGPropertyNode* pUnit = prop->getChild("unit");
+    if (pUnit) {
+        unit = pUnit->getValue<int>();
+    } else {
+        const SGPropertyNode* pName = prop->getChild("name");
+        if (pName)
+            try {
+                unit = boost::lexical_cast<int>(pName->getStringValue());
+            } catch (boost::bad_lexical_cast& lex) {
+                SG_LOG(SG_INPUT, SG_ALERT, "can't decode name as texture unit "
+                       << lex.what());
+            }
+    }
+    pass->setBufferUnit( unit, buffer );
+
+    // Return white for now. Would be overridden in Technique::ProcessDrawable
+    return StateAttributeFactory::instance()->getWhiteTexture();
+}
+
+namespace
+{
+    TextureBuilder::Registrar installDepthBuffer("depth-buffer", new GBufferBuilder(Effect::DEPTH_BUFFER));
+    TextureBuilder::Registrar installNormalBuffer("normal-buffer", new GBufferBuilder(Effect::NORMAL_BUFFER));
+    TextureBuilder::Registrar installDiffuseBuffer("diffuse-buffer", new GBufferBuilder(Effect::DIFFUSE_BUFFER));
+    TextureBuilder::Registrar installSpecularBuffer("spec-emis-buffer", new GBufferBuilder(Effect::SPEC_EMIS_BUFFER));
+    TextureBuilder::Registrar installLightingBuffer("lighting-buffer", new GBufferBuilder(Effect::LIGHTING_BUFFER));
+    TextureBuilder::Registrar installMiddleBloomBuffer("middle-bloom-buffer", new GBufferBuilder(Effect::MIDDLE_BLOOM_BUFFER));
+    TextureBuilder::Registrar installBloomBuffer("bloom-buffer", new GBufferBuilder(Effect::BLOOM_BUFFER));
+    TextureBuilder::Registrar installAoBuffer("ao-buffer", new GBufferBuilder(Effect::AO_BUFFER));
+    TextureBuilder::Registrar installShadowBuffer("shadow-buffer", new GBufferBuilder(Effect::SHADOW_BUFFER));
+}
+
 }