//*PMRawBegin
// starfield generator

#declare s = seed(17);
#declare m = 100000; // stellar distance
#declare pit2 = 6.283185307;
#declare pid2 = 1.57079632;

#declare n=0;
#while (n < 5000) // count of stars
  #declare ha= rand(s) * pit2;
  #declare va= rand(s) * pid2;
  #declare hm= cos(va) * m;
  #declare mx = cos(ha) * hm;
  #declare my = sin(va) * m;
  #declare mz = sin(ha) * hm;
  #declare sr1 = 75; // smallest stellar radius
  #declare sr2 = 200; // largest stellar radius
  #declare sr = (sr2-sr1) * rand(s) + sr1;

  sphere
  {
    <0, 0, 0> 
    sr // stellar radius
    translate <mx, my, mz>
    pigment
    {
      color <1,1,1>
    }
    finish
    {
      ambient 1.0 // self-illuminating
      diffuse 0.0
    }
  }

  #declare n=n+1;
#end
//*PMRawEnd

// Note to myself: using 120,000 photons
// at present to render the 6-beam raytrace
// diagram.
// 
// Eyepiece focal distance -9.5.
union {
   //*PMName Telescope
   
   intersection {
      //*PMName Photon-containing shape
      
      // Enable "Photon Containing Shape"
      // to allow display of photon paths.
      // This displays the light beam paths,
      // but takes much longer to render.
      // Remember, this object must be hollow.
      union {
         //*PMName Shape components
         
         cylinder {
            //*PMName Telescope Cylinder
            <-8.0001, 0, 0>, <20, 0, 0>, 6.001
         }
         
         cylinder {
            //*PMName Eyepiece Cylinder
            <-14, 0, 0>, <-8, 0, 0>, 0.9
         }
         translate y*16
         rotate <0, 0, 0>
         hollow
      }
      
      box {
         //*PMName Narrow photon-slice box for ray diagram mode
         <-15, -6, -0.05>, <21, 6, 0.05>
         scale 1
         rotate <0, 0, 0>
         translate y*16
         hollow
      }
      
      pigment {
         color rgbf <1, 1, 1, 1>
      }
      
      interior {
         media {
            scattering {
               1, rgb <1, 1, 1>
               extinction 0
            }
         }
      }
      
      photons {
         target
         collect off
      }
      hollow
   }
   
   difference {
      //*PMName Dissector group
      
      union {
         union {
            //*PMName Optical scope components
            
            union {
               //*PMName Front Glass Assembly
               
               intersection {
                  //*PMName Secondary Mirror Assembly
                  
                  cylinder {
                     <-1, 0, 0>, <0, 0, 0>, 2
                     
                     pigment {
                        color rgb <0, 0, 0>
                     }
                     scale 1
                     rotate <0, 0, 0>
                     translate <0, 0, 0>
                  }
                  
                  sphere {
                     <29, 0, 0>, 30
                     
                     finish {
                        reflection {
                           rgb <1, 1, 1>
                        }
                     }
                     scale 1
                     rotate <0, 0, 0>
                     translate <0, 0, 0>
                  }
               }
               
               cylinder {
                  //*PMName Glass Plate
                  <-0.2, 0, 0>, <0.2, 0, 0>, 6
                  
                  pigment {
                     color rgbt <0.23922, 0.54902, 0.81961, 1>
                  }
                  hollow false
               }
               translate <12, 16, 0>
            }
            
            difference {
               //*PMName Primary Mirror Assembly
               
               difference {
                  //*PMName Parabolic Mirror
                  
                  // Create a parabolic mirror by taking
                  // the CSG difference between
                  // a cylinder and a quadric. The
                  // quadric terms:
                  // 
                  // 1 * x^2
                  // 1 * z^2
                  // -1 * y
                  // 
                  // Meaning x^2 + z^2 - y = 0,
                  // or y = x^2 + z^2
                  // 
                  // Adjust y for desired focal length (fl):
                  // 
                  // y = r^2 / 4 fl
                  // 
                  // r = mirror radius
                  // 
                  // for this mirror, radius 6,
                  // focal length 24:
                  // 
                  // y = 6^2 / 4 * 24 = .375
                  // 
                  // 
                  cylinder {
                     <0, 0, 0>, <0, 3, 0>, 1
                  }
                  
                  quadric {
                     <1, 0, 1>,
                     <0, 0, 0>,
                     <0, -1, 0>, 0
                     translate y*1
                  }
                  
                  finish {
                     reflection {
                        rgb <1, 1, 1>
                     }
                  }
                  scale <6, 0.34, 6>
                  
                  photons {
                     target
                     collect off
                  }
                  
                  pigment {
                     color rgbt <1, 1, 1, 1>
                  }
                  rotate z*(-90)
               }
               
               cylinder {
                  //*PMName Eyepiece Opening
                  <-2, 0, 0>, <1, 0, 0>, 0.81
                  hollow false
               }
               translate <-8, 16, 0>
            }
            
            union {
               //*PMName Eyepiece
               
               // Disable this eyepiece lens to get
               // a clear view of the light path.
               // 
               // Latest focus number: -9.5
               intersection {
                  //*PMName Eyepiece Lens
                  
                  sphere {
                     <-3.9192, 0, 0>, 4
                  }
                  
                  sphere {
                     <3.9192, 0, 0>, 4
                  }
                  
                  interior {
                     ior 1.5
                  }
                  
                  pigment {
                     color rgbt <0.13725, 0.61176, 0.8, 1>
                  }
               }
               translate <-9.5, 16, 0>
            }
         }
         
         union {
            //*PMName Scope tube and mounting
            
            union {
               //*PMName Union of scope tube and eyepiece
               
               difference {
                  //*PMName Eyepiece Parts
                  
                  cylinder {
                     <-2, 0, 0>, <-10, 0, 0>, 1
                     hollow false
                  }
                  
                  cylinder {
                     //*PMName eyepiece opening
                     <-1.9, 0, 0>, <-10.1, 0, 0>, 0.8
                     
                     pigment {
                        color rgb <0.05, 0.05, 0.05>
                     }
                     hollow false
                  }
                  hollow
               }
               
               difference {
                  //*PMName Scope Tube Parts
                  
                  difference {
                     //*PMName Tube Cylinder
                     
                     cylinder {
                        //*PMName Outer Cylinder
                        <-8.2, 0, 0>, <13, 0, 0>, 6.2
                        hollow
                     }
                     
                     cylinder {
                        //*PMName Inner cylinder
                        <-7.8, 0, 0>, <13.1, 0, 0>, 6
                        
                        pigment {
                           color rgb <0.05, 0.05, 0.05>
                        }
                        hollow
                     }
                     hollow false
                  }
                  
                  cylinder {
                     //*PMName Eyepiece Opening
                     <-10, 0, 0>, <-2, 0, 0>, 0.81
                     hollow
                  }
                  hollow false
               }
               rotate <0, 0, 0>
               translate y*16
            }
            
            union {
               //*PMName Scope Mount
               
               union {
                  //*PMName Side Support
                  
                  cylinder {
                     //*PMName Mount
                     <0, 0, 1>, <0, 0, -1>, 1.5
                     translate y*16
                     hollow false
                  }
                  
                  box {
                     //*PMName Vertical
                     <-2.5, 0, -0.5>, <2.5, 16, 0.5>
                  }
                  
                  cylinder {
                     //*PMName Bearing
                     <0, 0, 0.55>, <0, 0, -0.55>, 3.5
                     translate y*16
                     hollow false
                  }
                  translate z*7.2
                  hollow false
               }
               
               cylinder {
                  //*PMName Ground Post
                  <0, 0, 0>, <0, -36, 0>, 2
                  scale 1
                  rotate <0, 0, 0>
                  translate <0, 0, 0>
               }
               
               union {
                  //*PMName Side Support
                  
                  cylinder {
                     //*PMName Mount
                     <0, 0, 1>, <0, 0, -1>, 1.5
                     translate y*16
                     hollow false
                  }
                  
                  box {
                     //*PMName Vertical
                     <-2.5, 0, -0.5>, <2.5, 16, 0.5>
                  }
                  
                  cylinder {
                     //*PMName Bearing
                     <0, 0, 0.55>, <0, 0, -0.55>, 3.5
                     translate y*16
                     hollow false
                  }
                  translate z*(-7.2)
                  hollow false
               }
               
               cylinder {
                  //*PMName Revolving Base
                  <0, 0.5, 0>, <0, -0.5, 0>, 8
               }
               
               pigment {
                  color rgb <0.0705882, 0.027451, 0.137255>
               }
               translate <0, 0, 0>
               rotate <0, 0, 0>
               
               finish {
                  specular 0.1
                  
                  reflection {
                     rgb <0.109804, 0.109804, 0.109804>
                  }
               }
               hollow false
            }
            rotate <0, 0, 0>
         }
      }
      
      // Enable "Dissecting Box" to show the scope's
      // insides, including the light paths.
      box {
         //*PMName Dissecting Box
         <-15, -8, -0.1>, <12, 15, -10>
         
         pigment {
            color rgb <0.4, 0, 0>
         }
         scale 1
         rotate <0, 0, 0>
         translate <3.48839, 8.76812, 0>
      }
   }
   
   pigment {
      color rgb <0.0745098, 0.0941176, 0.666667>
   }
   
   finish {
   }
   translate y*30
   rotate <0, 0, 0>
   hollow false
}

difference {
   //*PMName Aperture Plate
   
   box {
      //*PMName Plate
      <-0.01, -30, -30>, <0.01, 30, 30>
      scale 1
      rotate <0, 0, 0>
      translate <0, 0, 0>
      hollow false
   }
   
   union {
      //*PMName Holes
      
      cylinder {
         <-0.1, 0, 0>, <0.1, 0, 0>, 0.05
         translate y*5.5
         hollow false
      }
      
      cylinder {
         <-0.1, 0, 0>, <0.1, 0, 0>, 0.05
         translate y*4
         hollow false
      }
      
      cylinder {
         <-0.1, 0, 0>, <0.1, 0, 0>, 0.05
         translate y*2.5
         hollow false
      }
      
      cylinder {
         <-0.1, 0, 0>, <0.1, 0, 0>, 0.05
         translate y*(-2.5)
         hollow false
      }
      
      cylinder {
         <-0.1, 0, 0>, <0.1, 0, 0>, 0.05
         translate y*(-4)
         hollow false
      }
      
      cylinder {
         <-0.1, 0, 0>, <0.1, 0, 0>, 0.05
         translate y*(-5.5)
         hollow false
      }
      hollow false
   }
   translate <1000, 46, 0>
   
   pigment {
      color rgb <0, 1, 0.0156863>
   }
   hollow false
}

// To change the position or orientation
// of the scope, go the the last items in
// the list for "Telescope", find and use
// the "translate" and "rotate" items there.
// There are many other similar translate
// and rotate functions deeper in the tree,
// but they will just make your life complicated.
global_settings {
   adc_bailout 0.0039216
   ambient_light rgb <0.490196, 0.490196, 0.490196>
   assumed_gamma 1.5
   max_trace_level 20
   max_intersections 25
   noise_generator 2
   
   photons {
      count 120000
      media 120000, 0.005
      jitter 0
      autostop 1
   }
}

light_source {
   //*PMName Photon Light
   <100000, 46, 0>, rgb <0, 5, 5>
   spotlight
   radius 3
   falloff 3
   point_at <0, 46, 1>
   media_interaction off
   media_attenuation off
   
   photons {
      refraction on
      reflection on
   }
}

plane {
   <0, 1, 0>, 0
   
   pigment {
      agate
      agate_turb 0.5
      turbulence <1, 0, 1>
      
      color_map {
         [ 0 color rgb <0, 0, 0>
         ]
         [ 0.5 color rgb <0.517647, 0.396078, 0.235294>
         ]
      }
   }
   scale 1
   rotate <0, 0, 0>
   translate <0, 0, 0>
   hollow false
}

sky_sphere {
   pigment {
      color rgb <0.0627451, 0, 0.470588>
   }
}

light_source {
   //*PMName Narrow camera Light
   <-12, 42, 0>, rgb <0, 0, 0>
   media_interaction off
   media_attenuation off
}

camera {
   //*PMName Narrow Cam
   perspective
   location <-9.5, 46, -8.0401>
   sky <0, 1, 0>
   direction <0, 0, 1>
   right <1.3333, 0, 0>
   up <0, 1, 0>
   look_at <-9.5, 46, 0.038035>
   angle 30
}

light_source {
   //*PMName Non-photon Light
   <-102.87, 94.621, -107.45>, rgb <2, 2, 2>
   spotlight
   radius 10
   falloff 10
   point_at <0, 46, 1>
   media_interaction off
   media_attenuation off
}

light_source {
   //*PMName Non-photon Light
   <117.97, 97.021, -113.22>, rgb <2, 2, 2>
   spotlight
   radius 10
   falloff 10
   point_at <0, 46, 1>
   media_interaction off
   media_attenuation off
}