Texture.hh 8.37 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
11
12
#include <ACGL/ACGL.hh>

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

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

namespace ACGL{
Robert Menzel's avatar
Robert Menzel committed
19
namespace OpenGL{
Robert Menzel's avatar
Robert Menzel committed
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47

class Texture
{
    // ======================================================================================================== \/
    // ============================================================================================ GPU CONTEXT \/
    // ======================================================================================================== \/
private:
    static GLuint sTextureContext;

    // ========================================================================================================= \/
    // ============================================================================================ 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);
48
49
50
51
        if (openGLCriticalErrorOccured() ) {
            ACGL::Utils::error() << "could not generate texture!" << std::endl;
        }

Robert Menzel's avatar
Robert Menzel committed
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
    }

    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;                     }

    inline void setSize(
        GLsizei _width = 0,
        GLsizei _height = 0,
        GLsizei _depth = 0)
    {
        mWidth = _width;
        mHeight = _height;
        mDepth = _depth;
    }

    // ===================================================================================================== \/
    // ============================================================================================ WRAPPERS \/
    // ===================================================================================================== \/
public:
    //! Activate texture unit and bind this texture.
    inline void bind(GLenum _textureUnit) const
    {
        if(sTextureContext == mContext)
            return;
        glActiveTexture(GL_TEXTURE0 + _textureUnit);
        glBindTexture(mTarget, mContext);
103
        openGLRareError();
Robert Menzel's avatar
Robert Menzel committed
104
105
106
107
108
109
110
111
112
        sTextureContext = mContext;
    }
    
    //! Bind this texture.
    inline void bind(void) const
    {
        if(sTextureContext == mContext)
            return;
        glBindTexture(mTarget, mContext);
113
        openGLRareError();
Robert Menzel's avatar
Robert Menzel committed
114
115
116
117
118
119
120
        sTextureContext = mContext;
    }

    inline void setParameter(GLenum _parameter, GLint _value)
    {
        bind();
        glTexParameteri(mTarget, _parameter, _value);
121
        openGLRareError();
Robert Menzel's avatar
Robert Menzel committed
122
123
124
125
126
127
    }

    inline void setParameter(GLenum _parameter, GLfloat _value)
    {
        bind();
        glTexParameterf(mTarget, _parameter, _value);
128
        openGLRareError();
Robert Menzel's avatar
Robert Menzel committed
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
    }

    //! Set texture size for 1D, 2D or 3D textures and NULL data
    inline void setRenderTarget(
        GLsizei _width,
        GLsizei _height = 0,
        GLsizei _depth = 0)
    {
        bind();
        mWidth = _width;
        mHeight = _height;
        mDepth = _depth;
        if(mWidth > 0 && mHeight > 0 && mDepth > 0)
        {
            //3D
        }
        else if(mWidth > 0 && mHeight > 0)
        {
            glTexImage2D(
                mTarget,
                0,
                mInternalFormat,
                mWidth,
                mHeight,
                0,//no border
                mFormat,
                mType,
                NULL);
        }
        else if(mWidth > 0)
        {
            //1D
        }
    }

    //! Set texture size and NULL data
    inline void setRenderTarget2D(
        GLsizei _width,
        GLsizei _height)
    {
        bind();
        mWidth = _width;
        mHeight = _height;
        glTexImage2D(
            mTarget,
            0,
            mInternalFormat,
            mWidth,
            mHeight,
            0,//no border
            mFormat,
            mType,
            NULL);
    }

    //! 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.
     */
    inline void setTexImage2D(
        const GLvoid* _pData)
    {
        bind();
        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.
     */
    inline void setTexImage2D(
        const GLvoid* _pData,
        GLint         _mipmapLevel)
    {
        bind();
        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);
    }
    
    //! Generate mipmaps from the current base texture (i.e. the texture from level 0).
    void generateMipmaps(void) const
    {
        bind();
        glEnable(mTarget);
        glGenerateMipmap(mTarget);
233
        openGLRareError();
Robert Menzel's avatar
Robert Menzel committed
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
    } 

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

Robert Menzel's avatar
Robert Menzel committed
250
251
252
typedef std::tr1::shared_ptr<Texture> SharedTexture;

} // OpenGL
Robert Menzel's avatar
Robert Menzel committed
253
254
} // ACGL

255
#endif // ACGL_OPENGL_OBJECTS_TEXTURE_HH