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

genericgeometrytransferfunction.h 10.1 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
//      Chair for Computer Aided Medical Procedures
8
9
//      Technische Universität München
//      Boltzmannstr. 3, 85748 Garching b. München, 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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// 
// 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.
// 
// ================================================================================================

#ifndef GENERICGEOMETRYTRANSFERFUNCTION_H__
#define GENERICGEOMETRYTRANSFERFUNCTION_H__

#include "core/classification/abstracttransferfunction.h"

#include "tgt/assert.h"
#include "tgt/framebufferobject.h"
#include "tgt/logmanager.h"
#include "tgt/shadermanager.h"
#include "tgt/texture.h"
#include "tgt/textureunit.h"

#include <vector>

schultezub's avatar
schultezub committed
44
namespace campvis {
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66

    /**
     * Generic base class for transfer functions built from multiple geometries.
     * \tparam  T   Type of the base geometry class.
     */
    template<class T>
    class GenericGeometryTransferFunction : public AbstractTransferFunction, public sigslot::has_slots<> {
    public:
        /// Typedef for the geometry class this transfer function is built from.
        typedef T GeometryType;

        /**
         * Creates a new GenericGeometryTransferFunction.
         * \param   size            Size of the transfer function texture
         * \param   intensityDomain Intensity Domain where the transfer function is mapped to during classification
         */
        GenericGeometryTransferFunction(const tgt::vec3& size, const tgt::vec2& intensityDomain = tgt::vec2(0.f, 1.f));

        /**
         * Destructor, make sure to delete the OpenGL texture beforehand by calling deinit() with a valid OpenGL context!
         */
        virtual ~GenericGeometryTransferFunction();
67
68
69
70
71
        
        /**
         * Initializes the Shader, hence, this methods has to be called from a thread with a valid OpenGL context!
         */
        virtual void initShader();
72
73

        /**
74
         * Deletes the OpenGL texture and shader, hence, this methods has to be called from a thread with a valid OpenGL context!
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
104
105
106
107
108
109
110
111
112
113
114
         */
        virtual void deinit();

        /**
         * Gets the list of transfer function geometries.
         * \return  _geometries
         */
        const std::vector<T*>& getGeometries() const;

        /**
         * Adds the given TF geometry to this transfer function.
         * \note    GenericGeometryTransferFunction takes ownership \a geometry.
         * \param   geometry    TF geometry to add, GenericGeometryTransferFunction takes the ownership.
         */
        void addGeometry(T* geometry);

        /**
         * Removes the given TF geometry from this transfer function.
         * \note    After the call \a geometry will no longer be valid as GenericGeometryTransferFunction deletes the given T.
         * \param   geometry    TF geometry to remove, GenericGeometryTransferFunction will delete it
         */
        void removeGeometry(T* geometry);

        /**
         * Slot to be called by T's s_changed signal.
         */
        void onGeometryChanged();

        /// Signal to be emitted when the vector of T objects (_geometries) changed (The collection, not the actual geometry).
        sigslot::signal0<> s_geometryCollectionChanged;

    protected:
        /**
         * Creates the texture and uploads it to OpenGL.
         * Gets called by bind() with the local mutex already acquired.
         */
        virtual void createTexture();

        std::vector<T*> _geometries;        ///< The list of transfer function geometries.
        tgt::FramebufferObject* _fbo;       ///< The FBO used for render into texture.
115
        tgt::Shader* _shader;               ///< Shader for rendering the TF into a texture
116
117
118
119
120
    };

// ================================================================================================

    template<class T>
schultezub's avatar
schultezub committed
121
    campvis::GenericGeometryTransferFunction<T>::GenericGeometryTransferFunction(const tgt::vec3& size, const tgt::vec2& intensityDomain /*= tgt::vec2(0.f, 1.f)*/)
122
123
        : AbstractTransferFunction(size, intensityDomain)
        , _fbo(0)
124
        , _shader(0)
125
126
127
128
129
    {

    }

    template<class T>
schultezub's avatar
schultezub committed
130
    campvis::GenericGeometryTransferFunction<T>::~GenericGeometryTransferFunction() {
131
132
133
134
135
136
137
138
139
140
141
142
    }

    template<class T>
    void campvis::GenericGeometryTransferFunction<T>::initShader() {
        _shader = ShdrMgr.loadSeparate("core/glsl/passthrough.vert", "core/glsl/passthrough.frag", "", false);
        if (_shader != 0) {
            _shader->setAttributeLocation(0, "in_Position");
            _shader->setAttributeLocation(1, "in_TexCoord");
            _shader->setAttributeLocation(2, "in_Color");
        }
        else {
            LERROR("Could not create Shader for Rendering the TF into the lookup texture!");
143
144
145
146
        }
    }

    template<class T>
schultezub's avatar
schultezub committed
147
    void campvis::GenericGeometryTransferFunction<T>::deinit() {
148
149
150
151
152
153
        for (typename std::vector<T*>::iterator it = _geometries.begin(); it != _geometries.end(); ++it) {
            (*it)->s_changed.disconnect(this);
            delete *it;
        }
        _geometries.clear();

154
155
156
157
158
        if (_fbo != 0) {
            _fbo->activate();
            _fbo->detachAll();
            _fbo->deactivate();
            delete _fbo;
159
            _fbo = 0;
160
161
        }

162
163
        ShdrMgr.dispose(_shader);
        _shader = 0;
164
165
166
167
        AbstractTransferFunction::deinit();
    }

    template<class T>
schultezub's avatar
schultezub committed
168
    const std::vector<T*>& campvis::GenericGeometryTransferFunction<T>::getGeometries() const {
169
170
171
172
        return  _geometries;
    }

    template<class T>
schultezub's avatar
schultezub committed
173
    void campvis::GenericGeometryTransferFunction<T>::addGeometry(T* geometry) {
174
175
176
177
178
179
180
181
182
183
184
        {
            tbb::mutex::scoped_lock lock(_localMutex);
            _geometries.push_back(geometry);
        }
        geometry->s_changed.connect(this, &GenericGeometryTransferFunction<T>::onGeometryChanged);
        _dirtyTexture = true;
        s_geometryCollectionChanged();
        s_changed();
    }

    template<class T>
schultezub's avatar
schultezub committed
185
    void campvis::GenericGeometryTransferFunction<T>::removeGeometry(T* geometry) {
186
187
        {
            tbb::mutex::scoped_lock lock(_localMutex);
schultezub's avatar
schultezub committed
188
            for (typename std::vector<T*>::iterator it = _geometries.begin(); it != _geometries.end(); ++it) {
189
190
191
192
193
194
195
196
197
198
199
200
201
202
                if (*it == geometry) {
                    _geometries.erase(it);
                    break;
                }
            }
        }
        geometry->s_changed.disconnect(this);
        delete geometry;
        _dirtyTexture = true;
        s_geometryCollectionChanged();
        s_changed();
    }

    template<class T>
schultezub's avatar
schultezub committed
203
    void campvis::GenericGeometryTransferFunction<T>::onGeometryChanged() {
204
205
206
207
208
        _dirtyTexture = true;
        s_changed();
    }

    template<class T>
schultezub's avatar
schultezub committed
209
    void campvis::GenericGeometryTransferFunction<T>::createTexture() {
210
211
212
213
        if (_shader == 0) {
            initShader();
        }

214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
        // acqiure a new TextureUnit, so that we don't mess with other currently bound textures during texture upload...
        tgt::TextureUnit tfUnit;
        tfUnit.activate();

        // detach old texture from FBO and delete it
        if (_texture != 0 && _fbo != 0) {
            _fbo->activate();
            _fbo->detachAll();
            _fbo->deactivate();
        }
        delete _texture;

        // create FBO if needed
        if (_fbo == 0) 
            _fbo = new tgt::FramebufferObject();
        _fbo->activate();
        LGL_ERROR;

        // create texture
        GLenum dataType = GL_UNSIGNED_BYTE;
        _texture = new tgt::Texture(_size, GL_RGBA, dataType, tgt::Texture::LINEAR);
schultezub's avatar
schultezub committed
235
        _texture->setWrapping(tgt::Texture::CLAMP_TO_EDGE);
236
237
238
239
240
241
242
243
244
245
246
247
248
249
        _texture->uploadTexture();
        LGL_ERROR;

        // attach texture to FBO
        _fbo->attachTexture(_texture);
        if (! _fbo->isComplete()) {
            LERROR("Incomplete FBO.");
            _fbo->deactivate();
            return;
        }
        LGL_ERROR;

        // render TF geometries into texture
        glViewport(0, 0, _texture->getWidth(), _texture->getHeight());
250
        glClear(GL_COLOR_BUFFER_BIT);
251

252
253
254
        _shader->activate();
        _shader->setUniform("_projectionMatrix", tgt::mat4::createOrtho(0, 1, 0, 1, -1, 1));
        LGL_ERROR;
255

schultezub's avatar
schultezub committed
256
        for (typename std::vector<T*>::const_iterator it = _geometries.begin(); it != _geometries.end(); ++it) {
257
258
259
260
            (*it)->render();
        }
        LGL_ERROR;

261
262
        // deactivate Shader and FBO
        _shader->deactivate();
263
264
265
266
267
268
269
270
271
        _fbo->deactivate();
        LGL_ERROR;

        tgt::TextureUnit::setZeroUnit();
        _dirtyTexture = false;
    }
}

#endif // GENERICGEOMETRYTRANSFERFUNCTION_H__