abstracttransferfunction.cpp 4.44 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
26
#include "abstracttransferfunction.h"

27
#include <tbb/tbb.h>
28
29
30
#include "tgt/assert.h"
#include "tgt/logmanager.h"
#include "tgt/shadermanager.h"
31
#include "tgt/texture.h"
32
33
#include "tgt/textureunit.h"

34
#include "core/datastructures/imagerepresentationlocal.h"
35

schultezub's avatar
schultezub committed
36
namespace campvis {
37
    
schultezub's avatar
schultezub committed
38
    const std::string AbstractTransferFunction::loggerCat_ = "CAMPVis.core.classification.AbstractTransferFunction";
39
40
41
42
43
44

    AbstractTransferFunction::AbstractTransferFunction(const tgt::svec3& size, const tgt::vec2& intensityDomain /*= tgt::vec2(0.f, 1.f)*/)
        : _size(size)
        , _intensityDomain(intensityDomain)
        , _texture(0)
    {
45
        _dirtyTexture = false;
46
47
48
49
    }

    AbstractTransferFunction::~AbstractTransferFunction() {
        if (_texture != 0)
50
            LWARNING("Called AbstractTransferFunction dtor without proper deinitialization - you just wasted resources!");
51
52
53
54
55
56
57
    }

    void AbstractTransferFunction::deinit() {
        delete _texture;
        _texture = 0;
    }

58
    void AbstractTransferFunction::bind(tgt::Shader* shader, const tgt::TextureUnit& texUnit, const std::string& transFuncUniform /*= "_transferFunction"*/, const std::string& transFuncParamsUniform /*= "_transferFunctionParameters"*/) {
59
60
        tgtAssert(shader != 0, "Shader must not be 0.");

61
62
        {
            tbb::mutex::scoped_lock lock(_localMutex);
63
            if (_texture == 0 || _dirtyTexture) {
64
65
66
67
68
69
70
71
                if (shader->isActivated()) {
                    shader->deactivate();
                    createTexture();
                    shader->activate();
                }
                else {
                    createTexture();
                }
72
73
74
75
76
77
78
79
            }
        }

        texUnit.activate();
        _texture->bind();

        bool tmp = shader->getIgnoreUniformLocationError();
        shader->setIgnoreUniformLocationError(true);
80
        shader->setUniform(transFuncUniform, texUnit.getUnitNumber());
81
82
        switch (getDimensionality()) {
            case 1:
83
                shader->setUniform(transFuncParamsUniform + "._intensityDomain", tgt::vec2(_intensityDomain));
84
85
                break;
            case 2:
86
                shader->setUniform(transFuncParamsUniform + "._intensityDomainX", tgt::vec2(_intensityDomain));
87
88
89
90
91
92
                break;
            default:
                tgtAssert(false, "Unsupported TF dimensionality!");
                break;
        }
        
93
94
        shader->setIgnoreUniformLocationError(tmp);
    }
95
    
96
    void AbstractTransferFunction::setIntensityDomain(const tgt::vec2& newDomain) {
97
        tgtAssert(newDomain.x <= newDomain.y, "Intensity domain is not a valid interval.");
98
99
100
101
        {
            tbb::mutex::scoped_lock lock(_localMutex);
            _intensityDomain = newDomain;
        }
102
        s_intensityDomainChanged();
schultezub's avatar
schultezub committed
103
        s_changed();
104
105
    }

106
107
108
109
    const tgt::vec2& AbstractTransferFunction::getIntensityDomain() const {
        return _intensityDomain;
    }

110
111
112
    const tgt::Texture* AbstractTransferFunction::getTexture() {
        {
            tbb::mutex::scoped_lock lock(_localMutex);
113
            if (_texture == 0 || _dirtyTexture) {
114
115
116
117
118
                createTexture();
            }
        }
        return _texture;
    }
119
    
120
}