improved bloom system

This commit is contained in:
Harrison Deng 2018-09-03 19:31:16 -05:00
parent d6b39c8d73
commit 8fd8308d47
7 changed files with 96 additions and 53 deletions

View File

@ -17,6 +17,6 @@ void main() {
if (brightness > 0.7) { if (brightness > 0.7) {
gl_FragColor = color; gl_FragColor = color;
} else { } else {
gl_FragColor = vec4(0.0); gl_FragColor = vec4(0.0, 0.0, 0.0, 0.0);
} }
} }

View File

@ -1,34 +1,47 @@
#version 330 core varying vec4 vColor;
#ifdef GL_ES
#define LOWP lowp
precision mediump float;
#else
#define LOWP
#endif
varying LOWP vec4 vColor;
varying vec2 vTexCoord; varying vec2 vTexCoord;
uniform int horizontal;
uniform sampler2D u_texture; uniform sampler2D u_texture;
uniform float weight[5] = float[] (0.227027, 0.1945946, 0.1216216, 0.054054, 0.016216); uniform vec2 resolution;
uniform float radius;
uniform int pass;
void main() { void main() {
vec2 tex_offset = 1.0 / textureSize(u_texture, 0); //this will be our RGBA sum
vec4 color = texture(u_texture, vTexCoord); vec4 sum = vec4(0.0);
vec3 result = color.rgb * weight[0];
if (horizontal == 1) { //our original texcoord for this fragment
for (int i = 1; i < 5; ++i) { vec2 tc = vTexCoord;
result += texture(u_texture, vTexCoord + vec2(tex_offset.x * i, 0.0)).rgb * weight[i];
result += texture(u_texture, vTexCoord - vec2(tex_offset.x * i, 0.0)).rgb * weight[i]; //the amount to blur, i.e. how far off center to sample from
} //1.0 -> blur by one pixel
} else { //2.0 -> blur by two pixels, etc.
for (int i = 1; i < 5; ++i) { float blur = radius/resolution.y;
result += texture(u_texture, vTexCoord + vec2(0.0, tex_offset.y * i)).rgb * weight[i];
result += texture(u_texture, vTexCoord - vec2(0.0, tex_offset.y * i)).rgb * weight[i]; //the direction of our blur
} //(1.0, 0.0) -> x-axis blur
//(0.0, 1.0) -> y-axis blur
float hstep = 1.0;
float vstep = 0.0;
if (pass == 1) {
hstep = 0.0;
vstep = 1.0;
} }
gl_FragColor = vec4(result, color.a); sum += texture2D(u_texture, vec2(tc.x - 5.0*blur*hstep, tc.y - 5.0*blur*vstep)) * 0.014374;
sum += texture2D(u_texture, vec2(tc.x - 4.0*blur*hstep, tc.y - 4.0*blur*vstep)) * 0.035855;
sum += texture2D(u_texture, vec2(tc.x - 3.0*blur*hstep, tc.y - 3.0*blur*vstep)) * 0.072994;
sum += texture2D(u_texture, vec2(tc.x - 2.0*blur*hstep, tc.y - 2.0*blur*vstep)) * 0.121281;
sum += texture2D(u_texture, vec2(tc.x - 1.0*blur*hstep, tc.y - 1.0*blur*vstep)) * 0.164472;
sum += texture2D(u_texture, vec2(tc.x, tc.y)) * 0.182049;
sum += texture2D(u_texture, vec2(tc.x + 1.0*blur*hstep, tc.y + 1.0*blur*vstep)) * 0.164472;
sum += texture2D(u_texture, vec2(tc.x + 2.0*blur*hstep, tc.y + 2.0*blur*vstep)) * 0.121281;
sum += texture2D(u_texture, vec2(tc.x + 3.0*blur*hstep, tc.y + 3.0*blur*vstep)) * 0.072994;
sum += texture2D(u_texture, vec2(tc.x + 4.0*blur*hstep, tc.y + 4.0*blur*vstep)) * 0.035855;
sum += texture2D(u_texture, vec2(tc.x + 5.0*blur*hstep, tc.y + 5.0*blur*vstep)) * 0.014374;
gl_FragColor = vColor * vec4(sum.rgb, 1.0);
} }

View File

@ -0,0 +1,15 @@
attribute vec4 a_position;
attribute vec4 a_color;
attribute vec2 a_texCoord0;
uniform mat4 u_projTrans;
varying vec4 vColor;
varying vec2 vTexCoord;
void main() {
vColor = a_color;
vTexCoord = a_texCoord0;
gl_Position = u_projTrans * a_position;
}

View File

@ -86,7 +86,6 @@ public class CircularVisualizer implements Disposable {
((OrthographicCamera) camera).setToOrtho(false, Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); ((OrthographicCamera) camera).setToOrtho(false, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
shader.begin(); shader.begin();
shader.setUniformMatrix("u_projTrans", camera.combined);
mesh.render(shader, GL20.GL_LINE_STRIP, 0, vertexCount); mesh.render(shader, GL20.GL_LINE_STRIP, 0, vertexCount);
shader.end(); shader.end();

View File

@ -34,17 +34,17 @@ public class BloomShader implements Disposable {
Gdx.app.debug("Shader", "using glow shader"); Gdx.app.debug("Shader", "using glow shader");
brightFilterShader = new ShaderProgram(Gdx.files.internal("shaders/basic.vsh"), Gdx.files.internal("shaders/bright_filter.fsh")); brightFilterShader = new ShaderProgram(Gdx.files.internal("shaders/basic.vsh"), Gdx.files.internal("shaders/bright_filter.fsh"));
if (!brightFilterShader.isCompiled()) { if (!brightFilterShader.isCompiled()) {
Gdx.app.error("Shader failed to compile", brightFilterShader.getLog()); Gdx.app.error("Shader failed to compile bright filter shader", brightFilterShader.getLog());
System.exit(0); System.exit(1);
} }
if (brightFilterShader.getLog().length() != 0) { if (brightFilterShader.getLog().length() != 0) {
Gdx.app.error("Shader", brightFilterShader.getLog()); Gdx.app.error("Shader", brightFilterShader.getLog());
} }
gaussianBlurShader = new ShaderProgram(Gdx.files.internal("shaders/basic.vsh"), Gdx.files.internal("shaders/gaussian_blur.fsh")); gaussianBlurShader = new ShaderProgram(Gdx.files.internal("shaders/gaussian_blur.vsh"), Gdx.files.internal("shaders/gaussian_blur.fsh"));
if (!gaussianBlurShader.isCompiled()) { if (!gaussianBlurShader.isCompiled()) {
Gdx.app.error("Shader failed to compile", gaussianBlurShader.getLog()); Gdx.app.error("Shader failed to compile gaussian blur shader", gaussianBlurShader.getLog());
System.exit(0); System.exit(1);
} }
if (gaussianBlurShader.getLog().length() != 0) { if (gaussianBlurShader.getLog().length() != 0) {
Gdx.app.error("Shader", gaussianBlurShader.getLog()); Gdx.app.error("Shader", gaussianBlurShader.getLog());
@ -52,8 +52,8 @@ public class BloomShader implements Disposable {
combineShader = new ShaderProgram(Gdx.files.internal("shaders/basic.vsh"), Gdx.files.internal("shaders/combine.fsh")); combineShader = new ShaderProgram(Gdx.files.internal("shaders/basic.vsh"), Gdx.files.internal("shaders/combine.fsh"));
if (!combineShader.isCompiled()) { if (!combineShader.isCompiled()) {
Gdx.app.error("Shader failed to compile", combineShader.getLog()); Gdx.app.error("Shader failed to compile combination shader", combineShader.getLog());
System.exit(0); System.exit(1);
} }
if (combineShader.getLog().length() != 0) { if (combineShader.getLog().length() != 0) {
Gdx.app.error("Shader", combineShader.getLog()); Gdx.app.error("Shader", combineShader.getLog());
@ -71,10 +71,13 @@ public class BloomShader implements Disposable {
combineShader.setUniformi("u_texture1", 1); combineShader.setUniformi("u_texture1", 1);
combineShader.end(); combineShader.end();
gaussianBlurShader.begin();
gaussianBlurShader.setUniformf("resolution", hBlur.getWidth(), vBlur.getHeight());
gaussianBlurShader.end();
vBlur.getColorBufferTexture().bind(1); vBlur.getColorBufferTexture().bind(1);
Gdx.gl.glActiveTexture(GL20.GL_TEXTURE0); Gdx.gl.glActiveTexture(GL20.GL_TEXTURE0);
ShaderProgram.pedantic = false;
} }
public void begin() { public void begin() {
@ -102,31 +105,46 @@ public class BloomShader implements Disposable {
screenBatch.flush(); screenBatch.flush();
lightFilterBuffer.end(); lightFilterBuffer.end();
for (int i = 0; i <= bloomLevel; i++) {
// Horizontal gaussian blur // Horizontal gaussian blur
if (i > 0) {
fboRegion.setTexture(vBlur.getColorBufferTexture());
} else {
fboRegion.setTexture(lightFilterBuffer.getColorBufferTexture()); fboRegion.setTexture(lightFilterBuffer.getColorBufferTexture());
}
hBlur.begin(); hBlur.begin();
screenBatch.setShader(gaussianBlurShader); screenBatch.setShader(gaussianBlurShader);
gaussianBlurShader.setUniformi("horizontal", 1); gaussianBlurShader.setUniformf("radius", 1f + 0.2f*bloomLevel);
gaussianBlurShader.setUniformi("pass", 0);
screenBatch.draw(fboRegion, 0f, 0f, width, height); screenBatch.draw(fboRegion, 0f, 0f, width, height);
screenBatch.flush(); screenBatch.flush();
hBlur.end(); hBlur.end();
// //Vertical gaussian blur // //Vertical gaussian blur
vBlur.begin();
fboRegion.setTexture(hBlur.getColorBufferTexture()); fboRegion.setTexture(hBlur.getColorBufferTexture());
screenBatch.setShader(gaussianBlurShader); vBlur.begin();
gaussianBlurShader.setUniformi("horizontal", 0); gaussianBlurShader.setUniformi("pass", 1);
screenBatch.draw(fboRegion, 0f, 0f, width, height);
screenBatch.flush();
vBlur.end();
if (bloomLevel > 2) {
// Horizontal gaussian blur
fboRegion.setTexture(vBlur.getColorBufferTexture());
hBlur.begin();
gaussianBlurShader.setUniformf("radius", 1.1f);
gaussianBlurShader.setUniformi("pass", 0);
screenBatch.draw(fboRegion, 0f, 0f, width, height);
screenBatch.flush();
hBlur.end();
// //Vertical gaussian blur
fboRegion.setTexture(hBlur.getColorBufferTexture());
vBlur.begin();
gaussianBlurShader.setUniformi("pass", 1);
screenBatch.draw(fboRegion, 0f, 0f, width, height); screenBatch.draw(fboRegion, 0f, 0f, width, height);
screenBatch.flush(); screenBatch.flush();
vBlur.end(); vBlur.end();
} }
Gdx.gl.glClearColor(0f, 0f, 0f, 0f); Gdx.gl.glClearColor(0f, 0f, 0f, 1f);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
screenBatch.setShader(combineShader); screenBatch.setShader(combineShader);
fboRegion.setTexture(normalBuffer.getColorBufferTexture()); fboRegion.setTexture(normalBuffer.getColorBufferTexture());

View File

@ -110,7 +110,7 @@ public class PCMObtainer implements Observer, PCMSystem {
private boolean synchronizeBufferWithPlayback() { private boolean synchronizeBufferWithPlayback() {
int bufferPos = calcBufferPosition(); int bufferPos = calcBufferPosition();
if (bufferPos < playingBuffer.limit() || bufferPos >= 0) { if (bufferPos <= playingBuffer.limit() && bufferPos >= 0) {
synchronized (this) { synchronized (this) {
playingBuffer.position(bufferPos); playingBuffer.position(bufferPos);
windowsRead = (int) ((mc.getCurrentPosition() * sampleRate) / windowSize); windowsRead = (int) ((mc.getCurrentPosition() * sampleRate) / windowSize);

View File

@ -43,8 +43,6 @@ public class MainScreen extends ScreenAdapter implements ResizeReadyScreen {
private BloomShader bloomShader; private BloomShader bloomShader;
private float lerpAlpha;
private Texture background; private Texture background;
private Batch screenBatch; private Batch screenBatch;
@ -241,7 +239,7 @@ public class MainScreen extends ScreenAdapter implements ResizeReadyScreen {
} }
if (bloomLevel > 0) { if (bloomLevel > 0) {
if (bloomShader == null) bloomShader = new BloomShader(screenBatch); if (bloomShader == null) bloomShader = new BloomShader(screenBatch);
bloomShader.setBloomLevel((bloomLevel-1)*2); bloomShader.setBloomLevel((bloomLevel-1));
} else if (bloomShader != null) { } else if (bloomShader != null) {
bloomShader.dispose(); bloomShader.dispose();
bloomShader = null; bloomShader = null;