]> git.tdb.fi Git - libs/gl.git/blob - source/core/texture2darray.cpp
1928d4e3503c6f68f2446125ffe4b423c96ac5fc
[libs/gl.git] / source / core / 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         if(level>=levels || z>=depth)
21                 throw out_of_range("Texture2DArray::layer_image");
22
23         LinAl::Vector<unsigned, 3> size = get_level_size(level);
24         sub_image(level, 0, 0, z, size.x, size.y, 1, data);
25 }
26
27 void Texture2DArray::layer_image(unsigned level, unsigned z, PixelComponents comp, DataType type, const void *data)
28 {
29         if(comp!=get_components(format) || type!=get_component_type(format))
30                 throw incompatible_data("Texture2DArray::layer_image");
31         layer_image(level, z, data);
32 }
33
34 void Texture2DArray::layer_image(unsigned level, unsigned z, const Graphics::Image &img)
35 {
36         if(!get_width())
37                 throw invalid_operation("Texture2DArray::layer_image");
38
39         unsigned w = img.get_width();
40         unsigned h = img.get_height();
41         if(w!=get_width() || h!=get_height())
42                 throw incompatible_data("Texture2DArray::layer_image");
43         PixelFormat fmt = pixelformat_from_image(img);
44         if(get_components(fmt)!=get_components(format) || get_component_type(fmt)!=get_component_type(format))
45                 throw incompatible_data("Texture2DArray::layer_image");
46
47         PixelStore pstore = PixelStore::from_image(img);
48         BindRestore _bind_ps(pstore);
49
50         layer_image(level, z, img.get_pixels());
51 }
52
53
54 Texture2DArray::Loader::Loader(Texture2DArray &t):
55         DataFile::DerivedObjectLoader<Texture2DArray, Texture3D::Loader>(t)
56 {
57         init();
58 }
59
60 Texture2DArray::Loader::Loader(Texture2DArray &t, Collection &c):
61         DataFile::DerivedObjectLoader<Texture2DArray, Texture3D::Loader>(t, c)
62 {
63         init();
64 }
65
66 void Texture2DArray::Loader::init()
67 {
68         add("external_image", &Loader::external_image);
69 }
70
71 void Texture2DArray::Loader::external_image(unsigned z, const string &fn)
72 {
73         Graphics::Image img;
74         load_external_image(img, fn);
75         obj.layer_image(0, z, img);
76 }
77
78 } // namespace GL
79 } // namespace Msp