Skip to content

Commit b4fe9e7

Browse files
authored
Merge pull request #38 from Pilzschaf/tutorial42-simplepostprocessing
Simple post processing
2 parents a898540 + b05ffa1 commit b4fe9e7

4 files changed

Lines changed: 38 additions & 0 deletions

File tree

framebuffer.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ struct Framebuffer {
3535
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
3636
}
3737

38+
GLuint getTextureId() {
39+
return textures[0];
40+
}
41+
3842
void unbind() {
3943
glBindFramebuffer(GL_FRAMEBUFFER, 0);
4044
}

main.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ int main(int argc, char** argv) {
8888

8989
Shader fontShader("shaders_old/font.vs", "shaders_old/font.fs");
9090
Shader shader("shaders/basic.vs", "shaders/basic.fs");
91+
Shader postprocessingShader("shaders_old/postprocess.vs", "shaders_old/postprocess.fs");
9192
shader.bind();
9293
int directionLocation = GLCALL(glGetUniformLocation(shader.getShaderId(), "u_directional_light.direction"));
9394
glm::vec3 sunColor = glm::vec3(0.0f);
@@ -248,6 +249,7 @@ int main(int argc, char** argv) {
248249
camera.update();
249250

250251
framebuffer.bind();
252+
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
251253
shader.bind();
252254
model = glm::rotate(model, 1.0f*delta, glm::vec3(0, 1, 0));
253255
modelViewProj = camera.getViewProj() * model;
@@ -269,6 +271,14 @@ int main(int argc, char** argv) {
269271
shader.unbind();
270272
framebuffer.unbind();
271273

274+
// Postprocessing
275+
postprocessingShader.bind();
276+
GLCALL(glActiveTexture(GL_TEXTURE0));
277+
GLCALL(glBindTexture(GL_TEXTURE_2D, framebuffer.getTextureId()));
278+
GLCALL(glUniform1i(glGetUniformLocation(postprocessingShader.getShaderId(), "u_texture"), 0));
279+
glDrawArrays(GL_TRIANGLES, 0, 3);
280+
postprocessingShader.unbind();
281+
272282
fontShader.bind();
273283

274284
int w, h;

shaders_old/postprocess.fs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#version 130
2+
3+
varying vec2 v_tex_coords;
4+
5+
uniform sampler2D u_texture;
6+
7+
void main()
8+
{
9+
vec4 color = texture2D(u_texture, v_tex_coords);
10+
float average = (color.r + color.g + color.b) / 3.0f;
11+
gl_FragColor = vec4(average, average, average, color.a);
12+
}

shaders_old/postprocess.vs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#version 130
2+
3+
varying vec2 v_tex_coords;
4+
5+
void main()
6+
{
7+
float x = -1.0 + float((gl_VertexID & 1) << 2);
8+
float y = -1.0 + float((gl_VertexID & 2) << 1);
9+
v_tex_coords.x = (x+1.0)*0.5;
10+
v_tex_coords.y = (y+1.0)*0.5;
11+
gl_Position = vec4(x, y, 0, 1);
12+
}

0 commit comments

Comments
 (0)