]> git.tdb.fi Git - libs/gl.git/blobdiff - source/texture3d.cpp
Implement loading functionality for texture classes that were missing it
[libs/gl.git] / source / texture3d.cpp
index d5badc725435fb3efb37d4c15c52bedafd40ece6..8b014aee0c262fb811444c1c44a1fba0083df21e 100644 (file)
@@ -124,6 +124,39 @@ void Texture3D::load_image(const string &fn, int dp)
        image(0, fmt, UNSIGNED_BYTE, img.get_data());
 }
 
+void Texture3D::image(const Graphics::Image &img, bool srgb)
+{
+       unsigned w = img.get_width();
+       unsigned h = img.get_height();
+       unsigned d = 1;
+
+       if(depth)
+       {
+               if(h%depth)
+                       throw incompatible_data("Texture3D::load_image");
+               h /= depth;
+               d = depth;
+       }
+       else
+       {
+               if(h%w)
+                       throw incompatible_data("Texture3D::load_image");
+               d = h/w;
+               h = w;
+       }
+
+       PixelFormat fmt = pixelformat_from_graphics(img.get_format());
+       if(width==0)
+               storage(storage_pixelformat_from_graphics(img.get_format(), srgb), w, h, d);
+       else if(w!=width || h!=height || d!=depth)
+               throw incompatible_data("Texture3D::load_image");
+
+       PixelStore pstore = PixelStore::from_image(img);
+       BindRestore _bind_ps(pstore);
+
+       image(0, fmt, UNSIGNED_BYTE, img.get_data());
+}
+
 void Texture3D::get_level_size(unsigned level, unsigned &w, unsigned &h, unsigned &d)
 {
        w >>= level;
@@ -143,5 +176,36 @@ UInt64 Texture3D::get_data_size() const
        return id ? width*height*depth*get_pixel_size(ifmt) : 0;
 }
 
+
+Texture3D::Loader::Loader(Texture3D &t):
+       DataFile::DerivedObjectLoader<Texture3D, Texture::Loader>(t)
+{
+       init();
+}
+
+Texture3D::Loader::Loader(Texture3D &t, Collection &c):
+       DataFile::DerivedObjectLoader<Texture3D, Texture::Loader>(t, c)
+{
+       init();
+}
+
+void Texture3D::Loader::init()
+{
+       add("raw_data", &Loader::raw_data);
+       add("storage", &Loader::storage);
+}
+
+void Texture3D::Loader::raw_data(const string &data)
+{
+       obj.image(0, get_base_pixelformat(obj.ifmt), UNSIGNED_BYTE, data.data());
+}
+
+void Texture3D::Loader::storage(PixelFormat fmt, unsigned w, unsigned h, unsigned d)
+{
+       if(srgb)
+               fmt = get_srgb_pixelformat(fmt);
+       obj.storage(fmt, w, h, d);
+}
+
 } // namespace GL
 } // namespace Msp