0

我知道我比 libgdx 有点领先,因为它实际上更像是一个 2D 库,但我正在开发一个由 libgdx 提供支持的 3D 应用程序,并且需要在模型上编写文本。

到目前为止,我已经能够动态地更改模型的纹理。现在我需要将文本写入纹理以将此纹理应用于我的模型......这是否已经可以通过 libgdx 实现?如果是,如何? 到目前为止,我只找到了如何使用 BitmapFont 在屏幕上编写文本的教程,但只能通过 SpriteBatch,我认为不可能将 spritebatch 的输出写入纹理......

提前致谢!

4

2 回答 2

1

您可以将SpriteBatch(或任何 OpenGL 绘图命令)的输出发送到纹理,而不是将其发送到屏幕。在 Libgdx 中,您使用FrameBuffer对象来完成此操作。本教程涵盖了基础知识和更多内容:https ://github.com/mattdesl/lwjgl-basics/wiki/FrameBufferObjects

于 2013-06-24T02:42:52.927 回答
0

使用普通位图字体,您可以获得pixmap所有的Glpths,并且可以进行位图复制。

pixmap.drawPixmap(fontPixmap, x_place, (TILE_HEIGHT - aGlyph.height) / 2,
aGlyph.srcX, aGlyph.srcY, aGlyph.width, aGlyph.height);

The only way (I have found) of drawing raster fonts (`.ttf`) is as follows:

    Example framework:
    ==================
    package com.badlogic.gdx.tests.bullet;

    /**
    Question       :   libgdx write text on texture
    interpreted as :   In libgdx, how to create dynamic texture?
    Answer         :   Use a private render function to draw in a private frame buffer and convert the frame buffer to Pixmap, create Texture.
    Author  :   Jon Goodwin
    **/

    import com.badlogic.gdx.graphics.Texture;
    import com.badlogic.gdx.graphics.Pixmap;
    ...//(ctrl-shift-o) to auto-load imports in Eclipse


    public class BaseBulletTest extends BulletTest
    {
    //class variables
    =================
    public Texture           texture     = null;//create this
    public Array<Disposable> disposables = new Array<Disposable>();
    public Pixmap            pm          = null;
    //---------------------------
        @Override
        public void create ()
        {
            init();
        }
    //---------------------------
        public static void init ()
        {
            if(texture == null) texture(Color.BLUE, Color.WHITE);
            TextureAttribute ta_tex     = TextureAttribute.createDiffuse(texture);
            final Material material_box = new Material(ta_tex, ColorAttribute.createSpecular(1, 1, 1, 1),
                                                       FloatAttribute.createShininess(8f));
            final long attributes1      = Usage.Position | Usage.Normal | Usage.TextureCoordinates;
            final Model boxModel = modelBuilder.createBox(1f, 1f, 1f, material_box, attributes1);
            ...
        }
    //---------------------------
        public Texture texture(Color fg_color, Color bg_color)
        {
            Pixmap pm = render( fg_color, bg_color );
            texture = new Texture(pm);//***here's your new dynamic texture***
            disposables.add(texture);//store the texture
        }
    //---------------------------
        public Pixmap render(Color fg_color, Color bg_color)
        {
            int width = Gdx.graphics.getWidth();
            int height = Gdx.graphics.getHeight();

            SpriteBatch spriteBatch = new SpriteBatch();

            m_fbo = new FrameBuffer(Format.RGB565, (int)(width * m_fboScaler), (int)(height * m_fboScaler), false);
            m_fbo.begin();
            Gdx.gl.glClearColor(bg_color.r, bg_color.g, bg_color.b, bg_color.a);
            Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
            Matrix4 normalProjection = new Matrix4().setToOrtho2D(0, 0, Gdx.graphics.getWidth(),  Gdx.graphics.getHeight());
            spriteBatch.setProjectionMatrix(normalProjection);

            spriteBatch.begin();
            spriteBatch.setColor(fg_color);
            //do some drawing ***here's where you draw your dynamic texture***
            font.draw(spriteBatch, "5\n6\n2016",  width/4, height - 20);//multi-line draw
            ...
            spriteBatch.end();//finish write to buffer

            pm = ScreenUtils.getFrameBufferPixmap(0, 0, (int) width, (int) height);//write frame buffer to Pixmap

            m_fbo.end();
    //      pm.dispose();
    //      flipped.dispose();
    //      tx.dispose();
            m_fbo.dispose();
            m_fbo = null;
            spriteBatch.dispose();
    //      return texture;
            return pm;
        }
    //---------------------------
    }//class BaseBulletTest
    //---------------------------
于 2016-06-30T20:06:35.813 回答