1 #include <Core/Asset/BlinnPhongMaterialData.hpp>
2 #include <Engine/Data/BlinnPhongMaterial.hpp>
3 #include <Engine/Data/MaterialConverters.hpp>
4 #include <Engine/Data/ShaderConfigFactory.hpp>
5 #include <Engine/Data/ShaderProgramManager.hpp>
6 #include <Engine/Data/TextureManager.hpp>
7 #include <Engine/RadiumEngine.hpp>
8 #include <Engine/Rendering/RenderTechnique.hpp>
15 static const std::string materialName {
"BlinnPhong" };
17 nlohmann::json BlinnPhongMaterial::s_parametersMetadata = {};
26 void BlinnPhongMaterial::updateRenderingParameters() {
28 auto& renderParameters = getParameters();
29 renderParameters.addParameter(
"material.kd", m_kd );
30 renderParameters.addParameter(
"material.hasPerVertexKd", m_perVertexColor );
31 renderParameters.addParameter(
"material.renderAsSplat", m_renderAsSplat );
32 renderParameters.addParameter(
"material.ks", m_ks );
33 renderParameters.addParameter(
"material.ns", m_ns );
34 renderParameters.addParameter(
"material.alpha", std::min( m_alpha, m_kd[3] ) );
36 if ( tex !=
nullptr ) { renderParameters.addParameter(
"material.tex.kd", tex ); }
37 renderParameters.addParameter(
"material.tex.hasKd", tex !=
nullptr );
38 tex =
getTexture( BlinnPhongMaterial::TextureSemantic::TEX_SPECULAR );
39 if ( tex !=
nullptr ) { renderParameters.addParameter(
"material.tex.ks", tex ); }
40 renderParameters.addParameter(
"material.tex.hasKs", tex !=
nullptr );
41 tex =
getTexture( BlinnPhongMaterial::TextureSemantic::TEX_NORMAL );
42 if ( tex !=
nullptr ) { renderParameters.addParameter(
"material.tex.normal", tex ); }
43 renderParameters.addParameter(
"material.tex.hasNormal", tex !=
nullptr );
44 tex =
getTexture( BlinnPhongMaterial::TextureSemantic::TEX_SHININESS );
45 if ( tex !=
nullptr ) { renderParameters.addParameter(
"material.tex.ns", tex ); }
46 renderParameters.addParameter(
"material.tex.hasNs", tex !=
nullptr );
47 tex =
getTexture( BlinnPhongMaterial::TextureSemantic::TEX_ALPHA );
48 if ( tex !=
nullptr ) { renderParameters.addParameter(
"material.tex.alpha", tex ); }
49 renderParameters.addParameter(
"material.tex.hasAlpha", tex !=
nullptr );
56 auto texManager = RadiumEngine::getInstance()->getTextureManager();
57 for (
const auto& tex : m_pendingTextures ) {
59 bool tolinear = ( tex.first == TextureSemantic::TEX_DIFFUSE ||
60 tex.first == TextureSemantic::TEX_SPECULAR );
61 auto texture = texManager->getOrLoadTexture( tex.second, tolinear );
62 m_textures[tex.first] = texture;
67 m_pendingTextures.clear();
70 updateRenderingParameters();
74 auto& renderParameters = getParameters();
76 m_perVertexColor = renderParameters.getParameter<
bool>(
"material.hasPerVertexKd" );
77 m_renderAsSplat = renderParameters.getParameter<
bool>(
"material.renderAsSplat" );
79 m_ns = renderParameters.getParameter<Scalar>(
"material.ns" );
80 m_alpha = renderParameters.getParameter<Scalar>(
"material.alpha" );
89 auto resourcesRootDir { RadiumEngine::getInstance()->getResourcesDir() };
90 auto shaderProgramManager = RadiumEngine::getInstance()->getShaderProgramManager();
97 shaderProgramManager->addNamedString(
98 "/BlinnPhong.glsl", resourcesRootDir +
"Shaders/Materials/BlinnPhong/BlinnPhong.glsl" );
102 resourcesRootDir +
"Shaders/Materials/BlinnPhong/BlinnPhong.vert.glsl",
103 resourcesRootDir +
"Shaders/Materials/BlinnPhong/BlinnPhong.frag.glsl" );
107 "ZprepassBlinnPhong",
108 resourcesRootDir +
"Shaders/Materials/BlinnPhong/BlinnPhong.vert.glsl",
109 resourcesRootDir +
"Shaders/Materials/BlinnPhong/BlinnPhongZPrepass.frag.glsl" );
114 resourcesRootDir +
"Shaders/Materials/BlinnPhong/BlinnPhong.vert.glsl",
115 resourcesRootDir +
"Shaders/Materials/BlinnPhong/LitOITBlinnPhong.frag.glsl" );
119 Rendering::EngineRenderTechniques::registerDefaultTechnique(
126 rt.
setConfiguration( *lightpass, Rendering::DefaultRenderingPasses::LIGHTING_OPAQUE );
131 rt.
setConfiguration( *zprepass, Rendering::DefaultRenderingPasses::Z_PREPASS );
134 auto transparentpass =
137 Rendering::DefaultRenderingPasses::LIGHTING_TRANSPARENT );
147 Rendering::EngineRenderTechniques::removeDefaultTechnique( materialName );
155 auto source =
static_cast<const Ra::Core::Asset::BlinnPhongMaterialData*
>( toconvert );
157 if ( source->hasDiffuse() ) result->m_kd = source->m_diffuse;
158 if ( source->hasSpecular() ) result->m_ks = source->m_specular;
159 if ( source->hasShininess() ) result->m_ns = source->m_shininess;
160 if ( source->hasOpacity() ) result->m_alpha = source->m_opacity;
161 if ( source->hasDiffuseTexture() )
162 result->addTexture( BlinnPhongMaterial::TextureSemantic::TEX_DIFFUSE,
163 source->m_texDiffuse );
164 if ( source->hasSpecularTexture() )
165 result->addTexture( BlinnPhongMaterial::TextureSemantic::TEX_SPECULAR,
166 source->m_texSpecular );
167 if ( source->hasShininessTexture() )
168 result->addTexture( BlinnPhongMaterial::TextureSemantic::TEX_SHININESS,
169 source->m_texShininess );
170 if ( source->hasOpacityTexture() )
171 result->addTexture( BlinnPhongMaterial::TextureSemantic::TEX_ALPHA, source->m_texOpacity );
172 if ( source->hasNormalTexture() )
173 result->addTexture( BlinnPhongMaterial::TextureSemantic::TEX_NORMAL, source->m_texNormal );
virtual const std::string & getName() const
Acces to the name of the asset.
represent material data loaded by a file loader. Material data must be identified by a unique name....
~BlinnPhongMaterial() override
static void registerMaterial()
EIGEN_MAKE_ALIGNED_OPERATOR_NEW BlinnPhongMaterial(const std::string &instanceName)
bool isTransparent() const override
Texture * getTexture(const TextureSemantic &semantic) const
void updateFromParameters() override
Update the attributes of the ShaderParameterProvider to their actual values stored in the renderParam...
static void unregisterMaterial()
void updateGL() override
Update the OpenGL states used by the ShaderParameterProvider. These state could be the ones from an a...
virtual bool isTransparent() const
static void loadMetaData(const std::string &basename, nlohmann::json &destination)
Load the ParameterSet description.
void setConfiguration(const Data::ShaderConfiguration &newConfig, Core::Utils::Index pass=DefaultRenderingPasses::LIGHTING_OPAQUE)
bool removeMaterialConverter(const std::string &name)
bool registerMaterialConverter(const std::string &name, ConverterFunction converter)
void addConfiguration(const ShaderConfiguration &config)
Core::Utils::optional< ShaderConfiguration > getConfiguration(const std::string &name)