rag
graphic 2d engine
Material.h
1 #ifndef _Material_H_
2 #define _Material_H_
3 
4 #include <string>
5 #include <glm/glm.hpp>
6 #include <vector>
7 
8 namespace rag
9 {
10  class Renderer;
11 
12  enum EUniformType
13  {
14  UNIF_1F, // 1 float
15  UNIF_2F, // 2 floats
16  UNIF_3F, // 3 floats
17  UNIF_4F, // 4 floats
18  UNIF_MAT3, // 3 component matrix
19  UNIF_MAT4 // 4 component matrix
20  };
21 
22  struct TUniformVar
23  {
24  EUniformType type;
25  std::string name;
26  float value[16];
27  };
28 
29 
31 
39 class Material
40 {
41 
42 public:
43 
45  Material(const std::string& vsh, const std::string& fsh);
46 
48  virtual ~Material() {};
49 
50  enum EBlendMode
51  {
52  BLEND_NORMAL,
53  BLEND_ADD,
54  BLEND_PRE_ADD
55  };
56 
58  void setUniform(const std::string &name, float value);
60  void setUniform(const std::string &name, const glm::vec2& value);
62  void setUniform(const std::string &name, const glm::vec3& value);
64  void setUniform(const std::string &name, const glm::vec4& value);
66  void setUniform(const std::string &name, const glm::mat3& value);
68  void setUniform(const std::string &name, const glm::mat4& value);
69 
70  std::string vsh;
71  std::string fsh;
72  int textureId;
73  EBlendMode blendMode;
74  int priority;
76 
78  const std::vector<TUniformVar>& getUniforms() const
79  {
80  return uniforms;
81  }
82 
83 private:
84 
85  TUniformVar* getUniform(const std::string &name);
86 
87  friend class Renderer;
88 
89  std::vector<TUniformVar> uniforms;
90 
91 };
92 
94 inline bool operator==(const TUniformVar& lhs, const TUniformVar& rhs)
95 {
96  return
97  lhs.name == rhs.name &&
98  lhs.type == rhs.type &&
99  memcmp(lhs.value, rhs.value, sizeof(float)) == 0;
100 }
101 inline bool operator!=(const TUniformVar& lhs, const TUniformVar& rhs){ return !operator==(lhs, rhs); }
102 
103 
105 inline bool operator==(const Material& lhs, const Material& rhs)
106 {
107  // Anything that changes the render state.
108  return
109  lhs.vsh == rhs.vsh &&
110  lhs.fsh == rhs.fsh &&
111  lhs.textureId == rhs.textureId &&
112  lhs.blendMode == rhs.blendMode &&
113  lhs.getUniforms() == rhs.getUniforms();
114 }
115 inline bool operator!=(const Material& lhs, const Material& rhs){ return !operator==(lhs, rhs); }
116 
117 
118 } // rag
119 
120 #endif
Contains methods to render objects.
Definition: Renderer.h:49
int priority
Materials with higher priority are propagated down in the Display List.
Definition: Material.h:74
const std::vector< TUniformVar > & getUniforms() const
Returns the list of all uniforms related with the current shader program.
Definition: Material.h:78
Definition: Bitmap.h:8
Material(const std::string &vsh, const std::string &fsh)
Default constructor using two paths for vertex and fragment shader respectively.
Definition: Material.cpp:6
EBlendMode blendMode
Blending mode.
Definition: Material.h:73
int textureId
Texture id.
Definition: Material.h:72
Definition: Material.h:22
Represents a render material that would affect how objects will be rendered.
Definition: Material.h:39
void setUniform(const std::string &name, float value)
Set float shader uniform.
Definition: Material.cpp:16
std::string vsh
Path for vertex shader.
Definition: Material.h:70
virtual ~Material()
Default destructor.
Definition: Material.h:48
std::string fsh
Path for fragment shader.
Definition: Material.h:71
bool stopsPropagation
If true, no matter priorities, material won't be affected by parent materials.
Definition: Material.h:75