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

visualizationprocessor.h 7.43 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-2014, 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;
schultezub's avatar
schultezub committed
37
38

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

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

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


        private:
            VisualizationProcessor* _parentProcessor;
            tgt::FramebufferObject* _fbo;
        };
schultezub's avatar
schultezub committed
90
91
92

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

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

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


117
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

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

129
130
    protected:

131
132
133
134
135
136
137
138
139
140
141
142
143
144
        /**
         * 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
145

146
147
148
149
150
        /**
         * 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
151

152
153
154
155
156
        /**
         * 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
157

158
159
160
161
162
163
        /**
         * Returns the effective viewport size considering LQ mode.
         * \return  lqMode ? _viewportSize/2 : _viewportSize
         */
        tgt::ivec2 getEffectiveViewportSize() const;

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

170
171

        tgt::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__