2.5
[blender-staging.git] / source / blender / editors / preview / previewrender.c
1 /* 
2  * $Id$
3  *
4  * ***** BEGIN GPL 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.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) Blender Foundation.
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): none yet.
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  */
29
30 /* global includes */
31
32 #include <stdlib.h>
33 #include <math.h>
34 #include <string.h>
35
36 #ifndef WIN32
37 #include <unistd.h>
38 #else
39 #include <io.h>
40 #endif   
41 #include "MEM_guardedalloc.h"
42
43 #include "BLO_readfile.h" 
44
45 #include "BLI_arithb.h"
46 #include "BLI_blenlib.h"
47
48 #include "DNA_texture_types.h"
49 #include "DNA_world_types.h"
50 #include "DNA_camera_types.h"
51 #include "DNA_image_types.h"
52 #include "DNA_material_types.h"
53 #include "DNA_node_types.h"
54 #include "DNA_object_types.h"
55 #include "DNA_lamp_types.h"
56 #include "DNA_space_types.h"
57 #include "DNA_view3d_types.h"
58 #include "DNA_scene_types.h"
59 #include "DNA_screen_types.h"
60
61 #include "BKE_context.h"
62 #include "BKE_depsgraph.h"
63 #include "BKE_global.h"
64 #include "BKE_image.h"
65 #include "BKE_icons.h"
66 #include "BKE_library.h"
67 #include "BKE_main.h"
68 #include "BKE_texture.h"
69 #include "BKE_material.h"
70 #include "BKE_node.h"
71 #include "BKE_world.h"
72 #include "BKE_texture.h"
73 #include "BKE_utildefines.h"
74
75 #include "IMB_imbuf.h"
76 #include "IMB_imbuf_types.h"
77
78 #include "BIF_gl.h"
79 #include "BIF_glutil.h"
80
81 #include "PIL_time.h"
82
83 #include "RE_pipeline.h"
84
85 #include "GPU_material.h"
86
87 #include "ED_anim_api.h"
88 #include "ED_view3d.h"
89
90 #include "UI_interface.h"
91
92 #include "previewrender_intern.h"
93
94 #define PR_XMIN         10
95 #define PR_YMIN         5
96 #define PR_XMAX         200
97 #define PR_YMAX         195
98
99 /* XXX */
100 static int qtest() {return 0;}
101 /* XXX */
102
103 static void set_previewrect(ScrArea *sa, RenderInfo *ri)
104 {
105         ARegion *ar= NULL; // XXX
106         rctf viewplane;
107         
108         BLI_init_rctf(&viewplane, PR_XMIN, PR_XMAX, PR_YMIN, PR_YMAX);
109
110 //      ui_graphics_to_window_rct(ar->win, &viewplane, &ri->disprect);
111         
112         /* correction for gla draw */
113         BLI_translate_rcti(&ri->disprect, -ar->winrct.xmin, -ar->winrct.ymin);
114         
115         glMatrixMode(GL_PROJECTION);
116         glPushMatrix();
117         glMatrixMode(GL_MODELVIEW);
118         glPushMatrix();
119         
120         glaDefine2DArea(&ar->winrct);
121
122         ri->pr_rectx= (ri->disprect.xmax-ri->disprect.xmin);
123         ri->pr_recty= (ri->disprect.ymax-ri->disprect.ymin);
124 }
125
126 static void end_previewrect(ARegion *ar)
127 {
128         glMatrixMode(GL_PROJECTION);
129         glPopMatrix();
130         glMatrixMode(GL_MODELVIEW);
131         glPopMatrix();
132         
133         // restore viewport / scissor which was set by glaDefine2DArea
134         glViewport(ar->winrct.xmin, ar->winrct.ymin, ar->winx, ar->winy);
135         glScissor(ar->winrct.xmin, ar->winrct.ymin, ar->winx, ar->winy);
136
137 }
138
139 /* unused now */
140 void draw_tex_crop(Tex *tex)
141 {
142         rcti rct;
143         int ret= 0;
144         
145         if(tex==0) return;
146         
147         if(tex->type==TEX_IMAGE) {
148                 if(tex->cropxmin==0.0f) ret++;
149                 if(tex->cropymin==0.0f) ret++;
150                 if(tex->cropxmax==1.0f) ret++;
151                 if(tex->cropymax==1.0f) ret++;
152                 if(ret==4) return;
153                 
154                 rct.xmin= PR_XMIN+2+tex->cropxmin*(PR_XMAX-PR_XMIN-4);
155                 rct.xmax= PR_XMIN+2+tex->cropxmax*(PR_XMAX-PR_XMIN-4);
156                 rct.ymin= PR_YMIN+2+tex->cropymin*(PR_YMAX-PR_YMIN-4);
157                 rct.ymax= PR_YMIN+2+tex->cropymax*(PR_YMAX-PR_YMIN-4);
158
159                 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); 
160
161                 glColor3ub(0, 0, 0);
162                 glRecti(rct.xmin+1,  rct.ymin-1,  rct.xmax+1,  rct.ymax-1); 
163
164                 glColor3ub(255, 255, 255);
165                 glRecti(rct.xmin,  rct.ymin,  rct.xmax,  rct.ymax);
166
167                 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);                      
168         }
169         
170 }
171
172 /* temporal abuse; if id_code is -1 it only does texture.... solve! */
173 void BIF_preview_changed(short id_code)
174 {
175 #if 0   
176         ScrArea *sa;
177         
178         for(sa= G.curscreen->areabase.first; sa; sa= sa->next) {
179                 if(sa->spacetype==SPACE_BUTS) {
180                         SpaceButs *sbuts= sa->spacedata.first;
181                         if(sbuts->mainb==CONTEXT_SHADING) {
182                                 int tab= sbuts->tab[CONTEXT_SHADING];
183                                 if(tab==TAB_SHADING_MAT && (id_code==ID_MA || id_code==ID_TE)) {
184                                         if (sbuts->ri) sbuts->ri->curtile= 0;
185                                         addafterqueue(sa->win, RENDERPREVIEW, 1);
186                                 }
187                                 else if(tab==TAB_SHADING_TEX && (id_code==ID_TE || id_code==-1)) {
188                                         if (sbuts->ri) sbuts->ri->curtile= 0;
189                                         addafterqueue(sa->win, RENDERPREVIEW, 1);
190                                 }
191                                 else if(tab==TAB_SHADING_LAMP && (id_code==ID_LA || id_code==ID_TE)) {
192                                         if (sbuts->ri) sbuts->ri->curtile= 0;
193                                         addafterqueue(sa->win, RENDERPREVIEW, 1);
194                                 }
195                                 else if(tab==TAB_SHADING_WORLD && (id_code==ID_WO || id_code==ID_TE)) {
196                                         if (sbuts->ri) sbuts->ri->curtile= 0;
197                                         addafterqueue(sa->win, RENDERPREVIEW, 1);
198                                 }
199                         }
200                         else if (sbuts->ri) 
201                                 sbuts->ri->curtile= 0;  /* ensure changes always result in re-render when context is restored */
202                 }
203                 else if(sa->spacetype==SPACE_NODE) {
204                         SpaceNode *snode= sa->spacedata.first;
205                         if(snode->treetype==NTREE_SHADER && (id_code==ID_MA || id_code==ID_TE)) {
206                                 snode_tag_dirty(snode);
207                         }
208                 }
209                 else if(sa->spacetype==SPACE_VIEW3D) {
210                         View3D *vd= sa->spacedata.first;
211                         /* if is has a renderinfo, we consider that reason for signalling */
212                         if (vd->ri) {
213                                 vd->ri->curtile= 0;
214                                 addafterqueue(sa->win, RENDERPREVIEW, 1);
215                         }
216                 }
217         }
218
219         if(ELEM4(id_code, ID_MA, ID_TE, ID_LA, ID_WO)) {
220                 Object *ob;
221                 Material *ma;
222
223                 if(id_code == ID_WO) {
224                         for(ma=G.main->mat.first; ma; ma=ma->id.next) {
225                                 if(ma->gpumaterial.first) {
226                                         GPU_material_free(ma);
227                                 }
228                         }
229                 }
230                 else if(id_code == ID_LA) {
231                         for(ob=G.main->object.first; ob; ob=ob->id.next) {
232                                 if(ob->gpulamp.first) {
233                                         GPU_lamp_free(ob);
234                                 }
235                         }
236                 } else if(OBACT) {
237                         Object *ob = OBACT;
238
239                         ma= give_current_material(ob, ob->actcol);
240                         if(ma && ma->gpumaterial.first) {
241                                 GPU_material_free(ma);
242                         }
243                 }
244         }
245 #endif
246 }
247
248 /* *************************** Preview for buttons *********************** */
249
250 static Main *pr_main= NULL;
251
252 void BIF_preview_init_dbase(void)
253 {
254         BlendFileData *bfd;
255         extern int datatoc_preview_blend_size;
256         extern char datatoc_preview_blend[];
257         
258         G.fileflags |= G_FILE_NO_UI;
259         bfd= BLO_read_from_memory(datatoc_preview_blend, datatoc_preview_blend_size, NULL);
260         if (bfd) {
261                 pr_main= bfd->main;
262                 
263                 MEM_freeN(bfd);
264         }
265         G.fileflags &= ~G_FILE_NO_UI;
266 }
267
268 void BIF_preview_free_dbase(void)
269 {
270         if(pr_main)
271                 free_main(pr_main);
272 }
273
274 static Object *find_object(ListBase *lb, const char *name)
275 {
276         Object *ob;
277         for(ob= lb->first; ob; ob= ob->id.next)
278                 if(strcmp(ob->id.name+2, name)==0)
279                         break;
280         return ob;
281 }
282
283 /* call this with an ID pointer to initialize preview scene */
284 /* call this with ID NULL to restore assigned ID pointers in preview scene */
285 static Scene *preview_prepare_scene(Scene *scene, RenderInfo *ri, int id_type, ID *id, int pr_method)
286 {
287         Scene *sce;
288         Base *base;
289         
290         if(pr_main==NULL) return NULL;
291         
292         sce= pr_main->scene.first;
293         if(sce) {
294                 /* this flag tells render to not execute depsgraph or ipos etc */
295                 sce->r.scemode |= R_PREVIEWBUTS;
296                 /* set world always back, is used now */
297                 sce->world= pr_main->world.first;
298                 /* now: exposure copy */
299                 if(scene->world) {
300                         sce->world->exp= scene->world->exp;
301                         sce->world->range= scene->world->range;
302                 }
303                 
304                 sce->r.cfra= scene->r.cfra;
305                 
306                 if(id_type==ID_MA) {
307                         Material *mat= (Material *)id;
308                         
309                         if(id) {
310                                 init_render_material(mat, 0, NULL);             /* call that retrieves mode_l */
311                                 end_render_material(mat);
312                                 
313                                 /* turn on raytracing if needed */
314                                 if(mat->mode_l & (MA_RAYTRANSP|MA_RAYMIRROR))
315                                         sce->r.mode |= R_RAYTRACE;
316                                 if(mat->sss_flag & MA_DIFF_SSS)
317                                         sce->r.mode |= R_SSS;
318                                 
319                                 /* turn off fake shadows if needed */
320                                 /* this only works in a specific case where the preview.blend contains
321                                  * an object starting with 'c' which has a material linked to it (not the obdata)
322                                  * and that material has a fake shadow texture in the active texture slot */
323                                 for(base= sce->base.first; base; base= base->next) {
324                                         if(base->object->id.name[2]=='c') {
325                                                 Material *shadmat= give_current_material(base->object, base->object->actcol);
326                                                 if(shadmat) {
327                                                         if (mat->mode & MA_SHADBUF) shadmat->septex = 0;
328                                                         else shadmat->septex |= 1;
329                                                 }
330                                         }
331                                 }
332
333                                 
334                                 if(pr_method==PR_ICON_RENDER) {
335                                         if (mat->mode & MA_HALO) {
336                                                 sce->lay= 1<<MA_FLAT;
337                                         } 
338                                         else {
339                                                 sce->lay= 1<<MA_SPHERE_A;
340                                         }
341                                 }
342                                 else {
343                                         sce->lay= 1<<mat->pr_type;
344                                         if(mat->nodetree)
345                                                 ntreeInitPreview(mat->nodetree, ri->pr_rectx, ri->pr_recty);
346                                 }
347                         }
348                         else {
349                                 sce->r.mode &= ~(R_OSA|R_RAYTRACE|R_SSS);
350                         }
351                         
352                         for(base= sce->base.first; base; base= base->next) {
353                                 if(base->object->id.name[2]=='p') {
354                                         if(ELEM4(base->object->type, OB_MESH, OB_CURVE, OB_SURF, OB_MBALL))
355                                                 assign_material(base->object, mat, base->object->actcol);
356                                 }
357                         }
358                 }
359                 else if(id_type==ID_TE) {
360                         Tex *tex= (Tex *)id;
361                         
362                         sce->lay= 1<<MA_TEXTURE;
363                         
364                         for(base= sce->base.first; base; base= base->next) {
365                                 if(base->object->id.name[2]=='t') {
366                                         Material *mat= give_current_material(base->object, base->object->actcol);
367                                         if(mat && mat->mtex[0]) {
368                                                 mat->mtex[0]->tex= tex;
369                                                 /* show alpha in this case */
370                                                 if(tex==NULL || (tex->flag & TEX_PRV_ALPHA)) {
371                                                         mat->mtex[0]->mapto |= MAP_ALPHA;
372                                                         mat->alpha= 0.0f;
373                                                 }
374                                                 else {
375                                                         mat->mtex[0]->mapto &= ~MAP_ALPHA;
376                                                         mat->alpha= 1.0f;
377                                                 }
378                                         }
379                                 }
380                         }
381                 }
382                 else if(id_type==ID_LA) {
383                         Lamp *la= (Lamp *)id;
384                         
385                         if(la && la->type==LA_SUN && (la->sun_effect_type & LA_SUN_EFFECT_SKY)) {
386                                 sce->lay= 1<<MA_ATMOS;
387                                 sce->world= scene->world;
388                                 sce->camera= (Object *)find_object(&pr_main->object, "CameraAtmo");
389                         }
390                         else {
391                                 sce->lay= 1<<MA_LAMP;
392                                 sce->world= NULL;
393                                 sce->camera= (Object *)find_object(&pr_main->object, "Camera");
394                         }
395                         sce->r.mode &= ~R_SHADOW;
396                         
397                         for(base= sce->base.first; base; base= base->next) {
398                                 if(base->object->id.name[2]=='p') {
399                                         if(base->object->type==OB_LAMP)
400                                                 base->object->data= la;
401                                 }
402                         }
403                 }
404                 else if(id_type==ID_WO) {
405                         sce->lay= 1<<MA_SKY;
406                         sce->world= (World *)id;
407                 }
408                 
409                 return sce;
410         }
411         
412         return NULL;
413 }
414
415 static void previewrender_progress(RenderResult *rr, volatile rcti *renrect)
416 {
417         SpaceButs *sbuts= NULL; // XXX
418         RenderLayer *rl;
419         RenderInfo *ri= sbuts->ri;
420         float ofsx, ofsy;
421         
422         if(renrect) return;
423         
424         rl= rr->layers.first;
425         
426         ofsx= ri->disprect.xmin + rr->tilerect.xmin;
427         ofsy= ri->disprect.ymin + rr->tilerect.ymin;
428         
429         glDrawBuffer(GL_FRONT);
430         glaDrawPixelsSafe_to32(ofsx, ofsy, rr->rectx, rr->recty, rr->rectx, rl->rectf);
431         bglFlush();
432         glDrawBuffer(GL_BACK);
433 }
434
435
436 /* called by interface_icons.c, or by BIF_previewrender_buts or by nodes... */
437 void BIF_previewrender(Scene *scene, struct ID *id, struct RenderInfo *ri, struct ScrArea *area, int pr_method)
438 {
439         Render *re;
440         RenderStats *rstats;
441         Scene *sce;
442         SpaceButs *sbuts= NULL;
443         int oldx= ri->pr_rectx, oldy= ri->pr_recty;
444         char name [32];
445         
446         if(ri->tottile && ri->curtile>=ri->tottile) return;
447         
448         /* check for return with a new event */
449         if(pr_method!=PR_ICON_RENDER && qtest()) {
450 //              if(area)
451 //                      addafterqueue(area->win, RENDERPREVIEW, 1);
452                 return;
453         }
454         
455         /* get the stuff from the builtin preview dbase */
456         sce= preview_prepare_scene(scene, ri, GS(id->name), id, pr_method);
457         if(sce==NULL) return;
458         
459         /* set drawing conditions OK */
460         if(area) {
461                 sbuts= area->spacedata.first;   /* needed for flag */
462                 
463                 set_previewrect(area, ri); // uses UImat
464                 
465                 /* because preview render size can differs */
466                 if(ri->rect && (oldx!=ri->pr_rectx || oldy!=ri->pr_recty)) {
467                         MEM_freeN(ri->rect);
468                         ri->rect= NULL;
469                         ri->curtile= 0;
470                 }
471         }
472         
473 // XXX  sprintf(name, "ButsPreview %d", area?area->win:0);
474         re= RE_GetRender(name);
475         
476         /* full refreshed render from first tile */
477         if(re==NULL || ri->curtile==0) {
478                 
479                 re= RE_NewRender(name);
480                 
481                 /* handle cases */
482                 if(pr_method==PR_DRAW_RENDER) {
483                         RE_display_draw_cb(re, previewrender_progress);
484                         RE_test_break_cb(re, qtest);
485                         sce->r.scemode |= R_NODE_PREVIEW;
486                         if(sbuts->flag & SB_PRV_OSA)
487                                 sce->r.mode |= R_OSA;
488                         sce->r.scemode &= ~R_NO_IMAGE_LOAD;
489                 }
490                 else if(pr_method==PR_DO_RENDER) {
491                         RE_test_break_cb(re, qtest);
492                         sce->r.scemode |= R_NODE_PREVIEW;
493                         sce->r.scemode &= ~R_NO_IMAGE_LOAD;
494                 }
495                 else {  /* PR_ICON_RENDER */
496                         sce->r.scemode &= ~R_NODE_PREVIEW;
497                         sce->r.scemode |= R_NO_IMAGE_LOAD;
498                 }
499                 
500                 /* allocates render result */
501                 RE_InitState(re, NULL, &sce->r, ri->pr_rectx, ri->pr_recty, NULL);
502                 
503                 /* enforce preview image clear */
504                 if(GS(id->name)==ID_MA) {
505                         Material *ma= (Material *)id;
506                         ntreeClearPreview(ma->nodetree);
507                 }
508         }
509         /* entire cycle for render engine */
510         RE_SetCamera(re, sce->camera);
511         RE_Database_FromScene(re, sce, 1);
512         RE_TileProcessor(re, ri->curtile, 0);   // actual render engine
513         RE_Database_Free(re);
514         
515         /* handle results */
516         if(pr_method==PR_ICON_RENDER) {
517                 if(ri->rect==NULL)
518                         ri->rect= MEM_mallocN(sizeof(int)*ri->pr_rectx*ri->pr_recty, "BIF_previewrender");
519                 RE_ResultGet32(re, ri->rect);
520         }
521         else {
522                 rstats= RE_GetStats(re);
523                 
524                 if(rstats->partsdone!=ri->curtile) {
525                         if(ri->rect==NULL)
526                                 ri->rect= MEM_mallocN(sizeof(int)*ri->pr_rectx*ri->pr_recty, "BIF_previewrender");
527                         RE_ResultGet32(re, ri->rect);
528                 }
529                 
530                 if(rstats->totpart==rstats->partsdone && rstats->partsdone) {
531                         // allqueues
532                 }
533                 else {
534 //                      if(pr_method==PR_DRAW_RENDER && qtest())
535 //                              addafterqueue(area->win, RENDERPREVIEW, 1);
536                 }
537                 
538                 ri->curtile= rstats->partsdone;
539                 ri->tottile= rstats->totpart;
540         }
541
542         /* unassign the pointers, reset vars */
543         preview_prepare_scene(scene, ri, GS(id->name), NULL, 0);
544         
545 }
546
547
548 /* afterqueue call */
549 void BIF_previewrender_buts(Scene *scene, SpaceButs *sbuts)
550 {
551 //      ScrArea *sa= NULL; // XXX
552         ARegion *ar= NULL; // XXX
553         uiBlock *block;
554         struct ID* id = 0;
555 //      struct ID* idfrom = 0;
556         struct ID* idshow = 0;
557         Object *ob;
558         
559         if (!sbuts->ri) return;
560         
561         
562 //      block= uiFindOpenPanelBlockName(&sa->uiblocks, "Preview");
563         if(block==NULL) return;
564         
565         ob= ((scene->basact)? (scene->basact)->object: 0);
566         
567         /* we cant trust this global lockpoin.. for example with headerless window */
568 //      buttons_active_id(&id, &idfrom);
569         sbuts->lockpoin= id;
570         
571         if(sbuts->mainb==CONTEXT_SHADING) {
572                 int tab= sbuts->tab[CONTEXT_SHADING];
573                 
574                 if(tab==TAB_SHADING_MAT) 
575                         idshow = sbuts->lockpoin;
576                 else if(tab==TAB_SHADING_TEX) 
577                         idshow = sbuts->lockpoin;
578                 else if(tab==TAB_SHADING_LAMP) {
579                         if(ob && ob->type==OB_LAMP) idshow= ob->data;
580                 }
581                 else if(tab==TAB_SHADING_WORLD)
582                         idshow = sbuts->lockpoin;
583         }
584         else if(sbuts->mainb==CONTEXT_OBJECT) {
585                 if(ob && ob->type==OB_LAMP) idshow = ob->data;
586         }
587         
588         if (idshow) {
589                 BKE_icon_changed(BKE_icon_getid(idshow));
590 //              uiPanelPush(block);
591 //              BIF_previewrender(scene, idshow, sbuts->ri, sbuts->area, PR_DRAW_RENDER);
592 //              uiPanelPop(block);
593                 end_previewrect(ar);
594         }
595         else {
596                 /* no active block to draw. But we do draw black if possible */
597                 if(sbuts->ri->rect) {
598                         memset(sbuts->ri->rect, 0, sizeof(int)*sbuts->ri->pr_rectx*sbuts->ri->pr_recty);
599                         sbuts->ri->tottile= 10000;
600 //                      addqueue(sa->win, REDRAW, 1);
601                 }
602                 return;
603         }
604 }
605
606
607 /* is panel callback, supposed to be called with correct panel offset matrix */
608 void BIF_previewdraw(ScrArea *sa, uiBlock *block)
609 {
610         ARegion *ar= NULL; // XXX
611         SpaceButs *sbuts= sa->spacedata.first;
612         short id_code= 0;
613         
614         if(sbuts->lockpoin) {
615                 ID *id= sbuts->lockpoin;
616                 id_code= GS(id->name);
617         }
618         
619         if (!sbuts->ri) {
620                 sbuts->ri= MEM_callocN(sizeof(RenderInfo), "butsrenderinfo");
621                 sbuts->ri->tottile = 10000;
622         }
623         
624         if (sbuts->ri->rect==NULL) BIF_preview_changed(id_code);
625         else {
626                 RenderInfo *ri= sbuts->ri;
627                 int oldx= ri->pr_rectx, oldy= ri->pr_recty;
628                 
629                 /* we now do scalable previews! */
630                 set_previewrect(sa, ri);
631                 if( ABS(oldx-ri->pr_rectx)<2 && ABS(oldy-ri->pr_recty)<2 ) {
632                         /* restore old values for drawing! */
633                         ri->pr_rectx= oldx;
634                         ri->pr_recty= oldy;
635                         glaDrawPixelsSafe(ri->disprect.xmin, ri->disprect.ymin, ri->pr_rectx, ri->pr_recty, ri->pr_rectx, GL_RGBA, GL_UNSIGNED_BYTE, ri->rect);
636                 }
637                 else {
638                         MEM_freeN(ri->rect);
639                         ri->rect= NULL;
640                         sbuts->ri->curtile= 0;
641                 }
642                 end_previewrect(ar);
643         }
644         if(sbuts->ri->curtile==0) BIF_preview_changed(id_code);
645         
646 }
647
648 /* *************************** Preview for 3d window *********************** */
649 static void view3d_previewrender_stats(RenderStats *rs)
650 {
651 //      if(rs->convertdone) 
652 //              printf("rendered %d %.3f\n", rs->partsdone, rs->lastframetime);
653 }
654
655 static void view3d_previewrender_progress(RenderResult *rr, volatile rcti *renrect)
656 {
657 //      ScrArea *sa= NULL; // XXX
658 //      View3D *v3d= NULL; // XXX
659         RenderLayer *rl;
660         int ofsx, ofsy;
661         
662         if(renrect) return;
663         
664         rl= rr->layers.first;
665         
666         /* this case is when we render envmaps... */
667 //      if(rr->rectx > v3d->ri->pr_rectx || rr->recty > v3d->ri->pr_recty)
668 //              return;
669         
670 //      ofsx= v3d->ri->disprect.xmin + rr->tilerect.xmin;
671 //      ofsy= v3d->ri->disprect.ymin + rr->tilerect.ymin;
672         
673         glDrawBuffer(GL_FRONT);
674 //      glaDefine2DArea(&sa->winrct);
675         glaDrawPixelsSafe_to32(ofsx, ofsy, rr->rectx, rr->recty, rr->rectx, rl->rectf);
676         bglFlush();
677         glDrawBuffer(GL_BACK);
678
679 }
680
681 void BIF_view3d_previewrender_signal(ScrArea *sa, short signal)
682 {
683 #if 0
684         View3D *v3d= sa->spacedata.first;
685         
686         /* this can be called from other window... solve! */
687         if(sa->spacetype!=SPACE_VIEW3D)
688                 return; // XXX
689            
690         if(v3d && v3d->ri) {
691                 RenderInfo *ri= v3d->ri;
692                 ri->status &= ~signal;
693                 ri->curtile= 0;
694                 //printf("preview signal %d\n", signal);
695                 if(ri->re && (signal & PR_DBASE))
696                         RE_Database_Free(ri->re);
697
698 //              addafterqueue(sa->win, RENDERPREVIEW, 1);
699         }
700 #endif
701 }
702
703 void BIF_view3d_previewrender_free(View3D *v3d)
704 {
705 #if 0
706         if(v3d->ri) {
707                 RenderInfo *ri= v3d->ri;
708                 if(ri->re) {
709 //                      printf("free render\n");
710                         RE_Database_Free(ri->re);
711                         RE_FreeRender(ri->re);
712                         ri->re= NULL;
713                 }
714                 if (v3d->ri->rect) MEM_freeN(v3d->ri->rect);
715                 MEM_freeN(v3d->ri);
716                 v3d->ri= NULL;
717         }
718 #endif
719 }
720
721 /* returns 1 if OK, do not call while in panel space!  */
722 static int view3d_previewrender_get_rects(ScrArea *sa, rctf *viewplane, RenderInfo *ri, float *clipsta, float *clipend, int *ortho, float *pixsize)
723 {
724         View3D *v3d= NULL; // XXX
725         RegionView3D *rv3d= NULL; // XXX
726         int rectx, recty;
727 //      uiBlock *block;
728         
729 //      block= uiFindOpenPanelBlockName(&sa->uiblocks, "Preview");
730 //      if(block==NULL) return 0;
731         
732         /* calculate preview rect size */
733 //      BLI_init_rctf(viewplane, 15.0f, (block->maxx - block->minx)-15.0f, 15.0f, (block->maxy - block->miny)-15.0f);
734 //      uiPanelPush(block);
735 //      ui_graphics_to_window_rct(sa->win, viewplane, &ri->disprect);
736 //      uiPanelPop(block);
737         
738         /* correction for gla draw */
739 //      BLI_translate_rcti(&ri->disprect, -sa->winrct.xmin, -sa->winrct.ymin);
740         
741         *ortho= get_view3d_viewplane(v3d, rv3d, sa->winx, sa->winy, viewplane, clipsta, clipend, pixsize);
742
743         rectx= ri->disprect.xmax - ri->disprect.xmin;
744         recty= ri->disprect.ymax - ri->disprect.ymin;
745         
746         if(rectx<4 || recty<4) return 0;
747         
748         if(ri->rect && (rectx!=ri->pr_rectx || recty!=ri->pr_recty)) {
749                 MEM_freeN(ri->rect);
750                 ri->rect= NULL;
751                 ri->curtile= 0;
752                 printf("changed size\n");
753         }
754         ri->pr_rectx= rectx;
755         ri->pr_recty= recty;
756         
757         return 1;
758 }
759
760 /* called before a panel gets moved/scaled, makes sure we can see through */
761 void BIF_view3d_previewrender_clear(ScrArea *sa)
762 {
763 #if 0
764         View3D *v3d= sa->spacedata.first;
765
766         if(v3d->ri) {
767                 RenderInfo *ri= v3d->ri;
768                 ri->curtile= 0;
769                 if(ri->rect)
770                         MEM_freeN(ri->rect);
771                 ri->rect= NULL;
772         }
773 #endif
774 }
775
776 /* afterqueue call */
777 void BIF_view3d_previewrender(Scene *scene, ScrArea *sa)
778 {
779         bContext *C= NULL;
780         View3D *v3d= sa->spacedata.first;
781         RegionView3D *rv3d= NULL; // XXX
782         Render *re;
783         RenderInfo *ri; /* preview struct! */
784         RenderStats *rstats;
785         RenderData rdata;
786         rctf viewplane;
787         float clipsta, clipend, pixsize;
788         int orth;
789         
790         /* first get the render info right */
791 //      if (!v3d->ri) {
792 //              ri= v3d->ri= MEM_callocN(sizeof(RenderInfo), "butsrenderinfo");
793 //              ri->tottile= 10000;
794 //      }
795 //      ri= v3d->ri;
796         
797         if(0==view3d_previewrender_get_rects(sa, &viewplane, ri, &clipsta, &clipend, &orth, &pixsize))
798                 return;
799         
800         /* render is finished, so return */
801         if(ri->tottile && ri->curtile>=ri->tottile) return;
802
803         /* or return with a new event */
804         if(qtest()) {
805 //              addafterqueue(sa->win, RENDERPREVIEW, 1);
806                 return;
807         }
808         //printf("Enter previewrender\n");
809         /* ok, are we rendering all over? */
810         if(ri->re==NULL) {
811                 char name[32];
812                 
813                 ri->status= 0;
814                 
815                 sprintf(name, "View3dPreview %p", sa);
816                 re= ri->re= RE_NewRender(name);
817                 RE_display_draw_cb(re, view3d_previewrender_progress);
818                 RE_stats_draw_cb(re, view3d_previewrender_stats);
819                 RE_test_break_cb(re, qtest);
820                 
821                 /* no osa, blur, seq, layers, etc for preview render */
822                 rdata= scene->r;
823                 rdata.mode &= ~(R_OSA|R_MBLUR);
824                 rdata.scemode &= ~(R_DOSEQ|R_DOCOMP|R_FREE_IMAGE);
825                 rdata.layers.first= rdata.layers.last= NULL;
826                 rdata.renderer= R_INTERN;
827                  
828                 RE_InitState(re, NULL, &rdata, sa->winx, sa->winy, &ri->disprect);
829         
830                 if(orth)
831                         RE_SetOrtho(re, &viewplane, clipsta, clipend);
832                 else
833                         RE_SetWindow(re, &viewplane, clipsta, clipend);
834                 RE_SetPixelSize(re, pixsize);
835                 
836                 /* until here are no escapes */
837                 ri->status |= PR_DISPRECT;
838                 ri->curtile= 0;
839                 //printf("new render\n");
840         }
841
842         re= ri->re;
843         
844         PIL_sleep_ms(100);      /* wait 0.1 second if theres really no event... */
845         if(qtest()==0)  {
846                 
847                 /* check status */
848                 if((ri->status & PR_DISPRECT)==0) {
849                         RE_SetDispRect(ri->re, &ri->disprect);
850                         if(orth)
851                                 RE_SetOrtho(ri->re, &viewplane, clipsta, clipend);
852                         else
853                                 RE_SetWindow(ri->re, &viewplane, clipsta, clipend);
854                         RE_SetPixelSize(re, pixsize);
855                         ri->status |= PR_DISPRECT;
856                         ri->curtile= 0;
857                         //printf("disprect update\n");
858                 }
859                 if((ri->status & PR_DBASE)==0) {
860                         unsigned int lay= scene->lay;
861                         
862                         RE_SetView(re, rv3d->viewmat);
863                         
864                         /* allow localview render for objects with lights in normal layers */
865                         if(v3d->lay & 0xFF000000)
866                                 scene->lay |= v3d->lay;
867                         else scene->lay= v3d->lay;
868                         
869                         RE_Database_FromScene(re, scene, 0);            // 0= dont use camera view
870                         scene->lay= lay;
871                         
872                         rstats= RE_GetStats(re);
873                         if(rstats->convertdone) 
874                                 ri->status |= PR_DBASE|PR_PROJECTED|PR_ROTATED;
875                         ri->curtile= 0;
876                         
877                         /* database can have created render-resol data... */
878                         if(rstats->convertdone) 
879                                 ED_anim_dag_flush_update(C); // <--- only current scene XXX
880                         
881                         //printf("dbase update\n");
882                 }
883                 if((ri->status & PR_PROJECTED)==0) {
884                         if(ri->status & PR_DBASE) {
885                                 if(orth)
886                                         RE_SetOrtho(ri->re, &viewplane, clipsta, clipend);
887                                 else
888                                         RE_SetWindow(ri->re, &viewplane, clipsta, clipend);
889                                 RE_DataBase_ApplyWindow(re);
890                                 ri->status |= PR_PROJECTED;
891                         }
892                         ri->curtile= 0;
893                         //printf("project update\n");
894                 }
895         
896                 /* OK, can we enter render code? */
897                 if(ri->status==(PR_DISPRECT|PR_DBASE|PR_PROJECTED|PR_ROTATED)) {
898                         //printf("curtile %d tottile %d\n", ri->curtile, ri->tottile);
899                         RE_TileProcessor(ri->re, ri->curtile, 0);
900         
901                         if(ri->rect==NULL)
902                                 ri->rect= MEM_mallocN(sizeof(int)*ri->pr_rectx*ri->pr_recty, "preview view3d rect");
903                         
904                         RE_ResultGet32(ri->re, ri->rect);
905                 }
906                 
907                 rstats= RE_GetStats(ri->re);
908 //              if(rstats->totpart==rstats->partsdone && rstats->partsdone)
909 //                      addqueue(sa->win, REDRAW, 1);
910 //              else
911 //                      addafterqueue(sa->win, RENDERPREVIEW, 1);
912                 
913                 ri->curtile= rstats->partsdone;
914                 ri->tottile= rstats->totpart;
915         }
916         else {
917 //              addafterqueue(sa->win, RENDERPREVIEW, 1);
918         }
919         
920         //printf("\n");
921 }
922
923 /* in panel space! */
924 static void view3d_previewdraw_rect(ScrArea *sa, uiBlock *block, RenderInfo *ri)
925 {
926 //      rctf dispf;
927         
928         if(ri->rect==NULL)
929                 return;
930         
931 //      BLI_init_rctf(&dispf, 15.0f, (block->maxx - block->minx)-15.0f, 15.0f, (block->maxy - block->miny)-15.0f);
932 //      ui_graphics_to_window_rct(sa->win, &dispf, &ri->disprect);
933
934         /* correction for gla draw */
935 //      BLI_translate_rcti(&ri->disprect, -sa->winrct.xmin, -sa->winrct.ymin);
936         
937         /* when panel scale changed, free rect */
938         if(ri->disprect.xmax-ri->disprect.xmin != ri->pr_rectx ||
939            ri->disprect.ymax-ri->disprect.ymin != ri->pr_recty) {
940                 MEM_freeN(ri->rect);
941                 ri->rect= NULL;
942         }
943         else {
944 //              glaDefine2DArea(&sa->winrct);
945                 glaDrawPixelsSafe(ri->disprect.xmin, ri->disprect.ymin, ri->pr_rectx, ri->pr_recty, ri->pr_rectx, GL_RGBA, GL_UNSIGNED_BYTE, ri->rect);
946         }       
947 }
948
949 /* is panel callback, supposed to be called with correct panel offset matrix */
950 void BIF_view3d_previewdraw(struct ScrArea *sa, struct uiBlock *block)
951 {
952         RegionView3D *rv3d= NULL;
953
954 //      if (v3d->ri==NULL || v3d->ri->rect==NULL) 
955 //              addafterqueue(sa->win, RENDERPREVIEW, 1);
956 //      else {
957                 view3d_previewdraw_rect(sa, block, rv3d->ri);
958 //              if(v3d->ri->curtile==0) 
959 //                      addafterqueue(sa->win, RENDERPREVIEW, 1);
960 //      }
961 }
962