Engine
Frameworkcreatedbymeusableforthecreationofsimplegames.CurrentlysupportsOpenGL(Verysimple)andVulkan.
OpenGLShader.hpp
Go to the documentation of this file.
1 #pragma once
2 
4 #ifdef USING_OPENGL
5 #include "Engine/api.hpp"
7 
8 #include <ThirdParty/glm/glm/glm.hpp>
9 #include <ThirdParty/EASTL-master/include/EASTL/map.h>
10 #include <ThirdParty/EASTL-master/include/EASTL/shared_ptr.h>
11 #include <ThirdParty/glew-2.1.0/include/GL/glew.h>
12 
13 namespace Engine
14 {
15  //class Shader;
16  class Texture;
17  class Color;
18 
23  class ShaderParameter
24  {
25  friend class OpenGLShader;
26 
27  public:
28 
33  bool IsValid() const { return _location != -1; }
34 
39  GLenum GetType() const { return _type; }
40 
45  GLint GetLocation() const { return _location; }
50  void SetValue(float val) const;
51 
56  void SetValue(int val) const;
57 
62  void SetValue(bool val) const;
63 
68  void SetValue(const glm::vec2& vec) const;
69 
74  void SetValue(const glm::vec3& vec) const;
75 
80  void SetValue(const glm::vec4& vec) const;
81 
86  void SetValue(const Color& color);
87 
93  void SetValue(const glm::mat4x4& mtx, bool transpose = false) const;
94 
99  void SetValue(Texture& texture_) const;
100 
101  protected:
102 
111  ShaderParameter(OpenGLShader* shader, eastl::string name, GLenum type, GLint location, GLint sampler = -1)
112  : _shader(shader)
113  , _name(name)
114  , _type(type)
115  , _location(location)
116  , _sampler(sampler)
117  {}
118 
122  ShaderParameter() : _shader(nullptr), _name(""), _type(0), _location(-1), _sampler(-1) {}
123 
132  void Reset(OpenGLShader* shader, eastl::string name, GLenum type, GLint location, GLint sampler = -1)
133  {
134  this->_shader = shader;
135  this->_type = type;
136  this->_location = location;
137  this->_name = name;
138  this->_sampler = sampler;
139  }
140 
144  void Invalidate()
145  {
146  _shader = nullptr;
147  _name = "";
148  _type = 0;
149  _location = -1;
150  _sampler = -1;
151  }
152 
156  OpenGLShader* _shader;
157 
161  eastl::string _name;
162 
166  GLenum _type;
167 
171  GLint _location;
172 
176  GLint _sampler;
177  };
178 
179 
183  class ShaderAttribute
184  {
185  friend class OpenGLShader;
186 
187  public:
188 
193  bool IsValid() const { return _location != -1; }
194 
199  GLenum GetType() const { return _type; }
200 
205  GLint GetLocation() const { return _location; }
206 
216  void SetAttributePointer(
217  GLint size,
218  GLenum type,
219  GLboolean normalized,
220  GLsizei stride,
221  const GLvoid * pointer) const;
222 
226  void DisableAttributePointer() const;
227 
228  protected:
229 
238  ShaderAttribute(OpenGLShader* shader, eastl::string name, GLenum type, GLint location) :
239  _shader(shader),
240  _name(name),
241  _type(type),
242  _location(location)
243  {}
244 
248  ShaderAttribute() : _shader(nullptr), _name(""), _type(0), _location(-1) {}
249 
250 
258  void Reset(OpenGLShader* shader, eastl::string name, GLenum type, GLint location)
259  {
260  this->_shader = shader;
261  this->_type = type;
262  this->_location = location;
263  this->_name = name;
264  }
265 
269  void Invalidate()
270  {
271  _shader = nullptr;
272  _name = "";
273  _type = 0;
274  _location = -1;
275  }
276 
277 
278  protected:
282  OpenGLShader* _shader;
283 
287  eastl::string _name;
288 
292  GLenum _type;
293 
297  GLint _location;
298  };
299 
303  class ENGINE_API OpenGLShader : public Shader
304  {
305  private:
306 
307  private:
308  friend class OpenGLRenderer;
309 
318  explicit OpenGLShader(const eastl::string& vertexFileName,
319  const eastl::string& fragmentFileName);
320 
331  explicit OpenGLShader(const eastl::string& vertexFileName,
332  const eastl::string& fragmentFileName,
333  const eastl::string& geometryFileName);
334 
335  explicit OpenGLShader(const Shader& other) = delete;
336  OpenGLShader(OpenGLShader &&other) = delete;
337 
338  public:
350  bool LoadSource(const eastl::string& vertexShader,
351  const eastl::string& fragmentShader,
352  const eastl::string& geometryShader);
353 
359  eastl::weak_ptr<ShaderParameter> GetParameter(const eastl::string& name);
360 
366  eastl::weak_ptr<ShaderAttribute> GetAttribute(const eastl::string& name);
367 
372  GLuint GetProgram() const;
373 
377  void Activate() override;
381  void Deactivate() override;
382 
383  private:
384 
385  void LoadParameters();
386 
387  bool Validate() const;
388 
390  eastl::map<eastl::string, eastl::shared_ptr<ShaderParameter>> parameters;
391 
393  eastl::map<eastl::string, eastl::shared_ptr<ShaderAttribute>> attributes;
394 
396  GLuint program = 0;
397  };
398 } //namespace Engine
399 #endif
#define ENGINE_API
Definition: api.hpp:25