Beispiel #1
0
    public void mouseDragged(MouseEvent e) {

      oldMoveVector = new Vector3f(fixOldX, oldY, oldZ);
      oldWorldZMoveVector = new Vector3f(oldX, oldY, oldZ);

      // get x,y
      newX = e.getX() / (adjustToScreenSize / 2) - 1; // used to be (float)jframe.getWidth()
      newY = 1 - e.getY() / (adjustToScreenSize / 2); // used to be (float)jframe.getWidth()
      newZ = calculateZ(newX, newY);

      newMoveVector = new Vector3f(fixNewX, newY, newZ);
      newWorldZMoveVector = new Vector3f(newX, newY, newZ);

      // now calculate the Camera movement
      Vector3f moveAxis = new Vector3f(); // moving around this axis
      moveAxis.cross(
          oldMoveVector,
          newMoveVector); // TODO identify, if the axis points down or upwards => adjust the angle
      float moveAngle = oldMoveVector.angle(newMoveVector); // moving by this angle

      // special Vector including the angle
      AxisAngle4f moveAxisAngle = new AxisAngle4f();
      moveAxisAngle.set(moveAxis, moveAngle);

      // calculate turning axis
      mouseTurn = new Matrix4f();
      mouseTurn.setIdentity();
      mouseTurn.set(moveAxisAngle); // the new Multiplication-Matrix

      // now calculate the World movement
      Vector3f worldMoveAxis = new Vector3f(); // moving around this axis
      worldMoveAxis.x = 0;
      worldMoveAxis.y = 0;
      worldMoveAxis.z = 1;
      float worldMoveAngle = oldWorldZMoveVector.angle(newWorldZMoveVector); // moving by this angle

      // adjust world Angle for turning back
      Vector3f worldMoveAxisTemp = new Vector3f();
      worldMoveAxisTemp.cross(oldWorldZMoveVector, newWorldZMoveVector);
      if (worldMoveAxisTemp.z < 0) {
        worldMoveAngle = -worldMoveAngle;
      }

      // special Vector including the angle
      AxisAngle4f worldMoveAxisAngle = new AxisAngle4f();
      worldMoveAxisAngle.set(worldMoveAxis, worldMoveAngle);

      // calculate turning axis
      mouseWorldTurn = new Matrix4f();
      mouseWorldTurn.setIdentity();
      mouseWorldTurn.set(worldMoveAxisAngle); // the new Multiplication-Matrix

      // override the initial starting values
      fixOldX = fixNewX;
      oldX = newX;
      oldY = newY;
      oldZ = newZ;
    }
Beispiel #2
0
  protected void moveOrientation() {
    if (Camera.host != owner || !PerfIO.holdMouse) return;

    AxisAngle4f tmp = new AxisAngle4f(Camera.right, -PerfIO.dy * 0.001f);

    Quat4f rot = new Quat4f();
    rot.set(tmp);
    owner.getOrientWritable().mul(rot, owner.getOrientation());
    owner.getOrientWritable().normalize();

    tmp.set(0, Camera.up.y > 0 ? 1 : -1, 0, PerfIO.dx * 0.001f);
    rot.set(tmp);
    owner.getOrientWritable().mul(rot, owner.getOrientWritable());

    owner.flushChanges();
  }
  /** Calculate transforms needed to handle VRML semantics formula: T x C x R x SR x S x -SR x -C */
  private void updateTransform() {

    // System.out.println(this);
    tempVec.x = -vfCenter[0];
    tempVec.y = -vfCenter[1];
    tempVec.z = -vfCenter[2];

    matrix.setIdentity();
    matrix.setTranslation(tempVec);

    float scaleVal = 1.0f;

    if (floatEq(vfScale[0], vfScale[1]) && floatEq(vfScale[0], vfScale[2])) {

      scaleVal = vfScale[0];
      tempMtx1.set(scaleVal);
      // System.out.println("S" + tempMtx1);

    } else {
      // non-uniform scale
      // System.out.println("Non Uniform Scale");
      tempAxis.x = vfScaleOrientation[0];
      tempAxis.y = vfScaleOrientation[1];
      tempAxis.z = vfScaleOrientation[2];
      tempAxis.angle = -vfScaleOrientation[3];

      double tempAxisNormalizer =
          1
              / Math.sqrt(
                  tempAxis.x * tempAxis.x + tempAxis.y * tempAxis.y + tempAxis.z * tempAxis.z);

      tempAxis.x *= tempAxisNormalizer;
      tempAxis.y *= tempAxisNormalizer;
      tempAxis.z *= tempAxisNormalizer;

      tempMtx1.set(tempAxis);
      tempMtx2.mul(tempMtx1, matrix);

      // Set the scale by individually setting each element
      tempMtx1.setIdentity();
      tempMtx1.m00 = vfScale[0];
      tempMtx1.m11 = vfScale[1];
      tempMtx1.m22 = vfScale[2];

      matrix.mul(tempMtx1, tempMtx2);

      tempAxis.x = vfScaleOrientation[0];
      tempAxis.y = vfScaleOrientation[1];
      tempAxis.z = vfScaleOrientation[2];
      tempAxis.angle = vfScaleOrientation[3];
      tempMtx1.set(tempAxis);
    }

    tempMtx2.mul(tempMtx1, matrix);

    // System.out.println("Sx-C" + tempMtx2);
    float magSq =
        vfRotation[0] * vfRotation[0]
            + vfRotation[1] * vfRotation[1]
            + vfRotation[2] * vfRotation[2];

    if (magSq < ZEROEPS) {
      tempAxis.x = 0;
      tempAxis.y = 0;
      tempAxis.z = 1;
      tempAxis.angle = 0;
    } else {
      if ((magSq > 1.01) || (magSq < 0.99)) {

        float mag = (float) (1 / Math.sqrt(magSq));
        tempAxis.x = vfRotation[0] * mag;
        tempAxis.y = vfRotation[1] * mag;
        tempAxis.z = vfRotation[2] * mag;
      } else {
        tempAxis.x = vfRotation[0];
        tempAxis.y = vfRotation[1];
        tempAxis.z = vfRotation[2];
      }

      tempAxis.angle = vfRotation[3];
    }

    tempMtx1.set(tempAxis);
    // System.out.println("R" + tempMtx1);

    matrix.mul(tempMtx1, tempMtx2);
    // System.out.println("RxSx-C" + matrix);

    tempVec.x = vfCenter[0];
    tempVec.y = vfCenter[1];
    tempVec.z = vfCenter[2];

    tempMtx1.setIdentity();
    tempMtx1.setTranslation(tempVec);
    // System.out.println("C" + tempMtx1);

    tempMtx2.mul(tempMtx1, matrix);
    // System.out.println("CxRxSx-C" + tempMtx2);

    tempVec.x = vfTranslation[0];
    tempVec.y = vfTranslation[1];
    tempVec.z = vfTranslation[2];

    tempMtx1.setIdentity();
    tempMtx1.setTranslation(tempVec);

    matrix.mul(tempMtx1, tempMtx2);

    transform.set(matrix);
    implTG.setTransform(transform);
  }
Beispiel #4
0
  @SuppressWarnings("unchecked")
  public static String toJSON(String infoType, Object info) {

    // Logger.debug(infoType+" -- "+info);

    StringBuilder sb = new StringBuilder();
    String sep = "";
    if (info == null) return packageJSON(infoType, (String) null);
    if (info instanceof Integer || info instanceof Float || info instanceof Double)
      return packageJSON(infoType, info.toString());
    if (info instanceof String) return packageJSON(infoType, fixString((String) info));
    if (info instanceof String[]) {
      sb.append("[");
      int imax = ((String[]) info).length;
      for (int i = 0; i < imax; i++) {
        sb.append(sep).append(fixString(((String[]) info)[i]));
        sep = ",";
      }
      sb.append("]");
      return packageJSON(infoType, sb);
    }
    if (info instanceof int[]) {
      sb.append("[");
      int imax = ((int[]) info).length;
      for (int i = 0; i < imax; i++) {
        sb.append(sep).append(((int[]) info)[i]);
        sep = ",";
      }
      sb.append("]");
      return packageJSON(infoType, sb);
    }
    if (info instanceof float[]) {
      sb.append("[");
      int imax = ((float[]) info).length;
      for (int i = 0; i < imax; i++) {
        sb.append(sep).append(((float[]) info)[i]);
        sep = ",";
      }
      sb.append("]");
      return packageJSON(infoType, sb);
    }
    if (info instanceof double[]) {
      sb.append("[");
      int imax = ((double[]) info).length;
      for (int i = 0; i < imax; i++) {
        sb.append(sep).append(((double[]) info)[i]);
        sep = ",";
      }
      sb.append("]");
      return packageJSON(infoType, sb);
    }
    if (info instanceof Point3f[]) {
      sb.append("[");
      int imax = ((Point3f[]) info).length;
      for (int i = 0; i < imax; i++) {
        sb.append(sep);
        addJsonTuple(sb, ((Point3f[]) info)[i]);
        sep = ",";
      }
      sb.append("]");
      return packageJSON(infoType, sb);
    }
    if (info instanceof String[][]) {
      sb.append("[");
      int imax = ((String[][]) info).length;
      for (int i = 0; i < imax; i++) {
        sb.append(sep).append(toJSON(null, ((String[][]) info)[i]));
        sep = ",";
      }
      sb.append("]");
      return packageJSON(infoType, sb);
    }
    if (info instanceof int[][]) {
      sb.append("[");
      int imax = ((int[][]) info).length;
      for (int i = 0; i < imax; i++) {
        sb.append(sep).append(toJSON(null, ((int[][]) info)[i]));
        sep = ",";
      }
      sb.append("]");
      return packageJSON(infoType, sb);
    }
    if (info instanceof float[][]) {
      sb.append("[");
      int imax = ((float[][]) info).length;
      for (int i = 0; i < imax; i++) {
        sb.append(sep).append(toJSON(null, ((float[][]) info)[i]));
        sep = ",";
      }
      sb.append("]");
      return packageJSON(infoType, sb);
    }
    if (info instanceof float[][][]) {
      sb.append("[");
      int imax = ((float[][][]) info).length;
      for (int i = 0; i < imax; i++) {
        sb.append(sep).append(toJSON(null, ((float[][][]) info)[i]));
        sep = ",";
      }
      sb.append("]");
      return packageJSON(infoType, sb);
    }
    if (info instanceof List) {
      sb.append("[ ");
      int imax = ((List<?>) info).size();
      for (int i = 0; i < imax; i++) {
        sb.append(sep).append(toJSON(null, ((List<?>) info).get(i)));
        sep = ",";
      }
      sb.append(" ]");
      return packageJSON(infoType, sb);
    }
    if (info instanceof Matrix4f) {
      float[] x = new float[4];
      Matrix4f m4 = (Matrix4f) info;
      sb.append('[');
      for (int i = 0; i < 4; i++) {
        if (i > 0) sb.append(',');
        m4.getRow(i, x);
        sb.append(toJSON(null, x));
      }
      sb.append(']');
      return packageJSON(infoType, sb);
    }
    if (info instanceof Matrix3f) {
      float[] x = new float[3];
      Matrix3f m3 = (Matrix3f) info;
      sb.append('[');
      for (int i = 0; i < 3; i++) {
        if (i > 0) sb.append(',');
        m3.getRow(i, x);
        sb.append(toJSON(null, x));
      }
      sb.append(']');
      return packageJSON(infoType, sb);
    }
    if (info instanceof Tuple3f) {
      addJsonTuple(sb, (Tuple3f) info);
      return packageJSON(infoType, sb);
    }
    if (info instanceof AxisAngle4f) {
      sb.append("[")
          .append(((AxisAngle4f) info).x)
          .append(",")
          .append(((AxisAngle4f) info).y)
          .append(",")
          .append(((AxisAngle4f) info).z)
          .append(",")
          .append((float) (((AxisAngle4f) info).angle * 180d / Math.PI))
          .append("]");
      return packageJSON(infoType, sb);
    }
    if (info instanceof Point4f) {
      sb.append("[")
          .append(((Point4f) info).x)
          .append(",")
          .append(((Point4f) info).y)
          .append(",")
          .append(((Point4f) info).z)
          .append(",")
          .append(((Point4f) info).w)
          .append("]");
      return packageJSON(infoType, sb);
    }
    if (info instanceof Map) {
      sb.append("{ ");
      Iterator<String> e = ((Map<String, ?>) info).keySet().iterator();
      while (e.hasNext()) {
        String key = e.next();
        sb.append(sep).append(packageJSON(key, toJSON(null, ((Map<?, ?>) info).get(key))));
        sep = ",";
      }
      sb.append(" }");
      return packageJSON(infoType, sb);
    }
    return packageJSON(infoType, fixString(info.toString()));
  }