Radium Engine  1.5.20
Loading...
Searching...
No Matches
BlinnPhongMaterial.cpp
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>
9
10#include <fstream>
11
12namespace Ra {
13namespace Engine {
14namespace Data {
15static const std::string materialName { "BlinnPhong" };
16
17nlohmann::json BlinnPhongMaterial::s_parametersMetadata = {};
18
20 Material( instanceName, materialName, Material::MaterialAspect::MAT_OPAQUE ) {}
21
22void BlinnPhongMaterial::updateRenderingParameters() {
23 // update the rendering parameters
24 auto& renderParameters = getParameters();
25 renderParameters.setVariable( "material.kd", m_kd );
26 renderParameters.setVariable( "material.hasPerVertexKd", m_perVertexColor );
27 renderParameters.setVariable( "material.renderAsSplat", m_renderAsSplat );
28 renderParameters.setVariable( "material.ks", m_ks );
29 renderParameters.setVariable( "material.ns", m_ns );
30 renderParameters.setVariable( "material.alpha", std::min( m_alpha, m_kd[3] ) );
31 Texture* tex = getTexture( BlinnPhongMaterial::TextureSemantic::TEX_DIFFUSE );
32 if ( tex != nullptr ) { renderParameters.setTexture( "material.tex.kd", tex ); }
33 renderParameters.setVariable( "material.tex.hasKd", tex != nullptr );
34 tex = getTexture( BlinnPhongMaterial::TextureSemantic::TEX_SPECULAR );
35 if ( tex != nullptr ) { renderParameters.setTexture( "material.tex.ks", tex ); }
36 renderParameters.setVariable( "material.tex.hasKs", tex != nullptr );
37 tex = getTexture( BlinnPhongMaterial::TextureSemantic::TEX_NORMAL );
38 if ( tex != nullptr ) { renderParameters.setTexture( "material.tex.normal", tex ); }
39 renderParameters.setVariable( "material.tex.hasNormal", tex != nullptr );
40 tex = getTexture( BlinnPhongMaterial::TextureSemantic::TEX_SHININESS );
41 if ( tex != nullptr ) { renderParameters.setTexture( "material.tex.ns", tex ); }
42 renderParameters.setVariable( "material.tex.hasNs", tex != nullptr );
43 tex = getTexture( BlinnPhongMaterial::TextureSemantic::TEX_ALPHA );
44 if ( tex != nullptr ) { renderParameters.setTexture( "material.tex.alpha", tex ); }
45 renderParameters.setVariable( "material.tex.hasAlpha", tex != nullptr );
46}
47
49 if ( !isDirty() ) { return; }
50
51 updateRenderingParameters();
52 setClean();
53}
54
56 auto& renderParameters = getParameters();
57 m_kd = renderParameters.getVariable<Core::Utils::Color>( "material.kd" );
58 m_perVertexColor = renderParameters.getVariable<bool>( "material.hasPerVertexKd" );
59 m_renderAsSplat = renderParameters.getVariable<bool>( "material.renderAsSplat" );
60 m_ks = renderParameters.getVariable<Core::Utils::Color>( "material.ks" );
61 m_ns = renderParameters.getVariable<Scalar>( "material.ns" );
62 m_alpha = renderParameters.getVariable<Scalar>( "material.alpha" );
63}
64
66 return ( m_alpha < 1_ra ) || ( m_kd[3] < 1_ra ) || Material::isTransparent();
67}
68
70 // For resources access (glsl files) in a filesystem
71 auto resourcesRootDir { RadiumEngine::getInstance()->getResourcesDir() };
72 auto shaderProgramManager = RadiumEngine::getInstance()->getShaderProgramManager();
73
74 // Defining the material converter
77
78 // adding the material glsl implementation file
79 shaderProgramManager->addNamedString(
80 "/BlinnPhong.glsl", resourcesRootDir + "Shaders/Materials/BlinnPhong/BlinnPhong.glsl" );
81 // registering re-usable shaders
83 "BlinnPhong",
84 resourcesRootDir + "Shaders/Materials/BlinnPhong/BlinnPhong.vert.glsl",
85 resourcesRootDir + "Shaders/Materials/BlinnPhong/BlinnPhong.frag.glsl" );
87
88 Data::ShaderConfiguration zprepassconfig(
89 "ZprepassBlinnPhong",
90 resourcesRootDir + "Shaders/Materials/BlinnPhong/BlinnPhong.vert.glsl",
91 resourcesRootDir + "Shaders/Materials/BlinnPhong/BlinnPhongZPrepass.frag.glsl" );
93
94 Data::ShaderConfiguration transparentpassconfig(
95 "LitOITBlinnPhong",
96 resourcesRootDir + "Shaders/Materials/BlinnPhong/BlinnPhong.vert.glsl",
97 resourcesRootDir + "Shaders/Materials/BlinnPhong/LitOITBlinnPhong.frag.glsl" );
99
100 // Registering technique
101 Rendering::EngineRenderTechniques::registerDefaultTechnique(
102 materialName,
103
105 // Configure the technique to render this object using forward Renderer or any
106 // compatible one Main pass (Mandatory) : BlinnPhong
107 auto lightpass = Data::ShaderConfigurationFactory::getConfiguration( "BlinnPhong" );
109
110 // Z prepass (Recommended) : DepthAmbiantPass
111 auto zprepass =
114 // Transparent pass (0ptional) : If Transparent ... add LitOIT
115 if ( isTransparent ) {
116 auto transparentpass =
118 rt.setConfiguration( *transparentpass,
120 }
121 } );
122
123 // Registering parameters metadata
124 ParameterSetEditingInterface::loadMetaData( materialName, s_parametersMetadata );
125}
126
129 Rendering::EngineRenderTechniques::removeDefaultTechnique( materialName );
130}
131
133BlinnPhongMaterialConverter::operator()( const Ra::Core::Asset::MaterialData* toconvert ) {
134 auto result = new BlinnPhongMaterial( toconvert->getName() );
135 // we are sure here that the concrete type of "toconvert" is BlinnPhongMaterialData
136 // static cst is safe here
137 auto source = static_cast<const Ra::Core::Asset::BlinnPhongMaterialData*>( toconvert );
138
139 if ( source->hasDiffuse() ) result->m_kd = source->m_diffuse;
140 if ( source->hasSpecular() ) result->m_ks = source->m_specular;
141 if ( source->hasShininess() ) result->m_ns = source->m_shininess;
142 if ( source->hasOpacity() ) result->m_alpha = source->m_opacity;
144 data.sampler.wrapS = GL_REPEAT;
145 data.sampler.wrapT = GL_REPEAT;
146 data.sampler.minFilter = GL_LINEAR_MIPMAP_LINEAR;
147 auto texManager = RadiumEngine::getInstance()->getTextureManager();
148 if ( source->hasDiffuseTexture() ) {
149 data.name = "diffuse";
150 data.image = texManager->loadTextureImage( source->m_texDiffuse, true );
151 result->addTexture( BlinnPhongMaterial::TextureSemantic::TEX_DIFFUSE, data );
152 }
153 if ( source->hasSpecularTexture() ) {
154 data.name = "specular";
155 data.image = texManager->loadTextureImage( source->m_texSpecular, true );
156 result->addTexture( BlinnPhongMaterial::TextureSemantic::TEX_SPECULAR, data );
157 }
158 if ( source->hasShininessTexture() ) {
159 data.name = "shininess";
160 data.image = texManager->loadTextureImage( source->m_texShininess, false );
161 result->addTexture( BlinnPhongMaterial::TextureSemantic::TEX_SHININESS, data );
162 }
163
164 if ( source->hasOpacityTexture() ) {
165 data.name = "opacity";
166 data.image = texManager->loadTextureImage( source->m_texOpacity, false );
167 result->addTexture( BlinnPhongMaterial::TextureSemantic::TEX_ALPHA, data );
168 }
169 if ( source->hasNormalTexture() ) {
170 data.name = "normal";
171 data.image = texManager->loadTextureImage( source->m_texNormal, false );
172 result->addTexture( BlinnPhongMaterial::TextureSemantic::TEX_NORMAL, data );
173 }
174 return result;
175}
176} // namespace Data
177} // namespace Engine
178} // namespace Ra
virtual const std::string & getName() const
Acces to the name of the asset.
Definition AssetData.hpp:29
represent material data loaded by a file loader. Material data must be identified by a unique name....
BlinnPhongMaterial(const std::string &instanceName)
Construct a named BlinnPhongMaterial.
void updateFromParameters() override
Update the attributes of the ShaderParameterProvider to their actual values stored in the renderParam...
void updateGL() override
Update the OpenGL states used by the ShaderParameterProvider.
Texture * getTexture(const TextureSemantics::BlinnPhongMaterial &semantic) const
Base class for materials.
Definition Material.hpp:24
bool isDirty()
Return dirty state.
Definition Material.hpp:118
void setClean()
Set dirty state to false.
Definition Material.hpp:124
MaterialAspect
Identifies the type of the material.
Definition Material.hpp:31
virtual bool isTransparent() const
Definition Material.hpp:65
static void loadMetaData(const std::string &basename, nlohmann::json &destination)
Load the ParameterSet description.
Represent a Texture of the engine.
Definition Texture.hpp:120
void setConfiguration(const Data::ShaderConfiguration &newConfig, Core::Utils::Index pass=DefaultRenderingPasses::LIGHTING_OPAQUE)
T min(T... args)
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)
hepler function to manage enum as underlying types in VariableSet
Definition Cage.cpp:3
GLenum wrapT
OpenGL wrap mode in the t direction.
Definition Texture.hpp:26
GLenum minFilter
OpenGL minification filter ( GL_LINEAR or GL_NEAREST or GL_XXX_MIPMAP_YYY )
Definition Texture.hpp:30
GLenum wrapS
OpenGL wrap mode in the s direction.
Definition Texture.hpp:24
Describes the sampler and image of a texture.
Definition Texture.hpp:99