The name of the initial branch for new projects is now "main" instead of "master". Existing projects remain unchanged. More information: https://doku.lrz.de/display/PUBLIC/GitLab

raycastingprocessor.cpp 8.03 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, all rights reserved,
schultezub's avatar
schultezub committed
6
//      Christian Schulte zu Berge <christian.szb@in.tum.de>
7 8 9
//      Chair for Computer Aided Medical Procedures
//      Technische Universitt Mnchen
//      Boltzmannstr. 3, 85748 Garching b. Mnchen, Germany
schultezub's avatar
schultezub committed
10
// For a full list of authors and contributors, please refer to the file "AUTHORS.txt".
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
// 
// The licensing of this softare is not yet resolved. Until then, redistribution in source or
// binary forms outside the CAMP chair is not permitted, unless explicitly stated in legal form.
// However, the names of the original authors and the above copyright notice must retain in its
// original state in any case.
// 
// Legal disclaimer provided by the BSD license:
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 
// AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
// 
// ================================================================================================

30 31 32 33 34 35 36
#include "raycastingprocessor.h"

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

#include "core/datastructures/imagedata.h"
37 38
#include "core/datastructures/imagerepresentationgl.h"
#include "core/datastructures/imagerepresentationrendertarget.h"
39

40 41 42

#include "core/classification/simpletransferfunction.h"

schultezub's avatar
schultezub committed
43 44
namespace campvis {
    const std::string RaycastingProcessor::loggerCat_ = "CAMPVis.modules.vis.RaycastingProcessor";
45

46
    RaycastingProcessor::RaycastingProcessor(IVec2Property& renderTargetSize, const std::string& fragmentShaderFileName, bool bindEntryExitDepthTextures)
schultezub's avatar
schultezub committed
47
        : VisualizationProcessor(renderTargetSize)
48 49 50 51 52
        , p_sourceImageID("sourceImageID", "Input Image", "", DataNameProperty::READ)
        , p_entryImageID("entryImageID", "Input Entry Points Image", "", DataNameProperty::READ)
        , p_exitImageID("exitImageID", "Input Exit Points Image", "", DataNameProperty::READ)
        , p_camera("camera", "Camera")
        , p_transferFunction("transferFunction", "Transfer Function", new SimpleTransferFunction(256))
53
        , p_samplingRate("SamplingRate", "Sampling Rate", 2.f, 0.1f, 10.f)
54 55
        , p_jitterEntryPoints("jitterEntryPoints", "Jitter Entry Points", true)
        , p_jitterStepSizeMultiplier("jitterStepSizeMultiplier", "Jitter Step Size Multiplier", .5f, .1f, 1.f)
56 57 58
        , _fragmentShaderFilename(fragmentShaderFileName)
        , _shader(0)
        , _bindEntryExitDepthTextures(bindEntryExitDepthTextures)
59
        , _sourceImageTimestamp(0)
60
    {
61 62 63 64 65
        addProperty(&p_sourceImageID);
        addProperty(&p_entryImageID);
        addProperty(&p_exitImageID);
        addProperty(&p_camera);  
        addProperty(&p_transferFunction);
66
        addProperty(&p_samplingRate);
67 68
        addProperty(&p_jitterEntryPoints);
        addProperty(&p_jitterStepSizeMultiplier);
69 70 71 72 73 74 75 76
    }

    RaycastingProcessor::~RaycastingProcessor() {

    }

    void RaycastingProcessor::init() {
        VisualizationProcessor::init();
77
        _shader = ShdrMgr.loadSeparate("core/glsl/passthrough.vert", _fragmentShaderFilename, generateHeader(), false);
78 79
        _shader->setAttributeLocation(0, "in_Position");
        _shader->setAttributeLocation(1, "in_TexCoord");
80 81 82 83 84 85 86 87 88
    }

    void RaycastingProcessor::deinit() {
        ShdrMgr.dispose(_shader);
        _shader = 0;
        VisualizationProcessor::deinit();
    }

    void RaycastingProcessor::process(DataContainer& data) {
89 90 91
        ImageRepresentationGL::ScopedRepresentation img(data, p_sourceImageID.getValue());
        ImageRepresentationRenderTarget::ScopedRepresentation entryPoints(data, p_entryImageID.getValue());
        ImageRepresentationRenderTarget::ScopedRepresentation exitPoints(data, p_exitImageID.getValue());
92 93 94

        if (img != 0 && entryPoints != 0 && exitPoints != 0) {
            if (img->getDimensionality() == 3) {
95 96 97 98 99 100
                if (img.getDataHandle().getTimestamp() != _sourceImageTimestamp) {
                    // source DataHandle has changed
                    _sourceImageTimestamp = img.getDataHandle().getTimestamp();
                    p_transferFunction.getTF()->setImageHandle(img.getDataHandle());
                }

101
                if (hasInvalidShader()) {
102 103
                    _shader->setHeaders(generateHeader());
                    _shader->rebuild();
104
                    validate(INVALID_SHADER);
105 106 107
                }

                glPushAttrib(GL_ALL_ATTRIB_BITS);
108

109
                glEnable(GL_DEPTH_TEST);
110
                _shader->activate();
111 112

                _shader->setIgnoreUniformLocationError(true);
113
                decorateRenderProlog(data, _shader);
114
                _shader->setUniform("_viewportSizeRCP", 1.f / tgt::vec2(_renderTargetSize.getValue()));
115 116
                _shader->setUniform("_jitterEntryPoints", p_jitterEntryPoints.getValue());
                _shader->setUniform("_jitterStepSizeMultiplier", p_jitterStepSizeMultiplier.getValue());
117 118 119

                float samplingStepSize = 1.f / (p_samplingRate.getValue() * tgt::max(img->getSize()));
                _shader->setUniform("_samplingStepSize", samplingStepSize);
120

121
                const tgt::Camera& cam = p_camera.getValue();
122 123
                float n = cam.getNearDist();
                float f = cam.getFarDist();
124
                _shader->setUniform("_cameraPosition", cam.getPosition());
125 126 127 128 129 130
                _shader->setUniform("const_to_z_e_1", 0.5f + 0.5f*((f+n)/(f-n)));
                _shader->setUniform("const_to_z_e_2", ((f-n)/(f*n)));
                _shader->setUniform("const_to_z_w_1", ((f*n)/(f-n)));
                _shader->setUniform("const_to_z_w_2", 0.5f*((f+n)/(f-n))+0.5f);
                _shader->setIgnoreUniformLocationError(false);

131
                tgt::TextureUnit volumeUnit, entryUnit, exitUnit, tfUnit;
132
                img->bind(_shader, volumeUnit, "_volume", "_volumeTextureParams");
133
                p_transferFunction.getTF()->bind(_shader, tfUnit);
134 135

                if (! _bindEntryExitDepthTextures) {
136 137
                    entryPoints->bindColorTexture(_shader, entryUnit, "_entryPoints", "_entryParams");
                    exitPoints->bindColorTexture(_shader, exitUnit, "_exitPoints", "_exitParams");
138 139 140 141
                    processImpl(data);
                }
                else {
                    tgt::TextureUnit entryUnitDepth, exitUnitDepth;
142 143
                    entryPoints->bind(_shader, entryUnit, entryUnitDepth, "_entryPoints", "_entryPointsDepth", "_entryParams");
                    exitPoints->bind(_shader, exitUnit, exitUnitDepth, "_exitPoints", "_exitPointsDepth", "_exitParams");
144 145 146
                    processImpl(data);
                }

147
                decorateRenderEpilog(_shader);
148 149 150 151 152 153 154 155 156 157 158 159 160
                _shader->deactivate();
                tgt::TextureUnit::setZeroUnit();
                glPopAttrib();
                LGL_ERROR;
            }
            else {
                LERROR("Input image must have dimensionality of 3.");
            }
        }
        else {
            LERROR("No suitable input image found.");
        }

161
        validate(INVALID_RESULT);
162 163 164
    }

    std::string RaycastingProcessor::generateHeader() const {
165
        std::string toReturn = getDecoratedHeader();
166 167 168 169
        return toReturn;
    }

}