-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShader.hpp
More file actions
160 lines (127 loc) · 4.74 KB
/
Copy pathShader.hpp
File metadata and controls
160 lines (127 loc) · 4.74 KB
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#pragma once
#include <string>
#include <vector>
#include <iostream>
#include <fstream>
#include <string>
#include <GL/glew.h>
#include <glm/common.hpp>
#include <glm/matrix.hpp>
#include <GLFW/glfw3.h>
using namespace std;
class Shader
{
public:
Shader(string vertShderPath = "vertexShader.glsl", string fragShaderPath = "fragmentShader.glsl") {
this->load(vertShderPath.c_str(), fragShaderPath.c_str());
};
~Shader() {};
size_t load(const char * vertex_file_path, const char * fragment_file_path);
void set_uniform(const std::string& name, const glm::mat4 matrix) {
GLint location = glGetUniformLocation(shaderId, name.c_str());
if (location != -1) {
glUniformMatrix4fv(location, 1, false, &matrix[0][0]);
}
}
void set_uniform(const std::string& name, const vector<glm::vec3>) {
}
void set_uniform(const std::string& name, const bool value) {
GLint location = glGetUniformLocation(shaderId, name.c_str());
if (location != -1) {
glUniform1i(location, (int)value);
}
}
void set_uniform(const std::string& name, const int value) {
GLint location = glGetUniformLocation(shaderId, name.c_str());
if (location != -1) {
glUniform1i(location, value);
}
}
void set_uniform(const std::string& name, const float value) {
GLint location = glGetUniformLocation(shaderId, name.c_str());
if (location != -1) {
glUniform1i(location, value);
}
}
GLuint shaderId;
};
size_t Shader::load (const char * vertex_file_path, const char * fragment_file_path) {
// Create the shaders
GLuint vertex_shader_id = glCreateShader(GL_VERTEX_SHADER);
GLuint fragment_shader_id = glCreateShader(GL_FRAGMENT_SHADER);
// Read the Vertex Shader code from the file
std::string vertex_shader_code;
std::ifstream vertex_shader_stream(vertex_file_path, std::ios::in);
if (vertex_shader_stream.is_open()) {
std::string Line = "";
while (getline(vertex_shader_stream, Line))
vertex_shader_code += "\n" + Line;
vertex_shader_stream.close();
}
else {
cout << "Cannot to open " << vertex_file_path << ".Are you in the right directory ?" << endl;
return false;
}
// Read the Fragment Shader code from the file
std::string fragment_shader_code;
std::ifstream fragment_shader_stream(fragment_file_path, std::ios::in);
if (fragment_shader_stream.is_open()) {
std::string Line = "";
while (getline(fragment_shader_stream, Line))
fragment_shader_code += "\n" + Line;
fragment_shader_stream.close();
}
GLint result = GL_FALSE;
// Compile Vertex Shader
cout << "Compiling shader : " << vertex_file_path << endl;
char const * vertex_shader_pointer = vertex_shader_code.c_str();
glShaderSource(vertex_shader_id, 1, &vertex_shader_pointer, NULL);
glCompileShader(vertex_shader_id);
// Check Vertex Shader
glGetShaderiv(vertex_shader_id, GL_COMPILE_STATUS, &result);
if (result == 0) {
int info_log_length;
glGetShaderiv(vertex_shader_id, GL_INFO_LOG_LENGTH, &info_log_length);
char * vertex_shader_error_msg = new char[info_log_length + 1];
glGetShaderInfoLog(vertex_shader_id, info_log_length, NULL, &vertex_shader_error_msg[0]);
cout << string(vertex_shader_error_msg) << endl;
delete[] vertex_shader_error_msg;
}
// Compile Fragment Shader
cout << "Compiling shader : " << fragment_file_path << endl;
char const * FragmentSourcePointer = fragment_shader_code.c_str();
glShaderSource(fragment_shader_id, 1, &FragmentSourcePointer, NULL);
glCompileShader(fragment_shader_id);
// Check Fragment Shader
glGetShaderiv(fragment_shader_id, GL_COMPILE_STATUS, &result);
if (result == 0) {
int info_log_length;
glGetShaderiv(fragment_shader_id, GL_INFO_LOG_LENGTH, &info_log_length);
char * fragment_shader_error_msg = new char[info_log_length + 1];
glGetShaderInfoLog(fragment_shader_id, info_log_length, NULL, &fragment_shader_error_msg[0]);
cout << string(fragment_shader_error_msg) << endl;
delete[] fragment_shader_error_msg;
}
// Link the program
cout << "Linking program" << endl;
size_t shader_program_id = glCreateProgram();
glAttachShader(shader_program_id, vertex_shader_id);
glAttachShader(shader_program_id, fragment_shader_id);
glLinkProgram(shader_program_id);
// Check the program
glGetProgramiv(shader_program_id, GL_LINK_STATUS, &result);
if (result == 0) {
int info_log_length;
glGetShaderiv(fragment_shader_id, GL_INFO_LOG_LENGTH, &info_log_length);
char * link_error_msg = new char[info_log_length + 1];
glGetShaderInfoLog(fragment_shader_id, info_log_length, NULL, &link_error_msg[0]);
cout << string(link_error_msg) << endl;
delete[] link_error_msg;
}
glDetachShader(shader_program_id, vertex_shader_id);
glDetachShader(shader_program_id, fragment_shader_id);
glDeleteShader(vertex_shader_id);
glDeleteShader(fragment_shader_id);
shaderId = shader_program_id;
return shader_program_id;
}