/** @param renderer */
  public final void renderRemaining(
      final PolygonRasterizer
          renderer /*,final int xmin, final int xmax, final int ymin, final int ymax*/) {
    // renderer.setWindow(xmin,xmax,ymin,ymax);
    //		for (int i = polygonCount; i > 0;) {
    //			renderer.renderPolygon(polygons[--i],vertexData,polygons[i].getShader().isOutline());
    //			polygons[i].setShader(null);
    //		}
    //		for (int i = 0; i < transparentPolygonCount; i++) {
    //
    //	renderer.renderPolygon(transparentPolygons[i],vertexData,transparentPolygons[i].getShader().isOutline());
    //			transparentPolygons[i].setShader(null);
    //		}
    // System.out.println("polys "+ polygonCount);

    // we render the polygons front to back in order to have less to do in setPixel. transparent
    // polys are
    // sorted in reverse order and at the beginning, so we know, that they are rendered last and in
    // back to front order.
    //		for (int i = polygonCount; i > 0;) {
    //			renderer.renderPolygon(polygons[--i],vertexData,polygons[i].getShader().isOutline());
    //			polygons[i].setShader(null);
    //		}

    for (int i = 0; i < (polygonCount); i++) {
      Polygon p = polygons[i];
      renderer.renderPolygon(p, vertexData, p.getShader().isOutline());
      p.setShader(null);
      // System.out.println("centerZ "+p.getCenterZ() );
    }
  }
  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--;
  }