Symphony Of Empires
shader.hpp
Go to the documentation of this file.
1 // Eng3D - General purpouse game engine
2 // Copyright (C) 2021, Eng3D contributors
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <https://www.gnu.org/licenses/>.
16 //
17 // ----------------------------------------------------------------------------
18 // Name:
19 // shader.hpp
20 //
21 // Abstract:
22 // Does some important stuff.
23 // ----------------------------------------------------------------------------
24 
25 #pragma once
26 
27 #include <cassert>
28 #include <string>
29 #include <exception>
30 
31 #include <glm/gtc/type_ptr.hpp>
32 #include <glm/mat4x4.hpp>
33 
34 #include "eng3d/texture.hpp"
35 #include "eng3d/glsl_trans.hpp"
36 #include "eng3d/utils.hpp"
37 
38 namespace Eng3D {
39  class ShaderException : public std::exception {
40  std::string buffer;
41  public:
42  ShaderException(const std::string& _buffer)
43  : buffer{ _buffer }
44  {
45 
46  }
47 
48  virtual const char* what() const noexcept {
49  return buffer.c_str();
50  }
51  };
52 
53  namespace OpenGL {
56  class Option {
57  std::string _option;
58  public:
59  bool used = false;
60 
61  Option() = default;
62  Option(std::string option, bool use)
63  : _option{ option },
64  used{ use }
65  {
66 
67  }
68 
69  std::string get_option() const {
70  return _option;
71  }
72  };
73 
75  class Shader {
76  private:
77  void compile(unsigned int type);
78  std::string buffer;
79  unsigned int id;
80  std::vector<int> line_numbers;
81  public:
82  Shader(const std::string& _buffer, unsigned int type, bool use_transpiler = true, std::vector<Eng3D::GLSL::Define> defintions = {});
84  unsigned int get_id() const;
85  };
86 
87  class VertexShader: public Shader {
88  public:
89  VertexShader(const std::string& _buffer);
90  ~VertexShader() = default;
91  };
92 
93  class FragmentShader: public Shader {
94  public:
95  FragmentShader(const std::string& _buffer, bool use_transpiler = true, std::vector<Eng3D::GLSL::Define> defintions = {});
96  ~FragmentShader() = default;
97  };
98 
99 #if !defined E3D_BACKEND_GLES
100  class GeometryShader: public Shader {
101  public:
102  GeometryShader(const std::string& _buffer);
103  ~GeometryShader() = default;
104  };
105 
106  class TessControlShader: public Shader {
107  public:
108  TessControlShader(const std::string& _buffer);
109  ~TessControlShader() = default;
110  };
111 
112  class TessEvalShader: public Shader {
113  public:
114  TessEvalShader(const std::string& _buffer);
115  ~TessEvalShader() = default;
116  };
117 #endif
118 
119  class Program {
120  unsigned int id;
121  public:
124  void link();
126  void use() const;
127  void set_PVM(glm::mat4 projection, glm::mat4 view, glm::mat4 model) const;
128  void set_uniform(const std::string& name, glm::mat4 uniform) const;
129  void set_uniform(const std::string& name, float value1, float value2) const;
130  void set_uniform(const std::string& name, float value1, float value2, float value3) const;
131  void set_uniform(const std::string& name, glm::vec2 uniform) const;
132  void set_uniform(const std::string& name, glm::vec3 uniform) const;
133  void set_uniform(const std::string& name, glm::vec4 uniform) const;
134  void set_uniform(const std::string& name, float value1, float value2, float value3, float value4) const;
135  void set_uniform(const std::string& name, float value) const;
136  void set_uniform(const std::string& name, int value) const;
137  void set_texture(int value, const std::string& name, const Eng3D::Texture& texture) const;
138  void set_texture(int value, const std::string& name, const Eng3D::TextureArray& texture) const;
139  unsigned int get_id() const;
140  };
141  }
142 }
FragmentShader(const std::string &_buffer, bool use_transpiler=true, std::vector< Eng3D::GLSL::Define > defintions={})
GeometryShader(const std::string &_buffer)
Option that is passed to the GLSL transpiler for preprocessing the shaders and programming them on th...
Definition: shader.hpp:56
std::string get_option() const
Definition: shader.hpp:69
Option(std::string option, bool use)
Definition: shader.hpp:62
void set_uniform(const std::string &name, int value) const
void set_uniform(const std::string &name, glm::vec4 uniform) const
void set_uniform(const std::string &name, float value1, float value2) const
void set_uniform(const std::string &name, float value) const
void set_texture(int value, const std::string &name, const Eng3D::Texture &texture) const
void set_texture(int value, const std::string &name, const Eng3D::TextureArray &texture) const
void set_uniform(const std::string &name, float value1, float value2, float value3, float value4) const
void attach_shader(const Eng3D::OpenGL::Shader &shader)
void set_uniform(const std::string &name, glm::mat4 uniform) const
void set_uniform(const std::string &name, float value1, float value2, float value3) const
void set_uniform(const std::string &name, glm::vec2 uniform) const
unsigned int get_id() const
void set_PVM(glm::mat4 projection, glm::mat4 view, glm::mat4 model) const
void set_uniform(const std::string &name, glm::vec3 uniform) const
OpenGL shader object.
Definition: shader.hpp:75
unsigned int get_id() const
Shader(const std::string &_buffer, unsigned int type, bool use_transpiler=true, std::vector< Eng3D::GLSL::Define > defintions={})
TessControlShader(const std::string &_buffer)
TessEvalShader(const std::string &_buffer)
VertexShader(const std::string &_buffer)
ShaderException(const std::string &_buffer)
Definition: shader.hpp:42
virtual const char * what() const noexcept
Definition: shader.hpp:48