Ejemplo n.º 1
0
  /**
   * Reads the star color chart and loads them into a hash
   *
   * @param starColorChart
   * @param starColorMap
   */
  private static void ReadStarColorChart(
      final String starColorChart, final ArrayList<StarColorTuple> starColorMap) {
    int minColor = 0;
    int maxColor = 33000;

    Texture starColorImage = new Texture(VFile.getFileHandle(starColorChart));

    starColorImage.getTextureData().prepare();
    Pixmap pixelData = starColorImage.getTextureData().consumePixmap();

    for (int i = 1; i < starColorImage.getWidth(); i += 5) {
      Color color = new Color();
      Color.rgba8888ToColor(color, pixelData.getPixel(starColorImage.getWidth() - i, 0));
      color.a = 1.0F;

      StarColorTuple tuple =
          new StarColorTuple(
              (int)
                  ((float) (starColorImage.getWidth() - i)
                      / starColorImage.getWidth()
                      * (maxColor - minColor)),
              color);
      // System.err.println("added tuple: " + tuple.getTemperature() + " " +
      // tuple.getColor());
      starColorMap.add(tuple);
    }

    Color color = new Color();
    Color.rgba8888ToColor(color, pixelData.getPixel(starColorImage.getWidth(), 0));
    color.a = 1.0F;
    starColorMap.add(new StarColorTuple(Integer.MIN_VALUE, color));

    starColorImage.dispose();
  }
  public SkeletonData readSkeletonData(FileHandle file) {
    if (file == null) throw new IllegalArgumentException("file cannot be null.");

    float scale = this.scale;

    SkeletonData skeletonData = new SkeletonData();
    skeletonData.name = file.nameWithoutExtension();

    DataInput input = new DataInput(file.read(512));
    try {
      boolean nonessential = input.readBoolean();
      // Bones.
      for (int i = 0, n = input.readInt(true); i < n; i++) {
        String name = input.readString();
        BoneData parent = null;
        int parentIndex = input.readInt(true) - 1;
        if (parentIndex != -1) parent = skeletonData.bones.get(parentIndex);
        BoneData boneData = new BoneData(name, parent);
        boneData.x = input.readFloat() * scale;
        boneData.y = input.readFloat() * scale;
        boneData.scaleX = input.readFloat();
        boneData.scaleY = input.readFloat();
        boneData.rotation = input.readFloat();
        boneData.length = input.readFloat() * scale;
        boneData.inheritScale = input.readBoolean();
        boneData.inheritRotation = input.readBoolean();
        if (nonessential) Color.rgba8888ToColor(boneData.getColor(), input.readInt());
        skeletonData.addBone(boneData);
      }

      // Slots.
      for (int i = 0, n = input.readInt(true); i < n; i++) {
        String slotName = input.readString();
        BoneData boneData = skeletonData.bones.get(input.readInt(true));
        SlotData slotData = new SlotData(slotName, boneData);
        Color.rgba8888ToColor(slotData.getColor(), input.readInt());
        slotData.attachmentName = input.readString();
        slotData.additiveBlending = input.readBoolean();
        skeletonData.addSlot(slotData);
      }

      // Default skin.
      Skin defaultSkin = readSkin(input, "default", nonessential);
      if (defaultSkin != null) {
        skeletonData.defaultSkin = defaultSkin;
        skeletonData.addSkin(defaultSkin);
      }

      // Skins.
      for (int i = 0, n = input.readInt(true); i < n; i++)
        skeletonData.addSkin(readSkin(input, input.readString(), nonessential));

      // Events.
      for (int i = 0, n = input.readInt(true); i < n; i++) {
        EventData eventData = new EventData(input.readString());
        eventData.intValue = input.readInt(false);
        eventData.floatValue = input.readFloat();
        eventData.stringValue = input.readString();
        skeletonData.addEvent(eventData);
      }

      // Animations.
      for (int i = 0, n = input.readInt(true); i < n; i++)
        readAnimation(input.readString(), input, skeletonData);

    } catch (IOException ex) {
      throw new SerializationException("Error reading skeleton file.", ex);
    } finally {
      try {
        input.close();
      } catch (IOException ignored) {
      }
    }

    skeletonData.bones.shrink();
    skeletonData.slots.shrink();
    skeletonData.skins.shrink();
    return skeletonData;
  }
  private void readAnimation(String name, DataInput input, SkeletonData skeletonData) {
    Array<Timeline> timelines = new Array();
    float scale = this.scale;
    float duration = 0;

    try {
      // Slot timelines.
      for (int i = 0, n = input.readInt(true); i < n; i++) {
        int slotIndex = input.readInt(true);
        for (int ii = 0, nn = input.readInt(true); ii < nn; ii++) {
          int timelineType = input.readByte();
          int frameCount = input.readInt(true);
          switch (timelineType) {
            case TIMELINE_COLOR:
              {
                ColorTimeline timeline = new ColorTimeline(frameCount);
                timeline.slotIndex = slotIndex;
                for (int frameIndex = 0; frameIndex < frameCount; frameIndex++) {
                  float time = input.readFloat();
                  Color.rgba8888ToColor(tempColor, input.readInt());
                  timeline.setFrame(
                      frameIndex, time, tempColor.r, tempColor.g, tempColor.b, tempColor.a);
                  if (frameIndex < frameCount - 1) readCurve(input, frameIndex, timeline);
                }
                timelines.add(timeline);
                duration = Math.max(duration, timeline.getFrames()[frameCount * 5 - 5]);
                break;
              }
            case TIMELINE_ATTACHMENT:
              AttachmentTimeline timeline = new AttachmentTimeline(frameCount);
              timeline.slotIndex = slotIndex;
              for (int frameIndex = 0; frameIndex < frameCount; frameIndex++)
                timeline.setFrame(frameIndex, input.readFloat(), input.readString());
              timelines.add(timeline);
              duration = Math.max(duration, timeline.getFrames()[frameCount - 1]);
              break;
          }
        }
      }

      // Bone timelines.
      for (int i = 0, n = input.readInt(true); i < n; i++) {
        int boneIndex = input.readInt(true);
        for (int ii = 0, nn = input.readInt(true); ii < nn; ii++) {
          int timelineType = input.readByte();
          int frameCount = input.readInt(true);
          switch (timelineType) {
            case TIMELINE_ROTATE:
              {
                RotateTimeline timeline = new RotateTimeline(frameCount);
                timeline.boneIndex = boneIndex;
                for (int frameIndex = 0; frameIndex < frameCount; frameIndex++) {
                  timeline.setFrame(frameIndex, input.readFloat(), input.readFloat());
                  if (frameIndex < frameCount - 1) readCurve(input, frameIndex, timeline);
                }
                timelines.add(timeline);
                duration = Math.max(duration, timeline.getFrames()[frameCount * 2 - 2]);
                break;
              }
            case TIMELINE_TRANSLATE:
            case TIMELINE_SCALE:
              TranslateTimeline timeline;
              float timelineScale = 1;
              if (timelineType == TIMELINE_SCALE) timeline = new ScaleTimeline(frameCount);
              else {
                timeline = new TranslateTimeline(frameCount);
                timelineScale = scale;
              }
              timeline.boneIndex = boneIndex;
              for (int frameIndex = 0; frameIndex < frameCount; frameIndex++) {
                timeline.setFrame(
                    frameIndex,
                    input.readFloat(),
                    input.readFloat() * timelineScale,
                    input.readFloat() * timelineScale);
                if (frameIndex < frameCount - 1) readCurve(input, frameIndex, timeline);
              }
              timelines.add(timeline);
              duration = Math.max(duration, timeline.getFrames()[frameCount * 3 - 3]);
              break;
          }
        }
      }

      // FFD timelines.
      for (int i = 0, n = input.readInt(true); i < n; i++) {
        Skin skin = skeletonData.getSkins().get(input.readInt(true) + 1);
        for (int ii = 0, nn = input.readInt(true); ii < nn; ii++) {
          int slotIndex = input.readInt(true);
          for (int iii = 0, nnn = input.readInt(true); iii < nnn; iii++) {
            Attachment attachment = skin.getAttachment(slotIndex, input.readString());
            int frameCount = input.readInt(true);
            FfdTimeline timeline = new FfdTimeline(frameCount);
            timeline.slotIndex = slotIndex;
            timeline.attachment = attachment;
            for (int frameIndex = 0; frameIndex < frameCount; frameIndex++) {
              float time = input.readFloat();

              float[] vertices;
              int vertexCount;
              if (attachment instanceof MeshAttachment)
                vertexCount = ((MeshAttachment) attachment).getVertices().length;
              else vertexCount = ((SkinnedMeshAttachment) attachment).getWeights().length / 3 * 2;

              int end = input.readInt(true);
              if (end == 0) {
                if (attachment instanceof MeshAttachment)
                  vertices = ((MeshAttachment) attachment).getVertices();
                else vertices = new float[vertexCount];
              } else {
                vertices = new float[vertexCount];
                int start = input.readInt(true);
                end += start;
                if (scale == 1) {
                  for (int v = start; v < end; v++) vertices[v] = input.readFloat();
                } else {
                  for (int v = start; v < end; v++) vertices[v] = input.readFloat() * scale;
                }
                if (attachment instanceof MeshAttachment) {
                  float[] meshVertices = ((MeshAttachment) attachment).getVertices();
                  for (int v = 0, vn = vertices.length; v < vn; v++) vertices[v] += meshVertices[v];
                }
              }

              timeline.setFrame(frameIndex, time, vertices);
              if (frameIndex < frameCount - 1) readCurve(input, frameIndex, timeline);
            }
            timelines.add(timeline);
            duration = Math.max(duration, timeline.getFrames()[frameCount - 1]);
          }
        }
      }

      // Draw order timeline.
      int drawOrderCount = input.readInt(true);
      if (drawOrderCount > 0) {
        DrawOrderTimeline timeline = new DrawOrderTimeline(drawOrderCount);
        int slotCount = skeletonData.slots.size;
        for (int i = 0; i < drawOrderCount; i++) {
          int offsetCount = input.readInt(true);
          int[] drawOrder = new int[slotCount];
          for (int ii = slotCount - 1; ii >= 0; ii--) drawOrder[ii] = -1;
          int[] unchanged = new int[slotCount - offsetCount];
          int originalIndex = 0, unchangedIndex = 0;
          for (int ii = 0; ii < offsetCount; ii++) {
            int slotIndex = input.readInt(true);
            // Collect unchanged items.
            while (originalIndex != slotIndex) unchanged[unchangedIndex++] = originalIndex++;
            // Set changed items.
            drawOrder[originalIndex + input.readInt(true)] = originalIndex++;
          }
          // Collect remaining unchanged items.
          while (originalIndex < slotCount) unchanged[unchangedIndex++] = originalIndex++;
          // Fill in unchanged items.
          for (int ii = slotCount - 1; ii >= 0; ii--)
            if (drawOrder[ii] == -1) drawOrder[ii] = unchanged[--unchangedIndex];
          timeline.setFrame(i, input.readFloat(), drawOrder);
        }
        timelines.add(timeline);
        duration = Math.max(duration, timeline.getFrames()[drawOrderCount - 1]);
      }

      // Event timeline.
      int eventCount = input.readInt(true);
      if (eventCount > 0) {
        EventTimeline timeline = new EventTimeline(eventCount);
        for (int i = 0; i < eventCount; i++) {
          float time = input.readFloat();
          EventData eventData = skeletonData.events.get(input.readInt(true));
          Event event = new Event(eventData);
          event.intValue = input.readInt(false);
          event.floatValue = input.readFloat();
          event.stringValue = input.readBoolean() ? input.readString() : eventData.stringValue;
          timeline.setFrame(i, time, event);
        }
        timelines.add(timeline);
        duration = Math.max(duration, timeline.getFrames()[eventCount - 1]);
      }
    } catch (IOException ex) {
      throw new SerializationException("Error reading skeleton file.", ex);
    }

    timelines.shrink();
    skeletonData.addAnimation(new Animation(name, timelines, duration));
  }
  private Attachment readAttachment(
      DataInput input, Skin skin, String attachmentName, boolean nonessential) throws IOException {
    float scale = this.scale;

    String name = input.readString();
    if (name == null) name = attachmentName;

    switch (AttachmentType.values()[input.readByte()]) {
      case region:
        {
          String path = input.readString();
          if (path == null) path = name;
          RegionAttachment region = attachmentLoader.newRegionAttachment(skin, name, path);
          if (region == null) return null;
          region.setPath(path);
          region.setX(input.readFloat() * scale);
          region.setY(input.readFloat() * scale);
          region.setScaleX(input.readFloat());
          region.setScaleY(input.readFloat());
          region.setRotation(input.readFloat());
          region.setWidth(input.readFloat() * scale);
          region.setHeight(input.readFloat() * scale);
          Color.rgba8888ToColor(region.getColor(), input.readInt());
          region.updateOffset();
          return region;
        }
      case boundingbox:
        {
          BoundingBoxAttachment box = attachmentLoader.newBoundingBoxAttachment(skin, name);
          if (box == null) return null;
          box.setVertices(readFloatArray(input, scale));
          return box;
        }
      case mesh:
        {
          String path = input.readString();
          if (path == null) path = name;
          MeshAttachment mesh = attachmentLoader.newMeshAttachment(skin, name, path);
          if (mesh == null) return null;
          mesh.setPath(path);
          float[] uvs = readFloatArray(input, 1);
          short[] triangles = readShortArray(input);
          float[] vertices = readFloatArray(input, scale);
          mesh.setVertices(vertices);
          mesh.setTriangles(triangles);
          mesh.setRegionUVs(uvs);
          mesh.updateUVs();
          Color.rgba8888ToColor(mesh.getColor(), input.readInt());
          mesh.setHullLength(input.readInt(true) * 2);
          if (nonessential) {
            mesh.setEdges(readIntArray(input));
            mesh.setWidth(input.readFloat() * scale);
            mesh.setHeight(input.readFloat() * scale);
          }
          return mesh;
        }
      case skinnedmesh:
        {
          String path = input.readString();
          if (path == null) path = name;
          SkinnedMeshAttachment mesh = attachmentLoader.newSkinnedMeshAttachment(skin, name, path);
          if (mesh == null) return null;
          mesh.setPath(path);
          float[] uvs = readFloatArray(input, 1);
          short[] triangles = readShortArray(input);

          int vertexCount = input.readInt(true);
          FloatArray weights = new FloatArray(uvs.length * 3 * 3);
          IntArray bones = new IntArray(uvs.length * 3);
          for (int i = 0; i < vertexCount; i++) {
            int boneCount = (int) input.readFloat();
            bones.add(boneCount);
            for (int nn = i + boneCount * 4; i < nn; i += 4) {
              bones.add((int) input.readFloat());
              weights.add(input.readFloat() * scale);
              weights.add(input.readFloat() * scale);
              weights.add(input.readFloat());
            }
          }
          mesh.setBones(bones.toArray());
          mesh.setWeights(weights.toArray());
          mesh.setTriangles(triangles);
          mesh.setRegionUVs(uvs);
          mesh.updateUVs();
          Color.rgba8888ToColor(mesh.getColor(), input.readInt());
          mesh.setHullLength(input.readInt(true) * 2);
          if (nonessential) {
            mesh.setEdges(readIntArray(input));
            mesh.setWidth(input.readFloat() * scale);
            mesh.setHeight(input.readFloat() * scale);
          }
          return mesh;
        }
    }
    return null;
  }
Ejemplo n.º 5
0
 public Color int_to_color(int p_int) {
   Color return_color = new Color();
   Color.rgba8888ToColor(return_color, p_int);
   return return_color;
 }
  @Override
  public void init() {
    gl = Gdx.graphics.getGL10();

    spriteBatch = new SpriteBatch();

    worldCamera = new OrthographicCamera();

    worldCamera.setToOrtho(false, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
    // worldCamera.translate(-Gdx.graphics.getWidth() * 0.5f, -Gdx.graphics.getHeight() * 0.5f, 0f);
    worldCamera.update();

    shapeRenderer = new ShapeRenderer();

    resourceManager = new ResourceManagerImpl<String>();

    new LibgdxResourceBuilder(resourceManager) {
      {
        texture("FarmTexture", "pixmapconvexhull/farm.png");
        // texture("FarmTexture", "physicseditor/island01.png");
        sprite("FarmSprite", "FarmTexture");
      }
    };

    farmSprite = resourceManager.getResourceValue("FarmSprite");

    Pixmap pixmap = new Pixmap(Gdx.files.internal("pixmapconvexhull/farm.png"));
    // Pixmap pixmap = new Pixmap(Gdx.files.internal("physicseditor/island01.png"));

    inputDevicesMonitor = new InputDevicesMonitorImpl<String>();
    new LibgdxInputMappingBuilder<String>(inputDevicesMonitor, Gdx.input) {
      {
        monitorPointerDown("touch", 0);
      }
    };

    Gdx.graphics.getGL10().glClearColor(0f, 0f, 1f, 0f);

    points = new Array<Vector2>();

    Color color = new Color();

    for (int i = 0; i < pixmap.getWidth(); i++) {
      for (int j = 0; j < pixmap.getHeight(); j++) {
        int pixel = pixmap.getPixel(i, j);
        Color.rgba8888ToColor(color, pixel);
        if (color.a > 0) {
          convexHull2d.add(i, pixmap.getHeight() - j);
        }
      }
    }

    pixmap.dispose();

    convexHull2d.recalculate();

    int size = convexHull2d.getPointsCount();

    System.out.println(
        "calculation without optimization: " + convexHull2d.getPointsCount() + " points");

    int i = 0;

    // removes aligned points!!

    while (i < size) {
      int p0 = i;
      float x0 = convexHull2d.getX(p0);
      float y0 = convexHull2d.getY(p0);

      boolean aligned = true;

      smallConvexHull2d.add(x0, y0);

      int p1 = 0;
      int p2 = 0;

      do {
        p1 = i + 1;
        p2 = i + 2;

        if (p1 >= size) p1 = p1 % size;

        if (p2 >= size) p2 = p2 % size;

        float x1 = convexHull2d.getX(p1);
        float y1 = convexHull2d.getY(p1);
        float x2 = convexHull2d.getX(p2);
        float y2 = convexHull2d.getY(p2);

        float triangleArea = Math.abs(ShapeUtils.area(x0, y0, x1, y1, x2, y2));
        aligned = triangleArea < 0.01f;

        if (!aligned) break;

        i++;

      } while (aligned && i < size);

      i++;
    }

    smallConvexHull2d.recalculate();

    System.out.println(
        "calculation with optimization: " + smallConvexHull2d.getPointsCount() + " points");
  }