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