3 This file is part of libmspgl
4 Copyright © 2007 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
10 #include "texture2d.h"
17 Texture2D::Texture2D():
18 Texture(GL_TEXTURE_2D),
24 void Texture2D::storage(PixelFormat fmt, unsigned wd, unsigned ht)
27 throw InvalidState("Texture storage may only be specified once");
29 throw InvalidParameterValue("Invalid texture dimensions");
36 void Texture2D::allocate(unsigned level)
38 if(allocated&(1<<level))
41 image(level, get_base_pixelformat(ifmt), UNSIGNED_BYTE, 0);
44 void Texture2D::image(unsigned level, PixelFormat fmt, DataType type, const void *data)
50 get_level_size(level, w, h);
52 Bind _bind(this, true);
53 glTexImage2D(target, level, ifmt, w, h, 0, fmt, type, data);
55 allocated |= 1<<level;
56 if(gen_mipmap && level==0)
58 for(; (w || h); w>>=1, h>>=1, ++level) ;
59 allocated |= (1<<level)-1;
63 void Texture2D::sub_image(unsigned level, int x, int y, unsigned wd, unsigned ht, PixelFormat fmt, DataType type, const void *data)
68 Bind _bind(this, true);
69 glTexSubImage2D(target, level, x, y, wd, ht, fmt, type, data);
72 void Texture2D::load_image(const string &fn)
80 void Texture2D::image(const Graphics::Image &img)
82 unsigned w = img.get_width();
83 unsigned h = img.get_height();
84 PixelFormat fmt = pixelformat_from_graphics(img.get_format());
87 else if(w!=width || h!=height)
88 throw IncompatibleData("Image does not match texture storage");
90 image(0, fmt, UNSIGNED_BYTE, img.get_data());
93 void Texture2D::require_storage()
95 if(width==0 || height==0)
96 throw InvalidState("Texture storage has not been specified");
99 void Texture2D::get_level_size(unsigned level, unsigned &w, unsigned &h)
111 Texture2D::Loader::Loader(Texture2D &t):
114 add("image_data", &Loader::image_data);
115 add("raw_data", &Loader::raw_data);
116 add("storage", &Loader::storage);
117 add("storage", &Loader::storage_b);
120 void Texture2D::Loader::image_data(const string &data)
123 img.load_memory(data.data(), data.size());
125 static_cast<Texture2D &>(obj).image(img);
128 void Texture2D::Loader::raw_data(const string &data)
130 Texture2D &t2d = static_cast<Texture2D &>(obj);
131 t2d.image(0, t2d.ifmt, UNSIGNED_BYTE, data.data());
134 void Texture2D::Loader::storage(PixelFormat fmt, unsigned w, unsigned h)
136 static_cast<Texture2D &>(obj).storage(fmt, w, h);
139 void Texture2D::Loader::storage_b(PixelFormat fmt, unsigned w, unsigned h, unsigned)