【问题标题】:libgdx write text on texturelibgdx 在纹理上写入文本
【发布时间】:2013-06-22 11:07:55
【问题描述】:

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

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

提前致谢!

【问题讨论】:

    标签: android opengl-es 3d libgdx


    【解决方案1】:

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

    【讨论】:

    • 我认为这可行...实际上我的完整模型变黑了...有没有办法检查带有书面文字的纹理是什么样的?因为我读过一些关于文本大小问题的东西……也许它只是写到大……
    • 好的,肯定有另一个问题。出于测试目的,我在 new BitmapFont() 之后将 BitmapFont 的颜色设置为红色,在 batch.begin() 之前将 spritebatch 的颜色设置为蓝色,但模型保持黑色。如果纹理包含字体,模型不应该变成红色吗?
    • 也无法获取 Pixmap 并将字节写入文件...我总是得到“Pixmap 已被处理”-无论我尝试在 FrameBuffer 之前还是之后获取它,或者SpriteBatch .end()
    【解决方案2】:

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

    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
        //---------------------------
    

    【讨论】:

      猜你喜欢
      • 2014-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-29
      • 1970-01-01
      相关资源
      最近更新 更多