-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshader.cpp
More file actions
336 lines (250 loc) · 10.6 KB
/
shader.cpp
File metadata and controls
336 lines (250 loc) · 10.6 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
#include <iostream>
#include <string>
#include <glm/gtc/type_ptr.hpp>
#include "file_utils.h"
#include "shader.h"
namespace game {
Shader::Shader(void)
{
// Don't do work in the constructor, leave it for the Init() function
// Only initialize some variables with default values
shader_program_ = 0;
vbo_sprite_ = 0;
ebo_sprite_ = 0;
size_sprite_ = 0;
vbo_particles_ = 0;
ebo_particles_ = 0;
size_particles_ = 0;
}
void Shader::Init(const char *vertPath, const char *fragPath)
{
// Load shader program source code
// Vertex program
std::string vp = LoadTextFile(vertPath);
const char *source_vp = vp.c_str();
// Fragment program
std::string fp = LoadTextFile(fragPath);
const char *source_fp = fp.c_str();
// Create a shader from vertex program source code
GLuint vs = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vs, 1, &source_vp, NULL);
glCompileShader(vs);
// Check if shader compiled successfully
GLint status;
glGetShaderiv(vs, GL_COMPILE_STATUS, &status);
if (status != GL_TRUE) {
char buffer[512];
glGetShaderInfoLog(vs, 512, NULL, buffer);
throw(std::ios_base::failure(std::string("Error compiling vertex shader: ") + std::string(buffer)));
}
// Create a shader from the fragment program source code
GLuint fs = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fs, 1, &source_fp, NULL);
glCompileShader(fs);
// Check if shader compiled successfully
glGetShaderiv(fs, GL_COMPILE_STATUS, &status);
if (status != GL_TRUE) {
char buffer[512];
glGetShaderInfoLog(fs, 512, NULL, buffer);
throw(std::ios_base::failure(std::string("Error compiling fragment shader: ") + std::string(buffer)));
}
// Create a shader program linking both vertex and fragment shaders
// together
shader_program_ = glCreateProgram();
glAttachShader(shader_program_, vs);
glAttachShader(shader_program_, fs);
glLinkProgram(shader_program_);
// Check if shaders were linked successfully
glGetProgramiv(shader_program_, GL_LINK_STATUS, &status);
if (status != GL_TRUE) {
char buffer[512];
glGetShaderInfoLog(shader_program_, 512, NULL, buffer);
throw(std::ios_base::failure(std::string("Error linking shaders: ") + std::string(buffer)));
}
// Delete memory used by shaders, since they were already compiled
// and linked
glDeleteShader(vs);
glDeleteShader(fs);
// Set attributes for shaders
// Should be consistent with how we created the buffers for the square
GLint vertex_att = glGetAttribLocation(shader_program_, "vertex");
glVertexAttribPointer(vertex_att, 2, GL_FLOAT, GL_FALSE, 7 * sizeof(GLfloat), 0);
glEnableVertexAttribArray(vertex_att);
GLint color_att = glGetAttribLocation(shader_program_, "color");
glVertexAttribPointer(color_att, 3, GL_FLOAT, GL_FALSE, 7 * sizeof(GLfloat), (void *)(2 * sizeof(GLfloat)));
glEnableVertexAttribArray(color_att);
GLint tex_att = glGetAttribLocation(shader_program_, "uv");
glVertexAttribPointer(tex_att, 2, GL_FLOAT, GL_FALSE, 7 * sizeof(GLfloat), (void *)(5 * sizeof(GLfloat)));
glEnableVertexAttribArray(tex_att);
}
void Shader::CreateSprite(void) {
// The face of the square is defined by four vertices and two triangles
// Number of attributes for vertices and faces
// const int vertex_att = 7; // 7 attributes per vertex: 2D (or 3D) position (2), RGB color (3), 2D texture coordinates (2)
// const int face_att = 3; // Vertex indices (3)
GLfloat vertex[] = {
// Four vertices of a square
// Position Color Texture coordinates
-0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, // Top-left
0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, // Top-right
0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, // Bottom-right
-0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f // Bottom-left
};
// Two triangles referencing the vertices
GLuint face[] = {
0, 1, 2, // t1
2, 3, 0 // t2
};
// Create buffer for vertices
glGenBuffers(1, &vbo_sprite_);
glBindBuffer(GL_ARRAY_BUFFER, vbo_sprite_);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertex), vertex, GL_STATIC_DRAW);
// Create buffer for faces (index buffer)
glGenBuffers(1, &ebo_sprite_);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo_sprite_);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(face), face, GL_STATIC_DRAW);
// Set number of elements in array buffer (6 in this case)
size_sprite_ = sizeof(face) / sizeof(GLuint);
}
void Shader::CreateParticles(void)
{
// Each particle is a square with four vertices and two triangles
// Number of attributes for vertices and faces
const int vertex_attr = 7; // 7 attributes per vertex: 2D (or 3D) position (2), direction (2), 2D texture coordinates (2), time (1)
// const int face_att = 3; // Vertex indices (3)
GLfloat vertex[] = {
// Four vertices of a square
// Position Color Texture coordinates
-0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, // Top-left
0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, // Top-right
0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, // Bottom-right
-0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f // Bottom-left
};
// Two triangles referencing the vertices
GLuint face[] = {
0, 1, 2, // t1
2, 3, 0 // t2
};
// Initialize all the particle vertices
GLfloat particles[NUM_PARTICLES * vertex_attr];
float theta, r, tmod;
float pi = glm::pi<float>();
float two_pi = 2.0f * pi;
for (int i = 0; i < NUM_PARTICLES; i++) {
if (i % 4 == 0) {
//theta = (two_pi*(rand() % 1000) / 1000.0f);
theta = (2.0 * (rand() % 10000) / 10000.0f - 1.0f) * 0.13f + pi;
r = 0.0f + 0.8 * (rand() % 10000) / 10000.0f;
tmod = (rand() % 10000) / 10000.0f;
}
// Position
particles[i * vertex_attr + 0] = vertex[(i % 4) * 7 + 0];
particles[i * vertex_attr + 1] = vertex[(i % 4) * 7 + 1];
// Velocity
particles[i * vertex_attr + 2] = sin(theta) * r;
particles[i * vertex_attr + 3] = cos(theta) * r;
// Phase
particles[i * vertex_attr + 4] = tmod;
// Texture coordinate
particles[i * vertex_attr + 5] = vertex[(i % 4) * 7 + 5];
particles[i * vertex_attr + 6] = vertex[(i % 4) * 7 + 6];
}
// Initialize all the particle faces
GLuint manyfaces[NUM_PARTICLES * 6];
for (int i = 0; i < NUM_PARTICLES; i++) {
for (int j = 0; j < 6; j++) {
manyfaces[i * 6 + j] = face[j] + i * 4;
}
}
// Create buffer for vertices
glGenBuffers(1, &vbo_particles_);
glBindBuffer(GL_ARRAY_BUFFER, vbo_particles_);
glBufferData(GL_ARRAY_BUFFER, sizeof(particles), particles, GL_STATIC_DRAW);
// Create buffer for faces (index buffer)
glGenBuffers(1, &ebo_particles_);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo_particles_);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(manyfaces), manyfaces, GL_STATIC_DRAW);
// Set number of elements in array buffer
size_particles_ = sizeof(manyfaces) / sizeof(GLuint);
}
void Shader::SetSpriteAttributes(void)
{
// No blending
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glDisable(GL_BLEND);
// Bind buffers
glBindBuffer(GL_ARRAY_BUFFER, vbo_sprite_);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo_sprite_);
// Set attributes for shaders
// Should be consistent with how we created the buffers for the square
GLint vertex_att = glGetAttribLocation(shader_program_, "vertex");
glVertexAttribPointer(vertex_att, 2, GL_FLOAT, GL_FALSE, 7 * sizeof(GLfloat), 0);
glEnableVertexAttribArray(vertex_att);
GLint color_att = glGetAttribLocation(shader_program_, "color");
glVertexAttribPointer(color_att, 3, GL_FLOAT, GL_FALSE, 7 * sizeof(GLfloat), (void*)(2 * sizeof(GLfloat)));
glEnableVertexAttribArray(color_att);
GLint tex_att = glGetAttribLocation(shader_program_, "uv");
glVertexAttribPointer(tex_att, 2, GL_FLOAT, GL_FALSE, 7 * sizeof(GLfloat), (void*)(5 * sizeof(GLfloat)));
glEnableVertexAttribArray(tex_att);
}
void Shader::SetParticleAttributes(void) {
// Set blending
glDisable(GL_DEPTH_TEST);
glEnable(GL_BLEND);
glBlendFunc(GL_ONE, GL_ONE);
// Bind buffers
glBindBuffer(GL_ARRAY_BUFFER, vbo_particles_);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo_particles_);
// Set attributes for shaders
// Should be consistent with how we created the buffers for the particle elements
GLint vertex_att = glGetAttribLocation(shader_program_, "vertex");
glVertexAttribPointer(vertex_att, 2, GL_FLOAT, GL_FALSE, 7 * sizeof(GLfloat), 0);
glEnableVertexAttribArray(vertex_att);
GLint dir_att = glGetAttribLocation(shader_program_, "dir");
glVertexAttribPointer(dir_att, 2, GL_FLOAT, GL_FALSE, 7 * sizeof(GLfloat), (void*)(2 * sizeof(GLfloat)));
glEnableVertexAttribArray(dir_att);
GLint time_att = glGetAttribLocation(shader_program_, "t");
glVertexAttribPointer(time_att, 1, GL_FLOAT, GL_FALSE, 7 * sizeof(GLfloat), (void*)(4 * sizeof(GLfloat)));
glEnableVertexAttribArray(time_att);
GLint tex_att = glGetAttribLocation(shader_program_, "uv");
glVertexAttribPointer(tex_att, 2, GL_FLOAT, GL_FALSE, 7 * sizeof(GLfloat), (void*)(5 * sizeof(GLfloat)));
glEnableVertexAttribArray(tex_att);
}
void Shader::SetUniform1i(const GLchar *name, int value)
{
glUniform1i(glGetUniformLocation(shader_program_, name), value);
}
void Shader::SetUniform1f(const GLchar *name, float value)
{
glUniform1f(glGetUniformLocation(shader_program_, name), value);
}
void Shader::SetUniform2f(const GLchar *name, const glm::vec2 &vector)
{
glUniform2f(glGetUniformLocation(shader_program_, name), vector.x, vector.y);
}
void Shader::SetUniform3f(const GLchar *name, const glm::vec3 &vector)
{
glUniform3f(glGetUniformLocation(shader_program_, name), vector.x, vector.y, vector.z);
}
void Shader::SetUniform4f(const GLchar *name, const glm::vec4 &vector)
{
glUniform4f(glGetUniformLocation(shader_program_, name), vector.x, vector.y, vector.z, vector.w);
}
void Shader::SetUniformMat4(const GLchar *name, const glm::mat4 &matrix)
{
glUniformMatrix4fv(glGetUniformLocation(shader_program_, name), 1, GL_FALSE, glm::value_ptr(matrix));
}
Shader::~Shader()
{
glDeleteProgram(shader_program_);
}
void Shader::Enable()
{
glUseProgram(shader_program_);
}
void Shader::Disable()
{
glUseProgram(0);
}
} // namespace game