Пример #1
0
 public void applyMatricesToVertices(MDL mdlr) {
   int sz = numVerteces();
   for (int i = 0; i < sz; i++) {
     GeosetVertex gv = vertex.get(i);
     gv.clearBoneAttachments();
     Matrix mx = getMatrix(gv.getVertexGroup());
     mx.updateIds(mdlr);
     int szmx = mx.size();
     for (int m = 0; m < szmx; m++) {
       gv.addBoneAttachment((Bone) mdlr.getIdObject(mx.getBoneId(m)));
     }
   }
 }
Пример #2
0
 public void applyVerticesToMatrices(MDL mdlr) {
   matrix.clear();
   for (int i = 0; i < vertex.size(); i++) {
     Matrix newTemp = new Matrix(vertex.get(i).bones);
     boolean newMatrix = true;
     for (int m = 0; m < matrix.size() && newMatrix; m++) {
       if (newTemp.equals(matrix.get(m))) {
         newTemp = matrix.get(m);
         newMatrix = false;
       }
     }
     if (newMatrix) {
       matrix.add(newTemp);
       newTemp.updateIds(mdlr);
     }
     vertex.get(i).VertexGroup = matrix.indexOf(newTemp);
     vertex.get(i).setMatrix(newTemp);
   }
 }
Пример #3
0
 public void updateToObjects(MDL mdlr) {
   // upload the temporary UVLayer and Matrix objects into the vertices themselves
   int sz = numVerteces();
   for (Matrix m : matrix) {
     m.updateBones(mdlr);
   }
   for (int i = 0; i < sz; i++) {
     GeosetVertex gv = vertex.get(i);
     gv.clearTVerts();
     int szuv = uvlayers.size();
     for (int l = 0; l < szuv; l++) {
       try {
         gv.addTVertex(uvlayers.get(l).getTVertex(i));
       } catch (Exception e) {
         JOptionPane.showMessageDialog(
             MDLReader.getDefaultContainer(),
             "Error: Length of TVertices and Vertices chunk differ (Or some other unknown error has occurred)!");
       }
     }
     Matrix mx = getMatrix(gv.getVertexGroup());
     int szmx = mx.size();
     gv.clearBoneAttachments();
     for (int m = 0; m < szmx; m++) {
       gv.addBoneAttachment((Bone) mdlr.getIdObject(mx.getBoneId(m)));
     }
     gv.setNormal(normals.get(i));
     for (Triangle t : triangle) {
       if (t.containsRef(gv)) {
         gv.triangles.add(t);
       }
     }
     gv.geoset = this;
     //             gv.addBoneAttachment(null);//Why was this here?
   }
   try {
     material = mdlr.getMaterial(materialID);
   } catch (ArrayIndexOutOfBoundsException e) {
     JOptionPane.showMessageDialog(null, "Error: Material index out of bounds for geoset!");
   }
   parentModel = mdlr;
 }
Пример #4
0
  /**
   * Compute the bounding screen extent of a rotated rectangle.
   *
   * @param rect Rectangle to rotate.
   * @param x X coordinate of the rotation point.
   * @param y Y coordinate of the rotation point.
   * @param rotation Rotation angle.
   * @return The smallest rectangle that completely contains {@code rect} when rotated by the
   *     specified angle.
   */
  protected Rectangle computeRotatedScreenExtent(Rectangle rect, int x, int y, Angle rotation) {
    Rectangle r = new Rectangle(rect);

    // Translate the rectangle to the rotation point.
    r.translate(-x, -y);

    // Compute corner points
    Vec4[] corners = {
      new Vec4(r.getMaxX(), r.getMaxY()),
      new Vec4(r.getMaxX(), r.getMinY()),
      new Vec4(r.getMinX(), r.getMaxY()),
      new Vec4(r.getMinX(), r.getMinY())
    };

    // Rotate the rectangle
    Matrix rotationMatrix = Matrix.fromRotationZ(rotation);
    for (int i = 0; i < corners.length; i++) {
      corners[i] = corners[i].transformBy3(rotationMatrix);
    }

    // Find the bounding rectangle of rotated points.
    int minX = Integer.MAX_VALUE;
    int minY = Integer.MAX_VALUE;
    int maxX = -Integer.MAX_VALUE;
    int maxY = -Integer.MAX_VALUE;

    for (Vec4 v : corners) {
      if (v.x > maxX) maxX = (int) v.x;

      if (v.x < minX) minX = (int) v.x;

      if (v.y > maxY) maxY = (int) v.y;

      if (v.y < minY) minY = (int) v.y;
    }

    // Set bounds and translate the rectangle back to where it started.
    r.setBounds(minX, minY, maxX - minX, maxY - minY);
    r.translate(x, y);

    return r;
  }
Пример #5
0
  public void doSavePrep(MDL mdlr) {
    purifyFaces();

    // Normals cleared here, in case that becomes a problem later.
    normals.clear();
    // UV Layers cleared here
    uvlayers.clear();
    int bigNum = 0;
    int littleNum = -1;
    for (int i = 0; i < vertex.size(); i++) {
      int temp = vertex.get(i).tverts.size();
      if (temp > bigNum) {
        bigNum = temp;
      }
      if (littleNum == -1 || temp < littleNum) {
        littleNum = temp;
      }
    }
    if (littleNum != bigNum) {
      JOptionPane.showMessageDialog(
          null,
          "Error: Attempting to save a Geoset with Verteces that have differing numbers of TVertices! Empty TVertices will be autogenerated.");
    }
    for (int i = 0; i < bigNum; i++) {
      uvlayers.add(new UVLayer());
    }
    for (int i = 0; i < vertex.size(); i++) {
      normals.add(vertex.get(i).getNormal());
      for (int uv = 0; uv < bigNum; uv++) {
        TVertex temp = vertex.get(i).getTVertex(uv);
        if (temp != null) {
          uvlayers.get(uv).addTVertex(temp);
        } else {
          uvlayers.get(uv).addTVertex(new TVertex(0, 0));
        }
      }
    }
    // Clearing matrix list
    matrix.clear();
    for (int i = 0; i < vertex.size(); i++) {
      Matrix newTemp = new Matrix(vertex.get(i).bones);
      boolean newMatrix = true;
      for (int m = 0; m < matrix.size() && newMatrix; m++) {
        if (newTemp.equals(matrix.get(m))) {
          newTemp = matrix.get(m);
          newMatrix = false;
        }
      }
      if (newMatrix) {
        matrix.add(newTemp);
        newTemp.updateIds(mdlr);
      }
      vertex.get(i).VertexGroup = matrix.indexOf(newTemp);
      vertex.get(i).setMatrix(newTemp);
    }
    for (int i = 0; i < triangle.size(); i++) {
      triangle.get(i).updateVertexIds(this);
    }
    int boneRefCount = 0;
    for (int i = 0; i < matrix.size(); i++) {
      boneRefCount += matrix.get(i).bones.size();
    }
    for (int i = 0; i < matrix.size(); i++) {
      matrix.get(i).updateIds(mdlr);
    }
  }
Пример #6
0
  public void printTo(PrintWriter writer, MDL mdlr, boolean trianglesTogether) {
    purifyFaces();
    writer.println("Geoset {");
    writer.println("\tVertices " + vertex.size() + " {");

    String tabs = "\t\t";
    // Normals cleared here, in case that becomes a problem later.
    normals.clear();
    // UV Layers cleared here
    uvlayers.clear();
    int bigNum = 0;
    int littleNum = -1;
    for (int i = 0; i < vertex.size(); i++) {
      int temp = vertex.get(i).tverts.size();
      if (temp > bigNum) {
        bigNum = temp;
      }
      if (littleNum == -1 || temp < littleNum) {
        littleNum = temp;
      }
    }
    if (littleNum != bigNum) {
      JOptionPane.showMessageDialog(
          null,
          "Error: Attempting to save a Geoset with Verteces that have differing numbers of TVertices! Empty TVertices will be autogenerated.");
    }
    for (int i = 0; i < bigNum; i++) {
      uvlayers.add(new UVLayer());
    }
    for (int i = 0; i < vertex.size(); i++) {
      writer.println(tabs + vertex.get(i).toString() + ",");
      normals.add(vertex.get(i).getNormal());
      for (int uv = 0; uv < bigNum; uv++) {
        try {
          TVertex temp = vertex.get(i).getTVertex(uv);
          if (temp != null) {
            uvlayers.get(uv).addTVertex(temp);
          } else {
            uvlayers.get(uv).addTVertex(new TVertex(0, 0));
          }
        } catch (IndexOutOfBoundsException e) {
          uvlayers.get(uv).addTVertex(new TVertex(0, 0));
        }
      }
    }
    writer.println("\t}");
    writer.println("\tNormals " + normals.size() + " {");
    for (int i = 0; i < normals.size(); i++) {
      writer.println(tabs + normals.get(i).toString() + ",");
    }
    writer.println("\t}");
    for (int i = 0; i < uvlayers.size(); i++) {
      uvlayers.get(i).printTo(writer, 1, true);
    }
    // Clearing matrix list
    matrix.clear();
    writer.println("\tVertexGroup {");
    for (int i = 0; i < vertex.size(); i++) {
      Matrix newTemp = new Matrix(vertex.get(i).bones);
      boolean newMatrix = true;
      for (int m = 0; m < matrix.size() && newMatrix; m++) {
        if (newTemp.equals(matrix.get(m))) {
          newTemp = matrix.get(m);
          newMatrix = false;
        }
      }
      if (newMatrix) {
        matrix.add(newTemp);
        newTemp.updateIds(mdlr);
      }
      vertex.get(i).VertexGroup = matrix.indexOf(newTemp);
      vertex.get(i).setMatrix(newTemp);
      writer.println(tabs + vertex.get(i).VertexGroup + ",");
    }
    writer.println("\t}");
    if (trianglesTogether) {
      writer.println("\tFaces 1 " + (triangle.size() * 3) + " {");
      writer.println("\t\tTriangles {");
      String triangleOut = "\t\t\t{ ";
      for (int i = 0; i < triangle.size(); i++) {
        triangle.get(i).updateVertexIds(this);
        if (i != triangle.size() - 1) {
          triangleOut = triangleOut + triangle.get(i).toString() + ", ";
        } else {
          triangleOut = triangleOut + triangle.get(i).toString() + " ";
        }
      }
      writer.println(triangleOut + "},");
      writer.println("\t\t}");
    } else {
      writer.println("\tFaces " + triangle.size() + " " + (triangle.size() * 3) + " {");
      writer.println("\t\tTriangles {");
      String triangleOut = "\t\t\t{ ";
      for (int i = 0; i < triangle.size(); i++) {
        triangle.get(i).updateVertexIds(this);
        writer.println(triangleOut + triangle.get(i).toString() + " },");
      }
      writer.println("\t\t}");
    }
    writer.println("\t}");
    int boneRefCount = 0;
    for (int i = 0; i < matrix.size(); i++) {
      boneRefCount += matrix.get(i).bones.size();
    }
    writer.println("\tGroups " + matrix.size() + " " + boneRefCount + " {");
    for (int i = 0; i < matrix.size(); i++) {
      matrix.get(i).updateIds(mdlr);
      matrix.get(i).printTo(writer, 2); // 2 is the tab height
    }
    writer.println("\t}");
    if (extents != null) extents.printTo(writer, 1);
    for (int i = 0; i < anims.size(); i++) {
      anims.get(i).printTo(writer, 1);
    }

    writer.println("\tMaterialID " + materialID + ",");
    writer.println("\tSelectionGroup " + selectionGroup + ",");
    for (int i = 0; i < flags.size(); i++) {
      writer.println("\t" + flags.get(i) + ",");
    }

    writer.println("}");
  }
Пример #7
0
  public static Geoset read(BufferedReader mdl) {
    String line = MDLReader.nextLine(mdl);
    System.out.println("geo begins with " + line);
    if (line.contains("Geoset")) {
      line = MDLReader.nextLine(mdl);
      Geoset geo = new Geoset();
      if (!line.contains("Vertices")) {
        JOptionPane.showMessageDialog(
            MDLReader.getDefaultContainer(), "Error: Vertices not found at beginning of Geoset!");
      }
      while (!((line = MDLReader.nextLine(mdl)).contains("\t}"))) {
        geo.addVertex(GeosetVertex.parseText(line));
      }
      line = MDLReader.nextLine(mdl);
      if (line.contains("Normals")) {
        // If we have normals:
        while (!((line = MDLReader.nextLine(mdl)).contains("\t}"))) {
          geo.addNormal(Normal.parseText(line));
        }
      }
      while (((line = MDLReader.nextLine(mdl)).contains("TVertices"))) {
        geo.addUVLayer(UVLayer.read(mdl));
      }
      if (!line.contains("VertexGroup")) {
        JOptionPane.showMessageDialog(
            MDLReader.getDefaultContainer(), "Error: VertexGroups missing or invalid!");
      }
      int i = 0;
      while (!((line = MDLReader.nextLine(mdl)).contains("\t}"))) {
        geo.getVertex(i).setVertexGroup(MDLReader.readInt(line));
        i++;
      }
      line = MDLReader.nextLine(mdl);
      if (!line.contains("Faces")) {
        JOptionPane.showMessageDialog(
            MDLReader.getDefaultContainer(), "Error: Faces missing or invalid!");
      }
      line = MDLReader.nextLine(mdl);
      if (!line.contains("Triangles")) {
        System.out.println(line);
        JOptionPane.showMessageDialog(
            MDLReader.getDefaultContainer(), "Error: Triangles missing or invalid!");
      }
      geo.setTriangles(Triangle.read(mdl, geo));
      line = MDLReader.nextLine(mdl); // Throw away the \t} closer for faces
      line = MDLReader.nextLine(mdl);
      if (!line.contains("Groups")) {
        JOptionPane.showMessageDialog(
            MDLReader.getDefaultContainer(), "Error: Groups (Matrices) missing or invalid!");
      }
      while (!((line = MDLReader.nextLine(mdl)).contains("\t}"))) {
        geo.addMatrix(Matrix.parseText(line));
      }
      MDLReader.mark(mdl);
      line = MDLReader.nextLine(mdl);
      while (!line.contains("}") || line.contains("},")) {
        if (line.contains("Extent") || line.contains("BoundsRadius")) {
          System.out.println("Parsing geoset extLog:" + line);
          MDLReader.reset(mdl);
          geo.setExtLog(ExtLog.read(mdl));
          System.out.println("Completed geoset extLog.");
        } else if (line.contains("Anim")) {
          MDLReader.reset(mdl);
          geo.add(Animation.read(mdl));
          MDLReader.mark(mdl);
        } else if (line.contains("MaterialID")) {
          geo.materialID = MDLReader.readInt(line);
          MDLReader.mark(mdl);
        } else if (line.contains("SelectionGroup")) {
          geo.selectionGroup = MDLReader.readInt(line);
          MDLReader.mark(mdl);
        } else {
          geo.addFlag(MDLReader.readFlag(line));
          System.out.println("Reading to geoFlag: " + line);
          MDLReader.mark(mdl);
        }
        line = MDLReader.nextLine(mdl);
      }
      //             JOptionPane.showMessageDialog(MDLReader.getDefaultContainer(),"Geoset reading
      // completed!");
      System.out.println("Geoset reading completed!");

      return geo;
    } else {
      JOptionPane.showMessageDialog(
          MDLReader.getDefaultContainer(),
          "Unable to parse Geoset: Missing or unrecognized open statement '" + line + "'.");
    }
    return null;
  }
Пример #8
0
  /**
   * Compute the label's screen position from its geographic position.
   *
   * @param dc Current draw context.
   */
  protected void computeGeometry(DrawContext dc) {
    // Project the label position onto the viewport
    Position pos = this.getPosition();
    if (pos == null) return;

    this.placePoint = dc.computeTerrainPoint(pos.getLatitude(), pos.getLongitude(), 0);
    this.screenPlacePoint = dc.getView().project(this.placePoint);

    this.eyeDistance = this.placePoint.distanceTo3(dc.getView().getEyePoint());

    boolean orientationReversed = false;
    if (this.orientationPosition != null) {
      // Project the orientation point onto the screen
      Vec4 orientationPlacePoint =
          dc.computeTerrainPoint(
              this.orientationPosition.getLatitude(), this.orientationPosition.getLongitude(), 0);
      Vec4 orientationScreenPoint = dc.getView().project(orientationPlacePoint);

      this.rotation = this.computeRotation(this.screenPlacePoint, orientationScreenPoint);

      // The orientation is reversed if the orientation point falls to the right of the screen
      // point. Text is
      // never drawn upside down, so when the orientation is reversed the text flips vertically to
      // keep the text
      // right side up.
      orientationReversed = (orientationScreenPoint.x <= this.screenPlacePoint.x);
    }

    this.computeBoundsIfNeeded(dc);

    Offset offset = this.getOffset();
    Point2D offsetPoint =
        offset.computeOffset(this.bounds.getWidth(), this.bounds.getHeight(), null, null);

    // If a rotation is applied to the text, then rotate the offset as well. An offset in the x
    // direction
    // will move the text along the orientation line, and a offset in the y direction will move the
    // text
    // perpendicular to the orientation line.
    if (this.rotation != null) {
      double dy = offsetPoint.getY();

      // If the orientation is reversed we need to adjust the vertical offset to compensate for the
      // flipped
      // text. For example, if the offset normally aligns the top of the text with the place point
      // then without
      // this adjustment the bottom of the text would align with the place point when the
      // orientation is
      // reversed.
      if (orientationReversed) {
        dy = -(dy + this.bounds.getHeight());
      }

      Vec4 pOffset = new Vec4(offsetPoint.getX(), dy);
      Matrix rot = Matrix.fromRotationZ(this.rotation.multiply(-1));

      pOffset = pOffset.transformBy3(rot);

      offsetPoint = new Point((int) pOffset.getX(), (int) pOffset.getY());
    }

    int x = (int) (this.screenPlacePoint.x + offsetPoint.getX());
    int y = (int) (this.screenPlacePoint.y - offsetPoint.getY());

    this.screenPoint = new Point(x, y);
    this.screenExtent = this.computeTextExtent(x, y, this.rotation);
  }