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