imagerepresentationrendertarget.h 10.6 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 Universität München
//      Boltzmannstr. 3, 85748 Garching b. München, 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
#ifndef IMAGEREPRESENTATIONRENDERTARGET_H__
#define IMAGEREPRESENTATIONRENDERTARGET_H__
schultezub's avatar
schultezub committed
32
33
34

#include "tgt/framebufferobject.h"
#include "tgt/texture.h"
schultezub's avatar
schultezub committed
35
#include "tgt/textureunit.h"
schultezub's avatar
schultezub committed
36
#include "tgt/vector.h"
37
38
#include "core/datastructures/genericabstractimagerepresentation.h"
#include "core/datastructures/imagerepresentationgl.h"
schultezub's avatar
schultezub committed
39

40
#include <utility>
41
42
#include <vector>

schultezub's avatar
schultezub committed
43
44
45
46
namespace tgt {
    class Shader;
}

schultezub's avatar
schultezub committed
47
namespace campvis {
48
    class ImageRepresentationGL;
schultezub's avatar
schultezub committed
49
50
51

    /**
     * Stores render target data.
52
     * This is basically a wrapper for multiple OpenGL textures (color + depth) and their attachment to the framebuffer.
53
     * Each ImageRepresentationRenderTarget has at least one color texture and exactly one depth texture attachment.
schultezub's avatar
schultezub committed
54
55
56
57
58
59
60
     * 
     * \note    Its dimensionality must be 2.
     * 
     * \todo    The constructor only asserts if one of the textures or the FBO weren't successfully created.
     *          The member functions don't double check, whether the pointers are != 0. Make sure, that this 
     *          is a sane design and if not, adapt the code.
     */
61
    class ImageRepresentationRenderTarget : public GenericAbstractImageRepresentation<ImageRepresentationRenderTarget> {
schultezub's avatar
schultezub committed
62
    public:
63
64
65
66
67
68
69
70
71
72
73
74
75
        /**
         * Creates a new ImageRepresentationRenderTarget representation with one color and one depth attachment
         * and automatically adds it to \a parent which will take ownerwhip.
         *
         * \note    You do \b not own the returned pointer..
         *
         * \param   parent     Image this representation represents, must not be 0, will take ownership of the returned pointer.
         * \param   internalFormatColor   Internal OpenGL format for the color texture.
         * \param   internalFormatDepth   Internal OpenGL format for the depth texture.
         * \return  A pointer to the newly created ImageRepresentationRenderTarget, you do \b not own this pointer!
        */
        static ImageRepresentationRenderTarget* create(ImageData* parent, GLint internalFormatColor = GL_RGBA8, GLint internalFormatDepth = GL_DEPTH_COMPONENT24);

schultezub's avatar
schultezub committed
76
        /**
77
78
79
80
81
82
83
84
85
86
         * Creates a new ImageRepresentationRenderTarget and a new ImageData with the given specifications.
         * Assigns the representation to the image.
         * 
         * \param   size                  Size of this image (number of elements per dimension)
         * \param   internalFormatColor   Internal OpenGL format for the color texture.
         * \param   internalFormatDepth   Internal OpenGL format for the depth texture.
         * \return  A pair of the created ImageData and the created RenderTarget image representation.
         */
        static std::pair<ImageData*, ImageRepresentationRenderTarget*> createWithImageData(const tgt::svec2& size, GLint internalFormatColor = GL_RGBA8, GLint internalFormatDepth = GL_DEPTH_COMPONENT24);

schultezub's avatar
schultezub committed
87
88
89
        /**
         * Destructor
         */
90
        virtual ~ImageRepresentationRenderTarget();
schultezub's avatar
schultezub committed
91
       
92
93
94
        /**
         * Performs a conversion of \a source to an ImageRepresentationLocal if feasible.
         * Returns 0 if conversion was not successful or source representation type is not compatible.
95
         * \note    The callee, respectively the callee's parent, has the ownership of the returned pointer.
96
         * \param   source  Source image representation for conversion.
97
         * \return  A pointer to a local representation of \a source or 0 on failure. The caller does \b not have ownership.
98
99
100
         */
        static ImageRepresentationRenderTarget* tryConvertFrom(const AbstractImageRepresentation* source);

101
        /// \see AbstractImageRepresentation::clone()
102
        virtual ImageRepresentationRenderTarget* clone(ImageData* newParent) const;
schultezub's avatar
schultezub committed
103

104
        /// \see AbstractImageRepresentation::getLocalMemoryFootprint()
105
106
        virtual size_t getLocalMemoryFootprint() const;

107
        /// \see AbstractImageRepresentation::getVideoMemoryFootprint()
108
109
        virtual size_t getVideoMemoryFootprint() const;

110
        /// \see AbstractImageRepresentation::getSubImage
111
        virtual ImageRepresentationRenderTarget* getSubImage(ImageData* parent, const tgt::svec3& llf, const tgt::svec3& urb) const;
schultezub's avatar
schultezub committed
112

113
114
115
116
117
118
119
        /**
         * Creates and initializes a new OpenGL texture according to \a internalFormat and attaches it to the FBO.
         * \note    The amout of attachable color textures is limited by the driver/hardware - check 
         *          GpuCaps.getMaxColorAttachments(). You can only attach one depth texture.
         * \param   internalFormat  Internal OpenGL format for the texture to create.
         */
        void createAndAttachTexture(GLint internalFormat);
schultezub's avatar
schultezub committed
120

121
122
        /**
         * Gets the color texture of this render target
123
124
         * \param   index   Index of the color texture attachment to return.
         * \return  _colorTextures[index]
125
         */
126
        const tgt::Texture* getColorTexture(size_t index = 0) const;
127
128
129
130
131
132
133

        /**
         * Gets the depth texture of this render target
         * \return _depthTexture
         */
        const tgt::Texture* getDepthTexture() const;

schultezub's avatar
schultezub committed
134
135
136
137
138
139
140
141
142
143
144
145
146
        /**
         * Activates the render target (binds the FBO and sets the viewport).
         */
        void activate();

        /**
         * Deactivates the render target (unbinds the FBO).
         */
        void deactivate();

        /**
         * Binds the color texture without activating a texture unit.
         */
147
        void bindColorTexture(size_t index = 0) const;
schultezub's avatar
schultezub committed
148
149
150
151
152
153
154
155
156
157

        /**
         * Binds the depth texture without activating a texture unit.
         */
        void bindDepthTexture() const;

        /**
         * Activates the texture unit \a texUnit and binds the color texture.
         * \param   texUnit     Texture unit to activate
         */
158
        void bindColorTexture(const tgt::TextureUnit& texUnit, size_t index = 0) const;
schultezub's avatar
schultezub committed
159
160
161
162
163

        /**
         * Activates the texture unit \a texUnit and binds the depth texture.
         * \param   texUnit     Texture unit to activate
         */
schultezub's avatar
schultezub committed
164
        void bindDepthTexture(const tgt::TextureUnit& texUnit) const;
schultezub's avatar
schultezub committed
165

166
167
168
169
170
171
        /**
         * Gets the number of color textures/attachments of this render target.
         * \return  _colorTextures.size()
         */
        size_t getNumColorTextures() const;

schultezub's avatar
schultezub committed
172
173
174
175
176
177
178
179
        /**
         * Binds the textures of this render target and sets the according shader uniforms.
         * Of \a colorTexUnit or \a depthTexUnit is 0, the corresponding texture will not be bound
         * and the corresponding uniforms will not be set.
         * 
         * \param shader                    Shader to set the uniforms to.
         * \param colorTexUnit              Pointer to color texture unit, may be 0.
         * \param depthTexUnit              Pointer to depth texture unit, may be 0.
schultezub's avatar
schultezub committed
180
181
         * \param colorTexUniform           Name for color texture struct uniform.
         * \param depthTexUniform           Name for depth texture struct uniform.
schultezub's avatar
schultezub committed
182
         */
schultezub's avatar
schultezub committed
183
184
        void bind(
            tgt::Shader* shader,
schultezub's avatar
schultezub committed
185
186
            const tgt::TextureUnit* colorTexUnit, 
            const tgt::TextureUnit* depthTexUnit, 
187
            const std::string& colorTexUniform = "_colorTexture",
188
189
            const std::string& depthTexUniform = "_depthTexture",
            size_t index = 0) const;
schultezub's avatar
schultezub committed
190
191
192


    protected:
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
        /**
         * Creates a new ImageRepresentationRenderTarget representation with one color and one depth attachment.
         *
         * \param   parent                Image this representation represents, must not be 0.
         * \param   internalFormatColor   Internal OpenGL format for the color texture.
         * \param   internalFormatDepth   Internal OpenGL format for the depth texture.
         */
        ImageRepresentationRenderTarget(ImageData* parent, GLint internalFormatColor = GL_RGBA8, GLint internalFormatDepth = GL_DEPTH_COMPONENT24);

        /**
         * Creates a new ImageRepresentationRenderTarget from one color texture and one optional depth texture.
         * \param   colorTexture    Color texture, must not be 0
         * \param   depthTexture    Depth texture, optional, must have valid internal format and same dimensions as \a colorTexture
         */
        ImageRepresentationRenderTarget(ImageData* parent, const ImageRepresentationGL* colorTexture, const ImageRepresentationGL* depthTexture = 0);
schultezub's avatar
schultezub committed
208

209
210
211
        std::vector<tgt::Texture*> _colorTextures;      ///< color textures
        tgt::Texture* _depthTexture;                    ///< depth texture
        tgt::FramebufferObject* _fbo;                   ///< Framebuffer object color and depth textures are attached to
schultezub's avatar
schultezub committed
212
213
214
215
216
217


        static const std::string loggerCat_;
    };
}

218
#endif // IMAGEREPRESENTATIONRENDERTARGET_H__