Dynamic 在libgdx中,如何创建动态纹理?
在libgdx中,如何创建动态纹理?e、 g:创建山还是山?就像 谢谢你的回答,我在等待你的答复。Dynamic 在libgdx中,如何创建动态纹理?,dynamic,textures,libgdx,Dynamic,Textures,Libgdx,在libgdx中,如何创建动态纹理?e、 g:创建山还是山?就像 谢谢你的回答,我在等待你的答复。 示例 ======= 包com.badlogic.gdx.tests.bullet; /** 问题:在libgdx中,如何创建动态纹理? 答:使用专用渲染函数在专用帧缓冲区中绘制 将帧bufder转换为Pixmap,创建纹理。 作者:乔恩·古德温 **/ 导入com.badlogic.gdx.graphics.Texture; 导入com.badlogic.gdx.graphics.Pixmap;
示例
=======
包com.badlogic.gdx.tests.bullet;
/**
问题:在libgdx中,如何创建动态纹理?
答:使用专用渲染函数在专用帧缓冲区中绘制
将帧bufder转换为Pixmap,创建纹理。
作者:乔恩·古德温
**/
导入com.badlogic.gdx.graphics.Texture;
导入com.badlogic.gdx.graphics.Pixmap;
…/(ctrl-shift-o)在Eclipse中自动加载导入
公共类BaseBulletTest扩展了BulletTest
{
//类变量
=================
公共纹理纹理=null;//创建此
public Array disposables=new Array();
公共Pixmap pm=null;
//---------------------------
@凌驾
公共void创建()
{
init();
}
//---------------------------
公共静态void init()
{
if(texture==null)纹理(Color.BLUE,Color.WHITE);
TextureAttribute ta_tex=TextureAttribute.createDiffuse(纹理);
最终材质材质框=新材质(ta_tex,ColorAttribute.createSpecular(1,1,1,1),
FloatAttribute.createshiness(8f));
最终长属性1=用法.位置|用法.正常|用法.纹理坐标;
最终模型框Model=modelBuilder.createBox(1f、1f、1f、材质框、属性1);
...
}
//---------------------------
公共纹理纹理(颜色fg\U颜色、颜色bg\U颜色)
{
Pixmap pm=渲染(前景颜色、背景颜色);
纹理=新纹理(pm);//***这是您的新动态纹理***
一次性。添加(纹理);//存储纹理
}
//---------------------------
公共像素贴图渲染(颜色fg\U颜色、颜色bg\U颜色)
{
int-width=Gdx.graphics.getWidth();
int height=Gdx.graphics.getHeight();
SpriteBatch SpriteBatch=新SpriteBatch();
m_fbo=新帧缓冲区(Format.RGB565,(int)(宽度*m_fboScaler),(int)(高度*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\u颜色\u缓冲\u位);
Matrix4 normalProjection=new Matrix4().setToorTo2D(0,0,Gdx.graphics.getWidth(),Gdx.graphics.getHeight());
spriteBatch.setProjectionMatrix(法线投影);
spriteBatch.begin();
spriteBatch.setColor(fg_颜色);
//画画***这里是你画动态纹理的地方***
...
spriteBatch.end();//完成对缓冲区的写入
pm=ScreenUtils.getFrameBufferPixmap(0,0,(int)宽度,(int)高度);//将帧缓冲区写入Pixmap
m_fbo.end();
//pm.dispose();
//翻转。处置();
//tx.dispose();
m_fbo.dispose();
m_fbo=null;
spriteBatch.dispose();
//返回纹理;
返回pm;
}
//---------------------------
}//基于类的测试
//---------------------------
也许这对你来说很有趣
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
//---------------------------