New: rendering in background mode (blender -b) now prints a percentage to
[blender.git] / source / blender / render / intern / source / rendercore.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. The Blender
10  * Foundation also sells licenses for use in proprietary software under
11  * the Blender License.  See http://www.blender.org/BL/ for information
12  * about this.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22  *
23  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
24  * All rights reserved.
25  *
26  * The Original Code is: all of this file.
27  *
28  * Contributor(s): Hos, Robert Wenzlaff.
29  *
30  * ***** END GPL/BL DUAL LICENSE BLOCK *****
31  */
32
33
34 /* External modules: */
35 #include "MEM_guardedalloc.h"
36 #include "BLI_arithb.h"
37 #include "IMB_imbuf_types.h"
38 #include "IMB_imbuf.h"
39 #include "MTC_matrixops.h"
40 #include "BKE_utildefines.h"
41
42 #include "DNA_mesh_types.h"
43 #include "DNA_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)/(R.recty);
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 static void ambient_occlusion(World *wrld, ShadeInput *shi, ShadeResult *shr)
1644 {
1645         float f, shadfac[4];
1646         
1647         if((wrld->mode & WO_AMB_OCC) && (R.r.mode & R_RAYTRACE)) {
1648                 ray_ao(shi, wrld, shadfac);
1649
1650                 if(wrld->aocolor==WO_AOPLAIN) {
1651                         if (wrld->aomix==WO_AOADDSUB) shadfac[3] = 2.0*shadfac[3]-1.0;
1652                         else if (wrld->aomix==WO_AOSUB) shadfac[3] = shadfac[3]-1.0;
1653
1654                         f= wrld->aoenergy*shadfac[3]*shi->matren->amb;
1655                         shr->diff[0] += f;
1656                         shr->diff[1] += f;
1657                         shr->diff[2] += f;
1658                 }
1659                 else {
1660                         if (wrld->aomix==WO_AOADDSUB) {
1661                                 shadfac[0] = 2.0*shadfac[0]-1.0;
1662                                 shadfac[1] = 2.0*shadfac[1]-1.0;
1663                                 shadfac[2] = 2.0*shadfac[2]-1.0;
1664                         }
1665                         else if (wrld->aomix==WO_AOSUB) {
1666                                 shadfac[0] = shadfac[0]-1.0;
1667                                 shadfac[1] = shadfac[1]-1.0;
1668                                 shadfac[2] = shadfac[2]-1.0;
1669                         }
1670                         f= wrld->aoenergy*shi->matren->amb;
1671                         shr->diff[0] += f*shadfac[0];
1672                         shr->diff[1] += f*shadfac[1];
1673                         shr->diff[2] += f*shadfac[2];
1674                 }
1675         }
1676 }
1677
1678
1679 /* mask is used to define the amount of rays/samples */
1680 void shade_lamp_loop(ShadeInput *shi, ShadeResult *shr, int mask)
1681 {
1682         LampRen *lar;
1683         Material *ma;
1684         float i, inp, inpr, t, lv[3], lampdist, ld = 0;
1685         float lvrot[3], *vn, *view, shadfac[4], soft;   // shadfac = rgba
1686         int a;
1687
1688         vn= shi->vn;
1689         view= shi->view;
1690         ma= shi->matren;
1691         
1692         memset(shr, 0, sizeof(ShadeResult));
1693         
1694         /* separate loop */
1695         if(ma->mode & MA_ONLYSHADOW) {
1696                 float ir;
1697                 
1698                 shadfac[3]= ir= 0.0;
1699                 for(a=0; a<R.totlamp; a++) {
1700                         lar= R.la[a];
1701                         
1702                         if(lar->mode & LA_LAYER) if((lar->lay & shi->vlr->lay)==0) continue;
1703                         
1704                         lv[0]= shi->co[0]-lar->co[0];
1705                         lv[1]= shi->co[1]-lar->co[1];
1706                         lv[2]= shi->co[2]-lar->co[2];
1707
1708                         if(lar->shb) {
1709                                 /* only test within spotbundel */
1710                                 Normalise(lv);
1711                                 inpr= lv[0]*lar->vec[0]+lv[1]*lar->vec[1]+lv[2]*lar->vec[2];
1712                                 if(inpr>lar->spotsi) {
1713                                         
1714                                         inp= vn[0]*lv[0] + vn[1]*lv[1] + vn[2]*lv[2];
1715                                         
1716                                         i = testshadowbuf(lar->shb, shi->co, inp);
1717                                         
1718                                         t= inpr - lar->spotsi;
1719                                         if(t<lar->spotbl && lar->spotbl!=0.0) {
1720                                                 t/= lar->spotbl;
1721                                                 t*= t;
1722                                                 i= t*i+(1.0-t);
1723                                         }
1724                                         
1725                                         shadfac[3]+= i;
1726                                         ir+= 1.0;
1727                                 }
1728                                 else {
1729                                         shadfac[3]+= 1.0;
1730                                         ir+= 1.0;
1731                                 }
1732                         }
1733                         else if(lar->mode & LA_SHAD_RAY) {
1734                                 float shad[4];
1735                                 
1736                                 /* single sided? */
1737                                 if( shi->vlr->n[0]*lv[0] + shi->vlr->n[1]*lv[1] + shi->vlr->n[2]*lv[2] > -0.01) {
1738                                         ray_shadow(shi, lar, shad, mask);
1739                                         shadfac[3]+= shad[3];
1740                                         ir+= 1.0;
1741                                 }
1742                         }
1743
1744                 }
1745                 if(ir>0.0) shadfac[3]/= ir;
1746                 shr->alpha= (shi->mat->alpha)*(1.0-shadfac[3]);
1747                 
1748                 return;
1749         }
1750                 
1751         if(ma->mode & (MA_VERTEXCOLP|MA_FACETEXTURE)) {
1752                 ma->r= shi->vcol[0];
1753                 ma->g= shi->vcol[1];
1754                 ma->b= shi->vcol[2];
1755         }
1756         
1757         ma->alpha= shi->mat->alpha;     // copy to render material, for fresnel and spectra
1758         
1759         /* envmap hack, always reset */
1760         shi->refcol[0]= shi->refcol[1]= shi->refcol[2]= shi->refcol[3]= 0.0;
1761
1762         if(ma->texco) {
1763                 if(ma->mode & (MA_VERTEXCOLP|MA_FACETEXTURE)) {
1764                         shi->mat->r= shi->vcol[0];
1765                         shi->mat->g= shi->vcol[1];
1766                         shi->mat->b= shi->vcol[2];
1767                 }
1768                 do_material_tex(shi);
1769         }
1770         
1771         if(ma->mode & MA_SHLESS) {
1772                 shr->diff[0]= ma->r;
1773                 shr->diff[1]= ma->g;
1774                 shr->diff[2]= ma->b;
1775                 shr->alpha= ma->alpha;
1776                 return;
1777         }
1778
1779         if( (ma->mode & (MA_VERTEXCOL+MA_VERTEXCOLP))== MA_VERTEXCOL ) {
1780                 shr->diff[0]= ma->emit+shi->vcol[0];
1781                 shr->diff[1]= ma->emit+shi->vcol[1];
1782                 shr->diff[2]= ma->emit+shi->vcol[2];
1783         }
1784         else shr->diff[0]= shr->diff[1]= shr->diff[2]= ma->emit;
1785
1786         ambient_occlusion(&R.wrld, shi, shr);
1787
1788         for(a=0; a<R.totlamp; a++) {
1789                 lar= R.la[a];
1790
1791                 /* test for lamp layer */
1792                 if(lar->mode & LA_LAYER) if((lar->lay & shi->vlr->lay)==0) continue;
1793                 
1794                 /* lampdist calculation */
1795                 if(lar->type==LA_SUN || lar->type==LA_HEMI) {
1796                         VECCOPY(lv, lar->vec);
1797                         lampdist= 1.0;
1798                 }
1799                 else {
1800                         lv[0]= shi->co[0]-lar->co[0];
1801                         lv[1]= shi->co[1]-lar->co[1];
1802                         lv[2]= shi->co[2]-lar->co[2];
1803                         ld= sqrt(lv[0]*lv[0]+lv[1]*lv[1]+lv[2]*lv[2]);
1804                         lv[0]/= ld;
1805                         lv[1]/= ld;
1806                         lv[2]/= ld;
1807                         
1808                         /* ld is re-used further on (texco's) */
1809                         if(lar->type==LA_AREA) {
1810                                 lampdist= 1.0;
1811                         }
1812                         else {
1813                                 if(lar->mode & LA_QUAD) {
1814                                         t= 1.0;
1815                                         if(lar->ld1>0.0)
1816                                                 t= lar->dist/(lar->dist+lar->ld1*ld);
1817                                         if(lar->ld2>0.0)
1818                                                 t*= lar->distkw/(lar->distkw+lar->ld2*ld*ld);
1819         
1820                                         lampdist= t;
1821                                 }
1822                                 else {
1823                                         lampdist= (lar->dist/(lar->dist+ld));
1824                                 }
1825         
1826                                 if(lar->mode & LA_SPHERE) {
1827                                         t= lar->dist - ld;
1828                                         if(t<0.0) continue;
1829                                         
1830                                         t/= lar->dist;
1831                                         lampdist*= (t);
1832                                 }
1833                         }
1834                 }
1835                 
1836                 if(lar->mode & LA_TEXTURE)  do_lamp_tex(lar, lv, shi);
1837
1838                 /* init transp shadow */
1839                 shadfac[3]= 1.0;
1840                 if(ma->mode & MA_SHADOW_TRA) shadfac[0]= shadfac[1]= shadfac[2]= 1.0;
1841
1842                 if(lar->type==LA_SPOT) {
1843                         
1844                         if(lar->mode & LA_SQUARE) {
1845                                 if(lv[0]*lar->vec[0]+lv[1]*lar->vec[1]+lv[2]*lar->vec[2]>0.0) {
1846                                         float x;
1847                                         
1848                                         /* rotate view to lampspace */
1849                                         VECCOPY(lvrot, lv);
1850                                         MTC_Mat3MulVecfl(lar->imat, lvrot);
1851                                         
1852                                         x= MAX2(fabs(lvrot[0]/lvrot[2]) , fabs(lvrot[1]/lvrot[2]));
1853                                         /* 1.0/(sqrt(1+x*x)) is equivalent to cos(atan(x)) */
1854
1855                                         inpr= 1.0/(sqrt(1+x*x));
1856                                 }
1857                                 else inpr= 0.0;
1858                         }
1859                         else {
1860                                 inpr= lv[0]*lar->vec[0]+lv[1]*lar->vec[1]+lv[2]*lar->vec[2];
1861                         }
1862
1863                         t= lar->spotsi;
1864                         if(inpr<t) continue;
1865                         else {
1866                                 t= inpr-t;
1867                                 i= 1.0;
1868                                 soft= 1.0;
1869                                 if(t<lar->spotbl && lar->spotbl!=0.0) {
1870                                         /* soft area */
1871                                         i= t/lar->spotbl;
1872                                         t= i*i;
1873                                         soft= (3.0*t-2.0*t*i);
1874                                         inpr*= soft;
1875                                 }
1876                                 lampdist*=inpr;
1877                         }
1878
1879                         if(lar->mode & LA_OSATEX) {
1880                                 shi->osatex= 1; /* signal for multitex() */
1881                                 
1882                                 O.dxlv[0]= lv[0] - (shi->co[0]-lar->co[0]+O.dxco[0])/ld;
1883                                 O.dxlv[1]= lv[1] - (shi->co[1]-lar->co[1]+O.dxco[1])/ld;
1884                                 O.dxlv[2]= lv[2] - (shi->co[2]-lar->co[2]+O.dxco[2])/ld;
1885
1886                                 O.dylv[0]= lv[0] - (shi->co[0]-lar->co[0]+O.dyco[0])/ld;
1887                                 O.dylv[1]= lv[1] - (shi->co[1]-lar->co[1]+O.dyco[1])/ld;
1888                                 O.dylv[2]= lv[2] - (shi->co[2]-lar->co[2]+O.dyco[2])/ld;
1889                         }
1890                         
1891                 }
1892
1893                 /* dot product and reflectivity*/
1894                 
1895                 inp= vn[0]*lv[0] + vn[1]*lv[1] + vn[2]*lv[2];
1896                                 
1897                 if(lar->mode & LA_NO_DIFF) {
1898                         i= 0.0; // skip shaders
1899                 }
1900                 else if(lar->type==LA_HEMI) {
1901                         i= 0.5*inp + 0.5;
1902                 }
1903                 else {
1904                 
1905                         if(lar->type==LA_AREA) {
1906                                 /* single sided */
1907                                 if(lv[0]*lar->vec[0]+lv[1]*lar->vec[1]+lv[2]*lar->vec[2]>0.0)
1908                                         inp= area_lamp_energy(shi->co, shi->vn, lar);
1909                                 else inp= 0.0;
1910                         }
1911                         
1912                         /* diffuse shaders (oren nayer gets inp from area light) */
1913                         if(ma->diff_shader==MA_DIFF_ORENNAYAR) i= OrenNayar_Diff_i(inp, vn, lv, view, ma->roughness);
1914                         else if(ma->diff_shader==MA_DIFF_TOON) i= Toon_Diff(vn, lv, view, ma->param[0], ma->param[1]);
1915                         else i= inp;    // Lambert
1916                 }
1917                 
1918                 if(i>0.0) {
1919                         i*= lampdist*ma->ref;
1920                 }
1921
1922                 /* shadow and spec, (lampdist==0 outside spot) */
1923                 if(lampdist> 0.0) {
1924                         
1925                         if(i>0.0 && (R.r.mode & R_SHADOW)) {
1926                                 if(ma->mode & MA_SHADOW) {
1927                                         
1928                                         if(lar->shb) {
1929                                                 shadfac[3] = testshadowbuf(lar->shb, shi->co, inp);
1930                                         }
1931                                         else if(lar->type==LA_HEMI);    // no shadow
1932                                         else if(lar->mode & LA_SHAD_RAY) {
1933                                                 // this extra 0.001 prevents boundary cases (shadow on smooth sphere)
1934                                                 if((shi->vlr->n[0]*lv[0] + shi->vlr->n[1]*lv[1] + shi->vlr->n[2]*lv[2]) > -0.001) 
1935                                                         ray_shadow(shi, lar, shadfac, mask);
1936                                                 else shadfac[3]= 0.0;
1937                                         }
1938
1939                                         /* warning, here it skips the loop */
1940                                         if(lar->mode & LA_ONLYSHADOW) {
1941                                                 
1942                                                 shadfac[3]= i*lar->energy*(1.0-shadfac[3]);
1943                                                 shr->diff[0] -= shadfac[3];
1944                                                 shr->diff[1] -= shadfac[3];
1945                                                 shr->diff[2] -= shadfac[3];
1946                                                 
1947                                                 continue;
1948                                         }
1949                                         
1950                                         if(shadfac[3]==0.0) continue;
1951
1952                                         i*= shadfac[3];
1953                                 }
1954                         }
1955                 
1956                         /* specularity */
1957                         if(shadfac[3]>0.0 && ma->spec!=0.0 && !(lar->mode & LA_NO_SPEC)) {
1958                                 
1959                                 if(lar->type==LA_HEMI) {
1960                                         /* hemi uses no spec shaders (yet) */
1961                                         
1962                                         lv[0]+= view[0];
1963                                         lv[1]+= view[1];
1964                                         lv[2]+= view[2];
1965                                         
1966                                         Normalise(lv);
1967                                         
1968                                         t= vn[0]*lv[0]+vn[1]*lv[1]+vn[2]*lv[2];
1969                                         
1970                                         if(lar->type==LA_HEMI) {
1971                                                 t= 0.5*t+0.5;
1972                                         }
1973                                         
1974                                         t= shadfac[3]*ma->spec*spec(t, ma->har);
1975                                         shr->spec[0]+= t*(lar->r * ma->specr);
1976                                         shr->spec[1]+= t*(lar->g * ma->specg);
1977                                         shr->spec[2]+= t*(lar->b * ma->specb);
1978                                 }
1979                                 else {
1980                                         /* specular shaders */
1981                                         float specfac;
1982
1983                                         if(ma->spec_shader==MA_SPEC_PHONG) 
1984                                                 specfac= Phong_Spec(vn, lv, view, ma->har);
1985                                         else if(ma->spec_shader==MA_SPEC_COOKTORR) 
1986                                                 specfac= CookTorr_Spec(vn, lv, view, ma->har);
1987                                         else if(ma->spec_shader==MA_SPEC_BLINN) 
1988                                                 specfac= Blinn_Spec(vn, lv, view, ma->refrac, (float)ma->har);
1989                                         else 
1990                                                 specfac= Toon_Spec(vn, lv, view, ma->param[2], ma->param[3]);
1991                                 
1992                                         /* area lamp correction */
1993                                         if(lar->type==LA_AREA) specfac*= inp;
1994                                         
1995                                         t= shadfac[3]*ma->spec*lampdist*specfac;
1996                                         
1997                                         shr->spec[0]+= t*(lar->r * ma->specr);
1998                                         shr->spec[1]+= t*(lar->g * ma->specg);
1999                                         shr->spec[2]+= t*(lar->b * ma->specb);
2000                                 }
2001                         }
2002                 }
2003                 
2004                 /* in case 'no diffuse' we still do most calculus, spec can be in shadow */
2005                 if(i>0.0 && !(lar->mode & LA_NO_DIFF)) {
2006                         if(ma->mode & MA_SHADOW_TRA) {
2007                                 shr->diff[0]+= i*shadfac[0]*lar->r;
2008                                 shr->diff[1]+= i*shadfac[1]*lar->g;
2009                                 shr->diff[2]+= i*shadfac[2]*lar->b;
2010                         }
2011                         else {
2012                                 shr->diff[0]+= i*lar->r;
2013                                 shr->diff[1]+= i*lar->g;
2014                                 shr->diff[2]+= i*lar->b;
2015                         }
2016                 }
2017         }
2018
2019         if(ma->mode & (MA_ZTRA|MA_RAYTRANSP)) {
2020                 if(ma->fresnel_tra!=1.0) 
2021                         ma->alpha*= fresnel_fac(shi->view, shi->vn, ma->fresnel_tra_i, ma->fresnel_tra);
2022
2023                 if(ma->spectra!=0.0) {
2024
2025                         t = MAX3(shr->spec[0], shr->spec[1], shr->spec[2]);
2026                         t *= ma->spectra;
2027                         if(t>1.0) t= 1.0;
2028                         ma->alpha= (1.0-t)*ma->alpha+t;
2029                 }
2030         }
2031
2032         shr->alpha= ma->alpha;
2033
2034         if(shr->spec[0]<0.0) shr->spec[0]= 0.0;
2035         if(shr->spec[1]<0.0) shr->spec[1]= 0.0;
2036         if(shr->spec[2]<0.0) shr->spec[2]= 0.0;
2037
2038         
2039         shr->diff[0]+= ma->amb*shi->rad[0];
2040         shr->diff[0]*= ma->r;
2041         shr->diff[0]+= ma->ambr;
2042         if(shr->diff[0]<0.0) shr->diff[0]= 0.0;
2043         
2044         shr->diff[1]+= ma->amb*shi->rad[1];
2045         shr->diff[1]*= ma->g;
2046         shr->diff[1]+= ma->ambg;
2047         if(shr->diff[1]<0.0) shr->diff[1]= 0.0;
2048         
2049         shr->diff[2]+= ma->amb*shi->rad[2];
2050         shr->diff[2]*= ma->b;
2051         shr->diff[2]+= ma->ambb;
2052         if(shr->diff[2]<0.0) shr->diff[2]= 0.0;
2053         
2054         /* refcol is for envmap only */
2055         if(shi->refcol[0]!=0.0) {
2056                 shr->diff[0]= ma->mirr*shi->refcol[1] + (1.0 - ma->mirr*shi->refcol[0])*shr->diff[0];
2057                 shr->diff[1]= ma->mirg*shi->refcol[2] + (1.0 - ma->mirg*shi->refcol[0])*shr->diff[1];
2058                 shr->diff[2]= ma->mirb*shi->refcol[3] + (1.0 - ma->mirb*shi->refcol[0])*shr->diff[2];
2059         }
2060
2061 }
2062
2063 void shade_input_set_coords(ShadeInput *shi, float u, float v, int i1, int i2, int i3)
2064 {
2065         VertRen *v1, *v2, *v3;
2066         VlakRen *vlr= shi->vlr;
2067         float l, dl;
2068         short texco= shi->matren->texco;
2069         int mode= shi->matren->mode;
2070         char p1, p2, p3;
2071         
2072         /* for rendering of quads, the following values are used to denote vertices:
2073            0 1 2        scanline tria & first half quad, and ray tria
2074            0 2 3    scanline 2nd half quad
2075            0 1 3    raytracer first half quad
2076            2 1 3    raytracer 2nd half quad
2077         */
2078
2079         if(i1==0) {
2080                 v1= vlr->v1;
2081                 p1= ME_FLIPV1;
2082         } else {
2083                 v1= vlr->v3;
2084                 p1= ME_FLIPV3;
2085         }
2086
2087         if(i2==1) {
2088                 v2= vlr->v2;
2089                 p2= ME_FLIPV2;
2090         } else {
2091                 v2= vlr->v3;
2092                 p2= ME_FLIPV3;
2093         }
2094         
2095         if(i3==2) {
2096                 v3= vlr->v3;
2097                 p3= ME_FLIPV3;
2098         } else {
2099                 v3= vlr->v4;
2100                 p3= ME_FLIPV4;
2101         }
2102
2103         /* calculate U and V, for scanline (normal u and v are -1 to 0) */
2104         if(u==1.0) {
2105                 if( (vlr->flag & R_SMOOTH) || (texco & NEED_UV)) {
2106                         float detsh, t00, t10, t01, t11;
2107                         
2108                         if(vlr->snproj==0) {
2109                                 t00= v3->co[0]-v1->co[0]; t01= v3->co[1]-v1->co[1];
2110                                 t10= v3->co[0]-v2->co[0]; t11= v3->co[1]-v2->co[1];
2111                         }
2112                         else if(vlr->snproj==1) {
2113                                 t00= v3->co[0]-v1->co[0]; t01= v3->co[2]-v1->co[2];
2114                                 t10= v3->co[0]-v2->co[0]; t11= v3->co[2]-v2->co[2];
2115                         }
2116                         else {
2117                                 t00= v3->co[1]-v1->co[1]; t01= v3->co[2]-v1->co[2];
2118                                 t10= v3->co[1]-v2->co[1]; t11= v3->co[2]-v2->co[2];
2119                         }
2120                         
2121                         detsh= t00*t11-t10*t01;
2122                         t00/= detsh; t01/=detsh; 
2123                         t10/=detsh; t11/=detsh;
2124                 
2125                         if(vlr->snproj==0) {
2126                                 u= (shi->co[0]-v3->co[0])*t11-(shi->co[1]-v3->co[1])*t10;
2127                                 v= (shi->co[1]-v3->co[1])*t00-(shi->co[0]-v3->co[0])*t01;
2128                                 if(shi->osatex) {
2129                                         O.dxuv[0]=  O.dxco[0]*t11- O.dxco[1]*t10;
2130                                         O.dxuv[1]=  O.dxco[1]*t00- O.dxco[0]*t01;
2131                                         O.dyuv[0]=  O.dyco[0]*t11- O.dyco[1]*t10;
2132                                         O.dyuv[1]=  O.dyco[1]*t00- O.dyco[0]*t01;
2133                                 }
2134                         }
2135                         else if(vlr->snproj==1) {
2136                                 u= (shi->co[0]-v3->co[0])*t11-(shi->co[2]-v3->co[2])*t10;
2137                                 v= (shi->co[2]-v3->co[2])*t00-(shi->co[0]-v3->co[0])*t01;
2138                                 if(shi->osatex) {
2139                                         O.dxuv[0]=  O.dxco[0]*t11- O.dxco[2]*t10;
2140                                         O.dxuv[1]=  O.dxco[2]*t00- O.dxco[0]*t01;
2141                                         O.dyuv[0]=  O.dyco[0]*t11- O.dyco[2]*t10;
2142                                         O.dyuv[1]=  O.dyco[2]*t00- O.dyco[0]*t01;
2143                                 }
2144                         }
2145                         else {
2146                                 u= (shi->co[1]-v3->co[1])*t11-(shi->co[2]-v3->co[2])*t10;
2147                                 v= (shi->co[2]-v3->co[2])*t00-(shi->co[1]-v3->co[1])*t01;
2148                                 if(shi->osatex) {
2149                                         O.dxuv[0]=  O.dxco[1]*t11- O.dxco[2]*t10;
2150                                         O.dxuv[1]=  O.dxco[2]*t00- O.dxco[1]*t01;
2151                                         O.dyuv[0]=  O.dyco[1]*t11- O.dyco[2]*t10;
2152                                         O.dyuv[1]=  O.dyco[2]*t00- O.dyco[1]*t01;
2153                                 }
2154                         }
2155                 }
2156         
2157         }
2158         l= 1.0+u+v;
2159         
2160         /* calculate punos (vertexnormals) */
2161         if(vlr->flag & R_SMOOTH) { 
2162                 float n1[3], n2[3], n3[3];
2163                 
2164                 if(vlr->puno & p1) {
2165                         n1[0]= -v1->n[0]; n1[1]= -v1->n[1]; n1[2]= -v1->n[2];
2166                 } else {
2167                         n1[0]= v1->n[0]; n1[1]= v1->n[1]; n1[2]= v1->n[2];
2168                 }
2169                 if(vlr->puno & p2) {
2170                         n2[0]= -v2->n[0]; n2[1]= -v2->n[1]; n2[2]= -v2->n[2];
2171                 } else {
2172                         n2[0]= v2->n[0]; n2[1]= v2->n[1]; n2[2]= v2->n[2];
2173                 }
2174                 
2175                 if(vlr->puno & p3) {
2176                         n3[0]= -v3->n[0]; n3[1]= -v3->n[1]; n3[2]= -v3->n[2];
2177                 } else {
2178                         n3[0]= v3->n[0]; n3[1]= v3->n[1]; n3[2]= v3->n[2];
2179                 }
2180
2181                 shi->vn[0]= l*n3[0]-u*n1[0]-v*n2[0];
2182                 shi->vn[1]= l*n3[1]-u*n1[1]-v*n2[1];
2183                 shi->vn[2]= l*n3[2]-u*n1[2]-v*n2[2];
2184
2185                 Normalise(shi->vn);
2186
2187                 if(shi->osatex && (texco & (TEXCO_NORM|TEXCO_REFL)) ) {
2188                         dl= O.dxuv[0]+O.dxuv[1];
2189                         O.dxno[0]= dl*n3[0]-O.dxuv[0]*n1[0]-O.dxuv[1]*n2[0];
2190                         O.dxno[1]= dl*n3[1]-O.dxuv[0]*n1[1]-O.dxuv[1]*n2[1];
2191                         O.dxno[2]= dl*n3[2]-O.dxuv[0]*n1[2]-O.dxuv[1]*n2[2];
2192                         dl= O.dyuv[0]+O.dyuv[1];
2193                         O.dyno[0]= dl*n3[0]-O.dyuv[0]*n1[0]-O.dyuv[1]*n2[0];
2194                         O.dyno[1]= dl*n3[1]-O.dyuv[0]*n1[1]-O.dyuv[1]*n2[1];
2195                         O.dyno[2]= dl*n3[2]-O.dyuv[0]*n1[2]-O.dyuv[1]*n2[2];
2196
2197                 }
2198         }
2199         else {
2200                 VECCOPY(shi->vn, vlr->n);
2201         }
2202
2203         /* texture coordinates. O.dxuv O.dyuv have been set */
2204         if(texco & NEED_UV) {
2205                 if(texco & TEXCO_ORCO) {
2206                         if(v1->orco) {
2207                                 float *o1, *o2, *o3;
2208                                 
2209                                 o1= v1->orco;
2210                                 o2= v2->orco;
2211                                 o3= v3->orco;
2212                                 
2213                                 shi->lo[0]= l*o3[0]-u*o1[0]-v*o2[0];
2214                                 shi->lo[1]= l*o3[1]-u*o1[1]-v*o2[1];
2215                                 shi->lo[2]= l*o3[2]-u*o1[2]-v*o2[2];
2216         
2217                                 if(shi->osatex) {
2218                                         dl= O.dxuv[0]+O.dxuv[1];
2219                                         O.dxlo[0]= dl*o3[0]-O.dxuv[0]*o1[0]-O.dxuv[1]*o2[0];
2220                                         O.dxlo[1]= dl*o3[1]-O.dxuv[0]*o1[1]-O.dxuv[1]*o2[1];
2221                                         O.dxlo[2]= dl*o3[2]-O.dxuv[0]*o1[2]-O.dxuv[1]*o2[2];
2222                                         dl= O.dyuv[0]+O.dyuv[1];
2223                                         O.dylo[0]= dl*o3[0]-O.dyuv[0]*o1[0]-O.dyuv[1]*o2[0];
2224                                         O.dylo[1]= dl*o3[1]-O.dyuv[0]*o1[1]-O.dyuv[1]*o2[1];
2225                                         O.dylo[2]= dl*o3[2]-O.dyuv[0]*o1[2]-O.dyuv[1]*o2[2];
2226                                 }
2227                         }
2228                 }
2229                 
2230                 if(texco & TEXCO_GLOB) {
2231                         VECCOPY(shi->gl, shi->co);
2232                         MTC_Mat4MulVecfl(R.viewinv, shi->gl);
2233                         if(shi->osatex) {
2234                                 VECCOPY(O.dxgl, O.dxco);
2235                                 MTC_Mat3MulVecfl(R.imat, O.dxco);
2236                                 VECCOPY(O.dygl, O.dyco);
2237                                 MTC_Mat3MulVecfl(R.imat, O.dyco);
2238                         }
2239                 }
2240                 if((texco & TEXCO_UV) || (mode & (MA_VERTEXCOL|MA_FACETEXTURE)))  {
2241                         int j1=i1, j2=i2, j3=i3;
2242                         
2243                         /* to prevent storing new tfaces or vcols, we check a split runtime */
2244                         /*              4---3           4---3 */
2245                         /*              |\ 1|   or  |1 /| */
2246                         /*              |0\ |           |/ 0| */
2247                         /*              1---2           1---2   0 = orig face, 1 = new face */
2248                         
2249                         /* Update vert nums to point to correct verts of original face */
2250                         if(vlr->flag & R_DIVIDE_24) {  
2251                                 if(vlr->flag & R_FACE_SPLIT) {
2252                                         j1++; j2++; j3++;
2253                                 }
2254                                 else {
2255                                         j3++;
2256                                 }
2257                         }
2258                         else if(vlr->flag & R_FACE_SPLIT) {
2259                                 j2++; j3++; 
2260                         }
2261                         
2262                         if(mode & MA_VERTEXCOL) {
2263                                 
2264                                 if(vlr->vcol) {
2265                                         char *cp1, *cp2, *cp3;
2266                                         
2267                                         cp1= (char *)(vlr->vcol+j1);
2268                                         cp2= (char *)(vlr->vcol+j2);
2269                                         cp3= (char *)(vlr->vcol+j3);
2270
2271                                         shi->vcol[0]= (l*cp3[3]-u*cp1[3]-v*cp2[3])/255.0;
2272                                         shi->vcol[1]= (l*cp3[2]-u*cp1[2]-v*cp2[2])/255.0;
2273                                         shi->vcol[2]= (l*cp3[1]-u*cp1[1]-v*cp2[1])/255.0;
2274                                         
2275                                 }
2276                                 else {
2277                                         shi->vcol[0]= 0.0;
2278                                         shi->vcol[1]= 0.0;
2279                                         shi->vcol[2]= 0.0;
2280                                 }
2281                         }
2282                         if(vlr->tface) {
2283                                 float *uv1, *uv2, *uv3;
2284                                 
2285                                 uv1= vlr->tface->uv[j1];
2286                                 uv2= vlr->tface->uv[j2];
2287                                 uv3= vlr->tface->uv[j3];
2288                                 
2289                                 shi->uv[0]= -1.0 + 2.0*(l*uv3[0]-u*uv1[0]-v*uv2[0]);
2290                                 shi->uv[1]= -1.0 + 2.0*(l*uv3[1]-u*uv1[1]-v*uv2[1]);
2291                                 shi->uv[2]= 0.0;        // texture.c assumes there are 3 coords
2292                                 
2293                                 if(shi->osatex) {
2294                                         float duv[2];
2295                                         
2296                                         dl= O.dxuv[0]+O.dxuv[1];
2297                                         duv[0]= O.dxuv[0]; 
2298                                         duv[1]= O.dxuv[1];
2299                                         
2300                                         O.dxuv[0]= 2.0*(dl*uv3[0]-duv[0]*uv1[0]-duv[1]*uv2[0]);
2301                                         O.dxuv[1]= 2.0*(dl*uv3[1]-duv[0]*uv1[1]-duv[1]*uv2[1]);
2302         
2303                                         dl= O.dyuv[0]+O.dyuv[1];
2304                                         duv[0]= O.dyuv[0]; 
2305                                         duv[1]= O.dyuv[1];
2306         
2307                                         O.dyuv[0]= 2.0*(dl*uv3[0]-duv[0]*uv1[0]-duv[1]*uv2[0]);
2308                                         O.dyuv[1]= 2.0*(dl*uv3[1]-duv[0]*uv1[1]-duv[1]*uv2[1]);
2309                                 }
2310                                 
2311                                 if(mode & MA_FACETEXTURE) {
2312                                         if((mode & MA_VERTEXCOL)==0) {
2313                                                 shi->vcol[0]= 1.0;
2314                                                 shi->vcol[1]= 1.0;
2315                                                 shi->vcol[2]= 1.0;
2316                                         }
2317                                         if(vlr->tface) render_realtime_texture(shi);
2318                                 }
2319                         }
2320                         else {
2321                                 shi->uv[0]= 2.0*(u+.5);
2322                                 shi->uv[1]= 2.0*(v+.5);
2323                                 shi->uv[2]= 0.0;        // texture.c assumes there are 3 coords
2324                         }
2325                 }
2326                 if(texco & TEXCO_NORM) {
2327                         shi->orn[0]= shi->vn[0];
2328                         shi->orn[1]= shi->vn[1];
2329                         shi->orn[2]= shi->vn[2];
2330                 }
2331                                 
2332                 if(mode & MA_RADIO) {
2333                         shi->rad[0]= (l*v3->rad[0] - u*v1->rad[0] - v*v2->rad[0]);
2334                         shi->rad[1]= (l*v3->rad[1] - u*v1->rad[1] - v*v2->rad[1]);
2335                         shi->rad[2]= (l*v3->rad[2] - u*v1->rad[2] - v*v2->rad[2]);
2336                 }
2337                 else {
2338                         shi->rad[0]= shi->rad[1]= shi->rad[2]= 0.0;
2339                 }
2340                         
2341                 if(texco & TEXCO_REFL) {
2342                         /* mirror reflection colour textures (and envmap) */
2343                         calc_R_ref(shi);
2344                 }
2345                 
2346         }
2347         else {
2348                 shi->rad[0]= shi->rad[1]= shi->rad[2]= 0.0;
2349         }
2350 }
2351
2352   /* x,y: window coordinate from 0 to rectx,y */
2353   /* return pointer to rendered face */
2354   
2355 float bluroffsx, bluroffsy;     // set in initrender.c (ton)
2356
2357 void *shadepixel(float x, float y, int vlaknr, int mask, float *col)
2358 {
2359         ShadeResult shr;
2360         ShadeInput shi;
2361         VlakRen *vlr=NULL;
2362         
2363         if(vlaknr< 0) { /* error */
2364                 return NULL;
2365         }
2366         /* currently in use for dithering soft shadow */
2367         shi.xs= x;
2368         shi.ys= y;
2369         
2370         if(vlaknr==0) { /* sky */
2371                 col[0]= 0.0; col[1]= 0.0; col[2]= 0.0; col[3]= 0.0;
2372         }
2373         else if( (vlaknr & 0x7FFFFF) <= R.totvlak) {
2374                 VertRen *v1, *v2, *v3;
2375                 float alpha, fac, dvlak, deler;
2376                 
2377                 vlr= RE_findOrAddVlak( (vlaknr-1) & 0x7FFFFF);
2378                 
2379                 shi.mat= vlr->mat;
2380                 shi.matren= shi.mat->ren;
2381                 shi.vlr= vlr;
2382                 shi.osatex= (shi.matren->texco & TEXCO_OSA);
2383
2384                 v1= vlr->v1;
2385                 dvlak= v1->co[0]*vlr->n[0]+v1->co[1]*vlr->n[1]+v1->co[2]*vlr->n[2];
2386
2387                 /* COXYZ AND VIEW VECTOR  */
2388                 shi.view[0]= (x+(R.xstart)+1.0+bluroffsx);
2389
2390                 if(R.flag & R_SEC_FIELD) {
2391                         if(R.r.mode & R_ODDFIELD) shi.view[1]= (y+R.ystart+0.5)*R.ycor;
2392                         else shi.view[1]= (y+R.ystart+1.5)*R.ycor;
2393                 }
2394                 else shi.view[1]= (y+R.ystart+1.0+bluroffsy)*R.ycor;
2395                 
2396                 shi.view[2]= -R.viewfac;
2397
2398                 if(R.r.mode & R_PANORAMA) {
2399                         float panoco, panosi, u, v;
2400                         panoco = getPanovCo();
2401                         panosi = getPanovSi();
2402
2403                         u= shi.view[0]; v= shi.view[2];
2404                         shi.view[0]= panoco*u + panosi*v;
2405                         shi.view[2]= -panosi*u + panoco*v;
2406                 }
2407
2408                 deler= vlr->n[0]*shi.view[0] + vlr->n[1]*shi.view[1] + vlr->n[2]*shi.view[2];
2409                 if (deler!=0.0) fac= R.zcor= dvlak/deler;
2410                 else fac= R.zcor= 0.0;
2411                 
2412                 shi.co[0]= fac*shi.view[0];
2413                 shi.co[1]= fac*shi.view[1];
2414                 shi.co[2]= fac*shi.view[2];
2415                 
2416                 /* pixel dx/dy for render coord */
2417                 if(shi.osatex || (R.r.mode & R_SHADOW) ) {
2418                         float u= dvlak/(deler-vlr->n[0]);
2419                         float v= dvlak/(deler- R.ycor*vlr->n[1]);
2420
2421                         O.dxco[0]= shi.co[0]- (shi.view[0]-1.0)*u;
2422                         O.dxco[1]= shi.co[1]- (shi.view[1])*u;
2423                         O.dxco[2]= shi.co[2]- (shi.view[2])*u;
2424
2425                         O.dyco[0]= shi.co[0]- (shi.view[0])*v;
2426                         O.dyco[1]= shi.co[1]- (shi.view[1]-1.0*R.ycor)*v;
2427                         O.dyco[2]= shi.co[2]- (shi.view[2])*v;
2428
2429                 }
2430
2431                 fac= Normalise(shi.view);
2432                 R.zcor*= fac;   /* for mist */
2433                 
2434                 if(shi.osatex) {
2435                         if( (shi.matren->texco & TEXCO_REFL) ) {
2436                                 O.dxview= -1.0/fac;
2437                                 O.dyview= -R.ycor/fac;
2438                         }
2439                 }
2440                 
2441                 /* calcuate normals, texture coords, vertex colors, etc */
2442                 if(vlaknr & 0x800000)
2443                         shade_input_set_coords(&shi, 1.0, 1.0, 0, 2, 3);
2444                 else 
2445                         shade_input_set_coords(&shi, 1.0, 1.0, 0, 1, 2);
2446
2447                 /* this only avalailable for scanline */
2448                 if(shi.matren->texco & TEXCO_WINDOW) {
2449                         shi.winco[0]= (x+(R.xstart))/(float)R.afmx;
2450                         shi.winco[1]= (y+(R.ystart))/(float)R.afmy;
2451                 }
2452                 /* after this the u and v AND O.dxuv and O.dyuv are incorrect */
2453                 if(shi.matren->texco & TEXCO_STICKY) {
2454                         if(v1->sticky) {
2455                                 extern float Zmulx, Zmuly;
2456                                 float *o1, *o2, *o3, hox, hoy, l, dl, u, v;
2457                                 float s00, s01, s10, s11, detsh;
2458                                 
2459                                 if(vlaknr & 0x800000) {
2460                                         v2= vlr->v3; v3= vlr->v4;
2461                                 } else {
2462                                         v2= vlr->v2; v3= vlr->v3;
2463                                 }
2464                                 
2465                                 s00= v3->ho[0]/v3->ho[3] - v1->ho[0]/v1->ho[3];
2466                                 s01= v3->ho[1]/v3->ho[3] - v1->ho[1]/v1->ho[3];
2467                                 s10= v3->ho[0]/v3->ho[3] - v2->ho[0]/v2->ho[3];
2468                                 s11= v3->ho[1]/v3->ho[3] - v2->ho[1]/v2->ho[3];
2469                                 
2470                                 detsh= s00*s11-s10*s01;
2471                                 s00/= detsh; s01/=detsh; 
2472                                 s10/=detsh; s11/=detsh;
2473         
2474                                 /* recalc u and v again */
2475                                 hox= x/Zmulx -1.0;
2476                                 hoy= y/Zmuly -1.0;
2477                                 u= (hox - v3->ho[0]/v3->ho[3])*s11 - (hoy - v3->ho[1]/v3->ho[3])*s10;
2478                                 v= (hoy - v3->ho[1]/v3->ho[3])*s00 - (hox - v3->ho[0]/v3->ho[3])*s01;
2479                                 l= 1.0+u+v;
2480                                 
2481                                 o1= v1->sticky;
2482                                 o2= v2->sticky;
2483                                 o3= v3->sticky;
2484                                 
2485                                 shi.sticky[0]= l*o3[0]-u*o1[0]-v*o2[0];
2486                                 shi.sticky[1]= l*o3[1]-u*o1[1]-v*o2[1];
2487         
2488                                 if(shi.osatex) {
2489                                         O.dxuv[0]=  s11/Zmulx;
2490                                         O.dxuv[1]=  - s01/Zmulx;
2491                                         O.dyuv[0]=  - s10/Zmuly;
2492                                         O.dyuv[1]=  s00/Zmuly;
2493                                         
2494                                         dl= O.dxuv[0]+O.dxuv[1];
2495                                         O.dxsticky[0]= dl*o3[0]-O.dxuv[0]*o1[0]-O.dxuv[1]*o2[0];
2496                                         O.dxsticky[1]= dl*o3[1]-O.dxuv[0]*o1[1]-O.dxuv[1]*o2[1];
2497                                         dl= O.dyuv[0]+O.dyuv[1];
2498                                         O.dysticky[0]= dl*o3[0]-O.dyuv[0]*o1[0]-O.dyuv[1]*o2[0];
2499                                         O.dysticky[1]= dl*o3[1]-O.dyuv[0]*o1[1]-O.dyuv[1]*o2[1];
2500                                 }
2501                         }
2502                 }
2503                 
2504                 /* ------  main shading loop */
2505                 shade_lamp_loop(&shi, &shr, mask);
2506                 
2507                 if(shi.matren->translucency!=0.0) {
2508                         ShadeResult shr_t;
2509                         
2510                         VecMulf(shi.vn, -1.0);
2511                         VecMulf(shi.vlr->n, -1.0);
2512                         shade_lamp_loop(&shi, &shr_t, mask);
2513                         shr.diff[0]+= shi.matren->translucency*shr_t.diff[0];
2514                         shr.diff[1]+= shi.matren->translucency*shr_t.diff[1];
2515                         shr.diff[2]+= shi.matren->translucency*shr_t.diff[2];
2516                         VecMulf(shi.vn, -1.0);
2517                         VecMulf(shi.vlr->n, -1.0);
2518                 }
2519                 
2520                 if(R.r.mode & R_RAYTRACE) {
2521                         if(shi.matren->ray_mirror!=0.0 || (shi.mat->mode & MA_RAYTRANSP && shr.alpha!=1.0)) {
2522                                 ray_trace(&shi, &shr, mask);
2523                         }
2524                 }
2525                 else {
2526                         // doesnt look 'correct', but is better for preview, plus envmaps dont raytrace this
2527                         if(shi.mat->mode & MA_RAYTRANSP) shr.alpha= 1.0;
2528                 }
2529                 
2530                 VecAddf(col, shr.diff, shr.spec);
2531                 
2532                 /* exposure correction */
2533                 if(R.wrld.exp!=0.0 || R.wrld.range!=1.0) {
2534                         if((shi.matren->mode & MA_SHLESS)==0) {
2535                                 col[0]= R.wrld.linfac*(1.0-exp( col[0]*R.wrld.logfac) );
2536                                 col[1]= R.wrld.linfac*(1.0-exp( col[1]*R.wrld.logfac) );
2537                                 col[2]= R.wrld.linfac*(1.0-exp( col[2]*R.wrld.logfac) );
2538                         }
2539                 }
2540                 
2541                 /* MIST */
2542                 if( (R.wrld.mode & WO_MIST) && (shi.matren->mode & MA_NOMIST)==0 ){
2543                         alpha= mistfactor(shi.co);
2544                 }
2545                 else alpha= 1.0;
2546
2547                 if(shr.alpha!=1.0 || alpha!=1.0) {
2548                         fac= alpha*(shr.alpha);
2549                         
2550                         col[3]= fac;
2551                         col[0]*= fac;
2552                         col[1]*= fac;
2553                         col[2]*= fac;
2554                 }
2555                 else col[3]= 1.0;
2556         }
2557         
2558         if(R.flag & R_LAMPHALO) {
2559                 if(vlaknr<=0) { /* calc view vector and put shi.co at far */
2560                 
2561                         shi.view[0]= (x+(R.xstart)+1.0);
2562
2563                         if(R.flag & R_SEC_FIELD) {
2564                                 if(R.r.mode & R_ODDFIELD) shi.view[1]= (y+R.ystart+0.5)*R.ycor;
2565                                 else shi.view[1]= (y+R.ystart+1.5)*R.ycor;
2566                         }
2567                         else shi.view[1]= (y+R.ystart+1.0)*R.ycor;
2568                         
2569                         shi.view[2]= -R.viewfac;
2570                         
2571                         if(R.r.mode & R_PANORAMA) {
2572                                 float u,v, panoco, panosi;
2573                                 panoco = getPanovCo();
2574                                 panosi = getPanovSi();
2575                                 
2576                                 u= shi.view[0]; v= shi.view[2];
2577                                 shi.view[0]= panoco*u + panosi*v;
2578                                 shi.view[2]= -panosi*u + panoco*v;
2579                         }
2580
2581                         shi.co[2]= 0.0;
2582                         
2583                 }
2584                 renderspothalo(&shi, col);
2585         }
2586         
2587         return vlr;
2588 }
2589
2590 void shadepixel_short(float x, float y, int vlaknr, int mask, unsigned short *shortcol)
2591 {
2592         float colf[4];
2593         
2594         shadepixel(x, y, vlaknr, mask, colf);
2595
2596         if(colf[0]<=0.0) shortcol[0]= 0; else if(colf[0]>=1.0) shortcol[0]= 65535;
2597         else shortcol[0]= 65535.0*colf[0];
2598         if(colf[1]<=0.0) shortcol[1]= 0; else if(colf[1]>=1.0) shortcol[1]= 65535;
2599         else shortcol[1]= 65535.0*colf[1];
2600         if(colf[2]<=0.0) shortcol[2]= 0; else if(colf[2]>=1.0) shortcol[2]= 65535;
2601         else shortcol[2]= 65535.0*colf[2];
2602         if(colf[3]<=0.0) shortcol[3]= 0; else if(colf[3]>=1.0) shortcol[3]= 65535;
2603         else shortcol[3]= 65535.0*colf[3];
2604
2605         if(usegamtab) {
2606                 shortcol[0]= igamtab2[ shortcol[0] ];
2607                 shortcol[1]= igamtab2[ shortcol[1] ];
2608                 shortcol[2]= igamtab2[ shortcol[2] ];
2609         }
2610 }
2611
2612 PixStr *addpsmain()
2613 {
2614         PixStrMain *psm;
2615
2616         psm= &psmfirst;
2617
2618         while(psm->next) {
2619                 psm= psm->next;
2620         }
2621
2622         psm->next= (PixStrMain *)MEM_mallocN(sizeof(PixStrMain),"pixstrMain");
2623
2624         psm= psm->next;
2625         psm->next=0;
2626         psm->ps= (PixStr *)MEM_mallocN(4096*sizeof(PixStr),"pixstr");
2627         psmteller= 0;
2628
2629         return psm->ps;
2630 }
2631
2632 void freeps()
2633 {
2634         PixStrMain *psm,*next;
2635
2636         psm= &psmfirst;
2637
2638         while(psm) {
2639                 next= psm->next;
2640                 if(psm->ps) {
2641                         MEM_freeN(psm->ps);
2642                         psm->ps= 0;
2643                 }
2644                 if(psm!= &psmfirst) MEM_freeN(psm);
2645                 psm= next;
2646         }
2647
2648         psmfirst.next= 0;
2649         psmfirst.ps= 0;
2650 }
2651
2652 void addps(long *rd, int vlak, unsigned int z, short ronde)
2653 {
2654         static PixStr *prev;
2655         PixStr *ps, *last = NULL;
2656         int vlakand;
2657
2658         if( IS_A_POINTER_CODE(*rd)) {   
2659                 ps= (PixStr *) POINTER_FROM_CODE(*rd);
2660                 vlakand= (vlak & 0x7FFFFF);
2661                 
2662                 if( (ps->vlak0 & 0x7FFFFF) == vlakand ) return;
2663                 
2664                 while(ps) {
2665                         if( (ps->vlak & 0x7FFFFF) == vlakand ) {
2666                                 ps->mask |= (1<<ronde);
2667                                 return;
2668                         }
2669                         last= ps;
2670                         ps= ps->next;
2671                 }
2672
2673                 if((psmteller & 4095)==0) prev= addpsmain();
2674                 else prev++;
2675                 psmteller++;
2676
2677                 last->next= prev;
2678                 prev->next= 0;
2679                 prev->vlak= vlak;
2680                 prev->z= z;
2681                 prev->mask = (1<<ronde);
2682                 prev->ronde= ronde;
2683                 
2684                 return;
2685         }
2686
2687         /* make first PS (pixel struct) */
2688         if((psmteller & 4095)==0) prev= addpsmain();
2689         else prev++;
2690         psmteller++;
2691
2692         prev->next= 0;
2693         prev->vlak0= (int) *rd;
2694         prev->vlak= vlak;
2695         prev->z= z;
2696         prev->mask = (1<<ronde);
2697         prev->ronde= ronde;
2698         *rd= POINTER_TO_CODE(prev);
2699 }
2700
2701
2702 int count_mask(unsigned short mask)
2703 {
2704         return (cmask[mask & 255]+cmask[mask>>8]);
2705 }
2706
2707 float count_maskf(unsigned short mask)
2708 {
2709         return (fmask[mask & 255]+fmask[mask>>8]);
2710 }
2711
2712
2713 void add_filt_mask(unsigned int mask, unsigned short *col, unsigned int *rb1, unsigned int *rb2, unsigned int *rb3)
2714 {
2715         /* calc the value of mask */
2716         unsigned int a, maskand, maskshift;
2717         int j;
2718         unsigned short val, r, g, b, al;
2719         
2720         al= col[3];
2721         r= col[0];
2722         g= col[1];
2723         b= col[2];
2724
2725         maskand= (mask & 255);
2726         maskshift= (mask >>8);
2727
2728         for(j=2; j>=0; j--) {
2729
2730                 a= j;
2731
2732                 val= *(mask1[a] +maskand) + *(mask2[a] +maskshift);
2733                 if(val) {
2734                         rb1[3]+= val*al;
2735                         rb1[0]+= val*r;
2736                         rb1[1]+= val*g;
2737                         rb1[2]+= val*b;
2738                 }
2739                 a+=3;
2740
2741                 val= *(mask1[a] +maskand) + *(mask2[a] +maskshift);
2742                 if(val) {
2743                         rb2[3]+= val*al;
2744                         rb2[0]+= val*r;
2745                         rb2[1]+= val*g;
2746                         rb2[2]+= val*b;
2747                 }
2748                 a+=3;
2749
2750                 val= *(mask1[a] +maskand) + *(mask2[a] +maskshift);
2751                 if(val) {
2752                         rb3[3]+= val*al;
2753                         rb3[0]+= val*r;
2754                         rb3[1]+= val*g;
2755                         rb3[2]+= val*b;
2756                 }
2757
2758                 rb1+= 4;
2759                 rb2+= 4;
2760                 rb3+= 4;
2761         }
2762 }
2763
2764 void edge_enhance(void)
2765 {
2766         /* use zbuffer to define edges, add it to the image */
2767         int val, y, x, col, *rz, *rz1, *rz2, *rz3;
2768         char *cp;
2769         
2770         /* shift values in zbuffer 3 to the right */
2771         rz= (int *)R.rectz;
2772         if(rz==0) return;
2773         
2774         for(y=0; y<R.recty; y++) {
2775                 for(x=0; x<R.rectx; x++, rz++) {
2776                         (*rz)>>= 3;
2777                 }
2778         }
2779
2780         rz1= (int *)R.rectz;
2781         rz2= rz1+R.rectx;
2782         rz3= rz2+R.rectx;
2783         rz= (int *)R.rectot+R.rectx;
2784
2785         if(R.r.mode & R_OSA) {
2786                 cp= (char *)(R.rectaccu+R.rectx);
2787         }
2788         else {
2789                 cp= (char *)(R.rectot+R.rectx);
2790         }
2791
2792         /* rz itself does not seem to be used. */
2793         
2794         for(y=0; y<R.recty-2; y++) {
2795
2796                 rz++;
2797                 for(x=0; x<R.rectx-2; x++, rz++, rz1++, rz2++, rz3++, cp+=4) {
2798
2799                         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;
2800                         /* removed the abs... now, only front/back? pixels are           */
2801                         /* accentuated? No, the lines seem shifted strangely. the does   */
2802                         /* not seem to be any overlap? strange...                        */
2803 /*                      col= -( 12*rz2[1] */
2804 /*                                      -   rz1[0] - 2*rz1[1] -   rz1[2] */
2805 /*                                      - 2*rz2[0]            - 2*rz2[2] */
2806 /*                                      -   rz3[0] - 2*rz3[1] -   rz3[2]) /3; */
2807                         
2808                         col= (R.r.edgeint*col)>>14;
2809                         if(col>255) col= 255;
2810                         
2811                         if(col>0) {
2812                                 if(R.r.mode & R_OSA) {
2813                                         col/= R.osa;
2814                                         
2815                                         val= cp[3]+col;
2816                                         if(val>255) cp[3]= 255; else cp[3]= val;
2817                                 }
2818                                 else {
2819                                         val= cp[0]- col;
2820                                         if(val<0) cp[0]= 0; else cp[0]= val;
2821                                         val= cp[1]- col;
2822                                         if(val<0) cp[1]= 0; else cp[1]= val;
2823                                         val= cp[2]- col;
2824                                         if(val<0) cp[2]= 0; else cp[2]= val;
2825                                 }
2826                         }
2827                 }
2828                 rz++;
2829                 rz1+= 2;
2830                 rz2+= 2;
2831                 rz3+= 2;
2832                 cp+= 8;
2833         }
2834
2835 }
2836
2837 /* ********************* MAINLOOPS ******************** */
2838
2839 extern unsigned short *Acolrow;
2840 /*  short zbuffermetdehand(); */
2841 void zbufshadeDA(void)  /* Delta Accum Pixel Struct */
2842 {
2843         extern float Zjitx,Zjity;
2844         PixStr *ps;
2845         float xd, yd, xs, ys;
2846         unsigned int *rz, *rp, *rt, mask, fullmask;
2847         unsigned int  *rowbuf1, *rowbuf2, *rowbuf3, *rb1, *rb2, *rb3;
2848         int a, b;
2849         long *rd;
2850         unsigned short *colrb, *acol, shortcol[4];
2851         short v, x, y;
2852         char *colrt, tempcol[4];
2853
2854         R.rectdaps= MEM_callocN(sizeof(long)*R.rectx*R.recty+4,"zbufDArectd");
2855         if(R.flag & R_ZTRA) bgnaccumbuf();
2856
2857         psmteller= 0;
2858
2859         if(R.r.mode & R_EDGE) {
2860                 R.rectaccu= (unsigned int *)MEM_callocN(sizeof(int)*R.rectx*R.recty,"zbufshadeDA");
2861         }
2862
2863         for(v=0; v<R.osa; v++) {
2864
2865                 xd= jit[v][0];
2866                 yd= jit[v][1];
2867                 Zjitx= -xd;
2868                 Zjity= -yd;
2869
2870                 if((R.r.mode & R_MBLUR)==0) RE_local_printrenderinfo(0.0, v);
2871
2872                 /* RECTDELTA  */
2873                 fillrect(R.rectot,R.rectx,R.recty,0);
2874
2875                 zbufferall();
2876
2877                 if(v==0) {
2878                         a= R.rectx*R.recty;
2879                         rt= R.rectot;
2880                         rd= R.rectdaps;
2881                         while(a--) {
2882                                 *rd= (long)*rt;
2883                                 rd++; rt++;
2884                         }
2885                 }
2886                 else {
2887                         rd= R.rectdaps;
2888                         rp= R.rectot;
2889                         rz= R.rectz;
2890                         for(y=0; y<R.recty; y++) {
2891                                 for(x=0; x<R.rectx; x++, rp++, rd++) {
2892                                         if(*rd!= (long) *rp) {
2893                                                 addps(rd, *rp, *(rz+x), v);
2894                                         }
2895                                 }
2896                                 rz+= R.rectx;
2897                         }
2898                 }
2899                         /* 1 is for osa */
2900                 if(R.r.mode & R_EDGE) edge_enhance();
2901                 
2902                 if(RE_local_test_break()) break; 
2903         }