public int copyPolygon(Polygon p) {
   int newP = getFreePolygon();
   Polygon np = polygons[newP];
   np.length = p.length;
   for (int i = 0; i < p.length; i++) {
     np.vertices[i] = p.vertices[i];
     np.setShader(p.getShader());
     np.setCenterZ(p.getCenterZ());
   }
   return newP;
 }
  /**
   * This helper method assumes that the polygon following the one at polPos is free.
   *
   * @param polPos the position of the polygon in thepolygons array
   * @param index The index to compare --- e.g. Polyggn.SX
   * @param sign +/-1 which side of the halfspace.
   * @param k the location of the halfspace --- e.g. ClippingBox.x0
   */
  private void clipToHalfspace(int polPos, Polygon polygons[], int index, int sign, double k) {

    Polygon p = polygons[polPos];
    if (p.length == 0) return;
    // int vc = vertexCount;
    int u = p.vertices[p.length - 1];
    int v = p.vertices[0];
    double tu = sign * vertexData[u + index] - k * vertexData[u + Polygon.SW];
    // sign * vertexData[u + index] / vertexData[u+Polygon.SW] -k;
    double tv = 0;
    // HERE!!!!!
    Polygon newP = polygons[polPos + 1];
    newP.length = 0;
    for (int i = 0; i < p.length; i++, u = v, tu = tv, v = p.vertices[i]) {
      tv = sign * vertexData[v + index] - k * vertexData[v + Polygon.SW];
      // sign * vertexData[v + index] / vertexData[v+Polygon.SW] - k;
      if (tu <= 0. ^ tv <= 0.) { // edge crosses plane...
        double t = tu / (tu - tv);

        for (int j = 0; j < Polygon.VERTEX_LENGTH; j++) {
          vertexData[vertexCount + j] =
              vertexData[u + j] + t * (vertexData[v + j] - vertexData[u + j]);
        }
        newP.vertices[newP.length++] = vertexCount;
        vertexCount += Polygon.VERTEX_LENGTH;
      }
      if (tv <= 0.) { // vertex v is in ...
        //				for(int j = 0;j<Polygon.VERTEX_LENGTH;j++) {
        //					vertexData[vc+j] = vertexData[v+j];
        //				}
        newP.vertices[newP.length++] = v;
        //				vc+= Polygon.VERTEX_LENGTH;
      }
    }
    // It is left to swap:
    int vp[] = p.vertices;
    p.vertices = newP.vertices;
    p.length = newP.length;
    newP.vertices = vp;
  }
  private final void compute(int verticesLength) {

    //        boolean isTransparent = shader.getVertexShader().getTransparency() != 0.;

    if (polygonCount + 1 >= this.polygons.length) increasePolygonCapacity();

    Polygon p = polygons[polygonCount++];
    p.length = verticesLength;

    // We store the current shader for later shading
    p.setShader(shader);

    for (int i = 0; i < verticesLength; i++) {
      p.vertices[i] = vertexCount;
      vertexCount += Polygon.VERTEX_LENGTH;
    }

    // sky box shaders need the matrix...
    environment.setMatrix(matrix);
    // ****
    shader.shadePolygon(p, vertexData, environment);

    int numClip = environment.getNumClippingPlanes();
    if (numClip > 0) {
      ClippingPlaneSoft[] cp = environment.getClippingPlanes();
      // Intersector.dehomogenize(p,vertexData);
      for (int k = 0; k < numClip; k++) {
        double d = VecMat.dot(cp[k].getNormal(), cp[k].getPoint());
        // System.out.println("d "+d);
        int res = Intersector.clipToHalfspace(p, cp[k].getNormal(), -1, d, this);
        if (res == -1) { // clipped out
          p.setShader(null);
          vertexCount -= p.length * Polygon.VERTEX_LENGTH;
          polygonCount--;
          return;
        }
      }
    }
    for (int i = 0; i < p.length; i++) {
      perspective.perspective(vertexData, p.vertices[i]);
    }

    boolean clippedAway = clipPolygon();

    if (!clippedAway) {
      // if (p.length == 0)
      //    System.out.println("ZEROLENGTH!");
      // TODO: check wether we sort before computing this.
      // boolean isTransparent = (shader.getVertexShader().getTransparency() !=
      // 0.)||shader.hasTexture()||shader.interpolateAlpha();
      // shader might have changed after lighting:
      boolean isTransparent = p.getShader().needsSorting();
      ////            isTransparent = false;
      if (triangulate) {
        PolygonShader ps = p.getShader();
        if (polygonCount + 1 >= this.polygons.length) increasePolygonCapacity();
        polygonCount--;
        for (int i = 0, pl = p.length - 2; i < pl; i++) {
          Polygon pi = polygons[polygonCount++];
          pi.length = 3;
          pi.vertices[0] = p.vertices[0];
          pi.vertices[1] = p.vertices[i + 1];
          pi.vertices[2] = p.vertices[i + 2];
          pi.setShader(ps);
          if (queueOpaque || isTransparent) {
            if (sortOpaque || isTransparent) pi.computeCenterZ(vertexData);
            // pi.computeMaxZ(vertexData);
            // return;
          } else {
            renderer.renderPolygon(pi, vertexData, ps.isOutline());
            pi.setShader(null);
            // TODO:check wether this is save
            // vertexCount -= p.length * Polygon.VERTEX_LENGTH;
            polygonCount--;
          }
        }
        return;
      } else {

        if (queueOpaque || isTransparent) {
          if (sortOpaque || isTransparent) p.computeCenterZ(vertexData);
          // p.computeMaxZ(vertexData);
          return;
        } else {
          renderer.renderPolygon(p, vertexData, p.getShader().isOutline());
        }
      }
    }
    // if the polygon was clipped completely or if the polygon was opaque and therefore
    // rendered already, we can free its resources:
    p.setShader(null);
    vertexCount -= p.length * Polygon.VERTEX_LENGTH;
    polygonCount--;
  }