LibGDX Generating a png from a model

筅森魡賤 提交于 2019-12-11 04:10:34

问题


The title pretty much say it all, I need to get a image from a Model is this posible? I searched for this and found nothing.

Solution thanks to @retodaredevil

    FrameBuffer fbo   = new FrameBuffer(Pixmap.Format.RGBA4444, screenWidth, screenHeight, true);
    ModelBatch  batch = new ModelBatch();

    fbo.begin(); // make fbo the current buffer

    Gdx.gl.glViewport(0, 0,screenWidth, screenHeight);
    Gdx.gl.glClear(GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);

    batch.begin(camera);
    batch.render(instance, environment);
    batch.end();

    try{
        FileHandle fh = new FileHandle(output);
        Pixmap pixmap = ScreenUtils.getFrameBufferPixmap(0,0,screenWidth,screenHeight);
        PixmapIO.writePNG(fh, pixmap);
        pixmap.dispose();
    }catch (Exception e){
        e.printStackTrace();
    }

    fbo.end(); // Now you can draw on the display again

    batch.dispose();
    fbo.dispose();

回答1:


From what I understand, we'll have to use two different tutorials

https://github.com/mattdesl/lwjgl-basics/wiki/FrameBufferObjects

https://xoppa.github.io/blog/basic-3d-using-libgdx/ (You're probably already familiar with this)

FrameBuffer fbo = new FrameBuffer(Pixmap.Format.RGBA4444, width, height, hasDepth);
// I've never used a FrameBuffer before so you may have to play around with constructor parameters

ModelBatch batch = // your ModelBatch initialization here

fbo.begin(); // make fbo the current buffer
glClearColor(0f, 0f, 0f, 1f);
glClear(GL_COLOR_BUFFER_BIT);
batch.resize(fbo.getWidth(), fbo.getHeight());

batch.begin();
batch.render(modelInstance, environment);
batch.end();

fbo.end(); // Now you can draw on the display again

// If you want to, you can resize your batch and use it to draw on the display
batch.resize(Display.getWidth(), Display.getHeight());

Once you do whatever you need to do to your FrameBuffer, you can get the texture

fbo.getTexture();

EDIT: That method doesn't exist, look at OP's question for solution EDIT 2: FrameBuffer does actually have a method called getColorBufferTexture()

For saving a texture, look at https://www.badlogicgames.com/forum/viewtopic.php?p=8358#p8358 or https://www.badlogicgames.com/forum/viewtopic.php?t=5686

I used this to get one of the links: Libgdx save SpriteBatch in a texture everything else I duckduckgoed

If something doesn't work let me know and I can try to look around more.



来源:https://stackoverflow.com/questions/53939146/libgdx-generating-a-png-from-a-model

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!