问题描述
我想用libgdx为Android游戏绘制一些(填充的)多边形。它不应该充满图形/纹理。我只有多边形的顶点(闭合路径),并试图用网格物体进行可视化,但在某些时候,这不是最好的解决方案,我认为。
i wanted to draw some (filled) polygons with libgdx for an android game. it shoudn't be filled with a graphic/texture. i have only the vertices of the polygon (closed path) and tried to visualize with meshes but at some point this is not the best solution, i think.
我的代码一个矩形是:
private Mesh mesh;
@Override
public void create() {
if (mesh == null) {
mesh = new Mesh(true, 4, 0,
new VertexAttribute(Usage.Position, 3, "a_position")
);
mesh.setVertices(new float[] { -0.5f, -0.5f, 0
0.5f, -0.5f, 0,
-0.5f, 0.5f, 0,
0.5f, 0.5f, 0 });
}
}
...
@Override
public void render() {
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
mesh.render(GL10.GL_TRIANGLE_STRIP, 0, 4);
}
是否有一个函数或其他东西以更简单的方式绘制填充多边形?
is there a function or something to draw filled polygons in an easier way?
推荐答案
这是一个绘制2D凹面多边形的LIBGDX示例。
Here is a LIBGDX example which draws 2D concave poly.
//为PolygonSprite定义类成员PolygonSpriteBatch
// define class members for PolygonSprite PolygonSpriteBatch
PolygonSprite poly;
PolygonSpriteBatch polyBatch;
Texture textureSolid;
//创建实例,1x1大小纹理用红色像素作为变通方法,(x,y)数组用于初始化聚合物的坐标
// create instances, 1x1 size texture used with red pixel as workaround, (x,y) array of coords used for initialization of poly
ctor() {
textureSolid = makeTextureBox(1, 0xFFFF0000, 0, 0);
float a = 100;
float b = 100;
PolygonRegion polyReg = new PolygonRegion(new TextureRegion(textureSolid),
new float[] {
a*0, b*0,
a*0, b*2,
a*3, b*2,
a*3, b*0,
a*2, b*0,
a*2, b*1,
a*1, b*1,
a*1, b*0,
});
poly = new PolygonSprite(polyReg);
poly.setOrigin(a, b);
polyBatch = new PolygonSpriteBatch();
}
//绘制并旋转多边形
// draw and rotate poly
void draw() {
super.draw();
polyBatch.begin();
poly.draw(polyBatch);
polyBatch.end();
poly.rotate(1.1f);
}
这篇关于使用libgdx绘制填充多边形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!