Currently job artifacts in CI/CD pipelines on LRZ GitLab never expire. Starting from Wed 26.1.2022 the default expiration time will be 30 days (GitLab default). Currently existing artifacts in already completed jobs will not be affected by the change. The latest artifacts for all jobs in the latest successful pipelines will be kept. More information: https://gitlab.lrz.de/help/user/admin_area/settings/continuous_integration.html#default-artifacts-expiration

texture.h 8.05 KB
Newer Older
schultezub's avatar
schultezub committed
1
2
/**********************************************************************
 *                                                                    *
3
 * cgt - CAMP Graphics Toolbox, Copyright (C) 2012-2015               *
4
5
6
 *     Chair for Computer Aided Medical Procedures                    *
 *     Technische Universitaet Muenchen, Germany.                     *
 *     <http://campar.in.tum.de/>                                     *
schultezub's avatar
schultezub committed
7
 *                                                                    *
8
9
10
11
 * forked from tgt - Tiny Graphics Toolbox, Copyright (C) 2006-2011   *
 *     Visualization and Computer Graphics Group, Department of       *
 *     Computer Science, University of Muenster, Germany.             *
 *     <http://viscg.uni-muenster.de>                                 *
schultezub's avatar
schultezub committed
12
 *                                                                    *
13
 * This file is part of the cgt library. This library is free         *
schultezub's avatar
schultezub committed
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
 * software; you can redistribute it and/or modify it under the terms *
 * of the GNU Lesser General Public License version 2.1 as published  *
 * by the Free Software Foundation.                                   *
 *                                                                    *
 * This library is distributed in the hope that it will be useful,    *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of     *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the       *
 * GNU Lesser General Public License for more details.                *
 *                                                                    *
 * You should have received a copy of the GNU Lesser General Public   *
 * License in the file "LICENSE.txt" along with this library.         *
 * If not, see <http://www.gnu.org/licenses/>.                        *
 *                                                                    *
 **********************************************************************/

29
30
#ifndef CGT_TEXTURE_H
#define CGT_TEXTURE_H
schultezub's avatar
schultezub committed
31
32

#include <string>
33
#include "cgt/cgt_gl.h"
34
35
#include "cgt/types.h"
#include "cgt/vector.h"
schultezub's avatar
schultezub committed
36

37
namespace cgt {
schultezub's avatar
schultezub committed
38
39
40
41

/**
 * OpenGL Texture
 */
42
class CGT_API Texture {
schultezub's avatar
schultezub committed
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
public:
    friend class TextureManager;
    friend class TextureReader;

    enum Filter {
        NEAREST,
        LINEAR,
        MIPMAP,
        ANISOTROPIC
    };

    enum Wrapping {
        REPEAT = GL_REPEAT,
        CLAMP_TO_EDGE = GL_CLAMP_TO_EDGE,
        CLAMP_TO_BORDER = GL_CLAMP_TO_BORDER,
        MIRRORED_REPEAT = GL_MIRRORED_REPEAT
    };

    /**
62
63
64
65
66
67
     * Creates an empty texture with the given parameters.
     * 
     * \param   type            OpenGL texture type
     * \param   dimensions      Size of the texture
     * \param   internalFormat  Internal storage format
     * \param   filter          Filter to apply during sampling (defaults to linear)
schultezub's avatar
schultezub committed
68
     */
69
    Texture(GLenum type, const cgt::ivec3& dimensions, GLint internalFormat, Filter filter = LINEAR);
schultezub's avatar
schultezub committed
70
71

    /**
72
73
74
75
76
77
78
79
80
81
     * Creates a new OpenGL texture and initializes it with the given data.
     * \note    Works the same as constructing with the non-uploading ctor and then 
     *          calling uploadTexture(data, format, dataType).
     * \param type              OpenGL texture type
     * \param dimensions        Size of the texture
     * \param internalformat    Internal storage format
     * \param data              Pointer to data to upload, may be 0, but then you could use the other ctor directly...
     * \param format            OpenGL Format of the data in \a data.
     * \param dataType          OpenGL data type of the data in \a data.
     * \param filter            Filter to apply during sampling (defaults to linear)
schultezub's avatar
schultezub committed
82
     */
83
84
    Texture(GLenum type, const cgt::ivec3& dimensions, GLint internalFormat, 
            GLubyte* data, GLint format, GLenum dataType, Filter filter = LINEAR);
schultezub's avatar
schultezub committed
85
86
87
88
89

    /**
    * The destructor deletes the Texture in OpenGL.
    * Handled by the texturemanager!
    */
90
    virtual ~Texture();
schultezub's avatar
schultezub committed
91
92
93
94
95
96
97
98


    /// calculates the bytes per pixel from format dataType and dataType
    static int calcBpp(GLint format, GLenum dataType);

    /// calculates the bytes per pixel from the internal format
    static int calcBpp(GLint internalformat);

99
100
    /// calculates the number of channels from the passed internal format
    static int calcNumChannels(GLint internalFormat);
schultezub's avatar
schultezub committed
101

102
103
    static GLint calcMatchingFormat(GLint internalFormat);
    static GLenum calcMatchingDataType(GLint internalFormat);
schultezub's avatar
schultezub committed
104
105

    /**
106
107
108
109
110
111
112
113
     * Determines the best-matching internal format for the given OpenGL format and data type
     * \param   format      OpenGL Format of the data
     * \param   dataType    OpenGL data type of the data
     */
    static GLint calcInternalFormat(GLint format, GLenum dataType);

    /// calculates size on the GPU (using internalformat)
    int getSizeOnGPU() const;
schultezub's avatar
schultezub committed
114
115
116
117
118
119

    /**
     * Bind the texture to the active texture unit and target.
     *
     * Note: This does not enable texturing (use enable()).
     */
120
    void bind() const {
schultezub's avatar
schultezub committed
121
122
123
        glBindTexture(type_ , id_);
    }

124
125
126
    /**
     * unbind the current texture from the active texture unit and target.
     */
127
    void unbind() const {
128
129
130
        glBindTexture(type_, 0);
    }

schultezub's avatar
schultezub committed
131
132

    /**
133
     *   Return OpenGL texture ID
schultezub's avatar
schultezub committed
134
135
136
137
     */
    GLuint getId() const { return id_; }

    /**
138
139
140
     *   Generate OpenGL texture ID
     *   @return The generated ID
     */
schultezub's avatar
schultezub committed
141
142
143
144
145
146
147
148
149
150
151
152
153
154
    GLuint generateId() {
        id_ = 0;
        glGenTextures(1, &id_);
        return id_;
    }

    std::string getName() const { return name_; }
    void setName(const std::string& name) { name_ = name; }

    /**
    *   Returns type of the texture (GL_TEXTURE_1D...3D, GL_TEXTURE_RECTANGLE_ARB)
    */
    GLenum getType() const { return type_; }

155
    cgt::ivec3 getDimensions() const { return dimensions_;}
schultezub's avatar
schultezub committed
156
157
158
159
160
    int getWidth() const { return dimensions_.x; };
    int getHeight() const { return dimensions_.y; }
    int getDepth() const { return dimensions_.z; }
    GLint getInternalFormat() const { return internalformat_; }
    Filter getFilter() const { return filter_; }
161
162
    size_t getNumChannels() const { return calcNumChannels(internalformat_); }

schultezub's avatar
schultezub committed
163
164
165
166
167
168
169
170
171
172
173

    /**
    *   Returns the Bytes Per Pixel used,
    *   usually 3 or 4 for 24/32 bits per pixel.
    */
    GLubyte getBpp() const { return bpp_; }

    /**
    *   Set Priority of this texture in GL
    *   @param p Priority, [0...1]
    */
174
    void setPriority(GLclampf p);
schultezub's avatar
schultezub committed
175
176
177
178

    /**
    *   Check if texture is in resident GL memory
    */
179
    bool isResident() const;
schultezub's avatar
schultezub committed
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199

    /**
     *   Sets Filtering for Texture. Binds the texture.
     */
    void setFilter(Filter filter);

    /**
     * Applies the stored filter once again. Binds the texture.
     */
    void applyFilter();

    /// Set texture wrapping mode. Binds the texture.
    void setWrapping(Wrapping w);

    /// Applies the textur wrapping mode once againg. Binds the texture.
    void applyWrapping();

    Wrapping getWrapping() const { return wrapping_; }

    /**
200
     * Upload the given data to the texture. Binds the texture.
schultezub's avatar
schultezub committed
201
     *
202
203
204
     * \param data              Pointer to data to upload, may be 0, but then this is a NOP.
     * \param format            OpenGL Format of the data in \a data.
     * \param dataType          OpenGL data type of the data in \a data.
schultezub's avatar
schultezub committed
205
     */
206
    void uploadTexture(const GLubyte* data, GLint format, GLenum dataType);
207
    
schultezub's avatar
schultezub committed
208
209
210
211
212
213
    /**
     * Download texture from the GPU to a newly allocated buffer with
     * the passed format/data type and the texture's dimensions.
     */
    GLubyte* downloadTextureToBuffer(GLint format, GLenum dataType) const;

214
215
216
217
218
219
    /**
     * Returns, whether texture is a depth texture.
     * \return  internalformat_ == GL_DEPTH_COMPONENT
     */
    bool isDepthTexture() const;

schultezub's avatar
schultezub committed
220
protected:
221
    cgt::ivec3 dimensions_;
schultezub's avatar
schultezub committed
222
223
224
225
226
227
228
229
230
231
232
233
    GLint internalformat_;  ///< GL_RGB...
    Filter filter_;
    Wrapping wrapping_;

    GLuint id_;             ///< OpenGL texture id

    GLenum type_;           ///< 1D, 2D, 3D
    GLubyte bpp_;           ///< bytes per pixel

    std::string name_;      ///< optional, e.g. for storing texture file name

    // used internally in the constructors
234
    void init();
schultezub's avatar
schultezub committed
235
236
};

237
} // namespace cgt
schultezub's avatar
schultezub committed
238

239
#endif // CGT_TEXTURE_H