-
Notifications
You must be signed in to change notification settings - Fork 2
/
shader.cpp
137 lines (104 loc) · 3.36 KB
/
shader.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include "shader.h"
#include <QOpenGLContext>
#include <filesystem>
#include <fstream>
#include <memory>
Shader::Shader(QString path, ShaderType type)
: type_(type), file_path_(path.toStdString()) {
gl_functions_ = QOpenGLContext::currentContext()->functions();
switch (type_) {
case ShaderType::kVertex:
shader_id_ = gl_functions_->glCreateShader(GL_VERTEX_SHADER);
break;
case ShaderType::kFragment:
shader_id_ = gl_functions_->glCreateShader(GL_FRAGMENT_SHADER);
break;
}
}
Shader::~Shader() {
gl_functions_->glDeleteShader(shader_id_);
gl_functions_ = nullptr;
}
bool Shader::Compile() {
bool success = false;
auto src = ReadSource_();
GLchar const* shader_src = src->c_str();
gl_functions_->glShaderSource(shader_id_, 1, &shader_src, nullptr);
gl_functions_->glCompileShader(shader_id_);
int status;
gl_functions_->glGetShaderiv(shader_id_, GL_COMPILE_STATUS, &status);
if (!status) {
log_ = FetchLog_();
} else {
success = true;
}
return success;
}
std::unique_ptr<std::string> Shader::ReadSource_() {
std::unique_ptr<std::string> src = nullptr;
if (std::filesystem::exists(file_path_)) {
std::ifstream ifs(file_path_);
if (ifs.is_open()) {
src = std::make_unique<std::string>(
std::istreambuf_iterator<char>(ifs),
std::istreambuf_iterator<char>()
);
}
}
return std::move(src);
}
std::unique_ptr<std::string> Shader::FetchLog_() const {
std::unique_ptr<std::string> log = nullptr;
GLsizei log_length = 0;
gl_functions_->glGetShaderiv(shader_id_, GL_INFO_LOG_LENGTH, &log_length);
if (log_length > 0) {
char* raw_log = new char[log_length];
gl_functions_->glGetShaderInfoLog(
shader_id_, log_length, nullptr, raw_log
);
log = std::make_unique<std::string>(raw_log);
delete[] raw_log;
raw_log = nullptr;
}
return std::move(log);
}
ShaderProgram::ShaderProgram(
std::shared_ptr<Shader> vert, std::shared_ptr<Shader> frag
)
: vertex_(vert), fragment_(frag) {
gl_functions_ = QOpenGLContext::currentContext()->functions();
program_id_ = gl_functions_->glCreateProgram();
}
ShaderProgram::~ShaderProgram() {
gl_functions_->glDeleteProgram(program_id_);
gl_functions_ = nullptr;
}
bool ShaderProgram::Link() {
bool success = false;
gl_functions_->glAttachShader(program_id_, vertex_->Id());
gl_functions_->glAttachShader(program_id_, fragment_->Id());
gl_functions_->glLinkProgram(program_id_);
int status;
gl_functions_->glGetProgramiv(program_id_, GL_LINK_STATUS, &status);
if (!status) {
log_ = FetchLog_();
} else {
success = true;
}
return success;
}
std::unique_ptr<std::string> ShaderProgram::FetchLog_() const {
std::unique_ptr<std::string> log = nullptr;
GLsizei log_length = 0;
gl_functions_->glGetProgramiv(program_id_, GL_INFO_LOG_LENGTH, &log_length);
if (log_length > 0) {
char* raw_log = new char[log_length];
gl_functions_->glGetProgramInfoLog(
program_id_, log_length, nullptr, raw_log
);
log = std::make_unique<std::string>(raw_log);
delete[] raw_log;
raw_log = nullptr;
}
return std::move(log);
}