示例#1
0
  public void triangle(float x1, float y1, float x2, float y2, float x3, float y3) {
    if (currType != ShapeType.Filled && currType != ShapeType.Line)
      throw new GdxRuntimeException("Must call begin(ShapeType.Filled) or begin(ShapeType.Line)");
    checkDirty();
    checkFlush(6);
    if (currType == ShapeType.Line) {
      renderer.color(color.r, color.g, color.b, color.a);
      renderer.vertex(x1, y1, 0);
      renderer.color(color.r, color.g, color.b, color.a);
      renderer.vertex(x2, y2, 0);

      renderer.color(color.r, color.g, color.b, color.a);
      renderer.vertex(x2, y2, 0);
      renderer.color(color.r, color.g, color.b, color.a);
      renderer.vertex(x3, y3, 0);

      renderer.color(color.r, color.g, color.b, color.a);
      renderer.vertex(x3, y3, 0);
      renderer.color(color.r, color.g, color.b, color.a);
      renderer.vertex(x1, y1, 0);
    } else {
      renderer.color(color.r, color.g, color.b, color.a);
      renderer.vertex(x1, y1, 0);
      renderer.color(color.r, color.g, color.b, color.a);
      renderer.vertex(x2, y2, 0);
      renderer.color(color.r, color.g, color.b, color.a);
      renderer.vertex(x3, y3, 0);
    }
  }
示例#2
0
  /**
   * Draws a polyline in the x/y plane. The vertices must contain at least 2 points (4 floats x,y).
   * The {@link ShapeType} passed to begin has to be {@link ShapeType#Line}.
   *
   * @param vertices
   */
  public void polyline(float[] vertices, int offset, int count) {
    if (currType != ShapeType.Line)
      throw new GdxRuntimeException("Must call begin(ShapeType.Line)");
    if (count < 4) throw new IllegalArgumentException("Polylines must contain at least 2 points.");
    if (count % 2 != 0)
      throw new IllegalArgumentException("Polylines must have an even number of vertices.");

    checkDirty();
    checkFlush(count);

    for (int i = offset, n = offset + count - 2; i < n; i += 2) {
      float x1 = vertices[i];
      float y1 = vertices[i + 1];

      float x2;
      float y2;

      x2 = vertices[i + 2];
      y2 = vertices[i + 3];

      renderer.color(color.r, color.g, color.b, color.a);
      renderer.vertex(x1, y1, 0);
      renderer.color(color.r, color.g, color.b, color.a);
      renderer.vertex(x2, y2, 0);
    }
  }
示例#3
0
  /**
   * Draws a polyline in the x/y plane. The vertices must contain at least 2 points (4 floats x,y).
   * The {@link ShapeType} passed to begin has to be {@link ShapeType#Line}.
   *
   * @param vertices
   */
  public void polyline(float[] vertices) {
    if (currType != ShapeType.Line)
      throw new GdxRuntimeException("Must call begin(ShapeType.Line)");
    if (vertices.length < 4)
      throw new IllegalArgumentException("Polylines must contain at least 2 points.");
    if (vertices.length % 2 != 0)
      throw new IllegalArgumentException("Polylines must have a pair number of vertices.");
    final int numFloats = vertices.length;

    checkDirty();
    checkFlush(numFloats);

    for (int i = 0; i < numFloats - 2; i += 2) {
      float x1 = vertices[i];
      float y1 = vertices[i + 1];

      float x2;
      float y2;

      x2 = vertices[i + 2];
      y2 = vertices[i + 3];

      renderer.color(color.r, color.g, color.b, color.a);
      renderer.vertex(x1, y1, 0);
      renderer.color(color.r, color.g, color.b, color.a);
      renderer.vertex(x2, y2, 0);
    }
  }
示例#4
0
 /**
  * Draws a point. The {@link ShapeType} passed to begin has to be {@link ShapeType#Point}.
  *
  * @param x
  * @param y
  * @param z
  */
 public void point(float x, float y, float z) {
   if (currType != ShapeType.Point)
     throw new GdxRuntimeException("Must call begin(ShapeType.Point)");
   checkDirty();
   checkFlush(1);
   renderer.color(color.r, color.g, color.b, color.a);
   renderer.vertex(x, y, z);
 }
示例#5
0
 public void cone(float x, float y, float z, float radius, float height, int segments) {
   if (segments <= 0) throw new IllegalArgumentException("segments must be > 0.");
   if (currType != ShapeType.Filled && currType != ShapeType.Line)
     throw new GdxRuntimeException("Must call begin(ShapeType.Filled) or begin(ShapeType.Line)");
   checkDirty();
   checkFlush(segments * 4 + 2);
   float angle = 2 * 3.1415926f / segments;
   float cos = MathUtils.cos(angle);
   float sin = MathUtils.sin(angle);
   float cx = radius, cy = 0;
   if (currType == ShapeType.Line) {
     for (int i = 0; i < segments; i++) {
       renderer.color(color.r, color.g, color.b, color.a);
       renderer.vertex(x + cx, y + cy, z);
       renderer.color(color.r, color.g, color.b, color.a);
       renderer.vertex(x, y, z + height);
       renderer.color(color.r, color.g, color.b, color.a);
       renderer.vertex(x + cx, y + cy, z);
       float temp = cx;
       cx = cos * cx - sin * cy;
       cy = sin * temp + cos * cy;
       renderer.color(color.r, color.g, color.b, color.a);
       renderer.vertex(x + cx, y + cy, z);
     }
     // Ensure the last segment is identical to the first.
     renderer.color(color.r, color.g, color.b, color.a);
     renderer.vertex(x + cx, y + cy, z);
   } else {
     segments--;
     for (int i = 0; i < segments; i++) {
       renderer.color(color.r, color.g, color.b, color.a);
       renderer.vertex(x, y, z);
       renderer.color(color.r, color.g, color.b, color.a);
       renderer.vertex(x + cx, y + cy, z);
       float temp = cx;
       float temp2 = cy;
       cx = cos * cx - sin * cy;
       cy = sin * temp + cos * cy;
       renderer.color(color.r, color.g, color.b, color.a);
       renderer.vertex(x + cx, y + cy, z);
       renderer.color(color.r, color.g, color.b, color.a);
       renderer.vertex(x + temp, y + temp2, z);
       renderer.color(color.r, color.g, color.b, color.a);
       renderer.vertex(x + cx, y + cy, z);
       renderer.color(color.r, color.g, color.b, color.a);
       renderer.vertex(x, y, z + height);
     }
     // Ensure the last segment is identical to the first.
     renderer.color(color.r, color.g, color.b, color.a);
     renderer.vertex(x, y, z);
     renderer.color(color.r, color.g, color.b, color.a);
     renderer.vertex(x + cx, y + cy, z);
   }
   cx = radius;
   cy = 0;
   renderer.color(color.r, color.g, color.b, color.a);
   renderer.vertex(x + cx, y + cy, z);
 }
示例#6
0
 /**
  * Draws a line. The {@link ShapeType} passed to begin has to be {@link ShapeType#Line}. The line
  * is drawn with 2 colors interpolated between start & end point.
  *
  * @param c1 Color at start of the line
  * @param c2 Color at end of the line
  */
 public void line(float x, float y, float z, float x2, float y2, float z2, Color c1, Color c2) {
   if (currType != ShapeType.Line)
     throw new GdxRuntimeException("Must call begin(ShapeType.Line)");
   checkDirty();
   checkFlush(2);
   renderer.color(c1.r, c1.g, c1.b, c1.a);
   renderer.vertex(x, y, z);
   renderer.color(c2.r, c2.g, c2.b, c2.a);
   renderer.vertex(x2, y2, z2);
 }
示例#7
0
 /**
  * Draws a line in the x/y plane. The {@link ShapeType} passed to begin has to be {@link
  * ShapeType#Line}.
  *
  * @param x
  * @param y
  * @param x2
  * @param y2
  */
 public void line(float x, float y, float x2, float y2) {
   if (currType != ShapeType.Line)
     throw new GdxRuntimeException("Must call begin(ShapeType.Line)");
   checkDirty();
   checkFlush(2);
   renderer.color(color.r, color.g, color.b, color.a);
   renderer.vertex(x, y, 0);
   renderer.color(color.r, color.g, color.b, color.a);
   renderer.vertex(x2, y2, 0);
 }
示例#8
0
  public void arc(float x, float y, float radius, float start, float angle, int segments) {
    if (segments <= 0) throw new IllegalArgumentException("segments must be > 0.");
    if (currType != ShapeType.Filled && currType != ShapeType.Line)
      throw new GdxRuntimeException("Must call begin(ShapeType.Filled) or begin(ShapeType.Line)");
    checkDirty();

    float theta = (2 * 3.1415926f * (angle / 360.0f)) / segments;
    float cos = MathUtils.cos(theta);
    float sin = MathUtils.sin(theta);
    float cx = radius * MathUtils.cos(start * MathUtils.degreesToRadians);
    float cy = radius * MathUtils.sin(start * MathUtils.degreesToRadians);

    if (currType == ShapeType.Line) {
      checkFlush(segments * 2 + 2);

      renderer.color(color.r, color.g, color.b, color.a);
      renderer.vertex(x, y, 0);
      renderer.color(color.r, color.g, color.b, color.a);
      renderer.vertex(x + cx, y + cy, 0);
      for (int i = 0; i < segments; i++) {
        renderer.color(color.r, color.g, color.b, color.a);
        renderer.vertex(x + cx, y + cy, 0);
        float temp = cx;
        cx = cos * cx - sin * cy;
        cy = sin * temp + cos * cy;
        renderer.color(color.r, color.g, color.b, color.a);
        renderer.vertex(x + cx, y + cy, 0);
      }
      renderer.color(color.r, color.g, color.b, color.a);
      renderer.vertex(x + cx, y + cy, 0);
    } else {
      checkFlush(segments * 3 + 3);
      for (int i = 0; i < segments; i++) {
        renderer.color(color.r, color.g, color.b, color.a);
        renderer.vertex(x, y, 0);
        renderer.color(color.r, color.g, color.b, color.a);
        renderer.vertex(x + cx, y + cy, 0);
        float temp = cx;
        cx = cos * cx - sin * cy;
        cy = sin * temp + cos * cy;
        renderer.color(color.r, color.g, color.b, color.a);
        renderer.vertex(x + cx, y + cy, 0);
      }
      renderer.color(color.r, color.g, color.b, color.a);
      renderer.vertex(x, y, 0);
      renderer.color(color.r, color.g, color.b, color.a);
      renderer.vertex(x + cx, y + cy, 0);
    }

    float temp = cx;
    cx = 0;
    cy = 0;
    renderer.color(color.r, color.g, color.b, color.a);
    renderer.vertex(x + cx, y + cy, 0);
  }
示例#9
0
  /**
   * Draws a rectangle in the x/y plane. The x and y coordinate specify the bottom left corner of
   * the rectangle. The {@link ShapeType} passed to begin has to be {@link ShapeType#Filled} or
   * {@link ShapeType#Line}.
   *
   * @param col1 The color at (x, y)
   * @param col2 The color at (x + width, y)
   * @param col3 The color at (x + width, y + height)
   * @param col4 The color at (x, y + height)
   */
  public void rect(
      float x, float y, float width, float height, Color col1, Color col2, Color col3, Color col4) {
    if (currType != ShapeType.Filled && currType != ShapeType.Line)
      throw new GdxRuntimeException("Must call begin(ShapeType.Filled) or begin(ShapeType.Line)");

    checkDirty();
    checkFlush(8);

    if (currType == ShapeType.Line) {
      renderer.color(col1.r, col1.g, col1.b, col1.a);
      renderer.vertex(x, y, 0);
      renderer.color(col2.r, col2.g, col2.b, col2.a);
      renderer.vertex(x + width, y, 0);

      renderer.color(col2.r, col2.g, col2.b, col2.a);
      renderer.vertex(x + width, y, 0);
      renderer.color(col3.r, col3.g, col3.b, col3.a);
      renderer.vertex(x + width, y + height, 0);

      renderer.color(col3.r, col3.g, col3.b, col3.a);
      renderer.vertex(x + width, y + height, 0);
      renderer.color(col4.r, col4.g, col4.b, col4.a);
      renderer.vertex(x, y + height, 0);

      renderer.color(col4.r, col4.g, col4.b, col4.a);
      renderer.vertex(x, y + height, 0);
      renderer.color(col1.r, col1.g, col1.b, col1.a);
      renderer.vertex(x, y, 0);
    } else {
      renderer.color(col1.r, col1.g, col1.b, col1.a);
      renderer.vertex(x, y, 0);
      renderer.color(col2.r, col2.g, col2.b, col2.a);
      renderer.vertex(x + width, y, 0);
      renderer.color(col3.r, col3.g, col3.b, col3.a);
      renderer.vertex(x + width, y + height, 0);

      renderer.color(col3.r, col3.g, col3.b, col3.a);
      renderer.vertex(x + width, y + height, 0);
      renderer.color(col4.r, col4.g, col4.b, col4.a);
      renderer.vertex(x, y + height, 0);
      renderer.color(col1.r, col1.g, col1.b, col1.a);
      renderer.vertex(x, y, 0);
    }
  }
示例#10
0
  public void ellipse(float x, float y, float width, float height, int segments) {
    if (segments <= 0) throw new IllegalArgumentException("segments must be > 0.");
    if (currType != ShapeType.Filled && currType != ShapeType.Line)
      throw new GdxRuntimeException("Must call begin(ShapeType.Filled) or begin(ShapeType.Line)");
    checkDirty();
    checkFlush(segments * 3);

    float angle = 2 * 3.1415926f / segments;

    float cx = x + width / 2, cy = y + height / 2;
    if (currType == ShapeType.Line) {
      for (int i = 0; i < segments; i++) {
        renderer.color(color.r, color.g, color.b, color.a);
        renderer.vertex(
            cx + (width * 0.5f * MathUtils.cos(i * angle)),
            cy + (height * 0.5f * MathUtils.sin(i * angle)),
            0);

        renderer.color(color.r, color.g, color.b, color.a);
        renderer.vertex(
            cx + (width * 0.5f * MathUtils.cos((i + 1) * angle)),
            cy + (height * 0.5f * MathUtils.sin((i + 1) * angle)),
            0);
      }
    } else {
      for (int i = 0; i < segments; i++) {
        renderer.color(color.r, color.g, color.b, color.a);
        renderer.vertex(
            cx + (width * 0.5f * MathUtils.cos(i * angle)),
            cy + (height * 0.5f * MathUtils.sin(i * angle)),
            0);

        renderer.color(color.r, color.g, color.b, color.a);
        renderer.vertex(cx, cy, 0);

        renderer.color(color.r, color.g, color.b, color.a);
        renderer.vertex(
            cx + (width * 0.5f * MathUtils.cos((i + 1) * angle)),
            cy + (height * 0.5f * MathUtils.sin((i + 1) * angle)),
            0);
      }
    }
  }
示例#11
0
  /**
   * Draws a box. The x, y and z coordinate specify the bottom left front corner of the rectangle.
   * The {@link ShapeType} passed to begin has to be {@link ShapeType#Line}.
   */
  public void box(float x, float y, float z, float width, float height, float depth) {
    if (currType != ShapeType.Line)
      throw new GdxRuntimeException("Must call begin(ShapeType.Line)");

    checkDirty();
    checkFlush(16);

    depth = -depth;

    renderer.color(color.r, color.g, color.b, color.a);
    renderer.vertex(x, y, z);
    renderer.color(color.r, color.g, color.b, color.a);
    renderer.vertex(x + width, y, z);

    renderer.color(color.r, color.g, color.b, color.a);
    renderer.vertex(x + width, y, z);
    renderer.color(color.r, color.g, color.b, color.a);
    renderer.vertex(x + width, y, z + depth);

    renderer.color(color.r, color.g, color.b, color.a);
    renderer.vertex(x + width, y, z + depth);
    renderer.color(color.r, color.g, color.b, color.a);
    renderer.vertex(x, y, z + depth);

    renderer.color(color.r, color.g, color.b, color.a);
    renderer.vertex(x, y, z + depth);
    renderer.color(color.r, color.g, color.b, color.a);
    renderer.vertex(x, y, z);

    renderer.color(color.r, color.g, color.b, color.a);
    renderer.vertex(x, y, z);
    renderer.color(color.r, color.g, color.b, color.a);
    renderer.vertex(x, y + height, z);

    renderer.color(color.r, color.g, color.b, color.a);
    renderer.vertex(x, y + height, z);
    renderer.color(color.r, color.g, color.b, color.a);
    renderer.vertex(x + width, y + height, z);

    renderer.color(color.r, color.g, color.b, color.a);
    renderer.vertex(x + width, y + height, z);
    renderer.color(color.r, color.g, color.b, color.a);
    renderer.vertex(x + width, y + height, z + depth);

    renderer.color(color.r, color.g, color.b, color.a);
    renderer.vertex(x + width, y + height, z + depth);
    renderer.color(color.r, color.g, color.b, color.a);
    renderer.vertex(x, y + height, z + depth);

    renderer.color(color.r, color.g, color.b, color.a);
    renderer.vertex(x, y + height, z + depth);
    renderer.color(color.r, color.g, color.b, color.a);
    renderer.vertex(x, y + height, z);

    renderer.color(color.r, color.g, color.b, color.a);
    renderer.vertex(x + width, y, z);
    renderer.color(color.r, color.g, color.b, color.a);
    renderer.vertex(x + width, y + height, z);

    renderer.color(color.r, color.g, color.b, color.a);
    renderer.vertex(x + width, y, z + depth);
    renderer.color(color.r, color.g, color.b, color.a);
    renderer.vertex(x + width, y + height, z + depth);

    renderer.color(color.r, color.g, color.b, color.a);
    renderer.vertex(x, y, z + depth);
    renderer.color(color.r, color.g, color.b, color.a);
    renderer.vertex(x, y + height, z + depth);
  }
示例#12
0
  /**
   * Draws a rectangle in the x/y plane. The x and y coordinate specify the bottom left corner of
   * the rectangle. The originX and originY specify the point about which to rotate the rectangle.
   * The rotation is in degrees. The {@link ShapeType} passed to begin has to be {@link
   * ShapeType#Filled} or {@link ShapeType#Line}.
   *
   * @param col1 The color at (x, y)
   * @param col2 The color at (x + width, y)
   * @param col3 The color at (x + width, y + height)
   * @param col4 The color at (x, y + height)
   */
  public void rect(
      float x,
      float y,
      float width,
      float height,
      float originX,
      float originY,
      float rotation,
      Color col1,
      Color col2,
      Color col3,
      Color col4) {
    if (currType != ShapeType.Filled && currType != ShapeType.Line)
      throw new GdxRuntimeException("Must call begin(ShapeType.Filled) or begin(ShapeType.Line)");

    checkDirty();
    checkFlush(8);

    float r = (float) Math.toRadians(rotation);
    float c = (float) Math.cos(r);
    float s = (float) Math.sin(r);

    float x1, y1, x2, y2, x3, y3, x4, y4;

    x1 = x + c * (0 - originX) + -s * (0 - originY);
    y1 = y + s * (0 - originY) + c * (0 - originY);

    x2 = x + c * (width - originX) + -s * (0 - originY);
    y2 = y + s * (width - originY) + c * (0 - originY);

    x3 = x + c * (width - originX) + -s * (height - originY);
    y3 = y + s * (width - originY) + c * (height - originY);

    x4 = x + c * (0 - originX) + -s * (height - originY);
    y4 = y + s * (0 - originY) + c * (height - originY);

    if (currType == ShapeType.Line) {
      renderer.color(col1.r, col1.g, col1.b, col1.a);
      renderer.vertex(x1, y1, 0);
      renderer.color(col2.r, col2.g, col2.b, col2.a);
      renderer.vertex(x2, y2, 0);

      renderer.color(col2.r, col2.g, col2.b, col2.a);
      renderer.vertex(x2, y2, 0);
      renderer.color(col3.r, col3.g, col3.b, col3.a);
      renderer.vertex(x3, y3, 0);

      renderer.color(col3.r, col3.g, col3.b, col3.a);
      renderer.vertex(x3, y3, 0);
      renderer.color(col4.r, col4.g, col4.b, col4.a);
      renderer.vertex(x4, y4, 0);

      renderer.color(col4.r, col4.g, col4.b, col4.a);
      renderer.vertex(x4, y4, 0);
      renderer.color(col1.r, col1.g, col1.b, col1.a);
      renderer.vertex(x1, y1, 0);
    } else {
      renderer.color(color.r, color.g, color.b, color.a);
      renderer.vertex(x1, y1, 0);
      renderer.color(color.r, color.g, color.b, color.a);
      renderer.vertex(x2, y2, 0);
      renderer.color(color.r, color.g, color.b, color.a);
      renderer.vertex(x3, y3, 0);

      renderer.color(color.r, color.g, color.b, color.a);
      renderer.vertex(x3, y3, 0);
      renderer.color(color.r, color.g, color.b, color.a);
      renderer.vertex(x4, y4, 0);
      renderer.color(color.r, color.g, color.b, color.a);
      renderer.vertex(x1, y1, 0);
    }
  }
示例#13
0
  public void curve(
      float x1,
      float y1,
      float cx1,
      float cy1,
      float cx2,
      float cy2,
      float x2,
      float y2,
      int segments) {
    if (currType != ShapeType.Line)
      throw new GdxRuntimeException("Must call begin(ShapeType.Line)");
    checkDirty();
    checkFlush(segments * 2 + 2);

    // Algorithm from:
    // http://www.antigrain.com/research/bezier_interpolation/index.html#PAGE_BEZIER_INTERPOLATION
    float subdiv_step = 1f / segments;
    float subdiv_step2 = subdiv_step * subdiv_step;
    float subdiv_step3 = subdiv_step * subdiv_step * subdiv_step;

    float pre1 = 3 * subdiv_step;
    float pre2 = 3 * subdiv_step2;
    float pre4 = 6 * subdiv_step2;
    float pre5 = 6 * subdiv_step3;

    float tmp1x = x1 - cx1 * 2 + cx2;
    float tmp1y = y1 - cy1 * 2 + cy2;

    float tmp2x = (cx1 - cx2) * 3 - x1 + x2;
    float tmp2y = (cy1 - cy2) * 3 - y1 + y2;

    float fx = x1;
    float fy = y1;

    float dfx = (cx1 - x1) * pre1 + tmp1x * pre2 + tmp2x * subdiv_step3;
    float dfy = (cy1 - y1) * pre1 + tmp1y * pre2 + tmp2y * subdiv_step3;

    float ddfx = tmp1x * pre4 + tmp2x * pre5;
    float ddfy = tmp1y * pre4 + tmp2y * pre5;

    float dddfx = tmp2x * pre5;
    float dddfy = tmp2y * pre5;

    while (segments-- > 0) {
      renderer.color(color.r, color.g, color.b, color.a);
      renderer.vertex(fx, fy, 0);
      fx += dfx;
      fy += dfy;
      dfx += ddfx;
      dfy += ddfy;
      ddfx += dddfx;
      ddfy += dddfy;
      renderer.color(color.r, color.g, color.b, color.a);
      renderer.vertex(fx, fy, 0);
    }
    renderer.color(color.r, color.g, color.b, color.a);
    renderer.vertex(fx, fy, 0);
    renderer.color(color.r, color.g, color.b, color.a);
    renderer.vertex(x2, y2, 0);
  }