-
Notifications
You must be signed in to change notification settings - Fork 0
/
Shader.h
109 lines (99 loc) · 3.58 KB
/
Shader.h
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
#ifndef SHADER_H
#define SHADER_H
#include <GL/glew.h>
#include <string>
#include <iostream>
#include <sstream>
#include <fstream>
class Shader {
public:
GLuint program;
Shader(const GLchar* vertex_shader_path, const GLchar* fragment_shader_path, const GLchar* geometry_shader_path = nullptr) {
std::string vertex_code;
std::string fragment_code;
std::string geometry_code;
std::ifstream v_shader_file;
std::ifstream f_shader_file;
std::ifstream g_shader_file;
v_shader_file.exceptions(std::ifstream::failbit | std::ifstream::badbit);
f_shader_file.exceptions(std::ifstream::failbit | std::ifstream::badbit);
g_shader_file.exceptions(std::ifstream::failbit | std::ifstream::badbit);
try{
v_shader_file.open(vertex_shader_path);
f_shader_file.open(fragment_shader_path);
std::stringstream v_shader_stream, f_shader_stream;
v_shader_stream << v_shader_file.rdbuf();
f_shader_stream << f_shader_file.rdbuf();
v_shader_file.close();
f_shader_file.close();
vertex_code = v_shader_stream.str();
fragment_code = f_shader_stream.str();
if (geometry_shader_path != nullptr) {
g_shader_file.open(geometry_shader_path);
std::stringstream g_shader_stream;
g_shader_stream << g_shader_file.rdbuf();
g_shader_file.close();
geometry_code = g_shader_stream.str();
}
}
catch (const std::exception&){
std::cout << "Error: Shader not read\n";
}
const char* v_shader_code = vertex_code.c_str();
const char* f_shader_code = fragment_code.c_str();
GLuint vertex, fragment,geometry;
GLchar info_log[512];
vertex = glCreateShader(GL_VERTEX_SHADER);
fragment = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(vertex, 1, &v_shader_code, NULL);
glShaderSource(fragment, 1, &f_shader_code, NULL);
glCompileShader(vertex);
glCompileShader(fragment);
check_compile_error(vertex, "VERTEX");
check_compile_error(fragment, "FRAGMENT");
if (geometry_shader_path != nullptr) {
const char* g_shader_code = geometry_code.c_str();
geometry = glCreateShader(GL_GEOMETRY_SHADER);
glShaderSource(geometry, 1, &g_shader_code, NULL);
glCompileShader(geometry);
check_compile_error(geometry, "GEOMETRY");
}
this->program = glCreateProgram();
glAttachShader(this->program, vertex);
glAttachShader(this->program, fragment);
if (geometry_shader_path != nullptr) {
glAttachShader(this->program, geometry);
}
glLinkProgram(this->program);
check_compile_error(this->program, "PROGRAM");
glDeleteShader(vertex);
glDeleteShader(fragment);
if (geometry_shader_path != nullptr) {
glDeleteShader(geometry);
}
}
void use() { glUseProgram(this->program); }
private:
void check_compile_error(GLuint shader, std::string type) {
GLint success;
GLchar info_log[1024];
if (type == "PROGRAM") {
glGetProgramiv(shader, GL_LINK_STATUS, &success);
if (!success) {
glGetShaderInfoLog(shader, 1024, NULL, info_log);
std::cout << "| Error:: PROGRAM-LINKING-ERROR of type: " << type << "|\n" << info_log << "\n| -- --------------------------------------------------- -- |\n";
}
}
else if (type == "VERTEX" || type == "FRAGMENT" || type == "GEOMETRY") {
glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
if (!success) {
glGetShaderInfoLog(shader, 1024, NULL, info_log);
std::cout << "| Error:: SHADER-COMPILATION-ERROR of type: " << type << "|\n" << info_log << "\n| -- --------------------------------------------------- -- |\n";
}
}
else {
std::cout << "Error: incorrect input type\n";
}
}
};
#endif