-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShaderProgram.cpp
More file actions
155 lines (139 loc) · 4 KB
/
Copy pathShaderProgram.cpp
File metadata and controls
155 lines (139 loc) · 4 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
#include "ShaderProgram.hpp"
#include <sstream>
const std::string vertShaderSource =
"#version 410\n"
"in vec2 vPos;\n"
"void main() {\n"
" gl_Position = vec4(vPos, 0, 1);\n"
"}\n";
const std::string fragShaderSource =
"#version 410\n"
"uniform vec2 frag2Tex;\n"
"uniform vec2 offset;\n"
"uniform sampler2D texSampler;\n"
"uniform vec4 bgColor;\n"
"out vec4 color;\n"
"void main() {\n"
" vec2 tex = frag2Tex * (gl_FragCoord.xy + offset);\n"
" if(tex.x < 0.f || tex.y < 0.f ||\n"
" tex.x > 1.f || tex.y > 1.f) {\n"
" color = bgColor;\n"
" } else {\n"
" color = texture(texSampler, tex);\n"
" }\n"
"}\n";
const float verts[] = {
-1.f, 1.f, -1.f, -1.f, 1.f, 1.f, 1.f, 1.f, -1.f, -1.f, 1.f, -1.f
};
ShaderProgram::ShaderProgram()
:frag2TexLoc_(-1),
offsetLoc_(-1),
bgColorLoc_(-1)
{
GLuint fragShader = 0, vertShader = 0;
fragShader = compileShader(fragShaderSource, GL_FRAGMENT_SHADER);
vertShader = compileShader(vertShaderSource, GL_VERTEX_SHADER);
program_ = glCreateProgram();
glAttachShader(program_, vertShader);
glAttachShader(program_, fragShader);
glLinkProgram(program_);
GLint status;
glGetProgramiv(program_, GL_LINK_STATUS, &status);
if (status == GL_FALSE) //Linking failure
{
GLint logLength = 0;
glGetProgramiv(program_, GL_INFO_LOG_LENGTH, &logLength);
GLchar* errorLog = new GLchar[logLength];
glGetProgramInfoLog(program_, logLength, 0, errorLog);
std::string errorString(errorLog);
delete[] errorLog;
throw std::runtime_error("Error encountered when linking shaders: " +
errorString);
}
glDeleteShader(vertShader);
glDeleteShader(fragShader);
GLint loc = glGetUniformLocation(program_, "texSampler");
if(loc == -1) {
throw std::runtime_error("Unable to get uniform for texSampler");
}
glProgramUniform1i(program_, loc, 0);
GLint
vertAttrib = glGetAttribLocation(program_, "vPos"),
texAttrib = glGetAttribLocation(program_, "vTex");
glGenVertexArrays(1, &vao_);
glBindVertexArray(vao_);
glGenBuffers(1, &vertBuffer_);
glBindBuffer(GL_ARRAY_BUFFER, vertBuffer_);
glBufferData(GL_ARRAY_BUFFER, 12*sizeof(float), verts, GL_STATIC_DRAW);
glEnableVertexAttribArray(vertAttrib);
glVertexAttribPointer(vertAttrib, 2, GL_FLOAT, GL_FALSE, 0, NULL);
glEnableVertexAttribArray(texAttrib);
glVertexAttribPointer(texAttrib, 2, GL_FLOAT, GL_FALSE, 0, NULL);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
}
ShaderProgram::~ShaderProgram() throw()
{
glDeleteProgram(program_);
}
GLuint ShaderProgram::program()
{
return program_;
}
void ShaderProgram::setFrag2Tex(float x, float y)
{
if(frag2TexLoc_ == -1) {
frag2TexLoc_ = glGetUniformLocation(program_, "frag2Tex");
}
glProgramUniform2f(program_, frag2TexLoc_, x, y);
}
void ShaderProgram::setOffset(float x, float y)
{
if(offsetLoc_ == -1) {
offsetLoc_ = glGetUniformLocation(program_, "offset");
}
glProgramUniform2f(program_, offsetLoc_, x, y);
}
void ShaderProgram::setBgColor(float r, float g, float b, float a)
{
if(bgColorLoc_ == -1) {
bgColorLoc_ = glGetUniformLocation(program_, "bgColor");
}
glProgramUniform4f(program_, bgColorLoc_, r, g, b, a);
}
void ShaderProgram::use()
{
glUseProgram(program_);
}
void ShaderProgram::bindVertexArray()
{
glBindVertexArray(vao_);
}
void ShaderProgram::unbindVertexArray()
{
glBindVertexArray(0);
}
GLuint ShaderProgram::compileShader(const std::string &source, GLenum type)
{
GLuint shader = glCreateShader(type);
const char *cSource = source.c_str();
glShaderSource(shader, 1, &cSource, 0);
glCompileShader(shader);
GLint status;
glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
if (status == GL_FALSE) //Compilation failure.
{
GLint logLength = 0;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &logLength);
GLchar* errorLog = new GLchar[logLength];
glGetShaderInfoLog(shader, logLength, 0, errorLog);
std::string errorString(errorLog);
delete[] errorLog;
std::stringstream err;
err << "Failed to compile following shader source:\n"
<< source << std::endl << "With error:\n"
<< errorString;
throw std::runtime_error(err.str());
}
return shader;
}