Blue Brain BioExplorer
Material.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015-2024, EPFL/Blue Brain Project
3  *
4  * The Blue Brain BioExplorer is a tool for scientists to extract and analyse
5  * scientific data from visualization
6  *
7  * This file is part of Blue Brain BioExplorer <https://github.com/BlueBrain/BioExplorer>
8  *
9  * This library is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU Lesser General Public License version 3.0 as published
11  * by the Free Software Foundation.
12  *
13  * This library is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this library; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22 
23 #include "Material.h"
24 
28 
29 namespace core
30 {
31 Material::Material(const PropertyMap& properties)
32 {
33  setCurrentType(DEFAULT);
34  _properties.at(_currentType).merge(properties);
35 }
36 
38 {
39  const auto it = _textureDescriptors.find(type);
40  if (it == _textureDescriptors.end())
41  throw std::runtime_error("Failed to get texture with type " + std::to_string(static_cast<int>(type)));
42  return it->second;
43 }
44 
46 {
47  _textureDescriptors.clear();
48  markModified();
49 }
50 
51 bool Material::_loadTexture(const std::string& fileName, const TextureType type)
52 {
53  if (_textures.find(fileName) != _textures.end())
54  return true;
55 
56  auto texture = ImageManager::importTextureFromFile(fileName, type);
57  if (!texture)
58  return false;
59 
60  _textures[fileName] = texture;
61  CORE_DEBUG(fileName << ": " << texture->width << "x" << texture->height << "x" << (int)texture->channels << "x"
62  << (int)texture->depth << " added to the texture cache");
63  return true;
64 }
65 
66 void Material::setTexture(const std::string& fileName, const TextureType type)
67 {
68  auto i = _textureDescriptors.find(type);
69  if (i != _textureDescriptors.end() && i->second->filename == fileName)
70  return;
71 
72  if (_textures.find(fileName) == _textures.end())
73  if (!_loadTexture(fileName, type))
74  throw std::runtime_error("Failed to load texture from " + fileName);
75  _textureDescriptors[type] = _textures[fileName];
76  markModified();
77 }
78 
80 {
81  auto i = _textureDescriptors.find(type);
82  if (i == _textureDescriptors.end())
83  return;
84 
85  _textureDescriptors.erase(i);
86  markModified();
87 }
88 } // namespace core
void markModified(const bool triggerCallback=true)
Definition: BaseObject.h:65
static Texture2DPtr importTextureFromFile(const std::string &filename, const TextureType type)
Import a Texture from file.
TextureDescriptors _textureDescriptors
Definition: Material.h:265
PLATFORM_API void removeTexture(const TextureType type)
Removes the texture of the material for the specified texture type.
Definition: Material.cpp:79
bool _loadTexture(const std::string &fileName, const TextureType type)
Loads the texture for the specified texture type.
Definition: Material.cpp:51
PLATFORM_API void setTexture(const std::string &fileName, const TextureType type)
Sets the texture of the material for the specified texture type.
Definition: Material.cpp:66
PLATFORM_API Texture2DPtr getTexture(const TextureType type) const
Returns the texture of the material for the specified texture type.
Definition: Material.cpp:37
PLATFORM_API void clearTextures()
Clears all textures from the material object.
Definition: Material.cpp:45
TexturesMap _textures
Definition: Material.h:264
PLATFORM_API Material(const PropertyMap &properties={})
Constructs a Material object.
Definition: Material.cpp:31
void setCurrentType(const std::string &type)
std::string _currentType
std::map< std::string, PropertyMap > _properties
std::shared_ptr< Texture2D > Texture2DPtr
Definition: Types.h:159
TextureType
Definition: Types.h:244
#define CORE_DEBUG(__msg)
Definition: Logs.h:38