Synchronise game engine with Tuhopuu2 tree.
[blender.git] / source / blender / render / intern / source / initrender.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): none yet.
29  *
30  * ***** END GPL/BL DUAL LICENSE BLOCK *****
31  */
32
33
34 /* Global includes */
35
36 #include <math.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <stdio.h>
40
41 #ifdef HAVE_CONFIG_H
42 #include <config.h>
43 #endif
44
45 #ifdef WIN32
46 #include "BLI_winstuff.h"
47 #endif
48
49 #include "blendef.h"
50 #include "MEM_guardedalloc.h"
51
52 #include "PIL_time.h"
53
54 #include "BLI_arithb.h"
55 #include "BLI_blenlib.h"
56 #include "BLI_rand.h"
57
58 #include "MTC_matrixops.h"
59
60 #include "DNA_image_types.h"
61 #include "DNA_camera_types.h"
62 #include "DNA_lamp_types.h"
63 #include "DNA_scene_types.h"
64 #include "DNA_object_types.h"
65
66 #include "BKE_utildefines.h"
67 #include "BKE_global.h"
68 #include "BKE_material.h"
69 #include "BKE_object.h"
70 #include "BKE_image.h"
71 #include "BKE_ipo.h"
72 #include "BKE_key.h"
73 #include "BKE_ika.h"
74 #include "BKE_action.h"
75 #include "BKE_writeavi.h"
76 #include "BKE_scene.h"
77
78 #include "BIF_toolbox.h"
79 #include "BIF_writeavicodec.h"
80 #include "BIF_writemovie.h"             /* start_movie(), append_movie(), end_movie() */
81
82 #include "BSE_drawview.h"
83 #include "BSE_sequence.h"
84
85 #ifdef WITH_QUICKTIME
86 #include "quicktime_export.h"
87 #endif
88
89 /* this module */
90 #include "render.h"
91 #include "render_intern.h"
92
93 #include "RE_callbacks.h"
94 #include "zbuf.h"
95 #include "rendercore.h" /* part handler for the old renderer, shading functions */
96 #include "renderPreAndPost.h"
97 #include "outerRenderLoop.h"
98 #include "renderHelp.h"
99 #include "jitter.h"
100
101 /* Own includes */
102 #include "initrender.h"
103
104 /* yafray: include for yafray export/render */
105 #include "YafRay_Api.h"
106
107 /* Some crud :/ */
108 #define ELEM3(a, b, c, d)       ( ELEM(a, b, c) || (a)==(d) )
109
110
111 /* from render.c */
112 extern float fmask[256], centLut[16];
113 extern unsigned short *mask1[9], *mask2[9], /*  *igamtab1, */ *igamtab2/*,  *gamtab */;
114 extern char cmask[256], *centmask;
115
116 Material defmaterial;
117 short pa; /* pa is global part, for print */
118 short allparts[65][4];
119 int qscount;
120
121 /* ********************* *********************** */
122
123
124 void init_def_material(void)
125 {
126         Material *ma;
127
128         ma= &defmaterial;
129         
130         init_material(&defmaterial);
131
132         init_render_material(ma);
133 }
134
135 void RE_init_render_data(void)
136 {
137         memset(&R, 0, sizeof(RE_Render));
138         memset(&O, 0, sizeof(Osa));
139         O.dxwin[0]= 1.0;
140         O.dywin[1]= 1.0;
141         
142         R.blove= (VertRen **)MEM_callocN(sizeof(void *)*(TABLEINITSIZE),"Blove");
143         R.blovl= (VlakRen **)MEM_callocN(sizeof(void *)*(TABLEINITSIZE),"Blovl");
144         R.bloha= (HaloRen **)MEM_callocN(sizeof(void *)*(TABLEINITSIZE),"Bloha");
145         R.la= (LampRen **)MEM_mallocN(LAMPINITSIZE*sizeof(void *),"renderlamparray");
146
147         init_def_material();
148 }
149
150 void RE_free_render_data()
151 {
152         MEM_freeN(R.blove);
153         R.blove= 0;
154         MEM_freeN(R.blovl);
155         R.blovl= 0;
156         MEM_freeN(R.bloha);
157         R.bloha= 0;
158         MEM_freeN(R.la);
159         R.la= 0;
160         if(R.rectot) MEM_freeN(R.rectot);
161         if(R.rectz) MEM_freeN(R.rectz);
162         if(R.rectspare) MEM_freeN(R.rectspare);
163         R.rectot= 0;
164         R.rectz= 0;
165         R.rectspare= 0;
166
167         end_render_material(&defmaterial);
168 }
169
170 /* ****************** GAMMA, MASKS and LUTS **************** */
171
172 float  calc_weight(float *weight, int i, int j)
173 {
174         float x, y, dist, totw= 0.0, fac;
175         int a;
176
177         fac= R.r.gauss*R.r.gauss;
178         fac*= fac;
179
180         for(a=0; a<R.osa; a++) {
181                 x= jit[a][0]-0.5+ i;
182                 y= jit[a][1]-0.5+ j;
183                 dist= sqrt(x*x+y*y);
184
185                 weight[a]= 0.0;
186
187                 /* gaussian weighting has been cancelled */
188                 if(R.r.mode & R_GAUSS) {
189                         if(dist<R.r.gauss) {
190                                 x = dist*R.r.gauss;
191                                 weight[a]= (1.0/exp(x*x) - 1.0/exp(fac));
192                         }
193                 }
194                 else {
195                         if(i==0 && j==0) weight[a]= 1.0;
196                 }
197
198                 totw+= weight[a];
199
200         }
201         return totw;
202 }
203
204 void RE_init_filt_mask(void)
205 {
206         static int firsttime=1;
207         static float lastgamma= 0.0;
208         float gamma, igamma;
209         float weight[32], totw, val, *fpx1, *fpx2, *fpy1, *fpy2;
210         int i, j, a;
211         unsigned short *m1, *m2, shweight[32];
212
213         if(firsttime) {
214                 for(a=0; a<9;a++) {
215                         mask1[a]= MEM_mallocN(256*sizeof(short), "initfilt");
216                         mask2[a]= MEM_mallocN(256*sizeof(short), "initfilt");
217                 }
218                 for(a=0; a<256; a++) {
219                         cmask[a]= 0;
220                         if(a &   1) cmask[a]++;
221                         if(a &   2) cmask[a]++;
222                         if(a &   4) cmask[a]++;
223                         if(a &   8) cmask[a]++;
224                         if(a &  16) cmask[a]++;
225                         if(a &  32) cmask[a]++;
226                         if(a &  64) cmask[a]++;
227                         if(a & 128) cmask[a]++;
228                 }
229                 centmask= MEM_mallocN(65536, "Initfilt3");
230                 for(a=0; a<16; a++) {
231                         centLut[a]= -0.45+((float)a)/16.0;
232                 }
233
234                 gamtab= MEM_mallocN(65536*sizeof(short), "initGaus2");
235                 igamtab1= MEM_mallocN(256*sizeof(short), "initGaus2");
236                 igamtab2= MEM_mallocN(65536*sizeof(short), "initGaus2");
237
238         }
239
240         if(R.r.alphamode==R_ALPHAKEY) gamma= 1.0;       /* gamma correction of alpha is nasty */
241
242         if(R.r.mode & R_GAMMA) gamma= 2.0;
243         else gamma= 1.0;
244         igamma= 1.0/gamma;
245
246         if(gamma!= lastgamma) {
247                 lastgamma= gamma;
248
249                 /* gamtab: in short, out short */
250                 for(a=0; a<65536; a++) {
251                         val= a;
252                         val/= 65535.0;
253
254                         if(gamma==2.0) val= sqrt(val);
255                         else if(gamma!=1.0) val= pow(val, igamma);
256
257                         gamtab[a]= (65535.99*val);
258                 }
259                 /* inverse gamtab1 : in byte, out short */
260                 for(a=1; a<=256; a++) {
261                         if(gamma==2.0) igamtab1[a-1]= a*a-1;
262                         else if(gamma==1.0) igamtab1[a-1]= 256*a-1;
263                         else {
264                                 val= a/256.0;
265                                 igamtab1[a-1]= (65535.0*pow(val, gamma)) -1 ;
266                         }
267                 }
268
269                 /* inverse gamtab2 : in short, out short */
270                 for(a=0; a<65536; a++) {
271                         val= a;
272                         val/= 65535.0;
273                         if(gamma==2.0) val= val*val;
274                         else val= pow(val, gamma);
275
276                         igamtab2[a]= 65535.0*val;
277                 }
278         }
279
280         if(firsttime) {
281                 firsttime= 0;
282                 return;
283         }
284
285         val= 1.0/((float)R.osa);
286         for(a=0; a<256; a++) {
287                 fmask[a]= ((float)cmask[a])*val;
288         }
289
290         for(a=0; a<9;a++) {
291                 memset(mask1[a], 0, 256*2);
292                 memset(mask2[a], 0, 256*2);
293         }
294
295         /* calculate totw */
296         totw= 0.0;
297         for(j= -1; j<2; j++) {
298                 for(i= -1; i<2; i++) {
299                         totw+= calc_weight(weight, i, j);
300                 }
301         }
302
303         for(j= -1; j<2; j++) {
304                 for(i= -1; i<2; i++) {
305                         /* calculate using jit, with offset the weights */
306
307                         memset(weight, 0, 32*2);
308                         calc_weight(weight, i, j);
309
310                         for(a=0; a<16; a++) shweight[a]= weight[a]*(65535.0/totw);
311
312                         m1= mask1[ 3*(j+1)+i+1 ];
313                         m2= mask2[ 3*(j+1)+i+1 ];
314
315                         for(a=0; a<256; a++) {
316                                 if(a &   1) {
317                                         m1[a]+= shweight[0];
318                                         m2[a]+= shweight[8];
319                                 }
320                                 if(a &   2) {
321                                         m1[a]+= shweight[1];
322                                         m2[a]+= shweight[9];
323                                 }
324                                 if(a &   4) {
325                                         m1[a]+= shweight[2];
326                                         m2[a]+= shweight[10];
327                                 }
328                                 if(a &   8) {
329                                         m1[a]+= shweight[3];
330                                         m2[a]+= shweight[11];
331                                 }
332                                 if(a &  16) {
333                                         m1[a]+= shweight[4];
334                                         m2[a]+= shweight[12];
335                                 }
336                                 if(a &  32) {
337                                         m1[a]+= shweight[5];
338                                         m2[a]+= shweight[13];
339                                 }
340                                 if(a &  64) {
341                                         m1[a]+= shweight[6];
342                                         m2[a]+= shweight[14];
343                                 }
344                                 if(a & 128) {
345                                         m1[a]+= shweight[7];
346                                         m2[a]+= shweight[15];
347                                 }
348                         }
349                 }
350         }
351
352         /* centmask: the correct subpixel offset per mask */
353
354         fpx1= MEM_mallocN(256*sizeof(float), "initgauss4");
355         fpx2= MEM_mallocN(256*sizeof(float), "initgauss4");
356         fpy1= MEM_mallocN(256*sizeof(float), "initgauss4");
357         fpy2= MEM_mallocN(256*sizeof(float), "initgauss4");
358         for(a=0; a<256; a++) {
359                 fpx1[a]= fpx2[a]= 0.0;
360                 fpy1[a]= fpy2[a]= 0.0;
361                 if(a & 1) {
362                         fpx1[a]+= jit[0][0];
363                         fpy1[a]+= jit[0][1];
364                         fpx2[a]+= jit[8][0];
365                         fpy2[a]+= jit[8][1];
366                 }
367                 if(a & 2) {
368                         fpx1[a]+= jit[1][0];
369                         fpy1[a]+= jit[1][1];
370                         fpx2[a]+= jit[9][0];
371                         fpy2[a]+= jit[9][1];
372                 }
373                 if(a & 4) {
374                         fpx1[a]+= jit[2][0];
375                         fpy1[a]+= jit[2][1];
376                         fpx2[a]+= jit[10][0];
377                         fpy2[a]+= jit[10][1];
378                 }
379                 if(a & 8) {
380                         fpx1[a]+= jit[3][0];
381                         fpy1[a]+= jit[3][1];
382                         fpx2[a]+= jit[11][0];
383                         fpy2[a]+= jit[11][1];
384                 }
385                 if(a & 16) {
386                         fpx1[a]+= jit[4][0];
387                         fpy1[a]+= jit[4][1];
388                         fpx2[a]+= jit[12][0];
389                         fpy2[a]+= jit[12][1];
390                 }
391                 if(a & 32) {
392                         fpx1[a]+= jit[5][0];
393                         fpy1[a]+= jit[5][1];
394                         fpx2[a]+= jit[13][0];
395                         fpy2[a]+= jit[13][1];
396                 }
397                 if(a & 64) {
398                         fpx1[a]+= jit[6][0];
399                         fpy1[a]+= jit[6][1];
400                         fpx2[a]+= jit[14][0];
401                         fpy2[a]+= jit[14][1];
402                 }
403                 if(a & 128) {
404                         fpx1[a]+= jit[7][0];
405                         fpy1[a]+= jit[7][1];
406                         fpx2[a]+= jit[15][0];
407                         fpy2[a]+= jit[15][1];
408                 }
409         }
410
411         for(a= (1<<R.osa)-1; a>0; a--) {
412                 val= count_mask(a);
413                 i= (15.9*(fpy1[a & 255]+fpy2[a>>8])/val);
414                 i<<=4;
415                 i+= (15.9*(fpx1[a & 255]+fpx2[a>>8])/val);
416                 centmask[a]= i;
417         }
418
419         MEM_freeN(fpx1);
420         MEM_freeN(fpx2);
421         MEM_freeN(fpy1);
422         MEM_freeN(fpy2);
423
424 }
425
426 void RE_free_filt_mask()
427 {
428         int a;
429
430         for(a=0; a<9; a++) {
431                 MEM_freeN(mask1[a]);
432                 MEM_freeN(mask2[a]);
433         }
434         MEM_freeN(gamtab);
435         MEM_freeN(igamtab1);
436         MEM_freeN(igamtab2);
437
438         MEM_freeN(centmask);
439 }
440
441 /* add stuff */
442
443
444 void defaultlamp()
445 {
446         LampRen *lar;
447
448         lar= (LampRen *)MEM_callocN(sizeof(LampRen),"lampren");
449         R.la[R.totlamp++]=lar;
450
451         lar->type= LA_SUN;
452         lar->vec[0]= -R.viewmat[2][0];
453         lar->vec[1]= -R.viewmat[2][1];
454         lar->vec[2]= -R.viewmat[2][2];
455         Normalise(lar->vec);
456         lar->r= 1.0;
457         lar->g= 1.0;
458         lar->b= 1.0;
459         lar->lay= 65535;
460 }
461
462
463 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
464
465 void RE_make_existing_file(char *name)
466 {
467         char di[FILE_MAXDIR], fi[FILE_MAXFILE];
468
469         strcpy(di, name);
470         BLI_splitdirstring(di, fi);
471
472         /* test exist */
473         if (BLI_exists(di) == 0) {
474                 BLI_recurdir_fileops(di);
475         }
476 }
477
478
479 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
480
481 void RE_setwindowclip(int mode, int jmode)
482 {
483         extern float bluroffsx, bluroffsy;      // rendercore.c... hackish (ton)
484         Camera *cam=0;
485         float lens, minx, miny, maxx, maxy;
486         float xd, yd, afmx, afmy;
487
488         if(G.scene->camera==0) return;
489
490         afmx= R.afmx;
491         afmy= R.afmy;
492
493         if(mode) {
494
495                 if(G.scene->camera->type==OB_LAMP) {
496                         /* fac= cos( PI*((float)(256- la->spsi))/512.0 ); */
497
498                         /* phi= acos(fac); */
499                         /* lens= 16.0*fac/sin(phi); */
500                         lens= 35.0;
501                         R.near= 0.1;
502                         R.far= 1000.0;
503                 }
504                 else if(G.scene->camera->type==OB_CAMERA) {
505                         cam= G.scene->camera->data;
506
507                         lens= cam->lens;
508                         R.near= cam->clipsta;
509                         R.far= cam->clipend;
510                 }
511                 else {
512                         lens= 16.0;
513                 }
514
515                 if( (R.r.xasp*afmx) >= (R.r.yasp*afmy) ) {
516                         R.viewfac= (afmx*lens)/16.0;
517                 }
518                 else {
519                         R.viewfac= R.ycor*(afmy*lens)/16.0;
520                 }
521                 if(R.r.mode & R_ORTHO) {
522                         R.near*= 100.0; 
523                         R.viewfac*= 100.0;
524                 }
525
526                 R.pixsize= R.near/R.viewfac;
527
528         }
529
530         /* I think these offsets are wrong. They do not coincide with shadow     */
531         /* calculations, btw.                                                    */     
532         minx= R.xstart+.5; 
533         miny= R.ycor*(R.ystart+.5); 
534         maxx= R.xend+.4999; 
535         maxy= R.ycor*(R.yend+.4999); 
536         /* My guess: (or rather, what should be) */
537         /*      minx= R.xstart - 0.5;  */
538         /*      miny= R.ycor * (R.ystart - 0.5);  */
539         /* Since the SCS-s map directly to the pixel center coordinates, we need */
540         /* to stretch the clip area a bit, not just shift it. However, this gives*/
541         /* nasty problems for parts...                                           */
542
543         /* Dunno who wrote previous comment, but I found an error with uncorrected
544            blur offset in shadepixel(). Now solved with 2 globals, seems to work.
545            The whole method how to retrieve the correct coordinate needs revision. (ton) */
546            
547         if(R.flag & R_SEC_FIELD) {
548                 if(R.r.mode & R_ODDFIELD) {
549                         miny-= .5*R.ycor;
550                         maxy-= .5*R.ycor;
551                 }
552                 else {
553                         miny+= .5*R.ycor;
554                         maxy+= .5*R.ycor;
555                 }
556         }
557
558         xd= yd= 0.0;
559         if(jmode!= -1) {
560                 bluroffsx= xd= jit[jmode % R.osa][0];
561                 bluroffsy= yd= R.ycor*jit[jmode % R.osa][1];
562         }
563         else bluroffsx=bluroffsy= 0.0;
564
565         minx= R.pixsize*(minx+xd);
566         maxx= R.pixsize*(maxx+xd);
567         miny= R.pixsize*(miny+yd);
568         maxy= R.pixsize*(maxy+yd);
569
570         if(R.r.mode & R_ORTHO) {
571                 i_window(minx, maxx, miny, maxy, R.near, 100.0*R.far, R.winmat);
572         }
573         else i_window(minx, maxx, miny, maxy, R.near, R.far, R.winmat);
574
575 }
576
577
578 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
579 void initparts()
580 {
581         short nr, xd, yd, xpart, ypart, xparts, yparts;
582         short a, xminb, xmaxb, yminb, ymaxb;
583
584         if(R.r.mode & R_BORDER) {
585                 xminb= R.r.border.xmin*R.rectx;
586                 xmaxb= R.r.border.xmax*R.rectx;
587
588                 yminb= R.r.border.ymin*R.recty;
589                 ymaxb= R.r.border.ymax*R.recty;
590
591                 if(xminb<0) xminb= 0;
592                 if(xmaxb>R.rectx) xmaxb= R.rectx;
593                 if(yminb<0) yminb= 0;
594                 if(ymaxb>R.recty) ymaxb= R.recty;
595         }
596         else {
597                 xminb=yminb= 0;
598                 xmaxb= R.rectx;
599                 ymaxb= R.recty;
600         }
601
602         xparts= R.r.xparts;     /* for border */
603         yparts= R.r.yparts;
604
605         for(nr=0;nr<xparts*yparts;nr++)
606                 allparts[nr][0]= -1;    /* clear array */
607
608         xpart= R.rectx/xparts;
609         ypart= R.recty/yparts;
610
611         /* if border: test if amount of parts can be fewer */
612         if(R.r.mode & R_BORDER) {
613                 a= (xmaxb-xminb-1)/xpart+1; /* amount of parts in border */
614                 if(a<xparts) xparts= a;
615                 a= (ymaxb-yminb-1)/ypart+1; /* amount of parts in border */
616                 if(a<yparts) yparts= a;
617
618                 xpart= (xmaxb-xminb)/xparts;
619                 ypart= (ymaxb-yminb)/yparts;
620         }
621
622         for(nr=0; nr<xparts*yparts; nr++) {
623
624                 if(R.r.mode & R_PANORAMA) {
625                         allparts[nr][0]= 0;
626                         allparts[nr][1]= 0;
627                         allparts[nr][2]= R.rectx;
628                         allparts[nr][3]= R.recty;
629                 }
630                 else {
631                         xd= (nr % xparts);
632                         yd= (nr-xd)/xparts;
633
634                         allparts[nr][0]= xminb+ xd*xpart;
635                         allparts[nr][1]= yminb+ yd*ypart;
636                         if(xd<R.r.xparts-1) allparts[nr][2]= allparts[nr][0]+xpart;
637                         else allparts[nr][2]= xmaxb;
638                         if(yd<R.r.yparts-1) allparts[nr][3]= allparts[nr][1]+ypart;
639                         else allparts[nr][3]= ymaxb;
640
641                         if(allparts[nr][2]-allparts[nr][0]<=0) allparts[nr][0]= -1;
642                         if(allparts[nr][3]-allparts[nr][1]<=0) allparts[nr][0]= -1;
643                 }
644         }
645 }
646
647 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
648 short setpart(short nr) /* return 0 if incorrect part */
649 {
650
651         if(allparts[nr][0]== -1) return 0;
652
653         R.xstart= allparts[nr][0]-R.afmx;
654         R.ystart= allparts[nr][1]-R.afmy;
655         R.xend= allparts[nr][2]-R.afmx;
656         R.yend= allparts[nr][3]-R.afmy;
657         R.rectx= R.xend-R.xstart;
658         R.recty= R.yend-R.ystart;
659
660         return 1;
661 }
662
663 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
664 void addparttorect(short nr, Part *part)
665 {
666         unsigned int *rt, *rp;
667         short y, heigth, len;
668
669         /* the right offset in rectot */
670
671         rt= R.rectot+ (allparts[nr][1]*R.rectx+ allparts[nr][0]);
672         rp= part->rect;
673         len= (allparts[nr][2]-allparts[nr][0]);
674         heigth= (allparts[nr][3]-allparts[nr][1]);
675
676         for(y=0;y<heigth;y++) {
677                 memcpy(rt, rp, 4*len);
678                 rt+=R.rectx;
679                 rp+= len;
680         }
681 }
682
683
684 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
685
686  
687 void add_to_blurbuf(int blur)
688 {
689         static unsigned int *blurrect= 0;
690         int tot, gamval;
691         short facr, facb;
692         char *rtr, *rtb;
693
694         if(blur<0) {
695                 if(blurrect) {
696                         if(R.rectot) MEM_freeN(R.rectot);
697                         R.rectot= blurrect;
698                         blurrect= 0;
699                 }
700         }
701         else if(blur==R.osa-1) {
702                 /* first time */
703                 blurrect= MEM_mallocN(R.rectx*R.recty*sizeof(int), "rectblur");
704                 if(R.rectot) memcpy(blurrect, R.rectot, R.rectx*R.recty*4);
705         }
706         else if(blurrect) {
707                 /* accumulate */
708
709                 facr= 256/(R.osa-blur);
710                 facb= 256-facr;
711
712                 if(R.rectot) {
713                         rtr= (char *)R.rectot;
714                         rtb= (char *)blurrect;
715                         tot= R.rectx*R.recty;
716                         while(tot--) {
717                                 if( *((unsigned int *)rtb) != *((unsigned int *)rtr) ) {
718
719                                         if(R.r.mode & R_GAMMA) {
720                                                 gamval= (facr* igamtab2[ rtr[0]<<8 ] + facb* igamtab2[ rtb[0]<<8 ])>>8;
721                                                 rtb[0]= gamtab[ gamval ]>>8;
722                                                 gamval= (facr* igamtab2[ rtr[1]<<8 ] + facb* igamtab2[ rtb[1]<<8 ])>>8;
723                                                 rtb[1]= gamtab[ gamval ]>>8;
724                                                 gamval= (facr* igamtab2[ rtr[2]<<8 ] + facb* igamtab2[ rtb[2]<<8 ])>>8;
725                                                 rtb[2]= gamtab[ gamval ]>>8;
726                                                 gamval= (facr* igamtab2[ rtr[3]<<8 ] + facb* igamtab2[ rtb[3]<<8 ])>>8;
727                                                 rtb[3]= gamtab[ gamval ]>>8;
728                                         }
729                                         else {
730                                                 rtb[0]= (facr*rtr[0] + facb*rtb[0])>>8;
731                                                 rtb[1]= (facr*rtr[1] + facb*rtb[1])>>8;
732                                                 rtb[2]= (facr*rtr[2] + facb*rtb[2])>>8;
733                                                 rtb[3]= (facr*rtr[3] + facb*rtb[3])>>8;
734                                         }
735                                 }
736                                 rtr+= 4;
737                                 rtb+= 4;
738                         }
739                 }
740                 if(blur==0) {
741                         /* last time */
742                         if(R.rectot) MEM_freeN(R.rectot);
743                         R.rectot= blurrect;
744                         blurrect= 0;
745                 }
746         }
747 }
748
749
750 /* yafray: main yafray render/export call */
751 void yafrayRender()
752 {
753         R.flag |= R_RENDERING;  /* !!! */
754         printf("Starting scene conversion.\n");
755         prepareScene();
756         printf("Scene conversion done.\n");
757         YAF_exportScene();
758         finalizeScene();
759 }
760
761
762 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
763 void render() {
764         /* yafray: render, see above */
765         if (R.r.renderer==R_YAFRAY)
766                 yafrayRender();
767         else {
768                 /* not too neat... should improve... */
769                 if(R.r.mode & R_UNIFIED) {
770                         unifiedRenderingLoop();
771                 } else {
772                         oldRenderLoop();
773                 }
774         }
775 }
776
777
778 void oldRenderLoop(void)  /* here the PART and FIELD loops */
779 {
780         Part *part;
781         unsigned int *rt, *rt1, *rt2;
782         int len, y;
783         short blur, a,fields,fi,parts;  /* pa is a global because of print */
784
785         if (R.rectz) MEM_freeN(R.rectz);
786         R.rectz = 0;
787
788         /* FIELD LOOP */
789         fields= 1;
790         parts= R.r.xparts*R.r.yparts;
791
792         if(R.r.mode & R_FIELDS) {
793                 fields= 2;
794                 R.rectf1= R.rectf2= 0;  /* field rects */
795                 R.r.ysch/= 2;
796                 R.afmy/= 2;
797                 R.r.yasp*= 2;
798                 R.ycor= ( (float)R.r.yasp)/( (float)R.r.xasp);
799
800         }
801
802         
803         for(fi=0; fi<fields; fi++) {
804
805                 /* INIT */
806                 BLI_srand( 2*(G.scene->r.cfra)+fi);
807                         
808                 R.flag|= R_RENDERING;
809                 if(fi==1) R.flag |= R_SEC_FIELD;
810         
811                 /* MOTIONBLUR loop */
812                 if(R.r.mode & R_MBLUR) blur= R.osa;
813                 else blur= 1;
814                 while(blur--) {
815
816                         /* WINDOW */
817                         R.rectx= R.r.xsch;
818                         R.recty= R.r.ysch;
819                         R.xstart= -R.afmx;
820                         R.ystart= -R.afmy;
821                         R.xend= R.xstart+R.rectx-1;
822                         R.yend= R.ystart+R.recty-1;
823
824
825                         if(R.r.mode & R_MBLUR) set_mblur_offs(R.osa-blur);
826
827                         initparts(); /* always do, because of border */
828                         setpart(0);
829
830                         RE_local_init_render_display();
831                         RE_local_clear_render_display(R.win);
832                         RE_local_timecursor((G.scene->r.cfra));
833
834                         prepareScene();
835
836                         /* PARTS */
837                         R.parts.first= R.parts.last= 0;
838                         for(pa=0; pa<parts; pa++) {
839
840                                 if(RE_local_test_break()) break;
841
842                                 if(pa) {        /* because case pa==0 has been done */
843                                         if(setpart(pa)==0) break;
844                                 }
845
846                                 if(R.r.mode & R_MBLUR) RE_setwindowclip(0, blur);
847                                 else RE_setwindowclip(0,-1);
848
849                                 if(R.r.mode & R_PANORAMA) setPanoRot(pa);
850
851                                 /* HOMOGENIC COORDINATES AND ZBUF AND CLIP OPTIMISATION (per part) */
852                                 /* There may be some interference with z-coordinate    */
853                                 /* calculation here?                                   */
854
855                                 doClipping(RE_projectverto);
856                                 if(RE_local_test_break()) break;
857
858
859                                 /* ZBUFFER & SHADE: zbuffer stores integer distances, and integer face indices */
860                                 R.rectot= (unsigned int *)MEM_callocN(sizeof(int)*R.rectx*R.recty, "rectot");
861                                 R.rectz =  (unsigned int *)MEM_mallocN(sizeof(int)*R.rectx*R.recty, "rectz");
862
863                                 if(R.r.mode & R_MBLUR) RE_local_printrenderinfo(0.0, R.osa - blur);
864                                 else RE_local_printrenderinfo(0.0, -1);
865
866                                 /* choose render pipeline type, and whether or not to use the */
867                                 /* delta accumulation buffer. 3 choices.                      */
868                                 if(R.r.mode & R_OSA) zbufshadeDA();
869                                 else                 zbufshade();
870
871                                 if(RE_local_test_break()) break;
872
873                                 /* exception */
874                                 if( (R.r.mode & R_BORDER) && (R.r.mode & R_MOVIECROP));
875                                 else {
876                                         /* HANDLE PART OR BORDER */
877                                         if(parts>1 || (R.r.mode & R_BORDER)) {
878
879                                                 part= MEM_callocN(sizeof(Part), "part");
880                                                 BLI_addtail(&R.parts, part);
881                                                 part->rect= R.rectot;
882                                                 R.rectot= NULL;
883
884                                                 if (R.rectz) {
885                                                         MEM_freeN(R.rectz);
886                                                         R.rectz= NULL;
887                                                 }
888                                         }
889                                 }
890                         }
891
892                         /* JOIN PARTS OR INSERT BORDER */
893
894                         /* exception: crop */
895                         if( (R.r.mode & R_BORDER) && (R.r.mode & R_MOVIECROP)) ;
896                         else {
897                                 R.rectx= R.r.xsch;
898                                 R.recty= R.r.ysch;
899
900                                 if(R.r.mode & R_PANORAMA) R.rectx*= R.r.xparts;
901
902                                 if(parts>1 || (R.r.mode & R_BORDER)) {
903                                         if(R.rectot) MEM_freeN(R.rectot);
904                                         
905                                         R.rectot=(unsigned int *)MEM_callocN(sizeof(int)*R.rectx*R.recty, "rectot");
906                                         
907                                         part= R.parts.first;
908                                         for(pa=0; pa<parts; pa++) {
909                                                 if(allparts[pa][0]== -1) break;
910                                                 if(part==0) break;
911                                                 
912                                                 if(R.r.mode & R_PANORAMA) {
913                                                         if(pa) {
914                                                                 allparts[pa][0] += pa*R.r.xsch;
915                                                                 allparts[pa][2] += pa*R.r.xsch;
916                                                         }
917                                                 }
918                                                 addparttorect(pa, part);
919                                                 
920                                                 part= part->next;
921                                         }
922                                         
923                                         part= R.parts.first;
924                                         while(part) {
925                                                 MEM_freeN(part->rect);
926                                                 part= part->next;
927                                         }
928                                         BLI_freelistN(&R.parts);
929                                 }
930                         }
931
932                         if( (R.flag & R_HALO)) {
933                                 if(RE_local_test_break()==0) add_halo_flare();
934                         }
935
936                         if(R.r.mode & R_MBLUR) {
937                                 add_to_blurbuf(blur);
938                         }
939
940                         /* END (blur loop) */
941                         finalizeScene();
942
943                         if(RE_local_test_break()) break;
944                 }
945
946                 /* definite free */
947                 add_to_blurbuf(-1);
948
949                 /* HANDLE FIELD */
950                 if(R.r.mode & R_FIELDS) {
951                         if(R.flag & R_SEC_FIELD) R.rectf2= R.rectot;
952                         else R.rectf1= R.rectot;
953                         R.rectot= 0;
954                 }
955
956                 if(RE_local_test_break()) break;
957         }
958
959         /* JOIN FIELDS */
960         if(R.r.mode & R_FIELDS) {
961                 R.r.ysch*= 2;
962                 R.afmy*= 2;
963                 R.recty*= 2;
964                 R.r.yasp/=2;
965
966                 if(R.rectot) MEM_freeN(R.rectot);       /* happens when a render has been stopped */
967                 R.rectot=(unsigned int *)MEM_callocN(sizeof(int)*R.rectx*R.recty, "rectot");
968
969                 if(RE_local_test_break()==0) {
970                         rt= R.rectot;
971
972                         if(R.r.mode & R_ODDFIELD) {
973                                 rt2= R.rectf1;
974                                 rt1= R.rectf2;
975                         }
976                         else {
977                                 rt1= R.rectf1;
978                                 rt2= R.rectf2;
979                         }
980
981                         len= 4*R.rectx;
982
983                         for(a=0; a<R.recty; a+=2) {
984                                 memcpy(rt, rt1, len);
985                                 rt+= R.rectx;
986                                 rt1+= R.rectx;
987                                 memcpy(rt, rt2, len);
988                                 rt+= R.rectx;
989                                 rt2+= R.rectx;
990                         }
991                 }
992         }
993
994         /* R.rectx= R.r.xsch; */
995         /* if(R.r.mode & R_PANORAMA) R.rectx*= R.r.xparts; */
996         /* R.recty= R.r.ysch; */
997
998         /* if border: still do skybuf */
999         if(R.r.mode & R_BORDER) {
1000                 if( (R.r.mode & R_MOVIECROP)==0) {
1001                         if(R.r.bufflag & 1) {
1002                                 R.xstart= -R.afmx;
1003                                 R.ystart= -R.afmy;
1004                                 rt= R.rectot;
1005                                 for(y=0; y<R.recty; y++, rt+= R.rectx) scanlinesky((char *)rt, y);
1006                         }
1007                 }
1008         }
1009
1010         set_mblur_offs(0);
1011
1012         /* FREE */
1013
1014         /* zbuf test */
1015
1016         /* don't free R.rectz, only when its size is not the same as R.rectot */
1017
1018         if (R.rectz && parts == 1 && (R.r.mode & R_FIELDS) == 0);
1019         else {
1020                 if(R.rectz) MEM_freeN(R.rectz);
1021                 R.rectz= 0;
1022         }
1023
1024         if(R.rectf1) MEM_freeN(R.rectf1);
1025         R.rectf1= 0;
1026         if(R.rectf2) MEM_freeN(R.rectf2);
1027         R.rectf2= 0;
1028 }
1029
1030
1031
1032 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
1033 extern unsigned short usegamtab;
1034 void RE_initrender(struct View3D *ogl_render_view3d)
1035 {
1036         double start_time;
1037         Image *bima;
1038         char name[256];
1039         
1040         /* scene data to R */
1041         R.r= G.scene->r;
1042         R.r.postigamma= 1.0/R.r.postgamma;
1043         
1044         /* WINDOW size (sch='scherm' dutch for screen...) */
1045         R.r.xsch= (R.r.size*R.r.xsch)/100;
1046         R.r.ysch= (R.r.size*R.r.ysch)/100;
1047
1048         R.afmx= R.r.xsch/2;
1049         R.afmy= R.r.ysch/2;
1050
1051         /* to be sure: when a premature return (rectx can differ from xsch) */
1052         R.rectx= R.r.xsch;
1053         R.recty= R.r.ysch;
1054
1055         /* IS RENDERING ALLOWED? */
1056
1057         /* forbidden combination */
1058         if((R.r.mode & R_BORDER) && (R.r.mode & R_PANORAMA)) {
1059                 error("No border allowed for Panorama");
1060                 G.afbreek= 1;
1061                 return;
1062         }
1063
1064         
1065         if(R.r.xparts*R.r.yparts>64) {
1066                 error("No more than 64 parts");
1067                 G.afbreek= 1;
1068                 return;
1069         }
1070
1071         if(R.r.yparts>1 && (R.r.mode & R_PANORAMA)) {
1072                 error("No Y-Parts allowed for Panorama");
1073                 G.afbreek= 1;
1074                 return;
1075         }
1076
1077
1078         /* TEST BACKBUF */
1079         /* If an image is specified for use as backdrop, that image is loaded    */
1080         /* here.                                                                 */
1081         if((R.r.bufflag & 1) && (G.scene->r.scemode & R_OGL)==0) {
1082                 if(R.r.alphamode == R_ADDSKY) {
1083                         strcpy(name, R.r.backbuf);
1084                         BLI_convertstringcode(name, G.sce, G.scene->r.cfra);
1085
1086                         if(R.backbuf) {
1087                                 R.backbuf->id.us--;
1088                                 bima= R.backbuf;
1089                         }
1090                         else bima= 0;
1091
1092                         R.backbuf= add_image(name);
1093
1094                         if(bima && bima->id.us<1) {
1095                                 free_image_buffers(bima);
1096                         }
1097                         if(R.backbuf==0) {
1098                                 // error() doesnt work with render window open
1099                                 //error("No backbuf there!");
1100                                 printf("Error: No backbuf %s\n", name);
1101                                 G.afbreek= 1;
1102                                 return;
1103                         }
1104                 }
1105         }
1106
1107
1108         usegamtab= 0; /* see also further */
1109
1110         if(R.r.mode & (R_OSA|R_MBLUR)) {
1111                 R.osa= R.r.osa;
1112                 if(R.osa>16) R.osa= 16;
1113
1114                 init_render_jit(R.osa);
1115                 RE_init_filt_mask();
1116
1117                 /* this value sometimes is reset temporally, for example in transp zbuf */
1118                 if(R.r.mode & R_GAMMA) {
1119                         if((R.r.mode & R_OSA)) usegamtab= 1;
1120                 }
1121         }
1122         else R.osa= 0;
1123
1124         /* when rendered without camera object */
1125         /* it has to done here because of envmaps */
1126         R.near= 0.1;
1127         R.far= 1000.0;
1128
1129
1130         if(R.afmx<1 || R.afmy<1) {
1131                 error("Image too small");
1132                 return;
1133         }
1134         R.ycor= ( (float)R.r.yasp)/( (float)R.r.xasp);
1135
1136         start_time= PIL_check_seconds_timer();
1137
1138         if(R.r.scemode & R_DOSEQ) {
1139                 R.rectx= R.r.xsch;
1140                 R.recty= R.r.ysch;
1141
1142                 if(R.rectot) MEM_freeN(R.rectot);
1143                 R.rectot= (unsigned int *)MEM_callocN(sizeof(int)*R.rectx*R.recty, "rectot");
1144
1145                 RE_local_timecursor((G.scene->r.cfra));
1146
1147                 if(RE_local_test_break()==0) do_render_seq();
1148
1149                 /* display */
1150                 if(R.rectot) RE_local_render_display(0, R.recty-1,
1151                                                                                          R.rectx, R.recty,
1152                                                                                          R.rectot);
1153         }
1154         else if(R.r.scemode & R_OGL) {
1155                 R.rectx= R.r.xsch;
1156                 R.recty= R.r.ysch;
1157
1158                 if(R.rectot) MEM_freeN(R.rectot);
1159                 R.rectot= (unsigned int *)MEM_callocN(sizeof(int)*R.rectx*R.recty, "rectot");
1160
1161                 RE_local_init_render_display();
1162                 drawview3d_render(ogl_render_view3d);
1163         }
1164         else {
1165                 if(G.scene->camera==0) {
1166                         G.scene->camera= scene_find_camera(G.scene);
1167                 }
1168
1169                 if(G.scene->camera==0) {
1170                         error("No camera");
1171                         G.afbreek=1;
1172                         return;
1173                 }
1174                 else {
1175
1176                         if(G.scene->camera->type==OB_CAMERA) {
1177                                 Camera *cam= G.scene->camera->data;
1178                                 if(cam->type==CAM_ORTHO) R.r.mode |= R_ORTHO;
1179                         }
1180
1181                         render(); /* returns with complete rect xsch-ysch */
1182                 }
1183         }
1184
1185         /* display again: fields/seq/parts/pano etc */
1186         if(R.rectot) {
1187                 RE_local_init_render_display();
1188                 RE_local_render_display(0, R.recty-1,
1189                                                                 R.rectx, R.recty,
1190                                                                 R.rectot);
1191         }
1192         else RE_local_clear_render_display(R.win);
1193
1194         RE_local_printrenderinfo((PIL_check_seconds_timer() - start_time), -1);
1195         
1196         /* restore variables */
1197         //R.osatex= 0;
1198         //R.vlr= 0;     
1199         ///* at cubemap */
1200         R.flag= 0;
1201 }
1202
1203 void RE_animrender(struct View3D *ogl_render_view3d)
1204 {
1205         int cfrao;
1206         char name[256];
1207
1208         if(G.scene==0) return;
1209
1210         /* scenedata to R: (for backbuf, R.rectx etc) */
1211         R.r= G.scene->r;
1212
1213         /* START ANIMLOOP, everywhere NOT the cfra from R.r is gebruikt: because of rest blender */
1214         cfrao= (G.scene->r.cfra);
1215
1216         if(G.scene->r.scemode & R_OGL) R.r.mode &= ~R_PANORAMA;
1217         
1218         // these calculations apply for
1219         // all movie formats
1220         R.rectx= (R.r.size*R.r.xsch)/100;
1221         R.recty= (R.r.size*R.r.ysch)/100;
1222         if(R.r.mode & R_PANORAMA) {
1223                 R.rectx*= R.r.xparts;
1224                 R.recty*= R.r.yparts;
1225         }
1226
1227         if (0) {
1228 #ifdef __sgi
1229         } else if (R.r.imtype==R_MOVIE) {
1230                 start_movie();
1231 #endif
1232 #if defined(_WIN32) && !defined(FREE_WINDOWS)
1233         } else if (R.r.imtype == R_AVICODEC) {
1234                 start_avi_codec();
1235 #endif
1236 #if WITH_QUICKTIME
1237         } else if (R.r.imtype == R_QUICKTIME) {
1238                 start_qt();
1239 #endif
1240         } else if ELEM4(R.r.imtype, R_AVIRAW, R_AVIJPEG, R_MOVIE, R_AVICODEC) {
1241                 if ELEM(R.r.imtype, R_MOVIE, R_AVICODEC) {
1242                         printf("Selected movie format not supported on this platform,\nusing RAW AVI instead\n");
1243                 }
1244                 start_avi();
1245         }
1246
1247         for((G.scene->r.cfra)=(G.scene->r.sfra); (G.scene->r.cfra)<=(G.scene->r.efra); (G.scene->r.cfra)++) {
1248                 double starttime= PIL_check_seconds_timer();
1249
1250                 R.flag= R_ANIMRENDER;
1251
1252                 RE_initrender(ogl_render_view3d);
1253                 
1254                 /* WRITE IMAGE */
1255                 if(RE_local_test_break()==0) {
1256
1257                         if (0) {
1258 #ifdef __sgi
1259                         } else if (R.r.imtype == R_MOVIE) {
1260                                 append_movie((G.scene->r.cfra));
1261 #endif
1262 #if defined(_WIN32) && !defined(FREE_WINDOWS)
1263                         } else if (R.r.imtype == R_AVICODEC) {
1264                                 append_avi_codec((G.scene->r.cfra));
1265 #endif
1266 #ifdef WITH_QUICKTIME
1267                         } else if (R.r.imtype == R_QUICKTIME) {
1268                                 append_qt((G.scene->r.cfra));
1269 #endif
1270                         } else if ELEM4(R.r.imtype, R_AVIRAW, R_AVIJPEG, R_MOVIE, R_AVICODEC) {
1271                                 append_avi((G.scene->r.cfra));
1272                         } else {
1273                                 makepicstring(name, (G.scene->r.cfra));
1274                                 schrijfplaatje(name);
1275                                 if(RE_local_test_break()==0) printf("Saved: %s", name);
1276                         }
1277
1278                         timestr(PIL_check_seconds_timer()-starttime, name);
1279                         printf(" Time: %s\n", name);
1280                         fflush(stdout); /* needed for renderd !! */
1281                 }
1282
1283                 if(G.afbreek==1) break;
1284
1285         }
1286
1287         (G.scene->r.cfra)= cfrao;
1288
1289         /* restore time */
1290         if(R.r.mode & (R_FIELDS|R_MBLUR)) {
1291                 do_all_ipos();
1292                 do_all_keys();
1293                 do_all_actions();
1294                 do_all_ikas();
1295         }
1296
1297         if (0) {
1298 #ifdef __sgi    
1299         } else if (R.r.imtype==R_MOVIE) {
1300                 end_movie();
1301 #endif
1302 #if defined(_WIN32) && !defined(FREE_WINDOWS)
1303         } else if (R.r.imtype == R_AVICODEC) {
1304                 end_avi_codec();
1305 #endif
1306 #ifdef WITH_QUICKTIME
1307         } else if (R.r.imtype == R_QUICKTIME) {
1308                 end_qt();
1309 #endif
1310         } else if ELEM4(R.r.imtype, R_AVIRAW, R_AVIJPEG, R_MOVIE, R_AVICODEC) {
1311                 end_avi();
1312         }
1313 }
1314
1315 /* *************************************************** */
1316 /* ******************* Screendumps ******************** */
1317 /* moved to the windowControl thing */