// Cāmeras e luzes padronizadas // Last edited on 2024-05-01 19:43:01 by stolfi #macro camlight(ctr,rad,cav,liv,dst,upp,nrings,lux) // {ctr} = center of interest in scene. // {rad} = approximate bounding radius of scene. // {cav} = vector pointing from {ctr} to camera; only its direction matters. // {liv} = vector pointing from {ctr} to light source; only its direction matters. // {dst} = the distance from camera to {ctr}. // {upp} = the scene's vertical axis ({sky} parameter), usually {z} or {y}. // {lux} = a scaling factor for the intensity of standard light sources. #local cdr = vnormalize(cav); #local swh = sqrt(image_width/image_height); #local odiag = 1.3*dst/rad; camera { location ctr + dst*cdr right -swh/odiag*x up 1.0/swh/odiag*y sky upp look_at ctr } #if (lux > 0) #local ldr = vnormalize(liv); #local light_elev = degrees(atan2(ldr.z,sqrt(ldr.x*ldr.x + ldr.y*ldr.y))); #local light_azim = degrees(atan2(ldr.y,ldr.x)); #local angrad = 5; // Angular light array radius (degrees). object{ lamp_array(nrings,angrad,lux) rotate 360*(sqrt(5)-1)/2*x scale 2*dst rotate -(90 - 0.667*(90 - light_elev))*y rotate (light_azim + 15)*z translate ctr } #end #end #macro lamp_array(nrings,angrad,lux) // A round lamp array with {nrings} rings of lamps, // total angular radius {angrad}, and total strength {lux}. // The array is centered on the X axis at distance 1 // from the origin. // Compute normalization factor {st_norm} for ring strengths: #local k = 0; // Light ring index. #local st_norm = 0; #while (k < nrings) #local st = camlight_rel_ring_strength(k,nrings); #local st_norm = st_norm + st; #local k = k + 1; #end #local coin = seed(4615); union{ #local k = 0; // Light ring index. #while (k < nrings) // Number of lamps {np} in ring {k} and initial phase {phas}: #local np = (k = 0 ? 1 : 6*pow(2,k-1)); // Number of lamps in ring. #local phas = (k = 0 ? 0 : 0.6180/np); // Rel. position of first lamp // Total relative strength {st} of lamps in ring: #local st = camlight_rel_ring_strength(k,nrings); // Generate the lamps in the ring: #local p = 0; // Lamp index. #while (p < np) // Jittering terms in {k,p}: #local dk = (k = 0 ? 0 : 0.20*(2*rand(coin)-1)); #local dp = (k = 0 ? 0 : 0.20*(2*rand(coin)-1)); // Angular radius of ring: #local tau = camlight_rel_ring_radius(k+dk,nrings); // Relative radius of ring #local phi = (k = 0 ? 0 : angrad*tau); // Angular radius of ring. #local tht = 360*(phas + (p+dp)/np); light_source { <1,0,0> color rgb (st/st_norm/np)*lux*<1.0,1.0,1.0> rotate phi*z rotate tht*x } // #warning concat("light at phi = ", str(phi,6,1), " tht = ", str(tht,6,1), "\n") #local p = p + 1; #end #local k = k + 1; #end } #end #macro camlight_rel_ring_strength(k,nrings) // Relative total strength (unnormalized) of lights in ring {k} of {nrings} rings. #local st = (k = 0 ? 0.5*0.5 : 2*k)*((nrings-0.5)*(nrings-0.5) - k*k); st #end #macro camlight_rel_ring_radius(k,nrings) // Relative radius of light ring {k} in a lamp cluster with {nrings} rings. #local rr = (k = 0 ? 0 : pow(k/(nrings - 0.5),1.5)); rr #end