Merging r39717 through r39983 from trunk into soc-2011-tomato
[blender.git] / source / blender / editors / space_view3d / view3d_draw.c
index 98768e3..7f04ca0 100644 (file)
@@ -63,6 +63,7 @@
 #include "BKE_scene.h"
 #include "BKE_screen.h"
 #include "BKE_unit.h"
+#include "BKE_movieclip.h"
 
 #include "RE_pipeline.h"       // make_stars
 
@@ -1240,6 +1241,24 @@ static void drawviewborder(Scene *scene, ARegion *ar, View3D *v3d)
                        uiSetRoundBox(15);
                        uiDrawBox(GL_LINE_LOOP, x1, y1, x2, y2, 12.0);
                }
+               if (ca && (ca->flag & CAM_SHOWSENSOR)) {
+                       /* assume fixed sensor width for now */
+                       float aspx = (float) scene->r.xsch*scene->r.xasp;
+                       float aspy = (float) scene->r.ysch*scene->r.yasp;
+                       float sensor_scale = (x2i-x1i) / ca->sensor_x;
+                       float sensor_height, ymid, sy1, sy2;
+
+                       if(aspx < aspy) sensor_height = ca->sensor_x * sensor_scale * aspx / aspy;
+                       else sensor_height = ca->sensor_x * sensor_scale * aspy / aspx;
+
+                       ymid = y1i + (y2i-y1i)/2.f;
+                       sy1= ymid - sensor_height/2.f;
+                       sy2= ymid + sensor_height/2.f;
+
+                       UI_ThemeColorShade(TH_WIRE, 100);
+
+                       uiDrawBox(GL_LINE_LOOP, x1i, sy1, x2i, sy2, 2.0);
+               }
        }
 
        setlinestyle(0);
@@ -1479,7 +1498,8 @@ static void draw_bgpic(Scene *scene, ARegion *ar, View3D *v3d)
        RegionView3D *rv3d= ar->regiondata;
        BGpic *bgpic;
        Image *ima;
-       ImBuf *ibuf= NULL;
+       MovieClip *clip;
+       ImBuf *ibuf= NULL, *freeibuf;
        float vec[4], fac, asp, zoomx, zoomy;
        float x1, y1, x2, y2, cx, cy;
 
@@ -1490,15 +1510,36 @@ static void draw_bgpic(Scene *scene, ARegion *ar, View3D *v3d)
                        (bgpic->view & (1<<rv3d->view)) || /* check agaist flags */
                        (rv3d->persp==RV3D_CAMOB && bgpic->view == (1<<RV3D_VIEW_CAMERA))
                ) {
-                       ima= bgpic->ima;
-                       if(ima==NULL)
-                               continue;
-                       BKE_image_user_calc_frame(&bgpic->iuser, CFRA, 0);
-                       ibuf= BKE_image_get_ibuf(ima, &bgpic->iuser);
-                       if(ibuf==NULL || (ibuf->rect==NULL && ibuf->rect_float==NULL) )
+                       freeibuf= NULL;
+                       if(bgpic->source==V3D_BGPIC_IMAGE) {
+                               ima= bgpic->ima;
+                               if(ima==NULL)
+                                       continue;
+                               BKE_image_user_calc_frame(&bgpic->iuser, CFRA, 0);
+                               ibuf= BKE_image_get_ibuf(ima, &bgpic->iuser);
+                       } else {
+                               clip= bgpic->clip;
+                               if(clip==NULL)
+                                       continue;
+                               BKE_movieclip_user_set_frame(&bgpic->cuser, CFRA);
+                               ibuf= BKE_movieclip_acquire_ibuf(clip, &bgpic->cuser);
+
+                               /* working with ibuf from image and clip has got different workflow now.
+                                  ibuf acquired from clip is referenced by cache system and should
+                                  be dereferenced after usage. */
+                               freeibuf= ibuf;
+                       }
+
+                       if(ibuf==NULL)
                                continue;
-                       if(ibuf->channels!=4)
+
+                       if((ibuf->rect==NULL && ibuf->rect_float==NULL) || ibuf->channels!=4) { /* invalid image format */
+                               if(freeibuf)
+                                       IMB_freeImBuf(freeibuf);
+
                                continue;
+                       }
+
                        if(ibuf->rect==NULL)
                                IMB_rect_from_float(ibuf);
 
@@ -1537,10 +1578,12 @@ static void draw_bgpic(Scene *scene, ARegion *ar, View3D *v3d)
 
                        /* complete clip? */
 
-                       if(x2 < 0 ) continue;
-                       if(y2 < 0 ) continue;
-                       if(x1 > ar->winx ) continue;
-                       if(y1 > ar->winy ) continue;
+                       if(x2 < 0 || y2 < 0 || x1 > ar->winx || y1 > ar->winy) {
+                               if(freeibuf)
+                                       IMB_freeImBuf(freeibuf);
+
+                               continue;
+                       }
 
                        zoomx= (x2-x1)/ibuf->x;
                        zoomy= (y2-y1)/ibuf->y;
@@ -1595,6 +1638,9 @@ static void draw_bgpic(Scene *scene, ARegion *ar, View3D *v3d)
 
                        glDepthMask(1);
                        if(v3d->zbuf) glEnable(GL_DEPTH_TEST);
+
+                       if(freeibuf)
+                               IMB_freeImBuf(freeibuf);
                }
        }
 }
@@ -2420,10 +2466,10 @@ ImBuf *ED_view3d_draw_offscreen_imbuf(Scene *scene, View3D *v3d, ARegion *ar, in
        /* render 3d view */
        if(rv3d->persp==RV3D_CAMOB && v3d->camera) {
                float winmat[4][4];
-               float _clipsta, _clipend, _lens, _yco, _dx, _dy;
+               float _clipsta, _clipend, _lens, _yco, _dx, _dy, _sensor_x=0;
                rctf _viewplane;
 
-               object_camera_matrix(&scene->r, v3d->camera, sizex, sizey, 0, winmat, &_viewplane, &_clipsta, &_clipend, &_lens, &_yco, &_dx, &_dy);
+               object_camera_matrix(&scene->r, v3d->camera, sizex, sizey, 0, winmat, &_viewplane, &_clipsta, &_clipend, &_lens, &_sensor_x, &_yco, &_dx, &_dy);
 
                ED_view3d_draw_offscreen(scene, v3d, ar, sizex, sizey, NULL, winmat);
        }
@@ -2478,9 +2524,9 @@ ImBuf *ED_view3d_draw_offscreen_imbuf_simple(Scene *scene, Object *camera, int w
        invert_m4_m4(rv3d.viewmat, rv3d.viewinv);
 
        {
-               float _yco, _dx, _dy;
+               float _yco, _dx, _dy, _sensor_x=0;
                rctf _viewplane;
-               object_camera_matrix(&scene->r, v3d.camera, width, height, 0, rv3d.winmat, &_viewplane, &v3d.near, &v3d.far, &v3d.lens, &_yco, &_dx, &_dy);
+               object_camera_matrix(&scene->r, v3d.camera, width, height, 0, rv3d.winmat, &_viewplane, &v3d.near, &v3d.far, &v3d.lens, &_sensor_x, &_yco, &_dx, &_dy);
        }
 
        mul_m4_m4m4(rv3d.persmat, rv3d.viewmat, rv3d.winmat);