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