【问题标题】:LIBGDX: How do I draw a filled polygon with the shaperenderer?LIBGDX:如何使用 shaperenderer 绘制填充多边形?
【发布时间】:2013-04-19 10:36:30
【问题描述】:

我已经使用顶点数组定义了一个形状:

float[] points =  new float[]{50,60,50,70,60,70, 60,60,50,60};

我在这里画这个:

shapeRenderer.polygon(floatNew);

这只是给出了形状的轮廓。
如何用颜色填充它?
谢谢

【问题讨论】:

    标签: java libgdx


    【解决方案1】:

    目前,ShapeRenderer 支持(按线)绘制多边形,但不支持填充。

    这段代码在三角形上剪裁多边形,然后分别绘制每个三角形。

    像这样编辑 ShapeRenderer.java:

    EarClippingTriangulator ear = new EarClippingTriangulator();
    
    public void polygon(float[] vertices, int offset, int count)
    {
        if (shapeType != ShapeType.Filled && shapeType != ShapeType.Line)
            throw new GdxRuntimeException("Must call begin(ShapeType.Filled) or begin(ShapeType.Line)");
        if (count < 6)
            throw new IllegalArgumentException("Polygons must contain at least 3 points.");
        if (count % 2 != 0)
            throw new IllegalArgumentException("Polygons must have an even number of vertices.");
    
        check(shapeType, null, count);
    
        final float firstX = vertices[0];
        final float firstY = vertices[1];
        if (shapeType == ShapeType.Line)
        {
            for (int i = offset, n = offset + count; i < n; i += 2)
            {
                final float x1 = vertices[i];
                final float y1 = vertices[i + 1];
    
                final float x2;
                final float y2;
    
                if (i + 2 >= count)
                {
                    x2 = firstX;
                    y2 = firstY;
                } else
                {
                    x2 = vertices[i + 2];
                    y2 = vertices[i + 3];
                }
    
                renderer.color(color);
                renderer.vertex(x1, y1, 0);
                renderer.color(color);
                renderer.vertex(x2, y2, 0);
    
            }
        } else
        {
            ShortArray arrRes = ear.computeTriangles(vertices);
    
            for (int i = 0; i < arrRes.size - 2; i = i + 3)
            {
                float x1 = vertices[arrRes.get(i) * 2];
                float y1 = vertices[(arrRes.get(i) * 2) + 1];
    
                float x2 = vertices[(arrRes.get(i + 1)) * 2];
                float y2 = vertices[(arrRes.get(i + 1) * 2) + 1];
    
                float x3 = vertices[arrRes.get(i + 2) * 2];
                float y3 = vertices[(arrRes.get(i + 2) * 2) + 1];
    
                this.triangle(x1, y1, x2, y2, x3, y3);
            }
        }
    }
    

    【讨论】:

    • 如果您添加一些关于您发布的代码为何有效的解释,您可以大大改善您的答案。
    • 太棒了!最后我找到了它,它完全符合我的需要——只是一个简单的旧形状渲染器的简单示例。
    【解决方案2】:

    你还不能用 shaperender 绘制一个填充的多边形。从bugtracker看一下这个

    您也可以在 API 中阅读。

    公共 void 多边形(float[] 顶点)
    在 x/y 平面上绘制一个多边形。顶点必须至少包含 3 点(6 个浮点 x,y)。传递给 begin 的 ShapeRenderer.ShapeType 有 成为ShapeRenderer.ShapeType.Line。


    API ShapeRender
    当然,如果它与 ShapeType.Line 一起使用,您只会得到轮廓。
    在这种情况下,您需要使用三角形自己绘制。至少应该可以填充三角形。
    也许从 Stackoverflow 看一下这个:drawing-filled-polygon-with-libgdx

    【讨论】:

      【解决方案3】:

      编辑您的 ShapeRenderer.java 类,用以下代码替换 polygon() 方法:

      public void polygon(float[] vertices, int offset, int count) {
          if (currType != ShapeType.Filled && currType != ShapeType.Line)
              throw new GdxRuntimeException(
                      "Must call begin(ShapeType.Filled) or begin(ShapeType.Line)");
          if (count < 6)
              throw new IllegalArgumentException(
                      "Polygons must contain at least 3 points.");
          if (count % 2 != 0)
              throw new IllegalArgumentException(
                      "Polygons must have an even number of vertices.");
      
          checkDirty();
          checkFlush(count);
      
          final float firstX = vertices[0];
          final float firstY = vertices[1];
          if (currType == ShapeType.Line) {
              for (int i = offset, n = offset + count; i < n; i += 2) {
                  final float x1 = vertices[i];
                  final float y1 = vertices[i + 1];
      
                  final float x2;
                  final float y2;
      
                  if (i + 2 >= count) {
                      x2 = firstX;
                      y2 = firstY;
                  } else {
                      x2 = vertices[i + 2];
                      y2 = vertices[i + 3];
                  }
      
                  renderer.color(color);
                  renderer.vertex(x1, y1, 0);
                  renderer.color(color);
                  renderer.vertex(x2, y2, 0);
      
              }
          } else {
      
              for (int i = offset, n = offset + count; i < n; i += 4) {
      
                  final float x1 = vertices[i];
                  final float y1 = vertices[i + 1];
      
                  if (i + 2 >= count) {
                      break;
                  }
      
                  final float x2 = vertices[i + 2];
                  final float y2 = vertices[i + 3];
      
                  final float x3;
                  final float y3;
      
                  if (i + 4 >= count) {
                      x3 = firstX;
                      y3 = firstY;
                  } else {
                      x3 = vertices[i + 4];
                      y3 = vertices[i + 5];
                  }
      
                  renderer.color(color);
                  renderer.vertex(x1, y1, 0);
                  renderer.color(color);
                  renderer.vertex(x2, y2, 0);
                  renderer.color(color);
                  renderer.vertex(x3, y3, 0);
      
      
              }
      
          }
      }
      

      用法:

          gdx_shape_renderer.begin(ShapeType.Filled);
          gdx_shape_renderer.setColor(fill_r, fill_g, fill_b, fill_a);
          gdx_shape_renderer.polygon(vertices);
          gdx_shape_renderer.end();
      
          gdx_shape_renderer.begin(ShapeType.Line);
          gdx_shape_renderer.setColor(border_r, border_g, border_b, border_a);
          gdx_shape_renderer.polygon(vertices);
          gdx_shape_renderer.end();
      

      【讨论】:

      • 如果修复这么简单,为什么还没有合并?
      • 因为这不适用于顶点数超过 4 个的多边形。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-09-10
      • 2011-01-04
      • 1970-01-01
      • 2011-11-29
      • 2011-04-21
      • 1970-01-01
      相关资源
      最近更新 更多