Elevate Engine 1
Loading...
Searching...
No Matches
Shader.h
Go to the documentation of this file.
1#pragma once
2
3#include <string>
4#include <memory>
5
6#include <glm/glm.hpp>
8
9// the list of uniform names used by the shader
10// TODO v�rif si uniquement avec opengl ou non
11#define EE_SHADER_MODEL "model"
12#define EE_SHADER_VIEWPROJ "viewProj"
13#define EE_SHADER_CAMPOS "camPos"
14
15namespace Elevate
16{
17 class GameObject;
18 class Light;
19 class DirectionalLight;
20 class Camera;
21
22 class Shader
23 {
24 public:
26
27 virtual uint32_t GetID() const = 0;
28 virtual uint32_t GetHashCode() const = 0;
29
30 static std::shared_ptr<Shader> CreateDefaultNative(); // Safe fallback in case of shader creation failure
31 static std::shared_ptr<Shader> CreateDefaultErrorNative(); // Safe fallback with an error pattern shader
32 static std::shared_ptr<Shader> CreateDefault();
33 static std::shared_ptr<Shader> CreateDefaultError();
34
35 static std::shared_ptr<Shader> Create(const std::string& vertexSource, const std::string& fragmentSouce);
36 static std::shared_ptr<Shader> CreateFromFiles(const std::string& vertexSrcPath, const std::string& fragSrcPath);
37 static std::shared_ptr<Shader> CreateFromFiles(const std::string& vertexSrcPath, const std::string& fragSrcPath, const std::string& customVertCode, const std::string& customFragCode);
38
39 // Lights
40 void UseLight(Light* newLightSetting, const std::string& lightName);
41 void UseDirLight(DirectionalLight* newDirLight);
42
43 // Camera
44 void SetCameraPosition(const glm::vec3 cameraPosition) const;
45 void SetModelMatrix(const glm::mat4& modelMatrix);
46 void SetModelMatrix(const GameObject& object);
47
48 void SetProjectionViewMatrix(const glm::mat4 viewProjMatrix);
49
50 void SetProjectionViewMatrix(const Camera& cam);
51
53
59 virtual void SetUniform(const std::string& location, ShaderDataType type, void* value);
60
61 // FLOATS
62 virtual void SetUniform1f(const std::string& location, float value) const = 0;
63 virtual void SetUniform2f(const std::string& location, float x, float y) const = 0;
64 virtual void SetUniform3f(const std::string& location, float x, float y, float z) const = 0;
65 virtual void SetUniform4f(const std::string& location, float x, float y, float z, float w) const = 0;
66 // with glm vectors
67 inline void SetUniform2f(const std::string& location, glm::vec2 value) const
68 {
69 SetUniform2f(location, value.x, value.y);
70 }
71 inline void SetUniform3f(const std::string& location, glm::vec3 value) const
72 {
73 SetUniform3f(location, value.x, value.y, value.z);
74 }
75 inline void SetUniform4f(const std::string& location, glm::vec4 value) const
76 {
77 SetUniform4f(location, value.x, value.y, value.z, value.w);
78 }
79
80 // INT
81 virtual void SetUniform1i(const std::string& location, int value) const = 0;
82 virtual void SetUniform2i(const std::string& location, int x, int y) const = 0;
83 virtual void SetUniform3i(const std::string& location, int x, int y, int z) const = 0;
84 virtual void SetUniform4i(const std::string& location, int x, int y, int z, int w) const = 0;
85
86 // FLOAT VECTORS
87 virtual void SetUniform1fv(const std::string& location, int count, float* value) const = 0;
88 virtual void SetUniform2fv(const std::string& location, int count, float* value) const = 0;
89 virtual void SetUniform3fv(const std::string& location, int count, float* value) const = 0;
90 virtual void SetUniform4fv(const std::string& location, int count, float* value) const = 0;
91
92 // INT VECTORS
93 virtual void SetUniform1iv(const std::string& location, int count, int* value) const = 0;
94 virtual void SetUniform2iv(const std::string& location, int count, int* value) const = 0;
95 virtual void SetUniform3iv(const std::string& location, int count, int* value) const = 0;
96 virtual void SetUniform4iv(const std::string& location, int count, int* value) const = 0;
97
98 // MATRIX
99 virtual void SetUniformMatrix2fv(const std::string& location, float* data) const = 0;
100 void SetUniformMatrix2fv(const std::string& location, glm::mat2 data) const;
101 virtual void SetUniformMatrix3fv(const std::string& location, float* data) const = 0;
102 void SetUniformMatrix3fv(const std::string& location, glm::mat3 data) const;
103 virtual void SetUniformMatrix4fv(const std::string& location, float* data) const = 0;
104 void SetUniformMatrix4fv(const std::string& location, glm::mat4 data) const;
105
106 // TODO CHECK IF NEEDED FOR OTHER APIS
107 virtual unsigned int GetRendererID() const = 0;
108
109 inline bool IsInitialized() { return m_isInitialized; }
110
111 inline const BufferLayout& GetLayout() { return m_layout; }
112
113 protected:
115 inline void SetInitializationStatus(bool initialized) { m_isInitialized = initialized; }
116
117 private:
118 virtual void Bind() const = 0;
119 virtual void Unbind() const = 0;
120
121 private:
122 BufferLayout m_layout;
123 bool m_isInitialized = false;
124
125 friend class Renderer;
126 };
127
128 using ShaderPtr = std::shared_ptr<Shader>;
129
131 {
132 public:
133 static constexpr std::string_view GetVertexShader() {
134 return R"(
135layout(location = 0) in vec3 a_Position;
136
137uniform mat4 viewProj;
138uniform mat4 model;
139
140void main()
141{
142 gl_Position = viewProj * model * vec4(a_Position, 1.0);
143}
144)";
145 }
146
147 static constexpr std::string_view GetFragmentShader() {
148 return R"(
149layout(location = 0) out vec4 FragColor;
150
151void main()
152{
153 FragColor = vec4(1.0);
154}
155)";
156 }
157
158 static constexpr std::string_view GetErrorShader() {
159 return R"(
160layout(location = 0) out vec4 FragColor;
161uniform float time;
162
163void main()
164{
165 vec2 uv = gl_FragCoord.xy / 100.0;
166 float pattern = sin(uv.x * 10.0 + time) * cos(uv.y * 10.0 + time);
167 FragColor = vec4(1.0, 0.0, 1.0, 1.0);
168 if(pattern > 0.0) FragColor = vec4(0.0, 0.0, 0.0, 1.0);
169}
170)";
171 }
172 };
173}
static constexpr std::string_view GetFragmentShader()
Definition Shader.h:147
static constexpr std::string_view GetErrorShader()
Definition Shader.h:158
static constexpr std::string_view GetVertexShader()
Definition Shader.h:133
virtual void SetUniform4f(const std::string &location, float x, float y, float z, float w) const =0
virtual uint32_t GetHashCode() const =0
virtual void SetUniform3f(const std::string &location, float x, float y, float z) const =0
virtual void SetUniform3fv(const std::string &location, int count, float *value) const =0
void SetInitializationStatus(bool initialized)
Definition Shader.h:115
virtual void SetUniformMatrix4fv(const std::string &location, float *data) const =0
virtual void SetUniform(const std::string &location, ShaderDataType type, void *value)
UNIFORMS.
Definition Shader.cpp:158
virtual void SetUniform1i(const std::string &location, int value) const =0
const BufferLayout & GetLayout()
Definition Shader.h:111
virtual void SetUniform3iv(const std::string &location, int count, int *value) const =0
static std::shared_ptr< Shader > CreateDefaultErrorNative()
Definition Shader.cpp:28
static std::shared_ptr< Shader > CreateDefault()
Definition Shader.cpp:38
virtual void SetUniform1f(const std::string &location, float value) const =0
virtual void SetUniformMatrix2fv(const std::string &location, float *data) const =0
static std::shared_ptr< Shader > Create(const std::string &vertexSource, const std::string &fragmentSouce)
Definition Shader.cpp:48
virtual void SetUniform4i(const std::string &location, int x, int y, int z, int w) const =0
virtual void SetUniformMatrix3fv(const std::string &location, float *data) const =0
static std::shared_ptr< Shader > CreateDefaultError()
Definition Shader.cpp:43
virtual void SetUniform1iv(const std::string &location, int count, int *value) const =0
void SetUniform4f(const std::string &location, glm::vec4 value) const
Definition Shader.h:75
virtual void SetUniform2f(const std::string &location, float x, float y) const =0
void SetProjectionViewMatrix(const glm::mat4 viewProjMatrix)
Definition Shader.cpp:133
virtual void SetUniform1fv(const std::string &location, int count, float *value) const =0
virtual BufferLayout ExtractReflectionData() const =0
bool IsInitialized()
Definition Shader.h:109
void SetUniform3f(const std::string &location, glm::vec3 value) const
Definition Shader.h:71
virtual unsigned int GetRendererID() const =0
virtual uint32_t GetID() const =0
virtual void SetUniform4fv(const std::string &location, int count, float *value) const =0
virtual void SetUniform2fv(const std::string &location, int count, float *value) const =0
virtual void SetUniform4iv(const std::string &location, int count, int *value) const =0
virtual void SetUniform2iv(const std::string &location, int count, int *value) const =0
void SetCameraPosition(const glm::vec3 cameraPosition) const
Definition Shader.cpp:118
void UseDirLight(DirectionalLight *newDirLight)
Definition Shader.cpp:112
static std::shared_ptr< Shader > CreateFromFiles(const std::string &vertexSrcPath, const std::string &fragSrcPath)
Definition Shader.cpp:79
virtual void SetUniform3i(const std::string &location, int x, int y, int z) const =0
void UseLight(Light *newLightSetting, const std::string &lightName)
Definition Shader.cpp:104
virtual void SetUniform2i(const std::string &location, int x, int y) const =0
void SetUniform2f(const std::string &location, glm::vec2 value) const
Definition Shader.h:67
void SetModelMatrix(const glm::mat4 &modelMatrix)
Definition Shader.cpp:123
static std::shared_ptr< Shader > CreateDefaultNative()
Definition Shader.cpp:18
EngineDataType
Definition Data.h:106
std::shared_ptr< Shader > ShaderPtr
Definition Shader.h:128