depthdarkening.cpp 6.15 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 25
// 
// ================================================================================================

#include "depthdarkening.h"
26 27 28
#include "cgt/logmanager.h"
#include "cgt/shadermanager.h"
#include "cgt/textureunit.h"
29 30

#include "core/datastructures/imagedata.h"
31
#include "core/datastructures/imagerepresentationgl.h"
32
#include "core/datastructures/renderdata.h"
33
#include "core/tools/glreduction.h"
34 35 36 37 38

#include "core/classification/simpletransferfunction.h"

#include "core/tools/quadrenderer.h"

schultezub's avatar
schultezub committed
39 40
namespace campvis {
    const std::string DepthDarkening::loggerCat_ = "CAMPVis.modules.vis.DepthDarkening";
41

42 43
    DepthDarkening::DepthDarkening(IVec2Property* viewportSizeProp)
        : VisualizationProcessor(viewportSizeProp)
44 45
        , p_inputImage("InputImage", "Input Image", "", DataNameProperty::READ)
        , p_outputImage("OutputImage", "Output Image", "dd.output", DataNameProperty::WRITE)
46 47
        , p_sigma("Sigma", "Sigma of Gaussian Filter", 2.f, 0.f, 10.f, 0.1f)
        , p_lambda("Lambda", "Strength of Depth Darkening Effect", 10.f, 0.f, 50.f, 0.1f)
48
        , p_useColorCoding("UseColorCoding", "Cold/Warm Color Coding", false)
49 50
        , p_coldColor("ColdColor", "Cold Color (Far Objects)", cgt::vec3(0.f, 0.f, 1.f), cgt::vec3(0.f), cgt::vec3(1.f))
        , p_warmColor("WarmColor", "Warm Color (Near Objects)", cgt::vec3(1.f, 0.f, 0.f), cgt::vec3(0.f), cgt::vec3(1.f))
51
        , _shader(0)
52
        , _glReduction(0)
53
    {
54 55 56 57 58 59 60
        addProperty(p_inputImage);
        addProperty(p_outputImage);
        addProperty(p_sigma);
        addProperty(p_lambda);
        addProperty(p_useColorCoding, INVALID_RESULT | INVALID_SHADER);
        addProperty(p_coldColor);
        addProperty(p_warmColor);
61 62 63 64 65 66 67 68
    }

    DepthDarkening::~DepthDarkening() {

    }

    void DepthDarkening::init() {
        VisualizationProcessor::init();
69
        _shader = ShdrMgr.load("core/glsl/passthrough.vert", "modules/vis/glsl/depthdarkening.frag", generateHeader());
70
        _glReduction = new GlReduction(GlReduction::MIN_MAX_DEPTH_ONLY);
71 72 73 74
    }

    void DepthDarkening::deinit() {
        ShdrMgr.dispose(_shader);
75 76
        delete _glReduction;
        VisualizationProcessor::deinit();
77 78
    }

79
    void DepthDarkening::updateResult(DataContainer& data) {
80
        ScopedTypedData<RenderData> inputImage(data, p_inputImage.getValue());
81

82
        if (inputImage != 0 && inputImage->hasDepthTexture()) {
83
            const cgt::Texture* tex = inputImage->getDepthTexture()->getRepresentation<ImageRepresentationGL>()->getTexture();
84 85 86
            std::vector<float> tmp = _glReduction->reduce(tex);
            float minDepth = tmp[0];
            float maxDepth = tmp[1];
87

88
            FramebufferActivationGuard fag(this);
89 90 91 92
            glEnable(GL_DEPTH_TEST);
            glDepthFunc(GL_ALWAYS);

            _shader->activate();
93
            cgt::TextureUnit colorUnit, depthUnit, pass2DepthUnit;
94 95
            inputImage->bind(_shader, colorUnit, depthUnit);
            inputImage->bindDepthTexture(_shader, pass2DepthUnit, "_depthPass2Texture", "_pass2TexParams");
96
            
97 98
            _shader->setUniform("_viewportSizeRCP", 1.f / cgt::vec2(getEffectiveViewportSize()));
            _shader->setUniform("_direction", cgt::vec2(1.f, 0.f));
99 100
            _shader->setUniform("_sigma", p_sigma.getValue());
            _shader->setUniform("_lambda", p_lambda.getValue());
101 102
            _shader->setUniform("_minDepth", minDepth);
            _shader->setUniform("_maxDepth", maxDepth);
103 104 105
            if (p_useColorCoding.getValue()) {
                _shader->setUniform("_coldColor", p_coldColor.getValue());
                _shader->setUniform("_warmColor", p_warmColor.getValue());
106
            }
107

108
            createAndAttachColorTexture();
109
            createAndAttachDepthTexture();
110 111
            glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
            QuadRdr.renderQuad();
112

113
            RenderData tempTarget(_fbo);
114 115 116
            _fbo->detachAll();
            createAndAttachColorTexture();
            createAndAttachDepthTexture();
117

118
            inputImage->bind(_shader, colorUnit, depthUnit);
119
            tempTarget.bindDepthTexture(_shader, pass2DepthUnit, "_depthPass2Texture", "_pass2TexParams");
120
            _shader->setUniform("_direction", cgt::vec2(0.f, 1.f));
121 122 123 124 125
            
            glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
            QuadRdr.renderQuad();

            _shader->deactivate();
126
            cgt::TextureUnit::setZeroUnit();
127 128
            glDepthFunc(GL_LESS);
            glDisable(GL_DEPTH_TEST);
129 130
            LGL_ERROR;

131
            data.addData(p_outputImage.getValue(), new RenderData(_fbo));
132 133
        }
        else {
134
            LDEBUG("No suitable input image found.");
135 136 137
        }
    }

138
    std::string DepthDarkening::generateHeader() const {
139
        if (p_useColorCoding.getValue())
140 141 142 143 144
            return "#define USE_COLORCODING\n";
        else
            return "";
    }

145 146 147 148 149
    void DepthDarkening::updateShader() {
        _shader->setHeaders(generateHeader());
        _shader->rebuild();
    }

150 151
}