-
Notifications
You must be signed in to change notification settings - Fork 0
/
Shader.h
153 lines (105 loc) · 2.76 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
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
/*
* Shader.h
*
* Created on: 2011-11-15
* Author: zapo
*/
#ifndef SHADER_H_
#define SHADER_H_
#include <GL/glew.h>
#include <fstream>
#include <sstream>
#include <iostream>
#include <SFML/Graphics.hpp>
namespace Bomber {
class Shader {
public:
Shader(std::string vertex_filename, std::string fragment_filename) {
vertex = LoadShader(GL_VERTEX_SHADER, vertex_filename);
fragment = LoadShader(GL_FRAGMENT_SHADER, fragment_filename);
program = glCreateProgram();
glAttachShader(program, vertex);
glAttachShader(program, fragment);
glLinkProgram(program);
enabled = false;
}
std::string LoadSource(std::string filename) {
std::string line;
std::stringstream ss;
std::fstream file(filename.data());
if(file.is_open()){
while (file.good()) {
getline(file, line);
ss << line << std::endl;
}
file.close();
} else {
std::cerr << "Unable to open file : " << filename << std::endl;
}
return ss.str();
}
GLuint LoadShader(GLenum type, std::string filename)
{
GLuint shader = 0;
GLsizei logsize = 0;
GLint compile_status = GL_TRUE;
std::string src = "";
shader = glCreateShader(type);
if(shader == 0) {
std::cerr << "Can't create the shader" << std::endl;
return 0;
}
src = LoadSource(filename);
if(src.empty()) {
glDeleteShader(shader);
return 0;
}
const char * sources = src.data();
glShaderSource(shader, 1, &sources, NULL);
glCompileShader(shader);
glGetShaderiv(shader, GL_COMPILE_STATUS, &compile_status);
if(compile_status != GL_TRUE) {
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &logsize);
char * p_log = new char[logsize + 1];
glGetShaderInfoLog(shader, logsize, &logsize, p_log);
std::cerr << "Can't compile the shader '" << filename << "':" << std::endl << p_log;
delete p_log;
glDeleteShader(shader);
return 0;
}
return shader;
}
virtual ~Shader() {
glDeleteShader(vertex);
glDeleteShader(fragment);
}
void Enable() {
enabled = true;
}
void Disable() {
enabled = false;
}
void Use() {
glUseProgram(program);
}
void SendUniformFloat(std::string var, GLfloat i) {
GLint id = glGetUniformLocation(program, var.data());
glUseProgram(program);
glUniform1f(id, i);
}
void SendUniformInt(std::string var, GLint i) {
GLint id = glGetUniformLocation(program, var.data());
glUseProgram(program);
glUniform1i(id, i);
}
void SendUniformVec(std::string var, const sf::Vector3f vec) {
int id = glGetUniformLocation(program, var.data());
glUseProgram(program);
glUniform3f(id, vec.x, vec.y, vec.z);
}
bool enabled;
private:
GLuint program, vertex, fragment;
};
}
#endif /* SHADER_H_ */