rendertargetcompositor.cpp 5.23 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-2013, 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 Universität München
//      Boltzmannstr. 3, 85748 Garching b. München, 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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
        , p_alphaValue("AlphaValue", "AlphaValue", .5f, 0.f, 1.f)
        , _shader(0)
    {
        addProperty(&p_firstImageId);
        addProperty(&p_secondImageId);
        addProperty(&p_targetImageId);
        addProperty(&p_compositingMethod);
        addProperty(&p_alphaValue);

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

    RenderTargetCompositor::~RenderTargetCompositor() {

    }

    void RenderTargetCompositor::init() {
        VisualizationProcessor::init();
        _shader = ShdrMgr.loadSeparate("core/glsl/passthrough.vert", "modules/vis/glsl/rendertargetcompositor.frag", "", false);
        _shader->setAttributeLocation(0, "in_Position");
        _shader->setAttributeLocation(1, "in_TexCoord");
    }

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

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

        if (firstImage != 0 && secondImage != 0 ) {
90
            FramebufferActivationGuard fag(this);
91 92 93
            createAndAttachColorTexture();
            createAndAttachDepthTexture();

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

97 98
            firstImage->bind(_shader, firstColorUnit, firstDepthUnit, "_firstColor", "_firstDepth", "_firstTexParams");
            secondImage->bind(_shader, secondColorUnit, secondDepthUnit, "_secondColor", "_secondDepth", "_secondTexParams");
schultezub's avatar
schultezub committed
99
            _shader->setUniform("_compositingMethod", p_compositingMethod.getOptionValue());
schultezub's avatar
schultezub committed
100 101 102 103 104 105 106 107 108 109
            _shader->setUniform("_alpha", p_alphaValue.getValue());

            decorateRenderProlog(data, _shader);
            glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
            QuadRdr.renderQuad();

            _shader->deactivate();
            tgt::TextureUnit::setZeroUnit();
            LGL_ERROR;

110
            data.addData(p_targetImageId.getValue(), new RenderData(_fbo));
schultezub's avatar
schultezub committed
111 112 113 114 115
        }
        else {
            LERROR("No suitable input images found.");
        }

116
        validate(INVALID_RESULT);
schultezub's avatar
schultezub committed
117 118 119 120
    }

}