Radium Engine  1.5.0
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 
12 namespace Ra {
13 namespace Engine {
14 namespace Data {
15 static const std::string materialName { "BlinnPhong" };
16 
17 nlohmann::json BlinnPhongMaterial::s_parametersMetadata = {};
18 
19 BlinnPhongMaterial::BlinnPhongMaterial( const std::string& instanceName ) :
20  Material( instanceName, materialName, Material::MaterialAspect::MAT_OPAQUE ) {}
21 
23  m_textures.clear();
24 }
25 
26 void BlinnPhongMaterial::updateRenderingParameters() {
27  // update the rendering parameters
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] ) );
35  Texture* tex = getTexture( BlinnPhongMaterial::TextureSemantic::TEX_DIFFUSE );
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 );
50 }
51 
53  if ( !m_isDirty ) { return; }
54 
55  // Load textures
56  auto texManager = RadiumEngine::getInstance()->getTextureManager();
57  for ( const auto& tex : m_pendingTextures ) {
58  // ask to convert color textures from sRGB to Linear RGB
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;
63  // do not call addTexture since it invalidate m_pendingTextures itr
64  // addTexture( tex.first, texture );
65  }
66 
67  m_pendingTextures.clear();
68  m_isDirty = false;
69 
70  updateRenderingParameters();
71 }
72 
74  auto& renderParameters = getParameters();
75  m_kd = renderParameters.getParameter<Core::Utils::Color>( "material.kd" );
76  m_perVertexColor = renderParameters.getParameter<bool>( "material.hasPerVertexKd" );
77  m_renderAsSplat = renderParameters.getParameter<bool>( "material.renderAsSplat" );
78  m_ks = renderParameters.getParameter<Core::Utils::Color>( "material.ks" );
79  m_ns = renderParameters.getParameter<Scalar>( "material.ns" );
80  m_alpha = renderParameters.getParameter<Scalar>( "material.alpha" );
81 }
82 
84  return ( m_alpha < 1_ra ) || ( m_kd[3] < 1_ra ) || Material::isTransparent();
85 }
86 
88  // For resources access (glsl files) in a filesystem
89  auto resourcesRootDir { RadiumEngine::getInstance()->getResourcesDir() };
90  auto shaderProgramManager = RadiumEngine::getInstance()->getShaderProgramManager();
91 
92  // Defining the material converter
95 
96  // adding the material glsl implementation file
97  shaderProgramManager->addNamedString(
98  "/BlinnPhong.glsl", resourcesRootDir + "Shaders/Materials/BlinnPhong/BlinnPhong.glsl" );
99  // registering re-usable shaders
100  Data::ShaderConfiguration lpconfig(
101  "BlinnPhong",
102  resourcesRootDir + "Shaders/Materials/BlinnPhong/BlinnPhong.vert.glsl",
103  resourcesRootDir + "Shaders/Materials/BlinnPhong/BlinnPhong.frag.glsl" );
105 
106  Data::ShaderConfiguration zprepassconfig(
107  "ZprepassBlinnPhong",
108  resourcesRootDir + "Shaders/Materials/BlinnPhong/BlinnPhong.vert.glsl",
109  resourcesRootDir + "Shaders/Materials/BlinnPhong/BlinnPhongZPrepass.frag.glsl" );
111 
112  Data::ShaderConfiguration transparentpassconfig(
113  "LitOITBlinnPhong",
114  resourcesRootDir + "Shaders/Materials/BlinnPhong/BlinnPhong.vert.glsl",
115  resourcesRootDir + "Shaders/Materials/BlinnPhong/LitOITBlinnPhong.frag.glsl" );
117 
118  // Registering technique
119  Rendering::EngineRenderTechniques::registerDefaultTechnique(
120  materialName,
121 
122  []( Rendering::RenderTechnique& rt, bool isTransparent ) {
123  // Configure the technique to render this object using forward Renderer or any
124  // compatible one Main pass (Mandatory) : BlinnPhong
125  auto lightpass = Data::ShaderConfigurationFactory::getConfiguration( "BlinnPhong" );
126  rt.setConfiguration( *lightpass, Rendering::DefaultRenderingPasses::LIGHTING_OPAQUE );
127 
128  // Z prepass (Recommended) : DepthAmbiantPass
129  auto zprepass =
131  rt.setConfiguration( *zprepass, Rendering::DefaultRenderingPasses::Z_PREPASS );
132  // Transparent pass (0ptional) : If Transparent ... add LitOIT
133  if ( isTransparent ) {
134  auto transparentpass =
136  rt.setConfiguration( *transparentpass,
137  Rendering::DefaultRenderingPasses::LIGHTING_TRANSPARENT );
138  }
139  } );
140 
141  // Registering parameters metadata
142  ParameterSetEditingInterface::loadMetaData( materialName, s_parametersMetadata );
143 }
144 
147  Rendering::EngineRenderTechniques::removeDefaultTechnique( materialName );
148 }
149 
150 Material*
151 BlinnPhongMaterialConverter::operator()( const Ra::Core::Asset::MaterialData* toconvert ) {
152  auto result = new BlinnPhongMaterial( toconvert->getName() );
153  // we are sure here that the concrete type of "toconvert" is BlinnPhongMaterialData
154  // static cst is safe here
155  auto source = static_cast<const Ra::Core::Asset::BlinnPhongMaterialData*>( toconvert );
156 
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 );
174 
175  return result;
176 }
177 
178 } // namespace Data
179 } // namespace Engine
180 } // 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....
EIGEN_MAKE_ALIGNED_OPERATOR_NEW BlinnPhongMaterial(const std::string &instanceName)
Texture * getTexture(const TextureSemantic &semantic) const
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. These state could be the ones from an a...
virtual bool isTransparent() const
Definition: Material.cpp:14
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)
Definition: Cage.cpp:3