-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpost_processor.cpp
More file actions
123 lines (112 loc) · 4.72 KB
/
Copy pathpost_processor.cpp
File metadata and controls
123 lines (112 loc) · 4.72 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
/*******************************************************************
** This code is part of Breakout.
**
** Breakout is free software: you can redistribute it and/or modify
** it under the terms of the CC BY 4.0 license as published by
** Creative Commons, either version 4 of the License, or (at your
** option) any later version.
******************************************************************/
#include "post_processor.h"
#include <iostream>
PostProcessor::PostProcessor(Shader shader, GLuint width, GLuint height)
: PostProcessingShader(shader), Texture(), Width(width), Height(height), Confuse(GL_FALSE), Chaos(GL_FALSE), Shake(GL_FALSE)
{
// Initialize renderbuffer/framebuffer object
glGenFramebuffers(1, &this->MSFBO);
glGenFramebuffers(1, &this->FBO);
glGenRenderbuffers(1, &this->RBO);
// Initialize renderbuffer storage with a multisampled color buffer (don't need a depth/stencil buffer)
glBindFramebuffer(GL_FRAMEBUFFER, this->MSFBO);
glBindRenderbuffer(GL_RENDERBUFFER, this->RBO);
glRenderbufferStorageMultisample(GL_RENDERBUFFER, 8, GL_RGB, width, height); // Allocate storage for render buffer object
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, this->RBO); // Attach MS render buffer object to framebuffer
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
std::cout << "ERROR::POSTPROCESSOR: Failed to initialize MSFBO" << std::endl;
// Also initialize the FBO/texture to blit multisampled color-buffer to; used for shader operations (for postprocessing effects)
glBindFramebuffer(GL_FRAMEBUFFER, this->FBO);
this->Texture.Generate(width, height, NULL);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, this->Texture.ID, 0); // Attach texture to framebuffer as its color attachment
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
std::cout << "ERROR::POSTPROCESSOR: Failed to initialize FBO" << std::endl;
glBindFramebuffer(GL_FRAMEBUFFER, 0);
// Initialize render data and uniforms
this->initRenderData();
this->PostProcessingShader.SetInteger("scene", 0, GL_TRUE);
GLfloat offset = 1.0f / 300.0f;
GLfloat offsets[9][2] = {
{ -offset, offset }, // top-left
{ 0.0f, offset }, // top-center
{ offset, offset }, // top-right
{ -offset, 0.0f }, // center-left
{ 0.0f, 0.0f }, // center-center
{ offset, 0.0f }, // center - right
{ -offset, -offset }, // bottom-left
{ 0.0f, -offset }, // bottom-center
{ offset, -offset } // bottom-right
};
glUniform2fv(glGetUniformLocation(this->PostProcessingShader.ID, "offsets"), 9, (GLfloat*)offsets);
GLint edge_kernel[9] = {
-1, -1, -1,
-1, 8, -1,
-1, -1, -1
};
glUniform1iv(glGetUniformLocation(this->PostProcessingShader.ID, "edge_kernel"), 9, edge_kernel);
GLfloat blur_kernel[9] = {
1.0 / 16, 2.0 / 16, 1.0 / 16,
2.0 / 16, 4.0 / 16, 2.0 / 16,
1.0 / 16, 2.0 / 16, 1.0 / 16
};
glUniform1fv(glGetUniformLocation(this->PostProcessingShader.ID, "blur_kernel"), 9, blur_kernel);
}
void PostProcessor::BeginRender()
{
glBindFramebuffer(GL_FRAMEBUFFER, this->MSFBO);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
}
void PostProcessor::EndRender()
{
// Now resolve multisampled color-buffer into intermediate FBO to store to texture
glBindFramebuffer(GL_READ_FRAMEBUFFER, this->MSFBO);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, this->FBO);
glBlitFramebuffer(0, 0, this->Width, this->Height, 0, 0, this->Width, this->Height, GL_COLOR_BUFFER_BIT, GL_NEAREST);
glBindFramebuffer(GL_FRAMEBUFFER, 0); // Binds both READ and WRITE framebuffer to default framebuffer
}
void PostProcessor::Render(GLfloat time)
{
// Set uniforms/options
this->PostProcessingShader.Use();
this->PostProcessingShader.SetFloat("time", time);
this->PostProcessingShader.SetInteger("confuse", this->Confuse);
this->PostProcessingShader.SetInteger("chaos", this->Chaos);
this->PostProcessingShader.SetInteger("shake", this->Shake);
// Render textured quad
glActiveTexture(GL_TEXTURE0);
this->Texture.Bind();
glBindVertexArray(this->VAO);
glDrawArrays(GL_TRIANGLES, 0, 6);
glBindVertexArray(0);
}
void PostProcessor::initRenderData()
{
// Configure VAO/VBO
GLuint VBO;
GLfloat vertices[] = {
// Pos // Tex
-1.0f, -1.0f, 0.0f, 0.0f,
1.0f, 1.0f, 1.0f, 1.0f,
-1.0f, 1.0f, 0.0f, 1.0f,
-1.0f, -1.0f, 0.0f, 0.0f,
1.0f, -1.0f, 1.0f, 0.0f,
1.0f, 1.0f, 1.0f, 1.0f
};
glGenVertexArrays(1, &this->VAO);
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glBindVertexArray(this->VAO);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(GL_FLOAT), (GLvoid*)0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
}