Merge of trunk into blender 2.5:
[blender.git] / source / blender / render / intern / source / pixelshading.c
1 /**
2  *
3  * ***** BEGIN GPL LICENSE BLOCK *****
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version. 
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software Foundation,
17  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18  *
19  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
20  * All rights reserved.
21  *
22  * Contributor(s): 2004-2006, Blender Foundation, full recode
23  *
24  * ***** END GPL LICENSE BLOCK *****
25  */
26
27 #include <float.h>
28 #include <math.h>
29 #include <string.h>
30 #include "BLI_arithb.h"
31
32 /* External modules: */
33 #include "IMB_imbuf_types.h"
34 #include "IMB_imbuf.h"
35 #include "MTC_matrixops.h"
36 #include "MTC_vectorops.h"
37
38 #include "DNA_camera_types.h"
39 #include "DNA_group_types.h"
40 #include "DNA_material_types.h"
41 #include "DNA_object_types.h"
42 #include "DNA_image_types.h"
43 #include "DNA_texture_types.h"
44 #include "DNA_lamp_types.h"
45
46 #include "BKE_image.h"
47 #include "BKE_global.h"
48 #include "BKE_material.h"
49 #include "BKE_texture.h"
50 #include "BKE_utildefines.h"
51
52 /* own module */
53 #include "render_types.h"
54 #include "renderpipeline.h"
55 #include "renderdatabase.h"
56 #include "texture.h"
57 #include "pixelblending.h"
58 #include "rendercore.h"
59 #include "shadbuf.h"
60 #include "pixelshading.h"
61 #include "sunsky.h"
62
63 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
64 /* defined in pipeline.c, is hardcopy of active dynamic allocated Render */
65 /* only to be used here in this file, it's for speed */
66 extern struct Render R;
67 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
68
69
70 extern float hashvectf[];
71
72 static void render_lighting_halo(HaloRen *har, float *colf)
73 {
74         GroupObject *go;
75         LampRen *lar;
76         float i, inp, inpr, rco[3], dco[3], lv[3], lampdist, ld, t, *vn;
77         float ir, ig, ib, shadfac, soft, lacol[3];
78         
79         ir= ig= ib= 0.0;
80         
81         VECCOPY(rco, har->co);  
82         dco[0]=dco[1]=dco[2]= 1.0/har->rad;
83         
84         vn= har->no;
85         
86         for(go=R.lights.first; go; go= go->next) {
87                 lar= go->lampren;
88                 
89                 /* test for lamplayer */
90                 if(lar->mode & LA_LAYER) if((lar->lay & har->lay)==0) continue;
91                 
92                 /* lampdist cacluation */
93                 if(lar->type==LA_SUN || lar->type==LA_HEMI) {
94                         VECCOPY(lv, lar->vec);
95                         lampdist= 1.0;
96                 }
97                 else {
98                         lv[0]= rco[0]-lar->co[0];
99                         lv[1]= rco[1]-lar->co[1];
100                         lv[2]= rco[2]-lar->co[2];
101                         ld= sqrt(lv[0]*lv[0]+lv[1]*lv[1]+lv[2]*lv[2]);
102                         lv[0]/= ld;
103                         lv[1]/= ld;
104                         lv[2]/= ld;
105                         
106                         /* ld is re-used further on (texco's) */
107                         
108                         if(lar->mode & LA_QUAD) {
109                                 t= 1.0;
110                                 if(lar->ld1>0.0)
111                                         t= lar->dist/(lar->dist+lar->ld1*ld);
112                                 if(lar->ld2>0.0)
113                                         t*= lar->distkw/(lar->distkw+lar->ld2*ld*ld);
114                                 
115                                 lampdist= t;
116                         }
117                         else {
118                                 lampdist= (lar->dist/(lar->dist+ld));
119                         }
120                         
121                         if(lar->mode & LA_SPHERE) {
122                                 t= lar->dist - ld;
123                                 if(t<0.0) continue;
124                                 
125                                 t/= lar->dist;
126                                 lampdist*= (t);
127                         }
128                         
129                 }
130                 
131                 lacol[0]= lar->r;
132                 lacol[1]= lar->g;
133                 lacol[2]= lar->b;
134                 
135                 if(lar->mode & LA_TEXTURE) {
136                         ShadeInput shi;
137                         
138                         /* Warning, This is not that nice, and possibly a bit slow,
139                         however some variables were not initialized properly in, unless using shade_input_initialize(...), we need to do a memset */
140                         memset(&shi, 0, sizeof(ShadeInput)); 
141                         /* end warning! - Campbell */
142                         
143                         VECCOPY(shi.co, rco);
144                         shi.osatex= 0;
145                         do_lamp_tex(lar, lv, &shi, lacol, LA_TEXTURE);
146                 }
147                 
148                 if(lar->type==LA_SPOT) {
149                         
150                         if(lar->mode & LA_SQUARE) {
151                                 if(lv[0]*lar->vec[0]+lv[1]*lar->vec[1]+lv[2]*lar->vec[2]>0.0) {
152                                         float x, lvrot[3];
153                                         
154                                         /* rotate view to lampspace */
155                                         VECCOPY(lvrot, lv);
156                                         MTC_Mat3MulVecfl(lar->imat, lvrot);
157                                         
158                                         x= MAX2(fabs(lvrot[0]/lvrot[2]) , fabs(lvrot[1]/lvrot[2]));
159                                         /* 1.0/(sqrt(1+x*x)) is equivalent to cos(atan(x)) */
160                                         
161                                         inpr= 1.0/(sqrt(1.0+x*x));
162                                 }
163                                 else inpr= 0.0;
164                         }
165                         else {
166                                 inpr= lv[0]*lar->vec[0]+lv[1]*lar->vec[1]+lv[2]*lar->vec[2];
167                         }
168                         
169                         t= lar->spotsi;
170                         if(inpr<t) continue;
171                         else {
172                                 t= inpr-t;
173                                 i= 1.0;
174                                 soft= 1.0;
175                                 if(t<lar->spotbl && lar->spotbl!=0.0) {
176                                         /* soft area */
177                                         i= t/lar->spotbl;
178                                         t= i*i;
179                                         soft= (3.0*t-2.0*t*i);
180                                         inpr*= soft;
181                                 }
182                                 if(lar->mode & LA_ONLYSHADOW) {
183                                         /* if(ma->mode & MA_SHADOW) { */
184                                         /* dot product positive: front side face! */
185                                         inp= vn[0]*lv[0] + vn[1]*lv[1] + vn[2]*lv[2];
186                                         if(inp>0.0) {
187                                                 /* testshadowbuf==0.0 : 100% shadow */
188                                                 shadfac = testshadowbuf(&R, lar->shb, rco, dco, dco, inp, 0.0f);
189                                                 if( shadfac>0.0 ) {
190                                                         shadfac*= inp*soft*lar->energy;
191                                                         ir -= shadfac;
192                                                         ig -= shadfac;
193                                                         ib -= shadfac;
194                                                         
195                                                         continue;
196                                                 }
197                                         }
198                                         /* } */
199                                 }
200                                 lampdist*=inpr;
201                         }
202                         if(lar->mode & LA_ONLYSHADOW) continue;
203                         
204                 }
205                 
206                 /* dot product and  reflectivity*/
207                 
208                 inp= 1.0-fabs(vn[0]*lv[0] + vn[1]*lv[1] + vn[2]*lv[2]);
209                 
210                 /* inp= cos(0.5*M_PI-acos(inp)); */
211                 
212                 i= inp;
213                 
214                 if(lar->type==LA_HEMI) {
215                         i= 0.5*i+0.5;
216                 }
217                 if(i>0.0) {
218                         i*= lampdist;
219                 }
220                 
221                 /* shadow  */
222                 if(i> -0.41) {                  /* heuristic valua! */
223                         shadfac= 1.0;
224                         if(lar->shb) {
225                                 shadfac = testshadowbuf(&R, lar->shb, rco, dco, dco, inp, 0.0f);
226                                 if(shadfac==0.0) continue;
227                                 i*= shadfac;
228                         }
229                 }
230                 
231                 if(i>0.0) {
232                         ir+= i*lacol[0];
233                         ig+= i*lacol[1];
234                         ib+= i*lacol[2];
235                 }
236         }
237         
238         if(ir<0.0) ir= 0.0;
239         if(ig<0.0) ig= 0.0;
240         if(ib<0.0) ib= 0.0;
241
242         colf[0]*= ir;
243         colf[1]*= ig;
244         colf[2]*= ib;
245         
246 }
247
248
249 /**
250  * Converts a halo z-buffer value to distance from the camera's near plane
251  * @param z The z-buffer value to convert
252  * @return a distance from the camera's near plane in blender units
253  */
254 static float haloZtoDist(int z)
255 {
256         float zco = 0;
257
258         if(z >= 0x7FFFFF)
259                 return 10e10;
260         else {
261                 zco = (float)z/(float)0x7FFFFF;
262                 if(R.r.mode & R_ORTHO)
263                         return (R.winmat[3][2] - zco*R.winmat[3][3])/(R.winmat[2][2]);
264                 else
265                         return (R.winmat[3][2])/(R.winmat[2][2] - R.winmat[2][3]*zco);
266         }
267 }
268
269 /**
270  * @param col (float[4]) Store the rgb color here (with alpha)
271  * The alpha is used to blend the color to the background 
272  * color_new = (1-alpha)*color_background + color
273  * @param zz The current zbuffer value at the place of this pixel
274  * @param dist Distance of the pixel from the center of the halo squared. Given in pixels
275  * @param xn The x coordinate of the pixel relaticve to the center of the halo. given in pixels
276  * @param yn The y coordinate of the pixel relaticve to the center of the halo. given in pixels
277  */
278 int shadeHaloFloat(HaloRen *har,  float *col, int zz, 
279                                         float dist, float xn,  float yn, short flarec)
280 {
281         /* fill in col */
282         float t, zn, radist, ringf=0.0f, linef=0.0f, alpha, si, co;
283         int a;
284    
285         if(R.wrld.mode & WO_MIST) {
286        if(har->type & HA_ONLYSKY) {
287            /* stars but no mist */
288            alpha= har->alfa;
289        }
290        else {
291            /* a bit patchy... */
292            alpha= mistfactor(-har->co[2], har->co)*har->alfa;
293        }
294         }
295         else alpha= har->alfa;
296         
297         if(alpha==0.0)
298                 return 0;
299
300         /* soften the halo if it intersects geometry */
301         if(har->mat && har->mat->mode & MA_HALO_SOFT) {
302                 float segment_length, halo_depth, distance_from_z, visible_depth, soften;
303                 
304                 /* calculate halo depth */
305                 segment_length= har->hasize*sasqrt(1.0f - dist/(har->rad*har->rad));
306                 halo_depth= 2.0f*segment_length;
307
308                 if(halo_depth < FLT_EPSILON)
309                         return 0;
310
311                 /* calculate how much of this depth is visible */
312                 distance_from_z = haloZtoDist(zz) - haloZtoDist(har->zs);
313                 visible_depth = halo_depth;
314                 if(distance_from_z < segment_length) {
315                         soften= (segment_length + distance_from_z)/halo_depth;
316
317                         /* apply softening to alpha */
318                         if(soften < 1.0f)
319                                 alpha *= soften;
320                         if(alpha <= 0.0f)
321                                 return 0;
322                 }
323         }
324         else {
325                 /* not a soft halo. use the old softening code */
326                 /* halo being intersected? */
327                 if(har->zs> zz-har->zd) {
328                         t= ((float)(zz-har->zs))/(float)har->zd;
329                         alpha*= sqrt(sqrt(t));
330                 }
331         }
332
333         radist= sqrt(dist);
334
335         /* watch it: not used nicely: flarec is set at zero in pixstruct */
336         if(flarec) har->pixels+= (int)(har->rad-radist);
337
338         if(har->ringc) {
339                 float *rc, fac;
340                 int ofs;
341                 
342                 /* per ring an antialised circle */
343                 ofs= har->seed;
344                 
345                 for(a= har->ringc; a>0; a--, ofs+=2) {
346                         
347                         rc= hashvectf + (ofs % 768);
348                         
349                         fac= fabs( rc[1]*(har->rad*fabs(rc[0]) - radist) );
350                         
351                         if(fac< 1.0) {
352                                 ringf+= (1.0-fac);
353                         }
354                 }
355         }
356
357         if(har->type & HA_VECT) {
358                 dist= fabs( har->cos*(yn) - har->sin*(xn) )/har->rad;
359                 if(dist>1.0) dist= 1.0;
360                 if(har->tex) {
361                         zn= har->sin*xn - har->cos*yn;
362                         yn= har->cos*xn + har->sin*yn;
363                         xn= zn;
364                 }
365         }
366         else dist= dist/har->radsq;
367
368         if(har->type & HA_FLARECIRC) {
369                 
370                 dist= 0.5+fabs(dist-0.5);
371                 
372         }
373
374         if(har->hard>=30) {
375                 dist= sqrt(dist);
376                 if(har->hard>=40) {
377                         dist= sin(dist*M_PI_2);
378                         if(har->hard>=50) {
379                                 dist= sqrt(dist);
380                         }
381                 }
382         }
383         else if(har->hard<20) dist*=dist;
384
385         if(dist < 1.0f)
386                 dist= (1.0f-dist);
387         else
388                 dist= 0.0f;
389         
390         if(har->linec) {
391                 float *rc, fac;
392                 int ofs;
393                 
394                 /* per starpoint an antialiased line */
395                 ofs= har->seed;
396                 
397                 for(a= har->linec; a>0; a--, ofs+=3) {
398                         
399                         rc= hashvectf + (ofs % 768);
400                         
401                         fac= fabs( (xn)*rc[0]+(yn)*rc[1]);
402                         
403                         if(fac< 1.0f )
404                                 linef+= (1.0f-fac);
405                 }
406                 
407                 linef*= dist;
408         }
409
410         if(har->starpoints) {
411                 float ster, angle;
412                 /* rotation */
413                 angle= atan2(yn, xn);
414                 angle*= (1.0+0.25*har->starpoints);
415                 
416                 co= cos(angle);
417                 si= sin(angle);
418                 
419                 angle= (co*xn+si*yn)*(co*yn-si*xn);
420                 
421                 ster= fabs(angle);
422                 if(ster>1.0) {
423                         ster= (har->rad)/(ster);
424                         
425                         if(ster<1.0) dist*= sqrt(ster);
426                 }
427         }
428
429         /* disputable optimize... (ton) */
430         if(dist<=0.00001)
431                 return 0;
432         
433         dist*= alpha;
434         ringf*= dist;
435         linef*= alpha;
436         
437         /* The color is either the rgb spec-ed by the user, or extracted from   */
438         /* the texture                                                           */
439         if(har->tex) {
440                 col[0]= har->r; 
441                 col[1]= har->g; 
442                 col[2]= har->b;
443                 col[3]= dist;
444                 
445                 do_halo_tex(har, xn, yn, col);
446                 
447                 col[0]*= col[3];
448                 col[1]*= col[3];
449                 col[2]*= col[3];
450                 
451         }
452         else {
453                 col[0]= dist*har->r;
454                 col[1]= dist*har->g;
455                 col[2]= dist*har->b;
456                 if(har->type & HA_XALPHA) col[3]= dist*dist;
457                 else col[3]= dist;
458         }
459
460         if(har->mat) {
461                 if(har->mat->mode & MA_HALO_SHADE) {
462                         /* we test for lights because of preview... */
463                         if(R.lights.first) render_lighting_halo(har, col);
464                 }
465
466                 /* Next, we do the line and ring factor modifications. */
467                 if(linef!=0.0) {
468                         Material *ma= har->mat;
469                         
470                         col[0]+= linef * ma->specr;
471                         col[1]+= linef * ma->specg;
472                         col[2]+= linef * ma->specb;
473                         
474                         if(har->type & HA_XALPHA) col[3]+= linef*linef;
475                         else col[3]+= linef;
476                 }
477                 if(ringf!=0.0) {
478                         Material *ma= har->mat;
479
480                         col[0]+= ringf * ma->mirr;
481                         col[1]+= ringf * ma->mirg;
482                         col[2]+= ringf * ma->mirb;
483                         
484                         if(har->type & HA_XALPHA) col[3]+= ringf*ringf;
485                         else col[3]+= ringf;
486                 }
487         }
488         
489         /* alpha requires clip, gives black dots */
490         if(col[3] > 1.0f)
491                 col[3]= 1.0f;
492
493         return 1;
494 }
495
496 /* ------------------------------------------------------------------------- */
497
498 static void fillBackgroundImage(float *collector, float fx, float fy)
499 {
500         collector[0] = 0.0; 
501         collector[1] = 0.0; 
502         collector[2] = 0.0; 
503         collector[3] = 0.0; 
504         
505         if(R.backbuf) {
506                 float dx= 1.0f/(float)R.winx;
507                 float dy= 1.0f/(float)R.winy;
508                 
509                 image_sample(R.backbuf, fx*dx, fy*dy, dx, dy, collector);
510         }
511 }
512
513 /* Only view vector is important here. Result goes to colf[3] */
514 void shadeSkyView(float *colf, float *rco, float *view, float *dxyview)
515 {
516         float lo[3], zen[3], hor[3], blend, blendm;
517         int skyflag;
518         
519         /* flag indicating if we render the top hemisphere */
520         skyflag = WO_ZENUP;
521         
522         /* Some view vector stuff. */
523         if(R.wrld.skytype & WO_SKYREAL) {
524                 
525                 blend= view[0]*R.grvec[0]+ view[1]*R.grvec[1]+ view[2]*R.grvec[2];
526                 
527                 if(blend<0.0) skyflag= 0;
528                 
529                 blend= fabs(blend);
530         }
531         else if(R.wrld.skytype & WO_SKYPAPER) {
532                 blend= 0.5+ 0.5*view[1];
533         }
534         else {
535                 /* the fraction of how far we are above the bottom of the screen */
536                 blend= fabs(0.5+ view[1]);
537         }
538         
539         hor[0]= R.wrld.horr; hor[1]= R.wrld.horg; hor[2]= R.wrld.horb;
540         zen[0]= R.wrld.zenr; zen[1]= R.wrld.zeng; zen[2]= R.wrld.zenb;
541         
542         /* Careful: SKYTEX and SKYBLEND are NOT mutually exclusive! If           */
543         /* SKYBLEND is active, the texture and color blend are added.           */
544         if(R.wrld.skytype & WO_SKYTEX) {
545                 VECCOPY(lo, view);
546                 if(R.wrld.skytype & WO_SKYREAL) {
547                         
548                         MTC_Mat3MulVecfl(R.imat, lo);
549                         
550                         SWAP(float, lo[1],  lo[2]);
551                         
552                 }
553                 do_sky_tex(rco, lo, dxyview, hor, zen, &blend, skyflag);
554         }
555         
556         if(blend>1.0) blend= 1.0;
557         blendm= 1.0-blend;
558         
559         /* No clipping, no conversion! */
560         if(R.wrld.skytype & WO_SKYBLEND) {
561                 colf[0] = (blendm*hor[0] + blend*zen[0]);
562                 colf[1] = (blendm*hor[1] + blend*zen[1]);
563                 colf[2] = (blendm*hor[2] + blend*zen[2]);
564         } else {
565                 /* Done when a texture was grabbed. */
566                 colf[0]= hor[0];
567                 colf[1]= hor[1];
568                 colf[2]= hor[2];
569         }
570 }
571
572 /* shade sky according to sun lamps, all parameters are like shadeSkyView except sunsky*/
573 void shadeSunView(float *colf, float *view)
574 {
575         GroupObject *go;
576         LampRen *lar;
577         float sview[3];
578         int do_init= 1;
579         
580         for(go=R.lights.first; go; go= go->next) {
581                 lar= go->lampren;
582                 if(lar->type==LA_SUN && lar->sunsky && (lar->sunsky->effect_type & LA_SUN_EFFECT_SKY)){
583                         float sun_collector[3];
584                         float colorxyz[3];
585                         
586                         if(do_init) {
587                                 
588                                 VECCOPY(sview, view);
589                                 Normalize(sview);
590                                 MTC_Mat3MulVecfl(R.imat, sview);
591                                 if (sview[2] < 0.0)
592                                         sview[2] = 0.0;
593                                 Normalize(sview);
594                                 do_init= 0;
595                         }
596                         
597                         GetSkyXYZRadiancef(lar->sunsky, sview, colorxyz);
598                         xyz_to_rgb(colorxyz[0], colorxyz[1], colorxyz[2], &sun_collector[0], &sun_collector[1], &sun_collector[2], 
599                                            lar->sunsky->sky_colorspace);
600                         
601                         ramp_blend(lar->sunsky->skyblendtype, colf, colf+1, colf+2, lar->sunsky->skyblendfac, sun_collector);
602                 }
603         }
604 }
605
606
607 /*
608   Stuff the sky color into the collector.
609  */
610 void shadeSkyPixel(float *collector, float fx, float fy) 
611 {
612         float view[3], dxyview[2];
613
614         /*
615           The rules for sky:
616           1. Draw an image, if a background image was provided. Stop
617           2. get texture and color blend, and combine these.
618         */
619
620         float fac;
621
622         /* 1. Do a backbuffer image: */ 
623         if(R.r.bufflag & 1) {
624                 fillBackgroundImage(collector, fx, fy);
625         } 
626         else if((R.wrld.skytype & (WO_SKYBLEND+WO_SKYTEX))==0) {
627                 /* 2. solid color */
628                 collector[0] = R.wrld.horr;
629                 collector[1] = R.wrld.horg;
630                 collector[2] = R.wrld.horb;
631                 collector[3] = 0.0f;
632         } 
633         else {
634                 /* 3. */
635
636                 /* This one true because of the context of this routine  */
637                 if(R.wrld.skytype & WO_SKYPAPER) {
638                         view[0]= -1.0f + 2.0f*(fx/(float)R.winx);
639                         view[1]= -1.0f + 2.0f*(fy/(float)R.winy);
640                         view[2]= 0.0;
641                         
642                         dxyview[0]= 1.0f/(float)R.winx;
643                         dxyview[1]= 1.0f/(float)R.winy;
644                 }
645                 else {
646                         calc_view_vector(view, fx, fy);
647                         fac= Normalize(view);
648                         
649                         if(R.wrld.skytype & WO_SKYTEX) {
650                                 dxyview[0]= -R.viewdx/fac;
651                                 dxyview[1]= -R.viewdy/fac;
652                         }
653                 }
654                 
655                 /* get sky color in the collector */
656                 shadeSkyView(collector, NULL, view, dxyview);
657                 collector[3] = 0.0f;
658         }
659         
660         calc_view_vector(view, fx, fy);
661         shadeSunView(collector, view);
662 }
663
664 /* aerial perspective */
665 void shadeAtmPixel(struct SunSky *sunsky, float *collector, float fx, float fy, float distance)
666 {
667         float view[3];
668                 
669         calc_view_vector(view, fx, fy);
670         Normalize(view);
671         /*MTC_Mat3MulVecfl(R.imat, view);*/
672         AtmospherePixleShader(sunsky, view, distance, collector);
673 }
674
675 /* eof */