绘制平滑的曲线

12

我正在使用libgdx开发一款游戏,并且想要使用形状渲染器来绘制平滑的线条。

        shaperenderer.begin(ShapeType.Line);
        shaperenderer.line(fisrstVec2,secondVec2);
        shaperenderer.end();

我尝试过从libgdx博客使用多重采样抗锯齿。我还查阅了libgdx中的反锯齿填充形状,但不幸的是这些代码在最新版本的libgdx中已经不存在了。

   Gdx.gl.glEnable(GL10.GL_LINE_SMOOTH);
   Gdx.gl.glEnable(GL10.GL_POINT_SMOOTH); 

2
你是在 Android 设备上运行还是桌面电脑上? - m.antkowicz
3
我正在安卓设备上运行此程序。 - Amit Kumar Shrivastava
10
GL_LINE_SMOOTH已经过时(自OpenGL ES 1.0版开始,LibGDX不再支持)。您应该通过在从启动器类传入游戏的ApplicationConfiguration中设置“numSamples”来启用多采样。如果这样做不起作用,也许您正在测试不支持它的GPU上。还可以通过绘制具有一些空白边距的长而瘦的矩形精灵来获得无需反锯齿即可获得平滑线条的效果。 - Tenfour04
2
@Tenfour04 这可能是一个很好的答案,不仅仅是一条评论。 - antonio
1个回答

3

在配置中启用抗锯齿功能:

对于桌面:

    LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
    config.samples = 2;
    new LwjglApplication(new MyGdxGame(Helper.arrayList(arg)), config);

针对Android系统:

    AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
    config.numSamples = 2;
    initialize(new MyGdxGame(null), config);

或者你可以创建一个大小为1x1的白色像素图并使用它来创建精灵,然后使用该精灵绘制线条。我个人更喜欢这种方法,而不是使用ShapeRenderer(请注意,此方法中没有旋转):

/**
 * draws a line on the given axis between given points
 *
 * @param batch       spriteBatch
 * @param axis        axis of the line, vertical or horizontal
 * @param x          x position of the start of the line
 * @param y          y position of the start of the line
 * @param widthHeight width or height of the line according to the axis
 * @param thickness thickness of the line
 * @param color       color of the line, if the color is null, color will not be changed.
 */
public static void line(SpriteBatch batch, Axis axis, float x, float y, float widthHeight, float thickness, Color color, float alpha) {
    if (color != null) sprite.setColor(color);
    sprite.setAlpha(alpha);
    if (axis == Axis.vertical) {
        sprite.setSize(thickness, widthHeight);
    } else if (axis == Axis.horizontal) {
        sprite.setSize(widthHeight, 1);
    }
    sprite.setPosition(x,y);
    sprite.draw(batch);
    sprite.setAlpha(1);
}

通过对之前方法进行一些修改,您可以使用这种方法来绘制旋转的直线。

public static void rotationLine(SpriteBatch batch, float x1, float y1, float x2, float y2, float thickness, Color color, float alpha) {
    // set color and alpha
    if (color != null) sprite.setColor(color);
    sprite.setAlpha(alpha);
    // set origin and rotation
    sprite.setOrigin(0,0);
    sprite.setRotation(getDegree(x2,y2, x1, y1));
    // set position and dimension
    sprite.setSize(distance(x1,y1,x2,y2),thickness);
    sprite.setPosition(x1, y1);
    // draw
    sprite.draw(batch);
    // reset rotation
    sprite.rotate(0);
}

public static float getDegree(float x, float y, float originX, float originY) {
    float angle = (float) Math.toDegrees(Math.atan2(y - originY, x - originX));
    while (angle < 0 || angle > 360)
        if (angle < 0) angle += 360;
        else if (angle > 360) angle -= 360;
    return angle;
}

public static float distance(float x, float y, float x2, float y2) {
    return (float) Math.sqrt(Math.pow((x2 - x), 2) + Math.pow((y2 - y), 2));
}

你的旋转方法只有在启用多重采样时才有效。 - Guilherme Campos Hazan
@GuilhermeCamposHazan 多重采样默认启用了吗? - ossobuko
不,这是可选的。默认值为samples=0。 - Guilherme Campos Hazan

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