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