Radium Engine  1.5.20
Loading...
Searching...
No Matches
TextureManager.cpp
1#include <Engine/Data/Texture.hpp>
2#include <Engine/Data/TextureManager.hpp>
3
4#include <Core/Utils/Log.hpp>
5
6#include <globjects/AbstractUniform.h>
7#include <stb/stb_image.h>
8
9#include <memory>
10
11namespace Ra {
12namespace Engine {
13namespace Data {
14
15using namespace Core::Utils; // log
16
17TextureManager::TextureManager() = default;
18
19TextureManager::~TextureManager() = default;
20
21TextureManager::TextureHandle TextureManager::addTexture( const TextureParameters& parameters ) {
22
23 auto texture = std::make_unique<Texture>( parameters );
24 TextureHandle handle;
25 // find first free slot in m_textures, e.g. where the stored unique_ptr is nullptr
26 auto it = std::find_if(
27 m_textures.begin(), m_textures.end(), []( const auto& texture_ ) { return !texture_; } );
28 if ( it != m_textures.end() ) {
29 it->swap( texture );
30 handle.setValue( std::distance( m_textures.begin(), it ) );
31 }
32 else {
33 // if no free slot, push back a new texture
34 m_textures.push_back( std::move( texture ) );
35 handle.setValue( m_textures.size() - 1 );
36 }
37 m_textures[handle.getValue()]->initialize();
38 return handle;
39}
40
42 stbi_set_flip_vertically_on_load( true );
43 int n;
44 ImageParameters image;
45 int width;
46 int height;
47 unsigned char* data = stbi_load( filename.c_str(), &width, &height, &n, 0 );
48 image.width = width;
49 image.height = height;
50 if ( !data ) {
51 LOG( logERROR ) << "Something went wrong when loading image \"" << filename << "\".";
52 image.width = image.height = 0;
53 return image;
54 }
55
56 switch ( n ) {
57 case 1: {
58 image.format = GL_RED;
59 image.internalFormat = GL_R8;
60 } break;
61
62 case 2: {
63 // suppose it is GL_LUMINANCE_ALPHA
64 image.format = GL_RG;
65 image.internalFormat = GL_RG8;
66 } break;
67
68 case 3: {
69 image.format = GL_RGB;
70 image.internalFormat = GL_RGB8;
71 } break;
72
73 case 4: {
74 image.format = GL_RGBA;
75 image.internalFormat = GL_RGBA8;
76 } break;
77 default: {
78 image.format = GL_RGBA;
79 image.internalFormat = GL_RGBA8;
80 } break;
81 }
82
83 CORE_ASSERT( data, "Data is null" );
84 // make a shared ptr, with deleter from stb
85 image.texels = std::shared_ptr<void>( data, stbi_image_free );
86 image.type = GL_UNSIGNED_BYTE;
87 if ( linearize ) Texture::linearize( image );
88 return image;
89}
90
91Texture* TextureManager::getTexture( const TextureHandle& handle ) {
92 return handle.isValid() ? m_textures[handle.getValue()].get() : nullptr;
93}
94
95TextureManager::TextureHandle TextureManager::getTextureHandle( const std::string& name ) {
96 auto it = std::find_if( m_textures.begin(), m_textures.end(), [name]( const auto& texture ) {
97 return texture->getName() == name;
98 } );
99
100 return it != m_textures.end() ? TextureHandle { std::distance( m_textures.begin(), it ) }
101 : TextureHandle::Invalid();
102}
103
104void TextureManager::deleteTexture( const TextureHandle& handle ) {
105 if ( handle.isValid() ) m_textures[handle.getValue()].reset( nullptr );
106}
107
108} // namespace Data
109} // namespace Engine
110} // namespace Ra
T c_str(T... args)
TextureHandle getTextureHandle(const std::string &name)
Get a texture handle from textue name.
void deleteTexture(const TextureHandle &handle)
Remove a managed texture identified by handle.
static ImageParameters loadTextureImage(const std::string &filename, bool linearize=false)
Load filename and fill ImageParameters according to filename content.
Texture * getTexture(const TextureHandle &handle)
Get raw texture ptr from handle.
TextureHandle addTexture(const TextureParameters &p)
Add a texture given its TextureParameters.
Represent a Texture of the engine.
Definition Texture.hpp:120
static void linearize(ImageParameters &image)
Convert a color texture from sRGB to Linear RGB spaces.
Definition Texture.cpp:143
T distance(T... args)
T find_if(T... args)
T move(T... args)
hepler function to manage enum as underlying types in VariableSet
Definition Cage.cpp:3
GLenum internalFormat
OpenGL internal format (WARNING, for Integer textures, must be GL_XXX_INTEGER)
Definition Texture.hpp:77
Describes the sampler and image of a texture.
Definition Texture.hpp:99