]> git.tdb.fi Git - libs/gl.git/blob - source/texture2d.cpp
Use DevIL for loading images
[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 "ilwrap.h"
10 #include "texture2d.h"
11
12 using namespace std;
13
14 #include <iostream>
15
16 namespace Msp {
17 namespace GL {
18
19 Texture2D::Texture2D():
20         width(0),
21         height(0)
22 {
23         target=GL_TEXTURE_2D;
24         bind();
25 }
26
27 void Texture2D::storage(PixelFormat fmt, sizei wd, sizei ht, int brd)
28 {
29         if(width>0)
30                 throw InvalidState("Texture storage may only be specified once");
31         if(wd==0 || ht==0)
32                 throw InvalidParameterValue("Invalid texture dimensions");
33
34         ifmt=fmt;
35         width=wd;
36         height=ht;
37         border=brd;
38 }
39
40 void Texture2D::image(int level, PixelFormat fmt, GLenum type, const void *data)
41 {
42         if(width==0)
43                 throw InvalidState("Texture storage has not been specified");
44
45         maybe_bind();
46
47         glTexImage2D(target, level, ifmt, width, height, border, fmt, type, data);
48 }
49
50 void Texture2D::sub_image(int level, int x, int y, sizei wd, sizei ht, PixelFormat fmt, GLenum type, const void *data)
51 {
52         if(width==0)
53                 throw InvalidState("Texture storage has not been specified");
54
55         maybe_bind();
56
57         glTexSubImage2D(target, level, x, y, wd, ht, fmt, type, data);
58 }
59
60 void Texture2D::load_image(const string &fn)
61 {
62         Image img;
63         img.load(fn);
64
65         unsigned w=img.get_width();
66         unsigned h=img.get_height();
67         PixelFormat fmt=img.get_format();
68         if(width==0)
69                 storage(fmt, w, h, 0);
70         else if(w!=width || h!=height)
71                 throw IncompatibleData("Image does not match texture storage");
72
73         image(0, fmt, GL_UNSIGNED_BYTE, img.get_data());
74 }
75
76 } // namespace GL
77 } // namespace Msp