File: texture.cpp

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 (156 lines) | stat: -rw-r--r-- 4,818 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/************************************************************************
 *
 * Copyright (C) 2015-2025 IRCAD France
 * Copyright (C) 2015-2020 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/>.
 *
 ***********************************************************************/

#include "viz/scene3d/texture.hpp"

#include <viz/scene3d/detail/texture_manager.hpp>

namespace sight::viz::scene3d
{

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

texture::texture(const data::image::csptr& _image, const std::string& _suffix_id)
{
    m_resource = detail::texture_manager::get()->instantiate(_image, _suffix_id);
}

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

texture::~texture()
{
    if(m_resource)
    {
        detail::texture_manager::get()->release(m_resource);
    }
}

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

void texture::update()
{
    m_window = viz::scene3d::detail::texture_manager::get()->load(m_resource).second;
}

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

void texture::set_dirty()
{
    if(m_resource)
    {
        m_resource->touch();
    }
}

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

void texture::bind(
    Ogre::TextureUnitState* _tex_unit,
    Ogre::TextureType _type,
    Ogre::TextureFilterOptions _filter_type,
    Ogre::TextureAddressingMode _address_mode
) const
{
    SIGHT_ASSERT("The texture unit is null.", _tex_unit);

    _tex_unit->setTextureName(m_resource->getName(), _type);
    _tex_unit->setTextureFiltering(_filter_type);
    _tex_unit->setTextureAddressingMode(_address_mode);
}

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

void texture::bind(
    Ogre::Pass* _pass,
    const std::string& _sampler_name,
    std::optional<Ogre::TextureFilterOptions> _filter_type,
    std::optional<Ogre::TextureAddressingMode> _address_mode,
    bool _upload_window_uniform
) const
{
    SIGHT_ASSERT("The pass is null.", _pass);

    Ogre::TextureUnitState* tex_unit = _pass->getTextureUnitState(_sampler_name);
    SIGHT_ASSERT("The sampler '" + _sampler_name + "' cannot be retrieved.", tex_unit);

    if(_filter_type != std::nullopt)
    {
        tex_unit->setTextureFiltering(_filter_type.value());
    }

    if(_address_mode != std::nullopt)
    {
        tex_unit->setTextureAddressingMode(_address_mode.value());
    }

    if(m_resource)
    {
        tex_unit->setTexture(m_resource);
        tex_unit->setTextureName(m_resource->getName(), m_resource->getTextureType());
    }

    auto fp_params = _pass->getFragmentProgramParameters();
    if(_upload_window_uniform && fp_params && (fp_params->_findNamedConstantDefinition("u_window") != nullptr))
    {
        fp_params->setNamedConstant("u_window", this->window());
    }
}

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

void texture::create_binding(
    Ogre::Pass* _pass,
    const std::string& _sampler_name,
    const std::string& _uniform_name,
    std::optional<Ogre::TextureFilterOptions> _filter_type,
    std::optional<Ogre::TextureAddressingMode> _address_mode
) const
{
    SIGHT_ASSERT("The pass is null.", _pass);

    Ogre::TextureUnitState* const tex_unit = _pass->createTextureUnitState(_sampler_name);
    SIGHT_ASSERT("The sampler '" + _sampler_name + "' cannot be retrieved.", tex_unit);

    if(_filter_type != std::nullopt)
    {
        tex_unit->setTextureFiltering(_filter_type.value());
    }

    if(_address_mode != std::nullopt)
    {
        tex_unit->setTextureAddressingMode(_address_mode.value());
    }

    if(m_resource)
    {
        tex_unit->setTexture(m_resource);
        tex_unit->setTextureName(m_resource->getName(), m_resource->getTextureType());
    }

    auto fp_params      = _pass->getFragmentProgramParameters();
    auto tex_unit_index = _pass->getTextureUnitStateIndex(tex_unit);
    fp_params->setNamedConstant(_uniform_name, tex_unit_index);
}

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

} // namespace sight::viz::scene3d