24.09., 9:00 - 11:00: Due to updates GitLab will be unavailable for some minutes between 09:00 and 11:00.

visualizationprocessor.h 7.67 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-2015, 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 Universitaet Muenchen
//      Boltzmannstr. 3, 85748 Garching b. Muenchen, Germany
10
// 
schultezub's avatar
schultezub committed
11
// For a full list of authors and contributors, please refer to the file "AUTHORS.txt".
12
// 
13 14 15 16
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file 
// except in compliance with the License. You may obtain a copy of the License at
// 
// http://www.apache.org/licenses/LICENSE-2.0
17
// 
18 19 20 21
// Unless required by applicable law or agreed to in writing, software distributed under the 
// License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
// either express or implied. See the License for the specific language governing permissions 
// and limitations under the License.
22 23 24
// 
// ================================================================================================

schultezub's avatar
schultezub committed
25 26 27
#ifndef VISUALIZATIONPROCESSOR_H__
#define VISUALIZATIONPROCESSOR_H__

28 29 30
#include "cgt/framebufferobject.h"
#include "cgt/texture.h"
#include "cgt/vector.h"
31

schultezub's avatar
schultezub committed
32
#include "core/pipeline/abstractprocessor.h"
33
#include "core/properties/numericproperty.h"
schultezub's avatar
schultezub committed
34

schultezub's avatar
schultezub committed
35
namespace campvis {
36
    class ImageData;
Jakob Weiss's avatar
Jakob Weiss committed
37
    class RenderData;
schultezub's avatar
schultezub committed
38 39

    /**
40
     * Specialization of AbstractProcessor for visualization purposes.
41
     * 
42 43 44
     * 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.
45 46 47 48
     * 
     * 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
49
     * 
50
     * \sa VisualizationPipeline
schultezub's avatar
schultezub committed
51
     */
52
    class CAMPVIS_CORE_API VisualizationProcessor : public AbstractProcessor {
schultezub's avatar
schultezub committed
53
    public:
54
        /**
Hossain Mahmud's avatar
Hossain Mahmud committed
55
         * Utility struct to encapsulate off-screen rendering with this processor using FBOs.
56
         * This guard essentially activates the VisualizationProcessor's FBO and sets the OpenGL
Hossain Mahmud's avatar
Hossain Mahmud committed
57
         * viewport size. Upon destruction it detaches all textures and deactivates the FBO again.
58
         */
59
        struct CAMPVIS_CORE_API FramebufferActivationGuard {
60
        public:
61 62 63 64 65 66
            /**
             * 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)
Jakob Weiss's avatar
Jakob Weiss committed
67 68
                : //_parentProcessor(vp)
                _fbo(vp->_fbo)
69
            {
70 71
                cgtAssert(_fbo != nullptr, "FBO must not be 0.");
                cgtAssert(_fbo->getId() != 0, "The FBO's OpenGL ID is 0, this is wrong.");
72
                _fbo->activate();
73

74
                const cgt::ivec2& windowSize = vp->getEffectiveViewportSize();
75
                glViewport(0, 0, static_cast<GLsizei>(windowSize.x), static_cast<GLsizei>(windowSize.y));
76 77
            }

78 79 80
            /**
             * Destructor, detaches all textures and deactivates the FBO.
             */
81 82 83 84 85 86 87
            ~FramebufferActivationGuard() {
                _fbo->detachAll();
                _fbo->deactivate();
            }


        private:
Jakob Weiss's avatar
Jakob Weiss committed
88
            //VisualizationProcessor* _parentProcessor;
89
            cgt::FramebufferObject* _fbo;
90
        };
schultezub's avatar
schultezub committed
91 92 93

        /**
         * Creates a VisualizationProcessor.
94
         * 
95 96
         * \note    The render target size property of this VisualizationProcessor will automatically 
         *          be assigned as shared property of the given \a renderTargetSize property.
97 98 99 100 101
         * \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
102
         */
103
        explicit VisualizationProcessor(IVec2Property* viewportSizeProp);
schultezub's avatar
schultezub committed
104 105 106 107 108 109

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

110 111 112 113 114 115 116 117
                
        /// \see AbstractProcessor::init()
        virtual void init();
        
        /// \see AbstractProcessor::deinit()
        virtual void deinit();


118 119 120 121 122 123 124 125
        /**
         * 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);
126 127 128 129 130 131
        
        /**
         * Returns the effective viewport size considering LQ mode.
         * \return  lqMode ? _viewportSize/2 : _viewportSize
         */
        cgt::ivec2 getEffectiveViewportSize() const;
132 133 134

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

135 136
    protected:

137 138 139 140 141
        /**
         * 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
         */
Jakob Weiss's avatar
Jakob Weiss committed
142
        void createAndAttachTexture(GLint internalFormat, GLenum attachment, ScopedTypedData<RenderData, true>* renderData = nullptr);
143 144 145 146 147 148 149

        /**
         * 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
         */
Jakob Weiss's avatar
Jakob Weiss committed
150
        void createAndAttachTexture(GLint internalFormat, ScopedTypedData<RenderData, true>* renderData = nullptr);
schultezub's avatar
schultezub committed
151

152 153 154 155
        /**
         * Creates a color texture with format GL_RGBA8 and attaches it to the FBO using the 
         * default attachment.
         */
Jakob Weiss's avatar
Jakob Weiss committed
156
        void createAndAttachColorTexture(ScopedTypedData<RenderData, true>* renderData = nullptr);
schultezub's avatar
schultezub committed
157

158 159 160 161
        /**
         * Creates a depth texture with format GL_DEPTH_COMPONENT24 and attaches it to the FBO 
         * using the default attachment.
         */
Jakob Weiss's avatar
Jakob Weiss committed
162
        void createAndAttachDepthTexture(ScopedTypedData<RenderData, true>* renderData = nullptr);
schultezub's avatar
schultezub committed
163

164 165
        /**
         * Returns the current viewport size as ivec3.
166
         * \return  cgt::ivec3(getEffectiveViewportSize(), 1)
167
         */
168
        cgt::ivec3 getRenderTargetSize() const;
169

170

171
        cgt::FramebufferObject* _fbo;               ///< The FBO used by this VisualizationProcessor
172
        IVec2Property* _viewportSizeProperty;       ///< Pointer to the property defining the viewport (canvas) size.
173 174

        static const std::string loggerCat_;
schultezub's avatar
schultezub committed
175 176 177 178 179
    };

}

#endif // VISUALIZATIONPROCESSOR_H__