在libgdx中,如何创建动态纹理?

3
在libgdx中,如何创建动态纹理?例如:创建山丘或山峰?就像游戏那样。
谢谢您的回答,我在等待您的回复。 放些漂亮的颜色

也许这个链接 http://www.iforce2d.net/blog/2013-07-20 对你有兴趣。 - noone
请参见http://stackoverflow.com/questions/13337936/ccrendertexture-gl11extensionpack-libgdx-how-to。 - P.T.
1个回答

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接