-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShaderManager.cpp
116 lines (91 loc) · 4.07 KB
/
ShaderManager.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
//
// Created by pavli on 30/09/2022.
//
#include "ShaderManager.h"
ShaderManager::ShaderManager(const char *vertex_shader_file, const char *fragment_shader_file) {
string vertexShaderString = loadFile(vertex_shader_file);
string fragmentShaderString = loadFile(fragment_shader_file);
if (vertexShaderString.empty()) printf("Empty Vertex Shader\n");
if (fragmentShaderString.empty()) printf("Empty Fragment Shader\n");
const char *vertex = vertexShaderString.c_str();
const char *fragment = fragmentShaderString.c_str();
this->add_shader(GL_VERTEX_SHADER, &vertex);
this->add_shader(GL_FRAGMENT_SHADER, &fragment);
this->link_shaders();
}
void ShaderManager::add_shader(GLenum shader_type, const char **source) const {
GLuint shader = glCreateShader(shader_type);
glShaderSource(shader, 1, source, nullptr);
glCompileShader(shader);
glAttachShader(this->shaderProgram, shader);
}
void ShaderManager::link_shaders() const {
glLinkProgram(this->shaderProgram);
GLint status;
glGetProgramiv(this->shaderProgram, GL_LINK_STATUS, &status);
if (status == GL_FALSE) {
GLint infoLogLength;
glGetProgramiv(this->shaderProgram, GL_INFO_LOG_LENGTH, &infoLogLength);
auto *strInfoLog = new GLchar[infoLogLength + 1];
glGetProgramInfoLog(this->shaderProgram, infoLogLength, nullptr, strInfoLog);
fprintf(stderr, "Linker failure: %s\n", strInfoLog);
delete[] strInfoLog;
exit(EXIT_FAILURE);
}
}
void ShaderManager::use_shaders() const {
glUseProgram(this->shaderProgram);
}
ShaderManager *ShaderManager::link_matrix_name(const char *matrix_name) {
glGetUniformLocation(this->shaderProgram, matrix_name);
return this;
}
void ShaderManager::set_uniform(const char *matrix_name, glm::mat4 matrix) const {
use_shaders();
glUniformMatrix4fv(glGetUniformLocation(shaderProgram, matrix_name), 1, GL_FALSE, &(matrix)[0][0]);
}
void ShaderManager::set_uniform(const char *vec_name, glm::vec3 vec) const {
use_shaders();
glUniform3fv(glGetUniformLocation(shaderProgram, vec_name), 1, &vec[0]);
}
void ShaderManager::set_uniform(const char *vec_name, int value) const {
use_shaders();
glUniform1i(glGetUniformLocation(shaderProgram, vec_name), value);
}
void ShaderManager::set_uniform(const char *vec_name, float value) const {
use_shaders();
glUniform1f(glGetUniformLocation(shaderProgram, vec_name), value);
}
void ShaderManager::set_uniform(const string &light_name, DirLight *light) const {
set_uniform((light_name + ".type").c_str(), light->get_type());
set_uniform((light_name + ".color").c_str(), light->get_color());
set_uniform((light_name + ".direction").c_str(), light->get_direction());
}
void ShaderManager::set_uniform(const string &light_name, PointLight *light) const {
set_uniform((light_name + ".type").c_str(), light->get_type());
set_uniform((light_name + ".color").c_str(), light->get_color());
set_uniform((light_name + ".position").c_str(), light->get_position());
}
void ShaderManager::set_uniform(const string &light_name, SpotLight *light) const {
set_uniform((light_name + ".type").c_str(), light->get_type());
set_uniform((light_name + ".color").c_str(), light->get_color());
set_uniform((light_name + ".position").c_str(), light->get_position());
set_uniform((light_name + ".direction").c_str(), light->get_direction());
set_uniform((light_name + ".cut_off").c_str(), light->get_cut_off());
}
void ShaderManager::update(Subject *subject, Event event) {
glm::mat4 matrix;
switch (event) {
case Event::VIEW_UPDATE:
matrix = ((Camera *) subject)->get_view();
set_uniform("spotlight.position", ((Camera *) subject)->get_position());
set_uniform("spotlight.direction", ((Camera *) subject)->get_direction());
set_uniform("view", matrix);
break;
case Event::WINDOW_SIZE_CHANGE:
case Event::ZOOM_UPDATE:
matrix = ((Window *) subject)->get_projection();
set_uniform("projection", matrix);
break;
}
}