drrraycaster.cpp 5.16 KB
Newer Older
schultezub's avatar
schultezub 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
#include "drrraycaster.h"

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

#include "core/datastructures/imagedata.h"
#include "core/datastructures/imagedatagl.h"
#include "core/datastructures/imagedatarendertarget.h"
#include "core/datastructures/imagedataconverter.h"

#include "core/classification/simpletransferfunction.h"

namespace TUMVis {
    const std::string DRRRaycaster::loggerCat_ = "TUMVis.modules.vis.DRRRaycaster";

    DRRRaycaster::DRRRaycaster(GenericProperty<tgt::ivec2>& canvasSize)
        : VisualizationProcessor(canvasSize)
        , _sourceImageID("sourceImageID", "Input Image", "")
        , _entryImageID("entryImageID", "Output Entry Points Image", "")
        , _exitImageID("exitImageID", "Output Exit Points Image", "")
        , _targetImageID("targetImageID", "Output Image", "")
        , _transferFunction("transferFunction", "Transfer Function", new SimpleTransferFunction(256))
schultezub's avatar
schultezub committed
25
        , _samplingStepSize("samplingStepSize", "Sampling Step Size", .1f, 0.001f, 1.f)
schultezub's avatar
schultezub committed
26
27
28
        , _shift("shift", "Normalization Shift", 0.f, -10.f, 10.f)
        , _scale("scale", "Normalization Scale", 1.f, 0.f, 1000.f)
        , _invertMapping("invertMapping", "Invert Mapping", false, InvalidationLevel::INVALID_RESULT | InvalidationLevel::INVALID_SHADER)
schultezub's avatar
schultezub committed
29
        , _jitterEntryPoints("jitterEntryPoints", "Jitter Entry Points", true)
schultezub's avatar
schultezub committed
30
31
32
33
34
35
36
37
38
39
40
        , _shader(0)
    {
        addProperty(&_sourceImageID);
        addProperty(&_entryImageID);
        addProperty(&_exitImageID);
        addProperty(&_targetImageID);
        addProperty(&_transferFunction);
        addProperty(&_samplingStepSize);
        addProperty(&_shift);
        addProperty(&_scale);
        addProperty(&_invertMapping);
schultezub's avatar
schultezub committed
41
        addProperty(&_jitterEntryPoints);
schultezub's avatar
schultezub committed
42
43
44
45
46
47
48
49
    }

    DRRRaycaster::~DRRRaycaster() {

    }

    void DRRRaycaster::init() {
        VisualizationProcessor::init();
schultezub's avatar
schultezub committed
50
//         _shader = ShdrMgr.loadSeparate("core/glsl/passthrough.vert", "core/glsl/copyimage.frag", "", false);
schultezub's avatar
schultezub committed
51
        _shader = ShdrMgr.loadSeparate("core/glsl/passthrough.vert", "modules/vis/drrraycaster.frag", "", false);
schultezub's avatar
schultezub committed
52
53
        _shader->setHeaders(generateHeader());
        _shader->rebuild();
schultezub's avatar
schultezub committed
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
    }

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

    void DRRRaycaster::process(DataContainer& data) {
        DataContainer::ScopedTypedData<ImageDataGL> img(data, _sourceImageID.getValue());
        DataContainer::ScopedTypedData<ImageDataRenderTarget> entryPoints(data, _entryImageID.getValue());
        DataContainer::ScopedTypedData<ImageDataRenderTarget> exitPoints(data, _exitImageID.getValue());

        if (img != 0 && entryPoints != 0 && exitPoints != 0) {
            if (img->getDimensionality() == 3) {
                if (_invalidationLevel.isInvalidShader()) {
                    _shader->setHeaders(generateHeader());
                    _shader->rebuild();
                }

schultezub's avatar
schultezub committed
74
                ImageDataRenderTarget* rt = new ImageDataRenderTarget(tgt::svec3(_renderTargetSize.getValue(), 1));
schultezub's avatar
schultezub committed
75

schultezub's avatar
schultezub committed
76
                glPushAttrib(GL_ALL_ATTRIB_BITS);
schultezub's avatar
schultezub committed
77
                _shader->activate();
schultezub's avatar
schultezub committed
78
                _shader->setUniform("_viewportSizeRCP", 1.f / tgt::vec2(_renderTargetSize.getValue()));
schultezub's avatar
schultezub committed
79
                _shader->setUniform("_jitterEntryPoints", _jitterEntryPoints.getValue());
schultezub's avatar
schultezub committed
80
81
82
83
84
85
                _shader->setUniform("_samplingStepSize", _samplingStepSize.getValue());
                _shader->setUniform("_shift", _shift.getValue());
                _shader->setUniform("_scale", _scale.getValue());

                tgt::TextureUnit volumeUnit, entryUnit, exitUnit, tfUnit;
                img->bind(_shader, volumeUnit, "_volume");
schultezub's avatar
schultezub committed
86
87
                entryPoints->bind(_shader, &entryUnit, 0, "_entryPoints");
                exitPoints->bind(_shader, &exitUnit, 0, "_exitPoints");
schultezub's avatar
schultezub committed
88
89
                _transferFunction.getTF()->bind(_shader, tfUnit);

schultezub's avatar
schultezub committed
90
                rt->activate();
schultezub's avatar
schultezub committed
91
92
93
94
95
                if (_invertMapping.getValue())
                    glClearColor(0.f, 0.f, 0.f, 1.f);
                else
                    glClearColor(1.f, 1.f, 1.f, 1.f);

schultezub's avatar
schultezub committed
96
                glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
schultezub's avatar
schultezub committed
97
                LGL_ERROR;
schultezub's avatar
schultezub committed
98
                tgt::QuadRenderer::renderQuad();
schultezub's avatar
schultezub committed
99
100
                LGL_ERROR;
                rt->deactivate();
schultezub's avatar
schultezub committed
101
102
103

                _shader->deactivate();
                tgt::TextureUnit::setZeroUnit();
schultezub's avatar
schultezub committed
104
                glPopAttrib();
schultezub's avatar
schultezub committed
105

schultezub's avatar
schultezub committed
106
                data.addData(_targetImageID.getValue(), rt);
schultezub's avatar
schultezub committed
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
            }
            else {
                LERROR("Input image must have dimensionality of 3.");
            }
        }
        else {
            LERROR("No suitable input image found.");
        }

        _invalidationLevel.setValid();
    }

    std::string DRRRaycaster::generateHeader() const {
        std::string toReturn;

        if (_invertMapping.getValue())
            toReturn += "#define DRR_INVERT 1\n";
//         if (depthMapping_.get())
//             header +="#define DEPTH_MAPPING 1\n";

        return toReturn;
    }

}