/**
  * Set the textureTransform field.
  *
  * @param val The X3DNode[] to set.
  */
 public void setTextureTransform(X3DNode[] val) {
   if (textureTransform == null) {
     textureTransform = (MFNode) getField("textureTransform");
   }
   textureTransform.setValue(val.length, val);
 }
Ejemplo n.º 2
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;
  }
 /**
  * Set the topTexture field.
  *
  * @param val The X3DNode[] to set.
  */
 public void setTopTexture(X3DNode[] val) {
   if (topTexture == null) {
     topTexture = (MFNode) getField("topTexture");
   }
   topTexture.setValue(val.length, val);
 }
 /**
  * Return the number of MFNode items in the textureTransform field.
  *
  * @return the number of MFNode items in the textureTransform field.
  */
 public int getNumTextureTransform() {
   if (textureTransform == null) {
     textureTransform = (MFNode) getField("textureTransform");
   }
   return (textureTransform.getSize());
 }
 /**
  * Set the bottomTexture field.
  *
  * @param val The X3DNode[] to set.
  */
 public void setBottomTexture(X3DNode[] val) {
   if (bottomTexture == null) {
     bottomTexture = (MFNode) getField("bottomTexture");
   }
   bottomTexture.setValue(val.length, val);
 }
 /**
  * Return the number of MFNode items in the topTexture field.
  *
  * @return the number of MFNode items in the topTexture field.
  */
 public int getNumTopTexture() {
   if (topTexture == null) {
     topTexture = (MFNode) getField("topTexture");
   }
   return (topTexture.getSize());
 }
 /**
  * Set the rightTexture field.
  *
  * @param val The X3DNode[] to set.
  */
 public void setRightTexture(X3DNode[] val) {
   if (rightTexture == null) {
     rightTexture = (MFNode) getField("rightTexture");
   }
   rightTexture.setValue(val.length, val);
 }
 /**
  * Return the backTexture value in the argument X3DNode[]
  *
  * @param val The X3DNode[] to initialize.
  */
 public void getBackTexture(X3DNode[] val) {
   if (backTexture == null) {
     backTexture = (MFNode) getField("backTexture");
   }
   backTexture.getValue(val);
 }
 /**
  * Set the leftTexture field.
  *
  * @param val The X3DNode[] to set.
  */
 public void setLeftTexture(X3DNode[] val) {
   if (leftTexture == null) {
     leftTexture = (MFNode) getField("leftTexture");
   }
   leftTexture.setValue(val.length, val);
 }
 /**
  * Return the number of MFNode items in the rightTexture field.
  *
  * @return the number of MFNode items in the rightTexture field.
  */
 public int getNumRightTexture() {
   if (rightTexture == null) {
     rightTexture = (MFNode) getField("rightTexture");
   }
   return (rightTexture.getSize());
 }
 /**
  * Return the number of MFNode items in the leftTexture field.
  *
  * @return the number of MFNode items in the leftTexture field.
  */
 public int getNumLeftTexture() {
   if (leftTexture == null) {
     leftTexture = (MFNode) getField("leftTexture");
   }
   return (leftTexture.getSize());
 }
 /**
  * Set the frontTexture field.
  *
  * @param val The X3DNode[] to set.
  */
 public void setFrontTexture(X3DNode[] val) {
   if (frontTexture == null) {
     frontTexture = (MFNode) getField("frontTexture");
   }
   frontTexture.setValue(val.length, val);
 }
 /**
  * Return the number of MFNode items in the frontTexture field.
  *
  * @return the number of MFNode items in the frontTexture field.
  */
 public int getNumFrontTexture() {
   if (frontTexture == null) {
     frontTexture = (MFNode) getField("frontTexture");
   }
   return (frontTexture.getSize());
 }
Ejemplo n.º 14
0
  public void initialize() {
    // System.out.println("Creating Fence geometry");
    int num = 0;
    int len = startField.getSize();
    float[] start = new float[3];
    float[] end = new float[3];
    float height;

    // One shape for a panel, panelTop and post, post Top
    int shape_groups = 4;

    int num_panels = len;
    int num_panels_per_shape = fencesPerShapeField.getValue();
    int num_shapes = (num_panels / num_panels_per_shape) * shape_groups;
    int num_posts_per_shape = num_panels_per_shape;

    // System.out.println("Number of posts: " + len);
    // System.out.println("Number of panels: " + num_panels);
    // System.out.println("Number of shapes: " + num_shapes);
    // System.out.println("Number of panels per shape: " + num_panels_per_shape);
    // System.out.println("Number of posts per shape: " + num_posts_per_shape);

    X3DNode[] shapes = new X3DNode[num_shapes];

    X3DExecutionContext mainScene = browser.getExecutionContext();

    // Create a common Appearance to share

    X3DNode[] appearances = new X3DNode[appearanceField.getSize()];
    appearanceField.getValue(appearances);

    X3DNode panel_appearance = appearances[panelAppearanceField.getValue()];
    X3DNode panelTop_appearance = appearances[panelTopAppearanceField.getValue()];
    X3DNode panelRail_appearance = appearances[panelRailAppearanceField.getValue()];
    X3DNode post_appearance = appearances[postAppearanceField.getValue()];
    X3DNode postTop_appearance = appearances[postTopAppearanceField.getValue()];

    int shape_idx = 0;

    // Create Panels

    X3DNode[] new_shapes =
        createSegmentGeom(
            mainScene,
            panel_appearance,
            postField,
            postGeometryField,
            panelHeightField,
            startField,
            endField,
            false,
            null,
            len,
            (num_shapes / shape_groups),
            num_posts_per_shape,
            true,
            true,
            1,
            1);

    for (int i = 0; i < new_shapes.length; i++) {
      shapes[shape_idx++] = new_shapes[i];
    }

    // Create PanelTops
    new_shapes =
        createSegmentGeom(
            mainScene,
            panelTop_appearance,
            postField,
            postGeometryField,
            panelTopHeightField,
            startField,
            endField,
            true,
            panelHeightField,
            len,
            (num_shapes / shape_groups),
            num_posts_per_shape,
            true,
            true,
            15,
            10);

    for (int i = 0; i < new_shapes.length; i++) {
      X3DNode panel_shape = mainScene.createNode("Shape");
      shapes[shape_idx++] = new_shapes[i];
    }

    // Create Posts
    new_shapes =
        createVertexGeom(
            mainScene,
            post_appearance,
            postField,
            postGeometryField,
            postSizeField,
            startField,
            endField,
            false,
            null,
            (num_shapes / shape_groups),
            num_posts_per_shape);

    for (int i = 0; i < new_shapes.length; i++) {
      X3DNode panel_shape = mainScene.createNode("Shape");
      shapes[shape_idx++] = new_shapes[i];
    }

    // Create PostTops
    new_shapes =
        createVertexGeom(
            mainScene,
            postTop_appearance,
            postTopField,
            postTopGeometryField,
            postTopSizeField,
            startField,
            endField,
            true,
            postSizeField,
            (num_shapes / shape_groups),
            num_posts_per_shape);

    for (int i = 0; i < new_shapes.length; i++) {
      shapes[shape_idx++] = new_shapes[i];
    }

    childrenField.setValue(shapes.length, shapes);

    // System.out.println("Done creating fence geometry");
  }
 /**
  * Return the number of MFNode items in the bottomTexture field.
  *
  * @return the number of MFNode items in the bottomTexture field.
  */
 public int getNumBottomTexture() {
   if (bottomTexture == null) {
     bottomTexture = (MFNode) getField("bottomTexture");
   }
   return (bottomTexture.getSize());
 }
Ejemplo n.º 16
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;
  }
 /**
  * Return the number of MFNode items in the backTexture field.
  *
  * @return the number of MFNode items in the backTexture field.
  */
 public int getNumBackTexture() {
   if (backTexture == null) {
     backTexture = (MFNode) getField("backTexture");
   }
   return (backTexture.getSize());
 }