]> git.tdb.fi Git - libs/gl.git/blob - source/texture3d.cpp
Use DevIL for loading images
[libs/gl.git] / source / texture3d.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 #define GL_GLEXT_PROTOTYPES
9 #include <cmath>
10 #include "except.h"
11 #include "ilwrap.h"
12 #include "texture3d.h"
13
14 using namespace std;
15
16 namespace Msp {
17 namespace GL {
18
19 Texture3D::Texture3D():
20         width(0),
21         height(0),
22         depth(0)
23 {
24         target=GL_TEXTURE_3D;
25         bind();
26 }
27
28 void Texture3D::storage(PixelFormat f, sizei w, sizei h, sizei d, int b)
29 {
30         if(width>0)
31                 throw InvalidState("Textures may only be created once");
32         if(w==0 || h==0 || d==0)
33                 throw InvalidParameterValue("Invalid texture dimensions");
34
35         width=w;
36         height=h;
37         depth=d;
38         ifmt=f;
39         border=b;
40
41         image(0, ifmt, GL_UNSIGNED_BYTE, 0);
42 }
43
44 void Texture3D::image(int level, PixelFormat fmt, GLenum type, const void *data)
45 {
46         maybe_bind();
47         glTexImage3D(target, level, ifmt, width, height, depth, border, fmt, type, data);
48 }
49
50 void Texture3D::load_image(const string &fn, int dp)
51 {
52         Image img;
53         img.load(fn);
54
55         unsigned w=img.get_width();
56         unsigned h=img.get_height();
57         unsigned d=1;
58
59         if(dp==-1)
60         {
61                 if(h%w)
62                         throw IncompatibleData("Image height is not divisible by its width");
63                 d=h/w;
64                 h=w;
65         }
66         else if(dp==-2)
67         {
68                 for(d=h; d*d>h; d>>=2);
69                 for(; d*d<h; ++d);
70                 if(d*d!=h)
71                         throw IncompatibleData("Could not find a square root of texture height");
72                 h=d;
73         }
74         else if(dp>0)
75                 d=dp;
76
77         PixelFormat fmt=img.get_format();
78         if(width==0)
79                 storage(fmt, w, h, d, 0);
80         else if(w!=width || h!=height || d!=depth)
81                 throw IncompatibleData("Image does not match texture storage");
82
83         image(0, fmt, GL_UNSIGNED_INT, img.get_data());
84 }
85
86 } // namespace GL
87 } // namespace Msp