visualizationprocessor.cpp 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
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.
// 
// ================================================================================================

schultezub's avatar
schultezub committed
30
31
#include "visualizationprocessor.h"

32
33
34
#include "tgt/textureunit.h"
#include "core/datastructures/imagedata.h"

schultezub's avatar
schultezub committed
35
namespace campvis {
schultezub's avatar
schultezub committed
36

37
38
39
40
    const std::string VisualizationProcessor::loggerCat_ = "CAMPVis.core.datastructures.VisualizationProcessor";

// ================================================================================================

41
    VisualizationProcessor::VisualizationProcessor(IVec2Property* viewportSizeProp)
schultezub's avatar
schultezub committed
42
        : AbstractProcessor()
43
        , p_lqMode("LqMode", "Low Quality Mode", false)
44
        , _fbo(0)
45
        , _viewportSizeProperty(viewportSizeProp)
schultezub's avatar
schultezub committed
46
    {
47
        tgtAssert(_viewportSizeProperty != 0, "Pointer must not be 0!");
48
49

        addProperty(&p_lqMode);
schultezub's avatar
schultezub committed
50
51
52
53
54
    }

    VisualizationProcessor::~VisualizationProcessor() {
    }

55
56
    void VisualizationProcessor::init() {
        AbstractProcessor::init();
57

58
        _fbo = new tgt::FramebufferObject();
59
        _viewportSizeProperty->s_changed.connect<VisualizationProcessor>(this, &VisualizationProcessor::onPropertyChanged);
60
61
62
    }

    void VisualizationProcessor::deinit() {
63
        _viewportSizeProperty->s_changed.disconnect(this);
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
        _fbo->detachAll();
        delete _fbo;
        AbstractProcessor::deinit();
    }

    void VisualizationProcessor::createAndAttachTexture(GLint internalFormat, GLenum attachment) {
        tgtAssert(_fbo->isActive(), "Trying to attach a texture while FBO is not bound!");

        // acqiure a new TextureUnit, so that we don't mess with other currently bound textures during texture upload...
        tgt::TextureUnit rtUnit;
        rtUnit.activate();

        // Set OpenGL pixel alignment to 1 to avoid problems with NPOT textures
        glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

        // create texture
        tgt::Texture* tex = 0;
        switch(internalFormat) {
            case GL_RGB:
                tex = new tgt::Texture(0, getRenderTargetSize(), GL_RGB, GL_RGB, GL_UNSIGNED_BYTE, tgt::Texture::LINEAR);
                break;
            case GL_RGB16F_ARB:
                tex = new tgt::Texture(0, getRenderTargetSize(), GL_RGB, GL_RGB16F_ARB, GL_FLOAT, tgt::Texture::LINEAR);
                break;
            case GL_RGBA:
                tex = new tgt::Texture(0, getRenderTargetSize(), GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE, tgt::Texture::LINEAR);
                break;
            case GL_RGBA8:
                tex = new tgt::Texture(0, getRenderTargetSize(), GL_RGBA, GL_RGBA8, GL_UNSIGNED_BYTE, tgt::Texture::LINEAR);
                break;
            case GL_RGBA16:
                tex = new tgt::Texture(0, getRenderTargetSize(), GL_RGBA, GL_RGBA16, GL_UNSIGNED_SHORT, tgt::Texture::LINEAR);
                break;
            case GL_RGBA16F:
                tex = new tgt::Texture(0, getRenderTargetSize(), GL_RGBA, GL_RGBA16F, GL_FLOAT, tgt::Texture::LINEAR);
                break;
            case GL_RGBA32F:
                tex = new tgt::Texture(0, getRenderTargetSize(), GL_RGBA, GL_RGBA32F, GL_FLOAT, tgt::Texture::LINEAR);
                break;

            case GL_DEPTH_COMPONENT16:
                tex = new tgt::Texture(0, getRenderTargetSize(), GL_DEPTH_COMPONENT, GL_DEPTH_COMPONENT16, GL_FLOAT, tgt::Texture::LINEAR);
                break;
            case GL_DEPTH_COMPONENT24:
                tex = new tgt::Texture(0, getRenderTargetSize(), GL_DEPTH_COMPONENT, GL_DEPTH_COMPONENT24, GL_FLOAT, tgt::Texture::LINEAR);
                break;
#ifdef GL_DEPTH_COMPONENT32F
            case GL_DEPTH_COMPONENT32F:
                break;
#endif

            default:
                tgtAssert(false, "Unknown internal format!");
        }
        tex->uploadTexture();
        tex->setWrapping(tgt::Texture::CLAMP_TO_EDGE);

        // attach texture to FBO
        _fbo->attachTexture(tex, attachment);
    }

    void VisualizationProcessor::createAndAttachTexture(GLint internalFormat) {
        GLenum attachment = 0;
        switch(internalFormat) {
            case GL_RGB:
            case GL_RGB16F_ARB:
            case GL_RGBA:
            case GL_RGBA8:
            case GL_RGBA16:
            case GL_RGBA16F:
            case GL_RGBA32F:
                if (_fbo->getNumColorAttachments() >= static_cast<size_t>(GpuCaps.getMaxColorAttachments())) {
                    tgtAssert(false, "Tried to attach more color textures to FBO than supported!");
                    LWARNING("Tried to attach more color textures to FBO than supported, aborted.");
                    return;
                }
                attachment = static_cast<GLenum>(GL_COLOR_ATTACHMENT0 + _fbo->getNumColorAttachments());
                break;

            case GL_DEPTH_COMPONENT16:
            case GL_DEPTH_COMPONENT24:
#ifdef GL_DEPTH_COMPONENT32F
            case GL_DEPTH_COMPONENT32F:
#endif
                tgtAssert(_fbo->getDepthAttachment() == 0, "Tried to attach more than one depth texture.");
                attachment = GL_DEPTH_ATTACHMENT;
                break;

            default:
                tgtAssert(false, "Unknown internal format!");
        }
        createAndAttachTexture(internalFormat, attachment);
    }

158
159
160
161
    tgt::ivec2 VisualizationProcessor::getEffectiveViewportSize() const {
        return (p_lqMode.getValue() ? _viewportSizeProperty->getValue() / 2 : _viewportSizeProperty->getValue());
    }

162
    tgt::ivec3 VisualizationProcessor::getRenderTargetSize() const {
163
        return tgt::ivec3(getEffectiveViewportSize(), 1);
164
165
166
167
168
169
170
171
172
173
    }

    void VisualizationProcessor::createAndAttachColorTexture() {
        createAndAttachTexture(GL_RGBA8);
    }

    void VisualizationProcessor::createAndAttachDepthTexture() {
        createAndAttachTexture(GL_DEPTH_COMPONENT24);
    }

174
175
176
177
178
179
180
181
    void VisualizationProcessor::setViewportSizeProperty(IVec2Property* viewportSizeProp) {
        tgtAssert(viewportSizeProp != 0, "Pointer must not be 0.");

        _viewportSizeProperty->s_changed.disconnect(this);
        _viewportSizeProperty = viewportSizeProp;
        _viewportSizeProperty->s_changed.connect<VisualizationProcessor>(this, &VisualizationProcessor::onPropertyChanged);
    }

schultezub's avatar
schultezub committed
182
}