//Copy to comandline:
// +fn16 +kff3 -a
//
//This does not work with anti aliasing if you want it
//you'll have to render at a higher res and resize

#declare Step=frame_number; //Render all three Steps in an anim

#declare Cam_pos=<-8,10,-12>;// look at p_textures.inc
#declare Cam_look_at=<0,0,0>;// to see for what these 
#declare Scene_scale=20;     // variables are used

#declare Radius_Dist=2; //Blur radius for depth information
#declare Radius_Norm=2; //Blur radius for normal information

#declare Di_Dist=0.01;  // when the difference between the original image(from Step 2)
#declare Di_Norm=0.015; // and the blurred one is bigger than this threshold than the pixel is black.
                        // if you see some strange artifacts increase these values
                        // if there somewhere should be lines but aren't decrease them
      
#declare Dist=off; // use depth info to draw lines(not neccesary in this scene)
#declare Norm=on;  // --- normal ---- -- ---- -----

#include"math.inc"
#include"p_objects.inc"
#include"p_textures.inc"


//Camera for Step 1 and 2
#if(Step=1|Step=2)
  camera{
    location Cam_pos
    right x*image_width/image_height
    up y
    look_at Cam_look_at
    angle 60
  }
#end

//Step 1
#if(Step=1)
  global_settings{
    assumed_gamma 1
  }  

  light_source{<20,50,-10> rgb 1}
  
  object{Something
    texture{Something_tex}
  }  
  object{Ground
    texture{Ground_tex}
  }  
#end

//Step 2
#if(Step=2)
  object{Something
    texture{Info_tex}
  }  
  object{Ground
    texture{Info_tex}
  }
#end

//Step 3
/*
In Step 3 the lines are created
from the image created in Step 2 and finaly
the image from Step 1 and the lines are combined
*/
#if(Step=3)
  
  //we'll be looking at three planes with images on them
  //so we need an orthographic camera
  camera{
    orthographic
    location -z
    right image_width*x
    up image_height*y
    look_at 0
  }
  //create function containing image from Step 2
  #declare Ifun=
  function{
    pigment{
      image_map{
        png "p_main2"      
      }
      warp{repeat x flip x}// this is to avoid black lines at the image borders
      warp{repeat y flip y}// Thanks to TinCanMan for this
      translate -.5
      scale <image_width,image_height,1>  
    }  
  }
  
  //Here the red(depth) and green and blue(normal) channels get blurred
  #declare Ifun_red_blur=
  function{
    (
      #local Z1=-Radius_Dist;
      #while(Z1<Radius_Dist)
        #local Z2=-Radius_Dist;
        #while(Z2<Radius_Dist)
          Ifun(x+Z1,y+Z2,z).red+
          #local Z2=Z2+1;
        #end
        #local Z1=Z1+1;
      #end
      0
    )
    /(Radius_Dist*Radius_Dist*4)
  }
  
  #declare Ifun_green_blur=
  function{
    (
      #local Z1=-Radius_Norm;
      #while(Z1<Radius_Norm)
        #local Z2=-Radius_Norm;
        #while(Z2<Radius_Norm)
          Ifun(x+Z1,y+Z2,z).green+
          #local Z2=Z2+1;
        #end
        #local Z1=Z1+1;
      #end
      0
    )
    /(Radius_Norm*Radius_Norm*4)
  }
  
  #declare Ifun_blue_blur=
  function{
    (
      #local Z1=-Radius_Norm;
      #while(Z1<Radius_Norm)
        #local Z2=-Radius_Norm;
        #while(Z2<Radius_Norm)
          Ifun(x+Z1,y+Z2,z).blue+
          #local Z2=Z2+1;
        #end
        #local Z1=Z1+1;
      #end
      0
    )
    /(Radius_Norm*Radius_Norm*4)
  }
  
  
  //if the difference between the blurred and the original image is smaller   
  //than the threshold the function returns 0 otherwise it returns 1
  #declare Ofun_Distance=
  function{
    select(abs(Ifun_red_blur(x,y,z)-Ifun(x,y,z).red)-Di_Dist,0,1)  
  }
  
  
  //do the same for both normal channels and then put them together
  #declare Ofun_Norm=
  function{
    max(
      select(abs(Ifun_green_blur(x,y,z)-Ifun(x,y,z).green)-Di_Norm,0,1),
      select(abs(Ifun_blue_blur(x,y,z)-Ifun(x,y,z).blue)-Di_Norm,0,1)
    )    
  }
  
  
  //image from Step 1
  plane{z,2
    pigment{
      image_map{
        png "p_main1"      
      }
      translate -.5
      scale <image_width,image_height,1>
    }
    finish{diffuse 0 ambient 1}
  }
  
  //lines caused by the normal
  #if(Norm)  
    plane{z,1
      pigment{
        function{Ofun_Norm(x,y,z)} 
        color_map{
          [0 rgbt 1]
          [1 rgb 0]
        }    
      }
      finish{ambient 1 diffuse 0}
    }
  #end 
  
  //lines caused by the depth 
  #if(Dist)
    plane{z,0
      pigment{
        function{Ofun_Distance(x,y,z)} 
        color_map{
          [0 rgbt 1]
          [1 rgb 0]
        }    
      }
      finish{ambient 1 diffuse 0}
    }
  #end

#end