svn merge -r 23207:23528 https://svn.blender.org/svnroot/bf-blender/trunk/blender
[blender.git] / source / blender / render / intern / source / rayshade.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. 
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 1990-1998 NeoGeo BV.
21  * All rights reserved.
22  *
23  * Contributors: 2004/2005 Blender Foundation, full recode
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 #include <math.h>
29 #include <string.h>
30 #include <stdlib.h>
31 #include <float.h>
32 #include <assert.h>
33
34 #include "MEM_guardedalloc.h"
35
36 #include "DNA_material_types.h"
37 #include "DNA_lamp_types.h"
38
39 #include "BKE_global.h"
40 #include "BKE_node.h"
41 #include "BKE_utildefines.h"
42
43 #include "BLI_arithb.h"
44 #include "BLI_blenlib.h"
45 #include "BLI_jitter.h"
46 #include "BLI_rand.h"
47
48 #include "PIL_time.h"
49
50 #include "render_types.h"
51 #include "renderpipeline.h"
52 #include "rendercore.h"
53 #include "renderdatabase.h"
54 #include "pixelblending.h"
55 #include "pixelshading.h"
56 #include "shading.h"
57 #include "texture.h"
58 #include "volumetric.h"
59
60 #include "RE_raytrace.h"
61 #include "rayobject.h"
62 #include "raycounter.h"
63
64
65 #define USE_VLAK_PRIMITIVES 1
66 #define RAY_TRA         1
67 #define RAY_TRAFLIP     2
68
69 #define DEPTH_SHADOW_TRA  10
70
71 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
72 /* defined in pipeline.c, is hardcopy of active dynamic allocated Render */
73 /* only to be used here in this file, it's for speed */
74 extern struct Render R;
75 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
76 RayObject *  RE_rayobject_create(int type, int size)
77 {
78         if(type == R_RAYSTRUCTURE_AUTO)
79         {
80                 //TODO
81 //              if(detect_simd())
82 //                      type = R_RAYSTRUCTURE_SIMD_SVBVH;
83 //              else
84 //                      type = R_RAYSTRUCTURE_VBVH;
85
86                         type = R_RAYSTRUCTURE_SIMD_QBVH;
87         }
88         
89         
90         if(type == R_RAYSTRUCTURE_OCTREE)
91         {
92                 //TODO dynamic ocres
93                 return RE_rayobject_octree_create(R.r.ocres, size);
94         }
95         if(type == R_RAYSTRUCTURE_BLIBVH)
96         {
97                 return RE_rayobject_blibvh_create(size);
98         }
99         if(type == R_RAYSTRUCTURE_VBVH)
100         {
101                 return RE_rayobject_vbvh_create(size);
102         }
103         if(type == R_RAYSTRUCTURE_SIMD_SVBVH)
104         {
105                 return RE_rayobject_svbvh_create(size);
106         }
107         if(type == R_RAYSTRUCTURE_SIMD_QBVH)
108         {
109                 return RE_rayobject_qbvh_create(size);
110         }
111         if(type == R_RAYSTRUCTURE_BIH)
112         {
113 //              return RE_rayobject_bih_create(size);
114         }
115         
116         return NULL;
117 }
118
119 #ifdef RE_RAYCOUNTER
120 RayCounter re_rc_counter[BLENDER_MAX_THREADS] = {};
121 #endif
122
123
124 void freeraytree(Render *re)
125 {
126         ObjectInstanceRen *obi;
127         
128         if(re->raytree)
129         {
130                 RE_rayobject_free(re->raytree);
131                 re->raytree = NULL;
132         }
133         if(re->rayfaces)
134         {
135                 MEM_freeN(re->rayfaces);
136                 re->rayfaces = NULL;
137         }
138         if(re->rayprimitives)
139         {
140                 MEM_freeN(re->rayprimitives);
141                 re->rayprimitives = NULL;
142         }
143
144         for(obi=re->instancetable.first; obi; obi=obi->next)
145         {
146                 ObjectRen *obr = obi->obr;
147                 if(obr->raytree)
148                 {
149                         RE_rayobject_free(obr->raytree);
150                         obr->raytree = NULL;
151                 }
152                 if(obr->rayfaces)
153                 {
154                         MEM_freeN(obr->rayfaces);
155                         obr->rayfaces = NULL;
156                 }
157                 if(obi->raytree)
158                 {
159                         RE_rayobject_free(obi->raytree);
160                         obi->raytree = NULL;
161                 }
162         }
163         
164 #ifdef RE_RAYCOUNTER
165         {
166                 RayCounter sum = {};
167                 int i;
168                 for(i=0; i<BLENDER_MAX_THREADS; i++)
169                         RE_RC_MERGE(&sum, re_rc_counter+i);
170                 RE_RC_INFO(&sum);
171         }
172 #endif
173 }
174
175 static int is_raytraceable_vlr(Render *re, VlakRen *vlr)
176 {
177         if((re->flag & R_BAKE_TRACE) || (vlr->mat->mode & MA_TRACEBLE))
178         if(vlr->mat->material_type != MA_TYPE_WIRE)
179                 return 1;
180         return 0;
181 }
182
183 static int is_raytraceable(Render *re, ObjectInstanceRen *obi)
184 {
185         int v;
186         ObjectRen *obr = obi->obr;
187
188         if(re->excludeob && obr->ob == re->excludeob)
189                 return 0;
190
191         for(v=0;v<obr->totvlak;v++)
192         {
193                 VlakRen *vlr = obr->vlaknodes[v>>8].vlak + (v&255);
194                 if(is_raytraceable_vlr(re, vlr))
195                         return 1;
196         }
197         return 0;
198 }
199
200
201 RayObject* makeraytree_object(Render *re, ObjectInstanceRen *obi)
202 {
203         //TODO
204         // out-of-memory safeproof
205         // break render
206         // update render stats
207         ObjectRen *obr = obi->obr;
208         
209         if(obr->raytree == NULL)
210         {
211                 RayObject *raytree;
212                 RayFace *face;
213                 VlakPrimitive *vlakprimitive;
214                 int v;
215                 
216                 //Count faces
217                 int faces = 0;
218                 for(v=0;v<obr->totvlak;v++)
219                 {
220                         VlakRen *vlr = obr->vlaknodes[v>>8].vlak + (v&255);
221                         if(is_raytraceable_vlr(re, vlr))
222                                 faces++;
223                 }
224                 assert( faces > 0 );
225
226                 //Create Ray cast accelaration structure                
227                 raytree = obr->raytree = RE_rayobject_create( re->r.raytrace_tree_type, faces );
228                 if(USE_VLAK_PRIMITIVES)
229                         vlakprimitive = obr->rayprimitives = (VlakPrimitive*)MEM_callocN(faces*sizeof(VlakPrimitive), "ObjectRen primitives");
230                 else
231                         face = obr->rayfaces = (RayFace*)MEM_callocN(faces*sizeof(RayFace), "ObjectRen faces");
232
233                 obr->rayobi = obi;
234                 
235                 for(v=0;v<obr->totvlak;v++)
236                 {
237                         VlakRen *vlr = obr->vlaknodes[v>>8].vlak + (v&255);
238                         if(is_raytraceable_vlr(re, vlr))
239                         {
240                                 if(USE_VLAK_PRIMITIVES)
241                                 {
242                                         RE_rayobject_add( raytree, RE_vlakprimitive_from_vlak( vlakprimitive, obi, vlr ) );
243                                         vlakprimitive++;
244                                 }
245                                 else
246                                 {
247                                         RE_rayface_from_vlak( face, obi, vlr );                         
248                                         RE_rayobject_add( raytree, RE_rayobject_unalignRayFace(face) );
249                                         face++;
250                                 }
251                         }
252                 }
253                 RE_rayobject_done( raytree );
254         }
255
256
257         if((obi->flag & R_TRANSFORMED) && obi->raytree == NULL)
258         {
259                 obi->transform_primitives = 0;
260                 obi->raytree = RE_rayobject_instance_create( obr->raytree, obi->mat, obi, obi->obr->rayobi );
261         }
262         
263         if(obi->raytree) return obi->raytree;
264         return obi->obr->raytree;
265 }
266
267 static int has_special_rayobject(Render *re, ObjectInstanceRen *obi)
268 {
269         if( (obi->flag & R_TRANSFORMED) )
270         {
271                 ObjectRen *obr = obi->obr;
272                 int v, faces = 0;
273                 
274                 for(v=0;v<obr->totvlak;v++)
275                 {
276                         VlakRen *vlr = obr->vlaknodes[v>>8].vlak + (v&255);
277                         if(is_raytraceable_vlr(re, vlr))
278                         {
279                                 faces++;
280                                 if(faces > 4)
281                                         return 1;
282                         }
283                 }
284         }
285         return 0;
286 }
287 /*
288  * create a single raytrace structure with all faces
289  */
290 static void makeraytree_single(Render *re)
291 {
292         ObjectInstanceRen *obi;
293         RayObject *raytree;
294         RayFace *face;
295         VlakPrimitive *vlakprimitive;
296         int faces = 0, obs = 0, special = 0;
297
298         for(obi=re->instancetable.first; obi; obi=obi->next)
299         if(is_raytraceable(re, obi))
300         {
301                 int v;
302                 ObjectRen *obr = obi->obr;
303                 obs++;
304                 
305                 if(has_special_rayobject(re, obi))
306                 {
307                         special++;
308                 }
309                 else
310                 {
311                         for(v=0;v<obr->totvlak;v++)
312                         {
313                                 VlakRen *vlr = obr->vlaknodes[v>>8].vlak + (v&255);
314                                 if(is_raytraceable_vlr(re, vlr))
315                                         faces++;
316                         }
317                 }
318         }
319         
320         //Create raytree
321         raytree = re->raytree = RE_rayobject_create( re->r.raytrace_tree_type, faces+special );
322
323         if(USE_VLAK_PRIMITIVES)
324         {
325                 vlakprimitive = re->rayprimitives = (VlakPrimitive*)MEM_callocN(faces*sizeof(VlakPrimitive), "Raytrace vlak-primitives");
326         }
327         else
328         {
329                 face = re->rayfaces     = (RayFace*)MEM_callocN(faces*sizeof(RayFace), "Render ray faces");
330         }
331         
332         for(obi=re->instancetable.first; obi; obi=obi->next)
333         if(is_raytraceable(re, obi))
334         {
335                 if(has_special_rayobject(re, obi))
336                 {
337                         RayObject *obj = makeraytree_object(re, obi);
338                         RE_rayobject_add( re->raytree, obj );
339                 }
340                 else
341                 {
342                         int v;
343                         ObjectRen *obr = obi->obr;
344                         
345                         if(obi->flag & R_TRANSFORMED)
346                         {
347                                 obi->transform_primitives = 1;
348                         }
349
350                         for(v=0;v<obr->totvlak;v++)
351                         {
352                                 VlakRen *vlr = obr->vlaknodes[v>>8].vlak + (v&255);
353                                 if(is_raytraceable_vlr(re, vlr))
354                                 {
355                                         if(USE_VLAK_PRIMITIVES)
356                                         {
357                                                 RayObject *obj = RE_vlakprimitive_from_vlak( vlakprimitive, obi, vlr );
358                                                 RE_rayobject_add( raytree, obj );
359                                                 vlakprimitive++;
360                                         }
361                                         else
362                                         {
363                                                 RE_rayface_from_vlak(face, obi, vlr);
364                                                 if((obi->flag & R_TRANSFORMED))
365                                                 {
366                                                         Mat4MulVecfl(obi->mat, face->v1);
367                                                         Mat4MulVecfl(obi->mat, face->v2);
368                                                         Mat4MulVecfl(obi->mat, face->v3);
369                                                         if(RE_rayface_isQuad(face))
370                                                                 Mat4MulVecfl(obi->mat, face->v4);
371                                                 }
372
373                                                 RE_rayobject_add( raytree, RE_rayobject_unalignRayFace(face) );
374                                                 face++;
375                                         }
376                                 }
377                         }
378                 }
379         }
380         
381         re->i.infostr= "Raytree.. building";
382         re->stats_draw(re->sdh, &re->i);
383
384         RE_rayobject_done( raytree );   
385 }
386
387 void makeraytree(Render *re)
388 {
389         float min[3], max[3], sub[3];
390         int i;
391         
392         re->i.infostr= "Raytree.. preparing";
393         re->stats_draw(re->sdh, &re->i);
394
395         BENCH(makeraytree_single(re), tree_build);
396                 
397         //Calculate raytree max_size
398         //This is ONLY needed to kept a bogus behaviour of SUN and HEMI lights
399         RE_rayobject_merge_bb( re->raytree, min, max );
400         for(i=0; i<3; i++)
401         {
402                 min[i] += 0.01f;
403                 max[i] += 0.01f;
404                 sub[i] = max[i]-min[i];
405         }
406         re->maxdist = sqrt( sub[0]*sub[0] + sub[1]*sub[1] + sub[2]*sub[2] );
407
408         re->i.infostr= "Raytree finished";
409         re->stats_draw(re->sdh, &re->i);
410 }
411
412 void shade_ray(Isect *is, ShadeInput *shi, ShadeResult *shr)
413 {
414         ObjectInstanceRen *obi= (ObjectInstanceRen*)is->hit.ob;
415         VlakRen *vlr= (VlakRen*)is->hit.face;
416         int osatex= 0;
417         
418         /* set up view vector */
419         VECCOPY(shi->view, is->vec);
420
421         /* render co */
422         shi->co[0]= is->start[0]+is->labda*(shi->view[0]);
423         shi->co[1]= is->start[1]+is->labda*(shi->view[1]);
424         shi->co[2]= is->start[2]+is->labda*(shi->view[2]);
425         
426         Normalize(shi->view);
427
428         shi->obi= obi;
429         shi->obr= obi->obr;
430         shi->vlr= vlr;
431         shi->mat= vlr->mat;
432         shade_input_init_material(shi);
433         
434         // Osa structs we leave unchanged now
435         SWAP(int, osatex, shi->osatex);
436         
437         shi->dxco[0]= shi->dxco[1]= shi->dxco[2]= 0.0f;
438         shi->dyco[0]= shi->dyco[1]= shi->dyco[2]= 0.0f;
439         
440         // but, set Osa stuff to zero where it can confuse texture code
441         if(shi->mat->texco & (TEXCO_NORM|TEXCO_REFL) ) {
442                 shi->dxno[0]= shi->dxno[1]= shi->dxno[2]= 0.0f;
443                 shi->dyno[0]= shi->dyno[1]= shi->dyno[2]= 0.0f;
444         }
445
446         if(vlr->v4) {
447                 if(is->isect==2) 
448                         shade_input_set_triangle_i(shi, obi, vlr, 2, 1, 3);
449                 else
450                         shade_input_set_triangle_i(shi, obi, vlr, 0, 1, 3);
451         }
452         else {
453                 shade_input_set_triangle_i(shi, obi, vlr, 0, 1, 2);
454         }
455
456         shi->u= is->u;
457         shi->v= is->v;
458         shi->dx_u= shi->dx_v= shi->dy_u= shi->dy_v=  0.0f;
459
460         shade_input_set_normals(shi);
461
462         /* point normals to viewing direction */
463         if(INPR(shi->facenor, shi->view) < 0.0f)
464                 shade_input_flip_normals(shi);
465
466         shade_input_set_shade_texco(shi);
467         if (shi->mat->material_type == MA_TYPE_VOLUME) {
468                 if(ELEM(is->mode, RE_RAY_SHADOW, RE_RAY_SHADOW_TRA)) {
469                         shade_volume_shadow(shi, shr, is);
470                 } else {
471                         shade_volume_outside(shi, shr);
472                 }
473         }
474         else if(is->mode==RE_RAY_SHADOW_TRA) {
475                 /* temp hack to prevent recursion */
476                 if(shi->nodes==0 && shi->mat->nodetree && shi->mat->use_nodes) {
477                         ntreeShaderExecTree(shi->mat->nodetree, shi, shr);
478                         shi->mat= vlr->mat;             /* shi->mat is being set in nodetree */
479                 }
480                 else
481                         shade_color(shi, shr);
482         }
483         else {
484                 if(shi->mat->nodetree && shi->mat->use_nodes) {
485                         ntreeShaderExecTree(shi->mat->nodetree, shi, shr);
486                         shi->mat= vlr->mat;             /* shi->mat is being set in nodetree */
487                 }
488                 else {
489                         int tempdepth;
490                         /* XXX dodgy business here, set ray depth to -1
491                          * to ignore raytrace in shade_material_loop()
492                          * this could really use a refactor --Matt */
493                         if (shi->volume_depth == 0) {
494                                 tempdepth = shi->depth;
495                                 shi->depth = -1;
496                                 shade_material_loop(shi, shr);
497                                 shi->depth = tempdepth;
498                         } else {
499                                 shade_material_loop(shi, shr);
500                         }
501                 }
502                 /* raytrace likes to separate the spec color */
503                 VECSUB(shr->diff, shr->combined, shr->spec);
504         }       
505         
506         SWAP(int, osatex, shi->osatex);  // XXXXX!!!!
507
508 }
509
510 static int refraction(float *refract, float *n, float *view, float index)
511 {
512         float dot, fac;
513
514         VECCOPY(refract, view);
515         
516         dot= view[0]*n[0] + view[1]*n[1] + view[2]*n[2];
517
518         if(dot>0.0f) {
519                 index = 1.0f/index;
520                 fac= 1.0f - (1.0f - dot*dot)*index*index;
521                 if(fac<= 0.0f) return 0;
522                 fac= -dot*index + sqrt(fac);
523         }
524         else {
525                 fac= 1.0f - (1.0f - dot*dot)*index*index;
526                 if(fac<= 0.0f) return 0;
527                 fac= -dot*index - sqrt(fac);
528         }
529
530         refract[0]= index*view[0] + fac*n[0];
531         refract[1]= index*view[1] + fac*n[1];
532         refract[2]= index*view[2] + fac*n[2];
533
534         return 1;
535 }
536
537 /* orn = original face normal */
538 static void reflection(float *ref, float *n, float *view, float *orn)
539 {
540         float f1;
541         
542         f1= -2.0f*(n[0]*view[0]+ n[1]*view[1]+ n[2]*view[2]);
543         
544         ref[0]= (view[0]+f1*n[0]);
545         ref[1]= (view[1]+f1*n[1]);
546         ref[2]= (view[2]+f1*n[2]);
547
548         if(orn) {
549                 /* test phong normals, then we should prevent vector going to the back */
550                 f1= ref[0]*orn[0]+ ref[1]*orn[1]+ ref[2]*orn[2];
551                 if(f1>0.0f) {
552                         f1+= .01f;
553                         ref[0]-= f1*orn[0];
554                         ref[1]-= f1*orn[1];
555                         ref[2]-= f1*orn[2];
556                 }
557         }
558 }
559
560 #if 0
561 static void color_combine(float *result, float fac1, float fac2, float *col1, float *col2)
562 {
563         float col1t[3], col2t[3];
564         
565         col1t[0]= sqrt(col1[0]);
566         col1t[1]= sqrt(col1[1]);
567         col1t[2]= sqrt(col1[2]);
568         col2t[0]= sqrt(col2[0]);
569         col2t[1]= sqrt(col2[1]);
570         col2t[2]= sqrt(col2[2]);
571
572         result[0]= (fac1*col1t[0] + fac2*col2t[0]);
573         result[0]*= result[0];
574         result[1]= (fac1*col1t[1] + fac2*col2t[1]);
575         result[1]*= result[1];
576         result[2]= (fac1*col1t[2] + fac2*col2t[2]);
577         result[2]*= result[2];
578 }
579 #endif
580
581 static float shade_by_transmission(Isect *is, ShadeInput *shi, ShadeResult *shr)
582 {
583         float dx, dy, dz, d, p;
584
585         if (0 == (shi->mat->mode & MA_TRANSP))
586                 return -1;
587            
588         if (shi->mat->tx_limit <= 0.0f) {
589                 d= 1.0f;
590         } 
591         else {
592                 /* shi.co[] calculated by shade_ray() */
593                 dx= shi->co[0] - is->start[0];
594                 dy= shi->co[1] - is->start[1];
595                 dz= shi->co[2] - is->start[2];
596                 d= sqrt(dx*dx+dy*dy+dz*dz);
597                 if (d > shi->mat->tx_limit)
598                         d= shi->mat->tx_limit;
599
600                 p = shi->mat->tx_falloff;
601                 if(p < 0.0f) p= 0.0f;
602                 else if (p > 10.0f) p= 10.0f;
603
604                 shr->alpha *= pow(d, p);
605                 if (shr->alpha > 1.0f)
606                         shr->alpha= 1.0f;
607         }
608
609         return d;
610 }
611
612 static void ray_fadeout_endcolor(float *col, ShadeInput *origshi, ShadeInput *shi, ShadeResult *shr, Isect *isec, float *vec)
613 {
614         /* un-intersected rays get either rendered material color or sky color */
615         if (origshi->mat->fadeto_mir == MA_RAYMIR_FADETOMAT) {
616                 VECCOPY(col, shr->combined);
617         } else if (origshi->mat->fadeto_mir == MA_RAYMIR_FADETOSKY) {
618                 VECCOPY(shi->view, vec);
619                 Normalize(shi->view);
620                 
621                 shadeSkyView(col, isec->start, shi->view, NULL, shi->thread);
622                 shadeSunView(col, shi->view);
623         }
624 }
625
626 static void ray_fadeout(Isect *is, ShadeInput *shi, float *col, float *blendcol, float dist_mir)
627 {
628         /* if fading out, linear blend against fade color */
629         float blendfac;
630
631         blendfac = 1.0 - VecLenf(shi->co, is->start)/dist_mir;
632         
633         col[0] = col[0]*blendfac + (1.0 - blendfac)*blendcol[0];
634         col[1] = col[1]*blendfac + (1.0 - blendfac)*blendcol[1];
635         col[2] = col[2]*blendfac + (1.0 - blendfac)*blendcol[2];
636 }
637
638 /* the main recursive tracer itself */
639 static void traceray(ShadeInput *origshi, ShadeResult *origshr, short depth, float *start, float *vec, float *col, ObjectInstanceRen *obi, VlakRen *vlr, int traflag)
640 {
641         ShadeInput shi;
642         ShadeResult shr;
643         Isect isec;
644         float f, f1, fr, fg, fb;
645         float ref[3];
646         float dist_mir = origshi->mat->dist_mir;
647
648         /* Warning, This is not that nice, and possibly a bit slow for every ray,
649         however some variables were not initialized properly in, unless using shade_input_initialize(...), we need to do a memset */
650         memset(&shi, 0, sizeof(ShadeInput)); 
651         /* end warning! - Campbell */
652         
653         VECCOPY(isec.start, start);
654         VECCOPY(isec.vec, vec );
655         isec.labda = dist_mir > 0 ? dist_mir : RE_RAYTRACE_MAXDIST;
656         isec.mode= RE_RAY_MIRROR;
657         isec.skip = RE_SKIP_VLR_NEIGHBOUR | RE_SKIP_VLR_RENDER_CHECK;
658         isec.hint = 0;
659
660         isec.orig.ob   = obi;
661         isec.orig.face = vlr;
662         RE_RC_INIT(isec, shi);
663
664         if(RE_rayobject_raycast(R.raytree, &isec)) {
665                 float d= 1.0f;
666                 
667                 shi.mask= origshi->mask;
668                 shi.osatex= origshi->osatex;
669                 shi.depth= 1;                                   /* only used to indicate tracing */
670                 shi.thread= origshi->thread;
671                 //shi.sample= 0; // memset above, so dont need this
672                 shi.xs= origshi->xs;
673                 shi.ys= origshi->ys;
674                 shi.lay= origshi->lay;
675                 shi.passflag= SCE_PASS_COMBINED; /* result of tracing needs no pass info */
676                 shi.combinedflag= 0xFFFFFF;              /* ray trace does all options */
677                 //shi.do_preview= 0; // memset above, so dont need this
678                 shi.light_override= origshi->light_override;
679                 shi.mat_override= origshi->mat_override;
680                 
681                 memset(&shr, 0, sizeof(ShadeResult));
682                 
683                 shade_ray(&isec, &shi, &shr);
684                 if (traflag & RAY_TRA)
685                         d= shade_by_transmission(&isec, &shi, &shr);
686                 
687                 if(depth>0) {
688
689                         if((shi.mat->mode_l & MA_TRANSP) && shr.alpha < 1.0f) {
690                                 float nf, f, f1, refract[3], tracol[4];
691                                 
692                                 tracol[0]= shi.r;
693                                 tracol[1]= shi.g;
694                                 tracol[2]= shi.b;
695                                 tracol[3]= col[3];      // we pass on and accumulate alpha
696                                 
697                                 if((shi.mat->mode & MA_TRANSP) && (shi.mat->mode & MA_RAYTRANSP)) {
698                                         /* odd depths: use normal facing viewer, otherwise flip */
699                                         if(traflag & RAY_TRAFLIP) {
700                                                 float norm[3];
701                                                 norm[0]= - shi.vn[0];
702                                                 norm[1]= - shi.vn[1];
703                                                 norm[2]= - shi.vn[2];
704                                                 if (!refraction(refract, norm, shi.view, shi.ang))
705                                                         reflection(refract, norm, shi.view, shi.vn);
706                                         }
707                                         else {
708                                                 if (!refraction(refract, shi.vn, shi.view, shi.ang))
709                                                         reflection(refract, shi.vn, shi.view, shi.vn);
710                                         }
711                                         traflag |= RAY_TRA;
712                                         traceray(origshi, origshr, depth-1, shi.co, refract, tracol, shi.obi, shi.vlr, traflag ^ RAY_TRAFLIP);
713                                 }
714                                 else
715                                         traceray(origshi, origshr, depth-1, shi.co, shi.view, tracol, shi.obi, shi.vlr, 0);
716                                 
717                                 f= shr.alpha; f1= 1.0f-f;
718                                 nf= d * shi.mat->filter;
719                                 fr= 1.0f+ nf*(shi.r-1.0f);
720                                 fg= 1.0f+ nf*(shi.g-1.0f);
721                                 fb= 1.0f+ nf*(shi.b-1.0f);
722                                 shr.diff[0]= f*shr.diff[0] + f1*fr*tracol[0];
723                                 shr.diff[1]= f*shr.diff[1] + f1*fg*tracol[1];
724                                 shr.diff[2]= f*shr.diff[2] + f1*fb*tracol[2];
725                                 
726                                 shr.spec[0] *=f;
727                                 shr.spec[1] *=f;
728                                 shr.spec[2] *=f;
729
730                                 col[3]= f1*tracol[3] + f;
731                         }
732                         else 
733                                 col[3]= 1.0f;
734
735                         if(shi.mat->mode_l & MA_RAYMIRROR) {
736                                 f= shi.ray_mirror;
737                                 if(f!=0.0f) f*= fresnel_fac(shi.view, shi.vn, shi.mat->fresnel_mir_i, shi.mat->fresnel_mir);
738                         }
739                         else f= 0.0f;
740                         
741                         if(f!=0.0f) {
742                                 float mircol[4];
743                                 
744                                 reflection(ref, shi.vn, shi.view, NULL);                        
745                                 traceray(origshi, origshr, depth-1, shi.co, ref, mircol, shi.obi, shi.vlr, 0);
746                         
747                                 f1= 1.0f-f;
748
749                                 /* combine */
750                                 //color_combine(col, f*fr*(1.0f-shr.spec[0]), f1, col, shr.diff);
751                                 //col[0]+= shr.spec[0];
752                                 //col[1]+= shr.spec[1];
753                                 //col[2]+= shr.spec[2];
754                                 
755                                 fr= shi.mirr;
756                                 fg= shi.mirg;
757                                 fb= shi.mirb;
758                 
759                                 col[0]= f*fr*(1.0f-shr.spec[0])*mircol[0] + f1*shr.diff[0] + shr.spec[0];
760                                 col[1]= f*fg*(1.0f-shr.spec[1])*mircol[1] + f1*shr.diff[1] + shr.spec[1];
761                                 col[2]= f*fb*(1.0f-shr.spec[2])*mircol[2] + f1*shr.diff[2] + shr.spec[2];
762                         }
763                         else {
764                                 col[0]= shr.diff[0] + shr.spec[0];
765                                 col[1]= shr.diff[1] + shr.spec[1];
766                                 col[2]= shr.diff[2] + shr.spec[2];
767                         }
768                         
769                         if (dist_mir > 0.0) {
770                                 float blendcol[3];
771                                 
772                                 /* max ray distance set, but found an intersection, so fade this color
773                                  * out towards the sky/material color for a smooth transition */
774                                 ray_fadeout_endcolor(blendcol, origshi, &shi, origshr, &isec, vec);
775                                 ray_fadeout(&isec, &shi, col, blendcol, dist_mir);
776                         }
777                 }
778                 else {
779                         col[0]= shr.diff[0] + shr.spec[0];
780                         col[1]= shr.diff[1] + shr.spec[1];
781                         col[2]= shr.diff[2] + shr.spec[2];
782                 }
783                 
784         }
785         else {
786                 ray_fadeout_endcolor(col, origshi, &shi, origshr, &isec, vec);
787         }
788         RE_RC_MERGE(&origshi->raycounter, &shi.raycounter);
789 }
790
791 /* **************** jitter blocks ********** */
792
793 /* calc distributed planar energy */
794
795 static void DP_energy(float *table, float *vec, int tot, float xsize, float ysize)
796 {
797         int x, y, a;
798         float *fp, force[3], result[3];
799         float dx, dy, dist, min;
800         
801         min= MIN2(xsize, ysize);
802         min*= min;
803         result[0]= result[1]= 0.0f;
804         
805         for(y= -1; y<2; y++) {
806                 dy= ysize*y;
807                 for(x= -1; x<2; x++) {
808                         dx= xsize*x;
809                         fp= table;
810                         for(a=0; a<tot; a++, fp+= 2) {
811                                 force[0]= vec[0] - fp[0]-dx;
812                                 force[1]= vec[1] - fp[1]-dy;
813                                 dist= force[0]*force[0] + force[1]*force[1];
814                                 if(dist < min && dist>0.0f) {
815                                         result[0]+= force[0]/dist;
816                                         result[1]+= force[1]/dist;
817                                 }
818                         }
819                 }
820         }
821         vec[0] += 0.1*min*result[0]/(float)tot;
822         vec[1] += 0.1*min*result[1]/(float)tot;
823         // cyclic clamping
824         vec[0]= vec[0] - xsize*floor(vec[0]/xsize + 0.5);
825         vec[1]= vec[1] - ysize*floor(vec[1]/ysize + 0.5);
826 }
827
828 // random offset of 1 in 2
829 static void jitter_plane_offset(float *jitter1, float *jitter2, int tot, float sizex, float sizey, float ofsx, float ofsy)
830 {
831         float dsizex= sizex*ofsx;
832         float dsizey= sizey*ofsy;
833         float hsizex= 0.5*sizex, hsizey= 0.5*sizey;
834         int x;
835         
836         for(x=tot; x>0; x--, jitter1+=2, jitter2+=2) {
837                 jitter2[0]= jitter1[0] + dsizex;
838                 jitter2[1]= jitter1[1] + dsizey;
839                 if(jitter2[0] > hsizex) jitter2[0]-= sizex;
840                 if(jitter2[1] > hsizey) jitter2[1]-= sizey;
841         }
842 }
843
844 /* called from convertBlenderScene.c */
845 /* we do this in advance to get consistant random, not alter the render seed, and be threadsafe */
846 void init_jitter_plane(LampRen *lar)
847 {
848         float *fp;
849         int x, iter=12, tot= lar->ray_totsamp;
850         
851         /* test if already initialized */
852         if(lar->jitter) return;
853         
854         /* at least 4, or max threads+1 tables */
855         if(BLENDER_MAX_THREADS < 4) x= 4;
856         else x= BLENDER_MAX_THREADS+1;
857         fp= lar->jitter= MEM_callocN(x*tot*2*sizeof(float), "lamp jitter tab");
858         
859         /* if 1 sample, we leave table to be zero's */
860         if(tot>1) {
861                 
862                 /* set per-lamp fixed seed */
863                 BLI_srandom(tot);
864                 
865                 /* fill table with random locations, area_size large */
866                 for(x=0; x<tot; x++, fp+=2) {
867                         fp[0]= (BLI_frand()-0.5)*lar->area_size;
868                         fp[1]= (BLI_frand()-0.5)*lar->area_sizey;
869                 }
870                 
871                 while(iter--) {
872                         fp= lar->jitter;
873                         for(x=tot; x>0; x--, fp+=2) {
874                                 DP_energy(lar->jitter, fp, tot, lar->area_size, lar->area_sizey);
875                         }
876                 }
877         }       
878         /* create the dithered tables (could just check lamp type!) */
879         jitter_plane_offset(lar->jitter, lar->jitter+2*tot, tot, lar->area_size, lar->area_sizey, 0.5f, 0.0f);
880         jitter_plane_offset(lar->jitter, lar->jitter+4*tot, tot, lar->area_size, lar->area_sizey, 0.5f, 0.5f);
881         jitter_plane_offset(lar->jitter, lar->jitter+6*tot, tot, lar->area_size, lar->area_sizey, 0.0f, 0.5f);
882 }
883
884 /* table around origin, -0.5*size to 0.5*size */
885 static float *give_jitter_plane(LampRen *lar, int thread, int xs, int ys)
886 {
887         int tot;
888         
889         tot= lar->ray_totsamp;
890                         
891         if(lar->ray_samp_type & LA_SAMP_JITTER) {
892                 /* made it threadsafe */
893                 
894                 if(lar->xold[thread]!=xs || lar->yold[thread]!=ys) {
895                         jitter_plane_offset(lar->jitter, lar->jitter+2*(thread+1)*tot, tot, lar->area_size, lar->area_sizey, BLI_thread_frand(thread), BLI_thread_frand(thread));
896                         lar->xold[thread]= xs; 
897                         lar->yold[thread]= ys;
898                 }
899                 return lar->jitter+2*(thread+1)*tot;
900         }
901         if(lar->ray_samp_type & LA_SAMP_DITHER) {
902                 return lar->jitter + 2*tot*((xs & 1)+2*(ys & 1));
903         }
904         
905         return lar->jitter;
906 }
907
908
909 /* **************** QMC sampling *************** */
910
911 static void halton_sample(double *ht_invprimes, double *ht_nums, double *v)
912 {
913         // incremental halton sequence generator, from:
914         // "Instant Radiosity", Keller A.
915         unsigned int i;
916         
917         for (i = 0; i < 2; i++)
918         {
919                 double r = fabs((1.0 - ht_nums[i]) - 1e-10);
920                 
921                 if (ht_invprimes[i] >= r)
922                 {
923                         double lasth;
924                         double h = ht_invprimes[i];
925                         
926                         do {
927                                 lasth = h;
928                                 h *= ht_invprimes[i];
929                         } while (h >= r);
930                         
931                         ht_nums[i] += ((lasth + h) - 1.0);
932                 }
933                 else
934                         ht_nums[i] += ht_invprimes[i];
935                 
936                 v[i] = (float)ht_nums[i];
937         }
938 }
939
940 /* Generate Hammersley points in [0,1)^2
941  * From Lucille renderer */
942 static void hammersley_create(double *out, int n)
943 {
944         double p, t;
945         int k, kk;
946
947         for (k = 0; k < n; k++) {
948                 t = 0;
949                 for (p = 0.5, kk = k; kk; p *= 0.5, kk >>= 1) {
950                         if (kk & 1) {           /* kk mod 2 = 1         */
951                                 t += p;
952                         }
953                 }
954         
955                 out[2 * k + 0] = (double)k / (double)n;
956                 out[2 * k + 1] = t;
957         }
958 }
959
960 static struct QMCSampler *QMC_initSampler(int type, int tot)
961 {       
962         QMCSampler *qsa = MEM_callocN(sizeof(QMCSampler), "qmc sampler");
963         qsa->samp2d = MEM_callocN(2*sizeof(double)*tot, "qmc sample table");
964
965         qsa->tot = tot;
966         qsa->type = type;
967         
968         if (qsa->type==SAMP_TYPE_HAMMERSLEY) 
969                 hammersley_create(qsa->samp2d, qsa->tot);
970                 
971         return qsa;
972 }
973
974 static void QMC_initPixel(QMCSampler *qsa, int thread)
975 {
976         if (qsa->type==SAMP_TYPE_HAMMERSLEY)
977         {
978                 /* hammersley sequence is fixed, already created in QMCSampler init.
979                  * per pixel, gets a random offset. We create separate offsets per thread, for write-safety */
980                 qsa->offs[thread][0] = 0.5 * BLI_thread_frand(thread);
981                 qsa->offs[thread][1] = 0.5 * BLI_thread_frand(thread);
982         }
983         else {  /* SAMP_TYPE_HALTON */
984                 
985                 /* generate a new randomised halton sequence per pixel
986                  * to alleviate qmc artifacts and make it reproducable 
987                  * between threads/frames */
988                 double ht_invprimes[2], ht_nums[2];
989                 double r[2];
990                 int i;
991         
992                 ht_nums[0] = BLI_thread_frand(thread);
993                 ht_nums[1] = BLI_thread_frand(thread);
994                 ht_invprimes[0] = 0.5;
995                 ht_invprimes[1] = 1.0/3.0;
996                 
997                 for (i=0; i< qsa->tot; i++) {
998                         halton_sample(ht_invprimes, ht_nums, r);
999                         qsa->samp2d[2*i+0] = r[0];
1000                         qsa->samp2d[2*i+1] = r[1];
1001                 }
1002         }
1003 }
1004
1005 static void QMC_freeSampler(QMCSampler *qsa)
1006 {
1007         MEM_freeN(qsa->samp2d);
1008         MEM_freeN(qsa);
1009 }
1010
1011 static void QMC_getSample(double *s, QMCSampler *qsa, int thread, int num)
1012 {
1013         if (qsa->type == SAMP_TYPE_HAMMERSLEY) {
1014                 s[0] = fmod(qsa->samp2d[2*num+0] + qsa->offs[thread][0], 1.0f);
1015                 s[1] = fmod(qsa->samp2d[2*num+1] + qsa->offs[thread][1], 1.0f);
1016         }
1017         else { /* SAMP_TYPE_HALTON */
1018                 s[0] = qsa->samp2d[2*num+0];
1019                 s[1] = qsa->samp2d[2*num+1];
1020         }
1021 }
1022
1023 /* phong weighted disc using 'blur' for exponent, centred on 0,0 */
1024 static void QMC_samplePhong(float *vec, QMCSampler *qsa, int thread, int num, float blur)
1025 {
1026         double s[2];
1027         float phi, pz, sqr;
1028         
1029         QMC_getSample(s, qsa, thread, num);
1030
1031         phi = s[0]*2*M_PI;
1032         pz = pow(s[1], blur);
1033         sqr = sqrt(1.0f-pz*pz);
1034
1035         vec[0] = cos(phi)*sqr;
1036         vec[1] = sin(phi)*sqr;
1037         vec[2] = 0.0f;
1038 }
1039
1040 /* rect of edge lengths sizex, sizey, centred on 0.0,0.0 i.e. ranging from -sizex/2 to +sizey/2 */
1041 static void QMC_sampleRect(float *vec, QMCSampler *qsa, int thread, int num, float sizex, float sizey)
1042 {
1043         double s[2];
1044
1045         QMC_getSample(s, qsa, thread, num);
1046                 
1047         vec[0] = (s[0] - 0.5) * sizex;
1048         vec[1] = (s[1] - 0.5) * sizey;
1049         vec[2] = 0.0f;
1050 }
1051
1052 /* disc of radius 'radius', centred on 0,0 */
1053 static void QMC_sampleDisc(float *vec, QMCSampler *qsa, int thread, int num, float radius)
1054 {
1055         double s[2];
1056         float phi, sqr;
1057         
1058         QMC_getSample(s, qsa, thread, num);
1059         
1060         phi = s[0]*2*M_PI;
1061         sqr = sqrt(s[1]);
1062
1063         vec[0] = cos(phi)*sqr* radius/2.0;
1064         vec[1] = sin(phi)*sqr* radius/2.0;
1065         vec[2] = 0.0f;
1066 }
1067
1068 /* uniform hemisphere sampling */
1069 static void QMC_sampleHemi(float *vec, QMCSampler *qsa, int thread, int num)
1070 {
1071         double s[2];
1072         float phi, sqr;
1073         
1074         QMC_getSample(s, qsa, thread, num);
1075         
1076         phi = s[0]*2.f*M_PI;    
1077         sqr = sqrt(s[1]);
1078
1079         vec[0] = cos(phi)*sqr;
1080         vec[1] = sin(phi)*sqr;
1081         vec[2] = 1.f - s[1]*s[1];
1082 }
1083
1084 #if 0 /* currently not used */
1085 /* cosine weighted hemisphere sampling */
1086 static void QMC_sampleHemiCosine(float *vec, QMCSampler *qsa, int thread, int num)
1087 {
1088         double s[2];
1089         float phi, sqr;
1090         
1091         QMC_getSample(s, qsa, thread, num);
1092         
1093         phi = s[0]*2.f*M_PI;    
1094         sqr = s[1]*sqrt(2-s[1]*s[1]);
1095
1096         vec[0] = cos(phi)*sqr;
1097         vec[1] = sin(phi)*sqr;
1098         vec[2] = 1.f - s[1]*s[1];
1099
1100 }
1101 #endif
1102
1103 /* called from convertBlenderScene.c */
1104 void init_render_qmcsampler(Render *re)
1105 {
1106         re->qmcsamplers= MEM_callocN(sizeof(ListBase)*BLENDER_MAX_THREADS, "QMCListBase");
1107 }
1108
1109 static QMCSampler *get_thread_qmcsampler(Render *re, int thread, int type, int tot)
1110 {
1111         QMCSampler *qsa;
1112
1113         /* create qmc samplers as needed, since recursion makes it hard to
1114          * predict how many are needed */
1115
1116         for(qsa=re->qmcsamplers[thread].first; qsa; qsa=qsa->next) {
1117                 if(qsa->type == type && qsa->tot == tot && !qsa->used) {
1118                         qsa->used= 1;
1119                         return qsa;
1120                 }
1121         }
1122
1123         qsa= QMC_initSampler(type, tot);
1124         qsa->used= 1;
1125         BLI_addtail(&re->qmcsamplers[thread], qsa);
1126
1127         return qsa;
1128 }
1129
1130 static void release_thread_qmcsampler(Render *re, int thread, QMCSampler *qsa)
1131 {
1132         qsa->used= 0;
1133 }
1134
1135 void free_render_qmcsampler(Render *re)
1136 {
1137         QMCSampler *qsa, *next;
1138         int a;
1139
1140         if(re->qmcsamplers) {
1141                 for(a=0; a<BLENDER_MAX_THREADS; a++) {
1142                         for(qsa=re->qmcsamplers[a].first; qsa; qsa=next) {
1143                                 next= qsa->next;
1144                                 QMC_freeSampler(qsa);
1145                         }
1146
1147                         re->qmcsamplers[a].first= re->qmcsamplers[a].last= NULL;
1148                 }
1149
1150                 MEM_freeN(re->qmcsamplers);
1151                 re->qmcsamplers= NULL;
1152         }
1153 }
1154
1155 static int adaptive_sample_variance(int samples, float *col, float *colsq, float thresh)
1156 {
1157         float var[3], mean[3];
1158
1159         /* scale threshold just to give a bit more precision in input rather than dealing with 
1160          * tiny tiny numbers in the UI */
1161         thresh /= 2;
1162         
1163         mean[0] = col[0] / (float)samples;
1164         mean[1] = col[1] / (float)samples;
1165         mean[2] = col[2] / (float)samples;
1166
1167         var[0] = (colsq[0] / (float)samples) - (mean[0]*mean[0]);
1168         var[1] = (colsq[1] / (float)samples) - (mean[1]*mean[1]);
1169         var[2] = (colsq[2] / (float)samples) - (mean[2]*mean[2]);
1170         
1171         if ((var[0] * 0.4 < thresh) && (var[1] * 0.3 < thresh) && (var[2] * 0.6 < thresh))
1172                 return 1;
1173         else
1174                 return 0;
1175 }
1176
1177 static int adaptive_sample_contrast_val(int samples, float prev, float val, float thresh)
1178 {
1179         /* if the last sample's contribution to the total value was below a small threshold
1180          * (i.e. the samples taken are very similar), then taking more samples that are probably 
1181          * going to be the same is wasting effort */
1182         if (fabs( prev/(float)(samples-1) - val/(float)samples ) < thresh) {
1183                 return 1;
1184         } else
1185                 return 0;
1186 }
1187
1188 static float get_avg_speed(ShadeInput *shi)
1189 {
1190         float pre_x, pre_y, post_x, post_y, speedavg;
1191         
1192         pre_x = (shi->winspeed[0] == PASS_VECTOR_MAX)?0.0:shi->winspeed[0];
1193         pre_y = (shi->winspeed[1] == PASS_VECTOR_MAX)?0.0:shi->winspeed[1];
1194         post_x = (shi->winspeed[2] == PASS_VECTOR_MAX)?0.0:shi->winspeed[2];
1195         post_y = (shi->winspeed[3] == PASS_VECTOR_MAX)?0.0:shi->winspeed[3];
1196         
1197         speedavg = (sqrt(pre_x*pre_x + pre_y*pre_y) + sqrt(post_x*post_x + post_y*post_y)) / 2.0;
1198         
1199         return speedavg;
1200 }
1201
1202 /* ***************** main calls ************** */
1203
1204
1205 static void trace_refract(float *col, ShadeInput *shi, ShadeResult *shr)
1206 {
1207         QMCSampler *qsa=NULL;
1208         int samp_type;
1209         
1210         float samp3d[3], orthx[3], orthy[3];
1211         float v_refract[3], v_refract_new[3];
1212         float sampcol[4], colsq[4];
1213         
1214         float blur = pow(1.0 - shi->mat->gloss_tra, 3);
1215         short max_samples = shi->mat->samp_gloss_tra;
1216         float adapt_thresh = shi->mat->adapt_thresh_tra;
1217         
1218         int samples=0;
1219         
1220         colsq[0] = colsq[1] = colsq[2] = 0.0;
1221         col[0] = col[1] = col[2] = 0.0;
1222         col[3]= shr->alpha;
1223         
1224         if (blur > 0.0) {
1225                 if (adapt_thresh != 0.0) samp_type = SAMP_TYPE_HALTON;
1226                 else samp_type = SAMP_TYPE_HAMMERSLEY;
1227                         
1228                 /* all samples are generated per pixel */
1229                 qsa = get_thread_qmcsampler(&R, shi->thread, samp_type, max_samples);
1230                 QMC_initPixel(qsa, shi->thread);
1231         } else 
1232                 max_samples = 1;
1233         
1234
1235         while (samples < max_samples) {         
1236                 refraction(v_refract, shi->vn, shi->view, shi->ang);
1237                 
1238                 if (max_samples > 1) {
1239                         /* get a quasi-random vector from a phong-weighted disc */
1240                         QMC_samplePhong(samp3d, qsa, shi->thread, samples, blur);
1241                                                 
1242                         VecOrthoBasisf(v_refract, orthx, orthy);
1243                         VecMulf(orthx, samp3d[0]);
1244                         VecMulf(orthy, samp3d[1]);
1245                                 
1246                         /* and perturb the refraction vector in it */
1247                         VecAddf(v_refract_new, v_refract, orthx);
1248                         VecAddf(v_refract_new, v_refract_new, orthy);
1249                         
1250                         Normalize(v_refract_new);
1251                 } else {
1252                         /* no blurriness, use the original normal */
1253                         VECCOPY(v_refract_new, v_refract);
1254                 }
1255         
1256                 traceray(shi, shr, shi->mat->ray_depth_tra, shi->co, v_refract_new, sampcol, shi->obi, shi->vlr, RAY_TRA|RAY_TRAFLIP);
1257         
1258                 col[0] += sampcol[0];
1259                 col[1] += sampcol[1];
1260                 col[2] += sampcol[2];
1261                 col[3] += sampcol[3];
1262                 
1263                 /* for variance calc */
1264                 colsq[0] += sampcol[0]*sampcol[0];
1265                 colsq[1] += sampcol[1]*sampcol[1];
1266                 colsq[2] += sampcol[2]*sampcol[2];
1267                 
1268                 samples++;
1269                 
1270                 /* adaptive sampling */
1271                 if (adapt_thresh < 1.0 && samples > max_samples/2) 
1272                 {
1273                         if (adaptive_sample_variance(samples, col, colsq, adapt_thresh))
1274                                 break;
1275                         
1276                         /* if the pixel so far is very dark, we can get away with less samples */
1277                         if ( (col[0] + col[1] + col[2])/3.0/(float)samples < 0.01 )
1278                                 max_samples--;
1279                 }
1280         }
1281         
1282         col[0] /= (float)samples;
1283         col[1] /= (float)samples;
1284         col[2] /= (float)samples;
1285         col[3] /= (float)samples;
1286         
1287         if (qsa)
1288                 release_thread_qmcsampler(&R, shi->thread, qsa);
1289 }
1290
1291 static void trace_reflect(float *col, ShadeInput *shi, ShadeResult *shr, float fresnelfac)
1292 {
1293         QMCSampler *qsa=NULL;
1294         int samp_type;
1295         
1296         float samp3d[3], orthx[3], orthy[3];
1297         float v_nor_new[3], v_reflect[3];
1298         float sampcol[4], colsq[4];
1299                 
1300         float blur = pow(1.0 - shi->mat->gloss_mir, 3);
1301         short max_samples = shi->mat->samp_gloss_mir;
1302         float adapt_thresh = shi->mat->adapt_thresh_mir;
1303         float aniso = 1.0 - shi->mat->aniso_gloss_mir;
1304         
1305         int samples=0;
1306         
1307         col[0] = col[1] = col[2] = 0.0;
1308         colsq[0] = colsq[1] = colsq[2] = 0.0;
1309         
1310         if (blur > 0.0) {
1311                 if (adapt_thresh != 0.0) samp_type = SAMP_TYPE_HALTON;
1312                 else samp_type = SAMP_TYPE_HAMMERSLEY;
1313                         
1314                 /* all samples are generated per pixel */
1315                 qsa = get_thread_qmcsampler(&R, shi->thread, samp_type, max_samples);
1316                 QMC_initPixel(qsa, shi->thread);
1317         } else 
1318                 max_samples = 1;
1319         
1320         while (samples < max_samples) {
1321                                 
1322                 if (max_samples > 1) {
1323                         /* get a quasi-random vector from a phong-weighted disc */
1324                         QMC_samplePhong(samp3d, qsa, shi->thread, samples, blur);
1325
1326                         /* find the normal's perpendicular plane, blurring along tangents
1327                          * if tangent shading enabled */
1328                         if (shi->mat->mode & (MA_TANGENT_V)) {
1329                                 Crossf(orthx, shi->vn, shi->tang);      // bitangent
1330                                 VECCOPY(orthy, shi->tang);
1331                                 VecMulf(orthx, samp3d[0]);
1332                                 VecMulf(orthy, samp3d[1]*aniso);
1333                         } else {
1334                                 VecOrthoBasisf(shi->vn, orthx, orthy);
1335                                 VecMulf(orthx, samp3d[0]);
1336                                 VecMulf(orthy, samp3d[1]);
1337                         }
1338
1339                         /* and perturb the normal in it */
1340                         VecAddf(v_nor_new, shi->vn, orthx);
1341                         VecAddf(v_nor_new, v_nor_new, orthy);
1342                         Normalize(v_nor_new);
1343                 } else {
1344                         /* no blurriness, use the original normal */
1345                         VECCOPY(v_nor_new, shi->vn);
1346                 }
1347                 
1348                 if((shi->vlr->flag & R_SMOOTH)) 
1349                         reflection(v_reflect, v_nor_new, shi->view, shi->facenor);
1350                 else
1351                         reflection(v_reflect, v_nor_new, shi->view, NULL);
1352                 
1353                 traceray(shi, shr, shi->mat->ray_depth, shi->co, v_reflect, sampcol, shi->obi, shi->vlr, 0);
1354
1355                 
1356                 col[0] += sampcol[0];
1357                 col[1] += sampcol[1];
1358                 col[2] += sampcol[2];
1359         
1360                 /* for variance calc */
1361                 colsq[0] += sampcol[0]*sampcol[0];
1362                 colsq[1] += sampcol[1]*sampcol[1];
1363                 colsq[2] += sampcol[2]*sampcol[2];
1364                 
1365                 samples++;
1366
1367                 /* adaptive sampling */
1368                 if (adapt_thresh > 0.0 && samples > max_samples/3) 
1369                 {
1370                         if (adaptive_sample_variance(samples, col, colsq, adapt_thresh))
1371                                 break;
1372                         
1373                         /* if the pixel so far is very dark, we can get away with less samples */
1374                         if ( (col[0] + col[1] + col[2])/3.0/(float)samples < 0.01 )
1375                                 max_samples--;
1376                 
1377                         /* reduce samples when reflection is dim due to low ray mirror blend value or fresnel factor
1378                          * and when reflection is blurry */
1379                         if (fresnelfac < 0.1 * (blur+1)) {
1380                                 max_samples--;
1381                                 
1382                                 /* even more for very dim */
1383                                 if (fresnelfac < 0.05 * (blur+1)) 
1384                                         max_samples--;
1385                         }
1386                 }
1387         }
1388         
1389         col[0] /= (float)samples;
1390         col[1] /= (float)samples;
1391         col[2] /= (float)samples;
1392         
1393         if (qsa)
1394                 release_thread_qmcsampler(&R, shi->thread, qsa);
1395 }
1396
1397 /* extern call from render loop */
1398 void ray_trace(ShadeInput *shi, ShadeResult *shr)
1399 {
1400         float i, f, f1, fr, fg, fb;
1401         float mircol[4], tracol[4];
1402         float diff[3];
1403         int do_tra, do_mir;
1404         
1405         do_tra= ((shi->mat->mode & MA_TRANSP) && (shi->mat->mode & MA_RAYTRANSP) && shr->alpha!=1.0f);
1406         do_mir= ((shi->mat->mode & MA_RAYMIRROR) && shi->ray_mirror!=0.0f);
1407         
1408         /* raytrace mirror amd refract like to separate the spec color */
1409         if(shi->combinedflag & SCE_PASS_SPEC)
1410                 VECSUB(diff, shr->combined, shr->spec) /* no ; */
1411         else
1412                 VECCOPY(diff, shr->combined);
1413         
1414         if(do_tra) {
1415                 float olddiff[3];
1416                 
1417                 trace_refract(tracol, shi, shr);
1418                 
1419                 f= shr->alpha; f1= 1.0f-f;
1420                 fr= 1.0f+ shi->mat->filter*(shi->r-1.0f);
1421                 fg= 1.0f+ shi->mat->filter*(shi->g-1.0f);
1422                 fb= 1.0f+ shi->mat->filter*(shi->b-1.0f);
1423                 
1424                 /* for refract pass */
1425                 VECCOPY(olddiff, diff);
1426                 
1427                 diff[0]= f*diff[0] + f1*fr*tracol[0];
1428                 diff[1]= f*diff[1] + f1*fg*tracol[1];
1429                 diff[2]= f*diff[2] + f1*fb*tracol[2];
1430                 
1431                 if(shi->passflag & SCE_PASS_REFRACT)
1432                         VECSUB(shr->refr, diff, olddiff);
1433                 
1434                 if(!(shi->combinedflag & SCE_PASS_REFRACT))
1435                         VECSUB(diff, diff, shr->refr);
1436                 
1437                 shr->alpha= tracol[3];
1438         }
1439         
1440         if(do_mir) {
1441         
1442                 i= shi->ray_mirror*fresnel_fac(shi->view, shi->vn, shi->mat->fresnel_mir_i, shi->mat->fresnel_mir);
1443                 if(i!=0.0f) {
1444                 
1445                         trace_reflect(mircol, shi, shr, i);
1446                         
1447                         fr= i*shi->mirr;
1448                         fg= i*shi->mirg;
1449                         fb= i*shi->mirb;
1450
1451                         if(shi->passflag & SCE_PASS_REFLECT) {
1452                                 /* mirror pass is not blocked out with spec */
1453                                 shr->refl[0]= fr*mircol[0] - fr*diff[0];
1454                                 shr->refl[1]= fg*mircol[1] - fg*diff[1];
1455                                 shr->refl[2]= fb*mircol[2] - fb*diff[2];
1456                         }
1457                         
1458                         if(shi->combinedflag & SCE_PASS_REFLECT) {
1459                                 /* values in shr->spec can be greater then 1.0.
1460                                  * In this case the mircol uses a zero blending factor, so ignoring it is ok.
1461                                  * Fixes bug #18837 - when the spec is higher then 1.0,
1462                                  * diff can become a negative color - Campbell  */
1463                                 
1464                                 f1= 1.0f-i;
1465                                 
1466                                 diff[0] *= f1;
1467                                 diff[1] *= f1;
1468                                 diff[2] *= f1;
1469                                 
1470                                 if(shr->spec[0]<1.0f)   diff[0] += mircol[0] * (fr*(1.0f-shr->spec[0]));
1471                                 if(shr->spec[1]<1.0f)   diff[1] += mircol[1] * (fg*(1.0f-shr->spec[1]));
1472                                 if(shr->spec[2]<1.0f)   diff[2] += mircol[2] * (fb*(1.0f-shr->spec[2]));
1473                         }
1474                 }
1475         }
1476         /* put back together */
1477         if(shi->combinedflag & SCE_PASS_SPEC)
1478                 VECADD(shr->combined, diff, shr->spec) /* no ; */
1479         else
1480                 VECCOPY(shr->combined, diff);
1481 }
1482
1483 /* color 'shadfac' passes through 'col' with alpha and filter */
1484 /* filter is only applied on alpha defined transparent part */
1485 static void addAlphaLight(float *shadfac, float *col, float alpha, float filter)
1486 {
1487         float fr, fg, fb;
1488         
1489         fr= 1.0f+ filter*(col[0]-1.0f);
1490         fg= 1.0f+ filter*(col[1]-1.0f);
1491         fb= 1.0f+ filter*(col[2]-1.0f);
1492         
1493         shadfac[0]= alpha*col[0] + fr*(1.0f-alpha)*shadfac[0];
1494         shadfac[1]= alpha*col[1] + fg*(1.0f-alpha)*shadfac[1];
1495         shadfac[2]= alpha*col[2] + fb*(1.0f-alpha)*shadfac[2];
1496         
1497         shadfac[3]= (1.0f-alpha)*shadfac[3];
1498 }
1499
1500 static void ray_trace_shadow_tra(Isect *is, ShadeInput *origshi, int depth, int traflag)
1501 {
1502         /* ray to lamp, find first face that intersects, check alpha properties,
1503            if it has col[3]>0.0f  continue. so exit when alpha is full */
1504         ShadeInput shi;
1505         ShadeResult shr;
1506         float initial_labda = is->labda;
1507         
1508         if(RE_rayobject_raycast(R.raytree, is)) {
1509                 float d= 1.0f;
1510                 /* we got a face */
1511                 
1512                 /* Warning, This is not that nice, and possibly a bit slow for every ray,
1513                 however some variables were not initialized properly in, unless using shade_input_initialize(...), we need to do a memset */
1514                 memset(&shi, 0, sizeof(ShadeInput)); 
1515                 /* end warning! - Campbell */
1516                 
1517                 shi.depth= 1;                                   /* only used to indicate tracing */
1518                 shi.mask= origshi->mask;
1519                 shi.thread= origshi->thread;
1520                 shi.passflag= SCE_PASS_COMBINED;
1521                 shi.combinedflag= 0xFFFFFF;              /* ray trace does all options */
1522         
1523                 shi.xs= origshi->xs;
1524                 shi.ys= origshi->ys;
1525                 shi.lay= origshi->lay;
1526                 shi.nodes= origshi->nodes;
1527                 
1528                 shade_ray(is, &shi, &shr);
1529                 if (shi.mat->material_type == MA_TYPE_SURFACE) {
1530                         if (traflag & RAY_TRA)
1531                                 d= shade_by_transmission(is, &shi, &shr);
1532                         
1533                         /* mix colors based on shadfac (rgb + amount of light factor) */
1534                         addAlphaLight(is->col, shr.diff, shr.alpha, d*shi.mat->filter);
1535                 } else if (shi.mat->material_type == MA_TYPE_VOLUME) {
1536                         addAlphaLight(is->col, shr.combined, shr.alpha, 1.0f);
1537                 }
1538                 
1539                 if(depth>0 && is->col[3]>0.0f) {
1540                         
1541                         /* adapt isect struct */
1542                         VECCOPY(is->start, shi.co);
1543                         is->labda = initial_labda-is->labda;
1544                         is->orig.ob   = shi.obi;
1545                         is->orig.face = shi.vlr;
1546
1547                         ray_trace_shadow_tra(is, origshi, depth-1, traflag | RAY_TRA);
1548                 }
1549                 
1550                 RE_RC_MERGE(&origshi->raycounter, &shi.raycounter);
1551         }
1552 }
1553
1554 /* not used, test function for ambient occlusion (yaf: pathlight) */
1555 /* main problem; has to be called within shading loop, giving unwanted recursion */
1556 int ray_trace_shadow_rad(ShadeInput *ship, ShadeResult *shr)
1557 {
1558         static int counter=0, only_one= 0;
1559         extern float hashvectf[];
1560         Isect isec;
1561         ShadeInput shi;
1562         ShadeResult shr_t;
1563         float vec[3], accum[3], div= 0.0f;
1564         int a;
1565         
1566         assert(0);
1567         
1568         if(only_one) {
1569                 return 0;
1570         }
1571         only_one= 1;
1572         
1573         accum[0]= accum[1]= accum[2]= 0.0f;
1574         isec.mode= RE_RAY_MIRROR;
1575         isec.orig.ob   = ship->obi;
1576         isec.orig.face = ship->vlr;
1577         isec.hint = 0;
1578
1579         VECCOPY(isec.start, ship->co);
1580         
1581         RE_RC_INIT(isec, shi);
1582         
1583         for(a=0; a<8*8; a++) {
1584                 
1585                 counter+=3;
1586                 counter %= 768;
1587                 VECCOPY(vec, hashvectf+counter);
1588                 if(ship->vn[0]*vec[0]+ship->vn[1]*vec[1]+ship->vn[2]*vec[2]>0.0f) {
1589                         vec[0]-= vec[0];
1590                         vec[1]-= vec[1];
1591                         vec[2]-= vec[2];
1592                 }
1593
1594                 VECCOPY(isec.vec, vec );
1595                 isec.labda = RE_RAYTRACE_MAXDIST;
1596
1597                 if(RE_rayobject_raycast(R.raytree, &isec)) {
1598                         float fac;
1599                         
1600                         /* Warning, This is not that nice, and possibly a bit slow for every ray,
1601                         however some variables were not initialized properly in, unless using shade_input_initialize(...), we need to do a memset */
1602                         memset(&shi, 0, sizeof(ShadeInput)); 
1603                         /* end warning! - Campbell */
1604                         
1605                         shade_ray(&isec, &shi, &shr_t);
1606                         fac= isec.labda*isec.labda;
1607                         fac= 1.0f;
1608                         accum[0]+= fac*(shr_t.diff[0]+shr_t.spec[0]);
1609                         accum[1]+= fac*(shr_t.diff[1]+shr_t.spec[1]);
1610                         accum[2]+= fac*(shr_t.diff[2]+shr_t.spec[2]);
1611                         div+= fac;
1612                 }
1613                 else div+= 1.0f;
1614         }
1615         
1616         if(div!=0.0f) {
1617                 shr->diff[0]+= accum[0]/div;
1618                 shr->diff[1]+= accum[1]/div;
1619                 shr->diff[2]+= accum[2]/div;
1620         }
1621         shr->alpha= 1.0f;
1622         
1623         only_one= 0;
1624         return 1;
1625 }
1626
1627 /* aolight: function to create random unit sphere vectors for total random sampling */
1628 static void RandomSpherical(float *v)
1629 {
1630         float r;
1631         v[2] = 2.f*BLI_frand()-1.f;
1632         if ((r = 1.f - v[2]*v[2])>0.f) {
1633                 float a = 6.283185307f*BLI_frand();
1634                 r = sqrt(r);
1635                 v[0] = r * cos(a);
1636                 v[1] = r * sin(a);
1637         }
1638         else v[2] = 1.f;
1639 }
1640
1641 /* calc distributed spherical energy */
1642 static void DS_energy(float *sphere, int tot, float *vec)
1643 {
1644         float *fp, fac, force[3], res[3];
1645         int a;
1646         
1647         res[0]= res[1]= res[2]= 0.0f;
1648         
1649         for(a=0, fp=sphere; a<tot; a++, fp+=3) {
1650                 VecSubf(force, vec, fp);
1651                 fac= force[0]*force[0] + force[1]*force[1] + force[2]*force[2];
1652                 if(fac!=0.0f) {
1653                         fac= 1.0f/fac;
1654                         res[0]+= fac*force[0];
1655                         res[1]+= fac*force[1];
1656                         res[2]+= fac*force[2];
1657                 }
1658         }
1659
1660         VecMulf(res, 0.5);
1661         VecAddf(vec, vec, res);
1662         Normalize(vec);
1663         
1664 }
1665
1666 /* called from convertBlenderScene.c */
1667 /* creates an equally distributed spherical sample pattern */
1668 /* and allocates threadsafe memory */
1669 void init_ao_sphere(World *wrld)
1670 {
1671         float *fp;
1672         int a, tot, iter= 16;
1673
1674         /* we make twice the amount of samples, because only a hemisphere is used */
1675         tot= 2*wrld->aosamp*wrld->aosamp;
1676         
1677         wrld->aosphere= MEM_mallocN(3*tot*sizeof(float), "AO sphere");
1678         
1679         /* fixed random */
1680         BLI_srandom(tot);
1681         
1682         /* init */
1683         fp= wrld->aosphere;
1684         for(a=0; a<tot; a++, fp+= 3) {
1685                 RandomSpherical(fp);
1686         }
1687         
1688         while(iter--) {
1689                 for(a=0, fp= wrld->aosphere; a<tot; a++, fp+= 3) {
1690                         DS_energy(wrld->aosphere, tot, fp);
1691                 }
1692         }
1693         
1694         /* tables */
1695         wrld->aotables= MEM_mallocN(BLENDER_MAX_THREADS*3*tot*sizeof(float), "AO tables");
1696 }
1697
1698 /* give per thread a table, we have to compare xs ys because of way OSA works... */
1699 static float *threadsafe_table_sphere(int test, int thread, int xs, int ys, int tot)
1700 {
1701         static int xso[BLENDER_MAX_THREADS], yso[BLENDER_MAX_THREADS];
1702         static int firsttime= 1;
1703         
1704         if(firsttime) {
1705                 memset(xso, 255, sizeof(xso));
1706                 memset(yso, 255, sizeof(yso));
1707                 firsttime= 0;
1708         }
1709         
1710         if(xs==xso[thread] && ys==yso[thread]) return R.wrld.aotables+ thread*tot*3;
1711         if(test) return NULL;
1712         xso[thread]= xs; yso[thread]= ys;
1713         return R.wrld.aotables+ thread*tot*3;
1714 }
1715
1716 static float *sphere_sampler(int type, int resol, int thread, int xs, int ys)
1717 {
1718         int tot;
1719         float *vec;
1720         
1721         tot= 2*resol*resol;
1722
1723         if (type & WO_AORNDSMP) {
1724                 float *sphere;
1725                 int a;
1726                 
1727                 // always returns table
1728                 sphere= threadsafe_table_sphere(0, thread, xs, ys, tot);
1729
1730                 /* total random sampling. NOT THREADSAFE! (should be removed, is not useful) */
1731                 vec= sphere;
1732                 for (a=0; a<tot; a++, vec+=3) {
1733                         RandomSpherical(vec);
1734                 }
1735                 
1736                 return sphere;
1737         } 
1738         else {
1739                 float *sphere;
1740                 float cosfi, sinfi, cost, sint;
1741                 float ang, *vec1;
1742                 int a;
1743                 
1744                 // returns table if xs and ys were equal to last call
1745                 sphere= threadsafe_table_sphere(1, thread, xs, ys, tot);
1746                 if(sphere==NULL) {
1747                         sphere= threadsafe_table_sphere(0, thread, xs, ys, tot);
1748                         
1749                         // random rotation
1750                         ang= BLI_thread_frand(thread);
1751                         sinfi= sin(ang); cosfi= cos(ang);
1752                         ang= BLI_thread_frand(thread);
1753                         sint= sin(ang); cost= cos(ang);
1754                         
1755                         vec= R.wrld.aosphere;
1756                         vec1= sphere;
1757                         for (a=0; a<tot; a++, vec+=3, vec1+=3) {
1758                                 vec1[0]= cost*cosfi*vec[0] - sinfi*vec[1] + sint*cosfi*vec[2];
1759                                 vec1[1]= cost*sinfi*vec[0] + cosfi*vec[1] + sint*sinfi*vec[2];
1760                                 vec1[2]= -sint*vec[0] + cost*vec[2];                    
1761                         }
1762                 }
1763                 return sphere;
1764         }
1765 }
1766
1767 static void ray_ao_qmc(ShadeInput *shi, float *shadfac)
1768 {
1769         Isect isec;
1770         RayHint point_hint;
1771         QMCSampler *qsa=NULL;
1772         float samp3d[3];
1773         float up[3], side[3], dir[3], nrm[3];
1774         
1775         float maxdist = R.wrld.aodist;
1776         float fac=0.0f, prev=0.0f;
1777         float adapt_thresh = R.wrld.ao_adapt_thresh;
1778         float adapt_speed_fac = R.wrld.ao_adapt_speed_fac;
1779         
1780         int samples=0;
1781         int max_samples = R.wrld.aosamp*R.wrld.aosamp;
1782         
1783         float dxyview[3], skyadded=0, div;
1784         int aocolor;
1785         
1786         RE_RC_INIT(isec, *shi);
1787         isec.orig.ob   = shi->obi;
1788         isec.orig.face = shi->vlr;
1789         isec.skip = RE_SKIP_VLR_NEIGHBOUR | RE_SKIP_VLR_RENDER_CHECK | RE_SKIP_VLR_NON_SOLID_MATERIAL;
1790         isec.hint = 0;
1791
1792         isec.hit.ob   = 0;
1793         isec.hit.face = 0;
1794
1795         isec.last_hit = NULL;
1796         
1797         isec.mode= (R.wrld.aomode & WO_AODIST)?RE_RAY_SHADOW_TRA:RE_RAY_SHADOW;
1798         isec.lay= -1;
1799         
1800         VECCOPY(isec.start, shi->co);           
1801         RE_rayobject_hint_bb( R.raytree, &point_hint, isec.start, isec.start );
1802         isec.hint = &point_hint;
1803
1804         
1805         shadfac[0]= shadfac[1]= shadfac[2]= 0.0f;
1806         
1807         /* prevent sky colors to be added for only shadow (shadow becomes alpha) */
1808         aocolor= R.wrld.aocolor;
1809         if(shi->mat->mode & MA_ONLYSHADOW)
1810                 aocolor= WO_AOPLAIN;
1811         
1812         if(aocolor == WO_AOSKYTEX) {
1813                 dxyview[0]= 1.0f/(float)R.wrld.aosamp;
1814                 dxyview[1]= 1.0f/(float)R.wrld.aosamp;
1815                 dxyview[2]= 0.0f;
1816         }
1817         
1818         if(shi->vlr->flag & R_SMOOTH) {
1819                 VECCOPY(nrm, shi->vn);
1820         }
1821         else {
1822                 VECCOPY(nrm, shi->facenor);
1823         }
1824         
1825         VecOrthoBasisf(nrm, up, side);
1826         
1827         /* sampling init */
1828         if (R.wrld.ao_samp_method==WO_AOSAMP_HALTON) {
1829                 float speedfac;
1830                 
1831                 speedfac = get_avg_speed(shi) * adapt_speed_fac;
1832                 CLAMP(speedfac, 1.0, 1000.0);
1833                 max_samples /= speedfac;
1834                 if (max_samples < 5) max_samples = 5;
1835                 
1836                 qsa = get_thread_qmcsampler(&R, shi->thread, SAMP_TYPE_HALTON, max_samples);
1837         } else if (R.wrld.ao_samp_method==WO_AOSAMP_HAMMERSLEY)
1838                 qsa = get_thread_qmcsampler(&R, shi->thread, SAMP_TYPE_HAMMERSLEY, max_samples);
1839
1840         QMC_initPixel(qsa, shi->thread);
1841         
1842         
1843         while (samples < max_samples) {
1844
1845                 /* sampling, returns quasi-random vector in unit hemisphere */
1846                 QMC_sampleHemi(samp3d, qsa, shi->thread, samples);
1847
1848                 dir[0] = (samp3d[0]*up[0] + samp3d[1]*side[0] + samp3d[2]*nrm[0]);
1849                 dir[1] = (samp3d[0]*up[1] + samp3d[1]*side[1] + samp3d[2]*nrm[1]);
1850                 dir[2] = (samp3d[0]*up[2] + samp3d[1]*side[2] + samp3d[2]*nrm[2]);
1851                 
1852                 Normalize(dir);
1853                         
1854                 isec.vec[0] = -dir[0];
1855                 isec.vec[1] = -dir[1];
1856                 isec.vec[2] = -dir[2];
1857                 isec.labda = maxdist;
1858                 
1859                 prev = fac;
1860                 
1861                 if(RE_rayobject_raycast(R.raytree, &isec)) {
1862                         if (R.wrld.aomode & WO_AODIST) fac+= exp(-isec.labda*R.wrld.aodistfac); 
1863                         else fac+= 1.0f;
1864                 }
1865                 else if(aocolor!=WO_AOPLAIN) {
1866                         float skycol[4];
1867                         float skyfac, view[3];
1868                         
1869                         view[0]= -dir[0];
1870                         view[1]= -dir[1];
1871                         view[2]= -dir[2];
1872                         Normalize(view);
1873                         
1874                         if(aocolor==WO_AOSKYCOL) {
1875                                 skyfac= 0.5*(1.0f+view[0]*R.grvec[0]+ view[1]*R.grvec[1]+ view[2]*R.grvec[2]);
1876                                 shadfac[0]+= (1.0f-skyfac)*R.wrld.horr + skyfac*R.wrld.zenr;
1877                                 shadfac[1]+= (1.0f-skyfac)*R.wrld.horg + skyfac*R.wrld.zeng;
1878                                 shadfac[2]+= (1.0f-skyfac)*R.wrld.horb + skyfac*R.wrld.zenb;
1879                         }
1880                         else {  /* WO_AOSKYTEX */
1881                                 shadeSkyView(skycol, isec.start, view, dxyview, shi->thread);
1882                                 shadeSunView(skycol, shi->view);
1883                                 shadfac[0]+= skycol[0];
1884                                 shadfac[1]+= skycol[1];
1885                                 shadfac[2]+= skycol[2];
1886                         }
1887                         skyadded++;
1888                 }
1889                 
1890                 samples++;
1891                 
1892                 if (qsa->type == SAMP_TYPE_HALTON) {
1893                         /* adaptive sampling - consider samples below threshold as in shadow (or vice versa) and exit early */          
1894                         if (adapt_thresh > 0.0 && (samples > max_samples/2) ) {
1895                                 
1896                                 if (adaptive_sample_contrast_val(samples, prev, fac, adapt_thresh)) {
1897                                         break;
1898                                 }
1899                         }
1900                 }
1901         }
1902         
1903         if(aocolor!=WO_AOPLAIN && skyadded) {
1904                 div= (1.0f - fac/(float)samples)/((float)skyadded);
1905                 
1906                 shadfac[0]*= div;       // average color times distances/hits formula
1907                 shadfac[1]*= div;       // average color times distances/hits formula
1908                 shadfac[2]*= div;       // average color times distances/hits formula
1909         } else {
1910                 shadfac[0]= shadfac[1]= shadfac[2]= 1.0f - fac/(float)samples;
1911         }
1912         
1913         if (qsa)
1914                 release_thread_qmcsampler(&R, shi->thread, qsa);
1915 }
1916
1917 /* extern call from shade_lamp_loop, ambient occlusion calculus */
1918 static void ray_ao_spheresamp(ShadeInput *shi, float *shadfac)
1919 {
1920         Isect isec;
1921         RayHint point_hint;
1922         float *vec, *nrm, div, bias, sh=0.0f;
1923         float maxdist = R.wrld.aodist;
1924         float dxyview[3];
1925         int j= -1, tot, actual=0, skyadded=0, aocolor, resol= R.wrld.aosamp;
1926         
1927         RE_RC_INIT(isec, *shi);
1928         isec.orig.ob   = shi->obi;
1929         isec.orig.face = shi->vlr;
1930         isec.skip = RE_SKIP_VLR_NEIGHBOUR | RE_SKIP_VLR_RENDER_CHECK;
1931         isec.hint = 0;
1932
1933         isec.hit.ob   = 0;
1934         isec.hit.face = 0;
1935         
1936         isec.last_hit = NULL;
1937         
1938         isec.mode= (R.wrld.aomode & WO_AODIST)?RE_RAY_SHADOW_TRA:RE_RAY_SHADOW;
1939         isec.lay= -1;
1940
1941         VECCOPY(isec.start, shi->co);           
1942         RE_rayobject_hint_bb( R.raytree, &point_hint, isec.start, isec.start );
1943         isec.hint = &point_hint;
1944
1945         shadfac[0]= shadfac[1]= shadfac[2]= 0.0f;
1946
1947         /* bias prevents smoothed faces to appear flat */
1948         if(shi->vlr->flag & R_SMOOTH) {
1949                 bias= R.wrld.aobias;
1950                 nrm= shi->vn;
1951         }
1952         else {
1953                 bias= 0.0f;
1954                 nrm= shi->facenor;
1955         }
1956
1957         /* prevent sky colors to be added for only shadow (shadow becomes alpha) */
1958         aocolor= R.wrld.aocolor;
1959         if(shi->mat->mode & MA_ONLYSHADOW)
1960                 aocolor= WO_AOPLAIN;
1961         
1962         if(resol>32) resol= 32;
1963         
1964         vec= sphere_sampler(R.wrld.aomode, resol, shi->thread, shi->xs, shi->ys);
1965         
1966         // warning: since we use full sphere now, and dotproduct is below, we do twice as much
1967         tot= 2*resol*resol;
1968
1969         if(aocolor == WO_AOSKYTEX) {
1970                 dxyview[0]= 1.0f/(float)resol;
1971                 dxyview[1]= 1.0f/(float)resol;
1972                 dxyview[2]= 0.0f;
1973         }
1974         
1975         while(tot--) {
1976                 
1977                 if ((vec[0]*nrm[0] + vec[1]*nrm[1] + vec[2]*nrm[2]) > bias) {
1978                         /* only ao samples for mask */
1979                         if(R.r.mode & R_OSA) {
1980                                 j++;
1981                                 if(j==R.osa) j= 0;
1982                                 if(!(shi->mask & (1<<j))) {
1983                                         vec+=3;
1984                                         continue;
1985                                 }
1986                         }
1987                         
1988                         actual++;
1989                         
1990                         /* always set start/vec/labda */
1991                         isec.vec[0] = -vec[0];
1992                         isec.vec[1] = -vec[1];
1993                         isec.vec[2] = -vec[2];
1994                         isec.labda = maxdist;
1995                         
1996                         /* do the trace */
1997                         if(RE_rayobject_raycast(R.raytree, &isec)) {
1998                                 if (R.wrld.aomode & WO_AODIST) sh+= exp(-isec.labda*R.wrld.aodistfac); 
1999                                 else sh+= 1.0f;
2000                         }
2001                         else if(aocolor!=WO_AOPLAIN) {
2002                                 float skycol[4];
2003                                 float fac, view[3];
2004                                 
2005                                 view[0]= -vec[0];
2006                                 view[1]= -vec[1];
2007                                 view[2]= -vec[2];
2008                                 Normalize(view);
2009                                 
2010                                 if(aocolor==WO_AOSKYCOL) {
2011                                         fac= 0.5*(1.0f+view[0]*R.grvec[0]+ view[1]*R.grvec[1]+ view[2]*R.grvec[2]);
2012                                         shadfac[0]+= (1.0f-fac)*R.wrld.horr + fac*R.wrld.zenr;
2013                                         shadfac[1]+= (1.0f-fac)*R.wrld.horg + fac*R.wrld.zeng;
2014                                         shadfac[2]+= (1.0f-fac)*R.wrld.horb + fac*R.wrld.zenb;
2015                                 }
2016                                 else {  /* WO_AOSKYTEX */
2017                                         shadeSkyView(skycol, isec.start, view, dxyview, shi->thread);
2018                                         shadeSunView(skycol, shi->view);
2019                                         shadfac[0]+= skycol[0];
2020                                         shadfac[1]+= skycol[1];
2021                                         shadfac[2]+= skycol[2];
2022                                 }
2023                                 skyadded++;
2024                         }
2025                 }
2026                 // samples
2027                 vec+= 3;
2028         }
2029         
2030         if(actual==0) sh= 1.0f;
2031         else sh = 1.0f - sh/((float)actual);
2032         
2033         if(aocolor!=WO_AOPLAIN && skyadded) {
2034                 div= sh/((float)skyadded);
2035                 
2036                 shadfac[0]*= div;       // average color times distances/hits formula
2037                 shadfac[1]*= div;       // average color times distances/hits formula
2038                 shadfac[2]*= div;       // average color times distances/hits formula
2039         }
2040         else {
2041                 shadfac[0]= shadfac[1]= shadfac[2]= sh;
2042         }
2043 }
2044
2045 void ray_ao(ShadeInput *shi, float *shadfac)
2046 {
2047         /* Unfortunately, the unusual way that the sphere sampler calculates roughly twice as many
2048          * samples as are actually traced, and skips them based on bias and OSA settings makes it very difficult
2049          * to reuse code between these two functions. This is the easiest way I can think of to do it
2050          * --broken */
2051         if (ELEM(R.wrld.ao_samp_method, WO_AOSAMP_HAMMERSLEY, WO_AOSAMP_HALTON))
2052                 ray_ao_qmc(shi, shadfac);
2053         else if (R.wrld.ao_samp_method == WO_AOSAMP_CONSTANT)
2054                 ray_ao_spheresamp(shi, shadfac);
2055 }
2056
2057 static void ray_shadow_jittered_coords(ShadeInput *shi, int max, float jitco[RE_MAX_OSA][3], int *totjitco)
2058 {
2059         /* magic numbers for reordering sample positions to give better
2060          * results with adaptive sample, when it usually only takes 4 samples */
2061         int order8[8] = {0, 1, 5, 6, 2, 3, 4, 7};
2062         int order11[11] = {1, 3, 8, 10, 0, 2, 4, 5, 6, 7, 9};
2063         int order16[16] = {1, 3, 9, 12, 0, 6, 7, 8, 13, 2, 4, 5, 10, 11, 14, 15};
2064         int count = count_mask(shi->mask);
2065
2066         /* for better antialising shadow samples are distributed over the subpixel
2067          * sample coordinates, this only works for raytracing depth 0 though */
2068         if(!shi->strand && shi->depth == 0 && count > 1 && count <= max) {
2069                 float xs, ys, zs, view[3];
2070                 int samp, ordsamp, tot= 0;
2071
2072                 for(samp=0; samp<R.osa; samp++) {
2073                         if(R.osa == 8) ordsamp = order8[samp];
2074                         else if(R.osa == 11) ordsamp = order11[samp];
2075                         else if(R.osa == 16) ordsamp = order16[samp];
2076                         else ordsamp = samp;
2077
2078                         if(shi->mask & (1<<ordsamp)) {
2079                                 /* zbuffer has this inverse corrected, ensures xs,ys are inside pixel */
2080                                 xs= (float)shi->scanco[0] + R.jit[ordsamp][0] + 0.5f;
2081                                 ys= (float)shi->scanco[1] + R.jit[ordsamp][1] + 0.5f;
2082                                 zs= shi->scanco[2];
2083
2084                                 shade_input_calc_viewco(shi, xs, ys, zs, view, NULL, jitco[tot], NULL, NULL);
2085                                 tot++;
2086                         }
2087                 }
2088
2089                 *totjitco= tot;
2090         }
2091         else {
2092                 VECCOPY(jitco[0], shi->co);
2093                 *totjitco= 1;
2094         }
2095 }
2096
2097 static void ray_shadow_qmc(ShadeInput *shi, LampRen *lar, float *lampco, float *shadfac, Isect *isec)
2098 {
2099         QMCSampler *qsa=NULL;
2100         int samples=0;
2101         float samp3d[3];
2102
2103         float fac=0.0f, vec[3], end[3];
2104         float colsq[4];
2105         float adapt_thresh = lar->adapt_thresh;
2106         int min_adapt_samples=4, max_samples = lar->ray_totsamp;
2107         float *co;
2108         int do_soft=1, full_osa=0, i;
2109
2110         float min[3], max[3];
2111         RayHint bb_hint;
2112
2113         float jitco[RE_MAX_OSA][3];
2114         int totjitco;
2115
2116         colsq[0] = colsq[1] = colsq[2] = 0.0;
2117         if(isec->mode==RE_RAY_SHADOW_TRA) {
2118                 shadfac[0]= shadfac[1]= shadfac[2]= shadfac[3]= 0.0f;
2119         } else
2120                 shadfac[3]= 1.0f;
2121         
2122         if (lar->ray_totsamp < 2) do_soft = 0;
2123         if ((R.r.mode & R_OSA) && (R.osa > 0) && (shi->vlr->flag & R_FULL_OSA)) full_osa = 1;
2124         
2125         if (full_osa) {
2126                 if (do_soft) max_samples  = max_samples/R.osa + 1;
2127                 else max_samples = 1;
2128         } else {
2129                 if (do_soft) max_samples = lar->ray_totsamp;
2130                 else if (shi->depth == 0) max_samples = (R.osa > 4)?R.osa:5;
2131                 else max_samples = 1;
2132         }
2133         
2134         ray_shadow_jittered_coords(shi, max_samples, jitco, &totjitco);
2135
2136         /* sampling init */
2137         if (lar->ray_samp_method==LA_SAMP_HALTON)
2138                 qsa = get_thread_qmcsampler(&R, shi->thread, SAMP_TYPE_HALTON, max_samples);
2139         else if (lar->ray_samp_method==LA_SAMP_HAMMERSLEY)
2140                 qsa = get_thread_qmcsampler(&R, shi->thread, SAMP_TYPE_HAMMERSLEY, max_samples);
2141         
2142         QMC_initPixel(qsa, shi->thread);
2143
2144         INIT_MINMAX(min, max);
2145         for(i=0; i<totjitco; i++)
2146         {
2147                 DO_MINMAX(jitco[i], min, max);
2148         }
2149         RE_rayobject_hint_bb( R.raytree, &bb_hint, min, max);
2150         
2151         isec->hint = &bb_hint;
2152         isec->skip = RE_SKIP_VLR_NEIGHBOUR | RE_SKIP_VLR_RENDER_CHECK;
2153         VECCOPY(vec, lampco);
2154         
2155         while (samples < max_samples) {
2156
2157                 isec->orig.ob   = shi->obi;
2158                 isec->orig.face = shi->vlr;
2159
2160                 /* manually jitter the start shading co-ord per sample
2161                  * based on the pre-generated OSA texture sampling offsets, 
2162                  * for anti-aliasing sharp shadow edges. */
2163                 co = jitco[samples % totjitco];
2164
2165                 if (do_soft) {
2166                         /* sphere shadow source */
2167                         if (lar->type == LA_LOCAL) {
2168                                 float ru[3], rv[3], v[3], s[3];
2169                                 
2170                                 /* calc tangent plane vectors */
2171                                 v[0] = co[0] - lampco[0];
2172                                 v[1] = co[1] - lampco[1];
2173                                 v[2] = co[2] - lampco[2];
2174                                 Normalize(v);
2175                                 VecOrthoBasisf(v, ru, rv);
2176                                 
2177                                 /* sampling, returns quasi-random vector in area_size disc */
2178                                 QMC_sampleDisc(samp3d, qsa, shi->thread, samples,lar->area_size);
2179
2180                                 /* distribute disc samples across the tangent plane */
2181                                 s[0] = samp3d[0]*ru[0] + samp3d[1]*rv[0];
2182                                 s[1] = samp3d[0]*ru[1] + samp3d[1]*rv[1];
2183                                 s[2] = samp3d[0]*ru[2] + samp3d[1]*rv[2];
2184                                 
2185                                 VECCOPY(samp3d, s);
2186                         }
2187                         else {
2188                                 /* sampling, returns quasi-random vector in [sizex,sizey]^2 plane */
2189                                 QMC_sampleRect(samp3d, qsa, shi->thread, samples, lar->area_size, lar->area_sizey);
2190                                                                 
2191                                 /* align samples to lamp vector */
2192                                 Mat3MulVecfl(lar->mat, samp3d);
2193                         }
2194                         end[0] = vec[0]+samp3d[0];
2195                         end[1] = vec[1]+samp3d[1];
2196                         end[2] = vec[2]+samp3d[2];
2197                 } else {
2198                         VECCOPY(end, vec);
2199                 }
2200
2201                 if(shi->strand) {
2202                         /* bias away somewhat to avoid self intersection */
2203                         float jitbias= 0.5f*(VecLength(shi->dxco) + VecLength(shi->dyco));
2204                         float v[3];
2205
2206                         VECSUB(v, co, end);
2207                         Normalize(v);
2208
2209                         co[0] -= jitbias*v[0];
2210                         co[1] -= jitbias*v[1];
2211                         co[2] -= jitbias*v[2];
2212                 }
2213
2214                 VECCOPY(isec->start, co);
2215                 isec->vec[0] = end[0]-isec->start[0];
2216                 isec->vec[1] = end[1]-isec->start[1];
2217                 isec->vec[2] = end[2]-isec->start[2];
2218                 isec->labda = 1.0f; // * Normalize(isec->vec);
2219                 
2220                 /* trace the ray */
2221                 if(isec->mode==RE_RAY_SHADOW_TRA) {
2222                         isec->col[0]= isec->col[1]= isec->col[2]=  1.0f;
2223                         isec->col[3]= 1.0f;
2224                         
2225                         ray_trace_shadow_tra(isec, shi, DEPTH_SHADOW_TRA, 0);
2226                         shadfac[0] += isec->col[0];
2227                         shadfac[1] += isec->col[1];
2228                         shadfac[2] += isec->col[2];
2229                         shadfac[3] += isec->col[3];
2230                         
2231                         /* for variance calc */
2232                         colsq[0] += isec->col[0]*isec->col[0];
2233                         colsq[1] += isec->col[1]*isec->col[1];
2234                         colsq[2] += isec->col[2]*isec->col[2];
2235                 }
2236                 else {
2237                         if( RE_rayobject_raycast(R.raytree, isec) ) fac+= 1.0f;
2238                 }
2239                 
2240                 samples++;
2241                 
2242                 if ((lar->ray_samp_method == LA_SAMP_HALTON)) {
2243                 
2244                         /* adaptive sampling - consider samples below threshold as in shadow (or vice versa) and exit early */
2245                         if ((max_samples > min_adapt_samples) && (adapt_thresh > 0.0) && (samples > max_samples / 3)) {
2246                                 if (isec->mode==RE_RAY_SHADOW_TRA) {
2247                                         if ((shadfac[3] / samples > (1.0-adapt_thresh)) || (shadfac[3] / samples < adapt_thresh))
2248                                                 break;
2249                                         else if (adaptive_sample_variance(samples, shadfac, colsq, adapt_thresh))
2250                                                 break;
2251                                 } else {
2252                                         if ((fac / samples > (1.0-adapt_thresh)) || (fac / samples < adapt_thresh))
2253                                                 break;
2254                                 }
2255                         }
2256                 }
2257         }
2258         
2259         if(isec->mode==RE_RAY_SHADOW_TRA) {
2260                 shadfac[0] /= samples;
2261                 shadfac[1] /= samples;
2262                 shadfac[2] /= samples;
2263                 shadfac[3] /= samples;
2264         } else
2265                 shadfac[3]= 1.0f-fac/samples;
2266
2267         if (qsa)
2268                 release_thread_qmcsampler(&R, shi->thread, qsa);
2269 }
2270
2271 static void ray_shadow_jitter(ShadeInput *shi, LampRen *lar, float *lampco, float *shadfac, Isect *isec)
2272 {
2273         /* area soft shadow */
2274         float *jitlamp;
2275         float fac=0.0f, div=0.0f, vec[3];
2276         int a, j= -1, mask;
2277         RayHint point_hint;
2278         
2279         if(isec->mode==RE_RAY_SHADOW_TRA) {
2280                 shadfac[0]= shadfac[1]= shadfac[2]= shadfac[3]= 0.0f;
2281         }
2282         else shadfac[3]= 1.0f;
2283         
2284         fac= 0.0f;
2285         jitlamp= give_jitter_plane(lar, shi->thread, shi->xs, shi->ys);
2286
2287         a= lar->ray_totsamp;
2288         
2289         /* this correction to make sure we always take at least 1 sample */
2290         mask= shi->mask;
2291         if(a==4) mask |= (mask>>4)|(mask>>8);
2292         else if(a==9) mask |= (mask>>9);
2293         
2294         VECCOPY(isec->start, shi->co);          
2295         isec->orig.ob   = shi->obi;
2296         isec->orig.face = shi->vlr;
2297         RE_rayobject_hint_bb( R.raytree, &point_hint, isec->start, isec->start );
2298         isec->hint = &point_hint;
2299         
2300         while(a--) {
2301                 
2302                 if(R.r.mode & R_OSA) {
2303                         j++;
2304                         if(j>=R.osa) j= 0;
2305                         if(!(mask & (1<<j))) {
2306                                 jitlamp+= 2;
2307                                 continue;
2308                         }
2309                 }
2310                 
2311                 vec[0]= jitlamp[0];
2312                 vec[1]= jitlamp[1];
2313                 vec[2]= 0.0f;
2314                 Mat3MulVecfl(lar->mat, vec);
2315                 
2316                 /* set start and vec */
2317                 isec->vec[0] = vec[0]+lampco[0]-isec->start[0];
2318                 isec->vec[1] = vec[1]+lampco[1]-isec->start[1];
2319                 isec->vec[2] = vec[2]+lampco[2]-isec->start[2];
2320                 isec->labda = 1.0f;
2321                 isec->skip = RE_SKIP_VLR_NEIGHBOUR | RE_SKIP_VLR_RENDER_CHECK;
2322                 
2323                 if(isec->mode==RE_RAY_SHADOW_TRA) {
2324                         /* isec.col is like shadfac, so defines amount of light (0.0 is full shadow) */
2325                         isec->col[0]= isec->col[1]= isec->col[2]=  1.0f;
2326                         isec->col[3]= 1.0f;
2327                         
2328                         ray_trace_shadow_tra(isec, shi, DEPTH_SHADOW_TRA, 0);
2329                         shadfac[0] += isec->col[0];
2330                         shadfac[1] += isec->col[1];
2331                         shadfac[2] += isec->col[2];
2332                         shadfac[3] += isec->col[3];
2333                 }
2334                 else if( RE_rayobject_raycast(R.raytree, isec) ) fac+= 1.0f;
2335                 
2336                 div+= 1.0f;
2337                 jitlamp+= 2;
2338         }
2339         
2340         if(isec->mode==RE_RAY_SHADOW_TRA) {
2341                 shadfac[0] /= div;
2342                 shadfac[1] /= div;
2343                 shadfac[2] /= div;
2344                 shadfac[3] /= div;
2345         }
2346         else {
2347                 // sqrt makes nice umbra effect
2348                 if(lar->ray_samp_type & LA_SAMP_UMBRA)
2349                         shadfac[3]= sqrt(1.0f-fac/div);
2350                 else
2351                         shadfac[3]= 1.0f-fac/div;
2352         }
2353 }
2354 /* extern call from shade_lamp_loop */
2355 void ray_shadow(ShadeInput *shi, LampRen *lar, float *shadfac)
2356 {
2357         Isect isec;
2358         float lampco[3];
2359
2360         /* setup isec */
2361         RE_RC_INIT(isec, *shi);
2362         if(shi->mat->mode & MA_SHADOW_TRA) isec.mode= RE_RAY_SHADOW_TRA;
2363         else isec.mode= RE_RAY_SHADOW;
2364         isec.hint = 0;
2365         
2366         if(lar->mode & (LA_LAYER|LA_LAYER_SHADOW))
2367                 isec.lay= lar->lay;
2368         else
2369                 isec.lay= -1;
2370
2371         /* only when not mir tracing, first hit optimm */
2372         if(shi->depth==0) {
2373                 isec.last_hit = lar->last_hit[shi->thread];
2374         }
2375         else {
2376                 isec.last_hit = NULL;
2377         }
2378         
2379         if(lar->type==LA_SUN || lar->type==LA_HEMI) {
2380                 /* jitter and QMC sampling add a displace vector to the lamp position
2381                  * that's incorrect because a SUN lamp does not has an exact position
2382                  * and the displace should be done at the ray vector instead of the
2383                  * lamp position.
2384                  * This is easily verified by noticing that shadows of SUN lights change
2385                  * with the scene BB.
2386                  * 
2387                  * This was detected during SoC 2009 - Raytrace Optimization, but to keep
2388                  * consistency with older render code it wasn't removed.
2389                  * 
2390                  * If the render code goes through some recode/serious bug-fix then this
2391                  * is something to consider!
2392                  */
2393                 lampco[0]= shi->co[0] - R.maxdist*lar->vec[0];
2394                 lampco[1]= shi->co[1] - R.maxdist*lar->vec[1];
2395                 lampco[2]= shi->co[2] - R.maxdist*lar->vec[2];
2396         }
2397         else {
2398                 VECCOPY(lampco, lar->co);
2399         }
2400         
2401         if (ELEM(lar->ray_samp_method, LA_SAMP_HALTON, LA_SAMP_HAMMERSLEY)) {
2402                 
2403                 ray_shadow_qmc(shi, lar, lampco, shadfac, &isec);
2404                 
2405         } else {
2406                 if(lar->ray_totsamp<2) {
2407                         
2408                         isec.orig.ob   = shi->obi;
2409                         isec.orig.face = shi->vlr;
2410                         
2411                         shadfac[3]= 1.0f; // 1.0=full light
2412                         
2413                         /* set up isec vec */
2414                         VECCOPY(isec.start, shi->co);
2415                         VECSUB(isec.vec, lampco, isec.start);
2416                         isec.labda = 1.0f;
2417
2418                         if(isec.mode==RE_RAY_SHADOW_TRA) {
2419                                 /* isec.col is like shadfac, so defines amount of light (0.0 is full shadow) */
2420                                 isec.col[0]= isec.col[1]= isec.col[2]=  1.0f;
2421                                 isec.col[3]= 1.0f;
2422
2423                                 ray_trace_shadow_tra(&isec, shi, DEPTH_SHADOW_TRA, 0);
2424                                 QUATCOPY(shadfac, isec.col);
2425                         }
2426                         else if(RE_rayobject_raycast(R.raytree, &isec))
2427                                 shadfac[3]= 0.0f;
2428                 }
2429                 else {
2430                         ray_shadow_jitter(shi, lar, lampco, shadfac, &isec);
2431                 }
2432         }
2433                 
2434         /* for first hit optim, set last interesected shadow face */
2435         if(shi->depth==0) {
2436                 lar->last_hit[shi->thread] = isec.last_hit;
2437         }
2438
2439 }
2440
2441 #if 0
2442 /* only when face points away from lamp, in direction of lamp, trace ray and find first exit point */
2443 static void ray_translucent(ShadeInput *shi, LampRen *lar, float *distfac, float *co)
2444 {
2445         Isect isec;
2446         float lampco[3];
2447         
2448         assert(0);
2449         
2450         /* setup isec */
2451         RE_RC_INIT(isec, *shi);
2452         isec.mode= RE_RAY_SHADOW_TRA;
2453         isec.hint = 0;
2454         
2455         if(lar->mode & LA_LAYER) isec.lay= lar->lay; else isec.lay= -1;
2456         
2457         if(lar->type==LA_SUN || lar->type==LA_HEMI) {
2458                 lampco[0]= shi->co[0] - RE_RAYTRACE_MAXDIST*lar->vec[0];
2459                 lampco[1]= shi->co[1] - RE_RAYTRACE_MAXDIST*lar->vec[1];
2460                 lampco[2]= shi->co[2] - RE_RAYTRACE_MAXDIST*lar->vec[2];
2461         }
2462         else {
2463                 VECCOPY(lampco, lar->co);
2464         }
2465         
2466         isec.orig.ob   = shi->obi;
2467         isec.orig.face = shi->vlr;
2468         
2469         /* set up isec vec */
2470         VECCOPY(isec.start, shi->co);
2471         VECCOPY(isec.end, lampco);
2472         
2473         if(RE_rayobject_raycast(R.raytree, &isec)) {
2474                 /* we got a face */
2475                 
2476                 /* render co */
2477                 co[0]= isec.start[0]+isec.labda*(isec.vec[0]);
2478                 co[1]= isec.start[1]+isec.labda*(isec.vec[1]);
2479                 co[2]= isec.start[2]+isec.labda*(isec.vec[2]);
2480                 
2481                 *distfac= VecLength(isec.vec);
2482         }
2483         else
2484                 *distfac= 0.0f;
2485 }
2486
2487 #endif
2488