Elevate Engine 1
Loading...
Searching...
No Matches
Shader.cpp
Go to the documentation of this file.
1#include "eepch.h"
2#include "Shader.h"
3
4#include <glm/gtc/type_ptr.hpp>
5
7
15
16namespace Elevate
17{
18 std::shared_ptr<Shader> Shader::CreateDefaultNative()
19 {
20 switch (Renderer::GetAPI())
21 {
22 case RendererAPI::GraphicAPI::None: EE_CORE_ASSERT(false, "Renderer none is not supported");
23 case RendererAPI::GraphicAPI::OpenGL: return std::make_shared<OpenGLShader>(EE_SHADER_HEADER + std::string(DefaultShader::GetVertexShader()), EE_SHADER_HEADER + std::string(DefaultShader::GetFragmentShader()));
24 }
25 return nullptr;
26 }
27
28 std::shared_ptr<Shader> Shader::CreateDefaultErrorNative()
29 {
30 switch (Renderer::GetAPI())
31 {
32 case RendererAPI::GraphicAPI::None: EE_CORE_ASSERT(false, "Renderer none is not supported");
33 case RendererAPI::GraphicAPI::OpenGL: return std::make_shared<OpenGLShader>(EE_SHADER_HEADER + std::string(DefaultShader::GetVertexShader()), EE_SHADER_HEADER + std::string(DefaultShader::GetErrorShader()));
34 }
35 return nullptr;
36 }
37
38 std::shared_ptr<Shader> Shader::CreateDefault()
39 {
41 }
42
43 std::shared_ptr<Shader> Shader::CreateDefaultError()
44 {
46 }
47
48 ShaderPtr Shader::Create(const std::string& vertexSource, const std::string& fragmentSouce)
49 {
50 std::shared_ptr<Shader> shader = nullptr;
51
52 if (!vertexSource.empty() && !fragmentSouce.empty())
53 {
54 switch (Renderer::GetAPI())
55 {
56 case RendererAPI::GraphicAPI::None: EE_CORE_ASSERT(false, "Renderer none is not supported"); break;
57 case RendererAPI::GraphicAPI::OpenGL: shader = std::make_shared<OpenGLShader>(vertexSource, fragmentSouce);
58 }
59 }
60 else
61 {
62 EE_CORE_ERROR("Error : Tried to create a shader with an empty vertex or fragment source -> Vertex : {} And Fragment : {}", vertexSource, fragmentSouce);
64 }
65
66 if (!shader->IsInitialized())
67 {
68 shader.reset(); // Free the memory of that unused shader and reset to nullptr
69 EE_CORE_ERROR("Error : Could not create a valid shader. Fallback to default shader.");
71 }
72
73 shader->m_layout = shader->ExtractReflectionData();
74
75 EE_CORE_TRACE("Shader layout has {} elements.", shader->m_layout.GetElements().size());
76 return shader;
77 }
78
79 ShaderPtr Shader::CreateFromFiles(const std::string& vertexSrcPath, const std::string& fragSrcPath)
80 {
81 std::string vertexSource = File::GetFileContent(vertexSrcPath);
82 std::string fragmentSource = File::GetFileContent(fragSrcPath);
83 return Create(vertexSource, fragmentSource);
84 }
85
86 ShaderPtr Shader::CreateFromFiles(const std::string& vertexSrcPath, const std::string& fragSrcPath, const std::string& customVertCode, const std::string& customFragCode)
87 {
88 std::string vertexContent = File::GetFileContent(vertexSrcPath);
89 std::string fragmentContent = File::GetFileContent(fragSrcPath);
90
91 if (!vertexContent.empty() && !fragmentContent.empty())
92 {
93 std::string vertexSource = customVertCode + "\n" + vertexContent;
94 std::string fragmentSource = customFragCode + "\n" + fragmentContent;
95 return Create(vertexSource, fragmentSource);
96 }
97 else
98 {
99 EE_CORE_ERROR("Error : Tried to create a shader with an empty vertex or fragment source -> Vertex : {} And Fragment : {}", vertexContent, fragmentContent);
100 return CreateDefaultError();
101 }
102 }
103
104 void Shader::UseLight(Light* newLightSetting, const std::string& lightName)
105 {
106 SetUniform3f(lightName + ".ambient", newLightSetting->GetAmbientColor());
107 SetUniform3f(lightName + ".diffuse", newLightSetting->GetDiffuseColor());
108 SetUniform3f(lightName + ".specular", newLightSetting->GetSpecularColor());
109 SetUniform1f(lightName + ".intensity", newLightSetting->GetIntensity());
110 }
111
113 {
114 UseLight(newDirLight, "dirLight");
115 SetUniform3f("dirLight.direction", newDirLight->CalculateDirection());
116 }
117
118 void Shader::SetCameraPosition(const glm::vec3 cameraPosition) const
119 {
120 SetUniform3f(EE_SHADER_CAMPOS, cameraPosition);
121 }
122
123 void Shader::SetModelMatrix(const glm::mat4& modelMatrix)
124 {
125 SetUniformMatrix4fv(EE_SHADER_MODEL, modelMatrix); // set the model matrix
126 }
127
129 {
130 SetUniformMatrix4fv(EE_SHADER_MODEL, object.GetModelMatrix()); // set the model matrix
131 }
132
133 void Shader::SetProjectionViewMatrix(const glm::mat4 viewProjMatrix)
134 {
136 }
137
142
143 void Elevate::Shader::SetUniformMatrix2fv(const std::string& location, glm::mat2 data) const
144 {
145 SetUniformMatrix2fv(location, glm::value_ptr(data));
146 }
147
148 void Elevate::Shader::SetUniformMatrix3fv(const std::string& location, glm::mat3 data) const
149 {
150 SetUniformMatrix3fv(location, glm::value_ptr(data));
151 }
152
153 void Elevate::Shader::SetUniformMatrix4fv(const std::string& location, glm::mat4 data) const
154 {
155 SetUniformMatrix4fv(location, glm::value_ptr(data));
156 }
157
158 void Shader::SetUniform(const std::string& location, ShaderDataType type, void* value)
159 {
160 switch (type)
161 {
162 // todo impl all of the types here based on the functions we have in the shader part
163 // todo remove the need to cast to float* and directly use the SetUniform2fv functions (create them if necessary)
164 case ShaderDataType::Float: SetUniform1f(location, *(float*)value); break;
166 {
167 float* data = (float*)value;
168 SetUniform2f(location, data[0], data[1]);
169 break;
170 }
172 {
173 float* data = (float*)value;
174 SetUniform3f(location, data[0], data[1], data[2]);
175 break;
176 }
178 {
179 float* data = (float*)value;
180 SetUniform4f(location, data[0], data[1], data[2], data[3]);
181 break;
182 }
184 SetUniform1i(location, *(int*)value);
185 break;
187 SetUniformMatrix4fv(location, (float*) value);
188 break;
189 default:
190 EE_CORE_ASSERT(false, "(Shader::SetUniform()) : Unsupported datatype provided.");
191 break;
192 }
193 }
194}
#define EE_SHADER_HEADER
Definition Core.h:32
#define EE_SHADER_MODEL
Definition Shader.h:11
#define EE_SHADER_VIEWPROJ
Definition Shader.h:12
#define EE_SHADER_CAMPOS
Definition Shader.h:13
glm::mat4 GenViewProjectionMatrix() const
Definition Camera.cpp:64
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
const glm::vec3 CalculateDirection() const
const glm::vec3 & GetAmbientColor() const
Definition Light.h:37
const float & GetIntensity() const
Definition Light.h:40
const glm::vec3 & GetSpecularColor() const
Definition Light.h:39
const glm::vec3 & GetDiffuseColor() const
Definition Light.h:38
static RendererAPI::GraphicAPI GetAPI()
Definition Renderer.h:24
virtual void SetUniform4f(const std::string &location, float x, float y, float z, float w) const =0
virtual void SetUniform3f(const std::string &location, float x, float y, float z) const =0
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
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 SetUniformMatrix3fv(const std::string &location, float *data) const =0
static std::shared_ptr< Shader > CreateDefaultError()
Definition Shader.cpp:43
virtual void SetUniform2f(const std::string &location, float x, float y) const =0
void SetProjectionViewMatrix(const glm::mat4 viewProjMatrix)
Definition Shader.cpp:133
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
void UseLight(Light *newLightSetting, const std::string &lightName)
Definition Shader.cpp:104
void SetModelMatrix(const glm::mat4 &modelMatrix)
Definition Shader.cpp:123
static std::shared_ptr< Shader > CreateDefaultNative()
Definition Shader.cpp:18
std::string GetFileContent(std::string path)
EngineDataType
Definition Data.h:106
std::shared_ptr< Shader > ShaderPtr
Definition Shader.h:128