]> git.tdb.fi Git - libs/gl.git/blob - source/texture2d.cpp
Get rid of the generic Texture::parameter method
[libs/gl.git] / source / texture2d.cpp
1 /* $Id$
2
3 This file is part of libmspgl
4 Copyright © 2007  Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
6 */
7
8 #include "bindable.h"
9 #include "except.h"
10 #include "texture2d.h"
11
12 using namespace std;
13
14 namespace Msp {
15 namespace GL {
16
17 Texture2D::Texture2D():
18         Texture(GL_TEXTURE_2D),
19         width(0),
20         height(0)
21 { }
22
23 void Texture2D::storage(PixelFormat fmt, unsigned wd, unsigned ht, int brd)
24 {
25         if(width>0)
26                 throw InvalidState("Texture storage may only be specified once");
27         if(wd==0 || ht==0)
28                 throw InvalidParameterValue("Invalid texture dimensions");
29
30         ifmt = fmt;
31         width = wd;
32         height = ht;
33         border = brd;
34 }
35
36 void Texture2D::image(int level, PixelFormat fmt, DataType type, const void *data)
37 {
38         if(width==0)
39                 throw InvalidState("Texture storage has not been specified");
40
41         Bind _bind(this, true);
42         glTexImage2D(target, level, ifmt, width, height, border, fmt, type, data);
43 }
44
45 void Texture2D::sub_image(int level, int x, int y, unsigned wd, unsigned ht, PixelFormat fmt, DataType type, const void *data)
46 {
47         if(width==0)
48                 throw InvalidState("Texture storage has not been specified");
49
50         Bind _bind(this, true);
51         glTexSubImage2D(target, level, x, y, wd, ht, fmt, type, data);
52 }
53
54 void Texture2D::load_image(const string &fn)
55 {
56         Graphics::Image img;
57         img.load_file(fn);
58
59         image(img);
60 }
61
62 void Texture2D::image(const Graphics::Image &img)
63 {
64         unsigned w = img.get_width();
65         unsigned h = img.get_height();
66         PixelFormat fmt = pixelformat_from_graphics(img.get_format());
67         if(width==0)
68                 storage(fmt, w, h, 0);
69         else if(w!=width || h!=height)
70                 throw IncompatibleData("Image does not match texture storage");
71
72         image(0, fmt, UNSIGNED_BYTE, img.get_data());
73 }
74
75
76 Texture2D::Loader::Loader(Texture2D &t):
77         Texture::Loader(t)
78 {
79         add("image_data", &Loader::image_data);
80         add("raw_data", &Loader::raw_data);
81         add("storage", &Loader::storage);
82 }
83
84 void Texture2D::Loader::image_data(const string &data)
85 {
86         Graphics::Image img;
87         img.load_memory(data.data(), data.size());
88
89         static_cast<Texture2D &>(obj).image(img);
90 }
91
92 void Texture2D::Loader::raw_data(const string &data)
93 {
94         Texture2D &t2d = static_cast<Texture2D &>(obj);
95         t2d.image(0, t2d.ifmt, UNSIGNED_BYTE, data.data());
96 }
97
98 void Texture2D::Loader::storage(PixelFormat fmt, unsigned w, unsigned h, unsigned b)
99 {
100         static_cast<Texture2D &>(obj).storage(fmt, w, h, b);
101 }
102
103 } // namespace GL
104 } // namespace Msp