好的,有点无聊。这里是内容。
注意:
绿色方框只是用来显示帧缓冲纹理正在绘制的位置,只需在fbo.begin()语句之后将填充颜色更改为零alpha即可清除它。
BINDING_DEPTH_FRAMEBUFFER、BINDING_DEFAULT变量来自我的代码。除非您尝试切换纹理并使用定制着色器,否则您可能不需要担心这些内容。
绘制fbo,batch.draw(fbo.getColorBufferTexture(), 20, 20, 200, 200, 0, 0, 1, 1); 希望这对您来说没问题。如果您想旋转fbo,则应该能够从您的相机创建旋转的投影矩阵。如果您想使用定制着色器,只需小心地将fbo链接为着色器的纹理即可。
package com.funferret.pixelshapes;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.FrameBuffer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.utils.ScreenUtils;
public class MyGdxGame extends ApplicationAdapter {
ShapeRenderer shapeBatch;
SpriteBatch batch;
Texture img;
FrameBuffer fbo;
public OrthographicCamera fboCamera;
int fboWidth=40;
int fboHeight=40;
int BINDING_DEPTH_FRAMEBUFFER=3;
int BINDING_DEFAULT=0;
void createFrameBuffer()
{
if (fbo!=null)
{
fbo.dispose();
}
fbo = new FrameBuffer(Pixmap.Format.RGBA8888, fboWidth, fboHeight, false);
fbo.getColorBufferTexture().setFilter(Texture.TextureFilter.Nearest, Texture.TextureFilter.Nearest);
fbo.getColorBufferTexture().bind(BINDING_DEPTH_FRAMEBUFFER);
Gdx.graphics.getGL20().glActiveTexture(GL20.GL_TEXTURE0 + BINDING_DEFAULT);
fboCamera=new OrthographicCamera();
fboCamera.setToOrtho(false, fboWidth, fboHeight);
fboCamera.update();
}
@Override
public void create () {
batch = new SpriteBatch();
shapeBatch=new ShapeRenderer();
img = new Texture("badlogic.jpg");
}
@Override
public void render ()
{
if ((fbo==null) || (fbo.getWidth()!=fboWidth) || (fbo.getHeight()!=fboHeight))
{
createFrameBuffer();
}
fbo.begin();
ScreenUtils.clear(0, 1, 0, 0.5f);
shapeBatch.begin(ShapeRenderer.ShapeType.Line);
shapeBatch.setProjectionMatrix(fboCamera.combined);
Gdx.gl.glDisable(GL20.GL_BLEND);
shapeBatch.setColor(1f, 0f, 1f, 1);
shapeBatch.circle(20,20,15,10);
shapeBatch.end();
fbo.end();
ScreenUtils.clear(1, 0, 0, 1);
batch.begin();
batch.draw(img, 0, 0);
batch.draw(fbo.getColorBufferTexture(), 20, 20, 200, 200, 0, 0, 1, 1);
batch.end();
}
@Override
public void dispose () {
batch.dispose();
img.dispose();
shapeBatch.dispose();
fbo.dispose();
}
}