TextureData.hh 5.25 KB
Newer Older
1
2
3
4
5
/***********************************************************************
 * Copyright 2011-2012 Computer Graphics Group RWTH Aachen University. *
 * All rights reserved.                                                *
 * Distributed under the terms of the MIT License (see LICENSE.TXT).   *
 **********************************************************************/
6
7
8
9

#ifndef ACGL_OPENGL_DATA_TEXTUREDATA_HH
#define ACGL_OPENGL_DATA_TEXTUREDATA_HH

Robert Menzel's avatar
Robert Menzel committed
10
11
12
13
14
/**
 * TextureData holds the data of a 1,2 or 3 dimensional image to be used as a texture.
 * This structure is used for texture loading.
 */

15
16
17
18
#include <ACGL/ACGL.hh>

#include <ACGL/Base/Macros.hh>
#include <ACGL/OpenGL/GL.hh>
Robert Menzel's avatar
Robert Menzel committed
19
#include <ACGL/OpenGL/Tools.hh>
20
#include <ACGL/Math/Math.hh>
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36

namespace ACGL{
namespace OpenGL{

class TextureData
{
    // ========================================================================================================= \/
    // ============================================================================================ CONSTRUCTORS \/
    // ========================================================================================================= \/
public:
    TextureData(void)
    :   pData(NULL),
        width(0),
        height(0),
        depth(0),
        format(GL_RGBA),
37
        mType(GL_UNSIGNED_BYTE),
Robert Menzel's avatar
Robert Menzel committed
38
        paddingBytesPerRow(0)
39
40
41
42
43
44
45
46
47
48
49
    {}
    virtual ~TextureData(void)
    {
        delete[] pData;
    }

    // ========================================================================================================= \/
    // ================================================================================================= GETTERS \/
    // ========================================================================================================= \/
public:
    GLubyte* getData    (void) const { return pData;   }
50
51

    //! width in pixel
52
    GLsizei  getWidth   (void) const { return width;   }
53
54

    //! height in pixel
55
    GLsizei  getHeight  (void) const { return height;  }
56
57

    //! depth in pixel
58
    GLsizei  getDepth   (void) const { return depth;   }
59
60

    //! channels etc. e.g. GL_RGB
61
    GLenum   getFormat  (void) const { return format;  }
62
63
64
65
66

    //! data type (e.g. GL_BYTE)
    GLenum   getType    (void) const { return mType;   }

    //! each line can have a few bytes of padding, number of bytes is returned
Robert Menzel's avatar
Robert Menzel committed
67
    GLsizei  getPadding (void) const { return paddingBytesPerRow; }
68
69

    //! in bytes
Robert Menzel's avatar
Robert Menzel committed
70
    glm::uvec3 getSize  (void) const { return glm::uvec3( width, height, depth ); }
Robert Menzel's avatar
Robert Menzel committed
71
72
73
74
75
76

    //! the byte alignment of each pixel row, e.g. 1,2,4,8 (bytes)
    GLsizei  getPackAlignment()const;

    //! 1,2,3 or 4 based on format
    GLsizei  getNumberOfChannels() const;
77
78
79

    //! flips the image horizontally as some image formats have a different coordinate system as OpenGL has.
    void flipHorizontally();
80
81
82
83
84
85
86
87
88
89

    //! returns the texel converted to float (0..1 in case the data was int, -1..1 in case it was unsigned int).
    //! If the Image had less than 4 components it get's filled with (0,0,0,1)
    //! _texCoord is NOT normalized to 0..1! and gets clamped to the actual image size
    //! NOTE: this might be slow, for performance get the raw pointer and work on that!
    glm::vec4 getTexel( glm::uvec2 _texCoord );

    //! sets one texel, if the texture has less color components than 4, the superfluous components get ignored.
    //! in case the texture is int, the values from 0..1 will get scaled and clamped if needed.
    void setTexel( glm::uvec2 _texCoord, glm::vec4 _color );
90
91
92
93
    // ========================================================================================================= \/
    // ================================================================================================= SETTERS \/
    // ========================================================================================================= \/
public:
Robert Menzel's avatar
Robert Menzel committed
94
    //! _pData has to be created by new GLubyte[...] and will get deleted by this TextureData object!
95
96
97
98
99
    void setData  (GLubyte* _pData)  { pData  = _pData;  }
    void setWidth (GLsizei  _width)  { width  = _width;  }
    void setHeight(GLsizei  _height) { height = _height; }
    void setDepth (GLsizei  _depth)  { depth  = _depth;  }
    void setFormat(GLenum   _format) { format = _format; }
100
    void setType  (GLenum   _type)   { mType  = _type;   }
Robert Menzel's avatar
Robert Menzel committed
101
    void setPadding(GLsizei _padding){ paddingBytesPerRow = _padding; }
102
    void setSize  (const glm::uvec3 &_size) { width  = _size.x; height = _size.y; depth = _size.z; }
103
104
105
106
107

    // ========================================================================================================= \/
    // ================================================================================================== FIELDS \/
    // ========================================================================================================= \/
private:
108
    size_t   getBytesPerScanline() const;
109
110
111
112
    GLubyte* pData;
    GLsizei  width;
    GLsizei  height;
    GLsizei  depth;
Robert Menzel's avatar
Robert Menzel committed
113
    GLenum   format; // channel types and count
114
    GLenum   mType;   // data type
Robert Menzel's avatar
Robert Menzel committed
115
116
    GLsizei  paddingBytesPerRow; // number of padding bytes added per row: glReadPixel can read with padding and
                                 // some image writers support/need this as well (e.g. QT)
117
118
};

Robert Menzel's avatar
Robert Menzel committed
119
ACGL_SMARTPOINTER_TYPEDEFS(TextureData)
120
121
122
123
124

} // OpenGL
} // ACGL

#endif // ACGL_OPENGL_DATA_TEXTUREDATA_HH