Search code examples
dynamictextureslibgdx

in libgdx, how to create dynamic texture?


In libgdx, how to create dynamic texture? e.g: create hill or mountain?like the Game Thank you for your answer, i am waitting for your reply. put some beautiful color


Solution

  • Example
    =======
    package com.badlogic.gdx.tests.bullet;
    
    /**
    Question:   In libgdx, how to create dynamic texture?
    Answer  :   Use a private render function to draw in a private frame buffer
                convert the frame bufder 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***
            ...
            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
    //---------------------------