Blender V2.61 - r43446

kernel_emission.h

Go to the documentation of this file.
00001 /*
00002  * Copyright 2011, Blender Foundation.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License
00006  * as published by the Free Software Foundation; either version 2
00007  * of the License, or (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software Foundation,
00016  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00017  */
00018 
00019 CCL_NAMESPACE_BEGIN
00020 
00021 /* Direction Emission */
00022 
00023 __device float3 direct_emissive_eval(KernelGlobals *kg, float rando,
00024     LightSample *ls, float u, float v, float3 I)
00025 {
00026     /* setup shading at emitter */
00027     ShaderData sd;
00028 
00029     shader_setup_from_sample(kg, &sd, ls->P, ls->Ng, I, ls->shader, ls->object, ls->prim, u, v);
00030     ls->Ng = sd.Ng;
00031 
00032     /* no path flag, we're evaluating this for all closures. that's weak but
00033        we'd have to do multiple evaluations otherwise */
00034     shader_eval_surface(kg, &sd, rando, 0);
00035     
00036     float3 eval;
00037 
00038     /* evaluate emissive closure */
00039     if(sd.flag & SD_EMISSION)
00040         eval = shader_emissive_eval(kg, &sd);
00041     else
00042         eval = make_float3(0.0f, 0.0f, 0.0f);
00043 
00044     shader_release(kg, &sd);
00045 
00046     return eval;
00047 }
00048 
00049 __device bool direct_emission(KernelGlobals *kg, ShaderData *sd, int lindex,
00050     float randt, float rando, float randu, float randv, Ray *ray, float3 *eval)
00051 {
00052     LightSample ls;
00053 
00054 #ifdef __MULTI_LIGHT__
00055     if(lindex != -1) {
00056         /* sample position on a specified light */
00057         light_select(kg, lindex, randu, randv, sd->P, &ls);
00058     }
00059     else
00060 #endif
00061     {
00062         /* sample a light and position on int */
00063         light_sample(kg, randt, randu, randv, sd->P, &ls);
00064     }
00065 
00066     /* compute pdf */
00067     float pdf = light_sample_pdf(kg, &ls, -ls.D, ls.t);
00068 
00069     /* evaluate closure */
00070     *eval = direct_emissive_eval(kg, rando, &ls, randu, randv, -ls.D);
00071 
00072     if(is_zero(*eval) || pdf == 0.0f)
00073         return false;
00074 
00075     /* todo: use visbility flag to skip lights */
00076 
00077     /* evaluate BSDF at shading point */
00078     float bsdf_pdf;
00079     float3 bsdf_eval = shader_bsdf_eval(kg, sd, ls.D, &bsdf_pdf);
00080 
00081     *eval *= bsdf_eval/pdf;
00082 
00083     if(is_zero(*eval))
00084         return false;
00085 
00086     if(ls.prim != ~0) {
00087         /* multiple importance sampling */
00088         float mis_weight = power_heuristic(pdf, bsdf_pdf);
00089         *eval *= mis_weight;
00090     }
00091     /* todo: clean up these weights */
00092     else if(ls.shader & SHADER_AREA_LIGHT)
00093         *eval *= 0.25f; /* area lamp */
00094     else if(ls.t != FLT_MAX)
00095         *eval *= 0.25f*M_1_PI_F; /* point lamp */
00096 
00097     if(ls.shader & SHADER_CAST_SHADOW) {
00098         /* setup ray */
00099         ray->P = ray_offset(sd->P, sd->Ng);
00100 
00101         if(ls.t == FLT_MAX) {
00102             /* distant light */
00103             ray->D = ls.D;
00104             ray->t = ls.t;
00105         }
00106         else {
00107             /* other lights, avoid self-intersection */
00108             ray->D = ray_offset(ls.P, ls.Ng) - ray->P;
00109             ray->D = normalize_len(ray->D, &ray->t);
00110         }
00111     }
00112     else {
00113         /* signal to not cast shadow ray */
00114         ray->t = 0.0f;
00115     }
00116 
00117     return true;
00118 }
00119 
00120 /* Indirect Emission */
00121 
00122 __device float3 indirect_emission(KernelGlobals *kg, ShaderData *sd, float t, int path_flag, float bsdf_pdf)
00123 {
00124     /* evaluate emissive closure */
00125     float3 L = shader_emissive_eval(kg, sd);
00126 
00127     if(!(path_flag & PATH_RAY_MIS_SKIP) && (sd->flag & SD_SAMPLE_AS_LIGHT)) {
00128         /* multiple importance sampling */
00129         float pdf = triangle_light_pdf(kg, sd->Ng, sd->I, t);
00130         float mis_weight = power_heuristic(bsdf_pdf, pdf);
00131 
00132         return L*mis_weight;
00133     }
00134 
00135     return L;
00136 }
00137 
00138 CCL_NAMESPACE_END
00139