3 This file is part of libmspgl
4 Copyright © 2007 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
9 #include <msp/gbase/image.h>
12 #include "extension.h"
13 #include "texture3d.h"
14 #include "version_1_2.h"
21 Texture3D::Texture3D():
22 Texture(GL_TEXTURE_3D),
28 static RequireVersion _ver(1, 2);
31 void Texture3D::storage(PixelFormat f, unsigned w, unsigned h, unsigned d)
34 throw InvalidState("Textures storage may only be specified once");
35 if(w==0 || h==0 || d==0)
36 throw InvalidParameterValue("Invalid texture dimensions");
43 image(0, ifmt, UNSIGNED_BYTE, 0);
46 void Texture3D::allocate(unsigned level)
48 if(allocated&(1<<level))
51 image(level, get_base_pixelformat(ifmt), UNSIGNED_BYTE, 0);
54 void Texture3D::image(unsigned level, PixelFormat fmt, DataType type, const void *data)
61 get_level_size(level, w, h, d);
63 Bind _bind(this, true);
64 glTexImage3D(target, level, ifmt, width, height, depth, 0, fmt, type, data);
66 allocated |= 1<<level;
67 if(gen_mipmap && level==0)
69 for(; (w || h || d); w>>=1, h>>=1, d>>=1, ++level) ;
70 allocated |= (1<<level)-1;
74 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)
79 Bind _bind(this, true);
80 glTexSubImage3D(target, level, x, y, z, wd, ht, dp, fmt, type, data);
83 void Texture3D::load_image(const string &fn, int dp)
88 unsigned w = img.get_width();
89 unsigned h = img.get_height();
95 throw IncompatibleData("Image height is not divisible by its width");
101 for(d=h; d*d>h; d>>=2) ;
104 throw IncompatibleData("Could not find a square root of texture height");
110 PixelFormat fmt = pixelformat_from_graphics(img.get_format());
112 storage(fmt, w, h, d);
113 else if(w!=width || h!=height || d!=depth)
114 throw IncompatibleData("Image does not match texture storage");
116 image(0, fmt, UNSIGNED_INT, img.get_data());
119 void Texture3D::require_storage()
121 if(width==0 || height==0 || depth==0)
122 throw InvalidState("Texture storage has not been specified");
125 void Texture3D::get_level_size(unsigned level, unsigned &w, unsigned &h, unsigned &d)