Merging r40265 through r40492 from trunk into soc-2011-tomato
[blender.git] / source / blender / blenkernel / intern / object.c
index 90c5ac149a13ab8b229fb02f785f80f13c422ef7..57c47bb63e1c03b8b83bcaa43662d3817709fe52 100644 (file)
@@ -54,6 +54,7 @@
 #include "DNA_scene_types.h"
 #include "DNA_screen_types.h"
 #include "DNA_sequence_types.h"
+#include "DNA_sound_types.h"
 #include "DNA_space_types.h"
 #include "DNA_view3d_types.h"
 #include "DNA_world_types.h"
@@ -96,6 +97,7 @@
 #include "BKE_sca.h"
 #include "BKE_scene.h"
 #include "BKE_sequencer.h"
+#include "BKE_speaker.h"
 #include "BKE_softbody.h"
 #include "BKE_material.h"
 
@@ -233,6 +235,17 @@ void object_free_display(Object *ob)
        freedisplist(&ob->disp);
 }
 
+void free_sculptsession_deformMats(SculptSession *ss)
+{
+       if(ss->orig_cos) MEM_freeN(ss->orig_cos);
+       if(ss->deform_cos) MEM_freeN(ss->deform_cos);
+       if(ss->deform_imats) MEM_freeN(ss->deform_imats);
+
+       ss->orig_cos = NULL;
+       ss->deform_cos = NULL;
+       ss->deform_imats = NULL;
+}
+
 void free_sculptsession(Object *ob)
 {
        if(ob && ob->sculpt) {
@@ -263,6 +276,7 @@ void free_sculptsession(Object *ob)
        }
 }
 
+
 /* do not free object itself */
 void free_object(Object *ob)
 {
@@ -404,7 +418,7 @@ void unlink_object(Object *ob)
                                                for (ct= targets.first; ct; ct= ct->next) {
                                                        if (ct->tar == ob) {
                                                                ct->tar = NULL;
-                                                               strcpy(ct->subtarget, "");
+                                                               ct->subtarget[0]= '\0';
                                                                obt->recalc |= OB_RECALC_DATA;
                                                        }
                                                }
@@ -434,7 +448,7 @@ void unlink_object(Object *ob)
                                for (ct= targets.first; ct; ct= ct->next) {
                                        if (ct->tar == ob) {
                                                ct->tar = NULL;
-                                               strcpy(ct->subtarget, "");
+                                               ct->subtarget[0]= '\0';
                                                obt->recalc |= OB_RECALC_DATA;
                                        }
                                }
@@ -712,6 +726,7 @@ void *add_camera(const char *name)
        cam=  alloc_libblock(&G.main->camera, ID_CA, name);
 
        cam->lens= 35.0f;
+       cam->sensor_x = 32.f;
        cam->clipsta= 0.1f;
        cam->clipend= 100.0f;
        cam->drawsize= 0.5f;
@@ -989,6 +1004,7 @@ static void *add_obdata_from_type(int type)
        case OB_LAMP: return add_lamp("Lamp");
        case OB_LATTICE: return add_lattice("Lattice");
        case OB_ARMATURE: return add_armature("Armature");
+       case OB_SPEAKER: return add_speaker("Speaker");
        case OB_EMPTY: return NULL;
        default:
                printf("add_obdata_from_type: Internal error, bad type: %d\n", type);
@@ -1008,6 +1024,7 @@ static const char *get_obdata_defname(int type)
        case OB_LAMP: return "Lamp";
        case OB_LATTICE: return "Lattice";
        case OB_ARMATURE: return "Armature";
+       case OB_SPEAKER: return "Speaker";
        case OB_EMPTY: return "Empty";
        default:
                printf("get_obdata_defname: Internal error, bad type: %d\n", type);
@@ -1051,7 +1068,7 @@ Object *add_only_object(int type, const char *name)
        ob->empty_drawtype= OB_PLAINAXES;
        ob->empty_drawsize= 1.0;
 
-       if(type==OB_CAMERA || type==OB_LAMP) {
+       if(type==OB_CAMERA || type==OB_LAMP || type==OB_SPEAKER) {
                ob->trackflag= OB_NEGZ;
                ob->upflag= OB_POSY;
        }
@@ -1294,6 +1311,37 @@ static void copy_object_pose(Object *obn, Object *ob)
        }
 }
 
+static int object_pose_context(Object *ob)
+{
+       if(     (ob) &&
+               (ob->type == OB_ARMATURE) &&
+               (ob->pose) &&
+               (ob->mode & OB_MODE_POSE)
+       ) {
+               return 1;
+       }
+       else {
+               return 0;
+       }
+}
+
+//Object *object_pose_armature_get(Object *ob)
+Object *object_pose_armature_get(struct Object *ob)
+{
+       if(ob==NULL)
+               return NULL;
+
+       if(object_pose_context(ob))
+               return ob;
+
+       ob= modifiers_isDeformedByArmature(ob);
+
+       if(object_pose_context(ob))
+               return ob;
+
+       return NULL;
+}
+
 static void copy_object_transform(Object *ob_tar, Object *ob_src)
 {
        copy_v3_v3(ob_tar->loc, ob_src->loc);
@@ -2085,7 +2133,7 @@ void where_is_object_time(Scene *scene, Object *ob, float ctime)
        if(ob==NULL) return;
        
        /* execute drivers only, as animation has already been done */
-       BKE_animsys_evaluate_animdata(&ob->id, ob->adt, ctime, ADT_RECALC_DRIVERS);
+       BKE_animsys_evaluate_animdata(scene, &ob->id, ob->adt, ctime, ADT_RECALC_DRIVERS);
        
        if(ob->parent) {
                Object *par= ob->parent;
@@ -2143,6 +2191,39 @@ void where_is_object_time(Scene *scene, Object *ob, float ctime)
        else                                                    ob->transflag &= ~OB_NEG_SCALE;
 }
 
+/* get object transformation matrix without recalculating dependencies and
+   constraints -- assume dependencies are already solved by depsgraph.
+   no changes to object and it's parent would be done.
+   used for bundles orientation in 3d space relative to parented blender camera */
+void where_is_object_mat(Scene *scene, Object *ob, float obmat[4][4])
+{
+       float *fp1, *fp2, slowmat[4][4] = MAT4_UNITY;
+       float fac1, fac2;
+       int a;
+
+       if(ob->parent) {
+               Object *par= ob->parent;
+
+               solve_parenting(scene, ob, par, obmat, slowmat, 1);
+
+               if(ob->partype & PARSLOW) {
+                       // include framerate
+                       fac1= ( 1.0f / (1.0f + (float)fabs(give_timeoffset(ob))) );
+                       if(fac1 >= 1.0f) return;
+                       fac2= 1.0f-fac1;
+
+                       fp1= obmat[0];
+                       fp2= slowmat[0];
+                       for(a=0; a<16; a++, fp1++, fp2++) {
+                               fp1[0]= fac1*fp1[0] + fac2*fp2[0];
+                       }
+               }
+       }
+       else {
+               object_to_mat4(ob, obmat);
+       }
+}
+
 static void solve_parenting (Scene *scene, Object *ob, Object *par, float obmat[][4], float slowmat[][4], int simul)
 {
        float totmat[4][4];
@@ -2303,7 +2384,7 @@ BoundBox *unit_boundbox(void)
        BoundBox *bb;
        float min[3] = {-1.0f,-1.0f,-1.0f}, max[3] = {-1.0f,-1.0f,-1.0f};
 
-       bb= MEM_callocN(sizeof(BoundBox), "bb");
+       bb= MEM_callocN(sizeof(BoundBox), "OB-BoundBox");
        boundbox_set_from_min_max(bb, min, max);
        
        return bb;
@@ -2388,24 +2469,42 @@ void object_set_dimensions(Object *ob, const float *value)
 void minmax_object(Object *ob, float *min, float *max)
 {
        BoundBox bb;
-       Mesh *me;
-       Curve *cu;
        float vec[3];
        int a;
+       short change= FALSE;
        
        switch(ob->type) {
-               
        case OB_CURVE:
        case OB_FONT:
        case OB_SURF:
-               cu= ob->data;
-               
-               if(cu->bb==NULL) tex_space_curve(cu);
-               bb= *(cu->bb);
-               
-               for(a=0; a<8; a++) {
-                       mul_m4_v3(ob->obmat, bb.vec[a]);
-                       DO_MINMAX(bb.vec[a], min, max);
+               {
+                       Curve *cu= ob->data;
+
+                       if(cu->bb==NULL) tex_space_curve(cu);
+                       bb= *(cu->bb);
+
+                       for(a=0; a<8; a++) {
+                               mul_m4_v3(ob->obmat, bb.vec[a]);
+                               DO_MINMAX(bb.vec[a], min, max);
+                       }
+                       change= TRUE;
+               }
+               break;
+       case OB_LATTICE:
+               {
+                       Lattice *lt= ob->data;
+                       BPoint *bp= lt->def;
+                       int u, v, w;
+
+                       for(w=0; w<lt->pntsw; w++) {
+                               for(v=0; v<lt->pntsv; v++) {
+                                       for(u=0; u<lt->pntsu; u++, bp++) {
+                                               mul_v3_m4v3(vec, ob->obmat, bp->vec);
+                                               DO_MINMAX(vec, min, max);
+                                       }
+                               }
+                       }
+                       change= TRUE;
                }
                break;
        case OB_ARMATURE:
@@ -2417,25 +2516,27 @@ void minmax_object(Object *ob, float *min, float *max)
                                mul_v3_m4v3(vec, ob->obmat, pchan->pose_tail);
                                DO_MINMAX(vec, min, max);
                        }
-                       break;
+                       change= TRUE;
                }
-               /* no break, get_mesh will give NULL and it passes on to default */
+               break;
        case OB_MESH:
-               me= get_mesh(ob);
-               
-               if(me) {
-                       bb = *mesh_get_bb(ob);
-                       
-                       for(a=0; a<8; a++) {
-                               mul_m4_v3(ob->obmat, bb.vec[a]);
-                               DO_MINMAX(bb.vec[a], min, max);
+               {
+                       Mesh *me= get_mesh(ob);
+
+                       if(me) {
+                               bb = *mesh_get_bb(ob);
+
+                               for(a=0; a<8; a++) {
+                                       mul_m4_v3(ob->obmat, bb.vec[a]);
+                                       DO_MINMAX(bb.vec[a], min, max);
+                               }
+                               change= TRUE;
                        }
                }
-               if(min[0] < max[0] ) break;
-               
-               /* else here no break!!!, mesh can be zero sized */
-               
-       default:
+               break;
+       }
+
+       if(change == FALSE) {
                DO_MINMAX(ob->obmat[3], min, max);
 
                copy_v3_v3(vec, ob->obmat[3]);
@@ -2445,7 +2546,6 @@ void minmax_object(Object *ob, float *min, float *max)
                copy_v3_v3(vec, ob->obmat[3]);
                sub_v3_v3(vec, ob->size);
                DO_MINMAX(vec, min, max);
-               break;
        }
 }
 
@@ -2605,7 +2705,7 @@ void object_handle_update(Scene *scene, Object *ob)
                        if(adt) {
                                /* evaluate drivers */
                                // XXX: for mesh types, should we push this to derivedmesh instead?
-                               BKE_animsys_evaluate_animdata(data_id, adt, ctime, ADT_RECALC_DRIVERS);
+                               BKE_animsys_evaluate_animdata(scene, data_id, adt, ctime, ADT_RECALC_DRIVERS);
                        }
 
                        /* includes all keys and modifiers */
@@ -2623,11 +2723,12 @@ void object_handle_update(Scene *scene, Object *ob)
 
 #else                          /* ensure CD_MASK_BAREMESH for now */
                                        EditMesh *em = (ob == scene->obedit)? BKE_mesh_get_editmesh(ob->data): NULL;
+                                       unsigned int data_mask= scene->customdata_mask | ob->customdata_mask | CD_MASK_BAREMESH;
                                        if(em) {
-                                               makeDerivedMesh(scene, ob, em,  scene->customdata_mask | CD_MASK_BAREMESH); /* was CD_MASK_BAREMESH */
+                                               makeDerivedMesh(scene, ob, em,  data_mask); /* was CD_MASK_BAREMESH */
                                                BKE_mesh_end_editmesh(ob->data, em);
                                        } else
-                                               makeDerivedMesh(scene, ob, NULL, scene->customdata_mask | CD_MASK_BAREMESH);
+                                               makeDerivedMesh(scene, ob, NULL, data_mask);
 #endif
 
                                }
@@ -2735,6 +2836,33 @@ void object_handle_update(Scene *scene, Object *ob)
        }
 }
 
+void object_sculpt_modifiers_changed(Object *ob)
+{
+       SculptSession *ss= ob->sculpt;
+
+       if(!ss->cache) {
+               /* we free pbvh on changes, except during sculpt since it can't deal with
+                  changing PVBH node organization, we hope topology does not change in
+                  the meantime .. weak */
+               if(ss->pbvh) {
+                               BLI_pbvh_free(ss->pbvh);
+                               ss->pbvh= NULL;
+               }
+
+               free_sculptsession_deformMats(ob->sculpt);
+       } else {
+               PBVHNode **nodes;
+               int n, totnode;
+
+               BLI_pbvh_search_gather(ss->pbvh, NULL, NULL, &nodes, &totnode);
+
+               for(n = 0; n < totnode; n++)
+                       BLI_pbvh_node_mark_update(nodes[n]);
+
+               MEM_freeN(nodes);
+       }
+}
+
 float give_timeoffset(Object *ob) {
        if ((ob->ipoflag & OB_OFFS_PARENTADD) && ob->parent) {
                return ob->sf + give_timeoffset(ob->parent);
@@ -2853,7 +2981,7 @@ void object_camera_mode(RenderData *rd, Object *camera)
 /* 'lens' may be set for envmap only */
 void object_camera_matrix(
                RenderData *rd, Object *camera, int winx, int winy, short field_second,
-               float winmat[][4], rctf *viewplane, float *clipsta, float *clipend, float *lens, float *ycor,
+               float winmat[][4], rctf *viewplane, float *clipsta, float *clipend, float *lens, float *sensor_x, float *ycor,
                float *viewdx, float *viewdy
 ) {
        Camera *cam=NULL;
@@ -2886,6 +3014,7 @@ void object_camera_matrix(
                shiftx=cam->shiftx;
                shifty=cam->shifty;
                (*lens)= cam->lens;
+               (*sensor_x)= cam->sensor_x;
                (*clipsta)= cam->clipsta;
                (*clipend)= cam->clipend;
        }
@@ -2901,7 +3030,7 @@ void object_camera_matrix(
                (*clipend)= la->clipend;
        }
        else {  /* envmap exception... */;
-               if((*lens)==0.0f)
+               if((*lens)==0.0f) /* is this needed anymore? */
                        (*lens)= 16.0f;
 
                if((*clipsta)==0.0f || (*clipend)==0.0f) {
@@ -2922,8 +3051,8 @@ void object_camera_matrix(
                pixsize= cam->ortho_scale/viewfac;
        }
        else {
-               if(rd->xasp*winx >= rd->yasp*winy)      viewfac= ((*lens) * winx)/32.0f;
-               else                                                            viewfac= (*ycor) * ((*lens) * winy)/32.0f;
+               if(rd->xasp*winx >= rd->yasp*winy)      viewfac= ((*lens) * winx) / (*sensor_x);
+               else                                    viewfac= (*ycor) * ((*lens) * winy) / (*sensor_x);
                pixsize= (*clipsta) / viewfac;
        }
 
@@ -3115,7 +3244,7 @@ int object_is_modified(Scene *scene, Object *ob)
        int flag= 0;
 
        if(ob_get_key(ob)) {
-               flag |= eModifierMode_Render | eModifierMode_Render;
+               flag |= eModifierMode_Render;
        }
        else {
                ModifierData *md;