Пример #1
0
  /**
   * Shades a point with the same algorithm used by the {@link <a
   * href="http://surf.sourceforge.net">surf raytracer</a>}.
   *
   * @param hitPoint Intersection point.
   * @param v View vector (from intersection point to eye).
   * @param n Surface normal.
   * @param material Surface material.
   * @return
   */
  protected Color3f shadeWithMaterial(
      Point3d hitPoint,
      Vector3d v,
      Vector3d n,
      Color3f ambientColor,
      LightProducts[] lightProducts) {
    Vector3d l = new Vector3d();
    Vector3d h = new Vector3d();

    Color3f color = new Color3f(ambientColor);

    for (int i = 0; i < dcsd.lightSources.length; i++) {
      LightSource lightSource = dcsd.lightSources[i];

      l.sub(lightSource.getPosition(), hitPoint);
      l.normalize();

      float lambertTerm = (float) n.dot(l);
      if (lambertTerm > 0.0f) {
        // compute diffuse color component
        color.scaleAdd(lambertTerm, lightProducts[i].getDiffuseProduct(), color);

        // compute specular color component
        h.add(l, v);
        h.normalize();

        color.scaleAdd(
            (float)
                Math.pow(Math.max(0.0f, n.dot(h)), lightProducts[i].getMaterial().getShininess()),
            lightProducts[i].getSpecularProduct(),
            color);
      }
    }

    color.clampMax(1.0f);

    return color;
  }
Пример #2
0
  private Color3f antiAliasPixel(
      double ll_u,
      double ll_v,
      double u_incr,
      double v_incr,
      AntiAliasingPattern aap,
      Color3f ulColor,
      Color3f urColor,
      Color3f llColor,
      Color3f lrColor,
      HashMap<java.lang.Double, ColumnSubstitutorPair> csp_hm) {
    // first average pixel-corner colors
    Color3f finalColor;

    // adaptive supersampling
    float thresholdSqr = dcsd.antiAliasingThreshold * dcsd.antiAliasingThreshold;
    if (aap != AntiAliasingPattern.OG_2x2
        && (colorDiffSqr(ulColor, urColor) >= thresholdSqr
            || colorDiffSqr(ulColor, llColor) >= thresholdSqr
            || colorDiffSqr(ulColor, lrColor) >= thresholdSqr
            || colorDiffSqr(urColor, llColor) >= thresholdSqr
            || colorDiffSqr(urColor, lrColor) >= thresholdSqr
            || colorDiffSqr(llColor, lrColor) >= thresholdSqr)) {
      // anti-alias pixel with advanced sampling pattern
      finalColor = new Color3f();
      for (SamplingPoint sp : aap) {
        if (Thread.interrupted()) throw new RenderingInterruptedException();

        Color3f ss_color;
        if (sp.getU() == 0.0 && sp.getV() == 0.0) ss_color = llColor;
        else if (sp.getU() == 0.0 && sp.getV() == 1.0) ss_color = ulColor;
        else if (sp.getU() == 1.0 && sp.getV() == 1.0) ss_color = urColor;
        else if (sp.getU() == 1.0 && sp.getV() == 0.0) ss_color = lrColor;
        else {
          // color of this sample point is not known -> calculate
          double v = ll_v + sp.getV() * v_incr;
          double u = ll_u + sp.getU() * u_incr;
          ColumnSubstitutorPair csp = csp_hm.get(v);
          if (csp == null) {
            csp =
                new ColumnSubstitutorPair(
                    dcsd.surfaceRowSubstitutor.setV(v), dcsd.gradientRowSubstitutor.setV(v));
            csp_hm.put(v, csp);
          }
          ss_color = tracePolynomial(csp.scs, csp.gcs, u, v);
        }
        finalColor.scaleAdd(sp.getWeight(), ss_color, finalColor);

        if (false)
          return new Color3f(
              0, 0, 0); // paint pixels, that are supposed to be anti-aliased in black
      }
    } else {
      finalColor = new Color3f(ulColor);
      finalColor.add(urColor);
      finalColor.add(llColor);
      finalColor.add(lrColor);
      finalColor.scale(0.25f);
    }

    // clamp color, because floating point operations may yield values outside [0,1]
    finalColor.clamp(0f, 1f);
    return finalColor;
  }