Two in one:
[blender.git] / source / blender / src / renderwin.c
index 224f5859652bbe49aa1dfd8edb3d12f764db0638..8ca96f89936090edb891d19bfb1e88366ce115ea 100644 (file)
@@ -63,6 +63,7 @@
 #include "BMF_Api.h"
 
 #include "DNA_image_types.h"
+#include "DNA_space_types.h"
 #include "DNA_screen_types.h"
 #include "DNA_scene_types.h"
 #include "DNA_view3d_types.h"
@@ -88,6 +89,7 @@
 #include "BIF_toolbox.h"
 #include "BIF_writeimage.h"
 
+#include "BDR_sculptmode.h"
 #include "BDR_editobject.h"
 #include "BPY_extern.h" /* for BPY_do_all_scripts */
 
@@ -143,7 +145,7 @@ typedef struct {
        int active;
        short storespare, showspare;
        
-       int mbut[3];
+       int mbut[5];
        int lmouse[2];
        
        unsigned int flags;
@@ -174,7 +176,7 @@ static RenderWin *renderwin_alloc(Window *win)
        rw->render_text_spare= MEM_callocN(RW_MAXTEXT, "rendertext spare");
 
        rw->lmouse[0]= rw->lmouse[1]= 0;
-       rw->mbut[0]= rw->mbut[1]= rw->mbut[2]= 0;
+       rw->mbut[0]= rw->mbut[1]= rw->mbut[2]= rw->mbut[3] = rw->mbut[4] = 0;
 
        return rw;
 }
@@ -398,7 +400,24 @@ static void renderwin_draw(RenderWin *rw, int just_clear)
        if (set_back_mainwindow) mainwindow_make_active();      
 }
 
+
 /* ------ interactivity calls for RenderWin ------------- */
+static void renderwin_zoom(RenderWin *rw, int ZoomIn) {
+       if (ZoomIn) {
+               if (rw->zoom>0.26) {
+                       if(rw->zoom>1.0 && rw->zoom<2.0) rw->zoom= 1.0;
+                       else rw->zoom*= 0.5;
+               }
+       } else {
+               if (rw->zoom<15.9) {
+                       if(rw->zoom>0.5 && rw->zoom<1.0) rw->zoom= 1.0;
+                       else rw->zoom*= 2.0;
+               }
+       }
+       if (rw->zoom>1.0) rw->flags |= RW_FLAGS_OLDZOOM;
+       if (rw->zoom==1.0) rw->flags &= ~RW_FLAGS_OLDZOOM;
+       renderwin_queue_redraw(rw);
+}
 
 static void renderwin_mouse_moved(RenderWin *rw)
 {
@@ -469,8 +488,13 @@ static void renderwin_mousebut_changed(RenderWin *rw)
                rw->pan_mouse_start[1]= rw->lmouse[1];
                rw->pan_ofs_start[0]= rw->zoomofs[0];
                rw->pan_ofs_start[1]= rw->zoomofs[1];
-       } 
-       else {
+       } else if (rw->mbut[3]) {
+               renderwin_zoom(rw, 0);
+               rw->mbut[3]=0;
+       } else if (rw->mbut[4]) {
+               renderwin_zoom(rw, 1);
+               rw->mbut[4]=0;
+       } else {
                if (rw->flags & RW_FLAGS_PANNING) {
                        rw->flags &= ~RW_FLAGS_PANNING;
                        renderwin_queue_redraw(rw);
@@ -517,6 +541,11 @@ static void renderwin_handler(Window *win, void *user_data, short evt, short val
                rw->lmouse[evt==MOUSEY]= val;
                renderwin_mouse_moved(rw);
        } 
+       else if (ELEM(evt, WHEELUPMOUSE, WHEELDOWNMOUSE)) {
+               int which=(evt==WHEELUPMOUSE?3:4); 
+               rw->mbut[which]=val;
+               renderwin_mousebut_changed(rw);
+       }
        else if (ELEM3(evt, LEFTMOUSE, MIDDLEMOUSE, RIGHTMOUSE)) {
                int which= (evt==LEFTMOUSE)?0:(evt==MIDDLEMOUSE)?1:2;
                rw->mbut[which]= val;
@@ -552,19 +581,11 @@ static void renderwin_handler(Window *win, void *user_data, short evt, short val
                                renderwin_mouse_moved(rw);
                        }
                } 
-               else if (evt==PADPLUSKEY) {
-                       if (rw->zoom<15.9) {
-                               if(rw->zoom>0.5 && rw->zoom<1.0) rw->zoom= 1.0;
-                               else rw->zoom*= 2.0;
-                               renderwin_queue_redraw(rw);
-                       }
+               else if (ELEM(evt,PADPLUSKEY,PAGEUPKEY))  {
+                       renderwin_zoom(rw, 0);
                } 
-               else if (evt==PADMINUS) {
-                       if (rw->zoom>0.26) {
-                               if(rw->zoom>1.0 && rw->zoom<2.0) rw->zoom= 1.0;
-                               else rw->zoom*= 0.5;
-                               renderwin_queue_redraw(rw);
-                       }
+               else if (ELEM(evt,PADMINUS,PAGEDOWNKEY)) {
+                       renderwin_zoom(rw, 1);
                } 
                else if (evt==PADENTER || evt==HOMEKEY) {
                        if (rw->flags&RW_FLAGS_OLDZOOM) {
@@ -578,7 +599,7 @@ static void renderwin_handler(Window *win, void *user_data, short evt, short val
                                mainwindow_make_active();
                                rw->active= 0;
                                areawinset(find_biggest_area()->win);
-                               BIF_save_rendered_image_fs(0);
+                               BIF_save_rendered_image_fs();
                        }
                } 
                else if (evt==F11KEY) {
@@ -841,8 +862,7 @@ static void renderwin_progress(RenderWin *rw, RenderResult *rr, volatile rcti *r
 #ifdef __APPLE__
        window_swap_buffers(render_win->win);
 #else
-       /* no glFlush(); here... threads render hates it! */
-       glFinish();
+       glFlush();
        glDrawBuffer(GL_BACK);
 #endif 
 }
@@ -913,8 +933,7 @@ static void renderwin_renderinfo_cb(RenderStats *rs)
 #ifdef __APPLE__
                window_swap_buffers(render_win->win);
 #else
-               /* no glFlush(); here... threads render hates it! */
-               glFinish();
+               glFlush();
                glDrawBuffer(GL_BACK);
 #endif
        }
@@ -1016,8 +1035,7 @@ static void end_test_break_callback()
 {
        struct itimerval tmevalue;
 
-       tmevalue.it_value.tv_sec = 0;
-       tmevalue.it_value.tv_usec = 0;
+       memset(&tmevalue, 0, sizeof(struct itimerval));
 
        setitimer(ITIMER_REAL, &tmevalue, 0);
        signal(SIGALRM, SIG_IGN);
@@ -1039,9 +1057,11 @@ static void end_test_break_callback()
 
 static void do_render(int anim)
 {
+       Image *ima;
        Render *re= RE_NewRender(G.scene->id.name);
        unsigned int lay= G.scene->lay;
        int scemode= G.scene->r.scemode;
+       int sculptmode= G.f & G_SCULPTMODE;
        
        /* UGLY! we set this flag to prevent renderwindow queue to execute another render */
        /* is reset in RE_BlenderFrame */
@@ -1057,6 +1077,8 @@ static void do_render(int anim)
        if(G.obedit)
                exit_editmode(0);       /* 0 = no free data */
 
+       if(sculptmode) set_sculptmode();
+
        /* allow localview render for objects with lights in normal layers */
        if(curarea->spacetype==SPACE_VIEW3D) {
                if(G.vd->lay & 0xFF000000) {
@@ -1089,20 +1111,29 @@ static void do_render(int anim)
                
        scene_update_for_newframe(G.scene, G.scene->lay);       // no redraw needed, this restores to view as we left it
        
+       /* get a render result image, and make sure it is clean */
+       ima= BKE_image_verify_viewer(IMA_TYPE_R_RESULT, "Render Result");
+       BKE_image_signal(ima, NULL, IMA_SIGNAL_FREE);
+       
+       if(sculptmode) set_sculptmode();
+       
        waitcursor(0);
 }
 
+/* called before render, store old render in spare buffer */
 static void renderwin_store_spare(void)
 {
        RenderResult rres;
        
        if(render_win==0 || render_win->storespare==0)
                return;
-
-       if(render_win->showspare) {
-               render_win->showspare= 0;
-               window_set_title(render_win->win, renderwin_get_title(1));
-       }
+       
+       /* only store when it does not show spare */
+       if(render_win->showspare==0)
+               return;
+       
+       render_win->showspare= 0;
+       window_set_title(render_win->win, renderwin_get_title(1));
        
        BLI_strncpy(render_win->render_text_spare, render_win->render_text, RW_MAXTEXT);
        
@@ -1174,6 +1205,14 @@ void BIF_end_render_callbacks(void)
        }
 }
 
+void BIF_store_spare(void)
+{
+       if(render_win)
+               renderwin_store_spare();
+       else
+               imagewin_store_spare();
+}
+
 /* set up display, render an image or scene */
 void BIF_do_render(int anim)
 {
@@ -1187,8 +1226,7 @@ void BIF_do_render(int anim)
                }
        }
        
-       if(render_win && render_win->showspare)
-               renderwin_store_spare();
+       BIF_store_spare();
 
        do_render(anim);
 
@@ -1254,7 +1292,7 @@ void BIF_do_ogl_render(View3D *v3d, int anim)
                                char name[FILE_MAXDIR+FILE_MAXFILE];
                                int ok;
                                
-                               BKE_makepicstring(name, (G.scene->r.cfra));
+                               BKE_makepicstring(name, G.scene->r.pic, G.scene->r.cfra, G.scene->r.imtype);
 
                                ibuf->rect= rr->rect32;    
                                ok= BKE_write_ibuf(ibuf, name, G.scene->r.imtype, G.scene->r.subimtype, G.scene->r.quality);
@@ -1304,12 +1342,7 @@ void BIF_redraw_render_rect(void)
                renderwin_queue_redraw(render_win);
        }
        else {
-               Image *ima = (Image *)find_id("IM", "Render Result");
-               if(ima && ima->ibuf) {
-                       IMB_freeImBuf(ima->ibuf);
-                       ima->ibuf= NULL;
-                       allqueue(REDRAWIMAGE, 0);
-               }
+               allqueue(REDRAWIMAGE, 0);
        }
 }      
 
@@ -1317,22 +1350,26 @@ void BIF_swap_render_rects(void)
 {
        RenderResult rres;
        
-       if (render_win==NULL) return;
-       
-       render_win->storespare= 1;
-       render_win->showspare ^= 1;
+       if(G.displaymode!=R_DISPLAYWIN) {
+               imagewindow_swap_render_rects();
+       }
+       else if(render_win) {
                
-       RE_GetResultImage(RE_GetRender(G.scene->id.name), &rres);
+               render_win->storespare= 1;
+               render_win->showspare ^= 1;
+                       
+               RE_GetResultImage(RE_GetRender(G.scene->id.name), &rres);
+                       
+               if(render_win->sparex!=rres.rectx || render_win->sparey!=rres.recty) {
+                       if(render_win->rectspare) MEM_freeN(render_win->rectspare);
+                       render_win->rectspare= NULL;
+                       if(render_win->rectsparef) MEM_freeN(render_win->rectsparef);
+                       render_win->rectsparef= NULL;
+               }
                
-       if(render_win->sparex!=rres.rectx || render_win->sparey!=rres.recty) {
-               if(render_win->rectspare) MEM_freeN(render_win->rectspare);
-               render_win->rectspare= NULL;
-               if(render_win->rectsparef) MEM_freeN(render_win->rectsparef);
-               render_win->rectsparef= NULL;
+               window_set_title(render_win->win, renderwin_get_title(1));
        }
        
-       window_set_title(render_win->win, renderwin_get_title(1));
-
        /* redraw */
        BIF_redraw_render_rect();