fbo functioning

This commit is contained in:
Harrison Deng 2017-07-19 16:04:43 -05:00
parent 5f1958e62d
commit 942cd9f102
8 changed files with 99 additions and 80 deletions

38
android/assets/shaders/glow.fsh Executable file
View File

@ -0,0 +1,38 @@
#ifdef GL_ES
#define LOWP lowp
precision mediump float;
#else
#define LOWP
#endif
varying LOWP vec4 vColor;
varying vec2 vTexCoord;
uniform sampler2D u_texture;
void main() {
vec4 sum = vec4(0);
vec2 texcoord = vTexCoord;
int j;
int i;
for (i = -4; i < 4; i++) {
for (j = -3; j < 3; j++) {
sum += texture2D(u_texture, texcoord + vec2(j, i)*0.004) * 0.25;
}
}
if (texture2D(u_texture, texcoord).r < 0.3) {
gl_FragColor = vec4(sum * sum * 0.012 + texture2D(u_texture, texcoord))
* vColor;
} else {
if (texture2D(u_texture, texcoord).r < 0.5) {
gl_FragColor = vec4(
sum * sum * 0.009 + texture2D(u_texture, texcoord))
* vColor;
} else {
gl_FragColor = vec4(
sum * sum * 0.0075 + texture2D(u_texture, texcoord))
* vColor;
}
}
}

View File

@ -1,42 +0,0 @@
#ifdef GL_ES
#define LOWP lowp
precision mediump float;
#else
#define LOWP
#endif
varying LOWP vec4 vColor;
varying vec2 vTexCoord;
uniform sampler2D u_texture;
void main()
{
vec4 sum = vec4(0);
vec2 texcoord = vTexCoord;
int j;
int i;
for( i= -4 ;i < 4; i++)
{
for (j = -3; j < 3; j++)
{
sum += texture2D(u_texture, texcoord + vec2(j, i)*0.004) * 0.25;
}
}
if (texture2D(u_texture, texcoord).r < 0.3)
{
gl_FragColor = sum*sum*0.012 + texture2D(u_texture, texcoord);
}
else
{
if (texture2D(u_texture, texcoord).r < 0.5)
{
gl_FragColor = sum*sum*0.009 + texture2D(u_texture, texcoord);
}
else
{
gl_FragColor = sum*sum*0.0075 + texture2D(u_texture, texcoord);
}
}
}

View File

@ -1,17 +0,0 @@
#ifdef GL_ES
#define LOWP lowp
precision mediump float;
#else
#define LOWP
#endif
varying LOWP vec4 vColor;
varying vec2 vTexCoord;
uniform sampler2D u_texture;
void main() {
vec4 texColor = texture2D(u_texture, vTexCoord);
texColor.rgb = 1.0 - texColor.rgb;
gl_FragColor = texColor * vColor;
}

View File

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

View File

@ -3,7 +3,14 @@ package zero1hd.polyjet.ui.stages;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Preferences;
import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.graphics.Camera;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Pixmap.Format;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.graphics.glutils.FrameBuffer;
import com.badlogic.gdx.graphics.glutils.ShaderProgram;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.utils.viewport.FitViewport;
@ -33,7 +40,9 @@ public class GamePlayArea extends Stage {
private ShaderProgram glowShader;
private ShaderProgram bgShader;
private FrameBuffer blurTarget;
TextureRegion fboRegion;
private int fboSize;
private Texture background;
private float time;
@ -62,7 +71,7 @@ public class GamePlayArea extends Stage {
public void loadShaders(Preferences prefs) {
if (prefs.getBoolean("glow shader")) {
Gdx.app.debug("Shader", "using glow shader");
glowShader = new ShaderProgram(Gdx.files.internal("shaders/glow_vert.glsl").readString(), Gdx.files.internal("shaders/glow_frag.glsl").readString());
glowShader = new ShaderProgram(Gdx.files.internal("shaders/glow.vsh"), Gdx.files.internal("shaders/glow.fsh"));
if (!glowShader.isCompiled()) {
System.err.println(glowShader.getLog());
System.exit(0);
@ -70,13 +79,21 @@ public class GamePlayArea extends Stage {
if (glowShader.getLog().length()!=0) {
System.out.println(glowShader.getLog());
}
// blurTarget = new FrameBuffer(Format.RGBA8888, Gdx.graphics.getHeight()/Polyjet.GAME_AREA_HEIGHT, Polyjet.GAME_AREA_WIDTH*Gdx.graphics.getHeight()/Polyjet.GAME_AREA_HEIGHT, false);
// blurTarget = new FrameBuffer(Format.RGBA8888, Polyjet.GAME_AREA_WIDTH, Polyjet.GAME_AREA_HEIGHT, false);
fboSize = 4096;
blurTarget = new FrameBuffer(Format.RGBA8888, fboSize, fboSize, false);
// blurTarget = new FrameBuffer(Format.RGBA8888, (int) getCamera().viewportWidth, (int) getCamera().viewportHeight, false);
fboRegion = new TextureRegion(blurTarget.getColorBufferTexture());
fboRegion.flip(false, true);
}
if (prefs.getBoolean("bg shader")) {
Gdx.app.debug("Shader", "using background shader");
bgShader = new ShaderProgram(Gdx.files.internal("shaders/bg_vert.glsl").readString(), Gdx.files.internal("shaders/bg_frag.glsl").readString());
bgShader = new ShaderProgram(Gdx.files.internal("shaders/bg.vsh"), Gdx.files.internal("shaders/bg.fsh"));
if (!bgShader.isCompiled()) {
System.err.println(bgShader.getLog());
@ -85,7 +102,7 @@ public class GamePlayArea extends Stage {
if (bgShader.getLog().length()!=0) {
System.out.println(bgShader.getLog());
}
} else {
//TODO prepare background rendering without shader
}
@ -107,11 +124,48 @@ public class GamePlayArea extends Stage {
getBatch().end();
if (glowShader != null) {
getBatch().setShader(glowShader);
blurTarget.begin();
Gdx.gl.glClearColor(0f, 0f, 0f, 0f);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
//
getBatch().setShader(null);
// resizeBatch(fboSize, fboSize);
// resizeBatch(Polyjet.GAME_AREA_WIDTH, Polyjet.GAME_AREA_HEIGHT);
updateBatch();
getBatch().begin();
getRoot().draw(getBatch(), 1f);
getBatch().flush();
blurTarget.end(getViewport().getScreenX(), getViewport().getScreenY(), getViewport().getScreenWidth(), getViewport().getScreenHeight());
// resizeBatch(Polyjet.GAME_AREA_WIDTH, Polyjet.GAME_AREA_HEIGHT);
updateBatch();
// getBatch().setShader(glowShader);
getBatch().draw(fboRegion, 0f, 0f, Polyjet.GAME_AREA_WIDTH, Polyjet.GAME_AREA_HEIGHT);
getBatch().setShader(null);
getBatch().end();
} else {
getBatch().setShader(null);
super.draw();
}
super.draw();
}
private void updateBatch() {
Camera camera = getViewport().getCamera();
camera.update();
Batch batch = getBatch();
batch.setProjectionMatrix(camera.combined);
}
void resizeBatch(int width, int height) {
((OrthographicCamera) getCamera()).setToOrtho(false, width, height);
getBatch().setProjectionMatrix(getCamera().combined);
// getViewport().update(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
}
@Override
@ -232,4 +286,5 @@ public class GamePlayArea extends Stage {
public void dispose() {
super.dispose();
}
}