Radium Engine  1.5.0
SimpleMaterial.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 Engine {
14 namespace Data {
19 class RA_ENGINE_API SimpleMaterial : public Material, public ParameterSetEditingInterface
20 {
21  public:
23  enum class TextureSemantic { TEX_COLOR, TEX_MASK };
24 
29  explicit SimpleMaterial( const std::string& instanceName,
30  const std::string& materialName,
31  MaterialAspect aspect = MaterialAspect::MAT_OPAQUE );
32 
38  ~SimpleMaterial() override;
39 
44  void updateGL() override final;
45 
54  inline TextureParameters& addTexture( const TextureSemantic& semantic,
55  const TextureParameters& texture );
56 
62  inline void addTexture( const TextureSemantic& semantic, Texture* texture );
63 
69  inline Texture* getTexture( const TextureSemantic& semantic ) const;
70 
71  inline void setColoredByVertexAttrib( bool state ) override;
72 
73  inline bool isColoredByVertexAttrib() const override;
74 
75  public:
77  Core::Utils::Color m_color { 0.9, 0.9, 0.9, 1.0 };
80  bool m_perVertexColor { false };
81 
82  private:
86  void updateRenderingParameters();
87 
91  std::map<TextureSemantic, Texture*> m_textures;
95  std::map<TextureSemantic, TextureParameters> m_pendingTextures;
96 
97  protected:
99  static void loadMetaData( nlohmann::json& destination );
100 };
101 
102 // Add a texture as material parameter from an already existing Radium Texture
103 inline void SimpleMaterial::addTexture( const TextureSemantic& semantic, Texture* texture ) {
104  m_textures[semantic] = texture;
105  // remove pendingTexture with same semantic, since the latter would
106  // overwrite the former when updateGL will be called.
107  m_pendingTextures.erase( semantic );
108 }
109 
110 // Add a texture as material parameter with texture parameter set by the caller
112  const TextureParameters& texture ) {
113  m_pendingTextures[semantic] = texture;
114  m_isDirty = true;
115 
116  return m_pendingTextures[semantic];
117 }
118 
119 inline Texture* SimpleMaterial::getTexture( const TextureSemantic& semantic ) const {
120  Texture* tex = nullptr;
121 
122  auto it = m_textures.find( semantic );
123  if ( it != m_textures.end() ) { tex = it->second; }
124 
125  return tex;
126 }
127 
128 inline void SimpleMaterial::setColoredByVertexAttrib( bool state ) {
129  bool oldState = m_perVertexColor;
130  m_perVertexColor = state;
131  if ( oldState != m_perVertexColor ) { needUpdate(); }
132 }
133 
135  return m_perVertexColor;
136 }
137 
138 } // namespace Data
139 } // namespace Engine
140 } // namespace Ra
TextureSemantic
Semantic of the texture : define which BSDF parameter is controled by the texture.
bool isColoredByVertexAttrib() const override
Indicates if the material takes the VERTEX_COLOR attribute into account.
void setColoredByVertexAttrib(bool state) override
Makes the Material take its base color from the VERTEX_COLOR attribute of the rendered geometry.
TextureParameters & addTexture(const TextureSemantic &semantic, const TextureParameters &texture)
Texture * getTexture(const TextureSemantic &semantic) const
Definition: Cage.cpp:3