-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshader.h
123 lines (109 loc) · 4.1 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
#ifndef SHADER_H
#define SHADER_H
#include <glad/glad.h>
#include <string>
#include <fstream>
#include <sstream>
#include <iostream>
class Shader{
public:
GLuint ID;
Shader(const char* vertexPath, const char* fragmentPath)
{
// 1. retrieve the vertex/fragment source code from filePath
std::string vertexCode;
std::string fragmentCode;
std::ifstream vShaderFile;
std::ifstream fShaderFile;
// ensure ifstream objects can throw exceptions:
vShaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
fShaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
try{
// open files
vShaderFile.open(vertexPath);
fShaderFile.open(fragmentPath);
std::stringstream vShaderStream, fShaderStream;
// read file's buffer contents into streams
vShaderStream << vShaderFile.rdbuf();
fShaderStream << fShaderFile.rdbuf();
// close file handlers
vShaderFile.close();
fShaderFile.close();
// convert stream into string
vertexCode = vShaderStream.str();
fragmentCode = fShaderStream.str();
}catch (std::ifstream::failure& e){
std::cout << "ERROR::SHADER::FILE_NOT_SUCCESFULLY_READ: " << e.what() << std::endl;
}
const char* vShaderCode = vertexCode.c_str();
const char* fShaderCode = fragmentCode.c_str();
GLuint vertex = compileShader(vertexCode, GL_VERTEX_SHADER);
GLuint fragment = compileShader(fragmentCode, GL_FRAGMENT_SHADER);
ID = compileProgram(vertex, fragment);
}
Shader(std::string vShaderCode, std::string fShaderCode)
{
GLuint vertex = compileShader(vShaderCode, GL_VERTEX_SHADER);
GLuint fragment = compileShader(fShaderCode, GL_FRAGMENT_SHADER);
ID = compileProgram(vertex, fragment);
}
void use() {
glUseProgram(ID);
}
void setInteger(const GLchar *name, GLint value) {
glUniform1i(glGetUniformLocation(ID, name), value);
}
void setFloat(const GLchar* name, GLfloat value) {
glUniform1f(glGetUniformLocation(ID, name), value);
}
void setVector3f(const GLchar* name, GLfloat x, GLfloat y, GLfloat z) {
glUniform3f(glGetUniformLocation(ID, name), x, y, z);
}
void setVector3f(const GLchar* name, const glm::vec3& value) {
glUniform3f(glGetUniformLocation(ID, name), value.x, value.y, value.z);
}
void setMatrix4(const GLchar* name, const glm::mat4& matrix) {
glUniformMatrix4fv(glGetUniformLocation(ID, name), 1, GL_FALSE, glm::value_ptr(matrix));
}
private:
GLuint compileShader(std::string shaderCode, GLenum shaderType)
{
GLuint shader = glCreateShader(shaderType);
const char* code = shaderCode.c_str();
glShaderSource(shader, 1, &code, NULL);
glCompileShader(shader);
GLchar infoLog[1024];
GLint success;
glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
if (!success)
{
glGetShaderInfoLog(shader, 1024, NULL, infoLog);
std::string t = "undetermined";
if (shaderType == GL_VERTEX_SHADER) {
t = "vertex shader";
}
else if (shaderType == GL_FRAGMENT_SHADER) {
t = "fragment shader";
}
std::cout << "ERROR::SHADER_COMPILATION_ERROR of the " << t << ": " << shaderType << infoLog << std::endl;
}
return shader;
}
GLuint compileProgram(GLuint vertexShader, GLuint fragmentShader)
{
GLuint programID = glCreateProgram();
glAttachShader(programID, vertexShader);
glAttachShader(programID, fragmentShader);
glLinkProgram(programID);
GLchar infoLog[1024];
GLint success;
glGetProgramiv(programID, GL_LINK_STATUS, &success);
if (!success)
{
glGetProgramInfoLog(programID, 1024, NULL, infoLog);
std::cout << "ERROR::PROGRAM_LINKING_ERROR: " << infoLog << std::endl;
}
return programID;
}
};
#endif