]> git.tdb.fi Git - libs/gl.git/blob - source/texture2d.h
8508b0435847eaa7eaf9485c5b0765017e4dbe6f
[libs/gl.git] / source / texture2d.h
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 #ifndef MSP_GL_TEXTURE2D_H_
9 #define MSP_GL_TEXTURE2D_H_
10
11 #include <string>
12 #include <msp/gbase/image.h>
13 #include "datatype.h"
14 #include "pixelformat.h"
15 #include "texture.h"
16
17 namespace Msp {
18 namespace GL {
19
20 /**
21 Two-dimensional texture class.  This is the most common type of texture.
22 */
23 class Texture2D: public Texture
24 {
25 public:
26         class Loader: public Texture::Loader
27         {
28         public:
29                 Loader(Texture2D &);
30         private:
31                 void image_data(const std::string &);
32                 void raw_data(const std::string &);
33                 void storage(PixelFormat, unsigned, unsigned);
34                 void storage_b(PixelFormat, unsigned, unsigned, unsigned);
35         };
36
37 private:
38         PixelFormat ifmt;
39         unsigned width;
40         unsigned height;
41         unsigned allocated;
42
43 public:
44         Texture2D();
45
46         /**
47         Defines the texture storage.  This function may only be successfully called
48         once.
49         */
50         void storage(PixelFormat fmt, unsigned wd, unsigned ht);
51
52         /** Allocates texture storage.  If storage has already been allocated, this
53         function does nothing. */
54         void allocate(unsigned level);
55         
56         /** Uploads an image to the texture.  storage() must have been called prior to
57         this, and the image must have dimensions conforming to the specified
58         storage.  For level>0, mipmapping rules apply to the image dimensions. */
59         void image(unsigned level, PixelFormat fmt, DataType type, const void *data);
60
61         /**
62         Uploads a sub-image into the texture.  Unlike full image upload, there are
63         no constraints on the size of the sub-image.
64         */
65         void sub_image(unsigned level, int x, int y, unsigned wd, unsigned ht, PixelFormat fmt, DataType type, const void *data);
66
67         /**
68         Loads an image from a file and uploads it to the texture.  If storage() has
69         not been called, the storage format will be set to match the loaded image.
70         */
71         void load_image(const std::string &fn);
72
73         unsigned get_width() const  { return width; }
74         unsigned get_height() const { return height; }
75
76 private:
77         void image(const Graphics::Image &);
78         void require_storage();
79         void get_level_size(unsigned, unsigned &, unsigned &);
80 };
81
82 } // namespace GL
83 } // namespace Msp
84
85 #endif