File: transfer_function.hpp

package info (click to toggle)
sight 25.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 42,180 kB
  • sloc: cpp: 289,476; xml: 17,257; ansic: 9,878; python: 1,379; sh: 144; makefile: 33
file content (115 lines) | stat: -rw-r--r-- 4,342 bytes parent folder | download | duplicates (2)
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
104
105
106
107
108
109
110
111
112
113
114
115
/************************************************************************
 *
 * Copyright (C) 2015-2025 IRCAD France
 * Copyright (C) 2015-2019 IHU Strasbourg
 *
 * This file is part of Sight.
 *
 * Sight is free software: you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Sight is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with Sight. If not, see <https://www.gnu.org/licenses/>.
 *
 ***********************************************************************/

#pragma once

#include <sight/viz/scene3d/config.hpp>

#include <data/transfer_function.hpp>

#include <viz/scene3d/resource.hpp>

#include <OGRE/OgrePass.h>
#include <OGRE/OgreTexture.h>

namespace sight::viz::scene3d
{

/**
 * @brief This binds a sight::data::transfer_function to a shared Ogre texture. It allows to share the GPU memory from
 * multiple callers that use the same object, and only update the GPU buffer when necessary.
 */
class SIGHT_VIZ_SCENE3D_CLASS_API transfer_function : public resource<data::transfer_function,
                                                                      Ogre::Texture,
                                                                      transfer_function>
{
public:

    /// Instantiates a transfer function. If a texture matching the transfer function name does not exist, it creates a
    /// new one, otherwise it wraps the existing one.
    /// @param _tf input transfer function
    /// @param _suffix_id optional suffix to the object identifier, this allows to create different copies of the object
    SIGHT_VIZ_SCENE3D_API transfer_function(
        const data::transfer_function::csptr& _tf,
        const std::string& _suffix_id = ""
    );
    SIGHT_VIZ_SCENE3D_API ~transfer_function();

    /// Update the TF texture according to the transfer function data.
    SIGHT_VIZ_SCENE3D_API void update() override;

    /// Bind the texture and the uniforms in a given pass
    template<class GPU_PARAMETERS>
    void bind(
        const Ogre::Pass* _pass,
        const std::string& _tex_unit_name,
        std::shared_ptr<GPU_PARAMETERS> _params,
        const std::string& _uniform = "u_f3TFWindow"
    ) const;

    /// Stores the tf window to upload it when necessary as a fragment shader uniform
    Ogre::Vector3 m_window;

    Ogre::TextureFilterOptions m_filtering {Ogre::TFO_BILINEAR};
    Ogre::TextureAddressingMode m_addressing_mode {Ogre::TextureUnitState::TAM_CLAMP};
    Ogre::ColourValue m_border_color {Ogre::ColourValue(0.0, 0.0, 1.0, 1.0)};
};

//------------------------------------------------------------------------------

template<class GPU_PARAMETERS>
inline void transfer_function::bind(
    const Ogre::Pass* const _pass,
    const std::string& _tex_unit_name,
    std::shared_ptr<GPU_PARAMETERS> _params,
    const std::string& _uniform
) const
{
    SIGHT_ASSERT("Pass is null", _pass);
    SIGHT_ASSERT("Parameters pointer is null", _params);

    auto* tex_unit_state = _pass->getTextureUnitState(_tex_unit_name);
    SIGHT_ASSERT("'" + _tex_unit_name + "' texture unit is not found", tex_unit_state);

    if(m_resource && tex_unit_state->getTextureName() != m_resource->getName())
    {
        tex_unit_state->setTexture(m_resource);
    }

    tex_unit_state->setTextureFiltering(m_filtering);
    tex_unit_state->setTextureAddressingMode(m_addressing_mode);

    Ogre::Sampler::UVWAddressingMode addressing_mode =
        tex_unit_state->getTextureAddressingMode();
    if(addressing_mode.u == Ogre::TextureAddressingMode::TAM_BORDER
       && addressing_mode.v == Ogre::TextureAddressingMode::TAM_BORDER
       && addressing_mode.w == Ogre::TextureAddressingMode::TAM_BORDER)
    {
        tex_unit_state->setTextureBorderColour(m_border_color);
    }

    _params->setNamedConstant(_uniform, m_window);
}

//-----------------------------------------------------------------------------

} // namespace sight::viz::scene3d