rendertargetcompositor.cpp 6.16 KB
Newer Older
schultezub's avatar
schultezub committed
1
2
3
4
// ================================================================================================
// 
// This file is part of the CAMPVis Software Framework.
// 
5
// If not explicitly stated otherwise: Copyright (C) 2012-2014, all rights reserved,
schultezub's avatar
schultezub committed
6
7
//      Christian Schulte zu Berge <christian.szb@in.tum.de>
//      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
12
// For a full list of authors and contributors, please refer to the file "AUTHORS.txt".
// 
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
schultezub's avatar
schultezub committed
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.
schultezub's avatar
schultezub committed
22
23
24
25
26
27
28
29
// 
// ================================================================================================

#include "rendertargetcompositor.h"
#include "tgt/logmanager.h"
#include "tgt/shadermanager.h"
#include "tgt/textureunit.h"

30
#include "core/datastructures/renderdata.h"
31

schultezub's avatar
schultezub committed
32
33
34
35
36
37
38

#include "core/classification/simpletransferfunction.h"

#include "core/tools/quadrenderer.h"

namespace campvis {

39
40
41
42
43
44
    static const GenericOption<RenderTargetCompositor::CompositingMode> compositingOptions[5] = {
        GenericOption<RenderTargetCompositor::CompositingMode>("first", "Only First", RenderTargetCompositor::CompositingModeFirst),
        GenericOption<RenderTargetCompositor::CompositingMode>("second", "Only Second", RenderTargetCompositor::CompositingModeSecond),
        GenericOption<RenderTargetCompositor::CompositingMode>("alpha", "Alpha Blending", RenderTargetCompositor::CompositingModeAlpha),
        GenericOption<RenderTargetCompositor::CompositingMode>("diff", "Difference", RenderTargetCompositor::CompositingModeDifference),
        GenericOption<RenderTargetCompositor::CompositingMode>("depth", "Depth Test", RenderTargetCompositor::CompositingModeDepth)
schultezub's avatar
schultezub committed
45
46
47
48
    };

    const std::string RenderTargetCompositor::loggerCat_ = "CAMPVis.modules.vis.RenderTargetCompositor";

49
50
    RenderTargetCompositor::RenderTargetCompositor(IVec2Property* viewportSizeProp)
        : VisualizationProcessor(viewportSizeProp)
schultezub's avatar
schultezub committed
51
52
53
        , p_firstImageId("FirstImageId", "First Input Image", "", DataNameProperty::READ)
        , p_secondImageId("SecondImageId", "Second Input Image", "", DataNameProperty::READ)
        , p_targetImageId("TargetImageId", "Output Image", "", DataNameProperty::WRITE)
schultezub's avatar
schultezub committed
54
        , p_compositingMethod("CompositingMethod", "Compositing Method", compositingOptions, 5)
schultezub's avatar
schultezub committed
55
        , p_alphaValue("AlphaValue", "AlphaValue", .5f, 0.f, 1.f)
56
        , p_enableBackground("EnableBackground", "Enable Background", true)
schultezub's avatar
schultezub committed
57
58
        , _shader(0)
    {
59
60
61
62
63
64
        addProperty(p_firstImageId);
        addProperty(p_secondImageId);
        addProperty(p_targetImageId);
        addProperty(p_compositingMethod);
        addProperty(p_alphaValue);
        addProperty(p_enableBackground);
schultezub's avatar
schultezub committed
65
66
67
68
69
70
71
72
73
74
75
76

        addDecorator(new ProcessorDecoratorBackground());
        
        decoratePropertyCollection(this);
    }

    RenderTargetCompositor::~RenderTargetCompositor() {

    }

    void RenderTargetCompositor::init() {
        VisualizationProcessor::init();
77
        _shader = ShdrMgr.load("core/glsl/passthrough.vert", "modules/vis/glsl/rendertargetcompositor.frag", "");
schultezub's avatar
schultezub committed
78
79
80
81
82
83
84
85
86
        _shader->setAttributeLocation(0, "in_Position");
        _shader->setAttributeLocation(1, "in_TexCoord");
    }

    void RenderTargetCompositor::deinit() {
        VisualizationProcessor::deinit();
        ShdrMgr.dispose(_shader);
    }

87
    void RenderTargetCompositor::updateResult(DataContainer& data) {
88
89
        ScopedTypedData<RenderData> firstImage(data, p_firstImageId.getValue());
        ScopedTypedData<RenderData> secondImage(data, p_secondImageId.getValue());
schultezub's avatar
schultezub committed
90

91
92
93
94
95
96
97
        CompositingMode compositingMode = p_compositingMethod.getOptionValue();
        if (firstImage != nullptr && secondImage == nullptr && compositingMode != CompositingModeSecond) 
            compositingMode = CompositingModeFirst;
        if (firstImage == nullptr && secondImage != nullptr && compositingMode != CompositingModeFirst) 
            compositingMode = CompositingModeSecond;

        if ((compositingMode == CompositingModeSecond || firstImage != nullptr) && (compositingMode == CompositingModeFirst || secondImage != nullptr)) {
98
            FramebufferActivationGuard fag(this);
99
100
101
            createAndAttachColorTexture();
            createAndAttachDepthTexture();

schultezub's avatar
schultezub committed
102
103
104
            _shader->activate();
            tgt::TextureUnit firstColorUnit, firstDepthUnit, secondColorUnit, secondDepthUnit;

105
            if (compositingMode != CompositingModeSecond)
106
                firstImage->bind(_shader, firstColorUnit, firstDepthUnit, "_firstColor", "_firstDepth", "_firstTexParams");
107
            if (compositingMode != CompositingModeFirst)
108
109
                secondImage->bind(_shader, secondColorUnit, secondDepthUnit, "_secondColor", "_secondDepth", "_secondTexParams");

110
            _shader->setUniform("_compositingMethod", compositingMode);
schultezub's avatar
schultezub committed
111
            _shader->setUniform("_alpha", p_alphaValue.getValue());
112
            _shader->setUniform("_enableBackground", p_enableBackground.getValue());
schultezub's avatar
schultezub committed
113
114

            decorateRenderProlog(data, _shader);
115
116
117
118

            glEnable(GL_DEPTH_TEST);
            glDepthFunc(GL_ALWAYS);
            glClearDepth(1.0f);
schultezub's avatar
schultezub committed
119
            glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
120

schultezub's avatar
schultezub committed
121
122
            QuadRdr.renderQuad();

123
124
125
            glDepthFunc(GL_LESS);
            glDisable(GL_DEPTH_TEST);

schultezub's avatar
schultezub committed
126
127
128
129
            _shader->deactivate();
            tgt::TextureUnit::setZeroUnit();
            LGL_ERROR;

130
            data.addData(p_targetImageId.getValue(), new RenderData(_fbo));
schultezub's avatar
schultezub committed
131
132
        }
        else {
133
            LDEBUG("No suitable input images found.");
schultezub's avatar
schultezub committed
134
135
136
137
138
        }
    }

}