From b90d4b2d8ac00141336bc9d7bed2db4333a73cc8 Mon Sep 17 00:00:00 2001 From: Benau Date: Sat, 17 Apr 2021 13:54:50 +0800 Subject: [PATCH] Add image manipulator for srgb texture --- src/graphics/material.cpp | 28 ++++++++++++++++++++++++---- src/graphics/material.hpp | 5 +++-- src/graphics/stk_tex_manager.cpp | 6 ++++-- src/graphics/stk_tex_manager.hpp | 4 +++- 4 files changed, 34 insertions(+), 9 deletions(-) diff --git a/src/graphics/material.cpp b/src/graphics/material.cpp index df75f9726..566408112 100644 --- a/src/graphics/material.cpp +++ b/src/graphics/material.cpp @@ -31,6 +31,7 @@ #include "graphics/central_settings.hpp" #include "graphics/irr_driver.hpp" #include "graphics/particle_kind_manager.hpp" +#include "graphics/sp/sp_base.hpp" #include "graphics/stk_tex_manager.hpp" #include "io/file_manager.hpp" #include "io/xml_node.hpp" @@ -362,10 +363,28 @@ Material::Material(const XMLNode *node, bool deprecated) //----------------------------------------------------------------------------- video::ITexture* Material::getTexture(bool srgb, bool premul_alpha) { - if (!m_installed) + std::function image_mani; +#ifndef SERVER_ONLY + if (srgb) { - install(srgb, premul_alpha); + image_mani = [](video::IImage* img)->void + { + if (!CVS->isDeferredEnabled() || !CVS->isGLSL()) + return; + uint8_t* data = (uint8_t*)img->lock(); + for (unsigned int i = 0; i < img->getDimension().Width * + img->getDimension().Height; i++) + { + data[i * 4] = SP::srgb255ToLinear(data[i * 4]); + data[i * 4 + 1] = SP::srgb255ToLinear(data[i * 4 + 1]); + data[i * 4 + 2] = SP::srgb255ToLinear(data[i * 4 + 2]); + } + img->unlock(); + }; } +#endif + if (!m_installed) + install(image_mani); return m_texture; } // getTexture @@ -482,7 +501,7 @@ void Material::init() } // init //----------------------------------------------------------------------------- -void Material::install(bool srgb, bool premul_alpha) +void Material::install(std::function image_mani) { // Don't load a texture that are not supposed to be loaded automatically if (m_installed) return; @@ -500,7 +519,8 @@ void Material::install(bool srgb, bool premul_alpha) } else { - m_texture = STKTexManager::getInstance()->getTexture(m_sampler_path[0]); + m_texture = STKTexManager::getInstance()->getTexture(m_sampler_path[0], + image_mani); } if (m_texture == NULL) return; diff --git a/src/graphics/material.hpp b/src/graphics/material.hpp index 1f6a875a1..e5ab1e376 100644 --- a/src/graphics/material.hpp +++ b/src/graphics/material.hpp @@ -25,13 +25,14 @@ #include #include +#include #include #include #include namespace irr { - namespace video { class ITexture; class SMaterial; } + namespace video { class ITexture; class IImage; class SMaterial; } namespace scene { class ISceneNode; class IMeshBuffer; } } using namespace irr; @@ -201,7 +202,7 @@ private: std::string m_colorization_mask; void init (); - void install (bool srgb = false, bool premul_alpha = false); + void install (std::function image_mani = nullptr); void initCustomSFX(const XMLNode *sfx); void initParticlesEffect(const XMLNode *node); diff --git a/src/graphics/stk_tex_manager.cpp b/src/graphics/stk_tex_manager.cpp index 300c1c0bd..a37091541 100644 --- a/src/graphics/stk_tex_manager.cpp +++ b/src/graphics/stk_tex_manager.cpp @@ -62,7 +62,8 @@ video::ITexture* STKTexManager::findTextureInFileSystem(const std::string& filen } // findTextureInFileSystem // ---------------------------------------------------------------------------- -video::ITexture* STKTexManager::getTexture(const std::string& path) +video::ITexture* STKTexManager::getTexture(const std::string& path, + std::function image_mani) { if (path.empty()) { @@ -97,7 +98,8 @@ video::ITexture* STKTexManager::getTexture(const std::string& path) else { new_texture = - GE::createTexture(full_path.empty() ? path : full_path); + GE::createTexture(full_path.empty() ? path : full_path, + image_mani); } if (new_texture->getTextureHandler() == 0) { diff --git a/src/graphics/stk_tex_manager.hpp b/src/graphics/stk_tex_manager.hpp index 8c50eef29..f63f7d3e7 100644 --- a/src/graphics/stk_tex_manager.hpp +++ b/src/graphics/stk_tex_manager.hpp @@ -26,6 +26,7 @@ #include "ITexture.h" #include +#include #include #include @@ -74,7 +75,8 @@ public: // ------------------------------------------------------------------------ ~STKTexManager(); // ------------------------------------------------------------------------ - irr::video::ITexture* getTexture(const std::string& path); + irr::video::ITexture* getTexture(const std::string& path, + std::function image_mani = nullptr); // ------------------------------------------------------------------------ irr::video::ITexture* addTexture(irr::video::ITexture* texture); // ------------------------------------------------------------------------