// Persistence of Vision Ray Tracer Scene Description File
// File: ?.pov
// Vers: 3.5
// Desc: Basic Scene Example
// Date: mm/dd/yy
// Auth: ?
//

#version 3.5;

#include "colors.inc"
#include "glass.inc"
#include "textures.inc"
#include "mrg_misc2.inc"
//events
#declare lclock=clock+.6;
#declare t1=ltrans(lclock/.2);          //trace rays cross system
#declare t2=transitor(lclock,.3,.2);    // fade in labels
#declare t3=transitor(lclock,.5,.4);    // stop down aperature

global_settings {
  assumed_gamma 1.0
  ambient_light .5
}

background{ color rgb .0}
#declare cf=3;
camera {
  location  <-0, 0, -4.0>*cf
  direction 1.5*z
  right     x*image_width/image_height
  look_at   <1, 0.0,  0.0>
  angle 50/cf
}

light_source {
  <0, 0, 0>            // light's position (translated below)
  color rgb <1, .5, 1>  // light's color
  translate <-30, 30, -30>
}

// ----------------------------------------

//lens
intersection{
     sphere{0,1 translate .8*x}     
     sphere{0,1 translate -.8*x}
     texture{pigment { color rgbt <.1,.1,.9,.7> } finish{ambient .4 diffuse .4 specular 0.9}}
     //texture{T_Glass2}
     scale <.4,2,2>

}
//axis and focal point

cylinder{
     -1.5*x,3*x .01      
     no_reflection no_shadow
     texture{pigment { color rgb <.8,.6,.1> } finish{ambient 1 diffuse 1}}
     }
#declare fp0=2;
#if(t2>0)
     sphere{
          x*fp0,.02 scale <1,1.25,1.25> 
          no_reflection no_shadow
          texture{pigment { color rgbt <.5*t2,.0,.0,1-t2> } finish{ambient 1 diffuse 1}}
     }
     text {
          ttf  "arial.ttf", "Paraxial Focal Point",.05,0 
          texture{pigment { color rgb <.5,.0,.0>*t2 } finish{ambient 1 diffuse 1}}
          no_shadow no_reflection
          scale .125 translate fp0*x+.7*y-1.2*x
     }
     cylinder{.995*fp0*x+.05*y, .665*y+1.5*x,.0075 translate -.0051*z
          texture{pigment { color rgb <.5,.0,.0>*t2 } finish{ambient 1 diffuse 1}}
          no_shadow no_reflection
     }
#end
    
    
//aperature    
    
#declare apt_r=1.2*(1-t3)+.3*t3;
#declare apt_t=.075;
#declare apt_outr=1.3;
#declare apt_offset=.14;

#declare apt_p5=
     box { <-apt_t/2, apt_r, -.1>, <apt_t/2, apt_outr, .1>  translate -apt_offset*x
          texture{pigment { color rgb <.2,.2,.2> } finish{ambient .5 diffuse .2 specular .2}}
          };
object{apt_p5}
object{apt_p5 scale <1,-1,1>}




#declare fph=function(hh) { fp0-.5*hh*hh }
#declare yfph=function(xx,hh) {(1-xx/fph(hh))*hh}

//rays
#declare rdy=.1;
#declare hstart=1.15;
#declare h=hstart;
#declare rayend=1.2*fp0;
#declare arrow2x=1.1*fp0;
#declare alength=.1;
#declare rayprogx=-1+(rayend+1)*t1;
//only evaluate intersection while extending rays... render is too long for the impatient!!!
#if(t1<1)
     intersection{
          union{          
               #while (h>=0)
                    #declare raypair=union{
                         #if(h<apt_r)
                              #declare this_fp=fph(h);
                              #declare vx=alength*this_fp/sqrt(this_fp*this_fp+h*h);
                              #declare vy=-vx*h/this_fp;
                              cylinder{-1*x+h*y, h*y,.0075 } 
                              cylinder{rayend*x+yfph(rayend,h)*y, h*y,.0075}
                              cone{0,.02,alength*x,0 translate h*y-.5*x}
                              cone{0,.02,vx*x+vy*y,0 translate arrow2x*x+yfph(arrow2x,h)*y} 
                         #else
                              cylinder{-1*x+h*y, h*y-(apt_t/2+apt_offset)*x,.0075 } 
                              cone{0,.02,alength*x,0 translate h*y-.5*x}
                         #end
                    }
                    object{raypair}
                    object{raypair scale <1,-1,1>}
                    #declare h=h-rdy;
               #end
               no_reflection no_shadow
               texture{pigment { color rgbt <.8,.8,.8,.5> } finish{ambient 1 diffuse 1 specular 0}}
          }
          box{-2*x-3*(y+z), rayprogx*x+3*(y+z)}
     }
#else
     union{          
          #while (h>=0)
               #declare raypair=union{
                    #if(h<apt_r)
                         #declare this_fp=fph(h);
                         #declare vx=alength*this_fp/sqrt(this_fp*this_fp+h*h);
                         #declare vy=-vx*h/this_fp;
                         cylinder{-1*x+h*y, h*y,.0075 } 
                         cylinder{rayend*x+yfph(rayend,h)*y, h*y,.0075}
                         cone{0,.02,alength*x,0 translate h*y-.5*x}
                         cone{0,.02,vx*x+vy*y,0 translate arrow2x*x+yfph(arrow2x,h)*y} 
                    #else
                         cylinder{-1*x+h*y, h*y-(apt_t/2+apt_offset)*x,.0075 } 
                         cone{0,.02,alength*x,0 translate h*y-.5*x}
                    #end
               }
               object{raypair}
               object{raypair scale <1,-1,1>}
               #declare h=h-rdy;
          #end
          no_reflection no_shadow
          texture{pigment { color rgbt <.8,.8,.8,.5> } finish{ambient 1 diffuse 1 specular 0}}
     }
#end

/*   */
// find that circle of least confusion
#declare dx=.005;
#declare dy=.005;
#declare hstart=min(apt_r,hstart);
#declare tryx=(fp0+fph(hstart))/2;
#declare clcr=yfph(tryx,-hstart);
#declare tempr=0;


#while (tempr<clcr)
     #declare tryx=tryx-dx;
     #declare clcr=yfph(tryx,-hstart);

     //find the highest near-paraaxial ray at this x
     #declare temph=dy;
     #declare tempr=yfph(tryx,temph);
     #while (temph<hstart)
          #declare temph=temph+dy;
          #declare newr=yfph(tryx,temph);
          #if(newr>=tempr)
               #declare tempr=newr;     
          #else
               #declare temph=hstart; //we're here
          #end     
     #end
#end
#declare clcr = tempr;
#declare clcx=tryx; 
#if(t2>0)
     union{
          cylinder{-.0001*x,.0001*x, clcr  translate x*clcx}
          torus { clcr, 0.01 scale <1,1.5,1> rotate z*90 translate x*clcx  }
          cylinder{-(clcr+.1)*y,-.9*y,.0075 translate clcx*x}
          no_shadow no_reflection
          texture{pigment { color rgbt <.0,.75*t2,.0, 1-t2> } finish{ambient .5 diffuse 1} }
     
     }   
     
     text {
          ttf  "arial.ttf", "Circle of Least Confusion",.05,0 
          texture{pigment { color rgb <.0,.75,.0>*t2 } finish{ambient 1 diffuse 1}}
          no_shadow no_reflection
          scale .125 translate clcx*x-y-.6*x
     }
#end 