]> git.tdb.fi Git - libs/gl.git/blob - source/texture2d.h
Support linear to sRGB conversion when loading materials and textures
[libs/gl.git] / source / texture2d.h
1 #ifndef MSP_GL_TEXTURE2D_H_
2 #define MSP_GL_TEXTURE2D_H_
3
4 #include <string>
5 #include <msp/graphics/image.h>
6 #include "datatype.h"
7 #include "pixelformat.h"
8 #include "texture.h"
9
10 namespace Msp {
11 namespace GL {
12
13 /**
14 Two-dimensional texture.  Consists of an array of texels in the shape of a
15 rectangle.  Texture coordinate have a principal range of [0, 1].  This is the
16 most common type of texture.
17 */
18 class Texture2D: public Texture
19 {
20 public:
21         class Loader: public Msp::DataFile::DerivedObjectLoader<Texture2D, Texture::Loader>
22         {
23         public:
24                 Loader(Texture2D &);
25                 Loader(Texture2D &, Collection &);
26         private:
27                 void init();
28
29                 void image_data(const std::string &);
30                 void raw_data(const std::string &);
31                 void storage(PixelFormat, unsigned, unsigned);
32                 void storage_b(PixelFormat, unsigned, unsigned, unsigned);
33         };
34
35 private:
36         PixelFormat ifmt;
37         unsigned width;
38         unsigned height;
39         unsigned allocated;
40
41 public:
42         Texture2D();
43
44         /** Defines storage structure for the texture.  Must be called before an
45         image can be uploaded.  Once storage is defined, it can't be changed. */
46         void storage(PixelFormat fmt, unsigned wd, unsigned ht);
47
48         /** Allocates storage for the texture.  The contents are initially
49         undefined.  If storage has already been allocated, does nothing. */
50         void allocate(unsigned level);
51
52         /** Uploads an image to the texture.  Storage must be defined beforehand.
53         The image data must have dimensions and format compatible with the defined
54         storage. */
55         void image(unsigned level, PixelFormat fmt, DataType type, const void *data);
56
57         /** Updates a rectangular region of the texture.  Storage must be defined
58         and allocated beforehand.  The update region must be fully inside the
59         texture. */
60         void sub_image(unsigned level, int x, int y, unsigned wd, unsigned ht,
61                 PixelFormat fmt, DataType type, const void *data);
62
63         /** Loads an image from a file and uploads it to the texture.  If storage
64         has not been defined, it will be set to match the loaded image.  Otherwise
65         the image must be compatible with the defined storage. */
66         void load_image(const std::string &fn, bool srgb = false);
67
68         /** Uploads an image to the texture.  If storage has not been defined, it
69         will be set to match the image.  Otherwise the image must be compatible with
70         the defined storage. */
71         void image(const Graphics::Image &, bool srgb = false);
72
73         unsigned get_width() const { return width; }
74         unsigned get_height() const { return height; }
75
76 private:
77         void get_level_size(unsigned, unsigned &, unsigned &);
78 };
79
80 } // namespace GL
81 } // namespace Msp
82
83 #endif