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