Camera: some code refactoring, use an intermediate CameraParams struct instead
[blender.git] / source / blender / render / intern / source / initrender.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * Contributors: 2004/2005/2006 Blender Foundation, full recode
22  *
23  * ***** END GPL LICENSE BLOCK *****
24  */
25
26 /** \file blender/render/intern/source/initrender.c
27  *  \ingroup render
28  */
29
30
31
32 /* Global includes */
33
34 #include <math.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <stdio.h>
38
39 #include "MEM_guardedalloc.h"
40
41 #include "PIL_time.h"
42
43 #include "BLI_math.h"
44 #include "BLI_blenlib.h"
45 #include "BLI_jitter.h"
46 #include "BLI_utildefines.h"
47
48 #include "DNA_camera_types.h"
49 #include "DNA_group_types.h"
50 #include "DNA_image_types.h"
51 #include "DNA_lamp_types.h"
52 #include "DNA_object_types.h"
53 #include "DNA_scene_types.h"
54
55
56 #include "BKE_camera.h"
57 #include "BKE_global.h"
58 #include "BKE_material.h"
59 #include "BKE_object.h"
60 #include "BKE_image.h"
61 #include "BKE_ipo.h"
62 #include "BKE_key.h"
63 #include "BKE_action.h"
64 #include "BKE_writeavi.h"
65 #include "BKE_scene.h"
66
67 #include "IMB_imbuf_types.h"
68 #include "IMB_imbuf.h"
69
70 #ifdef WITH_QUICKTIME
71 #include "quicktime_export.h"
72 #endif
73
74 /* this module */
75 #include "renderpipeline.h"
76 #include "render_types.h"
77
78 #include "rendercore.h"
79 #include "pixelshading.h"
80 #include "zbuf.h"
81
82 /* Own includes */
83 #include "initrender.h"
84
85
86 /* ********************** */
87
88 static void init_render_jit(Render *re)
89 {
90         static float jit[32][2];        /* simple caching */
91         static float mblur_jit[32][2];  /* simple caching */
92         static int lastjit= 0;
93         static int last_mblur_jit= 0;
94         
95         if(lastjit!=re->r.osa || last_mblur_jit != re->r.mblur_samples) {
96                 memset(jit, 0, sizeof(jit));
97                 BLI_initjit(jit[0], re->r.osa);
98                 
99                 memset(mblur_jit, 0, sizeof(mblur_jit));
100                 BLI_initjit(mblur_jit[0], re->r.mblur_samples);
101         }
102         
103         lastjit= re->r.osa;
104         memcpy(re->jit, jit, sizeof(jit));
105         
106         last_mblur_jit= re->r.mblur_samples;
107         memcpy(re->mblur_jit, mblur_jit, sizeof(mblur_jit));
108 }
109
110
111 /* ****************** MASKS and LUTS **************** */
112
113 static float filt_quadratic(float x)
114 {
115         if (x <  0.0f) x = -x;
116         if (x < 0.5f) return 0.75f-(x*x);
117         if (x < 1.5f) return 0.50f*(x-1.5f)*(x-1.5f);
118         return 0.0f;
119 }
120
121
122 static float filt_cubic(float x)
123 {
124         float x2= x*x;
125         
126         if (x <  0.0f) x = -x;
127         
128         if (x < 1.0f) return 0.5f*x*x2 - x2 + 2.0f/3.0f;
129         if (x < 2.0f) return (2.0f-x)*(2.0f-x)*(2.0f-x)/6.0f;
130         return 0.0f;
131 }
132
133
134 static float filt_catrom(float x)
135 {
136         float x2= x*x;
137         
138         if (x <  0.0f) x = -x;
139         if (x < 1.0f) return  1.5f*x2*x - 2.5f*x2  + 1.0f;
140         if (x < 2.0f) return -0.5f*x2*x + 2.5f*x2 - 4.0f*x + 2.0f;
141         return 0.0f;
142 }
143
144 static float filt_mitchell(float x)     /* Mitchell & Netravali's two-param cubic */
145 {
146         float b = 1.0f/3.0f, c = 1.0f/3.0f;
147         float p0 = (  6.0f -  2.0f*b         ) / 6.0f;
148         float p2 = (-18.0f + 12.0f*b +  6.0f*c) / 6.0f;
149         float p3 = ( 12.0f -  9.0f*b -  6.0f*c) / 6.0f;
150         float q0 = (       8.0f*b + 24.0f*c) / 6.0f;
151         float q1 = (      - 12.0f *b - 48.0f*c) / 6.0f;
152         float q2 = (         6.0f *b + 30.0f*c) / 6.0f;
153         float q3 = (       -       b -  6.0f*c) / 6.0f;
154
155         if (x<-2.0f) return 0.0f;
156         if (x<-1.0f) return (q0-x*(q1-x*(q2-x*q3)));
157         if (x< 0.0f) return (p0+x*x*(p2-x*p3));
158         if (x< 1.0f) return (p0+x*x*(p2+x*p3));
159         if (x< 2.0f) return (q0+x*(q1+x*(q2+x*q3)));
160         return 0.0f;
161 }
162
163 /* x ranges from -1 to 1 */
164 float RE_filter_value(int type, float x)
165 {
166         float gaussfac= 1.6f;
167         
168         x= ABS(x);
169         
170         switch(type) {
171                 case R_FILTER_BOX:
172                         if(x>1.0f) return 0.0f;
173                         return 1.0f;
174                         
175                 case R_FILTER_TENT:
176                         if(x>1.0f) return 0.0f;
177                         return 1.0f-x;
178                         
179                 case R_FILTER_GAUSS:
180                         x*= gaussfac;
181                         return (1.0f/expf(x*x) - 1.0f/expf(gaussfac*gaussfac*2.25f));
182                         
183                 case R_FILTER_MITCH:
184                         return filt_mitchell(x*gaussfac);
185                         
186                 case R_FILTER_QUAD:
187                         return filt_quadratic(x*gaussfac);
188                         
189                 case R_FILTER_CUBIC:
190                         return filt_cubic(x*gaussfac);
191                         
192                 case R_FILTER_CATROM:
193                         return filt_catrom(x*gaussfac);
194         }
195         return 0.0f;
196 }
197
198 static float calc_weight(Render *re, float *weight, int i, int j)
199 {
200         float x, y, dist, totw= 0.0;
201         int a;
202
203         for(a=0; a<re->osa; a++) {
204                 x= re->jit[a][0] + i;
205                 y= re->jit[a][1] + j;
206                 dist= sqrt(x*x+y*y);
207
208                 weight[a]= 0.0;
209
210                 /* Weighting choices */
211                 switch(re->r.filtertype) {
212                 case R_FILTER_BOX:
213                         if(i==0 && j==0) weight[a]= 1.0;
214                         break;
215                         
216                 case R_FILTER_TENT:
217                         if(dist < re->r.gauss)
218                                 weight[a]= re->r.gauss - dist;
219                         break;
220                         
221                 case R_FILTER_GAUSS:
222                         x = dist*re->r.gauss;
223                         weight[a]= (1.0f/expf(x*x) - 1.0f/expf(re->r.gauss*re->r.gauss*2.25f));
224                         break;
225                 
226                 case R_FILTER_MITCH:
227                         weight[a]= filt_mitchell(dist*re->r.gauss);
228                         break;
229                 
230                 case R_FILTER_QUAD:
231                         weight[a]= filt_quadratic(dist*re->r.gauss);
232                         break;
233                         
234                 case R_FILTER_CUBIC:
235                         weight[a]= filt_cubic(dist*re->r.gauss);
236                         break;
237                         
238                 case R_FILTER_CATROM:
239                         weight[a]= filt_catrom(dist*re->r.gauss);
240                         break;
241                         
242                 }
243                 
244                 totw+= weight[a];
245
246         }
247         return totw;
248 }
249
250 void free_sample_tables(Render *re)
251 {
252         int a;
253         
254         if(re->samples) {
255                 for(a=0; a<9; a++) {
256                         MEM_freeN(re->samples->fmask1[a]);
257                         MEM_freeN(re->samples->fmask2[a]);
258                 }
259                 
260                 MEM_freeN(re->samples->centmask);
261                 MEM_freeN(re->samples);
262                 re->samples= NULL;
263         }
264 }
265
266 /* based on settings in render, it makes the lookup tables */
267 void make_sample_tables(Render *re)
268 {
269         static int firsttime= 1;
270         SampleTables *st;
271         float flweight[32];
272         float weight[32], totw, val, *fpx1, *fpx2, *fpy1, *fpy2, *m3, *m4;
273         int i, j, a;
274
275         /* optimization tables, only once */
276         if(firsttime) {
277                 firsttime= 0;
278         }
279         
280         free_sample_tables(re);
281         
282         init_render_jit(re);    /* needed for mblur too */
283         
284         if(re->osa==0) {
285                 /* just prevents cpu cycles for larger render and copying */
286                 re->r.filtertype= 0;
287                 return;
288         }
289         
290         st= re->samples= MEM_callocN(sizeof(SampleTables), "sample tables");
291         
292         for(a=0; a<9;a++) {
293                 st->fmask1[a]= MEM_callocN(256*sizeof(float), "initfilt");
294                 st->fmask2[a]= MEM_callocN(256*sizeof(float), "initfilt");
295         }
296         for(a=0; a<256; a++) {
297                 st->cmask[a]= 0;
298                 if(a &   1) st->cmask[a]++;
299                 if(a &   2) st->cmask[a]++;
300                 if(a &   4) st->cmask[a]++;
301                 if(a &   8) st->cmask[a]++;
302                 if(a &  16) st->cmask[a]++;
303                 if(a &  32) st->cmask[a]++;
304                 if(a &  64) st->cmask[a]++;
305                 if(a & 128) st->cmask[a]++;
306         }
307         
308         st->centmask= MEM_mallocN((1<<re->osa), "Initfilt3");
309         
310         for(a=0; a<16; a++) {
311                 st->centLut[a]= -0.45f+((float)a)/16.0f;
312         }
313
314         /* calculate totw */
315         totw= 0.0;
316         for(j= -1; j<2; j++) {
317                 for(i= -1; i<2; i++) {
318                         totw+= calc_weight(re, weight, i, j);
319                 }
320         }
321
322         for(j= -1; j<2; j++) {
323                 for(i= -1; i<2; i++) {
324                         /* calculate using jit, with offset the weights */
325
326                         memset(weight, 0, sizeof(weight));
327                         calc_weight(re, weight, i, j);
328
329                         for(a=0; a<16; a++) flweight[a]= weight[a]*(1.0f/totw);
330
331                         m3= st->fmask1[ 3*(j+1)+i+1 ];
332                         m4= st->fmask2[ 3*(j+1)+i+1 ];
333
334                         for(a=0; a<256; a++) {
335                                 if(a &   1) {
336                                         m3[a]+= flweight[0];
337                                         m4[a]+= flweight[8];
338                                 }
339                                 if(a &   2) {
340                                         m3[a]+= flweight[1];
341                                         m4[a]+= flweight[9];
342                                 }
343                                 if(a &   4) {
344                                         m3[a]+= flweight[2];
345                                         m4[a]+= flweight[10];
346                                 }
347                                 if(a &   8) {
348                                         m3[a]+= flweight[3];
349                                         m4[a]+= flweight[11];
350                                 }
351                                 if(a &  16) {
352                                         m3[a]+= flweight[4];
353                                         m4[a]+= flweight[12];
354                                 }
355                                 if(a &  32) {
356                                         m3[a]+= flweight[5];
357                                         m4[a]+= flweight[13];
358                                 }
359                                 if(a &  64) {
360                                         m3[a]+= flweight[6];
361                                         m4[a]+= flweight[14];
362                                 }
363                                 if(a & 128) {
364                                         m3[a]+= flweight[7];
365                                         m4[a]+= flweight[15];
366                                 }
367                         }
368                 }
369         }
370
371         /* centmask: the correct subpixel offset per mask */
372
373         fpx1= MEM_mallocN(256*sizeof(float), "initgauss4");
374         fpx2= MEM_mallocN(256*sizeof(float), "initgauss4");
375         fpy1= MEM_mallocN(256*sizeof(float), "initgauss4");
376         fpy2= MEM_mallocN(256*sizeof(float), "initgauss4");
377         for(a=0; a<256; a++) {
378                 fpx1[a]= fpx2[a]= 0.0;
379                 fpy1[a]= fpy2[a]= 0.0;
380                 if(a & 1) {
381                         fpx1[a]+= re->jit[0][0];
382                         fpy1[a]+= re->jit[0][1];
383                         fpx2[a]+= re->jit[8][0];
384                         fpy2[a]+= re->jit[8][1];
385                 }
386                 if(a & 2) {
387                         fpx1[a]+= re->jit[1][0];
388                         fpy1[a]+= re->jit[1][1];
389                         fpx2[a]+= re->jit[9][0];
390                         fpy2[a]+= re->jit[9][1];
391                 }
392                 if(a & 4) {
393                         fpx1[a]+= re->jit[2][0];
394                         fpy1[a]+= re->jit[2][1];
395                         fpx2[a]+= re->jit[10][0];
396                         fpy2[a]+= re->jit[10][1];
397                 }
398                 if(a & 8) {
399                         fpx1[a]+= re->jit[3][0];
400                         fpy1[a]+= re->jit[3][1];
401                         fpx2[a]+= re->jit[11][0];
402                         fpy2[a]+= re->jit[11][1];
403                 }
404                 if(a & 16) {
405                         fpx1[a]+= re->jit[4][0];
406                         fpy1[a]+= re->jit[4][1];
407                         fpx2[a]+= re->jit[12][0];
408                         fpy2[a]+= re->jit[12][1];
409                 }
410                 if(a & 32) {
411                         fpx1[a]+= re->jit[5][0];
412                         fpy1[a]+= re->jit[5][1];
413                         fpx2[a]+= re->jit[13][0];
414                         fpy2[a]+= re->jit[13][1];
415                 }
416                 if(a & 64) {
417                         fpx1[a]+= re->jit[6][0];
418                         fpy1[a]+= re->jit[6][1];
419                         fpx2[a]+= re->jit[14][0];
420                         fpy2[a]+= re->jit[14][1];
421                 }
422                 if(a & 128) {
423                         fpx1[a]+= re->jit[7][0];
424                         fpy1[a]+= re->jit[7][1];
425                         fpx2[a]+= re->jit[15][0];
426                         fpy2[a]+= re->jit[15][1];
427                 }
428         }
429
430         for(a= (1<<re->osa)-1; a>0; a--) {
431                 val= st->cmask[a & 255] + st->cmask[a>>8];
432                 i= 8+(15.9f*(fpy1[a & 255]+fpy2[a>>8])/val);
433                 CLAMP(i, 0, 15);
434                 j= 8+(15.9f*(fpx1[a & 255]+fpx2[a>>8])/val);
435                 CLAMP(j, 0, 15);
436                 i= j + (i<<4);
437                 st->centmask[a]= i;
438         }
439
440         MEM_freeN(fpx1);
441         MEM_freeN(fpx2);
442         MEM_freeN(fpy1);
443         MEM_freeN(fpy2);
444 }
445
446
447 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
448
449 struct Object *RE_GetCamera(Render *re)
450 {
451         return re->camera_override ? re->camera_override : re->scene->camera;
452 }
453
454 static void re_camera_params_get(Render *re, CameraParams *params, Object *cam_ob)
455 {
456         copy_m4_m4(re->winmat, params->winmat);
457
458         re->clipsta= params->clipsta;
459         re->clipend= params->clipend;
460
461         re->ycor= params->ycor;
462         re->viewdx= params->viewdx;
463         re->viewdy= params->viewdy;
464         re->viewplane= params->viewplane;
465
466         object_camera_mode(&re->r, cam_ob);
467 }
468
469 void RE_SetEnvmapCamera(Render *re, Object *cam_ob, float viewscale, float clipsta, float clipend)
470 {
471         CameraParams params;
472
473         /* setup parameters */
474         camera_params_init(&params);
475         camera_params_from_object(&params, cam_ob);
476
477         params.lens= 16.0f*viewscale;
478         params.sensor_x= 32.0f;
479         params.sensor_y= 32.0f;
480         params.sensor_fit = CAMERA_SENSOR_FIT_AUTO;
481         params.clipsta= clipsta;
482         params.clipend= clipend;
483         
484         /* compute matrix, viewplane, .. */
485         camera_params_compute(&params, re->winx, re->winy, 1.0f, 1.0f);
486
487         /* extract results */
488         re_camera_params_get(re, &params, cam_ob);
489 }
490
491 /* call this after InitState() */
492 /* per render, there's one persistent viewplane. Parts will set their own viewplanes */
493 void RE_SetCamera(Render *re, Object *cam_ob)
494 {
495         CameraParams params;
496
497         /* setup parameters */
498         camera_params_init(&params);
499         camera_params_from_object(&params, cam_ob);
500
501         params.use_fields= (re->r.mode & R_FIELDS);
502         params.field_second= (re->flag & R_SEC_FIELD);
503         params.field_odd= (re->r.mode & R_ODDFIELD);
504
505         /* compute matrix, viewplane, .. */
506         camera_params_compute(&params, re->winx, re->winy, re->r.xasp, re->r.yasp);
507
508         /* extract results */
509         re_camera_params_get(re, &params, cam_ob);
510 }
511
512 void RE_SetPixelSize(Render *re, float pixsize)
513 {
514         re->viewdx= pixsize;
515         re->viewdy= re->ycor*pixsize;
516 }
517
518 void RE_GetCameraWindow(struct Render *re, struct Object *camera, int frame, float mat[][4])
519 {
520         re->r.cfra= frame;
521         RE_SetCamera(re, camera);
522         copy_m4_m4(mat, re->winmat);
523 }
524
525 /* ~~~~~~~~~~~~~~~~ part (tile) calculus ~~~~~~~~~~~~~~~~~~~~~~ */
526
527
528 void freeparts(Render *re)
529 {
530         RenderPart *part= re->parts.first;
531         
532         while(part) {
533                 if(part->rectp) MEM_freeN(part->rectp);
534                 if(part->rectz) MEM_freeN(part->rectz);
535                 part= part->next;
536         }
537         BLI_freelistN(&re->parts);
538 }
539
540 void initparts(Render *re)
541 {
542         int nr, xd, yd, partx, party, xparts, yparts;
543         int xminb, xmaxb, yminb, ymaxb;
544         
545         freeparts(re);
546         
547         /* this is render info for caller, is not reset when parts are freed! */
548         re->i.totpart= 0;
549         re->i.curpart= 0;
550         re->i.partsdone= 0;
551         
552         /* just for readable code.. */
553         xminb= re->disprect.xmin;
554         yminb= re->disprect.ymin;
555         xmaxb= re->disprect.xmax;
556         ymaxb= re->disprect.ymax;
557         
558         xparts= re->r.xparts;
559         yparts= re->r.yparts;
560         
561         /* mininum part size, but for exr tile saving it was checked already */
562         if(!(re->r.scemode & (R_EXR_TILE_FILE|R_FULL_SAMPLE))) {
563                 if(re->r.mode & R_PANORAMA) {
564                         if(ceil(re->rectx/(float)xparts) < 8) 
565                                 xparts= 1 + re->rectx/8;
566                 }
567                 else
568                         if(ceil(re->rectx/(float)xparts) < 64) 
569                                 xparts= 1 + re->rectx/64;
570                 
571                 if(ceil(re->recty/(float)yparts) < 64) 
572                         yparts= 1 + re->recty/64;
573         }
574         
575         /* part size */
576         partx= ceil(re->rectx/(float)xparts);
577         party= ceil(re->recty/(float)yparts);
578         
579         re->xparts= xparts;
580         re->yparts= yparts;
581         re->partx= partx;
582         re->party= party;
583         
584         /* calculate rotation factor of 1 pixel */
585         if(re->r.mode & R_PANORAMA)
586                 re->panophi= panorama_pixel_rot(re);
587         
588         for(nr=0; nr<xparts*yparts; nr++) {
589                 rcti disprect;
590                 int rectx, recty;
591                 
592                 xd= (nr % xparts);
593                 yd= (nr-xd)/xparts;
594                 
595                 disprect.xmin= xminb+ xd*partx;
596                 disprect.ymin= yminb+ yd*party;
597                 
598                 /* ensure we cover the entire picture, so last parts go to end */
599                 if(xd<xparts-1) {
600                         disprect.xmax= disprect.xmin + partx;
601                         if(disprect.xmax > xmaxb)
602                                 disprect.xmax = xmaxb;
603                 }
604                 else disprect.xmax= xmaxb;
605                 
606                 if(yd<yparts-1) {
607                         disprect.ymax= disprect.ymin + party;
608                         if(disprect.ymax > ymaxb)
609                                 disprect.ymax = ymaxb;
610                 }
611                 else disprect.ymax= ymaxb;
612                 
613                 rectx= disprect.xmax - disprect.xmin;
614                 recty= disprect.ymax - disprect.ymin;
615                 
616                 /* so, now can we add this part? */
617                 if(rectx>0 && recty>0) {
618                         RenderPart *pa= MEM_callocN(sizeof(RenderPart), "new part");
619                         
620                         /* Non-box filters need 2 pixels extra to work */
621                         if((re->r.filtertype || (re->r.mode & R_EDGE))) {
622                                 pa->crop= 2;
623                                 disprect.xmin -= pa->crop;
624                                 disprect.ymin -= pa->crop;
625                                 disprect.xmax += pa->crop;
626                                 disprect.ymax += pa->crop;
627                                 rectx+= 2*pa->crop;
628                                 recty+= 2*pa->crop;
629                         }
630                         pa->disprect= disprect;
631                         pa->rectx= rectx;
632                         pa->recty= recty;
633
634                         BLI_addtail(&re->parts, pa);
635                         re->i.totpart++;
636                 }
637         }
638 }
639
640
641