forked from bwrrp/glblaat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGLShader.cpp
97 lines (82 loc) · 2.25 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Copyright (c) 2009 Stef Busking
// Distributed under the terms of the MIT License.
#include "GLShader.h"
#include "GLUtility.h"
using namespace std;
// ----------------------------------------------------------------------------
GLShader::GLShader(GLenum type)
: id(0), type(type)
{
id = glCreateShader(type);
#ifndef NDEBUG
GLUtility::CheckOpenGLError("GLShader: glCreateShader()");
#endif
}
// ----------------------------------------------------------------------------
GLShader::~GLShader()
{
glDeleteShader(id);
#ifndef NDEBUG
GLUtility::CheckOpenGLError("GLShader: glDeleteShader()");
#endif
}
// ----------------------------------------------------------------------------
string GLShader::GetSource() const
{
return source;
}
// ----------------------------------------------------------------------------
bool GLShader::SetSource(const string &source)
{
this->source = source;
// Need a char** for glShaderSource
const char *s = source.c_str();
glShaderSource(id, 1, &s, 0);
#ifndef NDEBUG
GLUtility::CheckOpenGLError("GLShader: glShaderSource()");
#endif
// Compile the shader
glCompileShader(id);
#ifndef NDEBUG
GLUtility::CheckOpenGLError("GLShader: glCompileShader()");
#endif
return IsOk();
}
// ----------------------------------------------------------------------------
bool GLShader::IsOk() const
{
GLint ok = GL_FALSE;
glGetShaderiv(id, GL_COMPILE_STATUS, &ok);
#ifndef NDEBUG
GLUtility::CheckOpenGLError("GLShader: IsOk()");
#endif
return (ok == GL_TRUE);
}
// ----------------------------------------------------------------------------
string GLShader::GetInfoLog() const
{
int loglength = 0;
// Get the length of the log
glGetShaderiv(id, GL_INFO_LOG_LENGTH, &loglength);
#ifndef NDEBUG
GLUtility::CheckOpenGLError("GLShader: GetInfoLog - getting log length");
#endif
if (loglength > 0)
{
// Allocate a buffer and get the log
int dummy;
char *buffer = new char[loglength];
glGetShaderInfoLog(id, loglength, &dummy, buffer);
#ifndef NDEBUG
GLUtility::CheckOpenGLError("GLShader: glGetShaderInfoLog()");
#endif
string infolog(buffer);
delete[] buffer;
return infolog;
}
else
{
// No log...
return string("");
}
}