rendertargetcompositor.cpp 5.87 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
        if ((p_compositingMethod.getOptionValue() == CompositingModeSecond || firstImage != nullptr) && (p_compositingMethod.getOptionValue() == CompositingModeFirst || secondImage != nullptr)) {
92
            FramebufferActivationGuard fag(this);
93 94 95
            createAndAttachColorTexture();
            createAndAttachDepthTexture();

schultezub's avatar
schultezub committed
96 97 98
            _shader->activate();
            tgt::TextureUnit firstColorUnit, firstDepthUnit, secondColorUnit, secondDepthUnit;

99 100 101 102 103
            if (p_compositingMethod.getOptionValue() != CompositingModeSecond)
                firstImage->bind(_shader, firstColorUnit, firstDepthUnit, "_firstColor", "_firstDepth", "_firstTexParams");
            if (p_compositingMethod.getOptionValue() != CompositingModeFirst)
                secondImage->bind(_shader, secondColorUnit, secondDepthUnit, "_secondColor", "_secondDepth", "_secondTexParams");

schultezub's avatar
schultezub committed
104
            _shader->setUniform("_compositingMethod", p_compositingMethod.getOptionValue());
schultezub's avatar
schultezub committed
105
            _shader->setUniform("_alpha", p_alphaValue.getValue());
106
            _shader->setUniform("_enableBackground", p_enableBackground.getValue());
schultezub's avatar
schultezub committed
107 108

            decorateRenderProlog(data, _shader);
109 110 111 112

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

schultezub's avatar
schultezub committed
115 116
            QuadRdr.renderQuad();

117 118 119
            glDepthFunc(GL_LESS);
            glDisable(GL_DEPTH_TEST);

schultezub's avatar
schultezub committed
120 121 122 123
            _shader->deactivate();
            tgt::TextureUnit::setZeroUnit();
            LGL_ERROR;

124
            data.addData(p_targetImageId.getValue(), new RenderData(_fbo));
schultezub's avatar
schultezub committed
125 126
        }
        else {
127
            LDEBUG("No suitable input images found.");
schultezub's avatar
schultezub committed
128 129 130 131 132
        }
    }

}