首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在libgdx中,如何创建动态纹理?

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

Stack Overflow用户
提问于 2013-11-13 05:48:11
回答 1查看 2.3K关注 0票数 3

在libgdx中,如何创建动态纹理?造山还是造山?就像游戏一样,谢谢你的回答,我等着你的答复。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-06-25 06:54:09

代码语言:javascript
复制
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
//---------------------------
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19946302

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档