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 for(; (w || h || d); w>>=1, h>>=1, d>>=1, ++level) ;
64 allocated |= (1<<level)-1;
68 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)
70 if(width==0 || height==0 || depth==0)
71 throw invalid_operation("Texture3D::image");
75 BindRestore _bind(this);
76 glTexSubImage3D(target, level, x, y, z, wd, ht, dp, fmt, type, data);
79 void Texture3D::load_image(const string &fn, int dp)
84 unsigned w = img.get_width();
85 unsigned h = img.get_height();
91 throw incompatible_data("Texture3D::load_image");
97 for(d=h; d*d>h; d>>=2) ;
100 throw incompatible_data("Texture3D::load_image");
107 throw incompatible_data("Texture3D::load_image");
111 throw invalid_argument("Texture3D::load_image");
113 PixelFormat fmt = pixelformat_from_graphics(img.get_format());
115 storage(storage_pixelformat_from_graphics(img.get_format()), w, h, d);
116 else if(w!=width || h!=height || d!=depth)
117 throw incompatible_data("Texture3D::load_image");
119 PixelStore pstore = PixelStore::from_image(img);
120 BindRestore _bind_ps(pstore);
122 image(0, fmt, UNSIGNED_BYTE, img.get_data());
125 void Texture3D::get_level_size(unsigned level, unsigned &w, unsigned &h, unsigned &d)
139 UInt64 Texture3D::get_data_size() const
141 return id ? width*height*depth*get_pixel_size(ifmt) : 0;