Monad Engine da02fba2
> Undercity Codex_
Loading...
Searching...
No Matches
GraphicsAPI.cpp
Go to the documentation of this file.
1#include "graphics/GraphicsAPI.h"
2#include "graphics/ShaderProgram.h"
3#include "render/Material.h"
4#include "render/Mesh.h"
5#include <iostream>
6
7namespace eng
8{
10 {
11 glEnable(GL_DEPTH_TEST);
12 return true;
13 }
14
15 std::shared_ptr<ShaderProgram> GraphicsAPI::CreateShaderProgram(const std::string& vertexSource,
16 const std::string& fragmentSource)
17 {
18 GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
19 const char* vertexShaderCStr = vertexSource.c_str();
20 glShaderSource(vertexShader, 1, &vertexShaderCStr, nullptr);
21 glCompileShader(vertexShader);
22
23 GLint success;
24 glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
25 if (!success)
26 {
27 char infoLog[512];
28 glGetShaderInfoLog(vertexShader, 512, nullptr, infoLog);
29 std::cerr << "ERROR:VERTEX_SHADER_COMPILATION_FAILED: " << infoLog << std::endl;
30 return nullptr;
31 }
32
33 GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
34 const char* fragmentShaderSourceCStr = fragmentSource.c_str();
35 glShaderSource(fragmentShader, 1, &fragmentShaderSourceCStr, nullptr);
36 glCompileShader(fragmentShader);
37
38 glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
39 if (!success)
40 {
41 char infoLog[512];
42 glGetShaderInfoLog(fragmentShader, 512, nullptr, infoLog);
43 std::cerr << "ERROR:FRAGMENT_SHADER_COMPILATION_FAILED: " << infoLog << std::endl;
44 return nullptr;
45 }
46
47 GLuint shaderProgramID = glCreateProgram();
48 glAttachShader(shaderProgramID, vertexShader);
49 glAttachShader(shaderProgramID, fragmentShader);
50 glLinkProgram(shaderProgramID);
51
52 glGetProgramiv(shaderProgramID, GL_LINK_STATUS, &success);
53 if (!success)
54 {
55 char infoLog[512];
56 glGetProgramInfoLog(shaderProgramID, 512, nullptr, infoLog);
57 std::cerr << "ERROR:SHADER_PROGRAM_LINKING_FAILED: " << infoLog << std::endl;
58 return nullptr;
59 }
60
61 glDeleteShader(vertexShader);
62 glDeleteShader(fragmentShader);
63
64 return std::make_shared<ShaderProgram>(shaderProgramID);
65 }
66
67 const std::shared_ptr<ShaderProgram>& GraphicsAPI::GetDefaultShaderProgram()
68 {
69 if (!m_defaultShaderProgram)
70 {
71 std::string vertexShaderSource = R"(
72 #version 330 core
73 layout (location = 0) in vec3 position;
74 layout (location = 1) in vec3 color;
75 layout (location = 2) in vec2 uv;
76 layout (location = 3) in vec3 normal;
77
78 out vec2 vUV;
79 out vec3 vNormal;
80 out vec3 vFragPos;
81
82 uniform mat4 uModel;
83 uniform mat4 uView;
84 uniform mat4 uProjection;
85
86 void main()
87 {
88 vUV = uv;
89 vNormal = normalize(transpose(inverse(mat3(uModel))) * normal);
90 vFragPos = vec3(uModel * vec4(position, 1.0));
91 gl_Position = uProjection * uView * uModel * vec4(position, 1.0);
92 }
93 )";
94
95 std::string fragmentShaderSource = R"(
96 #version 330 core
97
98 struct Light
99 {
100 vec3 color;
101 vec3 direction;
102 };
103
104 uniform Light uLight;
105 uniform vec3 uCameraPos;
106
107 out vec4 FragColor;
108
109 in vec2 vUV;
110 in vec3 vNormal;
111 in vec3 vFragPos;
112
113 uniform sampler2D baseColorTexture;
114
115 void main()
116 {
117 vec3 norm = normalize(vNormal);
118
119 // diffuse
120 vec3 lightDir = normalize(-uLight.direction);
121 float diff = max(dot(norm, lightDir), 0.0);
122 vec3 diffuse = diff * uLight.color;
123
124 // specular
125 vec3 viewDir = normalize(uCameraPos - vFragPos);
126 vec3 redlectDir = reflect(-lightDir, norm);
127 float spec = pow(max(dot(viewDir, redlectDir), 0.0), 32.0);
128 float specularStrength = 0.5;
129 vec3 specular = specularStrength * spec * uLight.color;
130
131 // ambient
132 const float ambientStrength = 0.4;
133 vec3 ambient = ambientStrength * uLight.color;
134
135 vec3 result = diffuse + specular + ambient;
136
137 vec4 texColor = texture(baseColorTexture, vUV);
138
139 FragColor = texColor * vec4(result, 1.0);
140 }
141 )";
142
143 m_defaultShaderProgram = CreateShaderProgram(vertexShaderSource, fragmentShaderSource);
144 }
145
146 return m_defaultShaderProgram;
148
149 const std::shared_ptr<ShaderProgram>& GraphicsAPI::GetDefault2DShaderProgram()
150 {
151 if (!m_default2DShaderProgram)
152 {
153 std::string vertexShaderSource = R"(
154 #version 330 core
155 layout (location = 0) in vec2 position;
156
157 out vec2 vUV;
158
159 uniform mat4 uModel;
160 uniform mat4 uView;
161 uniform mat4 uProjection;
162
163 uniform vec2 uPivot;
164 uniform vec2 uSize;
165
166 uniform vec2 uUVMin;
167 uniform vec2 uUVMax;
168
169 void main()
170 {
171 vec2 local = (position - uPivot) * uSize;
172 vUV = mix(uUVMin, uUVMax, position);
173
174 gl_Position = uProjection * uView * uModel * vec4(local, 0.0, 1.0);
175 }
176 )";
177
178 std::string fragmentShaderSource = R"(
179 #version 330 core
180
181 in vec2 vUV;
182
183 uniform vec4 uColor;
184
185 uniform sampler2D uTex;
186
187 out vec4 FragColor;
188
189 void main()
190 {
191 vec4 src = texture(uTex, vUV) * uColor;
192 FragColor = src;
193 }
194 )";
195
196 m_default2DShaderProgram = CreateShaderProgram(vertexShaderSource, fragmentShaderSource);
198 return m_default2DShaderProgram;
199 }
200
201 GLuint GraphicsAPI::CreateVertexBuffer(const std::vector<float>& vertices)
202 {
203 GLuint VBO = 0;
204 glGenBuffers(1, &VBO);
205 glBindBuffer(GL_ARRAY_BUFFER, VBO);
206 glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(float), vertices.data(), GL_STATIC_DRAW);
207 glBindBuffer(GL_ARRAY_BUFFER, 0);
208 return VBO;
209 }
210
211 GLuint GraphicsAPI::CreateIndexBuffer(const std::vector<uint32_t>& indices)
212 {
213 GLuint EBO = 0;
214 glGenBuffers(1, &EBO);
215 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
216 glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(uint32_t), indices.data(), GL_STATIC_DRAW);
217 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
218 return EBO;
219 }
220
221 void GraphicsAPI::SetClearColor(float r, float g, float b, float a)
223 glClearColor(r, g, b, a);
224 }
225
228 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
229 }
230
231 void GraphicsAPI::SetDepthTestEnabled(bool enabled)
232 {
233 if (enabled)
234 {
235 glEnable(GL_DEPTH_TEST);
236 }
237 else
238 {
239 glDisable(GL_DEPTH_TEST);
240 }
241 }
242
244 {
245 switch (mode)
246 {
248 {
249 glDisable(GL_BLEND);
250 }
251 break;
252 case BlendMode::Alpha:
253 {
254 glEnable(GL_BLEND);
255 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
256 }
257 break;
259 {
260 glEnable(GL_BLEND);
261 glBlendFunc(GL_ONE, GL_ONE);
262 }
263 break;
265 {
266 glEnable(GL_BLEND);
267 glBlendFunc(GL_DST_COLOR, GL_ZERO);
268 }
269 break;
270 default:
271 {
272 glDisable(GL_BLEND);
273 }
274 break;
275 }
276 }
277
278 void GraphicsAPI::BindShaderProgram(ShaderProgram* shaderProgram)
279 {
280 if (shaderProgram)
281 {
282 shaderProgram->Bind();
283 }
284 }
285
286 void GraphicsAPI::BindMaterial(Material* material)
287 {
288 if (material)
289 {
290 material->Bind();
291 }
292 }
293
294 void GraphicsAPI::BindMesh(Mesh* mesh)
295 {
296 if (mesh)
297 {
298 mesh->Bind();
299 }
300 }
301
302 void GraphicsAPI::UnbindMesh(Mesh* mesh)
303 {
304 if (mesh)
305 {
306 mesh->Unbind();
307 }
308 }
309
310 void GraphicsAPI::DrawMesh(Mesh* mesh)
311 {
312 if (mesh)
313 {
314 mesh->Draw();
315 }
316 }
317}
int GLint
Definition GLForward.h:13
unsigned int GLuint
Definition GLForward.h:11
std::shared_ptr< Material > material
std::shared_ptr< Mesh > mesh
void DrawMesh(Mesh *mesh)
const std::shared_ptr< ShaderProgram > & GetDefaultShaderProgram()
void UnbindMesh(Mesh *mesh)
void BindMesh(Mesh *mesh)
void SetDepthTestEnabled(bool enabled)
GLuint CreateIndexBuffer(const std::vector< uint32_t > &indices)
GLuint CreateVertexBuffer(const std::vector< float > &vertices)
std::shared_ptr< ShaderProgram > CreateShaderProgram(const std::string &vertexSource, const std::string &fragmentSource)
void BindMaterial(Material *material)
void SetClearColor(float r, float g, float b, float a)
void SetBlendMode(BlendMode mode)
void BindShaderProgram(ShaderProgram *shaderProgram)
const std::shared_ptr< ShaderProgram > & GetDefault2DShaderProgram()
BlendMode
Definition GraphicsAPI.h:14