Efficient Monte Carlo Rendering with Realistic Lenses

Efficient Monte Carlo Rendering with Realistic Lenses

Additional Material for: Efficient Monte Carlo Rendering with Realistic Lenses Johannes Hanika and Carsten Dachsbacher, Karlsruhe Institute of Technology, Germany 1 Adaptation of smallpt Figure 1: Smallpt without and with lens distortion. The lens system is a simple lensbaby- like lens to clearly show all aberrations. Since smallpt does not support spectral rendering, we used a grey transport version of the polynomials. We need to add 6 lines of code (not counting the preprocessor switches). The curved edges in the left image are due to the fact that the walls are actually modeled as spheres in this renderer. Listing 1 shows modified smallpt source code with camera distortion. The block between #if 1 and #else applies the transform. Sampling is done through the inner pupil and the transformation function is applied, i.e. no aperture importance sampling is done. The results with and without lens distortions can be seen in Figure 1. Listing 1: Adapted smallpt code #include <math . h> // smallpt,a Path Tracer by Kevin Beason, 2008 #include <s t d l i b . h> // Make:g++ −O3 −fopenmp smallpt.cpp −o smallpt #include <s t d i o . h> // Remove" −fopenmp" forg++ version < 4 . 2 s t r u c t Vec f // Usage: time./smallpt 5000 && xv image. ppm double x , y , z ;// position, also color(r,g,b) Vec (double x_=0, double y_=0, double z_=0)f x=x_ ; y=y_ ; z=z_ ; g Vec o p e r a t o r+(const Vec &b ) const f r e t u r n Vec ( x+b . x , y+b . y , z+b . z ); g Vec operator −(const Vec &b ) const f r e t u r n Vec ( x−b . x , y−b . y , z−b . z ); g Vec o p e r a t o r *(double b ) const f r e t u r n Vec ( x*b , y*b , z*b ); g Vec mult (const Vec &b ) const f r e t u r n Vec ( x*b . x , y*b . y , z*b . z ); g Vec& norm () f r e t u r n * t h i s= * t h i s * (1/ sqrt ( x*x+y*y+z*z )); g double dot (const Vec &b ) const f r e t u r n x*b . x+y*b . y+z*b . z ; g // cross: Vec o p e r a t o r%( Vec&b ) f r e t u r n Vec ( y*b . z−z*b . y , z*b . x−x*b . z , x*b . y−y*b . x ); g g ; s t r u c t Ray f Vec o , d ; Ray ( Vec o_ , Vec d_ ): o ( o_ ), d ( d_ ) fg g ; 1 enum Refl_t f DIFF , SPEC , REFR g ;// material types, used in radiance() s t r u c t Sphere f double rad ;// radius Vec p , e , c ;// position, emission, color Refl_t refl ;// reflection type(DIFFuse, SPECular, REFRactive) Sphere (double rad_ , Vec p_ , Vec e_ , Vec c_ , Refl_t refl_ ): rad ( rad_ ), p ( p_ ), e ( e_ ), c ( c_ ), refl ( refl_ ) fg double intersect (const Ray &r ) const f // returns distance,0 if nohit Vec op = p−r . o ;// Solvet^2 *d.d+2 * t *(o−p).d+(o −p).(o −p)−R^2=0 double t , eps=1e−4, b=op . dot ( r . d ), det=b*b−op . dot ( op )+rad * rad ; i f( det <0) return 0; else det=sqrt ( det ); r e t u r n( t=b−det )>eps ? t :(( t=b+det )>eps ? t : 0) ; g g ; Sphere spheres [ ] = f//Scene: radius, position, emission, color, material Sphere (1 e5 , Vec ( 1e5 +1,40.8,81.6) , Vec (), Vec (.75,.25,.25) , DIFF ),//Left Sphere (1 e5 , Vec(−1e5 +99,40.8,81.6) , Vec (), Vec (.25,.25,.75) , DIFF ),//Rght Sphere (1 e5 , Vec ( 5 0 , 4 0 . 8 , 1e5 ), Vec (), Vec (.75,.75,.75) , DIFF ),//Back Sphere (1 e5 , Vec (50 ,40.8 , −1 e5+170) , Vec (), Vec (), DIFF ),//Frnt Sphere (1 e5 , Vec ( 5 0 , 1e5 , 8 1 . 6 ) , Vec (), Vec (.75,.75,.75) , DIFF ),//Botm Sphere (1 e5 , Vec (50 , −1 e5 +81.6,81.6) , Vec (), Vec (.75,.75,.75) , DIFF ),//Top Sphere ( 1 6 . 5 , Vec (27,16.5,47) , Vec (), Vec ( 1 , 1 , 1 ) * . 9 9 9 , SPEC ),//Mirr Sphere ( 1 6 . 5 , Vec (73,16.5,78) , Vec (), Vec ( 1 , 1 , 1 ) * . 9 9 9 , REFR ),//Glas Sphere (600 , Vec ( 5 0 , 6 8 1 . 6 − . 2 7 , 8 1 . 6 ) , Vec (12,12,12) , Vec (), DIFF )//Lite g ; i n l i n e double clamp (double x ) f r e t u r n x<0 ? 0 : x>1 ? 1 : x ; g i n l i n e int toInt (double x ) f r e t u r n int( pow ( clamp ( x ) , 1 / 2 . 2 ) *255+.5) ; g i n l i n e bool intersect (const Ray &r , double& t , int& id ) f double n=sizeof( spheres )/sizeof( Sphere ), d , inf=t=1e20 ; f o r(int i=int( n ); i−−;) if(( d=spheres [ i ]. intersect ( r ) )&&d<t ) ft=d ; id=i ; g r e t u r n t<inf ; g Vec radiance (const Ray &r , int depth , unsigned short *Xi ) f double t ;// distance to intersection i n t id =0;// id of intersected object i f(! intersect ( r , t , id )) return Vec ();// if miss, return black const Sphere &obj = spheres [ id ];// the hit object Vec x=r . o+r . d*t , n=(x−obj . p ). norm (), nl=n . dot ( r . d ) <0?n : n*−1, f=obj . c ; double p = f . x>f . y && f . x>f . z ? f . x : f . y>f . z ? f . y : f . z ;// max refl i f (++depth >5) if( erand48 ( Xi )<p ) f=f *(1/ p ); else return obj . e ;//R.R. i f( obj . refl == DIFF ) f // Ideal DIFFUSE reflection double r1=2*M_PI * erand48 ( Xi ), r2=erand48 ( Xi ), r2s=sqrt ( r2 ); Vec w=nl , u=(( fabs ( w . x ) >.1? Vec ( 0 , 1 ) : Vec ( 1 ) )%w ). norm (), v=w%u ; Vec d = ( u* cos ( r1 ) * r2s + v* sin ( r1 ) * r2s + w* sqrt(1−r2 )). norm (); r e t u r n obj . e + f . mult ( radiance ( Ray ( x , d ), depth , Xi )); g e l s e if( obj . refl == SPEC )// Ideal SPECULAR reflection r e t u r n obj . e + f . mult ( radiance ( Ray ( x , r . d−n *2* n . dot ( r . d )), depth , Xi )); Ray reflRay ( x , r . d−n *2* n . dot ( r . d ));// Ideal dielectric REFRACTION bool into = n . dot ( nl ) >0;// Ray from outside going in? double nc=1, nt =1.5 , nnt=into ? nc/nt : nt/nc , ddn=r . d . dot ( nl ), cos2t ; i f(( cos2t=1−nnt * nnt*(1−ddn * ddn )) <0)// Total internal reflection r e t u r n obj . e + f . mult ( radiance ( reflRay , depth , Xi )); Vec tdir = ( r . d* nnt − n * (( into ?1: −1) *( ddn * nnt+sqrt ( cos2t )))). norm (); double a=nt−nc , b=nt+nc , R0=a*a /( b*b ), c = 1−(into?−ddn : tdir . dot ( n )); double Re=R0+(1−R0 ) *c*c*c*c*c , Tr=1−Re , P =.25+.5* Re , RP=Re/P , TP=Tr/(1−P ); r e t u r n obj . e + f . mult ( depth>2 ? ( erand48 ( Xi )<P ?// Russian roulette radiance ( reflRay , depth , Xi ) *RP : radiance ( Ray ( x , tdir ), depth , Xi ) *TP ): radiance ( reflRay , depth , Xi ) *Re+radiance ( Ray ( x , tdir ), depth , Xi ) *Tr ); g i n t main (int argc , char * argv []) f i n t w=1152 , h=768 , samps = argc==2 ? atoi ( argv [ 1 ] ) /4 : 1 ;//# samples // Ray cam(Vec(50,52,295.6), Vec(0, −0.042612 , −1).norm());// cam pos, dir Ray cam ( Vec (50,52,295.6) , Vec (0 , −0.12 , −1) . norm ());// cam pos, dir Vec cx=Vec ( w *.5135/ h ), cx1=Vec ( . 5 1 3 5 ) , cy=(cx%cam . d ). norm () * . 5 1 3 5 , r , *c=new Vec [ w*h ];// cx1 is forminga scaled ortho −nomal system with cy, no a s p e c t ratio skew in there. #pragma omp parallel for schedule(dynamic, 1) private(r)// OpenMP f o r(int py =0; py<h ; py++)f // Loop over image rows fprintf ( stderr ," n rRendering (%d spp) %5.2f%%", samps * 4 , 1 0 0 . * py /( h−1) ) ; f o r(unsigned short px=0, Xi [ 3 ] = f 0 , 0 , py*py*py g ; px<w ; px++)// Loop cols f o r(int sy=0, i=(h−py −1)*w+px ; sy <2; sy++)//2x2 subpixel rows f o r(int sx =0; sx <2; sx++, r=Vec ()) f //2x2 subpixel cols f o r(int s=0; s<samps ; s++)f double r1=2*erand48 ( Xi ), ddx=r1<1 ? sqrt ( r1 ) −1: 1−sqrt(2−r1 ); double r2=2*erand48 ( Xi ), ddy=r2<1 ? sqrt ( r2 ) −1: 1−sqrt(2−r2 ); #if1// simple lens element, gray transport f l o a t x = 3 6 . 0 f * (( sx +.5f + px )/w − . 5 ) , y = 2 4 . 0 f * (( sy +.5f + py )/h − . 5 ) ;// 36x24mm film back const float dx = ddx * 5 . f / 3 0 . f − x / 3 0 . f , dy = ddy * 5 . f / 3 0 . f − y / 3 0 . f ;// abuse pixel filter sampling for direction, scale by p u p i l radius/ distance to sensor x += 0.355 f * dx , y += 0.355 f * dy ;// need −0.355mm sensor offset to focus at 210dm world space const float out_x = 39.2597 * dx + −15.1427 * dx * dy * dy + −15.1427 * dx * dx * dx + −0.178326 * y * dx * dy + 0.00151706 * y * y * dx + 0.813123 * x + −0.456 * x * dy * dy + −0.634326 * x * dx * dx + −0.0121613 * x * y * dy + −7.55302e −05 * x * y * y + −0.0106442 * x * x * dx + −7.55302e−05 * x * x * x ; const float out_y = 39.2597 * dy + −15.1427 * dy * dy * dy + −15.1427 * dx * dx * dy + 0.813123 * y + −0.634326 * y * dy * dy + −0.456 * y * dx * dx + −0.0106442 * y * y * dy + −7.55302 e−05 * y * y * y + −0.178326 * x * dx * dy + −0.0121613 * x * y * dx + 0.00151706 * x * x * dy + −7.55302e−05 * x * x * y ; const float out_dx = 0.0143372 * dx + −1.24884 * dx * dy * dy + −1.24884 * dx * dx * dx + −0.0457492 * y * dx * dy + −0.000763789 * y * y * dx + −0.0251745 * x + −0.0230086 * x * dy * dy + −0.0687577 * x * dx * dx + −0.00105712 * x * y * dy + −2.29452e−05 * x * y * y + −0.00182091 * x * x * dx + −2.29452e−05 * x * x * x ; const float out_dy = 0.0143372 * dy + −1.24884 * dy * dy * dy + −1.24884 * dx * dx * dy + −0.0251745 * y + −0.0687577 * y * dy * dy + −0.0230086 * y * dx * dx + −0.00182091 * y * y * dy + −2.29452e−05 * y * y * y + −0.0457492 * x * dx * dy + −0.00105712 * x * y * dx + −0.000763789 * x * x * dy + −2.29452e−05 * x * x * y ; Vec d = cx1 * out_dx + cy* out_dy + cam .

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    17 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us