New feature: "Stress" texture input channel
[blender-staging.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 /* this is called in creator.c, on startup */
483 void RE_init_render_data(void)
484 {
485         memset(&R, 0, sizeof(RE_Render));
486         
487         RE_init_vertex_tables();
488         R.blovl= (VlakRen **)MEM_callocN(sizeof(void *)*(TABLEINITSIZE),"Blovl");
489         R.bloha= (HaloRen **)MEM_callocN(sizeof(void *)*(TABLEINITSIZE),"Bloha");
490         
491         init_def_material();
492         init_filt_mask();
493 }
494
495 /* called in usiblender.c on exit, also for blender -b render */
496 void RE_free_render_data()
497 {
498         MEM_freeN(R.vertnodes);
499         R.vertnodes= NULL;
500         MEM_freeN(R.blovl);
501         R.blovl= NULL;
502         MEM_freeN(R.bloha);
503         R.bloha= NULL;
504
505         if(R.rectot) MEM_freeN(R.rectot);
506         if(R.rectftot) MEM_freeN(R.rectftot);
507         if(R.rectz) MEM_freeN(R.rectz);
508         if(R.rectspare) MEM_freeN(R.rectspare);
509         R.rectot= NULL;
510         R.rectftot= NULL;
511         R.rectz= NULL;
512         R.rectspare= NULL;
513         
514         free_filt_mask();
515 }
516
517 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
518
519 void RE_make_existing_file(char *name)
520 {
521         char di[FILE_MAXDIR], fi[FILE_MAXFILE];
522
523         strcpy(di, name);
524         BLI_splitdirstring(di, fi);
525
526         /* test exist */
527         if (BLI_exists(di) == 0) {
528                 BLI_recurdir_fileops(di);
529         }
530 }
531
532
533 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
534
535 void RE_setwindowclip(int mode, int jmode)
536 {
537         extern float bluroffsx, bluroffsy;      // rendercore.c... hackish (ton)
538         Camera *cam=NULL;
539         float lens, minx, miny, maxx, maxy;
540         float xd, yd, afmx, afmy;
541
542         if(G.scene->camera==NULL) return;
543
544         afmx= R.afmx;
545         afmy= R.afmy;
546
547         if(mode) {
548
549                 if(G.scene->camera->type==OB_LAMP) {
550                         /* fac= cos( PI*((float)(256- la->spsi))/512.0 ); */
551
552                         /* phi= acos(fac); */
553                         /* lens= 16.0*fac/sin(phi); */
554                         lens= 35.0;
555                         R.near= 0.1;
556                         R.far= 1000.0;
557                 }
558                 else if(G.scene->camera->type==OB_CAMERA) {
559                         cam= G.scene->camera->data;
560
561                         lens= cam->lens;
562                         R.near= cam->clipsta;
563                         R.far= cam->clipend;
564                 }
565                 else {
566                         lens= 16.0;
567                 }
568
569                 if(R.r.mode & R_ORTHO) {
570                         if( (R.r.xasp*afmx) >= (R.r.yasp*afmy) ) {
571                                 R.viewfac= 2.0*afmx;
572                         }
573                         else {
574                                 R.viewfac= 2.0*R.ycor*afmy;
575                         }
576                         /* ortho_scale == 1.0 means exact 1 to 1 mapping */
577                         R.pixsize= cam->ortho_scale/R.viewfac;
578                 }
579                 else {
580                         if( (R.r.xasp*afmx) >= (R.r.yasp*afmy) ) {
581                                 R.viewfac= (afmx*lens)/16.0;
582                         }
583                         else {
584                                 R.viewfac= R.ycor*(afmy*lens)/16.0;
585                         }
586                         
587                         R.pixsize= R.near/R.viewfac;
588                 }
589                 
590                 /* pixsize is not a real global... get rid of it! (ton) */
591         }
592
593         /* revision / simplification of subpixel offsets:
594            - the matrix will go without offset from start (e.g. -100) to end (e.g. +99).
595            - filling in with zbuffer will set offset of 0.5. to make sure clipped faces fill in too
596            - in shadepixel() again that 0.5 offset is corrected
597         */
598         minx= R.xstart; 
599         miny= R.ycor*(R.ystart); 
600         maxx= R.xend; 
601         maxy= R.ycor*(R.yend); 
602            
603         if(R.flag & R_SEC_FIELD) {
604                 if(R.r.mode & R_ODDFIELD) {
605                         miny-= .5*R.ycor;
606                         maxy-= .5*R.ycor;
607                 }
608                 else {
609                         miny+= .5*R.ycor;
610                         maxy+= .5*R.ycor;
611                 }
612         }
613
614         xd= yd= 0.0;
615         if(jmode!= -1) {
616                 bluroffsx= xd= jit[jmode % R.osa][0];
617                 bluroffsy= yd= R.ycor*jit[jmode % R.osa][1];
618         }
619         else bluroffsx=bluroffsy= 0.0;
620
621         minx= R.pixsize*(minx+xd);
622         maxx= R.pixsize*(maxx+xd);
623         miny= R.pixsize*(miny+yd);
624         maxy= R.pixsize*(maxy+yd);
625
626         if(R.r.mode & R_ORTHO)
627                 i_ortho(minx, maxx, miny, maxy, R.near, R.far, R.winmat);
628         else 
629                 i_window(minx, maxx, miny, maxy, R.near, R.far, R.winmat);
630
631         //printmatrix4("win", R.winmat);
632 }
633
634
635 /* ~~~~~~~~~~~~~~~~ PARTS ~~~~~~~~~~~~~~~~~~~~~~ */
636
637 /** 
638 * Part as in part-rendering. An image rendered in parts is rendered
639 * to a list of parts, with x,y size, and a pointer to the render
640 * output stored per part. Internal!
641 */
642 typedef struct Part
643 {
644         struct Part *next, *prev;
645         unsigned int *rect;             // color 4x8 bits
646         float *rectf;                   // color 4x32 bits
647         unsigned int *rectz;    // zbuffer
648
649         short minx, miny, maxx, maxy, x, y;
650 } Part;
651
652 static void freeparts(void)
653 {
654         Part *part= R.parts.first;
655         while(part) {
656                 if(part->rect) MEM_freeN(part->rect);
657                 if(part->rectz) MEM_freeN(part->rectz);
658                 if(part->rectf) MEM_freeN(part->rectf);
659                 part= part->next;
660         }
661         BLI_freelistN(&R.parts);
662 }
663
664 static void initparts(void)
665 {
666         Part *pa;
667         short nr, xd, yd, xpart, ypart, xparts, yparts;
668         short a, xminb, xmaxb, yminb, ymaxb;
669
670         freeparts();
671         
672         if(R.r.mode & R_BORDER) {
673                 xminb= R.r.border.xmin*R.rectx;
674                 xmaxb= R.r.border.xmax*R.rectx;
675
676                 yminb= R.r.border.ymin*R.recty;
677                 ymaxb= R.r.border.ymax*R.recty;
678
679                 if(xminb<0) xminb= 0;
680                 if(xmaxb>R.rectx) xmaxb= R.rectx;
681                 if(yminb<0) yminb= 0;
682                 if(ymaxb>R.recty) ymaxb= R.recty;
683         }
684         else {
685                 xminb=yminb= 0;
686                 xmaxb= R.rectx;
687                 ymaxb= R.recty;
688         }
689
690         xparts= R.r.xparts;     /* for border */
691         yparts= R.r.yparts;
692
693         xpart= R.rectx/xparts;
694         ypart= R.recty/yparts;
695
696         /* if border: test if amount of parts can be fewer */
697         if(R.r.mode & R_BORDER) {
698                 a= (xmaxb-xminb-1)/xpart+1; /* amount of parts in border */
699                 if(a<xparts) xparts= a;
700                 a= (ymaxb-yminb-1)/ypart+1; /* amount of parts in border */
701                 if(a<yparts) yparts= a;
702
703                 xpart= (xmaxb-xminb)/xparts;
704                 ypart= (ymaxb-yminb)/yparts;
705         }
706         
707         for(nr=0; nr<xparts*yparts; nr++) {
708                 pa= MEM_callocN(sizeof(Part), "new part");
709                 
710                 if(R.r.mode & R_PANORAMA) {
711                         pa->minx= pa->miny= 0;
712                         pa->maxx= pa->x= R.rectx;
713                         pa->maxy= pa->y= R.recty;
714                 }
715                 else {
716                         xd= (nr % xparts);
717                         yd= (nr-xd)/xparts;
718
719                         pa->minx= xminb+ xd*xpart;
720                         pa->miny= yminb+ yd*ypart;
721                         if(xd<R.r.xparts-1) pa->maxx= pa->minx+xpart;
722                         else pa->maxx= xmaxb;
723                         if(yd<R.r.yparts-1) pa->maxy= pa->miny+ypart;
724                         else pa->maxy= ymaxb;
725
726                         pa->x= pa->maxx-pa->minx;
727                         pa->y= pa->maxy-pa->miny;
728                 }
729                 
730                 if(pa->x>0 && pa->y>0) {
731                         /* Non-box filters might need 1 pixel extra to work */
732                         if((R.r.filtertype)) {
733                                 pa->minx-= 1;
734                                 pa->miny-= 1;
735                                 pa->maxx+= 1;
736                                 pa->maxy+= 1;
737                                 pa->x+= 2;
738                                 pa->y+= 2;
739                         }
740                         BLI_addtail(&R.parts, pa);
741                 }
742                 else MEM_freeN(pa);
743         }
744         
745 }
746
747 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
748 static void setpart(Part *pa)
749 {
750
751         R.xstart= pa->minx-R.afmx;
752         R.ystart= pa->miny-R.afmy;
753         R.xend= pa->maxx-R.afmx;
754         R.yend= pa->maxy-R.afmy;
755         R.rectx= pa->x;
756         R.recty= pa->y;
757 }
758
759 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
760 static void addparttorect(Part *pa)
761 {
762         float *rf, *rfp;
763         unsigned int *rt, *rtp, *rz, *rzp;
764         int y, height, len, copylen;
765
766         /* calc the right offset in rects, zbuffer cannot exist... */
767         if(pa->rect==NULL) return;
768         
769         rtp= pa->rect;
770         rzp= pa->rectz;
771         rfp= pa->rectf;
772         
773         copylen=len= pa->x;
774         height= pa->y;
775         
776         if(R.r.filtertype) {    /* filters added 1 pixel extra */
777                 
778                 rtp+= 1+len;
779                 if(rzp) rzp+= 1+len;
780                 if(rfp) rfp+= 4*(1+len);
781                 
782                 copylen= len-2;
783                 height -= 2;
784                 rt= R.rectot+ (pa->miny + 1)*R.rectx+ (pa->minx+1);
785                 rz= R.rectz+ (pa->miny + 1)*R.rectx+ (pa->minx+1);
786                 rf= R.rectftot+ 4*( (pa->miny + 1)*R.rectx + (pa->minx+1) );
787         }
788         else {
789                 rt= R.rectot+ pa->miny*R.rectx+ pa->minx;
790                 rz= R.rectz+ pa->miny*R.rectx+ pa->minx;
791                 rf= R.rectftot+ 4*(pa->miny*R.rectx+ pa->minx);
792         }
793
794         for(y=0; y<height; y++) {
795                 memcpy(rt, rtp, 4*copylen);
796                 rt+= R.rectx;
797                 rtp+= len;
798                 
799                 if(rzp) {
800                         memcpy(rz, rzp, 4*copylen);
801                         rz+= R.rectx;
802                         rzp+= len;
803                 }
804                 if(rfp) {
805                         memcpy(rf, rfp, 16*copylen);
806                         rf+= 4*R.rectx;
807                         rfp+= 4*len;
808                 }
809         }
810 }
811
812
813 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
814
815  
816 static void add_to_blurbuf(int blur)
817 {
818         static unsigned int *blurrect= 0;
819         int tot, gamval;
820         short facr, facb;
821         char *rtr, *rtb;
822
823         if(blur<0) {
824                 if(blurrect) {
825                         if(R.rectot) MEM_freeN(R.rectot);
826                         R.rectot= blurrect;
827                         blurrect= 0;
828                 }
829         }
830         else if(blur==R.osa-1) {
831                 /* first time */
832                 blurrect= MEM_mallocN(R.rectx*R.recty*sizeof(int), "rectblur");
833                 if(R.rectot) memcpy(blurrect, R.rectot, R.rectx*R.recty*4);
834         }
835         else if(blurrect) {
836                 /* accumulate */
837
838                 facr= 256/(R.osa-blur);
839                 facb= 256-facr;
840
841                 if(R.rectot) {
842                         rtr= (char *)R.rectot;
843                         rtb= (char *)blurrect;
844                         tot= R.rectx*R.recty;
845                         while(tot--) {
846                                 if( *((unsigned int *)rtb) != *((unsigned int *)rtr) ) {
847
848                                         if(R.r.mode & R_GAMMA) {
849                                                 gamval= (facr* igamtab2[ rtr[0]<<8 ] + facb* igamtab2[ rtb[0]<<8 ])>>8;
850                                                 rtb[0]= gamtab[ gamval ]>>8;
851                                                 gamval= (facr* igamtab2[ rtr[1]<<8 ] + facb* igamtab2[ rtb[1]<<8 ])>>8;
852                                                 rtb[1]= gamtab[ gamval ]>>8;
853                                                 gamval= (facr* igamtab2[ rtr[2]<<8 ] + facb* igamtab2[ rtb[2]<<8 ])>>8;
854                                                 rtb[2]= gamtab[ gamval ]>>8;
855                                                 gamval= (facr* igamtab2[ rtr[3]<<8 ] + facb* igamtab2[ rtb[3]<<8 ])>>8;
856                                                 rtb[3]= gamtab[ gamval ]>>8;
857                                         }
858                                         else {
859                                                 rtb[0]= (facr*rtr[0] + facb*rtb[0])>>8;
860                                                 rtb[1]= (facr*rtr[1] + facb*rtb[1])>>8;
861                                                 rtb[2]= (facr*rtr[2] + facb*rtb[2])>>8;
862                                                 rtb[3]= (facr*rtr[3] + facb*rtb[3])>>8;
863                                         }
864                                 }
865                                 rtr+= 4;
866                                 rtb+= 4;
867                         }
868                 }
869                 if(blur==0) {
870                         /* last time */
871                         if(R.rectot) MEM_freeN(R.rectot);
872                         R.rectot= blurrect;
873                         blurrect= 0;
874                 }
875         }
876 }
877
878
879 /* yafray: main yafray render/export call */
880 static void yafrayRender(void)
881 {
882         R.flag |= R_RENDERING;  /* !!! */
883
884         /* bug #3193: these params were not initialized, needed later for winmat calc. */
885         R.xstart = -R.afmx;
886         R.ystart = -R.afmy;
887         R.xend = R.xstart + R.rectx - 1;
888         R.yend = R.ystart + R.recty - 1;
889
890         /* all allocs moved here, out of export code */
891         /* display rgba buf */
892         if (R.rectot) MEM_freeN(R.rectot);
893         R.rectot = MEM_callocN(sizeof(int)*R.rectx*R.recty, "rectot");
894         /* zbuf */
895         if (R.rectz) MEM_freeN(R.rectz);
896         R.rectz = (unsigned int *)MEM_mallocN(sizeof(int)*R.rectx*R.recty, "rectz");
897         /* float rgba buf */
898         if (R.rectftot) MEM_freeN(R.rectftot);
899         if (R.r.mode & R_FBUF) R.rectftot= MEM_callocN(4*sizeof(float)*R.rectx*R.recty, "rectftot");
900
901         // switch must be done before prepareScene()
902         if (!R.r.YFexportxml)
903                 YAF_switchFile();
904         else
905                 YAF_switchPlugin();
906
907         RE_local_init_render_display();
908         RE_local_clear_render_display(R.win);
909         RE_local_timecursor((G.scene->r.cfra));
910
911         printf("Starting scene conversion.\n");
912         prepareScene();
913         printf("Scene conversion done.\n");
914
915         YAF_exportScene();
916         finalizeScene();
917
918         // show postpro effects if floatbuffer used (plugin only)
919         if (R.r.YFexportxml) {
920                 if ((R.r.mode & R_FBUF) && R.rectftot)
921                         RE_floatbuffer_to_output();
922         }
923 }
924
925
926 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
927
928 // exported to other files, belongs in include... later
929 SDL_mutex *render_abuf_lock=NULL, *load_ibuf_lock=NULL;
930
931
932 static void renderloop_setblending(void)
933 {
934         
935         /* this value should only be set here. do_gamma is for gammablended adding of subpixels */
936         do_gamma= 0;
937         if(R.r.mode & R_GAMMA) {
938                 if(R.r.alphamode==R_ALPHAKEY);  // alpha corrected gamma doesnt work for key alpha
939                 else if((R.r.mode & R_OSA)) do_gamma= 1;
940         }
941         
942         /* always call, it does gamma tables used by alphaunder, but call after R.osa and jit was set */
943         init_filt_mask();
944         
945         switch (R.r.alphamode) {
946                 case R_ALPHAKEY:        
947                         setSkyBlendingMode(RE_ALPHA_KEY);
948                         break;
949                 case R_ALPHAPREMUL:     
950                         setSkyBlendingMode(RE_ALPHA_PREMUL);
951                         break;
952                 default:
953                         setSkyBlendingMode(RE_ALPHA_SKY);               
954         }
955         
956         /* SHould use slider when the gamma button is pressed. */
957         if (do_gamma) {         
958                 makeGammaTables(2.0);
959         } else {
960                 makeGammaTables(1.0);
961         }
962         
963 }
964
965 static void mainRenderLoop(void)  /* here the PART and FIELD loops */
966 {
967         Part *pa;
968         int blur, fields, fi, totparts, nr;
969
970         /* create mutexes for threaded render */
971         render_abuf_lock = SDL_CreateMutex();
972         load_ibuf_lock = SDL_CreateMutex();
973         
974         if(R.rectz) MEM_freeN(R.rectz);
975         R.rectz = NULL;
976         if(R.rectftot) MEM_freeN(R.rectftot);
977         R.rectftot = NULL;
978         
979         /* FIELD LOOP */
980         totparts= R.r.xparts*R.r.yparts;
981         fields= 1;
982
983         if(R.r.mode & R_FIELDS) {
984                 fields= 2;
985                 R.rectf1= R.rectf2= NULL;       /* field rects */
986                 R.r.ysch/= 2;
987                 R.afmy/= 2;
988                 R.r.yasp*= 2;
989                 R.ycor= ( (float)R.r.yasp)/( (float)R.r.xasp);
990
991         }
992         
993         for(fi=0; fi<fields; fi++) {
994
995                 /* INIT */
996                 BLI_srandom( 2*(G.scene->r.cfra)+fi);
997                         
998                 R.flag|= R_RENDERING;
999                 if(fi==1) R.flag |= R_SEC_FIELD;
1000         
1001                 /* MOTIONBLUR loop */
1002                 if(R.r.mode & R_MBLUR) blur= R.osa;
1003                 else blur= 1;
1004                 while(blur--) {
1005
1006                         /* WINDOW */
1007                         R.rectx= R.r.xsch;
1008                         R.recty= R.r.ysch;
1009                         R.xstart= -R.afmx;
1010                         R.ystart= -R.afmy;
1011                         R.xend= R.xstart+R.rectx-1;
1012                         R.yend= R.ystart+R.recty-1;
1013
1014                         if(R.r.mode & R_MBLUR) set_mblur_offs(R.osa-blur);
1015
1016                         initparts(); /* always do, because of border and gauss */
1017                         if(R.parts.first==NULL) {
1018                                 G.afbreek=1;
1019                                 error("Image too small");
1020                                 break;
1021                         }
1022                         
1023                         setpart(R.parts.first);
1024
1025                         RE_local_init_render_display();
1026                         RE_local_clear_render_display(R.win);
1027                         RE_local_timecursor((G.scene->r.cfra));
1028
1029                         prepareScene();
1030
1031                         /* PARTS LOOP */
1032                         nr= 0;
1033                         for(pa= R.parts.first; pa; pa= pa->next, nr++) {
1034
1035                                 if(RE_local_test_break()) break;
1036
1037                                 setpart(pa);
1038
1039                                 if(R.r.mode & R_MBLUR) RE_setwindowclip(0, blur);
1040                                 else RE_setwindowclip(0,-1);
1041
1042                                 if(R.r.mode & R_PANORAMA) setPanoRot(nr);
1043
1044                                 /* HOMOGENIC COORDINATES AND ZBUF AND CLIP OPTIMISATION (per part) */
1045                                 /* There may be some interference with z-coordinate    */
1046                                 /* calculation here?                                   */
1047
1048                                 doClipping(RE_projectverto);
1049                                 if(RE_local_test_break()) break;
1050                                 
1051                                 /* rectot is for result and integer face indices */
1052                                 if(R.rectot) MEM_freeN(R.rectot);
1053                                 R.rectot= MEM_callocN(sizeof(int)*R.rectx*R.recty, "rectot");
1054                                 
1055                                 if(R.rectftot) MEM_freeN(R.rectftot);
1056                                 if(R.r.mode & R_FBUF) R.rectftot= MEM_callocN(4*sizeof(float)*R.rectx*R.recty, "rectftot");
1057                                 
1058                                 if(R.r.mode & R_MBLUR) {
1059                                         RE_local_printrenderinfo(0.0, R.osa - blur);
1060                                         if(G.background && blur<R.osa) printf("\n"); // newline for percentage print
1061                                 }
1062                                 else RE_local_printrenderinfo(0.0, -1);
1063
1064                                 if(R.r.mode & R_UNIFIED) {
1065                                         zBufShadeAdvanced();
1066                                 }
1067                                 else {
1068                                         if(R.rectz) MEM_freeN(R.rectz);
1069                                         R.rectz =  (unsigned int *)MEM_mallocN(sizeof(int)*R.rectx*R.recty, "rectz");
1070
1071                                         if(R.r.mode & R_OSA) zbufshadeDA();
1072                                         else zbufshade();
1073                                 }
1074                                 
1075                                 /* exception */
1076                                 if( (R.r.mode & R_BORDER) && (R.r.mode & R_MOVIECROP));
1077                                 else {
1078                                         /* HANDLE PART OR BORDER */
1079                                         if(totparts>1 || (R.r.mode & R_BORDER) || (R.r.filtertype)) {
1080
1081                                                 pa->rect= R.rectot;
1082                                                 R.rectot= NULL;
1083                                                 pa->rectf= R.rectftot;
1084                                                 R.rectftot= NULL;
1085                                                 pa->rectz= R.rectz;
1086                                                 R.rectz= NULL;
1087                                         }
1088                                 }
1089
1090                                 if(RE_local_test_break()) break;
1091                         }
1092
1093                         /* JOIN PARTS OR INSERT BORDER */
1094
1095                         /* exception: crop */
1096                         if( (R.r.mode & R_BORDER) && (R.r.mode & R_MOVIECROP)) ;
1097                         else {
1098                                 R.rectx= R.r.xsch;
1099                                 R.recty= R.r.ysch;
1100
1101                                 if(R.r.mode & R_PANORAMA) R.rectx*= R.r.xparts;
1102
1103                                 if(totparts>1 || (R.r.mode & R_BORDER) || (R.r.filtertype)) {
1104                                         int a;
1105                                         
1106                                         if(R.rectot) MEM_freeN(R.rectot);
1107                                         if(R.rectftot) MEM_freeN(R.rectftot);
1108                                         if(R.rectz) MEM_freeN(R.rectz);
1109                                         
1110                                         R.rectot= MEM_callocN(sizeof(int)*R.rectx*R.recty, "rectot");
1111                                         
1112                                         if(R.r.mode & R_UNIFIED) R.rectz= NULL;
1113                                         else R.rectz= MEM_callocN(sizeof(int)*R.rectx*R.recty, "rectz");
1114
1115                                         if(R.r.mode & R_FBUF) R.rectftot= MEM_callocN(4*sizeof(float)*R.rectx*R.recty, "rectftot");
1116                                         else R.rectftot= NULL;
1117                                         
1118                                         for(a=0, pa= R.parts.first; pa; pa= pa->next, a++) {
1119                                                 
1120                                                 if(R.r.mode & R_PANORAMA) {             // pano is fake parts...
1121                                                         pa->minx += a*R.r.xsch;
1122                                                         pa->maxx += a*R.r.xsch;
1123                                                 }
1124                                                 addparttorect(pa);
1125                                         }
1126                                 }
1127                         }
1128                         
1129                         freeparts();
1130                         
1131                         if( (R.flag & R_HALO)) {
1132                                 if(RE_local_test_break()==0) add_halo_flare();
1133                         }
1134
1135                         if( (R.r.mode & R_ZBLUR)) {
1136                                 if(RE_local_test_break()==0) add_zblur();
1137                         }
1138
1139                         if(R.r.mode & R_MBLUR) {
1140                                 add_to_blurbuf(blur);
1141                         }
1142
1143                         /* END (blur loop) */
1144                         finalizeScene();
1145
1146                         if(RE_local_test_break()) break;
1147                 }
1148
1149                 /* definite free */
1150                 add_to_blurbuf(-1);
1151
1152                 /* HANDLE FIELD */
1153                 if(R.r.mode & R_FIELDS) {
1154                         if(R.flag & R_SEC_FIELD) R.rectf2= R.rectot;
1155                         else R.rectf1= R.rectot;
1156                         R.rectot= NULL;
1157                 }
1158
1159                 if(RE_local_test_break()) break;
1160         }
1161
1162         /* JOIN FIELDS */
1163         if(R.r.mode & R_FIELDS) {
1164                 R.r.ysch*= 2;
1165                 R.afmy*= 2;
1166                 R.recty*= 2;
1167                 R.r.yasp/=2;
1168
1169                 if(R.rectot) MEM_freeN(R.rectot);       /* happens when a render has been stopped */
1170                 R.rectot=(unsigned int *)MEM_callocN(sizeof(int)*R.rectx*R.recty, "rectot");
1171                 
1172                 if(RE_local_test_break()==0) {
1173                         unsigned int *rt, *rt1, *rt2;
1174                         int len, a;
1175                         
1176                         rt= R.rectot;
1177
1178                         if(R.r.mode & R_ODDFIELD) {
1179                                 rt2= R.rectf1;
1180                                 rt1= R.rectf2;
1181                         }
1182                         else {
1183                                 rt1= R.rectf1;
1184                                 rt2= R.rectf2;
1185                         }
1186
1187                         len= 4*R.rectx;
1188
1189                         for(a=0; a<R.recty; a+=2) {
1190                                 memcpy(rt, rt1, len);
1191                                 rt+= R.rectx;
1192                                 rt1+= R.rectx;
1193                                 memcpy(rt, rt2, len);
1194                                 rt+= R.rectx;
1195                                 rt2+= R.rectx;
1196                         }
1197                 }
1198                 
1199                 if(R.rectf1) MEM_freeN(R.rectf1);
1200                 R.rectf1= NULL;
1201                 if(R.rectf2) MEM_freeN(R.rectf2);
1202                 R.rectf2= NULL;
1203                 /* fbuf and zbuf free, image size differs now */
1204                 if(R.rectftot) MEM_freeN(R.rectftot);
1205                 R.rectftot= NULL;
1206                 if(R.rectz) MEM_freeN(R.rectz);
1207                 R.rectz= NULL;
1208                 
1209         }
1210
1211         /* if border: still do skybuf */
1212         if(R.r.mode & R_BORDER) {
1213                 if( (R.r.mode & R_MOVIECROP)==0) {
1214                         if(R.r.bufflag & 1) {
1215                                 unsigned int *rt;
1216                                 int x, y;
1217                                 
1218                                 R.xstart= -R.afmx;
1219                                 R.ystart= -R.afmy;
1220                                 rt= R.rectot;
1221                                 for(y=0; y<R.recty; y++) {
1222                                         for(x=0; x<R.rectx; x++, rt++) {
1223                                                 if(*rt==0) fillBackgroundImageChar((char *)rt, x, y);
1224                                         }
1225                                 }
1226                         }
1227                 }
1228         }
1229
1230         set_mblur_offs(0);
1231
1232         /* mutexes free */
1233         SDL_DestroyMutex(load_ibuf_lock);
1234         SDL_DestroyMutex(render_abuf_lock);
1235         load_ibuf_lock= NULL;
1236         render_abuf_lock= NULL;
1237 }
1238
1239 void render() {
1240         /* yafray: render, see above */
1241         if (R.r.renderer==R_YAFRAY)
1242                 yafrayRender();
1243         else
1244                 mainRenderLoop();
1245 }
1246
1247
1248 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
1249
1250 void RE_initrender(struct View3D *ogl_render_view3d)
1251 {
1252         double start_time;
1253         Image *bima;
1254         char name[256];
1255         
1256         /* scene data to R */
1257         R.r= G.scene->r;
1258         R.r.postigamma= 1.0/R.r.postgamma;
1259         
1260         /* WINDOW size (sch='scherm' dutch for screen...) */
1261         R.r.xsch= (R.r.size*R.r.xsch)/100;
1262         R.r.ysch= (R.r.size*R.r.ysch)/100;
1263         
1264         R.afmx= R.r.xsch/2;
1265         R.afmy= R.r.ysch/2;
1266         
1267         /* to be sure: when a premature return (rectx can differ from xsch) */
1268         R.rectx= R.r.xsch;
1269         R.recty= R.r.ysch;
1270         
1271         /* IS RENDERING ALLOWED? */
1272         
1273         /* forbidden combination */
1274         if(R.r.mode & R_PANORAMA) {
1275                 if(R.r.mode & R_BORDER) {
1276                         error("No border supported for Panorama");
1277                         G.afbreek= 1;
1278                 }
1279                 if(R.r.yparts>1) {
1280                         error("No Y-Parts supported for Panorama");
1281                         G.afbreek= 1;
1282                 }
1283                 if(R.r.mode & R_ORTHO) {
1284                         error("No Ortho render possible for Panorama");
1285                         G.afbreek= 1;
1286                 }
1287         }
1288
1289         if(R.r.mode & R_BORDER) {
1290                 if(R.r.border.xmax <= R.r.border.xmin || 
1291                         R.r.border.ymax <= R.r.border.ymin) {
1292                         error("No border area selected.");
1293                         G.afbreek= 1;
1294                 }
1295         }
1296         
1297         if(R.r.xparts*R.r.yparts>=2 && (R.r.mode & R_MOVIECROP) && (R.r.mode & R_BORDER)) {
1298                 error("Combination of border, crop and parts not allowed");
1299                 G.afbreek= 1;
1300                 return;
1301         }
1302         
1303         if(R.r.xparts*R.r.yparts>64) {
1304                 error("No more than 64 parts supported");
1305                 G.afbreek= 1;
1306                 return;
1307         }
1308         
1309         if(R.r.yparts>1 && (R.r.mode & R_PANORAMA)) {
1310                 error("No Y-Parts supported for Panorama");
1311                 G.afbreek= 1;
1312                 return;
1313         }
1314         
1315         if(G.afbreek==1) return;
1316         
1317         /* TEST BACKBUF */
1318         /* If an image is specified for use as backdrop, that image is loaded    */
1319         /* here.                                                                 */
1320         if((R.r.bufflag & 1) && (G.scene->r.scemode & R_OGL)==0) {
1321                 if(R.r.alphamode == R_ADDSKY) {
1322                         strcpy(name, R.r.backbuf);
1323                         BLI_convertstringcode(name, G.sce, G.scene->r.cfra);
1324                         
1325                         if(R.backbuf) {
1326                                 R.backbuf->id.us--;
1327                                 bima= R.backbuf;
1328                         }
1329                         else bima= NULL;
1330                         
1331                         R.backbuf= add_image(name);
1332                         
1333                         if(bima && bima->id.us<1) {
1334                                 free_image_buffers(bima);
1335                         }
1336
1337                         if(R.backbuf && R.backbuf->ibuf==NULL) {
1338                                 R.backbuf->ibuf= IMB_loadiffname(R.backbuf->name, IB_rect);
1339                                 if(R.backbuf->ibuf==NULL) R.backbuf->ok= 0;
1340                                 else R.backbuf->ok= 1;
1341                         }
1342                         if(R.backbuf==NULL || R.backbuf->ok==0) {
1343                                 // error() doesnt work with render window open
1344                                 //error("No backbuf there!");
1345                                 printf("Error: No backbuf %s\n", name);
1346                         }
1347                 }
1348         }
1349         
1350         if(R.r.mode & (R_OSA|R_MBLUR)) {
1351                 R.osa= R.r.osa;
1352                 if(R.osa>16) R.osa= 16;
1353                 
1354                 init_render_jit(R.osa);
1355                 
1356         }
1357         else R.osa= 0;
1358         
1359         /* just prevents cpu cycles for larger render and copying */
1360         if((R.r.mode & R_OSA)==0)
1361                 R.r.filtertype= 0;
1362         
1363         renderloop_setblending();       // alpha, sky, gamma
1364         
1365         /* when rendered without camera object */
1366         /* it has to done here because of envmaps */
1367         R.near= 0.1;
1368         R.far= 1000.0;
1369         
1370         
1371         if(R.afmx<1 || R.afmy<1) {
1372                 error("Image too small");
1373                 return;
1374         }
1375         R.ycor= ( (float)R.r.yasp)/( (float)R.r.xasp);
1376         
1377         start_time= PIL_check_seconds_timer();
1378         
1379         if(R.r.scemode & R_OGL) {
1380                 R.rectx= R.r.xsch;
1381                 R.recty= R.r.ysch;
1382                 
1383                 if(R.rectot) MEM_freeN(R.rectot);
1384                 R.rectot= (unsigned int *)MEM_callocN(sizeof(int)*R.rectx*R.recty, "rectot");
1385                 
1386                 if(R.rectftot) MEM_freeN(R.rectftot);
1387                 R.rectftot= NULL;
1388                 
1389                 RE_local_init_render_display();
1390                 drawview3d_render(ogl_render_view3d);
1391         }
1392         else if(R.r.scemode & R_DOSEQ) {
1393                 R.rectx= R.r.xsch;
1394                 R.recty= R.r.ysch;
1395                 if(R.r.mode & R_PANORAMA) {
1396                         R.rectx*= R.r.xparts;
1397                 }
1398                 
1399                 if(R.rectot) MEM_freeN(R.rectot);
1400                 R.rectot= (unsigned int *)MEM_callocN(sizeof(int)*R.rectx*R.recty, "rectot");
1401                 
1402                 if(R.rectftot) MEM_freeN(R.rectftot);
1403                 R.rectftot= NULL;
1404                 
1405                 RE_local_timecursor((G.scene->r.cfra));
1406                 
1407                 if(RE_local_test_break()==0) do_render_seq();
1408                 
1409                 /* display */
1410                 if(R.rectot) RE_local_render_display(0, R.recty-1, R.rectx, R.recty,R.rectot);
1411         }
1412         else {
1413                 if(G.scene->camera==0) {
1414                         G.scene->camera= scene_find_camera(G.scene);
1415                 }
1416                 
1417                 if(G.scene->camera==0) {
1418                         error("No camera");
1419                         /* needed because R.rectx and R.recty can be unmatching R.rectot */
1420                         
1421                         if(R.rectot) MEM_freeN(R.rectot);
1422                         R.rectot= NULL;
1423                         if(R.rectftot) MEM_freeN(R.rectftot);
1424                         R.rectftot= NULL;
1425                         
1426                         G.afbreek=1;
1427                         return;
1428                 }
1429                 else {
1430                         
1431                         if(G.scene->camera->type==OB_CAMERA) {
1432                                 Camera *cam= G.scene->camera->data;
1433                                 if(cam->type==CAM_ORTHO) R.r.mode |= R_ORTHO;
1434                         }
1435                         
1436                         render(); /* returns with complete rect xsch-ysch */
1437                 }
1438         }
1439         
1440         /* display again: fields/seq/parts/pano etc */
1441         if(R.rectot) {
1442                 RE_local_init_render_display();
1443                 RE_local_render_display(0, R.recty-1, R.rectx, R.recty, R.rectot);
1444         }
1445         else RE_local_clear_render_display(R.win);
1446         
1447         if ((G.scene->r.scemode & R_OGL)==0) /* header gets scrabled if renderwindow holds OGL context */       
1448                 RE_local_printrenderinfo((PIL_check_seconds_timer() - start_time), -1);
1449         
1450         /* grms... this is a nasty global */
1451         do_gamma= 0;
1452         
1453         /* these flags remain on, until reset in caller to render (renderwin.c) */
1454         R.flag &= (R_RENDERING|R_ANIMRENDER|R_REDRAW_PRV);
1455 }
1456
1457 void RE_animrender(struct View3D *ogl_render_view3d)
1458 {
1459         int cfrao;
1460         char name[256];
1461
1462         if(G.scene==NULL) return;
1463         if(G.scene->r.sfra > G.scene->r.efra) {
1464                 error("Startframe larger than Endframe");
1465                 return;
1466         }
1467         
1468         /* scenedata to R: (for backbuf, R.rectx etc) */
1469         R.r= G.scene->r;
1470
1471         /* START ANIMLOOP, everywhere NOT the cfra from R.r is gebruikt: because of rest blender */
1472         cfrao= (G.scene->r.cfra);
1473
1474         /* disable options for ogl render */
1475         if(G.scene->r.scemode & R_OGL) R.r.mode &= ~(R_PANORAMA|R_MOVIECROP);
1476         
1477         // these calculations apply for all movie formats
1478         R.rectx= (R.r.size*R.r.xsch)/100;
1479         R.recty= (R.r.size*R.r.ysch)/100;
1480         if(R.r.mode & R_PANORAMA) {
1481                 R.rectx*= R.r.xparts;
1482                 R.recty*= R.r.yparts;
1483         }
1484         if(R.r.mode & R_MOVIECROP) {
1485                 initparts();
1486                 setpart(R.parts.first);         // this will adjust r.rectx
1487         }
1488
1489         if (0) {
1490 #ifdef __sgi
1491         } else if (R.r.imtype==R_MOVIE) {
1492                 start_movie();
1493 #endif
1494 #if defined(_WIN32) && !defined(FREE_WINDOWS)
1495         } else if (R.r.imtype == R_AVICODEC) {
1496                 start_avi_codec();
1497 #endif
1498 #if WITH_QUICKTIME
1499         } else if (R.r.imtype == R_QUICKTIME) {
1500                 start_qt();
1501 #endif
1502         } else if ELEM4(R.r.imtype, R_AVIRAW, R_AVIJPEG, R_MOVIE, R_AVICODEC) {
1503                 if ELEM(R.r.imtype, R_MOVIE, R_AVICODEC) {
1504                         printf("Selected movie format not supported on this platform,\nusing RAW AVI instead\n");
1505                 }
1506                 start_avi();
1507         }
1508 // set initial conditions for softbodies here
1509 // ******************************************
1510         for((G.scene->r.cfra)=(G.scene->r.sfra); (G.scene->r.cfra)<=(G.scene->r.efra); (G.scene->r.cfra)++) {
1511                 double starttime= PIL_check_seconds_timer();
1512
1513                 R.flag |= R_ANIMRENDER; // unused now (ton)
1514
1515                 RE_initrender(ogl_render_view3d);
1516                 
1517                 /* WRITE IMAGE */
1518                 if(RE_local_test_break()==0) {
1519
1520                         if (0) {
1521 #ifdef __sgi
1522                         } else if (R.r.imtype == R_MOVIE) {
1523                                 append_movie((G.scene->r.cfra));
1524 #endif
1525 #if defined(_WIN32) && !defined(FREE_WINDOWS)
1526                         } else if (R.r.imtype == R_AVICODEC) {
1527                                 append_avi_codec((G.scene->r.cfra));
1528 #endif
1529 #ifdef WITH_QUICKTIME
1530                         } else if (R.r.imtype == R_QUICKTIME) {
1531                                 append_qt((G.scene->r.cfra));
1532 #endif
1533                         } else if ELEM4(R.r.imtype, R_AVIRAW, R_AVIJPEG, R_MOVIE, R_AVICODEC) {
1534                                 append_avi((G.scene->r.cfra));
1535                         } else {
1536                                 makepicstring(name, (G.scene->r.cfra));
1537                                 schrijfplaatje(name);
1538                                 if(RE_local_test_break()==0) printf("Saved: %s", name);
1539                         }
1540
1541                         timestr(PIL_check_seconds_timer()-starttime, name);
1542                         printf(" Time: %s\n", name);
1543                         fflush(stdout); /* needed for renderd !! */
1544                 }
1545
1546                 if(G.afbreek==1) break;
1547
1548         }
1549
1550         G.scene->r.cfra= cfrao;
1551
1552         /* restore time */
1553         if(R.r.mode & (R_FIELDS|R_MBLUR)) {
1554                 /* applies changes fully */
1555                 scene_update_for_newframe(G.scene, G.scene->lay);
1556         }
1557
1558         if (0) {
1559 #ifdef __sgi    
1560         } else if (R.r.imtype==R_MOVIE) {
1561                 end_movie();
1562 #endif
1563 #if defined(_WIN32) && !defined(FREE_WINDOWS)
1564         } else if (R.r.imtype == R_AVICODEC) {
1565                 end_avi_codec();
1566 #endif
1567 #ifdef WITH_QUICKTIME
1568         } else if (R.r.imtype == R_QUICKTIME) {
1569                 end_qt();
1570 #endif
1571         } else if ELEM4(R.r.imtype, R_AVIRAW, R_AVIJPEG, R_MOVIE, R_AVICODEC) {
1572                 end_avi();
1573         }
1574 }
1575
1576 /* *************************************************** */
1577 /* ******************* Screendumps ******************** */
1578 /* moved to the windowControl thing */