Пример #1
0
  /**
   * Create geometry for vertices.
   *
   * @param offset Should we offset vertically
   * @param offsetField A full SFVec3f that we only use the height off for offset
   */
  private X3DNode[] createVertexGeom(
      X3DExecutionContext mainScene,
      X3DNode appearance,
      MFInt32 geomIndicesField,
      MFNode geometryField,
      MFVec3f sizeField,
      MFVec3f startField,
      MFVec3f endField,
      boolean offset,
      MFVec3f offsetField,
      int shapesToCreate,
      int verticesPerShape) {

    // System.out.println("**** Creating vertex geom: " + geomIndicesField.getSize() + "
    // shapesToCreate: " + shapesToCreate + " verticesPerShape: " + verticesPerShape);
    int coord_size = 0;
    int index_size = 0;
    int texCoord_size = 0;
    int normal_size = 0;
    int num = 0;
    X3DNode[] ret_val = new X3DNode[shapesToCreate];
    int shape_idx = 0;

    // System.out.println("Vertex Geoms to add: " + shapesToCreate);
    for (int n = 0; n < shapesToCreate; n++) {
      coord_size = 0;
      index_size = 0;
      texCoord_size = 0;
      normal_size = 0;

      int numVerticesInShape = verticesPerShape;

      if (n == shapesToCreate - 1) {
        numVerticesInShape++;
      }

      // System.out.println("NumVertices in Shape: " + numVerticesInShape);
      for (int i = 0; i < numVerticesInShape; i++) {
        int idx = geomIndicesField.get1Value(i);

        if (idx > geometryField.getSize() - 1) {
          System.out.println("Invalid geometry index for vertex");
          continue;
        }

        X3DNode geom_node = geometryField.get1Value(idx);

        MField coord_field = (MField) geom_node.getField("coord");
        coord_size += coord_field.getSize() * 3;

        MField index_field = (MField) geom_node.getField("index");
        index_size += index_field.getSize();

        MField texCoord_field = (MField) geom_node.getField("texCoord");
        texCoord_size += texCoord_field.getSize() * 2;

        MField normal_field = (MField) geom_node.getField("normal");
        normal_size += texCoord_field.getSize() * 3;
      }
      // System.out.println("Coord size: " + coord_size);

      // System.out.println("Handling vertex shape: " + n);
      //            int[] index = new int[index_size / shapesToCreate];
      //            float[] point = new float[coord_size / shapesToCreate];
      //            float[] tc = new float[texCoord_size / shapesToCreate];

      int[] index = new int[index_size];
      float[] point = new float[coord_size];
      float[] tc = new float[texCoord_size];
      int iidx = 0;
      int pidx = 0;
      int tidx = 0;

      iidx = 0;
      pidx = 0;
      tidx = 0;

      // Post Shape
      X3DNode shape = mainScene.createNode("Shape");
      SFNode shape_appearance = (SFNode) (shape.getField("appearance"));
      shape_appearance.setValue(appearance);

      SFNode shape_geometry = (SFNode) (shape.getField("geometry"));
      X3DNode its = mainScene.createNode("IndexedTriangleSet");
      // SFBool its_solid = (SFBool) its.getField("solid");
      // its_solid.setValue(false);

      X3DNode coord = mainScene.createNode("Coordinate");
      SFNode its_coord = (SFNode) (its.getField("coord"));
      MFVec3f point_field = (MFVec3f) coord.getField("point");
      X3DNode normal = mainScene.createNode("Normal");
      SFNode its_normal = (SFNode) (its.getField("normal"));
      MFVec3f vector_field = (MFVec3f) normal.getField("vector");
      X3DNode texcoord = mainScene.createNode("TextureCoordinate");
      SFNode its_texcoord = (SFNode) (its.getField("texCoord"));
      MFVec2f tcpoint_field = (MFVec2f) texcoord.getField("point");
      MFInt32 its_index = (MFInt32) (its.getField("index"));

      float[] size = new float[3];
      int[] indexes = null;
      float[] coords = null;
      float[] texCoords = null;
      float[] normals = null;
      int currPost = -1;
      int post;
      int lastIdx = 0;
      float[] start = new float[3];
      float[] end = new float[3];
      float[] tmp = new float[3];

      // System.out.println("Vertices to add: " + verticesPerShape);
      // Handle all but the end vertices
      for (int i = 0; i < verticesPerShape; i++) {
        post = geomIndicesField.get1Value(num);

        // System.out.println("Getting start: " + num);
        startField.get1Value(num, start);
        // System.out.println("translate post: " + java.util.Arrays.toString(start));
        if (post != currPost) {
          X3DNode geom_node = geometryField.get1Value(post);

          MFVec3f coord_field = (MFVec3f) geom_node.getField("coord");
          MFInt32 index_field = (MFInt32) geom_node.getField("index");
          MFVec2f texCoord_field = (MFVec2f) geom_node.getField("texCoord");
          MFVec3f normal_field = (MFVec3f) geom_node.getField("normal");

          indexes = new int[index_field.getSize()];
          index_field.getValue(indexes);

          coords = new float[coord_field.getSize() * 3];
          coord_field.getValue(coords);
          // System.out.println("coords: " + coords.length);
          texCoords = new float[texCoord_field.getSize() * 2];
          texCoord_field.getValue(texCoords);

          normals = new float[normal_field.getSize() * 3];
          normal_field.getValue(normals);

          currPost = post;
        }

        sizeField.get1Value(num, size);

        int highest_idx = -1;
        int val;

        for (int j = 0; j < indexes.length; j++) {
          val = indexes[j] + lastIdx;
          index[iidx++] = val;

          if (val > highest_idx) highest_idx = val;
        }

        lastIdx = highest_idx + 1;
        float offsetVal = 0;

        for (int j = 0; j < coords.length / 3; j++) {
          point[pidx++] = (coords[j * 3] * size[0]) + start[0];
          if (offset) {
            offsetField.get1Value(num, tmp);
            offsetVal = tmp[1];
          }
          point[pidx++] = ((coords[j * 3 + 1] + 0.5f) * size[1]) + start[1] + offsetVal;

          point[pidx++] = (coords[j * 3 + 2] * size[2]) + start[2];
        }
        num++;
      }

      // System.out.println("pidx: " + pidx);
      // System.out.println("indexes: " + java.util.Arrays.toString(index));
      // System.out.println("Points1: " + java.util.Arrays.toString(point));

      if (n == shapesToCreate - 1) {
        // System.out.println("Handling end vertices");
        // Handle the end post

        if (num > geomIndicesField.getSize() - 1) {
          System.out.println("Error indexing geometry.  Num: " + num);
        }

        post = geomIndicesField.get1Value(num);

        endField.get1Value(endField.getSize() - 1, end);

        if (post != currPost) {
          X3DNode geom_node = geometryField.get1Value(post);

          MFVec3f coord_field = (MFVec3f) geom_node.getField("coord");
          MFInt32 index_field = (MFInt32) geom_node.getField("index");
          MFVec2f texCoord_field = (MFVec2f) geom_node.getField("texCoord");
          MFVec3f normal_field = (MFVec3f) geom_node.getField("normal");

          indexes = new int[index_field.getSize()];
          index_field.getValue(indexes);

          coords = new float[coord_field.getSize() * 3];
          coord_field.getValue(coords);

          texCoords = new float[texCoord_field.getSize() * 2];
          texCoord_field.getValue(texCoords);

          normals = new float[normal_field.getSize() * 3];
          normal_field.getValue(normals);

          currPost = post;
        }

        sizeField.get1Value(num, size);

        int highest_idx = -1;
        int val;
        float offsetVal = 0;

        for (int j = 0; j < indexes.length; j++) {
          val = indexes[j] + lastIdx;
          index[iidx++] = val;

          if (val > highest_idx) highest_idx = val;
        }

        lastIdx = highest_idx + 1;

        for (int j = 0; j < coords.length / 3; j++) {
          point[pidx++] = (coords[j * 3] * size[0]) + end[0];

          if (offset) {
            offsetField.get1Value(num, tmp);
            offsetVal = tmp[1];
          }
          point[pidx++] = ((coords[j * 3 + 1] + 0.5f) * size[1]) + end[1] + offsetVal;
          point[pidx++] = (coords[j * 3 + 2] * size[2]) + end[2];
        }

        // System.out.println("pidx: " + pidx);
        //    System.out.println("Points2: " + java.util.Arrays.toString(point));
        //    System.out.println("Index: " + java.util.Arrays.toString(index));

      }

      // System.out.println("TexCoord: " + java.util.Arrays.toString(tc));

      if (CREATE_POSTS) {
        point_field.setValue(point.length / 3, point);
        tcpoint_field.setValue(texCoords.length / 2, texCoords);
        vector_field.setValue(normals.length / 3, normals);
        its_coord.setValue(coord);
        its_texcoord.setValue(texcoord);
        its_index.setValue(index.length, index);

        shape_geometry.setValue(its);
      }

      // System.out.println("Add vertex shape to: " + (shape_idx) + " size: " + ret_val.length);
      ret_val[shape_idx++] = shape;
    }

    return ret_val;
  }
Пример #2
0
  /**
   * Output a specific entity to the specified stream.
   *
   * @param model The world model to export
   * @param entityID The entity to export
   * @param mainScene The X3D scene to write to
   */
  public X3DNode export(WorldModel model, int entityID, X3DScene mainScene, String worldURL) {

    // get the entity
    Entity entity = model.getEntity(entityID);

    if (entity == null) {
      errorReporter.messageReport("Cannot find model to export: " + entityID);
      return null;
    }

    if (entity.isController()) {
      return null;
    }

    try {

      X3DNode group;

      // if the entity has a position then place it
      if (entity instanceof PositionableEntity) {

        double[] position = new double[3];
        float[] rotation = new float[4];
        float[] scale = new float[3];

        ((PositionableEntity) entity).getPosition(position);
        ((PositionableEntity) entity).getRotation(rotation);
        ((PositionableEntity) entity).getScale(scale);

        float[] pos = new float[] {(float) position[0], (float) position[1], (float) position[2]};

        // create the transform group node
        group = mainScene.createNode("Transform");
        SFVec3f translationField = (SFVec3f) (group.getField("translation"));
        translationField.setValue(pos);
        SFRotation rotationField = (SFRotation) (group.getField("rotation"));
        rotationField.setValue(rotation);
        SFVec3f scaleField = (SFVec3f) (group.getField("scale"));
        scaleField.setValue(scale);

      } else {

        // create the transform group node
        group = mainScene.createNode("Group");
      }

      // create the inline node
      String url = worldURL + entity.getModelURL();

      X3DNode inline = mainScene.createNode("Inline");
      MFString urlField = (MFString) (inline.getField("url"));
      urlField.setValue(1, new String[] {url});

      // grab the child node to append to
      MFNode childrenField = (MFNode) (group.getField("children"));

      // add inline to the group/transform
      childrenField.append(inline);

      return group;

    } catch (Exception ex) {
      errorReporter.errorReport("Error.", ex);
    }

    return null;
  }
Пример #3
0
  /**
   * Create geometry for horizontal segments
   *
   * @param offset Should we offset vertically
   * @param offsetField A full SFVec3f that we only use the height off for offset
   */
  private X3DNode[] createSegmentGeom(
      X3DExecutionContext mainScene,
      X3DNode appearance,
      MFInt32 geomIndicesField,
      MFNode geometryField,
      MFFloat heightField,
      MFVec3f startField,
      MFVec3f endField,
      boolean offset,
      MFFloat offsetField,
      int vertices,
      int shapesToCreate,
      int panelsPerShape,
      boolean sRepeatPerMeter,
      boolean tRepeatPerMeter,
      float sMeterMult,
      float tMeterMult) {

    float[] post_coord;
    float[] post_normal;
    float[] post_texCoord;
    int[] post_index;
    X3DNode[] ret_val = new X3DNode[shapesToCreate];
    int shape_idx = 0;
    float[] start = new float[3];
    float[] end = new float[3];
    int num = 0;

    float sRepeat;
    float tRepeat;

    for (int n = 0; n < shapesToCreate; n++) {

      int[] index = new int[2 * 3 * vertices];
      float[] point = new float[4 * 3 * vertices];
      float[] tc = new float[4 * 2 * vertices];
      int iidx = 0;
      int pidx = 0;
      int tidx = 0;
      float height;
      float offsetVal = 0;

      // Panel Shape
      X3DNode panel_shape = mainScene.createNode("Shape");
      SFNode shape_appearance = (SFNode) (panel_shape.getField("appearance"));
      shape_appearance.setValue(appearance);

      SFNode shape_geometry = (SFNode) (panel_shape.getField("geometry"));
      X3DNode its = mainScene.createNode("IndexedTriangleSet");
      SFBool its_solid = (SFBool) its.getField("solid");
      its_solid.setValue(false);

      X3DNode coord = mainScene.createNode("Coordinate");
      SFNode its_coord = (SFNode) (its.getField("coord"));
      MFVec3f point_field = (MFVec3f) coord.getField("point");
      X3DNode normal = mainScene.createNode("Normal");
      SFNode its_normal = (SFNode) (its.getField("normal"));
      X3DNode texcoord = mainScene.createNode("TextureCoordinate");
      SFNode its_texcoord = (SFNode) (its.getField("texCoord"));
      MFVec2f tcpoint_field = (MFVec2f) texcoord.getField("point");
      MFInt32 its_index = (MFInt32) (its.getField("index"));

      for (int i = 0; i < panelsPerShape; i++) {

        startField.get1Value(num, start);
        endField.get1Value(num, end);
        height = heightField.get1Value(num);

        if (sRepeatPerMeter) {
          // calculate the length of the panel from start to end
          float x_delta = end[0] - start[0];
          float y_delta = end[1] - start[1];
          float z_delta = end[2] - start[2];
          float length =
              (float) Math.sqrt(x_delta * x_delta + y_delta * y_delta + z_delta * z_delta);
          // the s repeat factor is the length, in meters
          sRepeat = length * sMeterMult;
        } else {
          // the s repeat factor is 1 per panel
          sRepeat = 1.0f;
        }

        if (tRepeatPerMeter) {
          // the t repeat factor is the height, in meters
          tRepeat = height * tMeterMult;
        } else {
          // the t repeat factor is 1 per panel
          tRepeat = 1.0f;
        }
        // System.out.println( "sRepeat = "+ sRepeat +", tRepeat = "+ tRepeat );
        if (offset) {
          offsetVal = offsetField.get1Value(num);
        }

        num++;

        index[iidx++] = i * 4;
        index[iidx++] = i * 4 + 1;
        index[iidx++] = i * 4 + 2;
        index[iidx++] = i * 4 + 1;
        index[iidx++] = i * 4 + 3;
        index[iidx++] = i * 4 + 2;

        point[pidx++] = start[0];
        point[pidx++] = offsetVal + height + start[1];
        point[pidx++] = start[2];

        point[pidx++] = start[0];
        point[pidx++] = offsetVal + 0 + start[1];
        point[pidx++] = start[2];

        point[pidx++] = end[0];
        point[pidx++] = offsetVal + height + end[1];
        point[pidx++] = end[2];

        point[pidx++] = end[0];
        point[pidx++] = offsetVal + 0 + end[1];
        point[pidx++] = end[2];

        tc[tidx++] = 0;
        tc[tidx++] = 0;
        tc[tidx++] = 0;
        tc[tidx++] = tRepeat;
        tc[tidx++] = sRepeat;
        tc[tidx++] = 0;
        tc[tidx++] = sRepeat;
        tc[tidx++] = tRepeat;
      }

      // System.out.println("Points: " + java.util.Arrays.toString(point));
      // System.out.println("Index: " + java.util.Arrays.toString(index));
      // System.out.println("TexCoord: " + java.util.Arrays.toString(tc));

      if (CREATE_PANELS) {
        point_field.setValue(point.length / 3, point);
        tcpoint_field.setValue(tc.length / 2, tc);
        its_coord.setValue(coord);
        its_texcoord.setValue(texcoord);
        its_index.setValue(index.length, index);

        shape_geometry.setValue(its);
      }

      // System.out.println("Placing panel at: " + shape_idx);

      ret_val[shape_idx++] = panel_shape;
    }

    return ret_val;
  }