Texture.hh 9.33 KB
Newer Older
Robert Menzel's avatar
Robert Menzel committed
1
2
3
4
5
////////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2011, Computer Graphics Group RWTH Aachen University         //
// All rights reserved.                                                       //
////////////////////////////////////////////////////////////////////////////////

6
7
#ifndef ACGL_OPENGL_OBJECTS_TEXTURE_HH
#define ACGL_OPENGL_OBJECTS_TEXTURE_HH
Robert Menzel's avatar
Robert Menzel committed
8

Robert Menzel's avatar
Robert Menzel committed
9
10
#include <ACGL/ACGL.hh>

11
#include <ACGL/Base/Macros.hh>
Robert Menzel's avatar
Robert Menzel committed
12
13
#include <ACGL/OpenGL/GL.hh>
#include <ACGL/OpenGL/Tools.hh>
Robert Menzel's avatar
Robert Menzel committed
14
15
16
#include <ACGL/Math/Math.hh>

#include <vector>
Robert Menzel's avatar
Robert Menzel committed
17
#include <tr1/memory>
Robert Menzel's avatar
Robert Menzel committed
18
19

namespace ACGL{
Robert Menzel's avatar
Robert Menzel committed
20
namespace OpenGL{
Robert Menzel's avatar
Robert Menzel committed
21
22
23

class Texture
{
24
    ACGL_NOT_COPYABLE(Texture)
Robert Menzel's avatar
Robert Menzel committed
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44

    // ========================================================================================================= \/
    // ============================================================================================ CONSTRUCTORS \/
    // ========================================================================================================= \/
public:
    Texture(
        GLenum _internalFormat = GL_RGBA,
        GLenum _target = GL_TEXTURE_2D,
        GLenum _format = GL_RGBA,
        GLenum _type = GL_UNSIGNED_BYTE)
    :   mContext(0),
        mWidth(0),
        mHeight(0),
        mDepth(0),
        mInternalFormat(_internalFormat),
        mTarget(_target),
        mFormat(_format),
        mType(_type)
    {
        glGenTextures(1, &mContext);
45
46
47
        if (openGLCriticalErrorOccured() ) {
            ACGL::Utils::error() << "could not generate texture!" << std::endl;
        }
Robert Menzel's avatar
Robert Menzel committed
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
    }

    virtual ~Texture(void)
    {
        // context 0 will get ignored by OpenGL
        glDeleteTextures(1, &mContext);
    }

    // ==================================================================================================== \/
    // ============================================================================================ GETTERS \/
    // ==================================================================================================== \/
public:
    inline GLuint  getContext        (void) const { return mContext;        }
    inline GLsizei getWidth          (void) const { return mWidth;          }
    inline GLsizei getHeight         (void) const { return mHeight;         }
    inline GLsizei getDepth          (void) const { return mDepth;          }
    inline GLenum  getInternalFormat (void) const { return mInternalFormat; }
    inline GLenum  getTarget         (void) const { return mTarget;         }
    inline GLenum  getFormat         (void) const { return mFormat;         }
    inline GLenum  getType           (void) const { return mType;           }

    // ==================================================================================================== \/
    // ============================================================================================ SETTERS \/
    // ==================================================================================================== \/
public:
    inline void setInternalFormat (GLenum _internalFormat) { mInternalFormat = _internalFormat; }
    inline void setTarget         (GLenum _target)         { mTarget = _target;                 }
    inline void setFormat         (GLenum _format)         { mFormat = _format;                 }
    inline void setType           (GLenum _type)           { mType = _type;                     }

    // ===================================================================================================== \/
    // ============================================================================================ WRAPPERS \/
    // ===================================================================================================== \/
public:
    //! Activate texture unit and bind this texture.
    inline void bind(GLenum _textureUnit) const
    {
        glActiveTexture(GL_TEXTURE0 + _textureUnit);
        glBindTexture(mTarget, mContext);
87
        openGLRareError();
Robert Menzel's avatar
Robert Menzel committed
88
89
90
91
92
93
    }
    
    //! Bind this texture.
    inline void bind(void) const
    {
        glBindTexture(mTarget, mContext);
94
        openGLRareError();
Robert Menzel's avatar
Robert Menzel committed
95
96
    }

97
    //! Note: The function is not const, because it changes the corresponding GPU data
Robert Menzel's avatar
Robert Menzel committed
98
99
    inline void setParameter(GLenum _parameter, GLint _value)
    {
100
        glBindTexture(mTarget, mContext);
Robert Menzel's avatar
Robert Menzel committed
101
        glTexParameteri(mTarget, _parameter, _value);
102
        openGLRareError();
Robert Menzel's avatar
Robert Menzel committed
103
104
    }

105
    //! Note: The function is not const, because it changes the corresponding GPU data
Robert Menzel's avatar
Robert Menzel committed
106
107
    inline void setParameter(GLenum _parameter, GLfloat _value)
    {
108
        glBindTexture(mTarget, mContext);
Robert Menzel's avatar
Robert Menzel committed
109
        glTexParameterf(mTarget, _parameter, _value);
110
        openGLRareError();
Robert Menzel's avatar
Robert Menzel committed
111
112
113
    }

    //! Set texture size for 1D, 2D or 3D textures and NULL data
114
    inline void setSize(
Robert Menzel's avatar
Robert Menzel committed
115
116
117
118
119
120
121
122
123
124
125
126
127
        GLsizei _width,
        GLsizei _height = 0,
        GLsizei _depth = 0)
    {
        mWidth = _width;
        mHeight = _height;
        mDepth = _depth;
        if(mWidth > 0 && mHeight > 0 && mDepth > 0)
        {
            //3D
        }
        else if(mWidth > 0 && mHeight > 0)
        {
128
            setSize2D(mWidth, mHeight);
Robert Menzel's avatar
Robert Menzel committed
129
130
131
132
133
134
135
136
        }
        else if(mWidth > 0)
        {
            //1D
        }
    }

    //! Set texture size and NULL data
137
    inline void setSize2D(
Robert Menzel's avatar
Robert Menzel committed
138
139
140
141
142
        GLsizei _width,
        GLsizei _height)
    {
        mWidth = _width;
        mHeight = _height;
143
        glBindTexture(mTarget, mContext);
Robert Menzel's avatar
Robert Menzel committed
144
145
146
147
148
149
150
151
152
153
154
155
        glTexImage2D(
            mTarget,
            0,
            mInternalFormat,
            mWidth,
            mHeight,
            0,//no border
            mFormat,
            mType,
            NULL);
    }

156
157
158
159
    //! Set texture data
    inline void setImage2D(
        GLsizei       _width,
        GLsizei       _height,
Robert Menzel's avatar
Robert Menzel committed
160
161
        const GLvoid* _pData)
    {
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
        mWidth = _width;
        mHeight = _height;
        glBindTexture(mTarget, mContext);
        glTexImage2D(
            mTarget,
            0,
            mInternalFormat,
            mWidth,
            mHeight,
            0,//no border
            mFormat,
            mType,
            _pData);
    }

    //! Set texture data
    //! Note: The function is not const, because it changes the corresponding GPU data
    inline void setImage2D(
        const GLvoid* _pData)
    {
        glBindTexture(mTarget, mContext);
Robert Menzel's avatar
Robert Menzel committed
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
        glTexImage2D(
            mTarget,
            0,
            mInternalFormat,
            mWidth,
            mHeight,
            0,//no border
            mFormat,
            mType,
            _pData);
    }

    //! Set texture data for some mipmap level.
    /*!
        If you upload a texture for a higher mipmap level, the function will automatically determine
        its size calculating (w,h)/2^level where w and h are the width and height of the base texture.
199
        Note: The function is not const, because it changes the corresponding GPU data
Robert Menzel's avatar
Robert Menzel committed
200
     */
201
    inline void setImage2D(
Robert Menzel's avatar
Robert Menzel committed
202
203
204
        const GLvoid* _pData,
        GLint         _mipmapLevel)
    {
205
        glBindTexture(mTarget, mContext);
Robert Menzel's avatar
Robert Menzel committed
206
207
208
209
210
211
212
213
214
215
216
        glTexImage2D(
            mTarget,
            _mipmapLevel,
            mInternalFormat,
            mWidth  / GLsizei(Math::Functions::pow(2.0f, float(_mipmapLevel))),
            mHeight / GLsizei(Math::Functions::pow(2.0f, float(_mipmapLevel))),
            0,//no border
            mFormat,
            mType,
            _pData);
    }
217

218
    //! Set data for specific area within the 2D texture
219
220
    //! Note: The function is not const, because it changes the corresponding GPU data
    inline void setSubImage2D(
221
222
223
224
225
226
            const GLvoid* _pData,
            int _x,
            int _y,
            int _width,
            int _height)
    {
227
        glBindTexture(mTarget, mContext);
228
229
230
231
232
233
234
235
        glTexSubImage2D(
            mTarget,
            0,
            _x,
            _y,
            _width,
            _height,
            mFormat,
236
237
238
239
240
            mType,
            _pData);
    }

    //! Set data for specific area within the 3D texture
241
242
    //! Note: The function is not const, because it changes the corresponding GPU data
    inline void setSubImage3D(
243
244
245
246
247
248
249
250
            const GLvoid* _pData,
            int _x,
            int _y,
            int _z,
            int _width,
            int _height,
            int _depth)
    {
251
        glBindTexture(mTarget, mContext);
252
253
254
255
256
257
258
259
260
261
262
        glTexSubImage3D(
            mTarget,
            0,
            _x,
            _y,
            _z,
            _width,
            _height,
            _depth,
            mFormat,
            mType,
263
264
            _pData);
    }
Robert Menzel's avatar
Robert Menzel committed
265
    
266
267
268
    //! Generate mipmaps from the current base texture (i.e. the texture from level 0)
    //! Note: The function is not const, because it changes the corresponding GPU data
    void generateMipmaps(void)
Robert Menzel's avatar
Robert Menzel committed
269
    {
270
        glBindTexture(mTarget, mContext);
Robert Menzel's avatar
Robert Menzel committed
271
272
        glEnable(mTarget);
        glGenerateMipmap(mTarget);
273
        openGLRareError();
Robert Menzel's avatar
Robert Menzel committed
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
    } 

    // =================================================================================================== \/
    // ============================================================================================ FIELDS \/
    // =================================================================================================== \/
private:
    GLuint  mContext;
    GLsizei mWidth;
    GLsizei mHeight;
    GLsizei mDepth;
    GLenum  mInternalFormat;
    GLenum  mTarget;
    GLenum  mFormat;
    GLenum  mType;
};

290
291
ACGL_SHARED_TYPEDEF(Texture)

Robert Menzel's avatar
Robert Menzel committed
292
293

} // OpenGL
Robert Menzel's avatar
Robert Menzel committed
294
295
} // ACGL

296
#endif // ACGL_OPENGL_OBJECTS_TEXTURE_HH