visualizationprocessor.h 7.89 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
//      Chair for Computer Aided Medical Procedures
8
9
//      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.
// 
// ================================================================================================

schultezub's avatar
schultezub committed
30
31
32
#ifndef VISUALIZATIONPROCESSOR_H__
#define VISUALIZATIONPROCESSOR_H__

33
34
#include "tgt/framebufferobject.h"
#include "tgt/texture.h"
schultezub's avatar
schultezub committed
35
#include "tgt/vector.h"
36

schultezub's avatar
schultezub committed
37
#include "core/pipeline/abstractprocessor.h"
38
#include "core/properties/numericproperty.h"
schultezub's avatar
schultezub committed
39

schultezub's avatar
schultezub committed
40
namespace campvis {
41
    class ImageData;
schultezub's avatar
schultezub committed
42
43

    /**
44
     * Specialization of AbstractProcessor for visualization purposes.
45
     * 
46
47
48
     * VisualizationProcessors are required to be called by a VisualizationPipeline which ensure
     * to provide a valid OpenGL context when calling the processor's process() method. Hence, a
     * VisualizationProcessor is allowed/capable of performing OpenGl operations.
49
50
51
52
     * 
     * Each VisualizationProcessor has its own OpenGL FramebufferObject, which is created during
     * init(). For determining the canvas/viewport size, a VisualizationProcessor gets a reference 
     * to the parent pipeline's render target size property during instantiation.
schultezub's avatar
schultezub committed
53
     * 
54
     * \sa VisualizationPipeline
schultezub's avatar
schultezub committed
55
56
57
     */
    class VisualizationProcessor : public AbstractProcessor {
    public:
58
59
60
61
62
        /**
         * Utility struct to encapuslate off-screen rendering with this processor using FBOs.
         * This guard essentially activates the VisualizationProcessor's FBO and sets the OpenGL
         * viewport size. Upon destruction it detaches all texutres and deactivates the FBO again.
         */
63
64
        struct FramebufferActivationGuard {
        public:
65
66
67
68
69
70
            /**
             * Constructor.
             * Activates the FBO and sets the OpenGL viewport size to the effective viewport size of \a vp.
             * \param   vp  This Visualization processor (usually pointer to this).
             */
            explicit FramebufferActivationGuard(VisualizationProcessor* vp)
71
72
73
74
75
                : _parentProcessor(vp)
                , _fbo(vp->_fbo)
            {
                tgtAssert(_fbo != 0, "FBO must not be 0.");
                _fbo->activate();
76

77
                const tgt::ivec2& windowSize = vp->getEffectiveViewportSize();
78
                glViewport(0, 0, static_cast<GLsizei>(windowSize.x), static_cast<GLsizei>(windowSize.y));
79
80
            }

81
82
83
            /**
             * Destructor, detaches all textures and deactivates the FBO.
             */
84
85
86
87
88
89
90
91
92
93
            ~FramebufferActivationGuard() {
                _fbo->detachAll();
                _fbo->deactivate();
            }


        private:
            VisualizationProcessor* _parentProcessor;
            tgt::FramebufferObject* _fbo;
        };
schultezub's avatar
schultezub committed
94
95
96

        /**
         * Creates a VisualizationProcessor.
97
         * 
98
99
         * \note    The render target size property of this VisualizationProcessor will automatically 
         *          be assigned as shared property of the given \a renderTargetSize property.
100
101
102
103
104
         * \note    This processor will keep and access \a renderTargetSize, so make sure the referenced
         *          property exists at least as long as this processor or you set it to a different
         *          property before using setViewportSizeProperty().
         *          
         * \param   viewportSizeProp    Pointer to the property defining the viewport size, must not be 0.
schultezub's avatar
schultezub committed
105
         */
106
        explicit VisualizationProcessor(IVec2Property* viewportSizeProp);
schultezub's avatar
schultezub committed
107
108
109
110
111
112

        /**
         * Virtual Destructor
         **/
        virtual ~VisualizationProcessor();

113
114
115
116
117
118
119
120
                
        /// \see AbstractProcessor::init()
        virtual void init();
        
        /// \see AbstractProcessor::deinit()
        virtual void deinit();


121
122
123
124
125
126
127
128
129
        /**
         * Sets the property defining the viewport size to \a viewportSizeProp.
         * \note    This processor will keep and access this pointer, so make sure the referenced
         *          property exists at least as long as this processor or you set it to a different
         *          property before.
         * \param   viewportSizeProp    Pointer to the property defining the viewport size, must not be 0.
         */
        virtual void setViewportSizeProperty(IVec2Property* viewportSizeProp);

130
131
132

        BoolProperty p_lqMode;                      ///< Flag whether to enable LQ mode (halfsamples effective viewport size)

133
134
    protected:

135
136
137
138
139
140
141
142
143
144
145
146
147
148
        /**
         * Creates a texture with the given format and attaches it to the FBO to \a attachment.
         * \param   internalFormat  Internal OpenGL texture format
         * \param   attachment      Target attachment for texture
         */
        void createAndAttachTexture(GLint internalFormat, GLenum attachment);

        /**
         * Creates a texture with the given format and attaches it to the FBO using the default attachments.
         * \note    Default attachment are GL_DEPTH_ATTACHMENT for depth textures and 
         *          GL_COLOR_ATTACHMENT_0 + <number of color textures attached>.
         * \param   internalFormat  Internal OpenGL texture format
         */
        void createAndAttachTexture(GLint internalFormat);
schultezub's avatar
schultezub committed
149

150
151
152
153
154
        /**
         * Creates a color texture with format GL_RGBA8 and attaches it to the FBO using the 
         * default attachment.
         */
        void createAndAttachColorTexture();
schultezub's avatar
schultezub committed
155

156
157
158
159
160
        /**
         * Creates a depth texture with format GL_DEPTH_COMPONENT24 and attaches it to the FBO 
         * using the default attachment.
         */
        void createAndAttachDepthTexture();
schultezub's avatar
schultezub committed
161

162
163
164
165
166
167
        /**
         * Returns the effective viewport size considering LQ mode.
         * \return  lqMode ? _viewportSize/2 : _viewportSize
         */
        tgt::ivec2 getEffectiveViewportSize() const;

168
169
        /**
         * Returns the current viewport size as ivec3.
170
         * \return  tgt::ivec3(getEffectiveViewportSize(), 1)
171
         */
172
173
        tgt::ivec3 getRenderTargetSize() const;

174
175

        tgt::FramebufferObject* _fbo;               ///< The FBO used by this VisualizationProcessor
176
        IVec2Property* _viewportSizeProperty;       ///< Pointer to the property defining the viewport (canvas) size.
177
178

        static const std::string loggerCat_;
schultezub's avatar
schultezub committed
179
180
181
182
183
    };

}

#endif // VISUALIZATIONPROCESSOR_H__