LibGDX - 无法从FrameBuffer获取纹理

时间:2014-02-24 03:19:16

标签: java android opengl-es-2.0 libgdx framebuffer

我尝试实现一些后期处理,因此我需要使用FrameBuffer来收集整个图片,然后使用着色器对其进行后处理。我在LibGDX(Rendering a 3D model to texture in LibGDX)中找到了一些如何使用FrameBuffer的示例,但它不起作用。我得到了黑屏。我的代码片段:

@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();
}

我错了什么?

更新

我遇到了另一个问题。看看下面的图片:

enter image description here enter image description here

我看到这个隐藏的景观片段。我用

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

但是当我尝试打开FrameBuffer的深度时,它再次显示黑屏:

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

如何摆脱显示此景观片段以及为什么FrameBuffer不能与depth参数一起使用?

1 个答案:

答案 0 :(得分:1)

您可以使用ScreenUtils函数获取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)