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