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),
23 void Texture2D::storage(PixelFormat fmt, unsigned wd, unsigned ht, int brd)
26 throw InvalidState("Texture storage may only be specified once");
28 throw InvalidParameterValue("Invalid texture dimensions");
36 void Texture2D::image(int level, PixelFormat fmt, DataType type, const void *data)
39 throw InvalidState("Texture storage has not been specified");
41 Bind _bind(this, true);
42 glTexImage2D(target, level, ifmt, width, height, border, fmt, type, data);
45 void Texture2D::sub_image(int level, int x, int y, unsigned wd, unsigned ht, PixelFormat fmt, DataType type, const void *data)
48 throw InvalidState("Texture storage has not been specified");
50 Bind _bind(this, true);
51 glTexSubImage2D(target, level, x, y, wd, ht, fmt, type, data);
54 void Texture2D::load_image(const string &fn)
62 void Texture2D::image(const Graphics::Image &img)
64 unsigned w = img.get_width();
65 unsigned h = img.get_height();
66 PixelFormat fmt = pixelformat_from_graphics(img.get_format());
68 storage(fmt, w, h, 0);
69 else if(w!=width || h!=height)
70 throw IncompatibleData("Image does not match texture storage");
72 image(0, fmt, UNSIGNED_BYTE, img.get_data());
76 Texture2D::Loader::Loader(Texture2D &t):
79 add("image_data", &Loader::image_data);
80 add("raw_data", &Loader::raw_data);
81 add("storage", &Loader::storage);
84 void Texture2D::Loader::image_data(const string &data)
87 img.load_memory(data.data(), data.size());
89 static_cast<Texture2D &>(obj).image(img);
92 void Texture2D::Loader::raw_data(const string &data)
94 Texture2D &t2d = static_cast<Texture2D &>(obj);
95 t2d.image(0, t2d.ifmt, UNSIGNED_BYTE, data.data());
98 void Texture2D::Loader::storage(PixelFormat fmt, unsigned w, unsigned h, unsigned b)
100 static_cast<Texture2D &>(obj).storage(fmt, w, h, b);