Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- function minAngle($line, $p, $threshold, $focus, $depth){
- //---Find the side lengths of the triangle created by the point and the line
- $b = pow($line['x'][1]-$line['x'][2], 2)+pow($line['y'][1]-$line['y'][2], 2)+pow($line['z'][1]-$line['z'][2], 2); //Difference between the two points of the line
- $c = pow($line['x'][1]-$p['x'], 2)+pow($line['y'][1]-$p['y'], 2)+pow($line['z'][1]-$p['z'], 2); //Difference between the start of the line and the point
- $a = pow($p['x']-$line['x'][2], 2)+pow($p['y']-$line['y'][2], 2)+pow($p['z']-$line['z'][2], 2); //difference between the end of the line and the point
- //---Find the angle formed between the ray and the point
- $angle = rad2deg(acos(($b+$c-$a)/(2*sqrt($b)*sqrt($c)))); //cos^(-1)((b^2 + c^2 - a^2) / 2bc), will give the angle between the point an the ray.
- //---Find out how out of focus the point is
- $dif = intval(abs($p['y']-$focus)/$depth);
- if($dif == 0){
- $dif = 1;
- }
- if($angle < $threshold*$dif){
- //---Depending on the angle formed and the level of focus, return the brightness of the ray/pixel.
- $return = str_pad(intval((($threshold*$dif-$angle)/($threshold*$dif))*255), 3, "0", STR_PAD_LEFT);
- echo "test = $return</br>\n";
- return $return;
- }else{
- return '000';
- }
- }
- //---Variables
- $screen_dimensions = array('height' => 300, 'width' => 700); //Do not make this very large as too much memory/time will be used and the program will fail.
- $screen_position = array('x' => 0, 'y' => 0, 'z' => 0); //I spose this could be changed, but I haven't bothered with it.
- $screen_viewingAngle = 140; //In degrees
- $focus = (0.5*$screen_dimensions['width'])/tan(deg2rad(0.5*$screen_viewingAngle)); //The Y frame which will be in focus.
- $depth_of_field = 30; //This is kinda like the F-stop on a lens.
- $angular_threshold = 2; //This is kinda different, but it's basically the accuracy of the imaging. 0 would be perfect, but would not work.
- /* Do not use these yet.
- $screen_yaw = 0;
- $screen_pitch = 0;
- $screen_roll = 0;
- */
- /* BEGIN MAIN PROGRAM */
- //---Pre-processing variables (inititators of a sort)
- $offset['y'] = (0.5*$screen_dimensions['width'])/tan(deg2rad(0.5*$screen_viewingAngle));
- $offset['x'] = 0.5*$screen_dimensions['width'];
- $offset['z'] = 0.5*$screen_dimensions['height'];
- $current['z'] = $offset['z'];
- $current['x'] = -$offset['x'];
- $cycle = 0;
- //---Build points to be displayed
- for($i = 0; $i < 36; $i++){
- if($i > 0){
- $current['x'] += $screen_dimensions['width']/5;
- }
- if($current['x'] > $offset['x']){
- $current['x'] = -$offset['x'];
- $current['z'] -= $screen_dimensions['height']/5;
- }
- $points[$i]['x'] = $current['x'];
- $points[$i]['y'] = $offset['y'];
- $points[$i]['z'] = $current['z'];
- print "Z = ".$points[$i]['z'] ." Y = ".$points[$i]['y'] ." X = ".$points[$i]['x'] ."</br>\n";
- }
- //---Copy these points back a few times
- foreach($points as $key => $value){
- $points[$key+36]['x'] = $value['x'];
- $points[$key+36]['z'] = $value['z'];
- $points[$key+36]['y'] = $value['y']+40;
- $points[$key+72]['x'] = $value['x'];
- $points[$key+72]['z'] = $value['z'];
- $points[$key+72]['y'] = $value['y']+80;
- $points[$key+108]['x'] = $value['x'];
- $points[$key+108]['z'] = $value['z'];
- $points[$key+108]['y'] = $value['y']+120;
- }
- //---Reset '$current' variables
- $current['z'] = 0.5*$screen_dimensions['height'];
- $current['x'] = -0.5*$screen_dimensions['width'];
- //---Begin writing file
- unlink("image.ppm"); //file that we will be editing, we are are deleting the file so we have a clean file to work on
- $fh = fopen("image.ppm", 'a+'); //creates the image file that we will be editting and opens the file for appending
- $print_height = 1+$screen_dimensions['height'];
- $print_width = 1+$screen_dimensions['width'];
- $startWrite = "P3\n# image.ppm\n$print_width $print_height\n255"; //fomatting the ppm file
- fwrite($fh, $startWrite);
- //---Begin building rays and printing the file
- //These two steps are done at the same time for efficiency, and memory
- for($i = 0; $i < ($print_height*$print_width); $i++){
- //---Origin points
- $lines['x'][1] = $screen_position['x'];
- $lines['y'][1] = $screen_position['y'];
- $lines['z'][1] = $screen_position['z'];
- //---Resolve when to move to the next row of pixels
- if($i > 0){
- $current['x']++;
- }
- if($current['x'] > $offset['x']){
- $current['x'] = -$offset['x'];
- $current['z']--;
- }
- //---To points
- $lines['x'][2] = $current['x'];
- $lines['y'][2] = $offset['y'];
- $lines['z'][2] = $current['z'];
- echo $lines['x'][2].", ".$lines['z'][2].", ".$lines['y'][2]."</br> \n";
- if($i%5 == 0){ //basic formatting decides when a new line must be placed
- $Nl = " \n";
- }else{
- $Nl = " ";
- }
- $out = 000;
- foreach($points as $value){
- $colour = minAngle($lines, $value, $angular_threshold, $focus, $depth_of_field);
- if($colour == 255){
- $out = $colour;
- echo "white </br>\n";
- break 1;
- }elseif($colour >= 0 && $colour >= $out){
- $out = $colour;
- }
- }
- $write = "$Nl$out $out $out"; //the line that is to be outputted
- fwrite($fh, $write); //write the pixel to the file.
- }
- fclose($fh);
- ?>
Advertisement
Add Comment
Please, Sign In to add comment