Пример #1
0
  private void addPolyToSurf(SurfCell curSurf, String line, boolean inSmooth) {
    int curIndex = 0;
    StringTokenizer tokens;
    StringTokenizer vertTokens;
    PolyCell curPoly;
    PolyListCell curVertPoly;
    VertListCell curVert;

    tokens = new StringTokenizer(line);
    line = tokens.nextToken(); // "eat" up the f at the beginning of the line

    if (curSurf.polyHead == null) // This is the first polygon in the surface
    {
      curSurf.polyHead = new PolyCell();
      curPoly = curSurf.polyHead;
    } else // Other polygons are already in the surface
    {
      curPoly = curSurf.polyHead; // Begin at the first polygon
      while (curPoly.next != null) // Move to the next polygon as long as it exists
      curPoly = curPoly.next;

      curPoly.next = new PolyCell(); // Create a new polygon at the end of the list
      curPoly = curPoly.next;
    }
    curPoly.numVerts = 0;

    // At this point, we are dealing with an entirely new polygon!
    curPoly.parentSurf = curSurf;
    while (tokens
        .hasMoreTokens()) // Loop for constructing an entire polygon...one vertex at a time!
    {
      line = tokens.nextToken();
      vertTokens = new StringTokenizer(line, "/"); // A tokenizer separated by '/'

      curIndex = Integer.parseInt(vertTokens.nextToken()); // Grab the first number (a vertex index)
      if (curPoly.vert == null) // This is our first vertex in the polygon
      {
        curPoly.vert = new VertListCell();
        curVert = curPoly.vert;
      } else // Other vertices have already been added
      {
        curVert = curPoly.vert;
        while (curVert.next != null) curVert = curVert.next;

        curVert.next = new VertListCell();
        curVert = curVert.next;
      }
      // NOTE: copying of vertices should not be necessary since each surface is drawn by itself and
      // * each surface will either be drawn smoothed or flat and either way the correct normal will
      // be
      // * set and used for each polygon - if a surface is flat no vertex normals will be calculated
      // - could
      // * still be a problem if indeed vertices are reused in smoothed surfaces but that is
      // apparently not true??
      //////// All wrong - vertex normals are not recalculated at draw time so any vertex can NOT be
      // in
      ////////// multiple surfaces

      ////// CHECK for vertex copy here
      // if inSmooth then go ahead and setup the vertex references without regard to
      //   what is in vertexUsedArray
      // if !inSmooth then this is a one poly surface and if the vertex has already been
      //    used (as indicated in vertexUsedArray then a new copy of the vertex must be made

      if (!inSmooth) { // we are in an unsmoothed surface [often a one poly surface] -check to see
        // if vertex(curIndex-1) has
        // already been used in another surface
        if (curIndex <= vertUsedArray.size() && vertUsedArray.get(curIndex - 1) != null) {
          // make a copy of the vertex that curIndex-1 indicates
          vertArray.add(numVerts, new VertCell(vertArray.get(curIndex - 1)));
          // vertNormArray.add(numNorms++, new Double3D(vertNormArray.get(curIndex-1)));
          vertUsedArray.add(numVerts++, curSurf);
          curIndex = numVerts;
        }
      } else if (vertUsedArray.get(curIndex - 1) != null
          && vertUsedArray.get(curIndex - 1) != curSurf) {
        // we are in a smoothing group
        // but this vertex has already been used in another surface
        // so find out if there is already a copy in this surface
        int copyIndex = findCopyInSurf(curSurf, vertArray.get(curIndex - 1).worldPos);
        if (copyIndex == -1) {
          // make a copy of the vertex that curIndex-1 indicates
          vertArray.add(numVerts, new VertCell(vertArray.get(curIndex - 1)));
          // vertNormArray.add(numNorms++, new Double3D(vertNormArray.get(curIndex-1)));
          vertUsedArray.add(numVerts++, curSurf);
          curIndex = numVerts;
        } else {
          curIndex = copyIndex + 1;
        }
      }

      curVert.vert = curIndex - 1; // Assign the vertex index
      // curPoly.numVerts++;
      vertUsedArray.set(curIndex - 1, curSurf);

      /*
       * If we are not in a smoothing group at the moment do not build the
       * linked list specifying polygon membership for the vertex - when normals
       * are calculated any vertex with no list will be assumed to be part of  a
       * polygon which is to be flat shaded and the polygon normal will be used
       * for the vertex normals of each vertex.
       */
      if (inSmooth) {
        if (vertArray.get(curIndex - 1).polys == null) {
          vertArray.get(curIndex - 1).polys = new PolyListCell();
          curVertPoly = vertArray.get(curIndex - 1).polys;
        } else {
          curVertPoly = vertArray.get(curIndex - 1).polys;
          while (curVertPoly.next != null) curVertPoly = curVertPoly.next;
          curVertPoly.next = new PolyListCell();
          curVertPoly = curVertPoly.next;
        }
        curVertPoly.poly = curPoly;
      } else vertArray.get(curIndex - 1).polys = null;

      curIndex = line.indexOf('/'); // Find the first '/' if it exists
      if (curIndex > -1) {
        if (line.charAt(curIndex + 1) != '/') {
          curIndex = Integer.parseInt(vertTokens.nextToken()); // Grab the texture vertex index
          curVert.tex = curIndex - 1;
        } // end check for second /
        //////////////////////////////////////////////////////////////////////////////////////////////

      } // end check for ANY /'s
    } // end while
  } // end method addPolyToSurf