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