LibGDX - 渲染3D场景后修改像素

时间:2014-01-15 17:06:45

标签: java libgdx render ascii-art

我正在编写一个用ASCII艺术绘制的3D游戏,以达到特殊的效果。我将模型渲染到ModelBatch,现在我想在将最终结果绘制到屏幕之前将每个像素转换为ASCII符号。我已经有了将像素转换为ASCII的代码,但我不知道如何获得渲染结果的像素。

到目前为止

代码(不包括ASCII转换):

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.PerspectiveCamera;
import com.badlogic.gdx.graphics.VertexAttributes.Usage;
import com.badlogic.gdx.graphics.g3d.Model;
import com.badlogic.gdx.graphics.g3d.ModelBatch;
import com.badlogic.gdx.graphics.g3d.ModelInstance;
import com.badlogic.gdx.graphics.g3d.materials.ColorAttribute;
import com.badlogic.gdx.graphics.g3d.materials.Material;
import com.badlogic.gdx.graphics.g3d.utils.ModelBuilder;

public class MyGame implements ApplicationListener {
    private PerspectiveCamera cam;
    private ModelBatch batch;

    public Model model;
    public ModelInstance instance;

    @Override
    public void create() {      
        //float w = Gdx.graphics.getWidth();
        //float h = Gdx.graphics.getHeight();

        cam = new PerspectiveCamera(67, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
        cam.position.set(10f, 10f, 10f);
        cam.lookAt(0,0,0);
        cam.near = 0.1f;
        cam.far = 300f;
        cam.update();

        batch = new ModelBatch();

        ModelBuilder modelBuilder = new ModelBuilder();
        model = modelBuilder.createBox(5f, 5f, 5f,
            new Material(ColorAttribute.createDiffuse(Color.GREEN)),
            Usage.Position | Usage.Normal);
        instance = new ModelInstance(model);
    }

    @Override
    public void dispose() {
        batch.dispose();
        model.dispose();
    }

    @Override
    public void render() {      
        Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
        Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);

        batch.begin(cam);
        batch.render(instance);
        batch.end();
    }

    @Override
    public void resize(int width, int height) {
    }

    @Override
    public void pause() {
    }

    @Override
    public void resume() {
    }
}

1 个答案:

答案 0 :(得分:1)

我认为您想要渲染到纹理,将纹理转换为Pixmap,然后解析像素图以将ASCII字符绘制到真实屏幕。

首先,要在LibGDX中渲染纹理(屏幕外缓冲区),请使用FrameBufferObject

fbo = new FrameBuffer();
fbo.begin();
// draw your stuff
// ...
fbo.end();

其次,要获取CPU可以使用的字节,您可以使用[ScreenUtils类](http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/utils/ScreenUtils.html )。您可以获得Pixmap或原始byte[]。例如,将上面的// ...替换为:

Pixmap p = ScreenUtils.getFrameBufferPixmap(0, 0, width, height);

现在你可以漫游pixmap检查像素(使用[Pixmap.getPixel](http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/graphics/Pixmap.html#getPixel(int,int)))。