-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShader.cpp
117 lines (101 loc) · 3.79 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
//
// Created by Garret on 12/28/2022.
//
#include "Shader.h"
#include <glad/gl.h>
#include <fstream>
#include <sstream>
#include <iostream>
#define GL_CHECK do { GLenum gl_err = glGetError(); if (gl_err != 0) fprintf(stderr, "%s:%d GL error 0x%x returned \n", __FILE__, __LINE__, gl_err); } while (0) // Just error check
Shader::Shader(const std::string &vertexPath, const std::string &fragmentPath) :
vertexPath(vertexPath),
fragmentPath(fragmentPath),
id(0) {
using namespace std;
string vertexCode;
string fragmentCode;
ifstream vShaderFile;
ifstream fShaderFile;
// allows the ifstreams to throw exceptions
vShaderFile.exceptions(ifstream::failbit | ifstream::badbit);
fShaderFile.exceptions(ifstream::failbit | ifstream::badbit);
// attempt to read the sources
try {
vShaderFile.open(vertexPath);
fShaderFile.open(fragmentPath);
stringstream vShaderStream, fShaderStream;
vShaderStream << vShaderFile.rdbuf();
fShaderStream << fShaderFile.rdbuf();
vShaderFile.close();
fShaderFile.close();
vertexCode = vShaderStream.str();
fragmentCode = fShaderStream.str();
} catch (ifstream::failure &e) {
cerr << "Error: Shader file not successfully read: " << e.what() << endl;
}
GL_CHECK;
// compile sources
unsigned int vertexId = 0;
const char *vertexCodeSource = vertexCode.c_str();
vertexId = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexId, 1, &vertexCodeSource, NULL);
glCompileShader(vertexId);
GL_CHECK;
checkCompileErrors(vertexId, ShaderType::Vertex);
unsigned int fragmentId = 0;
const char *fragmentCodeSource = fragmentCode.c_str();
fragmentId = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentId, 1, &fragmentCodeSource, NULL);
glCompileShader(fragmentId);
checkCompileErrors(fragmentId, ShaderType::Fragment);
id = glCreateProgram();
glAttachShader(id, vertexId);
glAttachShader(id, fragmentId);
glLinkProgram(id);
GL_CHECK;
checkCompileErrors(id, ShaderType::Program);
GL_CHECK;
// once the shaders are linked, we can delete the individual shaders
GL_CHECK;
glDeleteShader(vertexId);
GL_CHECK;
glDeleteShader(fragmentId);
GL_CHECK;
}
void Shader::use() {
glUseProgram(id);
GL_CHECK;
}
void Shader::setBool(const std::string &name, bool value) const {
glUniform1i(glGetUniformLocation(id, name.c_str()), (int) value);
}
void Shader::setInt(const std::string &name, int value) const {
glUniform1i(glGetUniformLocation(id, name.c_str()), value);
}
void Shader::setFloat(const std::string &name, float value) const {
glUniform1f(glGetUniformLocation(id, name.c_str()), value);
}
void Shader::setMat4(const std::string &name, const glm::mat4 &mat) const {
glUniformMatrix4fv(glGetUniformLocation(id, name.c_str()), 1, GL_FALSE, &mat[0][0]);
}
void Shader::checkCompileErrors(unsigned int shader, Shader::ShaderType shaderType) {
int success;
char infoLog[1024];
if (shaderType != ShaderType::Program) {
glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
if (!success) {
glGetShaderInfoLog(shader, 1024, NULL, infoLog);
std::cerr << "Error: could not compile shader source: "
<< (shaderType == ShaderType::Fragment ? fragmentPath : vertexPath) << "\n"
<< infoLog << std::endl;
}
} else {
glGetProgramiv(shader, GL_LINK_STATUS, &success);
if (!success) {
glGetProgramInfoLog(shader, 1024, NULL, infoLog);
std::cerr << "Error: could not link shader program: "
<< vertexPath << " OR " << fragmentPath << "\n"
<< infoLog << std::endl;
}
}
}