Previous fix was in wrong part of code... not alpha adding was wrong, but
[blender-staging.git] / source / blender / render / intern / source / rendercore.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) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * Contributors: Hos, Robert Wenzlaff.
24  * Contributors: 2004/2005/2006 Blender Foundation, full recode
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 /* system includes */
30 #include <math.h>
31 #include <string.h>
32 #include <stdlib.h>
33
34 /* External modules: */
35 #include "MTC_matrixops.h"
36 #include "BLI_arithb.h"
37 #include "BLI_blenlib.h"
38 #include "BLI_rand.h"
39 #include "BLI_jitter.h"
40
41 #include "BKE_utildefines.h"
42
43 #include "DNA_group_types.h"
44 #include "DNA_image_types.h"
45 #include "DNA_lamp_types.h"
46 #include "DNA_material_types.h"
47 #include "DNA_mesh_types.h"
48 #include "DNA_meshdata_types.h"
49 #include "DNA_object_types.h"
50 #include "DNA_texture_types.h"
51
52 #include "BKE_global.h"
53 #include "BKE_material.h"
54 #include "BKE_node.h"
55 #include "BKE_texture.h"
56
57 /* local include */
58 #include "renderpipeline.h"
59 #include "render_types.h"
60 #include "renderdatabase.h"
61 #include "pixelblending.h"
62 #include "pixelshading.h"
63 #include "gammaCorrectionTables.h"
64 #include "shadbuf.h"
65 #include "zbuf.h"
66
67 #include "texture.h"
68
69 /* own include */
70 #include "rendercore.h"
71
72
73 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
74 /* defined in pipeline.c, is hardcopy of active dynamic allocated Render */
75 /* only to be used here in this file, it's for speed */
76 extern struct Render R;
77 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
78
79 /* x and y are current pixels in rect to be rendered */
80 /* do not normalize! */
81 void calc_view_vector(float *view, float x, float y)
82 {
83
84         if(R.r.mode & R_ORTHO) {
85                 view[0]= view[1]= 0.0;
86         }
87         else {
88                 /* move x and y to real viewplane coords */
89                 x= (x/(float)R.winx);
90                 view[0]= R.viewplane.xmin + x*(R.viewplane.xmax - R.viewplane.xmin);
91                 
92                 y= (y/(float)R.winy);
93                 view[1]= R.viewplane.ymin + y*(R.viewplane.ymax - R.viewplane.ymin);
94                 
95 //              if(R.flag & R_SEC_FIELD) {
96 //                      if(R.r.mode & R_ODDFIELD) view[1]= (y+R.ystart)*R.ycor;
97 //                      else view[1]= (y+R.ystart+1.0)*R.ycor;
98 //              }
99 //              else view[1]= (y+R.ystart+R.bluroffsy+0.5)*R.ycor;
100         }
101         
102         view[2]= -R.clipsta;
103         
104         if(R.r.mode & R_PANORAMA) {
105                 float u= view[0]; float v= view[2];
106                 view[0]= R.panoco*u + R.panosi*v;
107                 view[2]= -R.panosi*u + R.panoco*v;
108         }
109
110 }
111
112 #if 0
113 static void fogcolor(float *colf, float *rco, float *view)
114 {
115         float alpha, stepsize, startdist, dist, hor[4], zen[3], vec[3], dview[3];
116         float div=0.0f, distfac;
117         
118         hor[0]= R.wrld.horr; hor[1]= R.wrld.horg; hor[2]= R.wrld.horb;
119         zen[0]= R.wrld.zenr; zen[1]= R.wrld.zeng; zen[2]= R.wrld.zenb;
120         
121         VECCOPY(vec, rco);
122         
123         /* we loop from cur coord to mist start in steps */
124         stepsize= 1.0f;
125         
126         div= ABS(view[2]);
127         dview[0]= view[0]/(stepsize*div);
128         dview[1]= view[1]/(stepsize*div);
129         dview[2]= -stepsize;
130
131         startdist= -rco[2] + BLI_frand();
132         for(dist= startdist; dist>R.wrld.miststa; dist-= stepsize) {
133                 
134                 hor[0]= R.wrld.horr; hor[1]= R.wrld.horg; hor[2]= R.wrld.horb;
135                 alpha= 1.0f;
136                 do_sky_tex(vec, vec, NULL, hor, zen, &alpha);
137                 
138                 distfac= (dist-R.wrld.miststa)/R.wrld.mistdist;
139                 
140                 hor[3]= hor[0]*distfac*distfac;
141                 
142                 /* premul! */
143                 alpha= hor[3];
144                 hor[0]= hor[0]*alpha;
145                 hor[1]= hor[1]*alpha;
146                 hor[2]= hor[2]*alpha;
147                 addAlphaOverFloat(colf, hor);
148                 
149                 VECSUB(vec, vec, dview);
150         }       
151 }
152 #endif
153
154 float mistfactor(float zcor, float *co) /* dist en height, return alpha */
155 {
156         float fac, hi;
157         
158         fac= zcor - R.wrld.miststa;     /* zcor is calculated per pixel */
159
160         /* fac= -co[2]-R.wrld.miststa; */
161
162         if(fac>0.0) {
163                 if(fac< R.wrld.mistdist) {
164                         
165                         fac= (fac/(R.wrld.mistdist));
166                         
167                         if(R.wrld.mistype==0) fac*= fac;
168                         else if(R.wrld.mistype==1);
169                         else fac= sqrt(fac);
170                 }
171                 else fac= 1.0;
172         }
173         else fac= 0.0;
174         
175         /* height switched off mist */
176         if(R.wrld.misthi!=0.0 && fac!=0.0) {
177                 /* at height misthi the mist is completely gone */
178
179                 hi= R.viewinv[0][2]*co[0]+R.viewinv[1][2]*co[1]+R.viewinv[2][2]*co[2]+R.viewinv[3][2];
180                 
181                 if(hi>R.wrld.misthi) fac= 0.0;
182                 else if(hi>0.0) {
183                         hi= (R.wrld.misthi-hi)/R.wrld.misthi;
184                         fac*= hi*hi;
185                 }
186         }
187
188         return (1.0-fac)* (1.0-R.wrld.misi);    
189 }
190
191 static void spothalo(struct LampRen *lar, ShadeInput *shi, float *intens)
192 {
193         double a, b, c, disc, nray[3], npos[3];
194         float t0, t1 = 0.0, t2= 0.0, t3, haint;
195         float p1[3], p2[3], ladist, maxz = 0.0, maxy = 0.0;
196         int snijp, doclip=1, use_yco=0;
197         int ok1=0, ok2=0;
198         
199         *intens= 0.0;
200         haint= lar->haint;
201         
202         if(R.r.mode & R_ORTHO) {
203                 /* camera pos (view vector) cannot be used... */
204                 /* camera position (cox,coy,0) rotate around lamp */
205                 p1[0]= shi->co[0]-lar->co[0];
206                 p1[1]= shi->co[1]-lar->co[1];
207                 p1[2]= -lar->co[2];
208                 MTC_Mat3MulVecfl(lar->imat, p1);
209                 VECCOPY(npos, p1);      // npos is double!
210         }
211         else {
212                 VECCOPY(npos, lar->sh_invcampos);       /* in initlamp calculated */
213         }
214         
215         /* rotate view */
216         VECCOPY(nray, shi->view);
217         MTC_Mat3MulVecd(lar->imat, nray);
218         
219         if(R.wrld.mode & WO_MIST) {
220                 /* patchy... */
221                 haint *= mistfactor(-lar->co[2], lar->co);
222                 if(haint==0.0) {
223                         return;
224                 }
225         }
226
227
228         /* rotate maxz */
229         if(shi->co[2]==0.0) doclip= 0;  /* for when halo at sky */
230         else {
231                 p1[0]= shi->co[0]-lar->co[0];
232                 p1[1]= shi->co[1]-lar->co[1];
233                 p1[2]= shi->co[2]-lar->co[2];
234         
235                 maxz= lar->imat[0][2]*p1[0]+lar->imat[1][2]*p1[1]+lar->imat[2][2]*p1[2];
236                 maxz*= lar->sh_zfac;
237                 maxy= lar->imat[0][1]*p1[0]+lar->imat[1][1]*p1[1]+lar->imat[2][1]*p1[2];
238
239                 if( fabs(nray[2]) <0.000001 ) use_yco= 1;
240         }
241         
242         /* scale z to make sure volume is normalized */ 
243         nray[2]*= lar->sh_zfac;
244         /* nray does not need normalization */
245         
246         ladist= lar->sh_zfac*lar->dist;
247         
248         /* solve */
249         a = nray[0] * nray[0] + nray[1] * nray[1] - nray[2]*nray[2];
250         b = nray[0] * npos[0] + nray[1] * npos[1] - nray[2]*npos[2];
251         c = npos[0] * npos[0] + npos[1] * npos[1] - npos[2]*npos[2];
252
253         snijp= 0;
254         if (fabs(a) < 0.00000001) {
255                 /*
256                  * Only one intersection point...
257                  */
258                 return;
259         }
260         else {
261                 disc = b*b - a*c;
262                 
263                 if(disc==0.0) {
264                         t1=t2= (-b)/ a;
265                         snijp= 2;
266                 }
267                 else if (disc > 0.0) {
268                         disc = sqrt(disc);
269                         t1 = (-b + disc) / a;
270                         t2 = (-b - disc) / a;
271                         snijp= 2;
272                 }
273         }
274         if(snijp==2) {
275                 /* sort */
276                 if(t1>t2) {
277                         a= t1; t1= t2; t2= a;
278                 }
279
280                 /* z of intersection points with diabolo */
281                 p1[2]= npos[2] + t1*nray[2];
282                 p2[2]= npos[2] + t2*nray[2];
283
284                 /* evaluate both points */
285                 if(p1[2]<=0.0) ok1= 1;
286                 if(p2[2]<=0.0 && t1!=t2) ok2= 1;
287                 
288                 /* at least 1 point with negative z */
289                 if(ok1==0 && ok2==0) return;
290                 
291                 /* intersction point with -ladist, the bottom of the cone */
292                 if(use_yco==0) {
293                         t3= (-ladist-npos[2])/nray[2];
294                                 
295                         /* de we have to replace one of the intersection points? */
296                         if(ok1) {
297                                 if(p1[2]<-ladist) t1= t3;
298                         }
299                         else {
300                                 ok1= 1;
301                                 t1= t3;
302                         }
303                         if(ok2) {
304                                 if(p2[2]<-ladist) t2= t3;
305                         }
306                         else {
307                                 ok2= 1;
308                                 t2= t3;
309                         }
310                 }
311                 else if(ok1==0 || ok2==0) return;
312                 
313                 /* at least 1 visible interesction point */
314                 if(t1<0.0 && t2<0.0) return;
315                 
316                 if(t1<0.0) t1= 0.0;
317                 if(t2<0.0) t2= 0.0;
318                 
319                 if(t1==t2) return;
320                 
321                 /* sort again to be sure */
322                 if(t1>t2) {
323                         a= t1; t1= t2; t2= a;
324                 }
325                 
326                 /* calculate t0: is the maximum visible z (when halo is intersected by face) */ 
327                 if(doclip) {
328                         if(use_yco==0) t0= (maxz-npos[2])/nray[2];
329                         else t0= (maxy-npos[1])/nray[1];
330
331                         if(t0<t1) return;
332                         if(t0<t2) t2= t0;
333                 }
334
335                 /* calc points */
336                 p1[0]= npos[0] + t1*nray[0];
337                 p1[1]= npos[1] + t1*nray[1];
338                 p1[2]= npos[2] + t1*nray[2];
339                 p2[0]= npos[0] + t2*nray[0];
340                 p2[1]= npos[1] + t2*nray[1];
341                 p2[2]= npos[2] + t2*nray[2];
342                 
343                         
344                 /* now we have 2 points, make three lengths with it */
345                 
346                 a= sqrt(p1[0]*p1[0]+p1[1]*p1[1]+p1[2]*p1[2]);
347                 b= sqrt(p2[0]*p2[0]+p2[1]*p2[1]+p2[2]*p2[2]);
348                 c= VecLenf(p1, p2);
349                 
350                 a/= ladist;
351                 a= sqrt(a);
352                 b/= ladist; 
353                 b= sqrt(b);
354                 c/= ladist;
355                 
356                 *intens= c*( (1.0-a)+(1.0-b) );
357
358                 /* WATCH IT: do not clip a,b en c at 1.0, this gives nasty little overflows
359                         at the edges (especially with narrow halos) */
360                 if(*intens<=0.0) return;
361
362                 /* soft area */
363                 /* not needed because t0 has been used for p1/p2 as well */
364                 /* if(doclip && t0<t2) { */
365                 /*      *intens *= (t0-t1)/(t2-t1); */
366                 /* } */
367                 
368                 *intens *= haint;
369                 
370                 if(lar->shb && lar->shb->shadhalostep) {
371                         *intens *= shadow_halo(lar, p1, p2);
372                 }
373                 
374         }
375 }
376
377 static void renderspothalo(ShadeInput *shi, float *col, float alpha)
378 {
379         GroupObject *go;
380         LampRen *lar;
381         float i;
382         
383         if(alpha==0.0f) return;
384
385         for(go=R.lights.first; go; go= go->next) {
386                 lar= go->lampren;
387                 
388                 if(lar->type==LA_SPOT && (lar->mode & LA_HALO) && lar->haint>0) {
389         
390                         spothalo(lar, shi, &i);
391                         if(i>0.0) {
392                                 col[3]+= i*alpha;                       // all premul
393                                 col[0]+= i*lar->r*alpha;
394                                 col[1]+= i*lar->g*alpha;
395                                 col[2]+= i*lar->b*alpha;        
396                         }
397                 }
398         }
399         /* clip alpha, is needed for unified 'alpha threshold' (vanillaRenderPipe.c) */
400         if(col[3]>1.0) col[3]= 1.0;
401 }
402
403
404
405 /* also used in zbuf.c */
406 int count_mask(unsigned short mask)
407 {
408         if(R.samples)
409                 return (R.samples->cmask[mask & 255]+R.samples->cmask[mask>>8]);
410         return 0;
411 }
412
413 static int calchalo_z(HaloRen *har, int zz)
414 {
415         
416         if(har->type & HA_ONLYSKY) {
417                 if(zz!=0x7FFFFFFF) zz= - 0x7FFFFF;
418         }
419         else {
420                 zz= (zz>>8);
421         }
422         return zz;
423 }
424
425 static void halo_pixelstruct(HaloRen *har, float *rb, float dist, float xn, float yn, PixStr *ps)
426 {
427         float col[4], accol[4];
428         int amount, amountm, zz, flarec;
429         
430         amount= 0;
431         accol[0]=accol[1]=accol[2]=accol[3]= 0.0;
432         flarec= har->flarec;
433         
434         while(ps) {
435                 amountm= count_mask(ps->mask);
436                 amount+= amountm;
437                 
438                 zz= calchalo_z(har, ps->z);
439                 if(zz> har->zs) {
440                         float fac;
441                         
442                         shadeHaloFloat(har, col, zz, dist, xn, yn, flarec);
443                         fac= ((float)amountm)/(float)R.osa;
444                         accol[0]+= fac*col[0];
445                         accol[1]+= fac*col[1];
446                         accol[2]+= fac*col[2];
447                         accol[3]+= fac*col[3];
448                         flarec= 0;
449                 }
450                 
451                 ps= ps->next;
452         }
453         /* now do the sky sub-pixels */
454         amount= R.osa-amount;
455         if(amount) {
456                 float fac;
457
458                 shadeHaloFloat(har, col, 0x7FFFFF, dist, xn, yn, flarec);
459                 fac= ((float)amount)/(float)R.osa;
460                 accol[0]+= fac*col[0];
461                 accol[1]+= fac*col[1];
462                 accol[2]+= fac*col[2];
463                 accol[3]+= fac*col[3];
464         }
465         col[0]= accol[0];
466         col[1]= accol[1];
467         col[2]= accol[2];
468         col[3]= accol[3];
469         
470         addalphaAddfacFloat(rb, col, har->add);
471         
472 }
473
474 static void halo_tile(RenderPart *pa, float *pass, unsigned int lay)
475 {
476         HaloRen *har = NULL;
477         rcti disprect= pa->disprect;
478         float dist, xsq, ysq, xn, yn, *rb;
479         float col[4];
480         long *rd= NULL;
481         int a, *rz, zz, y;
482         short minx, maxx, miny, maxy, x;
483
484         for(a=0; a<R.tothalo; a++) {
485                 if((a & 255)==0) {
486                         if(R.test_break() ) break; 
487                         har= R.bloha[a>>8];
488                 }
489                 else har++;
490
491                 /* layer test, clip halo with y */
492                 if((har->lay & lay)==0);
493                 else if(disprect.ymin > har->maxy);
494                 else if(disprect.ymax < har->miny);
495                 else {
496                         
497                         minx= floor(har->xs-har->rad);
498                         maxx= ceil(har->xs+har->rad);
499                         
500                         if(disprect.xmin > maxx);
501                         else if(disprect.xmax < minx);
502                         else {
503                                 
504                                 minx= MAX2(minx, disprect.xmin);
505                                 maxx= MIN2(maxx, disprect.xmax);
506                         
507                                 miny= MAX2(har->miny, disprect.ymin);
508                                 maxy= MIN2(har->maxy, disprect.ymax);
509                         
510                                 for(y=miny; y<maxy; y++) {
511                                         int rectofs= (y-disprect.ymin)*pa->rectx + (minx - disprect.xmin);
512                                         rb= pass + 4*rectofs;
513                                         rz= pa->rectz + rectofs;
514                                         
515                                         if(pa->rectdaps)
516                                                 rd= pa->rectdaps + rectofs;
517                                         
518                                         yn= (y-har->ys)*R.ycor;
519                                         ysq= yn*yn;
520                                         
521                                         for(x=minx; x<maxx; x++, rb+=4, rz++) {
522                                                 xn= x- har->xs;
523                                                 xsq= xn*xn;
524                                                 dist= xsq+ysq;
525                                                 if(dist<har->radsq) {
526                                                         if(rd && *rd) {
527                                                                 halo_pixelstruct(har, rb, dist, xn, yn, (PixStr *)*rd);
528                                                         }
529                                                         else {
530                                                                 zz= calchalo_z(har, *rz);
531                                                                 if(zz> har->zs) {
532                                                                         shadeHaloFloat(har, col, zz, dist, xn, yn, har->flarec);
533                                                                         addalphaAddfacFloat(rb, col, har->add);
534                                                                 }
535                                                         }
536                                                 }
537                                                 if(rd) rd++;
538                                         }
539                                 }
540                         }
541                 }
542         }
543 }
544
545 /* ---------------- shaders ----------------------- */
546
547 static double Normalise_d(double *n)
548 {
549         double d;
550         
551         d= n[0]*n[0]+n[1]*n[1]+n[2]*n[2];
552
553         if(d>0.00000000000000001) {
554                 d= sqrt(d);
555
556                 n[0]/=d; 
557                 n[1]/=d; 
558                 n[2]/=d;
559         } else {
560                 n[0]=n[1]=n[2]= 0.0;
561                 d= 0.0;
562         }
563         return d;
564 }
565
566 /* mix of 'real' fresnel and allowing control. grad defines blending gradient */
567 float fresnel_fac(float *view, float *vn, float grad, float fac)
568 {
569         float t1, t2;
570         
571         if(fac==0.0) return 1.0;
572         
573         t1= (view[0]*vn[0] + view[1]*vn[1] + view[2]*vn[2]);
574         if(t1>0.0)  t2= 1.0+t1;
575         else t2= 1.0-t1;
576         
577         t2= grad + (1.0-grad)*pow(t2, fac);
578         
579         if(t2<0.0) return 0.0;
580         else if(t2>1.0) return 1.0;
581         return t2;
582 }
583
584 static double saacos_d(double fac)
585 {
586         if(fac<= -1.0f) return M_PI;
587         else if(fac>=1.0f) return 0.0;
588         else return acos(fac);
589 }
590
591 /* Stoke's form factor. Need doubles here for extreme small area sizes */
592 static float area_lamp_energy(float *co, float *vn, LampRen *lar)
593 {
594         double fac;
595         double vec[4][3];       /* vectors of rendered co to vertices lamp */
596         double cross[4][3];     /* cross products of this */
597         double rad[4];          /* angles between vecs */
598
599         VECSUB(vec[0], co, lar->area[0]);
600         VECSUB(vec[1], co, lar->area[1]);
601         VECSUB(vec[2], co, lar->area[2]);
602         VECSUB(vec[3], co, lar->area[3]);
603         
604         Normalise_d(vec[0]);
605         Normalise_d(vec[1]);
606         Normalise_d(vec[2]);
607         Normalise_d(vec[3]);
608
609         /* cross product */
610         CROSS(cross[0], vec[0], vec[1]);
611         CROSS(cross[1], vec[1], vec[2]);
612         CROSS(cross[2], vec[2], vec[3]);
613         CROSS(cross[3], vec[3], vec[0]);
614
615         Normalise_d(cross[0]);
616         Normalise_d(cross[1]);
617         Normalise_d(cross[2]);
618         Normalise_d(cross[3]);
619
620         /* angles */
621         rad[0]= vec[0][0]*vec[1][0]+ vec[0][1]*vec[1][1]+ vec[0][2]*vec[1][2];
622         rad[1]= vec[1][0]*vec[2][0]+ vec[1][1]*vec[2][1]+ vec[1][2]*vec[2][2];
623         rad[2]= vec[2][0]*vec[3][0]+ vec[2][1]*vec[3][1]+ vec[2][2]*vec[3][2];
624         rad[3]= vec[3][0]*vec[0][0]+ vec[3][1]*vec[0][1]+ vec[3][2]*vec[0][2];
625
626         rad[0]= saacos_d(rad[0]);
627         rad[1]= saacos_d(rad[1]);
628         rad[2]= saacos_d(rad[2]);
629         rad[3]= saacos_d(rad[3]);
630
631         /* Stoke formula */
632         fac=  rad[0]*(vn[0]*cross[0][0]+ vn[1]*cross[0][1]+ vn[2]*cross[0][2]);
633         fac+= rad[1]*(vn[0]*cross[1][0]+ vn[1]*cross[1][1]+ vn[2]*cross[1][2]);
634         fac+= rad[2]*(vn[0]*cross[2][0]+ vn[1]*cross[2][1]+ vn[2]*cross[2][2]);
635         fac+= rad[3]*(vn[0]*cross[3][0]+ vn[1]*cross[3][1]+ vn[2]*cross[3][2]);
636
637         if(fac<=0.0) return 0.0;
638         return pow(fac*lar->areasize, lar->k);  // corrected for buttons size and lar->dist^2
639 }
640
641 static float spec(float inp, int hard)  
642 {
643         float b1;
644         
645         if(inp>=1.0) return 1.0;
646         else if (inp<=0.0) return 0.0;
647         
648         b1= inp*inp;
649         /* avoid FPE */
650         if(b1<0.01) b1= 0.01;   
651         
652         if((hard & 1)==0)  inp= 1.0;
653         if(hard & 2)  inp*= b1;
654         b1*= b1;
655         if(hard & 4)  inp*= b1;
656         b1*= b1;
657         if(hard & 8)  inp*= b1;
658         b1*= b1;
659         if(hard & 16) inp*= b1;
660         b1*= b1;
661
662         /* avoid FPE */
663         if(b1<0.001) b1= 0.0;   
664
665         if(hard & 32) inp*= b1;
666         b1*= b1;
667         if(hard & 64) inp*=b1;
668         b1*= b1;
669         if(hard & 128) inp*=b1;
670
671         if(b1<0.001) b1= 0.0;   
672
673         if(hard & 256) {
674                 b1*= b1;
675                 inp*=b1;
676         }
677
678         return inp;
679 }
680
681 static float Phong_Spec( float *n, float *l, float *v, int hard, int tangent )
682 {
683         float h[3];
684         float rslt;
685         
686         h[0] = l[0] + v[0];
687         h[1] = l[1] + v[1];
688         h[2] = l[2] + v[2];
689         Normalise(h);
690         
691         rslt = h[0]*n[0] + h[1]*n[1] + h[2]*n[2];
692         if(tangent) rslt= sasqrt(1.0 - rslt*rslt);
693                 
694         if( rslt > 0.0 ) rslt= spec(rslt, hard);
695         else rslt = 0.0;
696         
697         return rslt;
698 }
699
700
701 /* reduced cook torrance spec (for off-specular peak) */
702 static float CookTorr_Spec(float *n, float *l, float *v, int hard, int tangent)
703 {
704         float i, nh, nv, h[3];
705
706         h[0]= v[0]+l[0];
707         h[1]= v[1]+l[1];
708         h[2]= v[2]+l[2];
709         Normalise(h);
710
711         nh= n[0]*h[0]+n[1]*h[1]+n[2]*h[2];
712         if(tangent) nh= sasqrt(1.0 - nh*nh);
713         else if(nh<0.0) return 0.0;
714         
715         nv= n[0]*v[0]+n[1]*v[1]+n[2]*v[2];
716         if(tangent) nv= sasqrt(1.0 - nv*nv);
717         else if(nv<0.0) nv= 0.0;
718
719         i= spec(nh, hard);
720
721         i= i/(0.1+nv);
722         return i;
723 }
724
725 /* Blinn spec */
726 static float Blinn_Spec(float *n, float *l, float *v, float refrac, float spec_power, int tangent)
727 {
728         float i, nh, nv, nl, vh, h[3];
729         float a, b, c, g=0.0, p, f, ang;
730
731         if(refrac < 1.0) return 0.0;
732         if(spec_power == 0.0) return 0.0;
733         
734         /* conversion from 'hardness' (1-255) to 'spec_power' (50 maps at 0.1) */
735         if(spec_power<100.0)
736                 spec_power= sqrt(1.0/spec_power);
737         else spec_power= 10.0/spec_power;
738         
739         h[0]= v[0]+l[0];
740         h[1]= v[1]+l[1];
741         h[2]= v[2]+l[2];
742         Normalise(h);
743
744         nh= n[0]*h[0]+n[1]*h[1]+n[2]*h[2]; /* Dot product between surface normal and half-way vector */
745         if(tangent) nh= sasqrt(1.0f - nh*nh);
746         else if(nh<0.0) return 0.0;
747
748         nv= n[0]*v[0]+n[1]*v[1]+n[2]*v[2]; /* Dot product between surface normal and view vector */
749         if(tangent) nv= sasqrt(1.0f - nv*nv);
750         if(nv<=0.0) nv= 0.01;                           /* hrms... */
751
752         nl= n[0]*l[0]+n[1]*l[1]+n[2]*l[2]; /* Dot product between surface normal and light vector */
753         if(tangent) nl= sasqrt(1.0f - nl*nl);
754         if(nl<=0.0) {
755                 return 0.0;
756         }
757
758         vh= v[0]*h[0]+v[1]*h[1]+v[2]*h[2]; /* Dot product between view vector and half-way vector */
759         if(vh<=0.0) vh= 0.01;
760
761         a = 1.0;
762         b = (2.0*nh*nv)/vh;
763         c = (2.0*nh*nl)/vh;
764
765         if( a < b && a < c ) g = a;
766         else if( b < a && b < c ) g = b;
767         else if( c < a && c < b ) g = c;
768
769         p = sqrt( (double)((refrac * refrac)+(vh*vh)-1.0) );
770         f = (((p-vh)*(p-vh))/((p+vh)*(p+vh)))*(1+((((vh*(p+vh))-1.0)*((vh*(p+vh))-1.0))/(((vh*(p-vh))+1.0)*((vh*(p-vh))+1.0))));
771         ang = saacos(nh);
772
773         i= f * g * exp((double)(-(ang*ang) / (2.0*spec_power*spec_power)));
774         if(i<0.0) i= 0.0;
775         
776         return i;
777 }
778
779 /* cartoon render spec */
780 static float Toon_Spec( float *n, float *l, float *v, float size, float smooth, int tangent)
781 {
782         float h[3];
783         float ang;
784         float rslt;
785         
786         h[0] = l[0] + v[0];
787         h[1] = l[1] + v[1];
788         h[2] = l[2] + v[2];
789         Normalise(h);
790         
791         rslt = h[0]*n[0] + h[1]*n[1] + h[2]*n[2];
792         if(tangent) rslt = sasqrt(1.0f - rslt*rslt);
793         
794         ang = saacos( rslt ); 
795         
796         if( ang < size ) rslt = 1.0;
797         else if( ang >= (size + smooth) || smooth == 0.0 ) rslt = 0.0;
798         else rslt = 1.0 - ((ang - size) / smooth);
799         
800         return rslt;
801 }
802
803 /* Ward isotropic gaussian spec */
804 static float WardIso_Spec( float *n, float *l, float *v, float rms, int tangent)
805 {
806         float i, nh, nv, nl, h[3], angle, alpha;
807
808
809         /* half-way vector */
810         h[0] = l[0] + v[0];
811         h[1] = l[1] + v[1];
812         h[2] = l[2] + v[2];
813         Normalise(h);
814
815         nh = n[0]*h[0]+n[1]*h[1]+n[2]*h[2]; /* Dot product between surface normal and half-way vector */
816         if(tangent) nh = sasqrt(1.0f - nh*nh);
817         if(nh<=0.0) nh = 0.001f;
818         
819         nv = n[0]*v[0]+n[1]*v[1]+n[2]*v[2]; /* Dot product between surface normal and view vector */
820         if(tangent) nv = sasqrt(1.0f - nv*nv);
821         if(nv<=0.0) nv = 0.001f;
822
823         nl = n[0]*l[0]+n[1]*l[1]+n[2]*l[2]; /* Dot product between surface normal and light vector */
824         if(tangent) nl = sasqrt(1.0f - nl*nl);
825         if(nl<=0.0) nl = 0.001;
826
827         angle = tan(saacos(nh));
828         alpha = MAX2(rms,0.001);
829
830         i= nl * (1.0/(4*M_PI*alpha*alpha)) * (exp( -(angle*angle)/(alpha*alpha))/(sqrt(nv*nl)));
831
832         return i;
833 }
834
835 /* cartoon render diffuse */
836 static float Toon_Diff( float *n, float *l, float *v, float size, float smooth )
837 {
838         float rslt, ang;
839
840         rslt = n[0]*l[0] + n[1]*l[1] + n[2]*l[2];
841
842         ang = saacos( (double)(rslt) );
843
844         if( ang < size ) rslt = 1.0;
845         else if( ang >= (size + smooth) || smooth == 0.0 ) rslt = 0.0;
846         else rslt = 1.0 - ((ang - size) / smooth);
847
848         return rslt;
849 }
850
851 /* Oren Nayar diffuse */
852
853 /* 'nl' is either dot product, or return value of area light */
854 /* in latter case, only last multiplication uses 'nl' */
855 static float OrenNayar_Diff(float nl, float *n, float *l, float *v, float rough )
856 {
857         float i, nh, nv, vh, realnl, h[3];
858         float a, b, t, A, B;
859         float Lit_A, View_A, Lit_B[3], View_B[3];
860         
861         h[0]= v[0]+l[0];
862         h[1]= v[1]+l[1];
863         h[2]= v[2]+l[2];
864         Normalise(h);
865         
866         nh= n[0]*h[0]+n[1]*h[1]+n[2]*h[2]; /* Dot product between surface normal and half-way vector */
867         if(nh<0.0) nh = 0.0;
868         
869         nv= n[0]*v[0]+n[1]*v[1]+n[2]*v[2]; /* Dot product between surface normal and view vector */
870         if(nv<=0.0) nv= 0.0;
871         
872         realnl= n[0]*l[0]+n[1]*l[1]+n[2]*l[2]; /* Dot product between surface normal and light vector */
873         if(realnl<=0.0) return 0.0;
874         if(nl<0.0) return 0.0;          /* value from area light */
875         
876         vh= v[0]*h[0]+v[1]*h[1]+v[2]*h[2]; /* Dot product between view vector and halfway vector */
877         if(vh<=0.0) vh= 0.0;
878         
879         Lit_A = saacos(realnl);
880         View_A = saacos( nv );
881         
882         Lit_B[0] = l[0] - (realnl * n[0]);
883         Lit_B[1] = l[1] - (realnl * n[1]);
884         Lit_B[2] = l[2] - (realnl * n[2]);
885         Normalise( Lit_B );
886         
887         View_B[0] = v[0] - (nv * n[0]);
888         View_B[1] = v[1] - (nv * n[1]);
889         View_B[2] = v[2] - (nv * n[2]);
890         Normalise( View_B );
891         
892         t = Lit_B[0]*View_B[0] + Lit_B[1]*View_B[1] + Lit_B[2]*View_B[2];
893         if( t < 0 ) t = 0;
894         
895         if( Lit_A > View_A ) {
896                 a = Lit_A;
897                 b = View_A;
898         }
899         else {
900                 a = View_A;
901                 b = Lit_A;
902         }
903         
904         A = 1 - (0.5 * ((rough * rough) / ((rough * rough) + 0.33)));
905         B = 0.45 * ((rough * rough) / ((rough * rough) + 0.09));
906         
907         b*= 0.95;       /* prevent tangens from shooting to inf, 'nl' can be not a dot product here. */
908                                 /* overflow only happens with extreme size area light, and higher roughness */
909         i = nl * ( A + ( B * t * sin(a) * tan(b) ) );
910         
911         return i;
912 }
913
914 /* Minnaert diffuse */
915 static float Minnaert_Diff(float nl, float *n, float *v, float darkness)
916 {
917
918         float i, nv;
919
920         /* nl = dot product between surface normal and light vector */
921         if (nl <= 0.0)
922                 return 0;
923
924         /* nv = dot product between surface normal and view vector */
925         nv = n[0]*v[0]+n[1]*v[1]+n[2]*v[2];
926         if (nv < 0.0)
927                 nv = 0;
928
929         if (darkness <= 1)
930                 i = nl * pow(MAX2(nv*nl, 0.1), (darkness - 1) ); /*The Real model*/
931         else
932                 i = nl * pow( (1.001 - nv), (darkness  - 1) ); /*Nvidia model*/
933
934         return i;
935 }
936
937 static float Fresnel_Diff(float *vn, float *lv, float *view, float fac_i, float fac)
938 {
939         return fresnel_fac(lv, vn, fac_i, fac);
940 }
941
942 /* --------------------------------------------- */
943 /* also called from texture.c */
944 void calc_R_ref(ShadeInput *shi)
945 {
946         float i;
947
948         /* shi->vn dot shi->view */
949         i= -2*(shi->vn[0]*shi->view[0]+shi->vn[1]*shi->view[1]+shi->vn[2]*shi->view[2]);
950
951         shi->ref[0]= (shi->view[0]+i*shi->vn[0]);
952         shi->ref[1]= (shi->view[1]+i*shi->vn[1]);
953         shi->ref[2]= (shi->view[2]+i*shi->vn[2]);
954         if(shi->osatex) {
955                 if(shi->vlr->flag & R_SMOOTH) {
956                         i= -2*( (shi->vn[0]+shi->dxno[0])*(shi->view[0]+shi->dxview) +
957                                 (shi->vn[1]+shi->dxno[1])*shi->view[1]+ (shi->vn[2]+shi->dxno[2])*shi->view[2] );
958
959                         shi->dxref[0]= shi->ref[0]- ( shi->view[0]+shi->dxview+i*(shi->vn[0]+shi->dxno[0]));
960                         shi->dxref[1]= shi->ref[1]- (shi->view[1]+ i*(shi->vn[1]+shi->dxno[1]));
961                         shi->dxref[2]= shi->ref[2]- (shi->view[2]+ i*(shi->vn[2]+shi->dxno[2]));
962
963                         i= -2*( (shi->vn[0]+shi->dyno[0])*shi->view[0]+
964                                 (shi->vn[1]+shi->dyno[1])*(shi->view[1]+shi->dyview)+ (shi->vn[2]+shi->dyno[2])*shi->view[2] );
965
966                         shi->dyref[0]= shi->ref[0]- (shi->view[0]+ i*(shi->vn[0]+shi->dyno[0]));
967                         shi->dyref[1]= shi->ref[1]- (shi->view[1]+shi->dyview+i*(shi->vn[1]+shi->dyno[1]));
968                         shi->dyref[2]= shi->ref[2]- (shi->view[2]+ i*(shi->vn[2]+shi->dyno[2]));
969
970                 }
971                 else {
972
973                         i= -2*( shi->vn[0]*(shi->view[0]+shi->dxview) +
974                                 shi->vn[1]*shi->view[1]+ shi->vn[2]*shi->view[2] );
975
976                         shi->dxref[0]= shi->ref[0]- (shi->view[0]+shi->dxview+i*shi->vn[0]);
977                         shi->dxref[1]= shi->ref[1]- (shi->view[1]+ i*shi->vn[1]);
978                         shi->dxref[2]= shi->ref[2]- (shi->view[2]+ i*shi->vn[2]);
979
980                         i= -2*( shi->vn[0]*shi->view[0]+
981                                 shi->vn[1]*(shi->view[1]+shi->dyview)+ shi->vn[2]*shi->view[2] );
982
983                         shi->dyref[0]= shi->ref[0]- (shi->view[0]+ i*shi->vn[0]);
984                         shi->dyref[1]= shi->ref[1]- (shi->view[1]+shi->dyview+i*shi->vn[1]);
985                         shi->dyref[2]= shi->ref[2]- (shi->view[2]+ i*shi->vn[2]);
986                 }
987         }
988
989 }
990
991 /* called from ray.c */
992 void shade_color(ShadeInput *shi, ShadeResult *shr)
993 {
994         Material *ma= shi->mat;
995
996         if(ma->mode & (MA_VERTEXCOLP|MA_FACETEXTURE)) {
997                 shi->r= shi->vcol[0];
998                 shi->g= shi->vcol[1];
999                 shi->b= shi->vcol[2];
1000         }
1001         
1002         if(ma->texco) {
1003                 if(ma->mode & (MA_VERTEXCOLP|MA_FACETEXTURE)) {
1004                         shi->r= shi->vcol[0];
1005                         shi->g= shi->vcol[1];
1006                         shi->b= shi->vcol[2];
1007                 }
1008                 do_material_tex(shi);
1009         }
1010
1011         if(ma->mode & (MA_ZTRA|MA_RAYTRANSP)) {
1012                 if(ma->fresnel_tra!=0.0) 
1013                         shi->alpha*= fresnel_fac(shi->view, shi->vn, ma->fresnel_tra_i, ma->fresnel_tra);
1014         }
1015
1016         shr->diff[0]= shi->r;
1017         shr->diff[1]= shi->g;
1018         shr->diff[2]= shi->b;
1019         shr->alpha= shi->alpha;
1020 }
1021
1022 /* ramp for at end of shade */
1023 static void ramp_diffuse_result(float *diff, ShadeInput *shi)
1024 {
1025         Material *ma= shi->mat;
1026         float col[4], fac=0;
1027
1028         if(ma->ramp_col) {
1029                 if(ma->rampin_col==MA_RAMP_IN_RESULT) {
1030                         
1031                         fac= 0.3*diff[0] + 0.58*diff[1] + 0.12*diff[2];
1032                         do_colorband(ma->ramp_col, fac, col);
1033                         
1034                         /* blending method */
1035                         fac= col[3]*ma->rampfac_col;
1036                         
1037                         ramp_blend(ma->rampblend_col, diff, diff+1, diff+2, fac, col);
1038                 }
1039         }
1040 }
1041
1042 /* r,g,b denote energy, ramp is used with different values to make new material color */
1043 static void add_to_diffuse(float *diff, ShadeInput *shi, float is, float r, float g, float b)
1044 {
1045         Material *ma= shi->mat;
1046         float col[4], colt[3], fac=0;
1047         
1048         if(ma->ramp_col && (ma->mode & MA_RAMP_COL)) {
1049                 
1050                 /* MA_RAMP_IN_RESULT is exceptional */
1051                 if(ma->rampin_col==MA_RAMP_IN_RESULT) {
1052                         // normal add
1053                         diff[0] += r * shi->r;
1054                         diff[1] += g * shi->g;
1055                         diff[2] += b * shi->b;
1056                 }
1057                 else {
1058                         /* input */
1059                         switch(ma->rampin_col) {
1060                         case MA_RAMP_IN_ENERGY:
1061                                 fac= 0.3*r + 0.58*g + 0.12*b;
1062                                 break;
1063                         case MA_RAMP_IN_SHADER:
1064                                 fac= is;
1065                                 break;
1066                         case MA_RAMP_IN_NOR:
1067                                 fac= shi->view[0]*shi->vn[0] + shi->view[1]*shi->vn[1] + shi->view[2]*shi->vn[2];
1068                                 break;
1069                         }
1070         
1071                         do_colorband(ma->ramp_col, fac, col);
1072                         
1073                         /* blending method */
1074                         fac= col[3]*ma->rampfac_col;
1075                         colt[0]= shi->r; colt[1]= shi->g; colt[2]= shi->b;
1076
1077                         ramp_blend(ma->rampblend_col, colt, colt+1, colt+2, fac, col);
1078
1079                         /* output to */
1080                         diff[0] += r * colt[0];
1081                         diff[1] += g * colt[1];
1082                         diff[2] += b * colt[2];
1083                 }
1084         }
1085         else {
1086                 diff[0] += r * shi->r;
1087                 diff[1] += g * shi->g;
1088                 diff[2] += b * shi->b;
1089         }
1090 }
1091
1092 static void ramp_spec_result(float *specr, float *specg, float *specb, ShadeInput *shi)
1093 {
1094         Material *ma= shi->mat;
1095         float col[4];
1096         float fac;
1097         
1098         if(ma->ramp_spec && (ma->rampin_spec==MA_RAMP_IN_RESULT)) {
1099                 fac= 0.3*(*specr) + 0.58*(*specg) + 0.12*(*specb);
1100                 do_colorband(ma->ramp_spec, fac, col);
1101                 
1102                 /* blending method */
1103                 fac= col[3]*ma->rampfac_spec;
1104                 
1105                 ramp_blend(ma->rampblend_spec, specr, specg, specb, fac, col);
1106                 
1107         }
1108 }
1109
1110 /* is = dot product shade, t = spec energy */
1111 static void do_specular_ramp(ShadeInput *shi, float is, float t, float *spec)
1112 {
1113         Material *ma= shi->mat;
1114         float col[4];
1115         float fac=0.0;
1116         
1117         spec[0]= shi->specr;
1118         spec[1]= shi->specg;
1119         spec[2]= shi->specb;
1120
1121         /* MA_RAMP_IN_RESULT is exception */
1122         if(ma->ramp_spec && (ma->rampin_spec!=MA_RAMP_IN_RESULT)) {
1123                 
1124                 /* input */
1125                 switch(ma->rampin_spec) {
1126                 case MA_RAMP_IN_ENERGY:
1127                         fac= t;
1128                         break;
1129                 case MA_RAMP_IN_SHADER:
1130                         fac= is;
1131                         break;
1132                 case MA_RAMP_IN_NOR:
1133                         fac= shi->view[0]*shi->vn[0] + shi->view[1]*shi->vn[1] + shi->view[2]*shi->vn[2];
1134                         break;
1135                 }
1136                 
1137                 do_colorband(ma->ramp_spec, fac, col);
1138                 
1139                 /* blending method */
1140                 fac= col[3]*ma->rampfac_spec;
1141                 
1142                 ramp_blend(ma->rampblend_spec, spec, spec+1, spec+2, fac, col);
1143         }
1144 }
1145
1146
1147
1148 static void ambient_occlusion(ShadeInput *shi, ShadeResult *shr)
1149 {
1150         float f, shadfac[4];
1151         
1152         if((R.wrld.mode & WO_AMB_OCC) && (R.r.mode & R_RAYTRACE) && shi->amb!=0.0) {
1153                 ray_ao(shi, shadfac);
1154
1155                 if(R.wrld.aocolor==WO_AOPLAIN) {
1156                         if (R.wrld.aomix==WO_AOADDSUB) shadfac[3] = 2.0*shadfac[3]-1.0;
1157                         else if (R.wrld.aomix==WO_AOSUB) shadfac[3] = shadfac[3]-1.0;
1158
1159                         f= R.wrld.aoenergy*shadfac[3]*shi->amb;
1160                         add_to_diffuse(shr->diff, shi, f, f, f, f);
1161                 }
1162                 else {
1163                         if (R.wrld.aomix==WO_AOADDSUB) {
1164                                 shadfac[0] = 2.0*shadfac[0]-1.0;
1165                                 shadfac[1] = 2.0*shadfac[1]-1.0;
1166                                 shadfac[2] = 2.0*shadfac[2]-1.0;
1167                         }
1168                         else if (R.wrld.aomix==WO_AOSUB) {
1169                                 shadfac[0] = shadfac[0]-1.0;
1170                                 shadfac[1] = shadfac[1]-1.0;
1171                                 shadfac[2] = shadfac[2]-1.0;
1172                         }
1173                         f= R.wrld.aoenergy*shi->amb;
1174                         add_to_diffuse(shr->diff, shi, f, f*shadfac[0], f*shadfac[1], f*shadfac[2]);
1175                 }
1176         }
1177 }
1178
1179 void shade_lamp_loop(ShadeInput *shi, ShadeResult *shr)
1180 {
1181         LampRen *lar;
1182         GroupObject *go;
1183         Material *ma= shi->mat;
1184         VlakRen *vlr= shi->vlr;
1185         ListBase *lights;
1186         float i, inp, inpr, is, t, lv[3], vnor[3], lacol[3], lampdist, ld = 0;
1187         float lvrot[3], *vn, *view, shadfac[4], soft, phongcorr;        // shadfac = rgba
1188
1189         vn= shi->vn;
1190         view= shi->view;
1191         
1192         memset(shr, 0, sizeof(ShadeResult));
1193         
1194         if((ma->mode & MA_RAYMIRROR)==0) shi->ray_mirror= 0.0;
1195         
1196         /* lights */
1197         if(ma->group)
1198                 lights= &ma->group->gobject;
1199         else
1200                 lights= &R.lights;
1201         
1202         /* separate loop */
1203         if(ma->mode & MA_ONLYSHADOW) {
1204                 float ir;
1205                 
1206                 if(R.r.mode & R_SHADOW) {
1207                         
1208                         shadfac[3]= ir= 0.0;
1209                         for(go=lights->first; go; go= go->next) {
1210                                 lar= go->lampren;
1211                                 if(lar==NULL) continue;
1212                                 
1213                                 /* yafray: ignore shading by photonlights, not used in Blender */
1214                                 if (lar->type==LA_YF_PHOTON) continue;
1215                                 
1216                                 if(lar->mode & LA_LAYER) if((lar->lay & vlr->lay)==0) continue;
1217                                 
1218                                 lv[0]= shi->co[0]-lar->co[0];
1219                                 lv[1]= shi->co[1]-lar->co[1];
1220                                 lv[2]= shi->co[2]-lar->co[2];
1221
1222                                 if(lar->type==LA_SPOT) {
1223                                         /* only test within spotbundel */
1224                                         if(lar->shb || (lar->mode & LA_SHAD_RAY)) {
1225
1226                                                 Normalise(lv);
1227                                                 inpr= lv[0]*lar->vec[0]+lv[1]*lar->vec[1]+lv[2]*lar->vec[2];
1228                                                 if(inpr>lar->spotsi) {
1229                                                         
1230                                                         inp= vn[0]*lv[0] + vn[1]*lv[1] + vn[2]*lv[2];
1231                                                         
1232                                                         if(lar->shb) i = testshadowbuf(lar->shb, shi->co, shi->dxco, shi->dyco, inp);
1233                                                         else {
1234                                                                 float shad[4];
1235                                                                 ray_shadow(shi, lar, shad);
1236                                                                 i= shad[3];
1237                                                         }
1238                                                         
1239                                                         t= inpr - lar->spotsi;
1240                                                         if(t<lar->spotbl && lar->spotbl!=0.0) {
1241                                                                 t/= lar->spotbl;
1242                                                                 t*= t;
1243                                                                 i= t*i+(1.0-t);
1244                                                         }
1245                                                         
1246                                                         shadfac[3]+= i;
1247                                                         ir+= 1.0;
1248                                                 }
1249                                                 else {
1250                                                         shadfac[3]+= 1.0;
1251                                                         ir+= 1.0;
1252                                                 }
1253                                         }
1254                                 }
1255                                 else if(lar->mode & LA_SHAD_RAY) {
1256                                         float shad[4];
1257                                         
1258                                         /* single sided? */
1259                                         if( shi->facenor[0]*lv[0] + shi->facenor[1]*lv[1] + shi->facenor[2]*lv[2] > -0.01) {
1260                                                 ray_shadow(shi, lar, shad);
1261                                                 shadfac[3]+= shad[3];
1262                                                 ir+= 1.0;
1263                                         }
1264                                 }
1265
1266                         }
1267                         if(ir>0.0) {
1268                                 shadfac[3]/= ir;
1269                                 shr->alpha= (shi->alpha)*(1.0-shadfac[3]);
1270                         }
1271                 }
1272                 
1273                 if((R.wrld.mode & WO_AMB_OCC) && (R.r.mode & R_RAYTRACE) && shi->amb!=0.0) {
1274                         float f;
1275
1276                         ray_ao(shi, shadfac);   // shadfac==0: full light
1277                         shadfac[3]= 1.0-shadfac[3];
1278                         
1279                         f= R.wrld.aoenergy*shadfac[3]*shi->amb;
1280                         
1281                         if(R.wrld.aomix==WO_AOADD) {
1282                                 shr->alpha += f;
1283                                 shr->alpha *= f;
1284                         }
1285                         else if(R.wrld.aomix==WO_AOSUB) {
1286                                 shr->alpha += f;
1287                         }
1288                         else {
1289                                 shr->alpha *= f;
1290                                 shr->alpha += f;
1291                         }
1292                 }
1293                 
1294                 return;
1295         }
1296                 
1297         if(ma->mode & (MA_VERTEXCOLP|MA_FACETEXTURE)) {
1298                 shi->r= shi->vcol[0];
1299                 shi->g= shi->vcol[1];
1300                 shi->b= shi->vcol[2];
1301         }
1302         
1303         /* envmap hack, always reset */
1304         shi->refcol[0]= shi->refcol[1]= shi->refcol[2]= shi->refcol[3]= 0.0;
1305
1306         if(ma->texco) {
1307                 if(ma->mode & (MA_VERTEXCOLP|MA_FACETEXTURE)) {
1308                         shi->r= shi->vcol[0];
1309                         shi->g= shi->vcol[1];
1310                         shi->b= shi->vcol[2];
1311                 }
1312                 do_material_tex(shi);
1313         }
1314         
1315         if(ma->mode & MA_SHLESS) {
1316                 shr->diff[0]= shi->r;
1317                 shr->diff[1]= shi->g;
1318                 shr->diff[2]= shi->b;
1319                 shr->alpha= shi->alpha;
1320                 return;
1321         }
1322
1323         if( (ma->mode & (MA_VERTEXCOL|MA_VERTEXCOLP))== MA_VERTEXCOL ) {        // vertexcolor light
1324                 // add_to_diffuse(shr->diff, shi, 1.0, ma->emit+shi->vcol[0], ma->emit+shi->vcol[1], ma->emit+shi->vcol[2]);
1325                 shr->diff[0]= shi->r*(shi->emit+shi->vcol[0]);
1326                 shr->diff[1]= shi->g*(shi->emit+shi->vcol[1]);
1327                 shr->diff[2]= shi->b*(shi->emit+shi->vcol[2]);
1328         }
1329         else {
1330                 // add_to_diffuse(shr->diff, shi, 1.0, ma->emit, ma->emit, ma->emit);
1331                 shr->diff[0]= shi->r*shi->emit;
1332                 shr->diff[1]= shi->g*shi->emit;
1333                 shr->diff[2]= shi->b*shi->emit;
1334         }
1335         
1336         ambient_occlusion(shi, shr);
1337
1338         for(go=lights->first; go; go= go->next) {
1339                 lar= go->lampren;
1340                 if(lar==NULL) continue;
1341                 
1342                 /* yafray: ignore shading by photonlights, not used in Blender */
1343                 if (lar->type==LA_YF_PHOTON) continue;
1344
1345                 /* test for lamp layer */
1346                 if(lar->mode & LA_LAYER) if((lar->lay & vlr->lay)==0) continue;
1347                 
1348                 /* lampdist calculation */
1349                 if(lar->type==LA_SUN || lar->type==LA_HEMI) {
1350                         VECCOPY(lv, lar->vec);
1351                         lampdist= 1.0;
1352                 }
1353                 else {
1354                         lv[0]= shi->co[0]-lar->co[0];
1355                         lv[1]= shi->co[1]-lar->co[1];
1356                         lv[2]= shi->co[2]-lar->co[2];
1357                         ld= sqrt(lv[0]*lv[0]+lv[1]*lv[1]+lv[2]*lv[2]);
1358                         lv[0]/= ld;
1359                         lv[1]/= ld;
1360                         lv[2]/= ld;
1361                         
1362                         /* ld is re-used further on (texco's) */
1363                         if(lar->type==LA_AREA) {
1364                                 lampdist= 1.0;
1365                         }
1366                         else {
1367                                 if(lar->mode & LA_QUAD) {
1368                                         t= 1.0;
1369                                         if(lar->ld1>0.0)
1370                                                 t= lar->dist/(lar->dist+lar->ld1*ld);
1371                                         if(lar->ld2>0.0)
1372                                                 t*= lar->distkw/(lar->distkw+lar->ld2*ld*ld);
1373         
1374                                         lampdist= t;
1375                                 }
1376                                 else {
1377                                         lampdist= (lar->dist/(lar->dist+ld));
1378                                 }
1379         
1380                                 if(lar->mode & LA_SPHERE) {
1381                                         t= lar->dist - ld;
1382                                         if(t<0.0) continue;
1383                                         
1384                                         t/= lar->dist;
1385                                         lampdist*= (t);
1386                                 }
1387                         }
1388                 }
1389
1390                 lacol[0]= lar->r;
1391                 lacol[1]= lar->g;
1392                 lacol[2]= lar->b;
1393                 
1394                 /* init transp shadow */
1395                 shadfac[3]= 1.0;
1396                 if(ma->mode & MA_SHADOW_TRA) shadfac[0]= shadfac[1]= shadfac[2]= 1.0;
1397
1398                 if(lar->type==LA_SPOT) {
1399                         
1400                         if(lar->mode & LA_SQUARE) {
1401                                 if(lv[0]*lar->vec[0]+lv[1]*lar->vec[1]+lv[2]*lar->vec[2]>0.0) {
1402                                         float x;
1403                                         
1404                                         /* rotate view to lampspace */
1405                                         VECCOPY(lvrot, lv);
1406                                         MTC_Mat3MulVecfl(lar->imat, lvrot);
1407                                         
1408                                         x= MAX2(fabs(lvrot[0]/lvrot[2]) , fabs(lvrot[1]/lvrot[2]));
1409                                         /* 1.0/(sqrt(1+x*x)) is equivalent to cos(atan(x)) */
1410
1411                                         inpr= 1.0f/(sqrt(1.0f+x*x));
1412                                 }
1413                                 else inpr= 0.0;
1414                         }
1415                         else {
1416                                 inpr= lv[0]*lar->vec[0]+lv[1]*lar->vec[1]+lv[2]*lar->vec[2];
1417                         }
1418
1419                         t= lar->spotsi;
1420                         if(inpr<t) continue;
1421                         else {
1422                                 t= inpr-t;
1423                                 i= 1.0;
1424                                 soft= 1.0;
1425                                 if(t<lar->spotbl && lar->spotbl!=0.0) {
1426                                         /* soft area */
1427                                         i= t/lar->spotbl;
1428                                         t= i*i;
1429                                         soft= (3.0*t-2.0*t*i);
1430                                         inpr*= soft;
1431                                 }
1432                                 lampdist*=inpr;
1433                         }
1434
1435                         if(lar->mode & LA_OSATEX) {
1436                                 shi->osatex= 1; /* signal for multitex() */
1437                                 
1438                                 shi->dxlv[0]= lv[0] - (shi->co[0]-lar->co[0]+shi->dxco[0])/ld;
1439                                 shi->dxlv[1]= lv[1] - (shi->co[1]-lar->co[1]+shi->dxco[1])/ld;
1440                                 shi->dxlv[2]= lv[2] - (shi->co[2]-lar->co[2]+shi->dxco[2])/ld;
1441
1442                                 shi->dylv[0]= lv[0] - (shi->co[0]-lar->co[0]+shi->dyco[0])/ld;
1443                                 shi->dylv[1]= lv[1] - (shi->co[1]-lar->co[1]+shi->dyco[1])/ld;
1444                                 shi->dylv[2]= lv[2] - (shi->co[2]-lar->co[2]+shi->dyco[2])/ld;
1445                         }
1446                         
1447                 }
1448
1449                 if(lar->mode & LA_TEXTURE)  do_lamp_tex(lar, lv, shi, lacol);
1450                 
1451                 /* dot product and reflectivity */
1452                 /* inp = dotproduct, is = shader result, i = lamp energy (with shadow) */
1453                 
1454                 /* tangent case; calculate fake face normal, aligned with lampvector */
1455                 if(vlr->flag & R_TANGENT) {
1456                         float cross[3];
1457                         Crossf(cross, lv, vn);
1458                         Crossf(vnor, cross, vn);
1459                         vnor[0]= -vnor[0];vnor[1]= -vnor[1];vnor[2]= -vnor[2];
1460                         vn= vnor;
1461                 }
1462                 else if(ma->mode & MA_TANGENT_V) {
1463                         float cross[3];
1464                         Crossf(cross, lv, shi->tang);
1465                         Crossf(vnor, cross, shi->tang);
1466                         vnor[0]= -vnor[0];vnor[1]= -vnor[1];vnor[2]= -vnor[2];
1467                         vn= vnor;
1468                 }
1469                 
1470                 inp= vn[0]*lv[0] + vn[1]*lv[1] + vn[2]*lv[2];
1471         
1472                 /* phong threshold to prevent backfacing faces having artefacts on ray shadow (terminator problem) */
1473                 if((ma->mode & MA_RAYBIAS) && (lar->mode & LA_SHAD_RAY) && (vlr->flag & R_SMOOTH)) {
1474                         float thresh= vlr->ob->smoothresh;
1475                         if(inp>thresh)
1476                                 phongcorr= (inp-thresh)/(inp*(1.0-thresh));
1477                         else
1478                                 phongcorr= 0.0;
1479                 }
1480                 else if(ma->sbias!=0.0f) {
1481                         if(inp>ma->sbias)
1482                                 phongcorr= (inp-ma->sbias)/(inp*(1.0-ma->sbias));
1483                         else
1484                                 phongcorr= 0.0;
1485                 }
1486                 else phongcorr= 1.0;
1487                 
1488                 /* diffuse shaders */
1489                 if(lar->mode & LA_NO_DIFF) {
1490                         is= 0.0;        // skip shaders
1491                 }
1492                 else if(lar->type==LA_HEMI) {
1493                         is= 0.5*inp + 0.5;
1494                 }
1495                 else {
1496                 
1497                         if(lar->type==LA_AREA) {
1498                                 /* single sided */
1499                                 if(lv[0]*lar->vec[0]+lv[1]*lar->vec[1]+lv[2]*lar->vec[2]>0.0) {
1500                                         inp= area_lamp_energy(shi->co, vn, lar);
1501                                 }
1502                                 else inp= 0.0;
1503                         }
1504                         
1505                         /* diffuse shaders (oren nayer gets inp from area light) */
1506                         if(ma->diff_shader==MA_DIFF_ORENNAYAR) is= OrenNayar_Diff(inp, vn, lv, view, ma->roughness);
1507                         else if(ma->diff_shader==MA_DIFF_TOON) is= Toon_Diff(vn, lv, view, ma->param[0], ma->param[1]);
1508                         else if(ma->diff_shader==MA_DIFF_MINNAERT) is= Minnaert_Diff(inp, vn, view, ma->darkness);
1509                         else if(ma->diff_shader==MA_DIFF_FRESNEL) is= Fresnel_Diff(vn, lv, view, ma->param[0], ma->param[1]);
1510                         else is= inp;   // Lambert
1511                 }
1512                 
1513                 i= is*phongcorr;
1514                 
1515                 if(i>0.0) {
1516                         i*= lampdist*shi->refl;
1517                 }
1518                 
1519                 vn= shi->vn;    // bring back original vector, we use special specular shaders for tangent
1520                 if(ma->mode & MA_TANGENT_V)
1521                         vn= shi->tang;
1522                         
1523                 /* shadow and spec, (lampdist==0 outside spot) */
1524                 if(lampdist> 0.0) {
1525                         
1526                         if(i>0.0 && (R.r.mode & R_SHADOW)) {
1527                                 if(ma->mode & MA_SHADOW) {
1528                                         if(lar->type==LA_HEMI); // no shadow
1529                                         else {
1530                                                 if(lar->shb) {
1531                                                         shadfac[3] = testshadowbuf(lar->shb, shi->co, shi->dxco, shi->dyco, inp);
1532                                                 }
1533                                                 else if(lar->mode & LA_SHAD_RAY) {
1534                                                         ray_shadow(shi, lar, shadfac);
1535                                                 }
1536         
1537                                                 /* warning, here it skips the loop */
1538                                                 if(lar->mode & LA_ONLYSHADOW) {
1539                                                         
1540                                                         shadfac[3]= i*lar->energy*(1.0-shadfac[3]);
1541                                                         shr->diff[0] -= shadfac[3]*shi->r;
1542                                                         shr->diff[1] -= shadfac[3]*shi->g;
1543                                                         shr->diff[2] -= shadfac[3]*shi->b;
1544                                                         
1545                                                         continue;
1546                                                 }
1547                                                 
1548                                                 if(shadfac[3]==0.0) continue;
1549         
1550                                                 i*= shadfac[3];
1551                                         }
1552                                 }
1553                         }
1554 #if 0                   
1555                         if(R.r.mode & R_RAYTRACE) {
1556                                 extern void ray_translucent(ShadeInput *shi, LampRen *lar, float *distfac, float *co);
1557                                 float co[3], distfac;
1558                                 
1559                                 ray_translucent(shi, lar, &distfac, co);
1560                                 
1561                                 if(distfac<0.01f*G.rt) {
1562                                 //      printf("distfac %f\n", distfac);
1563                                         distfac= 1.0f - distfac/(0.01f*G.rt);
1564                                         shr->diff[0]+= distfac;
1565                                         shr->diff[1]+= distfac;
1566                                         shr->diff[2]+= distfac;
1567                                 }
1568                         }
1569 #endif                  
1570                 
1571                         /* specularity */
1572                         if(shadfac[3]>0.0 && shi->spec!=0.0 && !(lar->mode & LA_NO_SPEC)) {
1573                                 
1574                                 if(lar->type==LA_HEMI) {
1575                                         /* hemi uses no spec shaders (yet) */
1576                                         
1577                                         lv[0]+= view[0];
1578                                         lv[1]+= view[1];
1579                                         lv[2]+= view[2];
1580                                         
1581                                         Normalise(lv);
1582                                         
1583                                         t= vn[0]*lv[0]+vn[1]*lv[1]+vn[2]*lv[2];
1584                                         
1585                                         if(lar->type==LA_HEMI) {
1586                                                 t= 0.5*t+0.5;
1587                                         }
1588                                         
1589                                         t= shadfac[3]*shi->spec*spec(t, shi->har);
1590                                         shr->spec[0]+= t*(lacol[0] * shi->specr);
1591                                         shr->spec[1]+= t*(lacol[1] * shi->specg);
1592                                         shr->spec[2]+= t*(lacol[2] * shi->specb);
1593                                 }
1594                                 else {
1595                                         /* specular shaders */
1596                                         float specfac;
1597
1598                                         if(ma->spec_shader==MA_SPEC_PHONG) 
1599                                                 specfac= Phong_Spec(vn, lv, view, shi->har, (vlr->flag & R_TANGENT) || (ma->mode & MA_TANGENT_V));
1600                                         else if(ma->spec_shader==MA_SPEC_COOKTORR) 
1601                                                 specfac= CookTorr_Spec(vn, lv, view, shi->har, (vlr->flag & R_TANGENT) || (ma->mode & MA_TANGENT_V));
1602                                         else if(ma->spec_shader==MA_SPEC_BLINN) 
1603                                                 specfac= Blinn_Spec(vn, lv, view, ma->refrac, (float)shi->har, (vlr->flag & R_TANGENT) || (ma->mode & MA_TANGENT_V));
1604                                         else if(ma->spec_shader==MA_SPEC_WARDISO)
1605                                                 specfac= WardIso_Spec( vn, lv, view, ma->rms, (vlr->flag & R_TANGENT) || (ma->mode & MA_TANGENT_V));
1606                                         else 
1607                                                 specfac= Toon_Spec(vn, lv, view, ma->param[2], ma->param[3], (vlr->flag & R_TANGENT) || (ma->mode & MA_TANGENT_V));
1608                                 
1609                                         /* area lamp correction */
1610                                         if(lar->type==LA_AREA) specfac*= inp;
1611                                         
1612                                         t= shadfac[3]*shi->spec*lampdist*specfac;
1613                                         
1614                                         if(ma->mode & MA_RAMP_SPEC) {
1615                                                 float spec[3];
1616                                                 do_specular_ramp(shi, specfac, t, spec);
1617                                                 shr->spec[0]+= t*(lacol[0] * spec[0]);
1618                                                 shr->spec[1]+= t*(lacol[1] * spec[1]);
1619                                                 shr->spec[2]+= t*(lacol[2] * spec[2]);
1620                                         }
1621                                         else {
1622                                                 shr->spec[0]+= t*(lacol[0] * shi->specr);
1623                                                 shr->spec[1]+= t*(lacol[1] * shi->specg);
1624                                                 shr->spec[2]+= t*(lacol[2] * shi->specb);
1625                                         }
1626                                 }
1627                         }
1628                 }
1629                 
1630                 /* in case 'no diffuse' we still do most calculus, spec can be in shadow */
1631                 if(i>0.0 && !(lar->mode & LA_NO_DIFF)) {
1632                         if(ma->mode & MA_SHADOW_TRA) {
1633                                 add_to_diffuse(shr->diff, shi, is, i*shadfac[0]*lacol[0], i*shadfac[1]*lacol[1], i*shadfac[2]*lacol[2]);
1634                         }
1635                         else {
1636                                 add_to_diffuse(shr->diff, shi, is, i*lacol[0], i*lacol[1], i*lacol[2]);
1637                         }
1638                 }
1639         }
1640
1641         if(ma->mode & (MA_ZTRA|MA_RAYTRANSP)) {
1642                 if(ma->fresnel_tra!=0.0) 
1643                         shi->alpha*= fresnel_fac(shi->view, shi->vn, ma->fresnel_tra_i, ma->fresnel_tra);
1644
1645                 if(shi->spectra!=0.0) {
1646
1647                         t = MAX3(shr->spec[0], shr->spec[1], shr->spec[2]);
1648                         t *= shi->spectra;
1649                         if(t>1.0) t= 1.0;
1650                         shi->alpha= (1.0-t)*shi->alpha+t;
1651                 }
1652         }
1653
1654         shr->alpha= shi->alpha;
1655
1656         shr->diff[0]+= shi->r*shi->amb*shi->rad[0];
1657         shr->diff[0]+= shi->ambr;
1658         
1659         shr->diff[1]+= shi->g*shi->amb*shi->rad[1];
1660         shr->diff[1]+= shi->ambg;
1661         
1662         shr->diff[2]+= shi->b*shi->amb*shi->rad[2];
1663         shr->diff[2]+= shi->ambb;
1664         
1665         if(ma->mode & MA_RAMP_COL) ramp_diffuse_result(shr->diff, shi);
1666         if(ma->mode & MA_RAMP_SPEC) ramp_spec_result(shr->spec, shr->spec+1, shr->spec+2, shi);
1667         
1668         /* refcol is for envmap only */
1669         if(shi->refcol[0]!=0.0) {
1670                 shr->diff[0]= shi->mirr*shi->refcol[1] + (1.0 - shi->mirr*shi->refcol[0])*shr->diff[0];
1671                 shr->diff[1]= shi->mirg*shi->refcol[2] + (1.0 - shi->mirg*shi->refcol[0])*shr->diff[1];
1672                 shr->diff[2]= shi->mirb*shi->refcol[3] + (1.0 - shi->mirb*shi->refcol[0])*shr->diff[2];
1673         }
1674
1675 }
1676
1677 /* this function sets all coords for render (shared with raytracer) */
1678 /* warning; exception for ortho render is here, can be done better! */
1679 void shade_input_set_coords(ShadeInput *shi, float u, float v, int i1, int i2, int i3)
1680 {
1681         VertRen *v1, *v2, *v3;
1682         VlakRen *vlr= shi->vlr;
1683         float l, dl;
1684         short texco= shi->mat->texco;
1685         int mode= shi->mat->mode_l;             /* or-ed result for all layers */
1686         char p1, p2, p3;
1687         
1688         /* for rendering of quads, the following values are used to denote vertices:
1689            0 1 2        scanline tria & first half quad, and ray tria
1690            0 2 3    scanline 2nd half quad
1691            0 1 3    raytracer first half quad
1692            2 1 3    raytracer 2nd half quad
1693         */
1694
1695         if(i1==0) {
1696                 v1= vlr->v1;
1697                 p1= ME_FLIPV1;
1698         } else {
1699                 v1= vlr->v3;
1700                 p1= ME_FLIPV3;
1701         }
1702
1703         if(i2==1) {
1704                 v2= vlr->v2;
1705                 p2= ME_FLIPV2;
1706         } else {
1707                 v2= vlr->v3;
1708                 p2= ME_FLIPV3;
1709         }
1710         
1711         if(i3==2) {
1712                 v3= vlr->v3;
1713                 p3= ME_FLIPV3;
1714         } else {
1715                 v3= vlr->v4;
1716                 p3= ME_FLIPV4;
1717         }
1718
1719         /* calculate U and V, for scanline (normal u and v are -1 to 0) */
1720         if(u==1.0) {
1721                 if( (vlr->flag & R_SMOOTH) || (texco & NEED_UV) ) {
1722                         /* exception case for wire render of edge */
1723                         if(vlr->v2==vlr->v3) {
1724                                 float lend, lenc;
1725                                 
1726                                 lend= VecLenf(v2->co, v1->co);
1727                                 lenc= VecLenf(shi->co, v1->co);
1728                                 
1729                                 if(lend==0.0f) {
1730                                         u=v= 0.0f;
1731                                 }
1732                                 else {
1733                                         u= - (1.0f - lenc/lend);
1734                                         v= 0.0f;
1735                                 }
1736                                 
1737                                 if(shi->osatex) {
1738                                         shi->dxuv[0]=  0.0f;
1739                                         shi->dxuv[1]=  0.0f;
1740                                         shi->dyuv[0]=  0.0f;
1741                                         shi->dyuv[1]=  0.0f;
1742                                 }
1743                         }
1744                         else {
1745                                 float detsh, t00, t10, t01, t11;
1746                                 
1747                                 if(vlr->snproj==0) {
1748                                         t00= v3->co[0]-v1->co[0]; t01= v3->co[1]-v1->co[1];
1749                                         t10= v3->co[0]-v2->co[0]; t11= v3->co[1]-v2->co[1];
1750                                 }
1751                                 else if(vlr->snproj==1) {
1752                                         t00= v3->co[0]-v1->co[0]; t01= v3->co[2]-v1->co[2];
1753                                         t10= v3->co[0]-v2->co[0]; t11= v3->co[2]-v2->co[2];
1754                                 }
1755                                 else {
1756                                         t00= v3->co[1]-v1->co[1]; t01= v3->co[2]-v1->co[2];
1757                                         t10= v3->co[1]-v2->co[1]; t11= v3->co[2]-v2->co[2];
1758                                 }
1759                                 
1760                                 detsh= 1.0/(t00*t11-t10*t01);
1761                                 t00*= detsh; t01*=detsh; 
1762                                 t10*=detsh; t11*=detsh;
1763                         
1764                                 if(vlr->snproj==0) {
1765                                         u= (shi->co[0]-v3->co[0])*t11-(shi->co[1]-v3->co[1])*t10;
1766                                         v= (shi->co[1]-v3->co[1])*t00-(shi->co[0]-v3->co[0])*t01;
1767                                         if(shi->osatex) {
1768                                                 shi->dxuv[0]=  shi->dxco[0]*t11- shi->dxco[1]*t10;
1769                                                 shi->dxuv[1]=  shi->dxco[1]*t00- shi->dxco[0]*t01;
1770                                                 shi->dyuv[0]=  shi->dyco[0]*t11- shi->dyco[1]*t10;
1771                                                 shi->dyuv[1]=  shi->dyco[1]*t00- shi->dyco[0]*t01;
1772                                         }
1773                                 }
1774                                 else if(vlr->snproj==1) {
1775                                         u= (shi->co[0]-v3->co[0])*t11-(shi->co[2]-v3->co[2])*t10;
1776                                         v= (shi->co[2]-v3->co[2])*t00-(shi->co[0]-v3->co[0])*t01;
1777                                         if(shi->osatex) {
1778                                                 shi->dxuv[0]=  shi->dxco[0]*t11- shi->dxco[2]*t10;
1779                                                 shi->dxuv[1]=  shi->dxco[2]*t00- shi->dxco[0]*t01;
1780                                                 shi->dyuv[0]=  shi->dyco[0]*t11- shi->dyco[2]*t10;
1781                                                 shi->dyuv[1]=  shi->dyco[2]*t00- shi->dyco[0]*t01;
1782                                         }
1783                                 }
1784                                 else {
1785                                         u= (shi->co[1]-v3->co[1])*t11-(shi->co[2]-v3->co[2])*t10;
1786                                         v= (shi->co[2]-v3->co[2])*t00-(shi->co[1]-v3->co[1])*t01;
1787                                         if(shi->osatex) {
1788                                                 shi->dxuv[0]=  shi->dxco[1]*t11- shi->dxco[2]*t10;
1789                                                 shi->dxuv[1]=  shi->dxco[2]*t00- shi->dxco[1]*t01;
1790                                                 shi->dyuv[0]=  shi->dyco[1]*t11- shi->dyco[2]*t10;
1791                                                 shi->dyuv[1]=  shi->dyco[2]*t00- shi->dyco[1]*t01;
1792                                         }
1793                                 }
1794                         }
1795                 }       
1796         }
1797         l= 1.0+u+v;
1798         
1799         /* calculate punos (vertexnormals) */
1800         if(vlr->flag & R_SMOOTH) { 
1801                 float n1[3], n2[3], n3[3];
1802                 
1803                 if(shi->puno & p1) {
1804                         n1[0]= -v1->n[0]; n1[1]= -v1->n[1]; n1[2]= -v1->n[2];
1805                 } else {
1806                         n1[0]= v1->n[0]; n1[1]= v1->n[1]; n1[2]= v1->n[2];
1807                 }
1808                 if(shi->puno & p2) {
1809                         n2[0]= -v2->n[0]; n2[1]= -v2->n[1]; n2[2]= -v2->n[2];
1810                 } else {
1811                         n2[0]= v2->n[0]; n2[1]= v2->n[1]; n2[2]= v2->n[2];
1812                 }
1813                 
1814                 if(shi->puno & p3) {
1815                         n3[0]= -v3->n[0]; n3[1]= -v3->n[1]; n3[2]= -v3->n[2];
1816                 } else {
1817                         n3[0]= v3->n[0]; n3[1]= v3->n[1]; n3[2]= v3->n[2];
1818                 }
1819
1820                 shi->vn[0]= l*n3[0]-u*n1[0]-v*n2[0];
1821                 shi->vn[1]= l*n3[1]-u*n1[1]-v*n2[1];
1822                 shi->vn[2]= l*n3[2]-u*n1[2]-v*n2[2];
1823
1824                 Normalise(shi->vn);
1825
1826                 if(shi->osatex && (texco & (TEXCO_NORM|TEXCO_REFL)) ) {
1827                         dl= shi->dxuv[0]+shi->dxuv[1];
1828                         shi->dxno[0]= dl*n3[0]-shi->dxuv[0]*n1[0]-shi->dxuv[1]*n2[0];
1829                         shi->dxno[1]= dl*n3[1]-shi->dxuv[0]*n1[1]-shi->dxuv[1]*n2[1];
1830                         shi->dxno[2]= dl*n3[2]-shi->dxuv[0]*n1[2]-shi->dxuv[1]*n2[2];
1831                         dl= shi->dyuv[0]+shi->dyuv[1];
1832                         shi->dyno[0]= dl*n3[0]-shi->dyuv[0]*n1[0]-shi->dyuv[1]*n2[0];
1833                         shi->dyno[1]= dl*n3[1]-shi->dyuv[0]*n1[1]-shi->dyuv[1]*n2[1];
1834                         shi->dyno[2]= dl*n3[2]-shi->dyuv[0]*n1[2]-shi->dyuv[1]*n2[2];
1835
1836                 }
1837                 
1838                 if(mode & MA_TANGENT_V) {
1839                         float *s1, *s2, *s3;
1840                         
1841                         s1= RE_vertren_get_tangent(&R, v1, 0);
1842                         s2= RE_vertren_get_tangent(&R, v2, 0);
1843                         s3= RE_vertren_get_tangent(&R, v3, 0);
1844                         if(s1 && s2 && s3) {
1845                                 shi->tang[0]= (l*s3[0] - u*s1[0] - v*s2[0]);
1846                                 shi->tang[1]= (l*s3[1] - u*s1[1] - v*s2[1]);
1847                                 shi->tang[2]= (l*s3[2] - u*s1[2] - v*s2[2]);
1848                         }
1849                         else shi->tang[0]= shi->tang[1]= shi->tang[2]= 0.0f;
1850                 }               
1851         }
1852         else {
1853                 VECCOPY(shi->vn, shi->facenor);
1854                 if(mode & MA_TANGENT_V) 
1855                         shi->tang[0]= shi->tang[1]= shi->tang[2]= 0.0f;
1856         }
1857
1858         /* texture coordinates. shi->dxuv shi->dyuv have been set */
1859         if(texco & NEED_UV) {
1860                 if(texco & TEXCO_ORCO) {
1861                         if(v1->orco) {
1862                                 float *o1, *o2, *o3;
1863                                 
1864                                 o1= v1->orco;
1865                                 o2= v2->orco;
1866                                 o3= v3->orco;
1867                                 
1868                                 shi->lo[0]= l*o3[0]-u*o1[0]-v*o2[0];
1869                                 shi->lo[1]= l*o3[1]-u*o1[1]-v*o2[1];
1870                                 shi->lo[2]= l*o3[2]-u*o1[2]-v*o2[2];
1871         
1872                                 if(shi->osatex) {
1873                                         dl= shi->dxuv[0]+shi->dxuv[1];
1874                                         shi->dxlo[0]= dl*o3[0]-shi->dxuv[0]*o1[0]-shi->dxuv[1]*o2[0];
1875                                         shi->dxlo[1]= dl*o3[1]-shi->dxuv[0]*o1[1]-shi->dxuv[1]*o2[1];
1876                                         shi->dxlo[2]= dl*o3[2]-shi->dxuv[0]*o1[2]-shi->dxuv[1]*o2[2];
1877                                         dl= shi->dyuv[0]+shi->dyuv[1];
1878                                         shi->dylo[0]= dl*o3[0]-shi->dyuv[0]*o1[0]-shi->dyuv[1]*o2[0];
1879                                         shi->dylo[1]= dl*o3[1]-shi->dyuv[0]*o1[1]-shi->dyuv[1]*o2[1];
1880                                         shi->dylo[2]= dl*o3[2]-shi->dyuv[0]*o1[2]-shi->dyuv[1]*o2[2];
1881                                 }
1882                         }
1883                 }
1884                 
1885                 if(texco & TEXCO_GLOB) {
1886                         VECCOPY(shi->gl, shi->co);
1887                         MTC_Mat4MulVecfl(R.viewinv, shi->gl);
1888                         if(shi->osatex) {
1889                                 VECCOPY(shi->dxgl, shi->dxco);
1890                                 MTC_Mat3MulVecfl(R.imat, shi->dxco);
1891                                 VECCOPY(shi->dygl, shi->dyco);
1892                                 MTC_Mat3MulVecfl(R.imat, shi->dyco);
1893                         }
1894                 }
1895                 if(texco & TEXCO_STRAND) {
1896                         shi->strand= (l*v3->accum - u*v1->accum - v*v2->accum);
1897                         if(shi->osatex) {
1898                                 dl= shi->dxuv[0]+shi->dxuv[1];
1899                                 shi->dxstrand= dl*v3->accum-shi->dxuv[0]*v1->accum-shi->dxuv[1]*v2->accum;
1900                                 dl= shi->dyuv[0]+shi->dyuv[1];
1901                                 shi->dystrand= dl*v3->accum-shi->dyuv[0]*v1->accum-shi->dyuv[1]*v2->accum;
1902                         }
1903                 }
1904                 if((texco & TEXCO_UV) || (mode & (MA_VERTEXCOL|MA_VERTEXCOLP|MA_FACETEXTURE)))  {
1905                         int j1=i1, j2=i2, j3=i3;
1906                         
1907                         /* to prevent storing new tfaces or vcols, we check a split runtime */
1908                         /*              4---3           4---3 */
1909                         /*              |\ 1|   or  |1 /| */
1910                         /*              |0\ |           |/ 0| */
1911                         /*              1---2           1---2   0 = orig face, 1 = new face */
1912                         
1913                         /* Update vert nums to point to correct verts of original face */
1914                         if(vlr->flag & R_DIVIDE_24) {  
1915                                 if(vlr->flag & R_FACE_SPLIT) {
1916                                         j1++; j2++; j3++;
1917                                 }
1918                                 else {
1919                                         j3++;
1920                                 }
1921                         }
1922                         else if(vlr->flag & R_FACE_SPLIT) {
1923                                 j2++; j3++; 
1924                         }
1925                         
1926                         if(mode & (MA_VERTEXCOL|MA_VERTEXCOLP)) {
1927                                 
1928                                 if(vlr->vcol) {
1929                                         char *cp1, *cp2, *cp3;
1930                                         
1931                                         cp1= (char *)(vlr->vcol+j1);
1932                                         cp2= (char *)(vlr->vcol+j2);
1933                                         cp3= (char *)(vlr->vcol+j3);
1934
1935                                         shi->vcol[0]= (l*((float)cp3[3]) - u*((float)cp1[3]) - v*((float)cp2[3]))/255.0;
1936                                         shi->vcol[1]= (l*((float)cp3[2]) - u*((float)cp1[2]) - v*((float)cp2[2]))/255.0;
1937                                         shi->vcol[2]= (l*((float)cp3[1]) - u*((float)cp1[1]) - v*((float)cp2[1]))/255.0;
1938                                 }
1939                                 else {
1940                                         shi->vcol[0]= 0.0;
1941                                         shi->vcol[1]= 0.0;
1942                                         shi->vcol[2]= 0.0;
1943                                 }
1944                         }
1945                         if(vlr->tface) {
1946                                 float *uv1, *uv2, *uv3;
1947                                 
1948                                 uv1= vlr->tface->uv[j1];
1949                                 uv2= vlr->tface->uv[j2];
1950                                 uv3= vlr->tface->uv[j3];
1951                                 
1952                                 shi->uv[0]= -1.0 + 2.0*(l*uv3[0]-u*uv1[0]-v*uv2[0]);
1953                                 shi->uv[1]= -1.0 + 2.0*(l*uv3[1]-u*uv1[1]-v*uv2[1]);
1954                                 shi->uv[2]= 0.0;        // texture.c assumes there are 3 coords
1955                                 
1956                                 if(shi->osatex) {
1957                                         float duv[2];
1958                                         
1959                                         dl= shi->dxuv[0]+shi->dxuv[1];
1960                                         duv[0]= shi->dxuv[0]; 
1961                                         duv[1]= shi->dxuv[1];
1962                                         
1963                                         shi->dxuv[0]= 2.0*(dl*uv3[0]-duv[0]*uv1[0]-duv[1]*uv2[0]);
1964                                         shi->dxuv[1]= 2.0*(dl*uv3[1]-duv[0]*uv1[1]-duv[1]*uv2[1]);
1965         
1966                                         dl= shi->dyuv[0]+shi->dyuv[1];
1967                                         duv[0]= shi->dyuv[0]; 
1968                                         duv[1]= shi->dyuv[1];
1969         
1970                                         shi->dyuv[0]= 2.0*(dl*uv3[0]-duv[0]*uv1[0]-duv[1]*uv2[0]);
1971                                         shi->dyuv[1]= 2.0*(dl*uv3[1]-duv[0]*uv1[1]-duv[1]*uv2[1]);
1972                                 }
1973                                 if(mode & MA_FACETEXTURE) {
1974                                         if((mode & (MA_VERTEXCOL|MA_VERTEXCOLP))==0) {
1975                                                 shi->vcol[0]= 1.0;
1976                                                 shi->vcol[1]= 1.0;
1977                                                 shi->vcol[2]= 1.0;
1978                                         }
1979                                         if(vlr->tface) render_realtime_texture(shi);
1980                                 }
1981                         }
1982                         else {
1983                                 shi->uv[0]= 2.0*(u+.5);
1984                                 shi->uv[1]= 2.0*(v+.5);
1985                                 shi->uv[2]= 0.0;        // texture.c assumes there are 3 coords
1986                                 if(mode & MA_FACETEXTURE) {
1987                                         /* no tface? set at 1.0 */
1988                                         shi->vcol[0]= 1.0;
1989                                         shi->vcol[1]= 1.0;
1990                                         shi->vcol[2]= 1.0;
1991                                 }
1992                         }
1993                 }
1994                 if(texco & TEXCO_NORM) {
1995                         shi->orn[0]= -shi->vn[0];
1996                         shi->orn[1]= -shi->vn[1];
1997                         shi->orn[2]= -shi->vn[2];
1998                 }
1999                 if(mode & MA_RADIO) {
2000                         float *r1, *r2, *r3;
2001                         
2002                         r1= RE_vertren_get_rad(&R, v1, 0);
2003                         r2= RE_vertren_get_rad(&R, v2, 0);
2004                         r3= RE_vertren_get_rad(&R, v3, 0);
2005                         
2006                         if(r1 && r2 && r3) {
2007                                 shi->rad[0]= (l*r3[0] - u*r1[0] - v*r2[0]);
2008                                 shi->rad[1]= (l*r3[1] - u*r1[1] - v*r2[1]);
2009                                 shi->rad[2]= (l*r3[2] - u*r1[2] - v*r2[2]);
2010                         }
2011                         else {
2012                                 shi->rad[0]= shi->rad[1]= shi->rad[2]= 0.0;
2013                         }
2014                 }
2015                 else {
2016                         shi->rad[0]= shi->rad[1]= shi->rad[2]= 0.0;
2017                 }
2018                 if(texco & TEXCO_REFL) {
2019                         /* mirror reflection colour textures (and envmap) */
2020                         calc_R_ref(shi);        /* wrong location for normal maps! XXXXXXXXXXXXXX */
2021                 }
2022                 if(texco & TEXCO_STRESS) {
2023                         float *s1, *s2, *s3;
2024                         
2025                         s1= RE_vertren_get_stress(&R, v1, 0);
2026                         s2= RE_vertren_get_stress(&R, v2, 0);
2027                         s3= RE_vertren_get_stress(&R, v3, 0);
2028                         if(s1 && s2 && s3) {
2029                                 shi->stress= l*s3[0] - u*s1[0] - v*s2[0];
2030                                 if(shi->stress<1.0f) shi->stress-= 1.0f;
2031                                 else shi->stress= (shi->stress-1.0f)/shi->stress;
2032                         }
2033                         else shi->stress= 0.0f;
2034                 }
2035                 if(texco & TEXCO_TANGENT) {
2036                         if((mode & MA_TANGENT_V)==0) {
2037                                 /* just prevent surprises */
2038                                 shi->tang[0]= shi->tang[1]= shi->tang[2]= 0.0f;
2039                         }
2040                 }
2041         }
2042         else {
2043                 shi->rad[0]= shi->rad[1]= shi->rad[2]= 0.0;
2044         }
2045 }
2046
2047 #if 0
2048 /* return labda for view vector being closest to line v3-v4 */
2049 /* was used for wire render */
2050 static float isec_view_line(float *view, float *v3, float *v4)
2051 {
2052         float vec[3];
2053         float dot0, dot1, dot2, veclen, viewlen;
2054         float fac, div;
2055         
2056         vec[0]= v4[0] - v3[0];
2057         vec[1]= v4[1] - v3[1];
2058         vec[2]= v4[2] - v3[2];
2059         
2060         dot0 = v3[0]*vec[0] + v3[1]*vec[1] + v3[2]*vec[2];
2061         dot1 = vec[0]*view[0] + vec[1]*view[1] + vec[2]*view[2];
2062         dot2 = v3[0]*view[0] + v3[1]*view[1] + v3[2]*view[2];
2063         
2064         veclen = vec[0]*vec[0] + vec[1]*vec[1] + vec[2]*vec[2];
2065         viewlen = view[0]*view[0] + view[1]*view[1] + view[2]*view[2];
2066         
2067         div = viewlen*veclen - dot1*dot1;
2068         if (div==0.0) return 0.0;
2069         
2070         fac = dot2*veclen - dot0*dot1;
2071         return fac/div;
2072 }
2073 #endif
2074
2075
2076 /* also used as callback for nodes */
2077 void shade_material_loop(ShadeInput *shi, ShadeResult *shr)
2078 {
2079         
2080         shade_lamp_loop(shi, shr);      /* clears shr */
2081         
2082         if(shi->translucency!=0.0) {
2083                 ShadeResult shr_t;
2084                 
2085                 VECCOPY(shi->vn, shi->vno);
2086                 VecMulf(shi->vn, -1.0);
2087                 VecMulf(shi->facenor, -1.0);
2088                 shade_lamp_loop(shi, &shr_t);
2089                 
2090                 shr->diff[0]+= shi->translucency*shr_t.diff[0];
2091                 shr->diff[1]+= shi->translucency*shr_t.diff[1];
2092                 shr->diff[2]+= shi->translucency*shr_t.diff[2];
2093                 VecMulf(shi->vn, -1.0);
2094                 VecMulf(shi->facenor, -1.0);
2095         }
2096         
2097         if(R.r.mode & R_RAYTRACE) {
2098                 if(shi->ray_mirror!=0.0 || ((shi->mat->mode & MA_RAYTRANSP) && shr->alpha!=1.0)) {
2099                         ray_trace(shi, shr);
2100                 }
2101         }
2102         else {
2103                 /* doesnt look 'correct', but is better for preview, plus envmaps dont raytrace this */
2104                 if(shi->mat->mode & MA_RAYTRANSP) shr->alpha= 1.0;
2105         }
2106         
2107 }
2108
2109 /* x,y: window coordinate from 0 to rectx,y */
2110 /* return pointer to rendered face */
2111 /* note, facenr declared volatile due to over-eager -O2 optimizations
2112  * on cygwin (particularly -frerun-cse-after-loop)
2113  */
2114 void *shadepixel(RenderPart *pa, float x, float y, int z, volatile int facenr, int mask, float *col, float *rco)
2115 {
2116         ShadeResult shr;
2117         ShadeInput shi;
2118         VlakRen *vlr=NULL;
2119         
2120         if(facenr< 0) { /* error */
2121                 return NULL;
2122         }
2123         /* currently in use for dithering (soft shadow) node preview */
2124         shi.xs= (int)(x+0.5f);
2125         shi.ys= (int)(y+0.5f);
2126         shi.thread= pa->thread;
2127         shi.do_preview= R.r.scemode & R_NODE_PREVIEW;
2128
2129         /* mask is used to indicate amount of samples (ray shad/mir and AO) */
2130         shi.mask= mask;
2131         shi.depth= 0;   // means first hit, not raytracing
2132         
2133         if(facenr==0) { /* sky */
2134                 col[0]= 0.0; col[1]= 0.0; col[2]= 0.0; col[3]= 0.0;
2135                 VECCOPY(rco, col);
2136         }
2137         else if( (facenr & 0x7FFFFF) <= R.totvlak) {
2138                 VertRen *v1;
2139                 float alpha, fac, zcor;
2140                 
2141                 vlr= RE_findOrAddVlak(&R, (facenr-1) & 0x7FFFFF);
2142                 
2143                 shi.vlr= vlr;
2144                 shi.mat= vlr->mat;
2145                 
2146                 shi.osatex= (shi.mat->texco & TEXCO_OSA);
2147                 
2148                 /* copy the face normal (needed because it gets flipped for tracing */
2149                 VECCOPY(shi.facenor, vlr->n);
2150                 shi.puno= vlr->puno;
2151                 
2152                 v1= vlr->v1;
2153                 
2154                 /* COXYZ AND VIEW VECTOR  */
2155                 calc_view_vector(shi.view, x, y);       /* returns not normalized, so is in viewplane coords */
2156
2157                 /* wire cannot use normal for calculating shi.co */
2158                 if(shi.mat->mode & MA_WIRE) {
2159                         float zco;
2160                         /* inverse of zbuf calc: zbuf = MAXZ*hoco_z/hoco_w */
2161                         
2162                         zco= ((float)z)/2147483647.0f;
2163                         shi.co[2]= R.winmat[3][2]/( R.winmat[2][3]*zco - R.winmat[2][2] );
2164                         
2165                         fac= zcor= shi.co[2]/shi.view[2];
2166                         
2167                         shi.co[0]= fac*shi.view[0];
2168                         shi.co[1]= fac*shi.view[1];
2169                 }
2170                 else {
2171                         float dface;
2172                         
2173                         dface= v1->co[0]*shi.facenor[0]+v1->co[1]*shi.facenor[1]+v1->co[2]*shi.facenor[2];
2174                         
2175                         /* ortho viewplane cannot intersect using view vector originating in (0,0,0) */
2176                         if(R.r.mode & R_ORTHO) {
2177                                 /* x and y 3d coordinate can be derived from pixel coord and winmat */
2178                                 float fx= 2.0/(R.winx*R.winmat[0][0]);
2179                                 float fy= 2.0/(R.winy*R.winmat[1][1]);
2180                                 
2181                                 shi.co[0]= (0.5 + x - 0.5*R.winx)*fx - R.winmat[3][0]/R.winmat[0][0];
2182                                 shi.co[1]= (0.5 + y - 0.5*R.winy)*fy - R.winmat[3][1]/R.winmat[1][1];
2183                                 
2184                                 /* using a*x + b*y + c*z = d equation, (a b c) is normal */
2185                                 if(shi.facenor[2]!=0.0f)
2186                                         shi.co[2]= (dface - shi.facenor[0]*shi.co[0] - shi.facenor[1]*shi.co[1])/shi.facenor[2];
2187                                 else
2188                                         shi.co[2]= 0.0f;
2189                                 
2190                                 zcor= 1.0; // only to prevent not-initialize
2191                                 
2192                                 if(shi.osatex || (R.r.mode & R_SHADOW) ) {
2193                                         shi.dxco[0]= fx;
2194                                         shi.dxco[1]= 0.0;
2195                                         if(shi.facenor[2]!=0.0f)
2196                                                 shi.dxco[2]= (shi.facenor[0]*fx)/shi.facenor[2];
2197                                         else 
2198                                                 shi.dxco[2]= 0.0f;
2199                                         
2200                                         shi.dyco[0]= 0.0;
2201                                         shi.dyco[1]= fy;
2202                                         if(shi.facenor[2]!=0.0f)
2203                                                 shi.dyco[2]= (shi.facenor[1]*fy)/shi.facenor[2];
2204                                         else 
2205                                                 shi.dyco[2]= 0.0f;
2206                                 }
2207                         }
2208                         else {
2209                                 float div;
2210                                 
2211                                 div= shi.facenor[0]*shi.view[0] + shi.facenor[1]*shi.view[1] + shi.facenor[2]*shi.view[2];
2212                                 if (div!=0.0) fac= zcor= dface/div;
2213                                 else fac= zcor= 0.0;
2214                                 
2215                                 shi.co[0]= fac*shi.view[0];
2216                                 shi.co[1]= fac*shi.view[1];
2217                                 shi.co[2]= fac*shi.view[2];
2218                         
2219                                 /* pixel dx/dy for render coord */
2220                                 if(shi.osatex || (R.r.mode & R_SHADOW) ) {
2221                                         float u= dface/(div - R.viewdx*shi.facenor[0]);
2222                                         float v= dface/(div - R.viewdy*shi.facenor[1]);
2223
2224                                         shi.dxco[0]= shi.co[0]- (shi.view[0]-R.viewdx)*u;
2225                                         shi.dxco[1]= shi.co[1]- (shi.view[1])*u;
2226                                         shi.dxco[2]= shi.co[2]- (shi.view[2])*u;
2227
2228                                         shi.dyco[0]= shi.co[0]- (shi.view[0])*v;
2229                                         shi.dyco[1]= shi.co[1]- (shi.view[1]-R.viewdy)*v;
2230                                         shi.dyco[2]= shi.co[2]- (shi.view[2])*v;
2231
2232                                 }
2233                         }
2234                 }
2235                 /* rco might be used for sky texture */
2236                 VECCOPY(rco, shi.co);
2237                 
2238                 /* cannot normalise earlier, code above needs it at viewplane level */
2239                 fac= Normalise(shi.view);
2240                 zcor*= fac;     // for mist, distance of point from camera
2241                 
2242                 if(shi.osatex) {
2243                         if( (shi.mat->texco & TEXCO_REFL) ) {
2244                                 shi.dxview= -R.viewdx/fac;
2245                                 shi.dyview= -R.viewdy/fac;
2246                         }
2247                 }
2248                 
2249                 /* calcuate normals, texture coords, vertex colors, etc */
2250                 if(facenr & 0x800000)
2251                         shade_input_set_coords(&shi, 1.0, 1.0, 0, 2, 3);
2252                 else 
2253                         shade_input_set_coords(&shi, 1.0, 1.0, 0, 1, 2);
2254
2255                 /* this only avalailable for scanline */
2256                 if(shi.mat->texco & TEXCO_WINDOW) {
2257                         shi.winco[0]= -1.0f + 2.0f*x/(float)R.winx;
2258                         shi.winco[1]= -1.0f + 2.0f*y/(float)R.winy;
2259                         shi.winco[2]= 0.0;
2260                         if(shi.osatex) {
2261                                 shi.dxwin[0]= 2.0/(float)R.winx;
2262                                 shi.dywin[1]= 2.0/(float)R.winy;
2263                                 shi.dxwin[1]= shi.dxwin[2]= 0.0;
2264                                 shi.dywin[0]= shi.dywin[2]= 0.0;
2265                         }
2266                 }
2267                 /* after this the u and v AND shi.dxuv and shi.dyuv are incorrect */
2268                 if(shi.mat->texco & TEXCO_STICKY) {
2269                         VertRen *v2, *v3;
2270                         float *s1, *s2, *s3;
2271                         
2272                         if(facenr & 0x800000) {
2273                                 v2= vlr->v3; v3= vlr->v4;
2274                         } else {
2275                                 v2= vlr->v2; v3= vlr->v3;
2276                         }
2277                         
2278                         s1= RE_vertren_get_sticky(&R, v1, 0);
2279                         s2= RE_vertren_get_sticky(&R, v2, 0);
2280                         s3= RE_vertren_get_sticky(&R, v3, 0);
2281                         
2282                         if(s1 && s2 && s3) {
2283                                 float Zmulx, Zmuly;
2284                                 float hox, hoy, l, dl, u, v;
2285                                 float s00, s01, s10, s11, detsh;
2286                                 
2287                                 /* XXXX */
2288                                 Zmulx= R.winx; Zmuly= R.winy;
2289                                 
2290                                 s00= v3->ho[0]/v3->ho[3] - v1->ho[0]/v1->ho[3];
2291                                 s01= v3->ho[1]/v3->ho[3] - v1->ho[1]/v1->ho[3];
2292                                 s10= v3->ho[0]/v3->ho[3] - v2->ho[0]/v2->ho[3];
2293                                 s11= v3->ho[1]/v3->ho[3] - v2->ho[1]/v2->ho[3];
2294                                 
2295                                 detsh= s00*s11-s10*s01;
2296                                 s00/= detsh; s01/=detsh; 
2297                                 s10/=detsh; s11/=detsh;
2298         
2299                                 /* recalc u and v again */
2300                                 hox= x/Zmulx -1.0;
2301                                 hoy= y/Zmuly -1.0;
2302                                 u= (hox - v3->ho[0]/v3->ho[3])*s11 - (hoy - v3->ho[1]/v3->ho[3])*s10;
2303                                 v= (hoy - v3->ho[1]/v3->ho[3])*s00 - (hox - v3->ho[0]/v3->ho[3])*s01;
2304                                 l= 1.0+u+v;
2305                                 
2306                                 shi.sticky[0]= l*s3[0]-u*s1[0]-v*s2[0];
2307                                 shi.sticky[1]= l*s3[1]-u*s1[1]-v*s2[1];
2308                                 shi.sticky[2]= 0.0;
2309                                 
2310                                 if(shi.osatex) {
2311                                         shi.dxuv[0]=  s11/Zmulx;
2312                                         shi.dxuv[1]=  - s01/Zmulx;
2313                                         shi.dyuv[0]=  - s10/Zmuly;
2314                                         shi.dyuv[1]=  s00/Zmuly;
2315                                         
2316                                         dl= shi.dxuv[0]+shi.dxuv[1];
2317                                         shi.dxsticky[0]= dl*s3[0]-shi.dxuv[0]*s1[0]-shi.dxuv[1]*s2[0];
2318                                         shi.dxsticky[1]= dl*s3[1]-shi.dxuv[0]*s1[1]-shi.dxuv[1]*s2[1];
2319                                         dl= shi.dyuv[0]+shi.dyuv[1];
2320                                         shi.dysticky[0]= dl*s3[0]-shi.dyuv[0]*s1[0]-shi.dyuv[1]*s2[0];
2321                                         shi.dysticky[1]= dl*s3[1]-shi.dyuv[0]*s1[1]-shi.dyuv[1]*s2[1];
2322                                 }
2323                         }
2324                 }
2325                 
2326                 /* ------  main shading loop -------- */
2327                 VECCOPY(shi.vno, shi.vn);
2328                 
2329                 if(shi.mat->nodetree && shi.mat->use_nodes) {
2330                         ntreeShaderExecTree(shi.mat->nodetree, &shi, &shr);
2331                 }
2332                 else {
2333                         /* copy all relevant material vars, note, keep this synced with render_types.h */
2334                         memcpy(&shi.r, &shi.mat->r, 23*sizeof(float));
2335                         shi.har= shi.mat->har;
2336                         
2337                         shade_material_loop(&shi, &shr);
2338                 }
2339                 
2340                 /* after shading and composit layers */
2341                 if(shr.spec[0]<0.0f) shr.spec[0]= 0.0f;
2342                 if(shr.spec[1]<0.0f) shr.spec[1]= 0.0f;
2343                 if(shr.spec[2]<0.0f) shr.spec[2]= 0.0f;
2344                 
2345                 if(shr.diff[0]<0.0f) shr.diff[0]= 0.0f;
2346                 if(shr.diff[1]<0.0f) shr.diff[1]= 0.0f;
2347                 if(shr.diff[2]<0.0f) shr.diff[2]= 0.0f;
2348                 
2349                 VECADD(col, shr.diff, shr.spec);
2350                 
2351                 /* NOTE: this is not correct here, sky from raytrace gets corrected... */
2352                 /* exposure correction */
2353                 if(R.wrld.exp!=0.0 || R.wrld.range!=1.0) {
2354                         if((shi.mat->mode & MA_SHLESS)==0) {
2355                                 col[0]= R.wrld.linfac*(1.0-exp( col[0]*R.wrld.logfac) );
2356                                 col[1]= R.wrld.linfac*(1.0-exp( col[1]*R.wrld.logfac) );
2357                                 col[2]= R.wrld.linfac*(1.0-exp( col[2]*R.wrld.logfac) );
2358                         }
2359                 }
2360                 
2361                 /* MIST */
2362                 if((R.wrld.mode & WO_MIST) && (shi.mat->mode & MA_NOMIST)==0 ) {
2363                         if(R.r.mode & R_ORTHO)
2364                                 alpha= mistfactor(-shi.co[2], shi.co);
2365                         else
2366                                 alpha= mistfactor(zcor, shi.co);
2367                 }
2368                 else alpha= 1.0;
2369
2370                 if(shr.alpha!=1.0 || alpha!=1.0) {
2371                         if(shi.mat->mode & MA_RAYTRANSP) {
2372                                 fac= alpha;     
2373                                 if(R.r.mode & R_UNIFIED)
2374                                         /* unified alpha overs everything... */
2375                                         col[3]= 1.0f;
2376                                 else {
2377                                         /* sky was applied allready for ray transp, only do mist */
2378                                         col[3]= shr.alpha;
2379                                 }
2380                         }
2381                         else {
2382                                 fac= alpha*(shr.alpha);
2383                                 col[3]= fac;
2384                         }                       
2385                         col[0]*= fac;
2386                         col[1]*= fac;
2387                         col[2]*= fac;
2388                 }
2389                 else col[3]= 1.0;
2390         }
2391         
2392         if(R.flag & R_LAMPHALO) {
2393                 if(facenr<=0) { /* calc view vector and put shi.co at far */
2394                         if(R.r.mode & R_ORTHO) {
2395                                 /* x and y 3d coordinate can be derived from pixel coord and winmat */
2396                                 float fx= 2.0/(R.rectx*R.winmat[0][0]);
2397                                 float fy= 2.0/(R.recty*R.winmat[1][1]);
2398                                 
2399                                 shi.co[0]= (0.5 + x - 0.5*R.rectx)*fx - R.winmat[3][0]/R.winmat[0][0];
2400                                 shi.co[1]= (0.5 + y - 0.5*R.recty)*fy - R.winmat[3][1]/R.winmat[1][1];
2401                         }
2402                         
2403                         calc_view_vector(shi.view, x, y);
2404                         shi.co[2]= 0.0;
2405                         
2406                         renderspothalo(&shi, col, 1.0);
2407                 }
2408                 else
2409                         renderspothalo(&shi, col, col[3]);
2410         }
2411         
2412         return vlr;
2413 }
2414
2415 static void shadepixel_sky(RenderPart *pa, float x, float y, int z, int facenr, int mask, float *colf)
2416 {
2417         VlakRen *vlr;
2418         float collector[4], rco[3];
2419         
2420         vlr= shadepixel(pa, x, y, z, facenr, mask, colf, rco);
2421         if(colf[3] != 1.0) {
2422                 /* bail out when raytrace transparency (sky included already) */
2423                 if(vlr && (R.r.mode & R_RAYTRACE))
2424                         if(vlr->mat->mode & MA_RAYTRANSP) return;
2425                 
2426                 renderSkyPixelFloat(collector, x, y, vlr?rco:NULL);
2427                 addAlphaOverFloat(collector, colf);
2428                 QUATCOPY(colf, collector);
2429         }
2430 }
2431
2432 /* adds only alpha values */
2433 static void edge_enhance_calc(RenderPart *pa, float *rectf)     
2434 {
2435         /* use zbuffer to define edges, add it to the image */
2436         int y, x, col, *rz, *rz1, *rz2, *rz3;
2437         int zval1, zval2, zval3;
2438         float *rf;
2439         
2440         /* shift values in zbuffer 4 to the right, for filter we need multiplying with 12 max */
2441         rz= pa->rectz;
2442         if(rz==NULL) return;
2443         
2444         for(y=0; y<pa->recty; y++) {
2445                 for(x=0; x<pa->rectx; x++, rz++) (*rz)>>= 4;
2446         }
2447         
2448         rz1= pa->rectz;
2449         rz2= rz1+pa->rectx;
2450         rz3= rz2+pa->rectx;
2451         
2452         rf= rectf+pa->rectx+1;
2453         
2454         for(y=0; y<pa->recty-2; y++) {
2455                 for(x=0; x<pa->rectx-2; x++, rz1++, rz2++, rz3++, rf++) {
2456                         
2457                         /* prevent overflow with sky z values */
2458                         zval1=   rz1[0] + 2*rz1[1] +   rz1[2];
2459                         zval2=  2*rz2[0]           + 2*rz2[2];
2460                         zval3=   rz3[0] + 2*rz3[1] +   rz3[2];
2461                         
2462                         col= abs ( 4*rz2[1] - (zval1 + zval2 + zval3)/3 );
2463                         
2464                         col >>= 5;
2465                         if(col > (1<<16)) col= (1<<16);
2466                         else col= (R.r.edgeint*col)>>8;
2467                         
2468                         if(col>0) {
2469                                 float fcol;
2470                                 
2471                                 if(col>255) fcol= 1.0f;
2472                                 else fcol= (float)col/255.0f;
2473                                 
2474                                 if(R.osa)
2475                                         *rf+= fcol/(float)R.osa;
2476                                 else
2477                                         *rf= fcol;
2478                         }
2479                 }
2480                 rz1+= 2;
2481                 rz2+= 2;
2482                 rz3+= 2;
2483                 rf+= 2;
2484         }
2485 }
2486
2487 static void edge_enhance_add(RenderPart *pa, float *rectf, float *arect)
2488 {
2489         float addcol[4];
2490         int pix;
2491         
2492         for(pix= pa->rectx*pa->recty; pix>0; pix--, arect++, rectf+=4) {
2493                 if(*arect != 0.0f) {
2494                         addcol[0]= *arect * R.r.edgeR;
2495                         addcol[1]= *arect * R.r.edgeG;
2496                         addcol[2]= *arect * R.r.edgeB;
2497                         addcol[3]= *arect;
2498                         addAlphaOverFloat(rectf, addcol);
2499                 }
2500         }
2501 }
2502
2503
2504 /* ********************* MAINLOOPS ******************** */
2505
2506 static void shadeDA_tile(RenderPart *pa, float *rectf, float *recta)
2507 {
2508         PixStr *ps;
2509         float xs, ys;
2510         float fcol[4], *rf, *grf, *acol= NULL;
2511         long *rd, *rectdaps= pa->rectdaps;
2512         int zbuf, samp, curmask, face, mask, fullmask;
2513         int b, x, y, full_osa, seed, crop=0;
2514         
2515         if(R.test_break()) return; 
2516         
2517         /* we set per pixel a fixed seed, for random AO and shadow samples */
2518         seed= pa->rectx*pa->disprect.ymin;
2519
2520         fullmask= (1<<R.osa)-1;
2521         
2522         /* might need it for gamma, in end of this function */
2523         grf= rectf;
2524         
2525         /* filtered render, for now we assume only 1 filter size */
2526         if(pa->crop) {
2527                 crop= 1;
2528                 rectf+= 4*(pa->rectx + 1);
2529                 rectdaps+= pa->rectx + 1;
2530                 if(recta) recta+= 4*(pa->rectx + 1);
2531         }
2532         
2533         for(y=pa->disprect.ymin+crop; y<pa->disprect.ymax-crop; y++) {
2534                 rf= rectf;
2535                 rd= rectdaps;
2536                 if(recta) acol= recta;
2537
2538                 for(x=pa->disprect.xmin+crop; x<pa->disprect.xmax-crop; x++, rd++, rf+=4) {
2539                         BLI_thread_srandom(pa->thread, seed+x);
2540                         
2541                         ps= (PixStr *)(*rd);
2542                         mask= 0;
2543
2544                         /* complex loop, because empty spots are sky, without mask */
2545                         while(TRUE) {
2546                                 
2547                                 if(ps==NULL) {
2548                                         face= 0;
2549                                         curmask= (~mask) & fullmask;
2550                                         zbuf= 0x7FFFFFFF;
2551                                 }
2552                                 else {
2553                                         face= ps->facenr;
2554                                         curmask= ps->mask;
2555                                         zbuf= ps->z;
2556                                 }
2557                                 
2558                                 /* check osa level */
2559                                 if(face==0) full_osa= 0;
2560                                 else {
2561                                         VlakRen *vlr= RE_findOrAddVlak(&R, (face-1) & 0x7FFFFF);
2562                                         full_osa= (vlr->flag & R_FULL_OSA);
2563                                 }
2564                                 
2565                                 if(full_osa) {
2566                                         for(samp=0; samp<R.osa; samp++) {
2567                                                 if(curmask & (1<<samp)) {
2568                                                         xs= (float)x + R.jit[samp][0];
2569                                                         ys= (float)y + R.jit[samp][1];
2570                                                         shadepixel_sky(pa, xs, ys, zbuf, face, (1<<samp), fcol);
2571                                                         
2572                                                         if(acol && acol[3]!=0.0) addAlphaOverFloat(fcol, acol);
2573                                                         if(R.do_gamma) {
2574                                                                 fcol[0]= gammaCorrect(fcol[0]);
2575                                                                 fcol[1]= gammaCorrect(fcol[1]);
2576                                                                 fcol[2]= gammaCorrect(fcol[2]);
2577                                                         }
2578                                                         add_filt_fmask(1<<samp, fcol, rf, pa->rectx);
2579                                                 }
2580                                         }
2581                                 }
2582                                 else {
2583                                         b= R.samples->centmask[curmask];
2584                                         xs= (float)x+R.samples->centLut[b & 15];
2585                                         ys= (float)y+R.samples->centLut[b>>4];
2586                                         shadepixel_sky(pa, xs, ys, zbuf, face, curmask, fcol);
2587                                         
2588                                         if(acol && acol[3]!=0.0) addAlphaOverFloat(fcol, acol);
2589                                         
2590                                         if(R.do_gamma) {
2591                                                 fcol[0]= gammaCorrect(fcol[0]);
2592                                                 fcol[1]= gammaCorrect(fcol[1]);
2593                                                 fcol[2]= gammaCorrect(fcol[2]);
2594                                         }
2595                                         add_filt_fmask(curmask, fcol, rf, pa->rectx);
2596                                 }
2597                                 
2598                                 mask |= curmask;
2599                                 
2600                                 if(ps==NULL) break;
2601                                 else ps= ps->next;
2602                         }
2603                         if(acol) acol+=4;
2604                 }
2605                 
2606                 rectf+= 4*pa->rectx;
2607                 rectdaps+= pa->rectx;
2608                 if(recta) recta+= 4*pa->rectx;
2609                 seed+= pa->rectx;
2610                 
2611                 if(y&1) if(R.test_break()) break; 
2612         }
2613         
2614         if(R.do_gamma) {
2615                 for(y= pa->rectx*pa->recty; y>0; y--, grf+=4) {
2616                         grf[0] = invGammaCorrect(grf[0]);
2617                         grf[1] = invGammaCorrect(grf[1]);
2618                         grf[2] = invGammaCorrect(grf[2]);
2619                 }
2620         }                       
2621         
2622 }
2623
2624 /* ************* pixel struct ******** */
2625
2626
2627 static PixStrMain *addpsmain(ListBase *lb)
2628 {
2629         PixStrMain *psm;
2630         
2631         psm= (PixStrMain *)RE_mallocN(sizeof(PixStrMain),"pixstrMain");
2632         BLI_addtail(lb, psm);
2633         
2634         psm->ps= (PixStr *)RE_mallocN(4096*sizeof(PixStr),"pixstr");
2635         psm->counter= 0;
2636         
2637         return psm;
2638 }
2639
2640 static void freeps(ListBase *lb)
2641 {
2642         PixStrMain *psm, *psmnext;
2643         
2644         for(psm= lb->first; psm; psm= psmnext) {
2645                 psmnext= psm->next;
2646                 if(psm->ps)
2647                         RE_freeN(psm->ps);
2648                 RE_freeN(psm);
2649         }
2650         lb->first= lb->last= NULL;
2651 }
2652
2653 static void addps(ListBase *lb, long *rd, int facenr, int z, unsigned short mask)
2654 {
2655         PixStrMain *psm;
2656         PixStr *ps, *last= NULL;
2657         
2658         if(*rd) {       
2659                 ps= (PixStr *)(*rd);
2660                 
2661                 while(ps) {
2662                         if( ps->facenr == facenr ) {
2663                                 ps->mask |= mask;
2664                                 return;
2665                         }
2666                         last= ps;
2667                         ps= ps->next;
2668                 }
2669         }
2670         
2671         /* make new PS (pixel struct) */
2672         psm= lb->last;
2673         
2674         if(psm->counter==4095)
2675                 psm= addpsmain(lb);
2676         
2677         ps= psm->ps + psm->counter++;
2678         
2679         if(last) last->next= ps;
2680         else *rd= (long)ps;
2681         
2682         ps->next= NULL;
2683         ps->facenr= facenr;
2684         ps->z= z;
2685         ps->mask = mask;
2686 }
2687
2688 static void make_pixelstructs(RenderPart *pa, ListBase *lb)
2689 {
2690         long *rd= pa->rectdaps;
2691         int *rp= pa->rectp;
2692         int *rz= pa->rectz;
2693         int x, y;
2694         int mask= 1<<pa->sample;
2695         
2696         for(y=0; y<pa->recty; y++) {
2697                 for(x=0; x<pa->rectx; x++, rd++, rp++) {
2698                         if(*rp) {
2699                                 addps(lb, rd, *rp, *(rz+x), mask);
2700                         }
2701                 }
2702                 rz+= pa->rectx;
2703         }
2704 }
2705
2706 /* supposed to be fully threadable! */
2707 void zbufshadeDA_tile(RenderPart *pa)
2708 {
2709         RenderLayer *rl;
2710         ListBase psmlist= {NULL, NULL};
2711         float *acolrect= NULL, *edgerect= NULL;
2712         
2713         set_part_zbuf_clipflag(pa);
2714         
2715         /* allocate the necessary buffers */
2716                                 /* zbuffer inits these rects */
2717         pa->rectp= RE_mallocN(sizeof(int)*pa->rectx*pa->recty, "rectp");
2718         pa->rectz= RE_mallocN(sizeof(int)*pa->rectx*pa->recty, "rectz");
2719         if(R.r.mode & R_EDGE) edgerect= RE_callocN(sizeof(float)*pa->rectx*pa->recty, "rectedge");
2720         
2721         for(rl= pa->result->layers.first; rl; rl= rl->next) {
2722
2723                 /* initialize pixelstructs */
2724                 addpsmain(&psmlist);
2725                 pa->rectdaps= RE_callocN(sizeof(long)*pa->rectx*pa->recty+4, "zbufDArectd");
2726                 
2727                 if(rl->layflag & SCE_LAY_SOLID) {
2728                         for(pa->sample=0; pa->sample<R.osa; pa->sample++) {
2729                                 zbuffer_solid(pa, rl->lay, rl->layflag);
2730                                 make_pixelstructs(pa, &psmlist);
2731                                 
2732                                 if(R.r.mode & R_EDGE) edge_enhance_calc(pa, edgerect);
2733                                 if(R.test_break()) break; 
2734                         }
2735                 }
2736                 else    /* need to clear rectz for next passes */
2737                         fillrect(pa->rectz, pa->rectx, pa->recty, 0x7FFFFFFF);
2738
2739                 
2740                 /* we do transp layer first, so its get added with filter in main buffer... still incorrect though */
2741                 if(R.flag & R_ZTRA) {
2742                         if(rl->layflag & SCE_LAY_ZTRA) {
2743                                 acolrect= RE_callocN(4*sizeof(float)*pa->rectx*pa->recty, "alpha layer");
2744                                 zbuffer_transp_shade(pa, acolrect, rl->lay, rl->layflag);
2745                         }
2746                 }
2747
2748                 /* shades solid and adds transparent layer */
2749                 if(rl->layflag & SCE_LAY_SOLID) 
2750                         shadeDA_tile(pa, rl->rectf, acolrect);
2751                 else if(acolrect) {
2752                         SWAP(float *, acolrect, rl->rectf);
2753                 }
2754                 
2755                 /* extra layers */
2756                 if(R.r.mode & R_EDGE) 
2757                         edge_enhance_add(pa, rl->rectf, edgerect);
2758                 if(R.flag & R_HALO)
2759                         if(rl->layflag & SCE_LAY_HALO)
2760                                 halo_tile(pa, rl->rectf, rl->lay);
2761                 
2762                 if(rl->passflag & SCE_PASS_Z)
2763                         convert_zbuf_to_distbuf(pa, rl);
2764                 
2765                 /* free stuff within loop! */
2766                 if(acolrect) {
2767                         RE_freeN(acolrect);
2768                         acolrect= NULL;
2769                 }
2770                 RE_freeN(pa->rectdaps); pa->rectdaps= NULL;
2771                 freeps(&psmlist);
2772         }
2773         
2774         /* free all */
2775         RE_freeN(pa->rectp); pa->rectp= NULL;
2776         RE_freeN(pa->rectz); pa->rectz= NULL;
2777         
2778         if(edgerect) RE_freeN(edgerect);
2779         
2780
2781 }
2782
2783
2784 /* ------------------------------------------------------------------------ */
2785
2786 /* supposed to be fully threadable! */
2787 void zbufshade_tile(RenderPart *pa)