-
Notifications
You must be signed in to change notification settings - Fork 1
/
shader.cpp
170 lines (135 loc) · 3.2 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
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
160
161
162
163
164
165
166
167
168
169
170
#include "shader.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
Shader::Shader()
{
shaderProg = 0;
}
Shader::~Shader()
{
for (ShaderObjList::iterator it = shaderObjList.begin() ; it != shaderObjList.end() ; it++)
{
glDeleteShader(*it);
}
if (shaderProg != 0)
{
glDeleteProgram(shaderProg);
shaderProg = 0;
}
}
bool Shader::init()
{
shaderProg = glCreateProgram();
if (shaderProg == 0)
{
printf("Error creating shader program\n");
return false;
}
return true;
}
// Use this method to add shaders to the program. When finished - call finalize()
bool Shader::addShader(GLenum ShaderType, const char* pFilename)
{
string s;
if (!readFile(pFilename, s)) {
return false;
}
GLuint ShaderObj = glCreateShader(ShaderType);
if (ShaderObj == 0) {
printf("Error creating shader type %d\n", ShaderType);
return false;
}
// Save the shader object - will be deleted in the destructor
shaderObjList.push_back(ShaderObj);
const GLchar* p[1];
p[0] = s.c_str();
GLint Lengths[1] = { (GLint)s.size() };
glShaderSource(ShaderObj, 1, p, Lengths);
glCompileShader(ShaderObj);
GLint success;
glGetShaderiv(ShaderObj, GL_COMPILE_STATUS, &success);
if (!success)
{
GLchar InfoLog[1024];
glGetShaderInfoLog(ShaderObj, 1024, NULL, InfoLog);
printf("Error compiling '%s': '%s'\n", pFilename, InfoLog);
return false;
}
glAttachShader(shaderProg, ShaderObj);
return true;
}
// After all the shaders have been added to the program call this function
// to link and validate the program.
bool Shader::finalize(bool isValidate)
{
GLint Success = 0;
GLchar ErrorLog[1024] = { 0 };
glLinkProgram(shaderProg);
glGetProgramiv(shaderProg, GL_LINK_STATUS, &Success);
if (Success == 0) {
glGetProgramInfoLog(shaderProg, sizeof(ErrorLog), NULL, ErrorLog);
printf("Error linking shader program: '%s'\n", ErrorLog);
return false;
}
if (isValidate)
{
validate();
}
// Delete the intermediate shader objects that have been added to the program
for (ShaderObjList::iterator it = shaderObjList.begin() ; it != shaderObjList.end() ; it++) {
glDeleteShader(*it);
}
shaderObjList.clear();
return glGetError() == GL_NO_ERROR;
}
bool Shader::validate()
{
GLint Success = 0;
GLchar ErrorLog[1024] = { 0 };
glValidateProgram(shaderProg);
glGetProgramiv(shaderProg, GL_VALIDATE_STATUS, &Success);
if (!Success)
{
glGetProgramInfoLog(shaderProg, sizeof(ErrorLog), NULL, ErrorLog);
printf("Invalid shader program: '%s'\n", ErrorLog);
return false;
}
return true;
}
void Shader::enable()
{
glUseProgram(shaderProg);
}
GLint Shader::getUniformLocation(const char* pUniformName)
{
GLuint Location = glGetUniformLocation(shaderProg, pUniformName);
if (Location == INVALID_UNIFORM_LOCATION)
{
printf("Warning! Unable to get the location of uniform '%s'\n", pUniformName);
}
return Location;
}
GLint Shader::getProgramParam(GLint param)
{
GLint ret;
glGetProgramiv(shaderProg, param, &ret);
return ret;
}
bool Shader::readFile(const char* pFileName, string &outFile)
{
ifstream f(pFileName);
if (f.is_open())
{
string line;
while (getline(f, line))
{
outFile.append(line);
outFile.append("\n");
}
f.close();
return true;
}
return false;
}