chore: remove magic string values in favour of enums in Material info

This commit is contained in:
Almamu 2025-09-02 00:28:34 +02:00
parent b66610cc26
commit bf74a94420
7 changed files with 151 additions and 44 deletions

View File

@ -33,7 +33,7 @@ class DynamicValue {
explicit DynamicValue(float value); explicit DynamicValue(float value);
explicit DynamicValue(int value); explicit DynamicValue(int value);
explicit DynamicValue(bool value); explicit DynamicValue(bool value);
~DynamicValue (); virtual ~DynamicValue ();
[[nodiscard]] const glm::ivec4& getIVec4 () const; [[nodiscard]] const glm::ivec4& getIVec4 () const;
[[nodiscard]] const glm::ivec3& getIVec3 () const; [[nodiscard]] const glm::ivec3& getIVec3 () const;

View File

@ -8,16 +8,42 @@
#include "Types.h" #include "Types.h"
namespace WallpaperEngine::Data::Model { namespace WallpaperEngine::Data::Model {
enum BlendingMode {
BlendingMode_Unknown = 0,
BlendingMode_Normal = 1,
BlendingMode_Translucent = 2,
BlendingMode_Additive = 3,
};
enum CullingMode {
CullingMode_Unknown = 0,
CullingMode_Normal = 1,
CullingMode_Disable = 2
};
enum DepthtestMode {
DepthtestMode_Unknown = 0,
DepthtestMode_Disabled = 1,
DepthtestMode_Enabled = 2,
};
enum DepthwriteMode {
DepthwriteMode_Unknown = 0,
DepthwriteMode_Disabled = 1,
DepthwriteMode_Enabled = 2,
};
struct MaterialPass { struct MaterialPass {
// TODO: WRITE ENUMS FOR THESE // TODO: WRITE ENUMS FOR THESE
/** Blending mode */ /** Blending mode */
std::string blending; BlendingMode blending;
/** Culling mode */ /** Culling mode */
std::string cullmode; CullingMode cullmode;
/** Depth test mode */ /** Depth test mode */
std::string depthtest; DepthtestMode depthtest;
/** Depth write mode */ /** Depth write mode */
std::string depthwrite; DepthwriteMode depthwrite;
/** Shader file to use for this pass */ /** Shader file to use for this pass */
std::string shader; std::string shader;
/** List of textures defined for this pass */ /** List of textures defined for this pass */

View File

@ -41,10 +41,10 @@ MaterialPassUniquePtr MaterialParser::parsePass (const JSON& it, Project& projec
return std::make_unique <MaterialPass>(MaterialPass { return std::make_unique <MaterialPass>(MaterialPass {
//TODO: REMOVE THIS UGLY STD::STRING CREATION //TODO: REMOVE THIS UGLY STD::STRING CREATION
.blending = it.optional ("blending", std::string ("normal")), .blending = parseBlendMode (it.optional ("blending", std::string ("normal"))),
.cullmode = it.optional ("cullmode", std::string ("disabled")), .cullmode = parseCullMode (it.optional ("cullmode", std::string ("nocull"))),
.depthtest = it.optional ("depthtest", std::string ("disabled")), .depthtest = parseDepthtestMode (it.optional ("depthtest", std::string ("disabled"))),
.depthwrite = it.optional ("depthwrite", std::string ("disabled")), .depthwrite = parseDepthwriteMode (it.optional ("depthwrite", std::string ("disabled"))),
.shader = it.require <std::string> ("shader", "Material pass must have a shader"), .shader = it.require <std::string> ("shader", "Material pass must have a shader"),
.textures = textures.has_value () ? parseTextures (*textures) : TextureMap {}, .textures = textures.has_value () ? parseTextures (*textures) : TextureMap {},
.usertextures = usertextures.has_value () ? parseTextures (*usertextures) : TextureMap {}, .usertextures = usertextures.has_value () ? parseTextures (*usertextures) : TextureMap {},
@ -90,3 +90,59 @@ std::map <std::string, int> MaterialParser::parseCombos (const JSON& it) {
return result; return result;
} }
BlendingMode MaterialParser::parseBlendMode (const std::string& mode) {
if (mode == "normal") {
return BlendingMode_Normal;
}
if (mode == "additive") {
return BlendingMode_Additive;
}
if (mode == "translucent") {
return BlendingMode_Translucent;
}
sLog.error ("Unknown blending mode: ", mode, " defaulting to normal");
return BlendingMode_Normal;
}
CullingMode MaterialParser::parseCullMode (const std::string& mode) {
if (mode == "nocull") {
return CullingMode_Disable;
}
if (mode == "normal") {
return CullingMode_Normal;
}
sLog.error ("Unknown culling mode: ", mode, " defaulting to nocull");
return CullingMode_Disable;
}
DepthtestMode MaterialParser::parseDepthtestMode (const std::string& mode) {
if (mode == "disabled") {
return DepthtestMode_Disabled;
}
if (mode == "enabled") {
return DepthtestMode_Enabled;
}
sLog.error ("Unknown depthtest mode: ", mode, " defaulting to disabled");
return DepthtestMode_Disabled;
}
DepthwriteMode MaterialParser::parseDepthwriteMode (const std::string& mode) {
if (mode == "disabled") {
return DepthwriteMode_Disabled;
}
if (mode == "enabled") {
return DepthwriteMode_Enabled;
}
sLog.error ("Unknown depthwrite mode: ", mode, " defaulting to disabled");
return DepthwriteMode_Disabled;
}

View File

@ -1,6 +1,7 @@
#pragma once #pragma once
#include "WallpaperEngine/Data/JSON.h" #include "WallpaperEngine/Data/JSON.h"
#include "WallpaperEngine/Data/Model/Material.h"
#include "WallpaperEngine/Data/Model/Types.h" #include "WallpaperEngine/Data/Model/Types.h"
namespace WallpaperEngine::Data::Parsers { namespace WallpaperEngine::Data::Parsers {
@ -15,5 +16,9 @@ class MaterialParser {
static MaterialPassUniquePtr parsePass (const JSON& it, Project& project); static MaterialPassUniquePtr parsePass (const JSON& it, Project& project);
static std::map <int, std::string> parseTextures (const JSON& it); static std::map <int, std::string> parseTextures (const JSON& it);
static std::map <std::string, int> parseCombos (const JSON& it); static std::map <std::string, int> parseCombos (const JSON& it);
static BlendingMode parseBlendMode (const std::string& mode);
static CullingMode parseCullMode (const std::string& mode);
static DepthtestMode parseDepthtestMode (const std::string& mode);
static DepthwriteMode parseDepthwriteMode (const std::string& mode);
}; };
} // namespace WallpaperEngine::Data::Parsers } // namespace WallpaperEngine::Data::Parsers

View File

@ -294,10 +294,10 @@ void CImage::setup () {
} }
const auto virtualPass = MaterialPass { const auto virtualPass = MaterialPass {
.blending = "normal", .blending = BlendingMode_Normal,
.cullmode = "nocull", .cullmode = CullingMode_Disable,
.depthtest = "disabled", .depthtest = DepthtestMode_Disabled,
.depthwrite = "disabled", .depthwrite = DepthwriteMode_Disabled,
.shader = "commands/copy", .shader = "commands/copy",
.textures = { .textures = {
{0, *(*curEffect)->source} {0, *(*curEffect)->source}
@ -363,7 +363,7 @@ void CImage::setup () {
const auto last = this->m_passes.rbegin (); const auto last = this->m_passes.rbegin ();
(*last)->setBlendingMode ((*first)->getBlendingMode ()); (*last)->setBlendingMode ((*first)->getBlendingMode ());
(*first)->setBlendingMode ("normal"); (*first)->setBlendingMode (BlendingMode_Normal);
} }
// calculate full animation time (if any) // calculate full animation time (if any)

View File

@ -89,36 +89,55 @@ void CPass::setupRenderFramebuffer () {
glViewport (0, 0, this->m_drawTo->getRealWidth (), this->m_drawTo->getRealHeight ()); glViewport (0, 0, this->m_drawTo->getRealWidth (), this->m_drawTo->getRealHeight ());
// set texture blending // set texture blending
if (this->getBlendingMode () == "translucent") { switch (this->getBlendingMode ()) {
case BlendingMode_Translucent:
glEnable (GL_BLEND); glEnable (GL_BLEND);
glBlendFuncSeparate (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glBlendFuncSeparate (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
} else if (this->getBlendingMode () == "additive") { break;
case BlendingMode_Additive:
glEnable (GL_BLEND); glEnable (GL_BLEND);
glBlendFuncSeparate (GL_SRC_ALPHA, GL_ONE, GL_SRC_ALPHA, GL_ONE); glBlendFuncSeparate (GL_SRC_ALPHA, GL_ONE, GL_SRC_ALPHA, GL_ONE);
} else if (this->getBlendingMode () == "normal") { break;
case BlendingMode_Normal:
glEnable (GL_BLEND); glEnable (GL_BLEND);
glBlendFuncSeparate (GL_ONE, GL_ZERO, GL_ONE, GL_ZERO); glBlendFuncSeparate (GL_ONE, GL_ZERO, GL_ONE, GL_ZERO);
} else { break;
default:
glDisable (GL_BLEND); glDisable (GL_BLEND);
break;
} }
// set depth testing switch (this->m_pass.depthtest) {
if (this->m_pass.depthtest == "disabled") { case DepthtestMode_Enabled:
glDisable (GL_DEPTH_TEST);
} else {
glEnable (GL_DEPTH_TEST); glEnable (GL_DEPTH_TEST);
break;
case DepthtestMode_Disabled:
default:
glDisable (GL_DEPTH_TEST);
break;
} }
if (this->m_pass.cullmode == "nocull") { switch (this->m_pass.cullmode) {
glDisable (GL_CULL_FACE); case CullingMode_Normal:
} else {
glEnable (GL_CULL_FACE); glEnable (GL_CULL_FACE);
break;
case CullingMode_Disable:
default:
glDisable (GL_CULL_FACE);
break;
} }
if (this->m_pass.depthwrite == "disabled") { switch (this->m_pass.depthwrite) {
glDepthMask (false); case DepthwriteMode_Enabled:
} else {
glDepthMask (true); glDepthMask (true);
break;
case DepthwriteMode_Disabled:
default:
glDepthMask (false);
break;
} }
} }
@ -322,11 +341,11 @@ void CPass::setViewProjectionMatrix (const glm::mat4* viewProjection) {
this->m_viewProjectionMatrix = viewProjection; this->m_viewProjectionMatrix = viewProjection;
} }
void CPass::setBlendingMode (std::string blendingmode) { void CPass::setBlendingMode (BlendingMode blendingmode) {
this->m_blendingmode = std::move(blendingmode); this->m_blendingmode = blendingmode;
} }
const std::string& CPass::getBlendingMode () const { BlendingMode CPass::getBlendingMode () const {
return this->m_blendingmode; return this->m_blendingmode;
} }

View File

@ -3,6 +3,7 @@
#include <glm/gtc/type_ptr.hpp> #include <glm/gtc/type_ptr.hpp>
#include <utility> #include <utility>
#include "WallpaperEngine/Data/Model/Material.h"
#include "WallpaperEngine/Assets/ITexture.h" #include "WallpaperEngine/Assets/ITexture.h"
#include "WallpaperEngine/Render/CFBO.h" #include "WallpaperEngine/Render/CFBO.h"
#include "WallpaperEngine/Render/CFBOProvider.h" #include "WallpaperEngine/Render/CFBOProvider.h"
@ -37,8 +38,8 @@ class CPass final : public Helpers::CContextAware {
void setModelViewProjectionMatrixInverse (const glm::mat4* projection); void setModelViewProjectionMatrixInverse (const glm::mat4* projection);
void setModelMatrix (const glm::mat4* model); void setModelMatrix (const glm::mat4* model);
void setViewProjectionMatrix (const glm::mat4* viewProjection); void setViewProjectionMatrix (const glm::mat4* viewProjection);
void setBlendingMode (std::string blendingmode); void setBlendingMode (BlendingMode blendingmode);
[[nodiscard]] const std::string& getBlendingMode () const; [[nodiscard]] BlendingMode getBlendingMode () const;
[[nodiscard]] std::shared_ptr<const CFBO> resolveFBO (const std::string& name) const; [[nodiscard]] std::shared_ptr<const CFBO> resolveFBO (const std::string& name) const;
[[nodiscard]] std::shared_ptr<const CFBOProvider> getFBOProvider () const; [[nodiscard]] std::shared_ptr<const CFBOProvider> getFBOProvider () const;
@ -163,7 +164,7 @@ class CPass final : public Helpers::CContextAware {
std::vector<AttribEntry*> m_attribs = {}; std::vector<AttribEntry*> m_attribs = {};
std::map<std::string, UniformEntry*> m_uniforms = {}; std::map<std::string, UniformEntry*> m_uniforms = {};
std::map<std::string, ReferenceUniformEntry*> m_referenceUniforms = {}; std::map<std::string, ReferenceUniformEntry*> m_referenceUniforms = {};
std::string m_blendingmode = ""; BlendingMode m_blendingmode = BlendingMode_Normal;
const glm::mat4* m_modelViewProjectionMatrix; const glm::mat4* m_modelViewProjectionMatrix;
const glm::mat4* m_modelViewProjectionMatrixInverse; const glm::mat4* m_modelViewProjectionMatrixInverse;
const glm::mat4* m_modelMatrix; const glm::mat4* m_modelMatrix;