Beispiel #1
0
 public function intersect($ray)
 {
     $info = new RayTracer_IntersectionInfo();
     $Vd = $this->position->dot($ray->direction);
     if ($Vd == 0) {
         return $info;
     }
     // no intersection
     $t = -($this->position->dot($ray->position) + $this->d) / $Vd;
     if ($t <= 0) {
         return $info;
     }
     $info->shape = $this;
     $info->isHit = true;
     $info->position = RayTracer_Vector::add($ray->position, RayTracer_Vector::multiplyScalar($ray->direction, $t));
     $info->normal = $this->position;
     $info->distance = $t;
     if ($this->material->hasTexture) {
         $vU = new RayTracer_Vector($this->position->y, $this->position->z, -$this->position->x);
         $vV = $vU->cross($this->position);
         $u = $info->position->dot($vU);
         $v = $info->position->dot($vV);
         $info->color = $this->material->getColor($u, $v);
     } else {
         $info->color = $this->material->getColor(0, 0);
     }
     return $info;
 }
Beispiel #2
0
 public function getRay($vx, $vy)
 {
     $pos = RayTracer_Vector::subtract($this->screen, RayTracer_Vector::subtract(RayTracer_Vector::multiplyScalar($this->equator, $vx), RayTracer_Vector::multiplyScalar($this->up, $vy)));
     $pos->y = $pos->y * -1;
     $dir = RayTracer_Vector::subtract($pos, $this->position);
     $ray = new RayTracer_Ray($pos, $dir->normalize());
     return $ray;
 }
Beispiel #3
0
function renderScene()
{
    $scene = new RayTracer_Scene();
    $scene->camera = new RayTracer_Camera(new RayTracer_Vector(0, 0, -15), new RayTracer_Vector(-0.2, 0, 5), new RayTracer_Vector(0, 1, 0));
    $scene->background = new RayTracer_Background(new RayTracer_Color(0.5, 0.5, 0.5), 0.4);
    $sphere = new RayTracer_Shape_Sphere(new RayTracer_Vector(-1.5, 1.5, 2), 1.5, new RayTracer_Material_Solid(new RayTracer_Color(0, 0.5, 0.5), 0.3, 0.0, 0.0, 2.0));
    $sphere1 = new RayTracer_Shape_Sphere(new RayTracer_Vector(1, 0.25, 1), 0.5, new RayTracer_Material_Solid(new RayTracer_Color(0.9, 0.9, 0.9), 0.1, 0.0, 0.0, 1.5));
    $v = new RayTracer_Vector(0.1, 0.9, -0.5);
    $plane = new RayTracer_Shape_Plane($v->normalize(), 1.2, new RayTracer_Material_Chessboard(new RayTracer_Color(1.0, 1.0, 1.0), new RayTracer_Color(0.0, 0.0, 0.0), 0.2, 0.0, 1.0, 0.7));
    $scene->shapes[] = $plane;
    $scene->shapes[] = $sphere;
    $scene->shapes[] = $sphere1;
    $light = new RayTracer_Light(new RayTracer_Vector(5, 10, -1), new RayTracer_Color(0.8, 0.8, 0.8));
    $light1 = new RayTracer_Light(new RayTracer_Vector(-3, 5, -15), new RayTracer_Color(0.8, 0.8, 0.8), 100);
    $scene->lights[] = $light;
    $scene->lights[] = $light1;
    $imageWidth = 100;
    // $F('imageWidth');
    $imageHeight = 100;
    // $F('imageHeight');
    $pixelSize = split(',', "5,5");
    //  $F('pixelSize').split(',');
    $renderDiffuse = true;
    // $F('renderDiffuse');
    $renderShadows = true;
    // $F('renderShadows');
    $renderHighlights = true;
    // $F('renderHighlights');
    $renderReflections = true;
    // $F('renderReflections');
    $rayDepth = 2;
    //$F('rayDepth');
    $raytracer = new RayTracer_Engine(array("canvasWidth" => $imageWidth, "canvasHeight" => $imageHeight, "pixelWidth" => $pixelSize[0], "pixelHeight" => $pixelSize[1], "renderDiffuse" => $renderDiffuse, "renderHighlights" => $renderHighlights, "renderShadows" => $renderShadows, "renderReflections" => $renderReflections, "rayDepth" => $rayDepth));
    // Create a 55x30 image
    //$im = imagecreatetruecolor(500, 500);
    $im = null;
    $raytracer->renderScene($scene, $im, 0);
    /*
        // Save the image
    	imagepng($im, './imagefilledrectangle.png');
    	imagedestroy($im);
    */
}
Beispiel #4
0
 public function intersect($ray)
 {
     $info = new RayTracer_IntersectionInfo();
     $info->shape = $this;
     $dst = RayTracer_Vector::subtract($ray->position, $this->position);
     $B = $dst->dot($ray->direction);
     $C = $dst->dot($dst) - $this->radius * $this->radius;
     $D = $B * $B - $C;
     if ($D > 0) {
         // intersection!
         $info->isHit = true;
         $info->distance = -$B - sqrt($D);
         $info->position = RayTracer_Vector::add($ray->position, RayTracer_Vector::multiplyScalar($ray->direction, $info->distance));
         $info->normal = RayTracer_Vector::subtract($info->position, $this->position)->normalize();
         $info->color = $this->material->getColor(0, 0);
     } else {
         $info->isHit = false;
     }
     return $info;
 }
Beispiel #5
0
 public function rayTrace(RayTracer_IntersectionInfo $info, RayTracer_Ray $ray, RayTracer_Scene $scene, $depth)
 {
     // Calc ambient
     $color = RayTracer_Color::multiplyScalar($info->color, $scene->background->ambience);
     $shininess = pow(10, $info->shape->material->gloss + 1);
     foreach ($scene->lights as $light) {
         // Calc diffuse lighting
         $v = RayTracer_Vector::subtract($light->position, $info->position)->normalize();
         if ($this->options['renderDiffuse']) {
             $L = $v->dot($info->normal);
             if ($L > 0.0) {
                 $color = RayTracer_Color::add($color, RayTracer_Color::multiply($info->color, RayTracer_Color::multiplyScalar($light->color, $L)));
             }
         }
         // The greater the depth the more accurate the colours, but
         // this is exponentially (!) expensive
         if ($depth <= $this->options['rayDepth']) {
             // calculate reflection ray
             if ($this->options['renderReflections'] && $info->shape->material->reflection > 0) {
                 $reflectionRay = $this->getReflectionRay($info->position, $info->normal, $ray->direction);
                 $refl = $this->testIntersection($reflectionRay, $scene, $info->shape);
                 if ($refl->isHit && $refl->distance > 0) {
                     $refl->color = $this->rayTrace($refl, $reflectionRay, $scene, $depth + 1);
                 } else {
                     $refl->color = $scene->background->color;
                 }
                 $color = RayTracer_Color::blend($color, $refl->color, $info->shape->material->reflection);
             }
             // Refraction
             /* TODO */
         }
         /* Render shadows and highlights */
         $shadowInfo = new RayTracer_IntersectionInfo();
         if ($this->options['renderShadows']) {
             $shadowRay = new RayTracer_Ray($info->position, $v);
             $shadowInfo = $this->testIntersection($shadowRay, $scene, $info->shape);
             if ($shadowInfo->isHit && $shadowInfo->shape != $info->shape) {
                 $vA = RayTracer_Color::multiplyScalar($color, 0.5);
                 $dB = 0.5 * pow($shadowInfo->shape->material->transparency, 0.5);
                 $color = RayTracer_Color::addScalar($vA, $dB);
             }
         }
         // Phong specular highlights
         if ($this->options['renderHighlights'] && !$shadowInfo->isHit && $info->shape->material->gloss > 0) {
             $Lv = RayTracer_Vector::subtract($info->shape->position, $light->position)->normalize();
             $E = RayTracer_Vector::subtract($scene->camera->position, $info->shape->position)->normalize();
             $H = RayTracer_Vector::subtract($E, $Lv)->normalize();
             $glossWeight = pow(max($info->normal->dot($H), 0), $shininess);
             $color = RayTracer_Color::add(RayTracer_Color::multiplyScalar($light->color, $glossWeight), $color);
         }
     }
     $color->limit();
     return $color;
 }