Beispiel #1
0
 /**
  * Prepare the light for rendering.
  *
  * @param width the output image width
  * @param height the output image height
  */
 public void prepare(int width, int height) {
   float lx = (float) (Math.cos(azimuth) * Math.cos(elevation));
   float ly = (float) (Math.sin(azimuth) * Math.cos(elevation));
   float lz = (float) Math.sin(elevation);
   direction = new Vector3f(lx, ly, lz);
   direction.normalize();
   if (type != DISTANT) {
     lx *= distance;
     ly *= distance;
     lz *= distance;
     lx += width * centreX;
     ly += height * centreY;
   }
   position = new Vector3f(lx, ly, lz);
   realColor.set(new Color(color));
   realColor.scale(intensity);
   cosConeAngle = (float) Math.cos(coneAngle);
 }
Beispiel #2
0
  protected Color4f phongShade(
      Vector3f position,
      Vector3f viewpoint,
      Vector3f normal,
      Color4f diffuseColor,
      Color4f specularColor,
      Material material,
      Light[] lightsArray) {
    shadedColor.set(diffuseColor);
    shadedColor.scale(material.ambientIntensity);

    for (int i = 0; i < lightsArray.length; i++) {
      Light light = lightsArray[i];
      n.set(normal);
      l.set(light.position);
      if (light.type != DISTANT) l.sub(position);
      l.normalize();
      float nDotL = n.dot(l);
      if (nDotL >= 0.0) {
        float dDotL = 0;

        v.set(viewpoint);
        v.sub(position);
        v.normalize();

        // Spotlight
        if (light.type == SPOT) {
          dDotL = light.direction.dot(l);
          if (dDotL < light.cosConeAngle) continue;
        }

        n.scale(2.0f * nDotL);
        n.sub(l);
        float rDotV = n.dot(v);

        float rv;
        if (rDotV < 0.0) rv = 0.0f;
        else
          //					rv = (float)Math.pow(rDotV, material.highlight);
          rv =
              rDotV
                  / (material.highlight
                      - material.highlight * rDotV
                      + rDotV); // Fast approximation to pow

        // Spotlight
        if (light.type == SPOT) {
          dDotL = light.cosConeAngle / dDotL;
          float e = dDotL;
          e *= e;
          e *= e;
          e *= e;
          e = (float) Math.pow(dDotL, light.focus * 10) * (1 - e);
          rv *= e;
          nDotL *= e;
        }

        diffuse_color.set(diffuseColor);
        diffuse_color.scale(material.diffuseReflectivity);
        diffuse_color.x *= light.realColor.x * nDotL;
        diffuse_color.y *= light.realColor.y * nDotL;
        diffuse_color.z *= light.realColor.z * nDotL;
        specular_color.set(specularColor);
        specular_color.scale(material.specularReflectivity);
        specular_color.x *= light.realColor.x * rv;
        specular_color.y *= light.realColor.y * rv;
        specular_color.z *= light.realColor.z * rv;
        diffuse_color.add(specular_color);
        diffuse_color.clamp(0, 1);
        shadedColor.add(diffuse_color);
      }
    }
    shadedColor.clamp(0, 1);
    return shadedColor;
  }