-
Notifications
You must be signed in to change notification settings - Fork 0
/
GLSLShader.cpp
130 lines (104 loc) · 2.68 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
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
/* ==========================================================================
File GLSLShader.cpp
Author Rob ("Phantom")
This file is part of a example program written
for the book "More OpenGL Game Programming."
========================================================================== */
#include "GLSLShader.h"
#include <string>
#include <fstream>
#include <iostream>
using namespace std;
// Public part :
GLSLShader::GLSLShader(const std::string &filename, GLenum shaderType) : compiled_(false)
{
//char *source;
handle_ = glCreateShader(shaderType);
const char * source = readShader(filename);
glShaderSource(handle_, 1, static_cast<const GLchar**>(&source), NULL);
compile();
delete [] source;
}
GLSLShader::GLSLShader(GLenum shaderType)
{
if(shaderType == GL_VERTEX_SHADER || shaderType == GL_FRAGMENT_SHADER)
{
handle_ = glCreateShader(shaderType);
}
else
{
handle_ = shaderType;
}
}
GLSLShader::~GLSLShader()
{
glDeleteShader(handle_);
}
void GLSLShader::compile()
{
GLint compiled;
glCompileShader(handle_);
getParameter(GL_COMPILE_STATUS, &compiled);
if(!compiled)
{
compiled_ = false;
}
else
{
compiled_ = true;
}
}
bool GLSLShader::isCompiled() const
{
return compiled_;
}
void GLSLShader::getShaderLog(std::string &log) const
{
GLchar *debug;
GLint debugLength;
getParameter(GL_INFO_LOG_LENGTH, &debugLength);
debug = new GLchar[debugLength];
glGetShaderInfoLog(handle_, debugLength, &debugLength, debug);
//cout << debug;
log.append(debug,0,debugLength);
delete [] debug;
}
void GLSLShader::getShaderSource(std::string &shader) const
{
GLchar *source;
GLint sourcelen;
getParameter(GL_SHADER_SOURCE_LENGTH, &sourcelen);
source = new GLchar[sourcelen];
glGetShaderSource(handle_,sourcelen, &sourcelen, source);
shader.append(source,0,sourcelen);
delete []source;
}
GLuint GLSLShader::getHandle() const
{
return handle_;
}
void GLSLShader::getParameter(GLenum param, GLint *data) const
{
glGetShaderiv(handle_, param, data);
}
void GLSLShader::setShaderSource(std::string &code)
{
const char *source = code.c_str();
glShaderSource(handle_,1,static_cast<const GLchar**>(&source),NULL);
}
// Private part :
char *GLSLShader::readShader(const std::string &filename)
{
ifstream temp(filename.c_str());
int count = 0;
char *buf;
temp.seekg(0, ios::end);
count = temp.tellg();
buf = new char[count + 1];
memset(buf,0,count);
temp.seekg(0, ios::beg);
temp.read(buf, count);
buf[count] = 0;
temp.close();
return buf;
}