forked from kenygia/MooseEdit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GlShader.cpp
80 lines (65 loc) · 1.47 KB
/
GlShader.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
#include "GlShader.h"
bool GlShader::isValid() const
{
return m_isValid;
}
void GlShader::setIsValid(bool value)
{
m_isValid = value;
}
int GlShader::getShaderId() const
{
return shaderId;
}
void GlShader::setShaderId(int value)
{
shaderId = value;
}
bool GlShader::compile() {
shaderId = glCreateShader(shaderType);
GLint length = shaderSource.length();
const char *str = shaderSource.c_str();
glShaderSource(shaderId, 1, (const GLchar**)&str, &length);
glCompileShader(shaderId);
GLint shader_ok;
glGetShaderiv(shaderId, GL_COMPILE_STATUS, &shader_ok);
if (!shader_ok) {
_setLastError("Compile failed: ");
return false;
}
return true;
}
std::string GlShader::getLastError() const
{
return lastError;
}
GLuint GlShader::getProgram() const
{
return program;
}
void GlShader::setProgram(const GLuint &value)
{
program = value;
}
void GlShader::_setLastError(std::string errorPrefix) {
m_isValid = false;
GLint log_length;
char *log;
glGetShaderiv(shaderId, GL_INFO_LOG_LENGTH, &log_length);
log = new char[log_length];
glGetShaderInfoLog(shaderId, log_length, NULL, log);
lastError = errorPrefix + log;
delete[] log;
glDeleteShader(shaderId);
}
GlShader::GlShader(GLenum shaderType, std::istream &stream) {
this->shaderType = shaderType;
stream.seekg(0, std::ios_base::end);
int size = stream.tellg();
stream.seekg(0, std::ios_base::beg);
char *data = new char[size + 1];
data[size] = 0;
stream.read(data, size);
shaderSource = data;
delete[] data;
}