AnimEngine
Material.hpp
1 #pragma once
2 #ifndef KJY_OBJECT_ORIENTED_OPENGL_MATERIAL_HPP_
3 #define KJY_OBJECT_ORIENTED_OPENGL_MATERIAL_HPP_
4 #include "../OOOGL_Core.hpp"
5 #include "../GLobjects/Texture.hpp"
6 #include <glm/glm.hpp>
7 #include "../GLSL.hpp"
8 #include "MaterialAttribute.hpp"
9 #include <unordered_map>
10 
11 namespace OOOGL{
12 
13 struct AttributeTypeMismatchException : public exception{
14  const char* what() const noexcept override{
15  return("Attempted to assign material attribute with mismatching type");
16  }
17 };
18 
19 /* For now these materials will be pretty bare bones. However I'd eventually
20  * like to integrate a bunch of utilities for automatically formatting material
21  * information into the correct layout for use with all sorts of GLSL aggregate
22  * type. */
24  public:
25 
26  /* Uploads material information as uniforms, binds textures, ect... Leaving
27  * the given shader program in the correct state for rendering with the given
28  * material. */
29  virtual void configureShader(GLuint program) const = 0;
30 
31  /* Upload material information to program as uniforms. */
32  virtual void uploadUniforms(GLuint program) const = 0;
33 
34  /* Get a description of the way material data gets layed out in GLSL code */
35  virtual const GLSLBlockLayoutConstPtr getMaterialGLSLLayout() const = 0;
36 
37  virtual bool hasAttribute(const std::string& attr_name) const = 0;
38  virtual const MaterialAttribute& getAttribute(const std::string& attr_name) const = 0;
39  virtual void setAttribute(const std::string& attr_name, const MaterialAttribute& attr) = 0;
40 
41 };
42 
44  public:
45 
46  virtual void configureShader(GLuint program) const override;
47 
48  /* Bind textures starting at texture unit 'texture_binding_offset'. Then
49  * attempt to automatically upload material parameters as uniform data by
50  * passing program to uploadUniforms */
51  virtual void configureShader(GLuint program, GLuint textured_binding_offset) const;
52 
53 
54  virtual void uploadUniforms(GLuint pid) const override;
55  virtual const GLSLBlockLayoutConstPtr getMaterialGLSLLayout() const override;
56 
57 
58  virtual bool hasAttribute(const std::string& attr_name) const override;
59  virtual const MaterialAttribute& getAttribute(const std::string& attr_name) const override;
60  virtual void setAttribute(const std::string& attr_name, const MaterialAttribute& attr) override;
61 
62 
63 
64  /* Bind textures starting at texture unit 'texture_binding_offset'. Then
65  * attempt to automatically upload material parameters as uniform data by
66  * passing program to uploadUniforms.
67  *
68  * Returns: The texture unit following the last unit bound by the function.
69  * */
70  GLuint autoBindTextures(GLuint program, GLuint texture_binding_offset = 0) const;
71 
72  /* Bind textures to the provided texture unit indices. Negative indices are ignored */
73  void bindTexturesExplicit(GLuint map_Ka_index, GLuint map_Kd_index, GLuint map_Ks_index, GLuint map_bump_index, GLuint map_d_index, GLuint map_disp_index) const;
74 
75  static const GLSLBlockLayoutConstPtr getMaterialGLSLLayout_static() {return(&_glsl_layout_);}
76 
77  protected:
78 
79  std::unordered_map<std::string, MaterialAttribute> _attributes = {
80  {"Ka", MaterialAttribute(glm::vec3())},
81  {"Kd", MaterialAttribute(glm::vec3())},
82  {"Ks", MaterialAttribute(glm::vec3())},
83  {"Ns", MaterialAttribute(0.0f)},
84  {"d", MaterialAttribute(0.0f)},
85  {"illum", MaterialAttribute(0)},
92  };
93 
94  private:
95 
96  void _bind_textures_(GLuint Ka, GLuint Kd, GLuint Ks, GLuint bump, GLuint d, GLuint disp) const;
97  static const TemporaryHardCodedLayout _glsl_layout_;
98 
99 };
100 
102  public:
103 
104  /* Uploads material information as uniforms, binds textures, ect... Leaving
105  * the given shader program in the correct state for rendering with the given
106  * material. */
107  virtual void configureShader(GLuint program) const override;
108 
109  /* Upload material information to program as uniforms. */
110  virtual void uploadUniforms(GLuint program) const override;
111 
112  /* Get a description of the way material data gets layed out in GLSL code */
113  virtual const GLSLBlockLayoutConstPtr getMaterialGLSLLayout() const override;
114 
115  virtual bool hasAttribute(const std::string& attr_name) const override;
116  virtual const MaterialAttribute& getAttribute(const std::string& attr_name) const override;
117  virtual void setAttribute(const std::string& attr_name, const MaterialAttribute& attr) override;
118 
119  virtual void printMaterial() const;
120 
121 
122  static const GLSLBlockLayoutConstPtr getMaterialGLSLLayout_static() {return(&_glsl_layout_);}
123 
124  protected:
125 
126  std::unordered_map<std::string, MaterialAttribute> _attributes = {
127  {"emissiveFactor", MaterialAttribute(glm::vec3(0.0))},
128  {"baseColorFactor", MaterialAttribute(glm::vec3(0.0))},
129  {"metallicFactor", MaterialAttribute(0.0f)},
130  {"roughnessFactor", MaterialAttribute(0.0f)},
131 
132  {"baseColorTexture", MaterialAttribute(OptionalSimpleTexture2D())},
133  {"metallicRoughnessTexture", MaterialAttribute(OptionalSimpleTexture2D())},
134  {"normalTexture", MaterialAttribute(OptionalSimpleTexture2D())},
135  {"occlusionTexture", MaterialAttribute(OptionalSimpleTexture2D())},
136  {"emissiveTexture", MaterialAttribute(OptionalSimpleTexture2D())}
137  };
138 
139  private:
140 
141  static const TemporaryHardCodedLayout _glsl_layout_;
142 
143 };
144 
145 }
146 
147 #endif
Definition: Material.hpp:43
Definition: Material.hpp:13
Definition: GLSL.hpp:27
Definition: OOOGL_Core.hpp:133
Definition: Material.hpp:23
Definition: MaterialAttribute.hpp:67
Definition: Core_Utilities.cpp:3
Definition: Material.hpp:101