-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShaderProgram.cpp
More file actions
127 lines (113 loc) · 3.79 KB
/
ShaderProgram.cpp
File metadata and controls
127 lines (113 loc) · 3.79 KB
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
#include "stdafx.h"
#include "ShaderProgram.h"
using namespace std;
namespace XKS {
void ShaderProgram::PrintInfoLog(GLuint shaderID) {
int infoLogLen = 0;
int charsWritten = 0;
GLchar *infoLog;
glGetShaderiv(shaderID, GL_INFO_LOG_LENGTH, &infoLogLen);
if (infoLogLen > 0) {
infoLog = new GLchar[infoLogLen];
// error check for fail to allocate memory omitted
glGetShaderInfoLog(shaderID, infoLogLen, &charsWritten, infoLog);
cout << "InfoLog : " << endl << infoLog << endl;
delete[] infoLog;
}
}
string ShaderProgram::FileContent(const char *filename) {
ifstream in;
in.exceptions(ios::badbit | ios::failbit);
in.open(filename, ios::in | ios::binary);
string content;
in.seekg(0, std::ios::end);
content.resize(unsigned(in.tellg()));
in.seekg(0, std::ios::beg);
in.read(&content[0], content.size());
in.close();
return content;
}
GLuint ShaderProgram::CompileShader(GLenum shaderType, const char* filename) {
GLuint shaderID = glCreateShader(shaderType);
if (shaderID == 0)
throw exception("Blad glCreateShader!");
string filecontent = FileContent(filename);
const char *fc = filecontent.c_str();
glShaderSource(shaderID, 1, &fc, NULL);
glCompileShader(shaderID);
GLint compiled;
glGetShaderiv(shaderID, GL_COMPILE_STATUS, &compiled);
if (compiled == 0) {
PrintInfoLog(shaderID);
throw exception("Blad kompilacji shadera!");
}
return shaderID;
}
ShaderProgram::ShaderProgram(const char *vsFile, const char *fsFile)
: m_vertexShaderID(0),
m_fragmentShaderID(0),
m_programID(0) {
m_vertexShaderID = CompileShader(GL_VERTEX_SHADER, vsFile);
m_fragmentShaderID = CompileShader(GL_FRAGMENT_SHADER, fsFile);
m_programID = glCreateProgram();
if (m_programID == 0)
throw exception("Blad glCreateProgram!");
GLenum err = glGetError();
glAttachShader(m_programID, m_vertexShaderID);
glAttachShader(m_programID, m_fragmentShaderID);
glLinkProgram(m_programID);
err = glGetError();
if (err != GL_NO_ERROR)
throw exception("Blad programu shaderow!");
}
void ShaderProgram::Attach() {
GLenum err = glGetError();
glUseProgram(m_programID);
err = glGetError();
if (err != GL_NO_ERROR)
throw exception("Blad glUseProgram!");
}
void ShaderProgram::Destroy() {
if (m_programID) {
GLuint currentProgram;
glGetIntegerv(GL_CURRENT_PROGRAM, reinterpret_cast<GLint*>(¤tProgram));
if (currentProgram == m_programID)
glUseProgram(0);
glDetachShader(m_programID, m_vertexShaderID);
glDetachShader(m_programID, m_fragmentShaderID);
glDeleteProgram(m_programID);
m_programID = 0;
}
if (m_fragmentShaderID) {
glDeleteShader(m_fragmentShaderID);
m_fragmentShaderID = 0;
}
if (m_vertexShaderID) {
glDeleteShader(m_vertexShaderID);
m_vertexShaderID = 0;
}
}
ShaderProgram::~ShaderProgram() {
Destroy();
}
GLint ShaderProgram::GetUniform(const char *uniformName) {
m_mtx.lock();
GLenum err = glGetError();
GLint loc = glGetUniformLocation(m_programID, uniformName);
err = glGetError();
if (loc < 0 || err != GL_NO_ERROR)
throw exception("Blad glGetUniformLocation!");
m_mtx.unlock();
return loc;
}
GLint ShaderProgram::GetAttrib(const char *attribName) {
m_mtx.lock();
GLenum err = glGetError();
GLint loc = glGetAttribLocation(m_programID, attribName);
err = glGetError();
if (loc < 0 || err != GL_NO_ERROR)
throw exception("Blad glGetAttribLocation!");
m_mtx.unlock();
return loc;
}
}