imagedatagl.h 8.26 KB
Newer Older
1
2
// ================================================================================================
// 
schultezub's avatar
schultezub committed
3
// This file is part of the CAMPVis Software Framework.
4
5
// 
// If not explicitly stated otherwise: Copyright (C) 2012, all rights reserved,
schultezub's avatar
schultezub committed
6
//      Christian Schulte zu Berge <christian.szb@in.tum.de>
7
8
9
//      Chair for Computer Aided Medical Procedures
//      Technische Universitt Mnchen
//      Boltzmannstr. 3, 85748 Garching b. Mnchen, Germany
schultezub's avatar
schultezub committed
10
// For a full list of authors and contributors, please refer to the file "AUTHORS.txt".
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// 
// The licensing of this softare is not yet resolved. Until then, redistribution in source or
// binary forms outside the CAMP chair is not permitted, unless explicitly stated in legal form.
// However, the names of the original authors and the above copyright notice must retain in its
// original state in any case.
// 
// Legal disclaimer provided by the BSD license:
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 
// AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
// 
// ================================================================================================

30
31
32
#ifndef IMAGEDATAGL_H__
#define IMAGEDATAGL_H__

schultezub's avatar
schultezub committed
33
#include "tgt/tgt_gl.h"
34
#include "tgt/texture.h"
schultezub's avatar
schultezub committed
35
#include "tgt/textureunit.h"
36
37
#include "tgt/vector.h"
#include "core/datastructures/imagedata.h"
38
#include "core/datastructures/genericimagedatalocal.h"
39
40
#include "core/tools/weaklytypedpointer.h"

schultezub's avatar
schultezub committed
41
42
43
44
namespace tgt {
    class Shader;
}

schultezub's avatar
schultezub committed
45
namespace campvis {
46
47
48
49
50
51
52

    /**
     * Stores image data as OpenGL texture.
     * Can bei instantiated either by a WeaklyTypedPointer or strongly typed by an ImageDataRAM.
     */
    class ImageDataGL : public ImageData {
    public:
schultezub's avatar
schultezub committed
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
        /**
         * Creates and returns new ImageDataGL representation from a tgt::Texture.
         * The caller takes ownership of the returned pointer.
         * 
         * \note   This static factory method serves mainly for convenience to automatically
         *         calculate the dimensionality and size of the texture.
         * \param  texture  OpenGL texture to use, must not be 0, ImageDataGL will take ownership of this texture.
         */
        static ImageDataGL* createFromTexture(tgt::Texture* texture);
        
        /**
         * Creates a new ImageDataGL representation from a tgt::Texture.
         *
         * \param dimensionality    Dimensionality of data
         * \param size              Size of this image (number of elements per dimension)
         * \param texture           OpenGL texture to use, must not be 0, ImageDataGL will take ownership of this texture.
         */
        ImageDataGL(size_t dimensionality, const tgt::svec3& size, tgt::Texture* texture);

72
73
74
75
76
        /**
         * Creates a new ImageDataGL representation.
         *
         * \param dimensionality    Dimensionality of data
         * \param size              Size of this image (number of elements per dimension)
schultezub's avatar
schultezub committed
77
         * \param wtp               WeaklyTypedPointer to the image data, must not be 0, ImageDataGL does \b not take ownership of that pointer.
78
79
80
81
         */
        ImageDataGL(size_t dimensionality, const tgt::svec3& size, const WeaklyTypedPointer& wtp);

        /**
82
         * Creates a new ImageDataGL representation from GenericImageDataLocal.
83
84
85
         *
         * \param dimensionality    Dimensionality of data
         * \param size              Size of this image (number of elements per dimension)
86
         * \param data              Pointer to the GenericImageDataLocal instance, must not be 0
87
88
89
90
         * \tparam  BASETYPE        Base type of image data
         * \tparam  NUMCHANNELS     Number of channels per element
         */
        template<typename BASETYPE, size_t NUMCHANNELS>
91
        ImageDataGL(size_t dimensionality, const tgt::svec3& size, const GenericImageDataLocal<BASETYPE, NUMCHANNELS>* data);
92
93
94
95
96
97
98
99
100
101
102
103

        /**
         * Destructor
         */
        virtual ~ImageDataGL();

       
        /**
         * \see AbstractData::clone()
         **/
        virtual ImageDataGL* clone() const;

104
105
106
107
108
109
        /// \see AbstractData::getLocalMemoryFootprint()
        virtual size_t getLocalMemoryFootprint() const;

        /// \see AbstractData::getVideoMemoryFootprint()
        virtual size_t getVideoMemoryFootprint() const;

110
111
112
113
114
115
        /**
         * \see ImageData::getSubImage
         */
        virtual ImageDataGL* getSubImage(const tgt::svec3& llf, const tgt::svec3& urb) const;


schultezub's avatar
schultezub committed
116
117
118
119
120
121
122
123
124
125
126
127
128
129
        /**
         * Binds the texture without activating a texture unit.
         */
        void bind() const;

        /**
         * Activates the texture unit \a texUnit and binds the texture.
         * \param   texUnit     Texture unit to activate
         */
        void bind(const tgt::TextureUnit& texUnit) const;

        void bind(
            tgt::Shader* shader,
            const tgt::TextureUnit& texUnit, 
schultezub's avatar
schultezub committed
130
            const std::string& texUniform = "_texture") const;
schultezub's avatar
schultezub committed
131

132
133
134
135
136
        /**
         * Gets the OpenGL texture.
         * \return  _texture
         */
        const tgt::Texture* getTexture() const;
schultezub's avatar
schultezub committed
137

138
139
140
141
142
143
144
145
146
147

    protected:
        /**
         * Creates the OpenGL texture from the given pointer \a wtp.
         * \param wtp   WeaklyTypedPointer with source image data
         */
        void createTexture(const WeaklyTypedPointer& wtp);

        /**
         * Creates the OpenGL texture from the given ImageDataRAM \a data.
148
         * \param data              Pointer to the GenericImageDataLocal instance, must not be 0
149
150
151
152
         * \tparam  BASETYPE        Base type of image data
         * \tparam  NUMCHANNELS     Number of channels per element
         */
        template<typename BASETYPE, size_t NUMCHANNELS>
153
        void createTexture(const GenericImageDataLocal<BASETYPE, NUMCHANNELS>* data);
154
155
156
157
158
159
160
161
162

        tgt::Texture* _texture;             //< OpenGL texture

        static const std::string loggerCat_;
    };

// = Template definition ==========================================================================

    template<typename BASETYPE, size_t NUMCHANNELS>
schultezub's avatar
schultezub committed
163
    campvis::ImageDataGL::ImageDataGL(size_t dimensionality, const tgt::svec3& size, const GenericImageDataLocal<BASETYPE, NUMCHANNELS>* data) 
164
165
166
167
168
169
        : ImageData(dimensionality, size)
    {
        createTexture<BASETYPE, NUMCHANNELS>(data);
    }

    template<typename BASETYPE, size_t NUMCHANNELS>
schultezub's avatar
schultezub committed
170
    void campvis::ImageDataGL::createTexture(const GenericImageDataLocal<BASETYPE, NUMCHANNELS>* data) {
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
        tgtAssert(data != 0, "Pointer to image must not be 0!");

        _texture = new tgt::Texture(
            reinterpret_cast<GLubyte*>(data->getImageData()), 
            _size, 
            TypeTraits<BASETYPE, NUMCHANNELS>::glFormat,
            TypeTraits<BASETYPE, NUMCHANNELS>::glInternalFormat, 
            TypeTraits<BASETYPE, NUMCHANNELS>::glDataType, 
            tgt::Texture::LINEAR);

        switch (_dimensionality) {
            case 1:
                _texture->setType(GL_TEXTURE_1D);
                break;
            case 2:
                _texture->setType(GL_TEXTURE_2D);
                break;
            case 3:
                _texture->setType(GL_TEXTURE_3D);
                break;
            default:
                tgtAssert(false, "Unsupported dimensionality of image.");
                break;
        }

        _texture->bind();
        _texture->uploadTexture();
        _texture->setWrapping(tgt::Texture::CLAMP);

        // revoke ownership of local pixel data from the texture
        _texture->setPixelData(0);

        LGL_ERROR;
    }
}

#endif // IMAGEDATAGL_H__