Kale
Loading...
Searching...
No Matches
Shader.cpp
Go to the documentation of this file.
1/*
2 Copyright 2022 Rishi Challa
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15*/
16
17#ifdef KALE_OPENGL
18
19#include "Shader.hpp"
20
21#include <fstream>
22#include <sstream>
23#include <string>
24#include <stdexcept>
25#include <memory>
26
27#include <glad/glad.h>
28
29using namespace Kale;
30using namespace Kale::OpenGL;
31
39unsigned int Shader::createShader(unsigned int type, const char* filePath) {
40 using namespace std::string_literals;
41
42 unsigned int shader = glCreateShader(type);
43
44 // Read in the file source
45 std::string src;
46 {
47 std::ifstream file(filePath);
48 std::ostringstream stream;
49 stream << file.rdbuf();
50 src = stream.str();
51 }
52
53 // Pass the file source to opengl
54 const char* cStrSrc = src.c_str();
55 int strLen = static_cast<int>(src.size());
56 glShaderSource(shader, 1, &cStrSrc, &strLen);
57
58 // compile the shader and deal with errors
59 glCompileShader(shader);
60 int successful;
61 glGetShaderiv(shader, GL_COMPILE_STATUS, &successful);
62
63 // deal with errors
64 if (!successful) {
65 int logLen = 0;
66 glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &logLen);
67 std::unique_ptr<char*> infoLog = std::make_unique<char*>(new char[logLen]);
68 glGetShaderInfoLog(shader, logLen, nullptr, *infoLog.get());
69 std::string strInfoLog(*infoLog.get(), logLen);
70 glDeleteShader(shader);
71 throw std::runtime_error("Unable to compile shader ("s + filePath + ") - \n" + strInfoLog);
72 }
73
74 return shader;
75}
76
83Shader::Shader(const char* vertShaderFile, const char* fragShaderFile) {
84
85 // Create the shaders
86 unsigned int vertexShader = createShader(GL_VERTEX_SHADER, vertShaderFile);
87 unsigned int fragmentShader = createShader(GL_FRAGMENT_SHADER, fragShaderFile);
88
89 // Create the program and link it with the shaders
90 program = glCreateProgram();
91 glAttachShader(program, vertexShader);
92 glAttachShader(program, fragmentShader);
93 glLinkProgram(program);
94
95 // Check if linking was successful
96 int success;
97 glGetProgramiv(program, GL_LINK_STATUS, &success);
98
99 // Deal with errors
100 if (!success) {
101 int logLen;
102 glGetProgramiv(program, GL_INFO_LOG_LENGTH, &logLen);
103 std::unique_ptr<char*> infoLog = std::make_unique<char*>(new char[logLen]);
104 glGetProgramInfoLog(program, logLen, nullptr, *infoLog.get());
105 std::string strInfoLog(*infoLog.get(), logLen);
106
107 glDeleteProgram(program);
108 glDeleteShader(vertexShader);
109 glDeleteShader(fragmentShader);
110 throw std::runtime_error("Unable to link shaders to program - " + strInfoLog);
111 }
112
113 glDeleteShader(vertexShader);
114 glDeleteShader(fragmentShader);
115 glUseProgram(program);
116}
117
122 glDeleteProgram(program);
123}
124
128void Shader::useProgram() const {
129 glUseProgram(program);
130}
131
137int Shader::getAttributeLocation(const char* name) const {
138 return glGetAttribLocation(program, name);
139}
140
146int Shader::getUniformLocation(const char* name) const {
147 return glGetUniformLocation(program, name);
148}
149
155void Shader::uniform(unsigned int location, const Vector2f& value) const {
156 useProgram();
157 glUniform2f(location, value.x, value.y);
158}
159
165void Shader::uniform(unsigned int location, const Vector3f& value) const {
166 useProgram();
167 glUniform3f(location, value.x, value.y, value.z);
168}
169
175void Shader::uniform(unsigned int location, const Vector4f& value) const {
176 useProgram();
177 glUniform4f(location, value.x, value.y, value.z, value.w);
178}
179
185void Shader::uniform(unsigned int location, const Matrix2f& value) const {
186 useProgram();
187 glUniformMatrix2fv(location, 1, GL_FALSE, value.data.data());
188}
189
195void Shader::uniform(unsigned int location, const Matrix3f& value) const {
196 useProgram();
197 glUniformMatrix3fv(location, 1, GL_FALSE, value.data.data());
198}
199
205void Shader::uniform(unsigned int location, const Matrix4f& value) const {
206 useProgram();
207 glUniformMatrix4fv(location, 1, GL_FALSE, value.data.data());
208}
209
215void Shader::uniform(unsigned int location, const Transform& value) const {
216 useProgram();
217 glUniformMatrix3fv(location, 1, GL_FALSE, value.data.data());
218}
219
225void Shader::uniform(unsigned int location, float value) const {
226 useProgram();
227 glUniform1f(location, value);
228}
229
235void Shader::uniform(unsigned int location, int value) const {
236 useProgram();
237 glUniform1i(location, value);
238}
239
245void Shader::uniform(unsigned int location, const std::vector<Vector2f>& value) const {
246 useProgram();
247 glUniform2fv(location, static_cast<GLsizei>(value.size()), reinterpret_cast<const float*>(value.data()));
248}
249
255void Shader::uniform(unsigned int location, const std::vector<Vector3f>& value) const {
256 useProgram();
257 glUniform3fv(location, static_cast<GLsizei>(value.size()), reinterpret_cast<const float*>(value.data()));
258}
259
265void Shader::uniform(unsigned int location, const std::vector<Vector4f>& value) const {
266 useProgram();
267 glUniform4fv(location, static_cast<GLsizei>(value.size()), reinterpret_cast<const float*>(value.data()));
268}
269
275void Shader::uniform(unsigned int location, const std::vector<Matrix2f>& value) const {
276 useProgram();
277 glUniformMatrix2fv(location, static_cast<GLsizei>(value.size()), GL_FALSE, value[0].data.data());
278}
279
285void Shader::uniform(unsigned int location, const std::vector<Matrix3f>& value) const {
286 useProgram();
287 glUniformMatrix3fv(location, static_cast<GLsizei>(value.size()), GL_FALSE, value[0].data.data());
288}
289
295void Shader::uniform(unsigned int location, const std::vector<Matrix4f>& value) const {
296 useProgram();
297 glUniformMatrix4fv(location, static_cast<GLsizei>(value.size()), GL_FALSE, value[0].data.data());
298}
299
305void Shader::uniform(unsigned int location, const std::vector<Transform>& value) const {
306 useProgram();
307 glUniformMatrix3fv(location, static_cast<GLsizei>(value.size()), GL_FALSE, value[0].data.data());
308}
309
315void Shader::uniform(unsigned int location, const std::vector<float>& value) const {
316 useProgram();
317 glUniform1fv(location, static_cast<GLsizei>(value.size()), value.data());
318}
319
326void Shader::uniform(unsigned int location, const Vector2f* ptr, size_t size) const {
327 useProgram();
328 glUniform2fv(location, static_cast<GLsizei>(size), reinterpret_cast<const float*>(ptr));
329}
330
337void Shader::uniform(unsigned int location, const Vector3f* ptr, size_t size) const {
338 useProgram();
339 glUniform3fv(location, static_cast<GLsizei>(size), reinterpret_cast<const float*>(ptr));
340}
341
348void Shader::uniform(unsigned int location, const Vector4f* ptr, size_t size) const {
349 useProgram();
350 glUniform4fv(location, static_cast<GLsizei>(size), reinterpret_cast<const float*>(ptr));
351}
352
359void Shader::uniform(unsigned int location, const Matrix2f* ptr, size_t size) const {
360 useProgram();
361 glUniformMatrix2fv(location, static_cast<GLsizei>(size), GL_FALSE, ptr->data.data());
362}
363
370void Shader::uniform(unsigned int location, const Matrix3f* ptr, size_t size) const {
371 useProgram();
372 glUniformMatrix3fv(location, static_cast<GLsizei>(size), GL_FALSE, ptr->data.data());
373}
374
381void Shader::uniform(unsigned int location, const Matrix4f* ptr, size_t size) const {
382 useProgram();
383 glUniformMatrix4fv(location, static_cast<GLsizei>(size), GL_FALSE, ptr->data.data());
384}
385
392void Shader::uniform(unsigned int location, const Transform* ptr, size_t size) const {
393 useProgram();
394 glUniformMatrix3fv(location, static_cast<GLsizei>(size), GL_FALSE, ptr->data.data());
395}
396
403void Shader::uniform(unsigned int location, const float* ptr, size_t size) const {
404 useProgram();
405 glUniform1fv(location, static_cast<GLsizei>(size), ptr);
406}
407
408#endif
std::array< T, w *h > data
Definition Matrix.hpp:55
void uniform(unsigned int location, const Vector2f &value) const
Definition Shader.cpp:155
int getAttributeLocation(const char *name) const
Definition Shader.cpp:137
Shader(const char *vertShaderFile, const char *fragShaderFile)
Definition Shader.cpp:83
unsigned int program
Definition Shader.hpp:34
int getUniformLocation(const char *name) const
Definition Shader.cpp:146
void useProgram() const
Definition Shader.cpp:128
unsigned int createShader(unsigned int type, const char *filePath)
Definition Shader.cpp:39