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