Orange branch: Revived hidden treasure, the Groups!
[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 #include "blendef.h"
46 #include "MEM_guardedalloc.h"
47
48 #include "PIL_time.h"
49
50 #include "BLI_arithb.h"
51 #include "BLI_blenlib.h"
52 #include "BLI_rand.h"
53
54 #include "MTC_matrixops.h"
55
56 #include "DNA_image_types.h"
57 #include "DNA_camera_types.h"
58 #include "DNA_lamp_types.h"
59 #include "DNA_scene_types.h"
60 #include "DNA_object_types.h"
61
62 #include "BKE_utildefines.h"
63 #include "BKE_global.h"
64 #include "BKE_material.h"
65 #include "BKE_object.h"
66 #include "BKE_image.h"
67 #include "BKE_ipo.h"
68 #include "BKE_key.h"
69 #include "BKE_action.h"
70 #include "BKE_writeavi.h"
71 #include "BKE_scene.h"
72
73 #include "BIF_toolbox.h"
74 #include "BIF_writeavicodec.h"
75 #include "BIF_writemovie.h"             /* start_movie(), append_movie(), end_movie() */
76
77 #include "BSE_drawview.h"
78 #include "BSE_sequence.h"
79
80 #include "IMB_imbuf_types.h"
81 #include "IMB_imbuf.h"
82
83 #ifdef WITH_QUICKTIME
84 #include "quicktime_export.h"
85 #endif
86
87 #include "SDL_thread.h"
88
89 /* this module */
90 #include "render.h"
91
92 #include "RE_callbacks.h"
93 #include "zbuf.h"
94 #include "rendercore.h" /* part handler for the old renderer, shading functions */
95 #include "pixelshading.h"
96 #include "renderPreAndPost.h"
97 #include "vanillaRenderPipe.h"
98 #include "renderHelp.h"
99 #include "jitter.h"
100 #include "gammaCorrectionTables.h"
101 #include "zblur.h"
102
103 /* Own includes */
104 #include "initrender.h"
105
106 /* yafray: include for yafray export/render */
107 #include "YafRay_Api.h"
108
109
110 float centLut[16], *fmask1[9], *fmask2[9];
111 unsigned short *gamtab, *igamtab2, *igamtab1;
112 char cmask[256], *centmask;
113
114 Material defmaterial;
115
116 /* ------- prototypes ----------- */ 
117 void init_filt_mask(void);
118
119
120 /* ****************** GAMMA, MASKS and LUTS **************** */
121
122 static float filt_quadratic(float x)
123 {
124     if (x <  0.0f) x = -x;
125     if (x < 0.5f) return 0.75f-(x*x);
126     if (x < 1.5f) return 0.50f*(x-1.5f)*(x-1.5f);
127     return 0.0f;
128 }
129
130
131 static float filt_cubic(float x)
132 {
133         float x2= x*x;
134         
135     if (x <  0.0f) x = -x;
136         
137     if (x < 1.0f) return 0.5*x*x2 - x2 + 2.0f/3.0f;
138     if (x < 2.0f) return (2.0-x)*(2.0-x)*(2.0-x)/6.0f;
139     return 0.0f;
140 }
141
142
143 static float filt_catrom(float x)
144 {
145         float x2= x*x;
146         
147     if (x <  0.0f) x = -x;
148     if (x < 1.0f) return  1.5f*x2*x - 2.5f*x2  + 1.0f;
149     if (x < 2.0f) return -0.5f*x2*x + 2.5*x2 - 4.0f*x + 2.0f;
150     return 0.0f;
151 }
152
153 static float filt_mitchell(float x)     /* Mitchell & Netravali's two-param cubic */
154 {
155         float b = 1.0f/3.0f, c = 1.0f/3.0f;
156         float p0 = (  6.0 -  2.0*b         ) / 6.0;
157         float p2 = (-18.0 + 12.0*b +  6.0*c) / 6.0;
158         float p3 = ( 12.0 -  9.0*b -  6.0*c) / 6.0;
159         float q0 = (       8.0*b + 24.0*c) / 6.0;
160         float q1 = (      - 12.0*b - 48.0*c) / 6.0;
161         float q2 = (         6.0*b + 30.0*c) / 6.0;
162         float q3 = (       -     b -  6.0*c) / 6.0;
163
164         if (x<-2.0) return 0.0;
165         if (x<-1.0) return (q0-x*(q1-x*(q2-x*q3)));
166         if (x< 0.0) return (p0+x*x*(p2-x*p3));
167         if (x< 1.0) return (p0+x*x*(p2+x*p3));
168         if (x< 2.0) return (q0+x*(q1+x*(q2+x*q3)));
169         return 0.0;
170 }
171
172 static float calc_weight(float *weight, int i, int j)
173 {
174         float x, y, dist, totw= 0.0;
175         int a;
176
177         for(a=0; a<R.osa; a++) {
178                 x= jit[a][0] + i;
179                 y= jit[a][1] + j;
180                 dist= sqrt(x*x+y*y);
181
182                 weight[a]= 0.0;
183
184                 /* Weighting choices */
185                 switch(R.r.filtertype) {
186                 case R_FILTER_BOX:
187                         if(i==0 && j==0) weight[a]= 1.0;
188                         break;
189                         
190                 case R_FILTER_TENT:
191                         if(dist < R.r.gauss)
192                                 weight[a]= R.r.gauss - dist;
193                         break;
194                         
195                 case R_FILTER_GAUSS:
196                         x = dist*R.r.gauss;
197                         weight[a]= (1.0/exp(x*x) - 1.0/exp(R.r.gauss*R.r.gauss*2.25));
198                         break;
199                 
200                 case R_FILTER_MITCH:
201                         weight[a]= filt_mitchell(dist*R.r.gauss);
202                         break;
203                 
204                 case R_FILTER_QUAD:
205                         weight[a]= filt_quadratic(dist*R.r.gauss);
206                         break;
207                         
208                 case R_FILTER_CUBIC:
209                         weight[a]= filt_cubic(dist*R.r.gauss);
210                         break;
211                         
212                 case R_FILTER_CATROM:
213                         weight[a]= filt_catrom(dist*R.r.gauss);
214                         break;
215                         
216                 }
217                 
218                 totw+= weight[a];
219
220         }
221         return totw;
222 }
223
224 // extern called in render_envmap, to disable gauss...
225 // extern called in sequence.c, it needs the gamtab
226 void init_filt_mask(void)
227 {
228         static int firsttime=1;
229         static int lastosa=0;
230         static int lastfilter= -1;
231         static float lastgamma= 0.0f, lastgaussdist=0.0f;
232         float gamma, igamma, flweight[32], fmask[256];
233         float weight[32], totw, val, *fpx1, *fpx2, *fpy1, *fpy2, *m3, *m4;
234         int i, j, a;
235
236         if(firsttime) {
237                 firsttime= 0;
238                 
239                 for(a=0; a<9;a++) {
240                         fmask1[a]= MEM_mallocN(256*sizeof(float), "initfilt");
241                         fmask2[a]= MEM_mallocN(256*sizeof(float), "initfilt");
242                 }
243                 for(a=0; a<256; a++) {
244                         cmask[a]= 0;
245                         if(a &   1) cmask[a]++;
246                         if(a &   2) cmask[a]++;
247                         if(a &   4) cmask[a]++;
248                         if(a &   8) cmask[a]++;
249                         if(a &  16) cmask[a]++;
250                         if(a &  32) cmask[a]++;
251                         if(a &  64) cmask[a]++;
252                         if(a & 128) cmask[a]++;
253                 }
254                 centmask= MEM_mallocN(65536, "Initfilt3");
255                 for(a=0; a<16; a++) {
256                         centLut[a]= -0.45+((float)a)/16.0;
257                 }
258
259                 gamtab= MEM_mallocN(65536*sizeof(short), "initGaus2");
260                 igamtab1= MEM_mallocN(256*sizeof(short), "initGaus2");
261                 igamtab2= MEM_mallocN(65536*sizeof(short), "initGaus2");
262                 
263                 return; // this case is called on startup
264         }
265
266         if(R.r.alphamode==R_ALPHAKEY) gamma= 1.0;       /* gamma correction of alpha is nasty */
267         else if(R.r.mode & R_GAMMA) gamma= 2.0;
268         else gamma= 1.0;
269         
270         igamma= 1.0/gamma;
271
272         if(gamma!= lastgamma) {
273                 lastgamma= gamma;
274
275                 /* gamtab: in short, out short */
276                 for(a=0; a<65536; a++) {
277                         val= a;
278                         val/= 65535.0;
279
280                         if(gamma==2.0) val= sqrt(val);
281                         else if(gamma!=1.0) val= pow(val, igamma);
282
283                         gamtab[a]= (65535.99*val);
284                 }
285                 /* inverse gamtab1 : in byte, out short */
286                 for(a=1; a<=256; a++) {
287                         if(gamma==2.0) igamtab1[a-1]= a*a-1;
288                         else if(gamma==1.0) igamtab1[a-1]= 256*a-1;
289                         else {
290                                 val= a/256.0;
291                                 igamtab1[a-1]= (65535.0*pow(val, gamma)) -1 ;
292                         }
293                 }
294
295                 /* inverse gamtab2 : in short, out short */
296                 for(a=0; a<65536; a++) {
297                         val= a;
298                         val/= 65535.0;
299                         if(gamma==2.0) val= val*val;
300                         else val= pow(val, gamma);
301
302                         igamtab2[a]= 65535.0*val;
303                 }
304         }
305
306         if(R.osa && (lastosa!=R.osa || lastfilter != (R.r.filtertype) || lastgaussdist!=R.r.gauss)) {
307                 lastosa= R.osa;
308                 lastfilter= R.r.filtertype;
309                 lastgaussdist= R.r.gauss;
310                 
311                 val= 1.0/((float)R.osa);
312                 for(a=0; a<256; a++) {
313                         fmask[a]= ((float)cmask[a])*val;
314                 }
315
316                 for(a=0; a<9;a++) {
317                         memset(fmask1[a], 0, 256*sizeof(float));
318                         memset(fmask2[a], 0, 256*sizeof(float));
319                 }
320
321                 /* calculate totw */
322                 totw= 0.0;
323                 for(j= -1; j<2; j++) {
324                         for(i= -1; i<2; i++) {
325                                 totw+= calc_weight(weight, i, j);
326                         }
327                 }
328
329                 for(j= -1; j<2; j++) {
330                         for(i= -1; i<2; i++) {
331                                 /* calculate using jit, with offset the weights */
332
333                                 memset(weight, 0, sizeof(weight));
334                                 calc_weight(weight, i, j);
335
336                                 for(a=0; a<16; a++) flweight[a]= weight[a]*(1.0/totw);
337
338                                 m3= fmask1[ 3*(j+1)+i+1 ];
339                                 m4= fmask2[ 3*(j+1)+i+1 ];
340
341                                 for(a=0; a<256; a++) {
342                                         if(a &   1) {
343                                                 m3[a]+= flweight[0];
344                                                 m4[a]+= flweight[8];
345                                         }
346                                         if(a &   2) {
347                                                 m3[a]+= flweight[1];
348                                                 m4[a]+= flweight[9];
349                                         }
350                                         if(a &   4) {
351                                                 m3[a]+= flweight[2];
352                                                 m4[a]+= flweight[10];
353                                         }
354                                         if(a &   8) {
355                                                 m3[a]+= flweight[3];
356                                                 m4[a]+= flweight[11];
357                                         }
358                                         if(a &  16) {
359                                                 m3[a]+= flweight[4];
360                                                 m4[a]+= flweight[12];
361                                         }
362                                         if(a &  32) {
363                                                 m3[a]+= flweight[5];
364                                                 m4[a]+= flweight[13];
365                                         }
366                                         if(a &  64) {
367                                                 m3[a]+= flweight[6];
368                                                 m4[a]+= flweight[14];
369                                         }
370                                         if(a & 128) {
371                                                 m3[a]+= flweight[7];
372                                                 m4[a]+= flweight[15];
373                                         }
374                                 }
375                         }
376                 }
377
378                 /* centmask: the correct subpixel offset per mask */
379
380                 fpx1= MEM_mallocN(256*sizeof(float), "initgauss4");
381                 fpx2= MEM_mallocN(256*sizeof(float), "initgauss4");
382                 fpy1= MEM_mallocN(256*sizeof(float), "initgauss4");
383                 fpy2= MEM_mallocN(256*sizeof(float), "initgauss4");
384                 for(a=0; a<256; a++) {
385                         fpx1[a]= fpx2[a]= 0.0;
386                         fpy1[a]= fpy2[a]= 0.0;
387                         if(a & 1) {
388                                 fpx1[a]+= jit[0][0];
389                                 fpy1[a]+= jit[0][1];
390                                 fpx2[a]+= jit[8][0];
391                                 fpy2[a]+= jit[8][1];
392                         }
393                         if(a & 2) {
394                                 fpx1[a]+= jit[1][0];
395                                 fpy1[a]+= jit[1][1];
396                                 fpx2[a]+= jit[9][0];
397                                 fpy2[a]+= jit[9][1];
398                         }
399                         if(a & 4) {
400                                 fpx1[a]+= jit[2][0];
401                                 fpy1[a]+= jit[2][1];
402                                 fpx2[a]+= jit[10][0];
403                                 fpy2[a]+= jit[10][1];
404                         }
405                         if(a & 8) {
406                                 fpx1[a]+= jit[3][0];
407                                 fpy1[a]+= jit[3][1];
408                                 fpx2[a]+= jit[11][0];
409                                 fpy2[a]+= jit[11][1];
410                         }
411                         if(a & 16) {
412                                 fpx1[a]+= jit[4][0];
413                                 fpy1[a]+= jit[4][1];
414                                 fpx2[a]+= jit[12][0];
415                                 fpy2[a]+= jit[12][1];
416                         }
417                         if(a & 32) {
418                                 fpx1[a]+= jit[5][0];
419                                 fpy1[a]+= jit[5][1];
420                                 fpx2[a]+= jit[13][0];
421                                 fpy2[a]+= jit[13][1];
422                         }
423                         if(a & 64) {
424                                 fpx1[a]+= jit[6][0];
425                                 fpy1[a]+= jit[6][1];
426                                 fpx2[a]+= jit[14][0];
427                                 fpy2[a]+= jit[14][1];
428                         }
429                         if(a & 128) {
430                                 fpx1[a]+= jit[7][0];
431                                 fpy1[a]+= jit[7][1];
432                                 fpx2[a]+= jit[15][0];
433                                 fpy2[a]+= jit[15][1];
434                         }
435                 }
436
437                 for(a= (1<<R.osa)-1; a>0; a--) {
438                         val= count_mask(a);
439                         i= 8+(15.9*(fpy1[a & 255]+fpy2[a>>8])/val);
440                         CLAMP(i, 0, 15);
441                         j= 8+(15.9*(fpx1[a & 255]+fpx2[a>>8])/val);
442                         CLAMP(j, 0, 15);
443                         i= j + (i<<4);
444                         centmask[a]= i;
445                 }
446
447                 MEM_freeN(fpx1);
448                 MEM_freeN(fpx2);
449                 MEM_freeN(fpy1);
450                 MEM_freeN(fpy2);
451         }
452 }
453
454 static void free_filt_mask()
455 {
456         int a;
457
458         for(a=0; a<9; a++) {
459                 MEM_freeN(fmask1[a]);
460                 MEM_freeN(fmask2[a]);
461         }
462         MEM_freeN(gamtab);
463         MEM_freeN(igamtab1);
464         MEM_freeN(igamtab2);
465
466         MEM_freeN(centmask);
467 }
468
469 /* ********************* init calls *********************** */
470
471 static void init_def_material(void)
472 {
473         Material *ma;
474         
475         ma= &defmaterial;
476         
477         init_material(&defmaterial);
478         
479         init_render_material(ma);
480 }
481
482 void RE_init_render_data(void)
483 {
484         memset(&R, 0, sizeof(RE_Render));
485         
486         R.blove= (VertRen **)MEM_callocN(sizeof(void *)*(TABLEINITSIZE),"Blove");
487         R.blovl= (VlakRen **)MEM_callocN(sizeof(void *)*(TABLEINITSIZE),"Blovl");
488         R.bloha= (HaloRen **)MEM_callocN(sizeof(void *)*(TABLEINITSIZE),"Bloha");
489         
490         init_def_material();
491         init_filt_mask();
492 }
493
494 void RE_free_render_data()
495 {
496         MEM_freeN(R.blove);
497         R.blove= NULL;
498         MEM_freeN(R.blovl);
499         R.blovl= NULL;
500         MEM_freeN(R.bloha);
501         R.bloha= NULL;
502
503         if(R.rectot) MEM_freeN(R.rectot);
504         if(R.rectftot) MEM_freeN(R.rectftot);
505         if(R.rectz) MEM_freeN(R.rectz);
506         if(R.rectspare) MEM_freeN(R.rectspare);
507         R.rectot= NULL;
508         R.rectftot= NULL;
509         R.rectz= NULL;
510         R.rectspare= NULL;
511         
512         free_filt_mask();
513 }
514
515 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
516
517 void RE_make_existing_file(char *name)
518 {
519         char di[FILE_MAXDIR], fi[FILE_MAXFILE];
520
521         strcpy(di, name);
522         BLI_splitdirstring(di, fi);
523
524         /* test exist */
525         if (BLI_exists(di) == 0) {
526                 BLI_recurdir_fileops(di);
527         }
528 }
529
530
531 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
532
533 void RE_setwindowclip(int mode, int jmode)
534 {
535         extern float bluroffsx, bluroffsy;      // rendercore.c... hackish (ton)
536         Camera *cam=NULL;
537         float lens, minx, miny, maxx, maxy;
538         float xd, yd, afmx, afmy;
539
540         if(G.scene->camera==NULL) return;
541
542         afmx= R.afmx;
543         afmy= R.afmy;
544
545         if(mode) {
546
547                 if(G.scene->camera->type==OB_LAMP) {
548                         /* fac= cos( PI*((float)(256- la->spsi))/512.0 ); */
549
550                         /* phi= acos(fac); */
551                         /* lens= 16.0*fac/sin(phi); */
552                         lens= 35.0;
553                         R.near= 0.1;
554                         R.far= 1000.0;
555                 }
556                 else if(G.scene->camera->type==OB_CAMERA) {
557                         cam= G.scene->camera->data;
558
559                         lens= cam->lens;
560                         R.near= cam->clipsta;
561                         R.far= cam->clipend;
562                 }
563                 else {
564                         lens= 16.0;
565                 }
566
567                 if(R.r.mode & R_ORTHO) {
568                         if( (R.r.xasp*afmx) >= (R.r.yasp*afmy) ) {
569                                 R.viewfac= 2.0*afmx;
570                         }
571                         else {
572                                 R.viewfac= 2.0*R.ycor*afmy;
573                         }
574                         /* ortho_scale == 1.0 means exact 1 to 1 mapping */
575                         R.pixsize= cam->ortho_scale/R.viewfac;
576                 }
577                 else {
578                         if( (R.r.xasp*afmx) >= (R.r.yasp*afmy) ) {
579                                 R.viewfac= (afmx*lens)/16.0;
580                         }
581                         else {
582                                 R.viewfac= R.ycor*(afmy*lens)/16.0;
583                         }
584                         
585                         R.pixsize= R.near/R.viewfac;
586                 }
587                 
588                 /* pixsize is not a real global... get rid of it! (ton) */
589         }
590
591         /* revision / simplification of subpixel offsets:
592            - the matrix will go without offset from start (e.g. -100) to end (e.g. +99).
593            - filling in with zbuffer will set offset of 0.5. to make sure clipped faces fill in too
594            - in shadepixel() again that 0.5 offset is corrected
595         */
596         minx= R.xstart; 
597         miny= R.ycor*(R.ystart); 
598         maxx= R.xend; 
599         maxy= R.ycor*(R.yend); 
600            
601         if(R.flag & R_SEC_FIELD) {
602                 if(R.r.mode & R_ODDFIELD) {
603                         miny-= .5*R.ycor;
604                         maxy-= .5*R.ycor;
605                 }
606                 else {
607                         miny+= .5*R.ycor;
608                         maxy+= .5*R.ycor;
609                 }
610         }
611
612         xd= yd= 0.0;
613         if(jmode!= -1) {
614                 bluroffsx= xd= jit[jmode % R.osa][0];
615                 bluroffsy= yd= R.ycor*jit[jmode % R.osa][1];
616         }
617         else bluroffsx=bluroffsy= 0.0;
618
619         minx= R.pixsize*(minx+xd);
620         maxx= R.pixsize*(maxx+xd);
621         miny= R.pixsize*(miny+yd);
622         maxy= R.pixsize*(maxy+yd);
623
624         if(R.r.mode & R_ORTHO)
625                 i_ortho(minx, maxx, miny, maxy, R.near, R.far, R.winmat);
626         else 
627                 i_window(minx, maxx, miny, maxy, R.near, R.far, R.winmat);
628
629         //printmatrix4("win", R.winmat);
630 }
631
632
633 /* ~~~~~~~~~~~~~~~~ PARTS ~~~~~~~~~~~~~~~~~~~~~~ */
634
635 /** 
636 * Part as in part-rendering. An image rendered in parts is rendered
637 * to a list of parts, with x,y size, and a pointer to the render
638 * output stored per part. Internal!
639 */
640 typedef struct Part
641 {
642         struct Part *next, *prev;
643         unsigned int *rect;             // color 4x8 bits
644         float *rectf;                   // color 4x32 bits
645         unsigned int *rectz;    // zbuffer
646
647         short minx, miny, maxx, maxy, x, y;
648 } Part;
649
650 static void freeparts(void)
651 {
652         Part *part= R.parts.first;
653         while(part) {
654                 if(part->rect) MEM_freeN(part->rect);
655                 if(part->rectz) MEM_freeN(part->rectz);
656                 if(part->rectf) MEM_freeN(part->rectf);
657                 part= part->next;
658         }
659         BLI_freelistN(&R.parts);
660 }
661
662 static void initparts(void)
663 {
664         Part *pa;
665         short nr, xd, yd, xpart, ypart, xparts, yparts;
666         short a, xminb, xmaxb, yminb, ymaxb;
667
668         freeparts();
669         
670         if(R.r.mode & R_BORDER) {
671                 xminb= R.r.border.xmin*R.rectx;
672                 xmaxb= R.r.border.xmax*R.rectx;
673
674                 yminb= R.r.border.ymin*R.recty;
675                 ymaxb= R.r.border.ymax*R.recty;
676
677                 if(xminb<0) xminb= 0;
678                 if(xmaxb>R.rectx) xmaxb= R.rectx;
679                 if(yminb<0) yminb= 0;
680                 if(ymaxb>R.recty) ymaxb= R.recty;
681         }
682         else {
683                 xminb=yminb= 0;
684                 xmaxb= R.rectx;
685                 ymaxb= R.recty;
686         }
687
688         xparts= R.r.xparts;     /* for border */
689         yparts= R.r.yparts;
690
691         xpart= R.rectx/xparts;
692         ypart= R.recty/yparts;
693
694         /* if border: test if amount of parts can be fewer */
695         if(R.r.mode & R_BORDER) {
696                 a= (xmaxb-xminb-1)/xpart+1; /* amount of parts in border */
697                 if(a<xparts) xparts= a;
698                 a= (ymaxb-yminb-1)/ypart+1; /* amount of parts in border */
699                 if(a<yparts) yparts= a;
700
701                 xpart= (xmaxb-xminb)/xparts;
702                 ypart= (ymaxb-yminb)/yparts;
703         }
704         
705         for(nr=0; nr<xparts*yparts; nr++) {
706                 pa= MEM_callocN(sizeof(Part), "new part");
707                 
708                 if(R.r.mode & R_PANORAMA) {
709                         pa->minx= pa->miny= 0;
710                         pa->maxx= pa->x= R.rectx;
711                         pa->maxy= pa->y= R.recty;
712                 }
713                 else {
714                         xd= (nr % xparts);
715                         yd= (nr-xd)/xparts;
716
717                         pa->minx= xminb+ xd*xpart;
718                         pa->miny= yminb+ yd*ypart;
719                         if(xd<R.r.xparts-1) pa->maxx= pa->minx+xpart;
720                         else pa->maxx= xmaxb;
721                         if(yd<R.r.yparts-1) pa->maxy= pa->miny+ypart;
722                         else pa->maxy= ymaxb;
723
724                         pa->x= pa->maxx-pa->minx;
725                         pa->y= pa->maxy-pa->miny;
726                 }
727                 
728                 if(pa->x>0 && pa->y>0) {
729                         /* Non-box filters might need 1 pixel extra to work */
730                         if((R.r.filtertype)) {
731                                 pa->minx-= 1;
732                                 pa->miny-= 1;
733                                 pa->maxx+= 1;
734                                 pa->maxy+= 1;
735                                 pa->x+= 2;
736                                 pa->y+= 2;
737                         }
738                         BLI_addtail(&R.parts, pa);
739                 }
740                 else MEM_freeN(pa);
741         }
742         
743 }
744
745 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
746 static void setpart(Part *pa)
747 {
748
749         R.xstart= pa->minx-R.afmx;
750         R.ystart= pa->miny-R.afmy;
751         R.xend= pa->maxx-R.afmx;
752         R.yend= pa->maxy-R.afmy;
753         R.rectx= pa->x;
754         R.recty= pa->y;
755 }
756
757 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
758 static void addparttorect(Part *pa)
759 {
760         float *rf, *rfp;
761         unsigned int *rt, *rtp, *rz, *rzp;
762         int y, height, len, copylen;
763
764         /* calc the right offset in rects, zbuffer cannot exist... */
765         if(pa->rect==NULL) return;
766         
767         rtp= pa->rect;
768         rzp= pa->rectz;
769         rfp= pa->rectf;
770         
771         copylen=len= pa->x;
772         height= pa->y;
773         
774         if(R.r.filtertype) {    /* filters added 1 pixel extra */
775                 
776                 rtp+= 1+len;
777                 if(rzp) rzp+= 1+len;
778                 if(rfp) rfp+= 4*(1+len);
779                 
780                 copylen= len-2;
781                 height -= 2;
782                 rt= R.rectot+ (pa->miny + 1)*R.rectx+ (pa->minx+1);
783                 rz= R.rectz+ (pa->miny + 1)*R.rectx+ (pa->minx+1);
784                 rf= R.rectftot+ 4*( (pa->miny + 1)*R.rectx + (pa->minx+1) );
785         }
786         else {
787                 rt= R.rectot+ pa->miny*R.rectx+ pa->minx;
788                 rz= R.rectz+ pa->miny*R.rectx+ pa->minx;
789                 rf= R.rectftot+ 4*(pa->miny*R.rectx+ pa->minx);
790         }
791
792         for(y=0; y<height; y++) {
793                 memcpy(rt, rtp, 4*copylen);
794                 rt+= R.rectx;
795                 rtp+= len;
796                 
797                 if(rzp) {
798                         memcpy(rz, rzp, 4*copylen);
799                         rz+= R.rectx;
800                         rzp+= len;
801                 }
802                 if(rfp) {
803                         memcpy(rf, rfp, 16*copylen);
804                         rf+= 4*R.rectx;
805                         rfp+= 4*len;
806                 }
807         }
808 }
809
810
811 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
812
813  
814 static void add_to_blurbuf(int blur)
815 {
816         static unsigned int *blurrect= 0;
817         int tot, gamval;
818         short facr, facb;
819         char *rtr, *rtb;
820
821         if(blur<0) {
822                 if(blurrect) {
823                         if(R.rectot) MEM_freeN(R.rectot);
824                         R.rectot= blurrect;
825                         blurrect= 0;
826                 }
827         }
828         else if(blur==R.osa-1) {
829                 /* first time */
830                 blurrect= MEM_mallocN(R.rectx*R.recty*sizeof(int), "rectblur");
831                 if(R.rectot) memcpy(blurrect, R.rectot, R.rectx*R.recty*4);
832         }
833         else if(blurrect) {
834                 /* accumulate */
835
836                 facr= 256/(R.osa-blur);
837                 facb= 256-facr;
838
839                 if(R.rectot) {
840                         rtr= (char *)R.rectot;
841                         rtb= (char *)blurrect;
842                         tot= R.rectx*R.recty;
843                         while(tot--) {
844                                 if( *((unsigned int *)rtb) != *((unsigned int *)rtr) ) {
845
846                                         if(R.r.mode & R_GAMMA) {
847                                                 gamval= (facr* igamtab2[ rtr[0]<<8 ] + facb* igamtab2[ rtb[0]<<8 ])>>8;
848                                                 rtb[0]= gamtab[ gamval ]>>8;
849                                                 gamval= (facr* igamtab2[ rtr[1]<<8 ] + facb* igamtab2[ rtb[1]<<8 ])>>8;
850                                                 rtb[1]= gamtab[ gamval ]>>8;
851                                                 gamval= (facr* igamtab2[ rtr[2]<<8 ] + facb* igamtab2[ rtb[2]<<8 ])>>8;
852                                                 rtb[2]= gamtab[ gamval ]>>8;
853                                                 gamval= (facr* igamtab2[ rtr[3]<<8 ] + facb* igamtab2[ rtb[3]<<8 ])>>8;
854                                                 rtb[3]= gamtab[ gamval ]>>8;
855                                         }
856                                         else {
857                                                 rtb[0]= (facr*rtr[0] + facb*rtb[0])>>8;
858                                                 rtb[1]= (facr*rtr[1] + facb*rtb[1])>>8;
859                                                 rtb[2]= (facr*rtr[2] + facb*rtb[2])>>8;
860                                                 rtb[3]= (facr*rtr[3] + facb*rtb[3])>>8;
861                                         }
862                                 }
863                                 rtr+= 4;
864                                 rtb+= 4;
865                         }
866                 }
867                 if(blur==0) {
868                         /* last time */
869                         if(R.rectot) MEM_freeN(R.rectot);
870                         R.rectot= blurrect;
871                         blurrect= 0;
872                 }
873         }
874 }
875
876
877 /* yafray: main yafray render/export call */
878 static void yafrayRender(void)
879 {
880         R.flag |= R_RENDERING;  /* !!! */
881
882         /* bug #3193: these params were not initialized, needed later for winmat calc. */
883         R.xstart = -R.afmx;
884         R.ystart = -R.afmy;
885         R.xend = R.xstart + R.rectx - 1;
886         R.yend = R.ystart + R.recty - 1;
887
888         /* all allocs moved here, out of export code */
889         /* display rgba buf */
890         if (R.rectot) MEM_freeN(R.rectot);
891         R.rectot = MEM_callocN(sizeof(int)*R.rectx*R.recty, "rectot");
892         /* zbuf */
893         if (R.rectz) MEM_freeN(R.rectz);
894         R.rectz = (unsigned int *)MEM_mallocN(sizeof(int)*R.rectx*R.recty, "rectz");
895         /* float rgba buf */
896         if (R.rectftot) MEM_freeN(R.rectftot);
897         if (R.r.mode & R_FBUF) R.rectftot= MEM_callocN(4*sizeof(float)*R.rectx*R.recty, "rectftot");
898
899         // switch must be done before prepareScene()
900         if (!R.r.YFexportxml)
901                 YAF_switchFile();
902         else
903                 YAF_switchPlugin();
904
905         RE_local_init_render_display();
906         RE_local_clear_render_display(R.win);
907         RE_local_timecursor((G.scene->r.cfra));
908
909         printf("Starting scene conversion.\n");
910         prepareScene();
911         printf("Scene conversion done.\n");
912
913         YAF_exportScene();
914         finalizeScene();
915
916         // show postpro effects if floatbuffer used (plugin only)
917         if (R.r.YFexportxml) {
918                 if ((R.r.mode & R_FBUF) && R.rectftot)
919                         RE_floatbuffer_to_output();
920         }
921 }
922
923
924 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
925
926 // exported to other files, belongs in include... later
927 SDL_mutex *render_abuf_lock=NULL, *load_ibuf_lock=NULL;
928
929
930 static void renderloop_setblending(void)
931 {
932         
933         /* this value should only be set here. do_gamma is for gammablended adding of subpixels */
934         do_gamma= 0;
935         if(R.r.mode & R_GAMMA) {
936                 if(R.r.alphamode==R_ALPHAKEY);  // alpha corrected gamma doesnt work for key alpha
937                 else if((R.r.mode & R_OSA)) do_gamma= 1;
938         }
939         
940         /* always call, it does gamma tables used by alphaunder, but call after R.osa and jit was set */
941         init_filt_mask();
942         
943         switch (R.r.alphamode) {
944                 case R_ALPHAKEY:        
945                         setSkyBlendingMode(RE_ALPHA_KEY);
946                         break;
947                 case R_ALPHAPREMUL:     
948                         setSkyBlendingMode(RE_ALPHA_PREMUL);
949                         break;
950                 default:
951                         setSkyBlendingMode(RE_ALPHA_SKY);               
952         }
953         
954         /* SHould use slider when the gamma button is pressed. */
955         if (do_gamma) {         
956                 makeGammaTables(2.0);
957         } else {
958                 makeGammaTables(1.0);
959         }
960         
961 }
962
963 static void mainRenderLoop(void)  /* here the PART and FIELD loops */
964 {
965         Part *pa;
966         int blur, fields, fi, totparts, nr;
967
968         /* create mutexes for threaded render */
969         render_abuf_lock = SDL_CreateMutex();
970         load_ibuf_lock = SDL_CreateMutex();
971         
972         if(R.rectz) MEM_freeN(R.rectz);
973         R.rectz = NULL;
974         if(R.rectftot) MEM_freeN(R.rectftot);
975         R.rectftot = NULL;
976         
977         /* FIELD LOOP */
978         totparts= R.r.xparts*R.r.yparts;
979         fields= 1;
980
981         if(R.r.mode & R_FIELDS) {
982                 fields= 2;
983                 R.rectf1= R.rectf2= NULL;       /* field rects */
984                 R.r.ysch/= 2;
985                 R.afmy/= 2;
986                 R.r.yasp*= 2;
987                 R.ycor= ( (float)R.r.yasp)/( (float)R.r.xasp);
988
989         }
990         
991         for(fi=0; fi<fields; fi++) {
992
993                 /* INIT */
994                 BLI_srandom( 2*(G.scene->r.cfra)+fi);
995                         
996                 R.flag|= R_RENDERING;
997                 if(fi==1) R.flag |= R_SEC_FIELD;
998         
999                 /* MOTIONBLUR loop */
1000                 if(R.r.mode & R_MBLUR) blur= R.osa;
1001                 else blur= 1;
1002                 while(blur--) {
1003
1004                         /* WINDOW */
1005                         R.rectx= R.r.xsch;
1006                         R.recty= R.r.ysch;
1007                         R.xstart= -R.afmx;
1008                         R.ystart= -R.afmy;
1009                         R.xend= R.xstart+R.rectx-1;
1010                         R.yend= R.ystart+R.recty-1;
1011
1012                         if(R.r.mode & R_MBLUR) set_mblur_offs(R.osa-blur);
1013
1014                         initparts(); /* always do, because of border and gauss */
1015                         if(R.parts.first==NULL) {
1016                                 G.afbreek=1;
1017                                 error("Image too small");
1018                                 break;
1019                         }
1020                         
1021                         setpart(R.parts.first);
1022
1023                         RE_local_init_render_display();
1024                         RE_local_clear_render_display(R.win);
1025                         RE_local_timecursor((G.scene->r.cfra));
1026
1027                         prepareScene();
1028
1029                         /* PARTS LOOP */
1030                         nr= 0;
1031                         for(pa= R.parts.first; pa; pa= pa->next, nr++) {
1032
1033                                 if(RE_local_test_break()) break;
1034
1035                                 setpart(pa);
1036
1037                                 if(R.r.mode & R_MBLUR) RE_setwindowclip(0, blur);
1038                                 else RE_setwindowclip(0,-1);
1039
1040                                 if(R.r.mode & R_PANORAMA) setPanoRot(nr);
1041
1042                                 /* HOMOGENIC COORDINATES AND ZBUF AND CLIP OPTIMISATION (per part) */
1043                                 /* There may be some interference with z-coordinate    */
1044                                 /* calculation here?                                   */
1045
1046                                 doClipping(RE_projectverto);
1047                                 if(RE_local_test_break()) break;
1048                                 
1049                                 /* rectot is for result and integer face indices */
1050                                 if(R.rectot) MEM_freeN(R.rectot);
1051                                 R.rectot= MEM_callocN(sizeof(int)*R.rectx*R.recty, "rectot");
1052                                 
1053                                 if(R.rectftot) MEM_freeN(R.rectftot);
1054                                 if(R.r.mode & R_FBUF) R.rectftot= MEM_callocN(4*sizeof(float)*R.rectx*R.recty, "rectftot");
1055                                 
1056                                 if(R.r.mode & R_MBLUR) {
1057                                         RE_local_printrenderinfo(0.0, R.osa - blur);
1058                                         if(G.background && blur<R.osa) printf("\n"); // newline for percentage print
1059                                 }
1060                                 else RE_local_printrenderinfo(0.0, -1);
1061
1062                                 if(R.r.mode & R_UNIFIED) {
1063                                         zBufShadeAdvanced();
1064                                 }
1065                                 else {
1066                                         if(R.rectz) MEM_freeN(R.rectz);
1067                                         R.rectz =  (unsigned int *)MEM_mallocN(sizeof(int)*R.rectx*R.recty, "rectz");
1068
1069                                         if(R.r.mode & R_OSA) zbufshadeDA();
1070                                         else zbufshade();
1071                                 }
1072                                 
1073                                 /* exception */
1074                                 if( (R.r.mode & R_BORDER) && (R.r.mode & R_MOVIECROP));
1075                                 else {
1076                                         /* HANDLE PART OR BORDER */
1077                                         if(totparts>1 || (R.r.mode & R_BORDER) || (R.r.filtertype)) {
1078
1079                                                 pa->rect= R.rectot;
1080                                                 R.rectot= NULL;
1081                                                 pa->rectf= R.rectftot;
1082                                                 R.rectftot= NULL;
1083                                                 pa->rectz= R.rectz;
1084                                                 R.rectz= NULL;
1085                                         }
1086                                 }
1087
1088                                 if(RE_local_test_break()) break;
1089                         }
1090
1091                         /* JOIN PARTS OR INSERT BORDER */
1092
1093                         /* exception: crop */
1094                         if( (R.r.mode & R_BORDER) && (R.r.mode & R_MOVIECROP)) ;
1095                         else {
1096                                 R.rectx= R.r.xsch;
1097                                 R.recty= R.r.ysch;
1098
1099                                 if(R.r.mode & R_PANORAMA) R.rectx*= R.r.xparts;
1100
1101                                 if(totparts>1 || (R.r.mode & R_BORDER) || (R.r.filtertype)) {
1102                                         int a;
1103                                         
1104                                         if(R.rectot) MEM_freeN(R.rectot);
1105                                         if(R.rectftot) MEM_freeN(R.rectftot);
1106                                         if(R.rectz) MEM_freeN(R.rectz);
1107                                         
1108                                         R.rectot= MEM_callocN(sizeof(int)*R.rectx*R.recty, "rectot");
1109                                         
1110                                         if(R.r.mode & R_UNIFIED) R.rectz= NULL;
1111                                         else R.rectz= MEM_callocN(sizeof(int)*R.rectx*R.recty, "rectz");
1112
1113                                         if(R.r.mode & R_FBUF) R.rectftot= MEM_callocN(4*sizeof(float)*R.rectx*R.recty, "rectftot");
1114                                         else R.rectftot= NULL;
1115                                         
1116                                         for(a=0, pa= R.parts.first; pa; pa= pa->next, a++) {
1117                                                 
1118                                                 if(R.r.mode & R_PANORAMA) {             // pano is fake parts...
1119                                                         pa->minx += a*R.r.xsch;
1120                                                         pa->maxx += a*R.r.xsch;
1121                                                 }
1122                                                 addparttorect(pa);
1123                                         }
1124                                 }
1125                         }
1126                         
1127                         freeparts();
1128                         
1129                         if( (R.flag & R_HALO)) {
1130                                 if(RE_local_test_break()==0) add_halo_flare();
1131                         }
1132
1133                         if( (R.r.mode & R_ZBLUR)) {
1134                                 if(RE_local_test_break()==0) add_zblur();
1135                         }
1136
1137                         if(R.r.mode & R_MBLUR) {
1138                                 add_to_blurbuf(blur);
1139                         }
1140
1141                         /* END (blur loop) */
1142                         finalizeScene();
1143
1144                         if(RE_local_test_break()) break;
1145                 }
1146
1147                 /* definite free */
1148                 add_to_blurbuf(-1);
1149
1150                 /* HANDLE FIELD */
1151                 if(R.r.mode & R_FIELDS) {
1152                         if(R.flag & R_SEC_FIELD) R.rectf2= R.rectot;
1153                         else R.rectf1= R.rectot;
1154                         R.rectot= NULL;
1155                 }
1156
1157                 if(RE_local_test_break()) break;
1158         }
1159
1160         /* JOIN FIELDS */
1161         if(R.r.mode & R_FIELDS) {
1162                 R.r.ysch*= 2;
1163                 R.afmy*= 2;
1164                 R.recty*= 2;
1165                 R.r.yasp/=2;
1166
1167                 if(R.rectot) MEM_freeN(R.rectot);       /* happens when a render has been stopped */
1168                 R.rectot=(unsigned int *)MEM_callocN(sizeof(int)*R.rectx*R.recty, "rectot");
1169                 
1170                 if(RE_local_test_break()==0) {
1171                         unsigned int *rt, *rt1, *rt2;
1172                         int len, a;
1173                         
1174                         rt= R.rectot;
1175
1176                         if(R.r.mode & R_ODDFIELD) {
1177                                 rt2= R.rectf1;
1178                                 rt1= R.rectf2;
1179                         }
1180                         else {
1181                                 rt1= R.rectf1;
1182                                 rt2= R.rectf2;
1183                         }
1184
1185                         len= 4*R.rectx;
1186
1187                         for(a=0; a<R.recty; a+=2) {
1188                                 memcpy(rt, rt1, len);
1189                                 rt+= R.rectx;
1190                                 rt1+= R.rectx;
1191                                 memcpy(rt, rt2, len);
1192                                 rt+= R.rectx;
1193                                 rt2+= R.rectx;
1194                         }
1195                 }
1196                 
1197                 if(R.rectf1) MEM_freeN(R.rectf1);
1198                 R.rectf1= NULL;
1199                 if(R.rectf2) MEM_freeN(R.rectf2);
1200                 R.rectf2= NULL;
1201                 /* fbuf and zbuf free, image size differs now */
1202                 if(R.rectftot) MEM_freeN(R.rectftot);
1203                 R.rectftot= NULL;
1204                 if(R.rectz) MEM_freeN(R.rectz);
1205                 R.rectz= NULL;
1206                 
1207         }
1208
1209         /* if border: still do skybuf */
1210         if(R.r.mode & R_BORDER) {
1211                 if( (R.r.mode & R_MOVIECROP)==0) {
1212                         if(R.r.bufflag & 1) {
1213                                 unsigned int *rt;
1214                                 int x, y;
1215                                 
1216                                 R.xstart= -R.afmx;
1217                                 R.ystart= -R.afmy;
1218                                 rt= R.rectot;
1219                                 for(y=0; y<R.recty; y++) {
1220                                         for(x=0; x<R.rectx; x++, rt++) {
1221                                                 if(*rt==0) fillBackgroundImageChar((char *)rt, x, y);
1222                                         }
1223                                 }
1224                         }
1225                 }
1226         }
1227
1228         set_mblur_offs(0);
1229
1230         /* mutexes free */
1231         SDL_DestroyMutex(load_ibuf_lock);
1232         SDL_DestroyMutex(render_abuf_lock);
1233         load_ibuf_lock= NULL;
1234         render_abuf_lock= NULL;
1235 }
1236
1237 void render() {
1238         /* yafray: render, see above */
1239         if (R.r.renderer==R_YAFRAY)
1240                 yafrayRender();
1241         else
1242                 mainRenderLoop();
1243 }
1244
1245
1246 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
1247
1248 void RE_initrender(struct View3D *ogl_render_view3d)
1249 {
1250         double start_time;
1251         Image *bima;
1252         char name[256];
1253         
1254         /* scene data to R */
1255         R.r= G.scene->r;
1256         R.r.postigamma= 1.0/R.r.postgamma;
1257         
1258         /* WINDOW size (sch='scherm' dutch for screen...) */
1259         R.r.xsch= (R.r.size*R.r.xsch)/100;
1260         R.r.ysch= (R.r.size*R.r.ysch)/100;
1261         
1262         R.afmx= R.r.xsch/2;
1263         R.afmy= R.r.ysch/2;
1264         
1265         /* to be sure: when a premature return (rectx can differ from xsch) */
1266         R.rectx= R.r.xsch;
1267         R.recty= R.r.ysch;
1268         
1269         /* IS RENDERING ALLOWED? */
1270         
1271         /* forbidden combination */
1272         if(R.r.mode & R_PANORAMA) {
1273                 if(R.r.mode & R_BORDER) {
1274                         error("No border supported for Panorama");
1275                         G.afbreek= 1;
1276                 }
1277                 if(R.r.yparts>1) {
1278                         error("No Y-Parts supported for Panorama");
1279                         G.afbreek= 1;
1280                 }
1281                 if(R.r.mode & R_ORTHO) {
1282                         error("No Ortho render possible for Panorama");
1283                         G.afbreek= 1;
1284                 }
1285         }
1286
1287         if(R.r.mode & R_BORDER) {
1288                 if(R.r.border.xmax <= R.r.border.xmin || 
1289                         R.r.border.ymax <= R.r.border.ymin) {
1290                         error("No border area selected.");
1291                         G.afbreek= 1;
1292                 }
1293         }
1294         
1295         if(R.r.xparts*R.r.yparts>=2 && (R.r.mode & R_MOVIECROP) && (R.r.mode & R_BORDER)) {
1296                 error("Combination of border, crop and parts not allowed");
1297                 G.afbreek= 1;
1298                 return;
1299         }
1300         
1301         if(R.r.xparts*R.r.yparts>64) {
1302                 error("No more than 64 parts supported");
1303                 G.afbreek= 1;
1304                 return;
1305         }
1306         
1307         if(R.r.yparts>1 && (R.r.mode & R_PANORAMA)) {
1308                 error("No Y-Parts supported for Panorama");
1309                 G.afbreek= 1;
1310                 return;
1311         }
1312         
1313         if(G.afbreek==1) return;
1314         
1315         /* TEST BACKBUF */
1316         /* If an image is specified for use as backdrop, that image is loaded    */
1317         /* here.                                                                 */
1318         if((R.r.bufflag & 1) && (G.scene->r.scemode & R_OGL)==0) {
1319                 if(R.r.alphamode == R_ADDSKY) {
1320                         strcpy(name, R.r.backbuf);
1321                         BLI_convertstringcode(name, G.sce, G.scene->r.cfra);
1322                         
1323                         if(R.backbuf) {
1324                                 R.backbuf->id.us--;
1325                                 bima= R.backbuf;
1326                         }
1327                         else bima= NULL;
1328                         
1329                         R.backbuf= add_image(name);
1330                         
1331                         if(bima && bima->id.us<1) {
1332                                 free_image_buffers(bima);
1333                         }
1334
1335                         if(R.backbuf && R.backbuf->ibuf==NULL) {
1336                                 R.backbuf->ibuf= IMB_loadiffname(R.backbuf->name, IB_rect);
1337                                 if(R.backbuf->ibuf==NULL) R.backbuf->ok= 0;
1338                                 else R.backbuf->ok= 1;
1339                         }
1340                         if(R.backbuf==NULL || R.backbuf->ok==0) {
1341                                 // error() doesnt work with render window open
1342                                 //error("No backbuf there!");
1343                                 printf("Error: No backbuf %s\n", name);
1344                         }
1345                 }
1346         }
1347         
1348         if(R.r.mode & (R_OSA|R_MBLUR)) {
1349                 R.osa= R.r.osa;
1350                 if(R.osa>16) R.osa= 16;
1351                 
1352                 init_render_jit(R.osa);
1353                 
1354         }
1355         else R.osa= 0;
1356         
1357         /* just prevents cpu cycles for larger render and copying */
1358         if((R.r.mode & R_OSA)==0)
1359                 R.r.filtertype= 0;
1360         
1361         renderloop_setblending();       // alpha, sky, gamma
1362         
1363         /* when rendered without camera object */
1364         /* it has to done here because of envmaps */
1365         R.near= 0.1;
1366         R.far= 1000.0;
1367         
1368         
1369         if(R.afmx<1 || R.afmy<1) {
1370                 error("Image too small");
1371                 return;
1372         }
1373         R.ycor= ( (float)R.r.yasp)/( (float)R.r.xasp);
1374         
1375         start_time= PIL_check_seconds_timer();
1376         
1377         if(R.r.scemode & R_OGL) {
1378                 R.rectx= R.r.xsch;
1379                 R.recty= R.r.ysch;
1380                 
1381                 if(R.rectot) MEM_freeN(R.rectot);
1382                 R.rectot= (unsigned int *)MEM_callocN(sizeof(int)*R.rectx*R.recty, "rectot");
1383                 
1384                 if(R.rectftot) MEM_freeN(R.rectftot);
1385                 R.rectftot= NULL;
1386                 
1387                 RE_local_init_render_display();
1388                 drawview3d_render(ogl_render_view3d);
1389         }
1390         else if(R.r.scemode & R_DOSEQ) {
1391                 R.rectx= R.r.xsch;
1392                 R.recty= R.r.ysch;
1393                 if(R.r.mode & R_PANORAMA) {
1394                         R.rectx*= R.r.xparts;
1395                 }
1396                 
1397                 if(R.rectot) MEM_freeN(R.rectot);
1398                 R.rectot= (unsigned int *)MEM_callocN(sizeof(int)*R.rectx*R.recty, "rectot");
1399                 
1400                 if(R.rectftot) MEM_freeN(R.rectftot);
1401                 R.rectftot= NULL;
1402                 
1403                 RE_local_timecursor((G.scene->r.cfra));
1404                 
1405                 if(RE_local_test_break()==0) do_render_seq();
1406                 
1407                 /* display */
1408                 if(R.rectot) RE_local_render_display(0, R.recty-1, R.rectx, R.recty,R.rectot);
1409         }
1410         else {
1411                 if(G.scene->camera==0) {
1412                         G.scene->camera= scene_find_camera(G.scene);
1413                 }
1414                 
1415                 if(G.scene->camera==0) {
1416                         error("No camera");
1417                         /* needed because R.rectx and R.recty can be unmatching R.rectot */
1418                         
1419                         if(R.rectot) MEM_freeN(R.rectot);
1420                         R.rectot= NULL;
1421                         if(R.rectftot) MEM_freeN(R.rectftot);
1422                         R.rectftot= NULL;
1423                         
1424                         G.afbreek=1;
1425                         return;
1426                 }
1427                 else {
1428                         
1429                         if(G.scene->camera->type==OB_CAMERA) {
1430                                 Camera *cam= G.scene->camera->data;
1431                                 if(cam->type==CAM_ORTHO) R.r.mode |= R_ORTHO;
1432                         }
1433                         
1434                         render(); /* returns with complete rect xsch-ysch */
1435                 }
1436         }
1437         
1438         /* display again: fields/seq/parts/pano etc */
1439         if(R.rectot) {
1440                 RE_local_init_render_display();
1441                 RE_local_render_display(0, R.recty-1, R.rectx, R.recty, R.rectot);
1442         }
1443         else RE_local_clear_render_display(R.win);
1444         
1445         if ((G.scene->r.scemode & R_OGL)==0) /* header gets scrabled if renderwindow holds OGL context */       
1446                 RE_local_printrenderinfo((PIL_check_seconds_timer() - start_time), -1);
1447         
1448         /* grms... this is a nasty global */
1449         do_gamma= 0;
1450         
1451         /* these flags remain on, until reset in caller to render (renderwin.c) */
1452         R.flag &= (R_RENDERING|R_ANIMRENDER|R_REDRAW_PRV);
1453 }
1454
1455 void RE_animrender(struct View3D *ogl_render_view3d)
1456 {
1457         int cfrao;
1458         char name[256];
1459
1460         if(G.scene==NULL) return;
1461         if(G.scene->r.sfra > G.scene->r.efra) {
1462                 error("Startframe larger than Endframe");
1463                 return;
1464         }
1465         
1466         /* scenedata to R: (for backbuf, R.rectx etc) */
1467         R.r= G.scene->r;
1468
1469         /* START ANIMLOOP, everywhere NOT the cfra from R.r is gebruikt: because of rest blender */
1470         cfrao= (G.scene->r.cfra);
1471
1472         /* disable options for ogl render */
1473         if(G.scene->r.scemode & R_OGL) R.r.mode &= ~(R_PANORAMA|R_MOVIECROP);
1474         
1475         // these calculations apply for all movie formats
1476         R.rectx= (R.r.size*R.r.xsch)/100;
1477         R.recty= (R.r.size*R.r.ysch)/100;
1478         if(R.r.mode & R_PANORAMA) {
1479                 R.rectx*= R.r.xparts;
1480                 R.recty*= R.r.yparts;
1481         }
1482         if(R.r.mode & R_MOVIECROP) {
1483                 initparts();
1484                 setpart(R.parts.first);         // this will adjust r.rectx
1485         }
1486
1487         if (0) {
1488 #ifdef __sgi
1489         } else if (R.r.imtype==R_MOVIE) {
1490                 start_movie();
1491 #endif
1492 #if defined(_WIN32) && !defined(FREE_WINDOWS)
1493         } else if (R.r.imtype == R_AVICODEC) {
1494                 start_avi_codec();
1495 #endif
1496 #if WITH_QUICKTIME
1497         } else if (R.r.imtype == R_QUICKTIME) {
1498                 start_qt();
1499 #endif
1500         } else if ELEM4(R.r.imtype, R_AVIRAW, R_AVIJPEG, R_MOVIE, R_AVICODEC) {
1501                 if ELEM(R.r.imtype, R_MOVIE, R_AVICODEC) {
1502                         printf("Selected movie format not supported on this platform,\nusing RAW AVI instead\n");
1503                 }
1504                 start_avi();
1505         }
1506 // set initial conditions for softbodies here
1507 // ******************************************
1508         for((G.scene->r.cfra)=(G.scene->r.sfra); (G.scene->r.cfra)<=(G.scene->r.efra); (G.scene->r.cfra)++) {
1509                 double starttime= PIL_check_seconds_timer();
1510
1511                 R.flag |= R_ANIMRENDER; // unused now (ton)
1512
1513                 RE_initrender(ogl_render_view3d);
1514                 
1515                 /* WRITE IMAGE */
1516                 if(RE_local_test_break()==0) {
1517
1518                         if (0) {
1519 #ifdef __sgi
1520                         } else if (R.r.imtype == R_MOVIE) {
1521                                 append_movie((G.scene->r.cfra));
1522 #endif
1523 #if defined(_WIN32) && !defined(FREE_WINDOWS)
1524                         } else if (R.r.imtype == R_AVICODEC) {
1525                                 append_avi_codec((G.scene->r.cfra));
1526 #endif
1527 #ifdef WITH_QUICKTIME
1528                         } else if (R.r.imtype == R_QUICKTIME) {
1529                                 append_qt((G.scene->r.cfra));
1530 #endif
1531                         } else if ELEM4(R.r.imtype, R_AVIRAW, R_AVIJPEG, R_MOVIE, R_AVICODEC) {
1532                                 append_avi((G.scene->r.cfra));
1533                         } else {
1534                                 makepicstring(name, (G.scene->r.cfra));
1535                                 schrijfplaatje(name);
1536                                 if(RE_local_test_break()==0) printf("Saved: %s", name);
1537                         }
1538
1539                         timestr(PIL_check_seconds_timer()-starttime, name);
1540                         printf(" Time: %s\n", name);
1541                         fflush(stdout); /* needed for renderd !! */
1542                 }
1543
1544                 if(G.afbreek==1) break;
1545
1546         }
1547
1548         G.scene->r.cfra= cfrao;
1549
1550         /* restore time */
1551         if(R.r.mode & (R_FIELDS|R_MBLUR)) {
1552                 /* applies changes fully */
1553                 scene_update_for_newframe(G.scene, G.scene->lay);
1554         }
1555
1556         if (0) {
1557 #ifdef __sgi    
1558         } else if (R.r.imtype==R_MOVIE) {
1559                 end_movie();
1560 #endif
1561 #if defined(_WIN32) && !defined(FREE_WINDOWS)
1562         } else if (R.r.imtype == R_AVICODEC) {
1563                 end_avi_codec();
1564 #endif
1565 #ifdef WITH_QUICKTIME
1566         } else if (R.r.imtype == R_QUICKTIME) {
1567                 end_qt();
1568 #endif
1569         } else if ELEM4(R.r.imtype, R_AVIRAW, R_AVIJPEG, R_MOVIE, R_AVICODEC) {
1570                 end_avi();
1571         }
1572 }
1573
1574 /* *************************************************** */
1575 /* ******************* Screendumps ******************** */
1576 /* moved to the windowControl thing */