]> git.tdb.fi Git - libs/gl.git/blob - source/texture2darray.cpp
1ecb4cf2be69e1b34a28f1f8b8cea643b84e2325
[libs/gl.git] / source / texture2darray.cpp
1 #include <msp/datafile/collection.h>
2 #include <msp/gl/extensions/ext_texture_array.h>
3 #include "error.h"
4 #include "pixelstore.h"
5 #include "texture2darray.h"
6
7 using namespace std;
8
9 namespace Msp {
10 namespace GL {
11
12 Texture2DArray::Texture2DArray():
13         Texture3D(GL_TEXTURE_2D_ARRAY)
14 {
15         static Require _req(EXT_texture_array);
16 }
17
18 void Texture2DArray::layer_image(unsigned level, unsigned z, const void *data)
19 {
20         unsigned w = get_width();
21         unsigned h = get_height();
22         unsigned d = get_depth();
23         get_level_size(level, w, h, d);
24
25         sub_image(level, 0, 0, z, w, h, 1, data);
26 }
27
28 void Texture2DArray::layer_image(unsigned level, unsigned z, PixelComponents comp, DataType type, const void *data)
29 {
30         if(comp!=get_components(format) || type!=get_component_type(format))
31                 throw incompatible_data("Texture2DArray::layer_image");
32         layer_image(level, z, data);
33 }
34
35 void Texture2DArray::layer_image(unsigned level, unsigned z, const Graphics::Image &img)
36 {
37         if(!get_width())
38                 throw invalid_operation("Texture2DArray::layer_image");
39
40         unsigned w = img.get_width();
41         unsigned h = img.get_height();
42         if(w!=get_width() || h!=get_height())
43                 throw incompatible_data("Texture2DArray::layer_image");
44         PixelFormat fmt = pixelformat_from_image(img);
45         if(get_components(fmt)!=get_components(format) || get_component_type(fmt)!=get_component_type(format))
46                 throw incompatible_data("Texture2DArray::layer_image");
47
48         PixelStore pstore = PixelStore::from_image(img);
49         BindRestore _bind_ps(pstore);
50
51         layer_image(level, z, img.get_pixels());
52 }
53
54
55 Texture2DArray::Loader::Loader(Texture2DArray &t):
56         DataFile::DerivedObjectLoader<Texture2DArray, Texture3D::Loader>(t)
57 {
58         init();
59 }
60
61 Texture2DArray::Loader::Loader(Texture2DArray &t, Collection &c):
62         DataFile::DerivedObjectLoader<Texture2DArray, Texture3D::Loader>(t, c)
63 {
64         init();
65 }
66
67 void Texture2DArray::Loader::init()
68 {
69         add("external_image", &Loader::external_image);
70 }
71
72 void Texture2DArray::Loader::external_image(unsigned z, const string &fn)
73 {
74         Graphics::Image img;
75         load_external_image(img, fn);
76         obj.layer_image(0, z, img);
77 }
78
79 } // namespace GL
80 } // namespace Msp