-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshader.cpp
More file actions
177 lines (146 loc) · 4.41 KB
/
Copy pathshader.cpp
File metadata and controls
177 lines (146 loc) · 4.41 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
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
171
172
173
174
175
176
177
#include "shader.hpp"
#include "windows.h"
#include <fstream>
char errBufAscii[256];
char16_t errMsg[256];
std::string getSource(const char* path) {
std::string content;
std::ifstream fileStream(path, std::ios::in);
std::string line = "";
while (!fileStream.eof()) {
getline(fileStream, line);
content.append(line + "\n");
}
fileStream.close();
return content;
}
void copy_convert(const char* string, char16_t* emptyBuf) {
size_t i;
for (i = 0; string[i] != '\0'; ++i) {
emptyBuf[i] = (char16_t)string[i];
}
emptyBuf[i] = (char16_t)'\0';
}
///////////////////////////////////////////////////////////////////////////
bool shader::checkErrors(GLint targetEntity, GLuint target) {
switch (targetEntity) {
case GL_VERTEX_SHADER: {
GLint status;
glGetShaderiv(target, GL_COMPILE_STATUS, &status);
if (status != 1) {
glGetShaderInfoLog(target, 512, nullptr, errBufAscii);
copy_convert(errBufAscii, errMsg);
glfwTerminate();
MessageBox(NULL, (LPCWSTR)errMsg, (LPCWSTR)u"vertex shader compilation failed\n", MB_OK);
return true;
}
break;
}
case GL_FRAGMENT_SHADER: {
GLint status;
glGetShaderiv(target, GL_COMPILE_STATUS, &status);
if (status != 1) {
glGetShaderInfoLog(target, 512, nullptr, errBufAscii);
copy_convert(errBufAscii, errMsg);
glfwTerminate();
MessageBox(NULL, (LPCWSTR)errMsg, (LPCWSTR)u"fragment shader compilation failed\n", MB_OK);
return true;
}
break;
}
case GL_LINK_STATUS: {
GLint status;
glGetProgramiv(target, GL_LINK_STATUS, &status);
if (status != 1) {
glGetProgramInfoLog(target, 512, nullptr, errBufAscii);
copy_convert(errBufAscii, errMsg);
glfwTerminate();
MessageBox(NULL, (LPCWSTR)errMsg, (LPCWSTR)u"shaders linking failed\n", MB_OK);
return true;
}
break;
}
}
return false;
}
shader::shader() {
this->ID = (GLuint)((1llu << 32) - 1);
}
shader::shader(const char* vertPath, const char* fragPath) {
bool createStatus = false;
std::string vsource, fsource;
try {
vsource = getSource(vertPath);
fsource = getSource(fragPath);
}
catch (std::ifstream::failure& error) {
copy_convert(error.what(), errMsg);
MessageBox(nullptr, (LPCWSTR)errMsg, (LPCWSTR)u"failed to open Source file", MB_OK);
abort();
}
const char* vertexShaderSource = vsource.c_str();
const char* fragmentShaderSource = fsource.c_str();
GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(vertexShader, 1, &vertexShaderSource, nullptr);
glShaderSource(fragmentShader, 1, &fragmentShaderSource, nullptr);
glCompileShader(vertexShader);
createStatus = (createStatus || checkErrors(GL_VERTEX_SHADER, vertexShader));
glCompileShader(fragmentShader);
createStatus = (createStatus || checkErrors(GL_FRAGMENT_SHADER, fragmentShader));
GLuint newProgram = glCreateProgram();
glAttachShader(newProgram, vertexShader);
glAttachShader(newProgram, fragmentShader);
glLinkProgram(newProgram);
createStatus = (createStatus || checkErrors(GL_LINK_STATUS, newProgram));
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
if (createStatus) abort();
this->ID = newProgram;
}
void shader::shutdown() {
glDeleteProgram(this->ID);
}
GLuint shader::getID() {
return this->ID;
}
void shader::use() {
glUseProgram(this->ID);
}
void shader::setFloatValue(const char* name, float value) {
glUniform1f(glGetUniformLocation(this->ID, name), value);
}
void shader::setFloatPermanentValue(const char* name, float value) {
glProgramUniform1f(this->ID, glGetUniformLocation(this->ID, name), value);
}
void shader::setVec2Value(const char* name, const glm::vec2& value) {
glUniform2f(glGetUniformLocation(this->ID, name), value.x, value.y);
}
void shader::setVec2PermanentValue(const char* name, const glm::vec2& value) {
glProgramUniform2f(this->ID, glGetUniformLocation(this->ID, name), value.x, value.y);
}
void shader::setMat2x2Value(const char* name, const glm::mat2x2& value) {
glUniformMatrix2fv(
glGetUniformLocation(this->ID, name),
1,
GL_FALSE,
&value[0][0]
);
}
void shader::setMat4x4Value(const char* name, const glm::mat4x4& value) {
glUniformMatrix4fv(
glGetUniformLocation(this->ID, name),
1,
GL_FALSE,
&value[0][0]
);
}
void shader::setMat4x4PermanentValue(const char* name, const glm::mat4x4& value) {
glProgramUniformMatrix4fv(
this->ID,
glGetUniformLocation(this->ID, name),
1,
GL_FALSE,
&value[0][0]
);
}