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