24.09., 9:00 - 11:00: Due to updates GitLab will be unavailable for some minutes between 09:00 and 11:00.

localambientocclusiondecorator.cpp 4.67 KB
Newer Older
Jakob Weiss's avatar
Jakob Weiss committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 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 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
// ================================================================================================
// 
// This file is part of the CAMPVis Software Framework.
// 
// If not explicitly stated otherwise: Copyright (C) 2012-2015, all rights reserved,
//      Christian Schulte zu Berge <christian.szb@in.tum.de>
//      Chair for Computer Aided Medical Procedures
//      Technische Universitaet Muenchen
//      Boltzmannstr. 3, 85748 Garching b. Muenchen, Germany
// 
// For a full list of authors and contributors, please refer to the file "AUTHORS.txt".
// 
// 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
// 
// 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.
// 
// ================================================================================================

#include "localambientocclusiondecorator.h"

#include "cgt/shadermanager.h"
#include "cgt/textureunit.h"
#include "core/properties/propertycollection.h"

#include "core/datastructures/imagerepresentationgl.h"
#include "core/datastructures/renderdata.h"
#include "core/classification/geometry1dtransferfunction.h"

namespace campvis {

    LocalAmbientOcclusionDecorator::LocalAmbientOcclusionDecorator()
        : AbstractProcessorDecorator()
        , p_aoRays("AORays", "Number of directional samples", 8, 0, 50)
        , p_aoSamples("AOSamples", "Number of Samples per ray (controls size of AO sphere)", 10, 0, 80)
        , p_aoSphereRadius("AOSphereRadius", "AO Sphere Radius [voxels]", 8.0f, 0.1f, 50.0f, 0.25f)
        , p_aoOpacityScale("OpacityScale", "Opacity Scaling", 1.0f, 0.0f, 5.f)
        , p_aoEffectGamma("AOEffectGamma", "AO Scale Gamma", 1.0f, .0f, 5.f)
        , p_aoEmissiveTransferFunction("AOTransferFunction", "Emissive Transfer Function", new Geometry1DTransferFunction(128))
        , p_aoEmissiveScale("EmissiveScale", "Emissive Color Scaling", 1.0f, 0.0f, 10.f)
    {
    }

    LocalAmbientOcclusionDecorator::~LocalAmbientOcclusionDecorator() {

    }

    void LocalAmbientOcclusionDecorator::addProperties(AbstractProcessor* propCollection) {
        propCollection->addProperty(p_aoRays, AbstractProcessor::INVALID_RESULT | AbstractProcessor::INVALID_PROPERTIES | AbstractProcessor::INVALID_SHADER);
        propCollection->addProperty(p_aoSamples, AbstractProcessor::INVALID_RESULT | AbstractProcessor::INVALID_PROPERTIES | AbstractProcessor::INVALID_SHADER);
        propCollection->addProperty(p_aoSphereRadius);
        propCollection->addProperty(p_aoOpacityScale);
        propCollection->addProperty(p_aoEffectGamma);
        propCollection->addProperty(p_aoEmissiveTransferFunction);
        propCollection->addProperty(p_aoEmissiveScale);
    }

    void LocalAmbientOcclusionDecorator::renderProlog(const DataContainer& dataContainer, cgt::Shader* shader) {
        auto aotf = p_aoEmissiveTransferFunction.getTF();
        _aoTFUnit = std::make_unique<cgt::TextureUnit>();
        aotf->bind(shader, *_aoTFUnit, "_aoEmissiveTF", "_aoEmissiveTFParams");

        shader->setUniform("_aoSphereRadius", p_aoSphereRadius.getValue());
        shader->setUniform("_aoGamma", p_aoEffectGamma.getValue());
        shader->setUniform("_aoEmissiveScale", p_aoEmissiveScale.getValue());
        shader->setUniform("_aoOpacityScale", p_aoOpacityScale.getValue());
    }

    void LocalAmbientOcclusionDecorator::renderEpilog(cgt::Shader * shader)
    {
        // delete the TF texture unit
        _aoTFUnit = nullptr;
    }

    std::string LocalAmbientOcclusionDecorator::generateHeader() const
    {
        std::string toReturn;
        // the defines need to exist before the include
        toReturn +=
            "#define NUM_AO_RAYS " + std::to_string(p_aoRays.getValue()) + "\n"
            "#define NUM_AO_RAY_STEPS " + std::to_string(p_aoSamples.getValue()) + "\n"
            "\n"
            "uniform float _aoSphereRadius;\n"
            "uniform float _aoGamma;\n"
            "uniform float _aoOpacityScale;\n"
            "uniform float _aoEmissiveScale;\n"
            "\n"
            "#define AO_GAMMA _aoGamma\n"
            "#define AO_OPACITY_SCALE _aoOpacityScale\n"
            "#define AO_EMISSIVE_SCALE _aoEmissiveScale\n";

        // the include has the actual functions
        toReturn += "#include \"modules/advancedraycasting/glsl/localambientocclusion.frag\"\n";

        return toReturn;
    }

}