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