Radium Engine  1.5.0
BlinnPhongMaterial.hpp
1 #pragma once
2 
3 #include <Engine/RaEngine.hpp>
4 
5 #include <map>
6 #include <string>
7 
8 #include <Core/Utils/Color.hpp>
9 #include <Engine/Data/Material.hpp>
10 #include <Engine/Data/Texture.hpp>
11 
12 namespace Ra {
13 namespace Core {
14 namespace Asset {
15 class MaterialData;
16 }
17 } // namespace Core
18 
19 namespace Engine {
20 
21 namespace Data {
22 class ShaderProgram;
23 
29 class RA_ENGINE_API BlinnPhongMaterial final : public Material, public ParameterSetEditingInterface
30 {
31  friend class BlinnPhongMaterialConverter;
32 
33  public:
35  enum class TextureSemantic { TEX_DIFFUSE, TEX_SPECULAR, TEX_NORMAL, TEX_SHININESS, TEX_ALPHA };
36 
37  public:
38  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
43  explicit BlinnPhongMaterial( const std::string& instanceName );
49  ~BlinnPhongMaterial() override;
50 
51  void updateGL() override;
52  void updateFromParameters() override;
53  bool isTransparent() const override;
54 
60  inline void addTexture( const TextureSemantic& semantic, Texture* texture );
61 
67  inline Texture* getTexture( const TextureSemantic& semantic ) const;
68 
74  static void registerMaterial();
75 
80  static void unregisterMaterial();
81 
86  inline nlohmann::json getParametersMetadata() const override;
87 
88  inline void setColoredByVertexAttrib( bool state ) override;
89 
90  inline bool isColoredByVertexAttrib() const override;
91 
92  public:
93  Core::Utils::Color m_kd { 0.7, 0.7, 0.7, 1.0 };
94  Core::Utils::Color m_ks { 0.3, 0.3, 0.3, 1.0 };
95  Scalar m_ns { 64.0 };
96  Scalar m_alpha { 1.0 };
97  bool m_perVertexColor { false };
98  bool m_renderAsSplat { false };
99 
106  inline TextureParameters& addTexture( const TextureSemantic& semantic,
107  const TextureParameters& texture );
108 
109  private:
110  std::map<TextureSemantic, Texture*> m_textures;
111  std::map<TextureSemantic, TextureParameters> m_pendingTextures;
112  static nlohmann::json s_parametersMetadata;
113 
120  inline TextureParameters& addTexture( const TextureSemantic& semantic,
121  const std::string& texture );
122 
126  void updateRenderingParameters();
127 };
128 
132 class RA_ENGINE_API BlinnPhongMaterialConverter final
133 {
134  public:
135  BlinnPhongMaterialConverter() = default;
136  ~BlinnPhongMaterialConverter() = default;
137 
138  Material* operator()( const Ra::Core::Asset::MaterialData* toconvert );
139 };
140 
141 // Add a texture as material parameter from an already existing Radium Texture
142 inline void BlinnPhongMaterial::addTexture( const TextureSemantic& semantic, Texture* texture ) {
143  m_textures[semantic] = texture;
144  // remove pendingTexture with same semantic, since the latter would
145  // overwrite the former when updateGL will be called.
146  m_pendingTextures.erase( semantic );
147 }
148 
149 // Add a texture as material parameter with texture parameter set by default for this material
150 inline TextureParameters& BlinnPhongMaterial::addTexture( const TextureSemantic& semantic,
151  const std::string& texture ) {
152  CORE_ASSERT( !texture.empty(), "Invalid texture name" );
153 
154  TextureParameters data;
155  data.name = texture;
156  data.wrapS = GL_REPEAT;
157  data.wrapT = GL_REPEAT;
158  if ( semantic != TextureSemantic::TEX_NORMAL ) { data.minFilter = GL_LINEAR_MIPMAP_LINEAR; }
159  return addTexture( semantic, data );
160 }
161 
162 // Add a texture as material parameter with texture parameter set by the caller
163 // The textures will be finalized (i.e loaded from a file if needed and transformed to OpenGL
164 // texture) only when needed by the updateGL method.
166  const TextureParameters& texture ) {
167  m_pendingTextures[semantic] = texture;
168  m_isDirty = true;
169 
170  return m_pendingTextures[semantic];
171 }
172 
173 inline Texture* BlinnPhongMaterial::getTexture( const TextureSemantic& semantic ) const {
174  Texture* tex = nullptr;
175 
176  auto it = m_textures.find( semantic );
177  if ( it != m_textures.end() ) { tex = it->second; }
178 
179  return tex;
180 }
181 
182 inline nlohmann::json BlinnPhongMaterial::getParametersMetadata() const {
183  return s_parametersMetadata;
184 }
185 
187  bool oldState = m_perVertexColor;
188  m_perVertexColor = state;
189  if ( oldState != m_perVertexColor ) { needUpdate(); }
190 }
191 
193  return m_perVertexColor;
194 }
195 
196 } // namespace Data
197 } // namespace Engine
198 } // namespace Ra
represent material data loaded by a file loader. Material data must be identified by a unique name....
bool isColoredByVertexAttrib() const override
Indicates if the material takes the VERTEX_COLOR attribute into account.
void addTexture(const TextureSemantic &semantic, Texture *texture)
TextureSemantic
Semantic of the texture : define which BSDF parameter is controled by the texture.
Texture * getTexture(const TextureSemantic &semantic) const
void setColoredByVertexAttrib(bool state) override
Makes the Material take its base color from the VERTEX_COLOR attribute of the rendered geometry.
nlohmann::json getParametersMetadata() const override
Definition: Cage.cpp:3
GLenum wrapT
OpenGL wrap mode in the t direction.
Definition: Texture.hpp:62
GLenum wrapS
OpenGL wrap mode in the s direction.
Definition: Texture.hpp:60
GLenum minFilter
OpenGL minification filter ( GL_LINEAR or GL_NEAREST or GL_XXX_MIPMAP_YYY )
Definition: Texture.hpp:66
std::string name
Name of the texture.
Definition: Texture.hpp:44