Package se.llbit.math

Examples of se.llbit.math.Ray$RayPool


   */
  public static final void pathTrace(Scene scene, Ray ray, WorkerState state,
      int addEmitted, boolean first) {

    Random random = state.random;
    Ray reflected = state.rayPool.get();
    Ray transmitted = state.rayPool.get();
    Ray refracted = state.rayPool.get();
    Vector3d ox = state.vectorPool.get(ray.x);
    Vector3d od = state.vectorPool.get(ray.d);
    double s = 0;

    while (true) {

      if (!RayTracer.nextIntersection(scene, ray, state)) {
        if (ray.depth == 0) {
          // direct sky hit
          scene.sky.getSkyColorInterpolated(ray, scene.waterHeight > 0);

        } else if (ray.specular) {
          // sky color
          scene.sky.getSkySpecularColor(ray, scene.waterHeight > 0);
        } else {
          scene.sky.getSkyColor(ray, scene.waterHeight > 0);
        }
        break;
      }

      double pSpecular = 0;

      Block currentBlock = ray.getCurrentBlock();
      Block prevBlock = ray.getPrevBlock();

      if (!scene.stillWater && ray.n.y != 0 &&
          ((currentBlock == Block.WATER && prevBlock == Block.AIR) ||
          (currentBlock == Block.AIR && prevBlock == Block.WATER))) {

        WaterModel.doWaterDisplacement(ray);

        if (currentBlock == Block.AIR) {
          ray.n.y = -ray.n.y;
        }
      }

      if (currentBlock.isShiny) {
        if (currentBlock == Block.WATER) {
          pSpecular = Scene.WATER_SPECULAR;
        } else {
          pSpecular = Scene.SPECULAR_COEFF;
        }
      }

      double pDiffuse = ray.color.w;

      float n1 = prevBlock.ior;
      float n2 = currentBlock.ior;

      if (pDiffuse + pSpecular < Ray.EPSILON && n1 == n2)
        continue;

      if (first) {
        s = ray.distance;
        first = false;
      }

      if (currentBlock.isShiny &&
          random.nextDouble() < pSpecular) {

        reflected.specularReflection(ray);

        if (!scene.kill(reflected, random)) {
          pathTrace(scene, reflected, state, 1, false);
          if (reflected.hit) {
            ray.color.x *= reflected.color.x;
            ray.color.y *= reflected.color.y;
            ray.color.z *= reflected.color.z;
            ray.hit = true;
          }
        }

      } else {

        if (random.nextDouble() < pDiffuse) {

          reflected.set(ray);
          if (!scene.kill(reflected, random)) {

            double emittance = 0;

            if (scene.emittersEnabled && currentBlock.isEmitter) {

              emittance = addEmitted;
              ray.emittance.x = ray.color.x * ray.color.x *
                  currentBlock.emittance * scene.emitterIntensity;
              ray.emittance.y = ray.color.y * ray.color.y *
                  currentBlock.emittance * scene.emitterIntensity;
              ray.emittance.z = ray.color.z * ray.color.z *
                  currentBlock.emittance * scene.emitterIntensity;
              ray.hit = true;
            }

            if (scene.sunEnabled) {
              scene.sun.getRandomSunDirection(reflected, random, state.vectorPool);

              double directLightR = 0;
              double directLightG = 0;
              double directLightB = 0;

              boolean frontLight = reflected.d.dot(ray.n) > 0;

              if (frontLight || (currentBlock.subSurfaceScattering &&
                  random.nextDouble() < Scene.fSubSurface)) {

                if (!frontLight) {
                  reflected.x.scaleAdd(-Ray.OFFSET, ray.n, reflected.x);
                }

                reflected.currentMaterial = ray.prevMaterial;

                getDirectLightAttenuation(scene, reflected, state);

                Vector4d attenuation = state.attenuation;
                if (attenuation.w > 0) {
                  double mult = QuickMath.abs(reflected.d.dot(ray.n));
                  directLightR = attenuation.x*attenuation.w * mult;
                  directLightG = attenuation.y*attenuation.w * mult;
                  directLightB = attenuation.z*attenuation.w * mult;
                  ray.hit = true;
                }
              }

              reflected.diffuseReflection(ray, random);
              pathTrace(scene, reflected, state, 0, false);
              ray.hit = ray.hit || reflected.hit;
              if (ray.hit) {
                ray.color.x = ray.color.x
                  * (emittance + directLightR * scene.sun.emittance.x
                    + (reflected.color.x + reflected.emittance.x));
                ray.color.y = ray.color.y
                  * (emittance + directLightG * scene.sun.emittance.y
                    + (reflected.color.y + reflected.emittance.y));
                ray.color.z = ray.color.z
                  * (emittance + directLightB * scene.sun.emittance.z
                    + (reflected.color.z + reflected.emittance.z));
              }

            } else {
              reflected.diffuseReflection(ray, random);

              pathTrace(scene, reflected, state, 0, false);
              ray.hit = ray.hit || reflected.hit;
              if (ray.hit) {
                ray.color.x = ray.color.x
                  * (emittance + (reflected.color.x + reflected.emittance.x));
                ray.color.y = ray.color.y
                  * (emittance + (reflected.color.y + reflected.emittance.y));
                ray.color.z = ray.color.z
                  * (emittance + (reflected.color.z + reflected.emittance.z));
              }
            }
          }
        } else if (n1 != n2) {

          boolean doRefraction =
              currentBlock == Block.WATER ||
              prevBlock == Block.WATER ||
              currentBlock == Block.ICE ||
              prevBlock == Block.ICE;

          // refraction
          float n1n2 = n1 / n2;
          double cosTheta = - ray.n.dot(ray.d);
          double radicand = 1 - n1n2*n1n2 * (1 - cosTheta*cosTheta);
          if (doRefraction && radicand < Ray.EPSILON) {
            // total internal reflection
            reflected.specularReflection(ray);
            if (!scene.kill(reflected, random)) {
              pathTrace(scene, reflected, state, 1, false);
              if (reflected.hit) {

                ray.color.x = reflected.color.x;
                ray.color.y = reflected.color.y;
                ray.color.z = reflected.color.z;
                ray.hit = true;
              }
            }
          } else {
            refracted.set(ray);
            if (!scene.kill(refracted, random)) {

              // Calculate angle-dependent reflectance using
              // Fresnel equation approximation
              // R(theta) = R0 + (1 - R0) * (1 - cos(theta))^5
View Full Code Here


  /**
   * Perform auto focus
   */
  public void autoFocus() {
    Ray ray = RayPool.getDefaultRayPool().get();
    trace(ray);
    if (!ray.hit) {
      camera.setDof(Double.POSITIVE_INFINITY);
    } else {
      camera.setSubjectDistance(ray.distance);
View Full Code Here

          int x0 = width/2;
          int y0 = height/2;
          g.setColor(java.awt.Color.white);
          g.drawLine(x0, y0-4, x0, y0+4);
          g.drawLine(x0-4, y0, x0+4, y0);
          Ray ray = rayPool.get();
          trace(ray);
          g.setFont(infoFont);
          if (ray.hit) {
            Block block = ray.getCurrentBlock();
            g.drawString(String.format("target: %.2f m", ray.distance), 5, height-18);
            g.drawString(String.format("[0x%08X] %s (%s)",
                ray.currentMaterial,
                block,
                block.description(ray.getBlockData())),
                5, height-5);
          }
          Vector3d pos = camera.getPosition();
          g.drawString(String.format("(%.1f, %.1f, %.1f)",
              pos.x, pos.y, pos.z), 5, 11);
View Full Code Here

  /**
   * @param scene
   * @param state
   */
  public static void quickTrace(Scene scene, WorkerState state) {
    Ray ray = state.ray;
    while (true) {
      if (!nextIntersection(scene, ray, state)) {
        if (scene.waterHeight > 0 &&
            ray.d.y < 0 && ray.x.y > scene.waterHeight-.125) {

          ray.t = (scene.waterHeight-.125-ray.x.y) / ray.d.y;
          ray.distance += ray.t;
          ray.x.scaleAdd(ray.t, ray.d, ray.x);
          ray.currentMaterial = Block.WATER.id;
          ray.prevMaterial = 0;
          WaterModel.intersect(ray);
        }
        break;
      } else if (ray.getCurrentBlock() == Block.WATER) {
        break;
      } else if (ray.currentMaterial != 0 && ray.color.w > 0) {
        break;
      } else {
        ray.x.scaleAdd(Ray.OFFSET, ray.d, ray.x);
View Full Code Here

   * @return Next intersection
   */
  public static boolean nextIntersection(Scene scene, Ray ray, WorkerState state) {

    if (scene.sky().cloudsEnabled()) {
      Ray oct = state.rayPool.get(ray);
      if  (scene.sky().cloudIntersection(scene, ray, state.random)) {
        if (nextWorldIntersection(scene, oct, state.rayPool) &&
            oct.distance <= ray.distance) {
          ray.distance = oct.distance;
          ray.d.set(oct.d);
View Full Code Here

  private static boolean nextWorldIntersection(Scene scene, Ray ray,
      RayPool rayPool) {

    boolean hit = false;
    Ray oct = rayPool.get(ray);
    if (scene.intersect(oct)) {
      ray.distance = oct.distance;
      ray.x.set(oct.x);
      ray.n.set(oct.n);
      ray.color.set(oct.color);
View Full Code Here

   */
  private void raytrace() {

    double aspect = width / (double) height;

    Ray ray = rayPool.get();

    camPos.set(0, -distance, 0);
    camera.transform(camPos);
    camPos.add(.5, .5, .5);

    for (int x = 0; x < width; ++x) {

      double fovTan = Camera.clampedFovTan(70);
      double rayx = fovTan * aspect *
          (.5 - ((double) x) / width);

      for (int y = 0; y < height; ++y) {

        ray.setDefault();
        ray.d.set(rayx, 1,
            fovTan * (.5 - ((double) y) / height));
        ray.d.normalize();
        camera.transform(ray.d);

View Full Code Here

  private final void work(int jobId) throws InterruptedException {

    Scene scene = manager.bufferedScene();
   
    Random random = state.random;
    Ray ray = state.ray;

    int width = scene.canvasWidth();
    int height = scene.canvasHeight();

    double halfWidth = width/(2.0*height);

    // calculate pixel bounds for this job
    int xjobs = (width+(manager.tileWidth-1))/manager.tileWidth;
    int x0 = manager.tileWidth * (jobId % xjobs);
    int x1 = Math.min(x0 + manager.tileWidth, width);
    int y0 = manager.tileWidth * (jobId / xjobs);
    int y1 = Math.min(y0 + manager.tileWidth, height);

    double[] samples = scene.getSampleBuffer();
    final Camera cam = scene.camera();

    long jobStart = System.nanoTime();

    if (scene.pathTrace()) {

      // this is intentionally incorrectly indented for readability
      for (int y = y0; y < y1; ++y) {
        int offset = y * width * 3 + x0 * 3;
        for (int x = x0; x < x1; ++x) {

          double sr = 0;
          double sg = 0;
          double sb = 0;

          for (int i = 0; i < RenderConstants.SPP_PASS; ++i) {
            double oy = random.nextDouble();
            double ox = random.nextDouble();

            cam.calcViewRay(ray, random, (-halfWidth + (x + ox)
                / height), (-.5 + (y + oy) / height));

            scene.pathTrace(state);

            sr += ray.color.x;
            sg += ray.color.y;
            sb += ray.color.z;
          }
          double sinv = 1.0 / (scene.spp + RenderConstants.SPP_PASS);
          samples[offset+0] = (samples[offset+0] * scene.spp + sr) * sinv;
          samples[offset+1] = (samples[offset+1] * scene.spp + sg) * sinv;
          samples[offset+2] = (samples[offset+2] * scene.spp + sb) * sinv;

          if (scene.finalizeBuffer()) {
            scene.finalizePixel(x, y);
          }

          offset += 3;
        }
      }

    } else {

      Ray target = state.rayPool.get(ray);
      scene.trace(target);
      int tx = (int) QuickMath.floor(target.x.x + target.d.x * Ray.OFFSET);
      int ty = (int) QuickMath.floor(target.x.y + target.d.y * Ray.OFFSET);
      int tz = (int) QuickMath.floor(target.x.z + target.d.z * Ray.OFFSET);

 
View Full Code Here

TOP

Related Classes of se.llbit.math.Ray$RayPool

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.