]> git.tdb.fi Git - libs/gl.git/blob - source/texture.h
6d26f23684200405018cfd1f56c3286c32d75227
[libs/gl.git] / source / texture.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_TEXTURE_H_
9 #define MSP_GL_TEXTURE_H_
10
11 #include <istream>
12 #include <msp/datafile/objectloader.h>
13 #include "gl.h"
14 #include "predicate.h"
15
16 namespace Msp {
17 namespace GL {
18
19 enum TextureFilter
20 {
21         NEAREST                = GL_NEAREST,
22         LINEAR                 = GL_LINEAR,
23         NEAREST_MIPMAP_NEAREST = GL_NEAREST_MIPMAP_NEAREST,
24         NEAREST_MIPMAP_LINEAR  = GL_NEAREST_MIPMAP_LINEAR,
25         LINEAR_MIPMAP_NEAREST  = GL_LINEAR_MIPMAP_NEAREST,
26         LINEAR_MIPMAP_LINEAR   = GL_LINEAR_MIPMAP_LINEAR
27 };
28
29 std::istream &operator>>(std::istream &, TextureFilter &);
30
31
32 enum TextureWrap
33 {
34         REPEAT          = GL_REPEAT,
35         CLAMP_TO_EDGE   = GL_CLAMP_TO_EDGE,
36         MIRRORED_REPEAT = GL_MIRRORED_REPEAT
37 };
38
39
40 /**
41 Base class for textures.  This class only defines operations common for all
42 texture types and is not instantiable.  For specifying images for textures, see
43 one of the dimensioned texture classes.
44 */
45 class Texture
46 {
47 protected:
48         class Loader: public DataFile::ObjectLoader<Texture>
49         {
50         public:
51                 Loader(Texture &);
52                 void generate_mipmap(bool);
53                 void mag_filter(TextureFilter);
54                 void min_filter(TextureFilter);
55                 void wrap(TextureWrap);
56                 void wrap_r(TextureWrap);
57                 void wrap_s(TextureWrap);
58                 void wrap_t(TextureWrap);
59         };
60
61         enum ParameterMask
62         {
63                 MIN_FILTER = 1,
64                 MAG_FILTER = 2,
65                 WRAP_S = 4,
66                 WRAP_T = 8,
67                 WRAP_R = 16,
68                 GENERATE_MIPMAP = 32,
69                 COMPARE = 64,
70                 COMPARE_FUNC = 128
71         };
72
73         unsigned id;
74         GLenum target;
75         TextureFilter min_filter;
76         TextureFilter mag_filter;
77         TextureWrap wrap_s;
78         TextureWrap wrap_t;
79         TextureWrap wrap_r;
80         bool gen_mipmap;
81         bool compare;
82         Predicate cmp_func;
83         mutable int dirty_params;
84
85         Texture(GLenum);
86         Texture(const Texture &);
87         Texture &operator=(const Texture &);
88 public:
89         ~Texture();
90
91 protected:
92         void update_parameter(int) const;
93 public:
94         void set_min_filter(TextureFilter);
95         void set_mag_filter(TextureFilter);
96         void set_wrap(TextureWrap);
97         void set_wrap_s(TextureWrap);
98         void set_wrap_t(TextureWrap);
99         void set_wrap_r(TextureWrap);
100         void set_generate_mipmap(bool);
101         void set_compare_enabled(bool);
102         void set_compare_func(Predicate);
103         GLenum get_target() const { return target; }
104         unsigned get_id() const { return id; }
105
106         void bind() const;
107         void bind_to(unsigned) const;
108
109         static const Texture *current();
110         static void unbind();
111         static void unbind_from(unsigned);
112 };
113
114 } // namespace GL
115 } // namespace Msp
116
117 #endif