简体   繁体   中英

LibGDX - Cannot get texture from FrameBuffer

I try to implement some post-processing, so I need to use FrameBuffer to collect entire picture and then post-process it with shader. I've found some examples of how to work with FrameBuffer in LibGDX( Rendering a 3D model to texture in LibGDX ), but it doesn't work. I get black screen. My code snippet:

@Override
public void show() {
    modelBatch = new ModelBatch();
    environment = new Environment();
    environment.set(new ColorAttribute(ColorAttribute.AmbientLight, 1f, 1f, 0f, 1f));
    environment.add(new DirectionalLight().set(1f, 0.0f, 0.0f, 0f, -2f, 0f));

    cam = new PerspectiveCamera(67, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
    cam.position.set(1f, 1f, 1f);
    cam.lookAt(0,0,0);
    cam.near = 0.1f;
    cam.far = 300f;
    cam.update();
com.badlogic.gdx.graphics.g3d.loader.ObjLoader loader =new ObjLoader();
    model = loader.loadModel(Gdx.files.internal("cube/cube.obj"));

    instance = new ModelInstance(model);

 fbo = new FrameBuffer(Format.RGBA8888, Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), true);
    sb= new SpriteBatch();

    camController = new CameraInputController(cam);
    Gdx.input.setInputProcessor(camController);
}

@Override
public void render(float delta) {
fbo.begin();

    camController.update();

    Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
    Gdx.gl.glClearColor(0, 0, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
    modelBatch.begin(cam);

    modelBatch.render(instance, environment);        
    modelBatch.end();

fbo.end();

    //getting texture
    tex = fbo.getColorBufferTexture();

//render texture
  spriteBatch.begin();        
    spriteBatch.draw(tex,0,0, Gdx.graphics.getWidth(),  Gdx.graphics.getHeight());
    spriteBatch.end();
}

What do I wrong?

UPDATE

I got another problem. Look at the pictures bellow:

在此处输入图片说明在此处输入图片说明

I see this hidden fragment of landscape. I use

fb = new FrameBuffer(Format.RGBA8888, Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), false);

But when I try to switch on the depth of FrameBuffer , it shows me black screen again:

fb = new FrameBuffer(Format.RGBA8888, Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), true);

How to get rid of showing this fragment of landscape and why FrameBuffer doesn't work with depth parameter?

You can use ScreenUtils functions to get framebuffer/

http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/utils/ScreenUtils.html

ScreenUtils.getFrameBufferPixels(int x, int y, int w, int h, boolean flipY)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM