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,41 +201,28 @@ 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 );
204207 }
205208
206209 int width{};
207210 int height{};
208- int channels{};
209211
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 );
212+ std::unique_ptr<stbi_uc , decltype (&free )> imageData (stbi_load (file.filePath .c_str (), &width, &height, nullptr , 4 ), free );
211213
212- if (imageData == nullptr )
214+ if (imageData. get () == nullptr )
213215 {
214216 LOG_DEBUG (" [TextureManager] Failed to decode image data." );
215217 return {};
216218 }
217219
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);
220+ auto format = TextureFormatFromChannels (4 );
221+ 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 );
223222
224223 imageData.reset ();
225224
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 );
225+ uint32_t const memoryBytes = width * height * 4 ; // RGBA, unsigned byte color channels.
234226 m_textures[file.lowerCaseBaseName ] = newTexture;
235227 m_textureStats.insert ({file.lowerCaseBaseName , {memoryBytes}});
236228
@@ -369,5 +361,22 @@ void TextureManager::ScanTextures()
369361 }
370362}
371363
364+ uint32_t TextureManager::TextureFormatFromChannels (int channels)
365+ {
366+ switch (channels)
367+ {
368+ case 1 :
369+ return GL_RED;
370+ case 2 :
371+ return GL_RG;
372+ case 3 :
373+ return GL_RGB;
374+ case 4 :
375+ return GL_RGBA;
376+ default :
377+ return 0 ;
378+ }
379+ }
380+
372381} // namespace Renderer
373382} // namespace libprojectM
0 commit comments