// raytracying MEL // Atsushi Morimoto(for Mayonnaize Production) // $Id: ray.mel,v 0.1 2001/10/30 01:35:35 jagajin Exp jagajin $ global proc vector make_eye(int $x, int $y) { vector $v; $v = << -2.0 + $x/50.0, 2.0 - $y/50.0, 3.0>>; return $v; } global proc shading(int $fp, float $t, vector $from, vector $to, vector $point) { vector $l, $v; float $d; int $col; $l = <<0.5773, 0.5773, -0.5773>>; $v = $from + $to * $t -$point; // normalize $d = sqrt($v.x * $v.x + $v.y * $v.y + $v.z * $v.z); $v = << $v.x / $d, $v.y / $d, $v.z / $d >>; $d = $l * $v; $col = (int)(255.0 * $d) + 50; if ( $col > 255 ) $col = 255; if ( $col < 0) $col = 0; fprint $fp (""+$col+" 0 0 "); } global proc float intersect(vector $from, vector $to, vector $point, float $rad) { vector $v; float $b, $c, $d, $det, $t; $v = $from - $point; $b = $to * $v; $c = ($v * $v) - ($rad * $rad); $d = $b * $b -$c; if ( $d < 0 ) return -1.0; $det = sqrt($d); $t = -$b + $det; return $t; } global proc ray() { int $fp; int $x, $y; vector $from, $to, $point; float $t, $rad; $fp = `fopen "ray.ppm" "w"`; fprint $fp "P3\r\n200 200\r\n255\r\n"; $rad = 1.0; $to = << 0, 0, -1>>; $point = << 0, 0, 0 >>; for ( $y = 0; $y < 200; $y++) { for ( $x = 0; $x < 200; $x++) { $from = make_eye($x, $y); $t = intersect($from, $to, $point, $rad); if ( $t >= 0 ) shading($fp, $t, $from, $to, $point); else fprint $fp "100 100 100 "; } fprint $fp "\r\n"; } fclose $fp; }