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