-
Notifications
You must be signed in to change notification settings - Fork 1
/
GLSLShader.cpp
102 lines (86 loc) · 3.08 KB
/
GLSLShader.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
// This is an open source non-commercial project. Dear PVS-Studio, please check it.
// PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
#include "GLSLShader.hpp"
#include <fstream>
#include <iostream>
void GLSLShader::DeleteShaderProgram() {
glDeleteProgram(_program);
}
void GLSLShader::LoadFromString(GLenum type, const std::string& source) {
GLuint shader = glCreateShader(type);
const char* ptmp = source.c_str();
glShaderSource(shader, 1, &ptmp, nullptr);
// check whether the shader loads fine
GLint status;
glCompileShader(shader);
glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
if(static_cast<GLboolean>(status) == GL_FALSE) {
GLint infoLogLength;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLogLength);
GLchar* infoLog = new GLchar[static_cast<std::size_t>(infoLogLength)];
glGetShaderInfoLog(shader, infoLogLength, nullptr, infoLog);
std::cerr << "Compile log: " << infoLog << std::endl;
delete[] infoLog;
}
_shaders[_totalShaders++] = shader;
}
void GLSLShader::CreateAndLinkProgram() {
_program = glCreateProgram();
if(_shaders[VERTEX_SHADER] != 0) {
glAttachShader(_program, _shaders[VERTEX_SHADER]);
}
if(_shaders[FRAGMENT_SHADER] != 0) {
glAttachShader(_program, _shaders[FRAGMENT_SHADER]);
}
if(_shaders[GEOMETRY_SHADER] != 0) {
glAttachShader(_program, _shaders[GEOMETRY_SHADER]);
}
// link and check whether the program links fine
GLint status;
glLinkProgram(_program);
glGetProgramiv(_program, GL_LINK_STATUS, &status);
if(static_cast<GLboolean>(status) == GL_FALSE) {
GLint infoLogLength;
glGetProgramiv(_program, GL_INFO_LOG_LENGTH, &infoLogLength);
GLchar* infoLog = new GLchar[static_cast<std::size_t>(infoLogLength)];
glGetProgramInfoLog(_program, infoLogLength, nullptr, infoLog);
std::cerr << "Link log: " << infoLog << std::endl;
delete[] infoLog;
}
glDeleteShader(_shaders[VERTEX_SHADER]);
glDeleteShader(_shaders[FRAGMENT_SHADER]);
glDeleteShader(_shaders[GEOMETRY_SHADER]);
}
void GLSLShader::Use() {
glUseProgram(_program);
}
void GLSLShader::UnUse() {
glUseProgram(0);
}
void GLSLShader::AddAttribute(const std::string& attribute) {
_attributeList[attribute] = static_cast<GLuint>(glGetAttribLocation(_program, attribute.c_str()));
}
// An indexer that returns the location of the attribute
GLuint GLSLShader::operator[](const std::string& attribute) {
return _attributeList[attribute];
}
void GLSLShader::AddUniform(const std::string& uniform) {
_uniformLocationList[uniform] = static_cast<GLuint>(glGetUniformLocation(_program, uniform.c_str()));
}
GLuint GLSLShader::operator()(const std::string& uniform) {
return _uniformLocationList[uniform];
}
void GLSLShader::LoadFromFile(GLenum whichShader, const std::string& filename) {
std::ifstream fp(filename);
if(fp.is_open()) {
std::string line, buffer;
while(getline(fp, line)) {
buffer.append(line);
buffer.append("\r\n");
}
// copy to source
LoadFromString(whichShader, buffer);
} else {
std::cerr << "Error loading shader: " << filename << std::endl;
}
}