Skip to content

Instantly share code, notes, and snippets.

@mgsx-dev
Last active October 4, 2018 17:00
Show Gist options
  • Save mgsx-dev/d472d1ddae3cfda2b0c208d14d249ad2 to your computer and use it in GitHub Desktop.
Save mgsx-dev/d472d1ddae3cfda2b0c208d14d249ad2 to your computer and use it in GitHub Desktop.
How to render stage in a FBO and display it on screen (doesn't work well with screen viewport for now)
package com.mygdx;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Pixmap.Format;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.glutils.FrameBuffer;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.SelectBox;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.utils.viewport.FitViewport;
public class FBOStageSSCCE extends ApplicationAdapter
{
public static void main (String[] arg) {
LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
config.width = 1024;
config.height = 768;
new LwjglApplication(new FBOStageSSCCE(), config);
}
private Stage stage;
private FrameBuffer fbo;
/** > 1 for blur/pixelized, < 1 for antialias (over sampling) */
private float decimate = 2f;
@Override
public void create() {
stage = new Stage(new FitViewport(Gdx.graphics.getWidth(), Gdx.graphics.getHeight()));
Skin skin = new Skin(Gdx.files.internal("skins/game-skin.json"));
Table root = new Table(skin);
SelectBox<String> sb = new SelectBox<String>(skin);
sb.setItems("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t");
root.add(sb);
stage.addActor(root);
root.setFillParent(true);
Gdx.input.setInputProcessor(stage);
fbo = new FrameBuffer(Format.RGBA8888, (int)(Gdx.graphics.getWidth()/decimate), (int)(Gdx.graphics.getHeight()/decimate), false);
// optional : blur or pixelize
fbo.getColorBufferTexture().setFilter(TextureFilter.Nearest, TextureFilter.Nearest);
}
@Override
public void resize(int width, int height) {
stage.getViewport().update(width, height, true);
}
@Override
public void render()
{
Gdx.gl.glClearColor(0, 0, 0, 0);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
boolean useFBO = true;
if(useFBO){
stage.getViewport().update(fbo.getWidth(), fbo.getHeight(), true);
fbo.begin();
Gdx.gl.glClearColor(0, 0, 0, 0);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
stage.act();
stage.draw();
fbo.end();
stage.getViewport().update(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), true);
Batch batch = stage.getBatch();
batch.getProjectionMatrix().setToOrtho2D(0, 0, 1, 1);
batch.begin();
batch.draw(fbo.getColorBufferTexture(), 0, 0, 1, 1, 0, 0, 1, 1);
batch.end();
}
else{
stage.act();
stage.draw();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment