55#include " Renderer/MilkdropNoise.hpp"
66#include " Renderer/Texture.hpp"
77
8+ #include < Logging.hpp>
89#include < Utils.hpp>
910
10- #include < Logging.hpp>
11- #include < SOIL2/SOIL2.h>
11+ #include < SOIL2/stb_image.h>
1212
1313#include < algorithm>
14+ #include < cstdlib>
1415#include < memory>
1516#include < random>
1617#include < vector>
@@ -70,24 +71,27 @@ void TextureManager::Preload()
7071
7172 int width{};
7273 int height{};
74+ int channels{};
7375
74- unsigned int tex = SOIL_load_OGL_texture_from_memory (
75- M_data,
76- M_bytes,
77- SOIL_LOAD_AUTO,
78- SOIL_CREATE_NEW_ID,
79- SOIL_FLAG_POWER_OF_TWO | SOIL_FLAG_MULTIPLY_ALPHA);
76+ {
77+ std::unique_ptr<stbi_uc, decltype (&free)> const imageData (stbi_load_from_memory (M_data, M_bytes, &width, &height, &channels, 0 ), free);
8078
81- m_textures[" idlem" ] = std::make_shared<Texture>(" idlem" , tex, GL_TEXTURE_2D, width, height, false );;
79+ if (imageData.get () != nullptr )
80+ {
81+ auto format = TextureFormatFromChannels (channels);
82+ m_textures[" idlem" ] = std::make_shared<Texture>(" idlem" , reinterpret_cast <const void *>(imageData.get ()), GL_TEXTURE_2D, width, height, 0 , format, format, GL_UNSIGNED_BYTE, false );
83+ }
84+ }
8285
83- tex = SOIL_load_OGL_texture_from_memory (
84- headphones_data,
85- headphones_bytes,
86- SOIL_LOAD_AUTO,
87- SOIL_CREATE_NEW_ID,
88- SOIL_FLAG_POWER_OF_TWO | SOIL_FLAG_MULTIPLY_ALPHA);
86+ {
87+ std::unique_ptr<stbi_uc, decltype (&free)> const imageData (stbi_load_from_memory (headphones_data, headphones_bytes, &width, &height, &channels, 0 ), free);
8988
90- m_textures[" idleheadphones" ] = std::make_shared<Texture>(" idleheadphones" , tex, GL_TEXTURE_2D, width, height, false );;
89+ if (imageData.get () != nullptr )
90+ {
91+ auto format = TextureFormatFromChannels (channels);
92+ m_textures[" idleheadphones" ] = std::make_shared<Texture>(" idleheadphones" , reinterpret_cast <const void *>(imageData.get ()), GL_TEXTURE_2D, width, height, 0 , format, format, GL_UNSIGNED_BYTE, false );
93+ }
94+ }
9195
9296 // Noise textures
9397 m_textures[" noise_lq_lite" ] = MilkdropNoise::LowQualityLite ();
@@ -184,7 +188,8 @@ auto TextureManager::TryLoadingTexture(const std::string& name) -> TextureSample
184188 if (texture)
185189 {
186190 LOG_DEBUG (" [TextureManager] Loaded texture \" " + unqualifiedName + " \" from file: " + file.filePath );
187- return {texture, m_samplers.at ({wrapMode, filterMode}), name, unqualifiedName};;
191+ return {texture, m_samplers.at ({wrapMode, filterMode}), name, unqualifiedName};
192+ ;
188193 }
189194 }
190195
@@ -196,8 +201,6 @@ auto TextureManager::TryLoadingTexture(const std::string& name) -> TextureSample
196201
197202auto TextureManager::LoadTexture (const ScannedFile& file) -> std::shared_ptr<Texture>
198203{
199- std::string unqualifiedName;
200-
201204 if (m_textures.find (file.lowerCaseBaseName ) != m_textures.end ())
202205 {
203206 return m_textures.at (file.lowerCaseBaseName );
@@ -207,30 +210,20 @@ auto TextureManager::LoadTexture(const ScannedFile& file) -> std::shared_ptr<Tex
207210 int height{};
208211 int channels{};
209212
210- std::unique_ptr<unsigned char , decltype (&SOIL_free_image_data )> imageData (SOIL_load_image (file.filePath .c_str (), &width, &height, &channels, SOIL_LOAD_RGBA ), SOIL_free_image_data );
213+ std::unique_ptr<stbi_uc , decltype (&free )> imageData (stbi_load (file.filePath .c_str (), &width, &height, &channels, 4 ), free );
211214
212- if (imageData == nullptr )
215+ if (imageData. get () == nullptr )
213216 {
214217 LOG_DEBUG (" [TextureManager] Failed to decode image data." );
215218 return {};
216219 }
217220
218- unsigned int const tex = SOIL_create_OGL_texture (
219- imageData.get (),
220- &width, &height, SOIL_LOAD_RGBA,
221- SOIL_CREATE_NEW_ID,
222- SOIL_FLAG_MULTIPLY_ALPHA);
221+ auto format = TextureFormatFromChannels (4 );
222+ auto newTexture = std::make_shared<Texture>(file.lowerCaseBaseName , reinterpret_cast <const void *>(imageData.get ()), GL_TEXTURE_2D, width, height, 0 , format, format, GL_UNSIGNED_BYTE, true );
223223
224224 imageData.reset ();
225225
226- if (tex == 0 )
227- {
228- LOG_DEBUG (" [TextureManager] Failed to create GPU texture from image data." );
229- return {};
230- }
231-
232- uint32_t memoryBytes = width * height * 4 ; // RGBA, unsigned byte color channels.
233- auto newTexture = std::make_shared<Texture>(unqualifiedName, tex, GL_TEXTURE_2D, width, height, true );
226+ uint32_t const memoryBytes = width * height * 4 ; // RGBA, unsigned byte color channels.
234227 m_textures[file.lowerCaseBaseName ] = newTexture;
235228 m_textureStats.insert ({file.lowerCaseBaseName , {memoryBytes}});
236229
@@ -369,5 +362,22 @@ void TextureManager::ScanTextures()
369362 }
370363}
371364
365+ uint32_t TextureManager::TextureFormatFromChannels (int channels)
366+ {
367+ switch (channels)
368+ {
369+ case 1 :
370+ return GL_RED;
371+ case 2 :
372+ return GL_RG;
373+ case 3 :
374+ return GL_RGB;
375+ case 4 :
376+ return GL_RGBA;
377+ default :
378+ return 0 ;
379+ }
380+ }
381+
372382} // namespace Renderer
373383} // namespace libprojectM
0 commit comments