2 #include <msp/gl/extensions/ext_texture3d.h>
3 #include <msp/graphics/image.h>
6 #include "pixelstore.h"
14 Texture3D::Texture3D():
15 Texture(GL_TEXTURE_3D),
22 static Require _req(EXT_texture3D);
25 void Texture3D::storage(PixelFormat fmt, unsigned wd, unsigned ht, unsigned dp)
28 throw invalid_operation("Texture3D::storage");
29 if(wd==0 || ht==0 || dp==0)
30 throw invalid_argument("Texture3D::storage");
31 require_pixelformat(fmt);
39 void Texture3D::allocate(unsigned level)
41 if(allocated&(1<<level))
44 image(level, get_base_pixelformat(ifmt), UNSIGNED_BYTE, 0);
47 void Texture3D::image(unsigned level, PixelFormat fmt, DataType type, const void *data)
49 if(width==0 || height==0 || depth==0)
50 throw invalid_operation("Texture3D::image");
55 get_level_size(level, w, h, d);
57 BindRestore _bind(this);
58 glTexImage3D(target, level, ifmt, width, height, depth, 0, fmt, type, data);
60 allocated |= 1<<level;
61 if(gen_mipmap && level==0)
63 auto_generate_mipmap();
64 for(; (w || h || d); w>>=1, h>>=1, d>>=1, ++level) ;
65 allocated |= (1<<level)-1;
69 void Texture3D::sub_image(unsigned level, int x, int y, int z, unsigned wd, unsigned ht, unsigned dp, PixelFormat fmt, DataType type, const void *data)
71 if(width==0 || height==0 || depth==0)
72 throw invalid_operation("Texture3D::image");
76 BindRestore _bind(this);
77 glTexSubImage3D(target, level, x, y, z, wd, ht, dp, fmt, type, data);
80 void Texture3D::load_image(const string &fn, int dp)
85 unsigned w = img.get_width();
86 unsigned h = img.get_height();
92 throw incompatible_data("Texture3D::load_image");
98 for(d=h; d*d>h; d>>=2) ;
101 throw incompatible_data("Texture3D::load_image");
108 throw incompatible_data("Texture3D::load_image");
112 throw invalid_argument("Texture3D::load_image");
114 PixelFormat fmt = pixelformat_from_graphics(img.get_format());
116 storage(storage_pixelformat_from_graphics(img.get_format()), w, h, d);
117 else if(w!=width || h!=height || d!=depth)
118 throw incompatible_data("Texture3D::load_image");
120 PixelStore pstore = PixelStore::from_image(img);
121 BindRestore _bind_ps(pstore);
123 image(0, fmt, UNSIGNED_BYTE, img.get_data());
126 void Texture3D::get_level_size(unsigned level, unsigned &w, unsigned &h, unsigned &d)
140 UInt64 Texture3D::get_data_size() const
142 return id ? width*height*depth*get_pixel_size(ifmt) : 0;